From c805caa553c5fd1d92c4b546200037c530e57a97 Mon Sep 17 00:00:00 2001 From: asofold Date: Fri, 1 Mar 2013 21:43:26 +0100 Subject: [PATCH] RayTracing: Add ability to check number of steps done. Now available in the abstract base class. --- .../nocheatplus/utilities/RayTracing.java | 48 +++++++++++++++++-- .../nocheatplus/test/TestRayTracing.java | 18 ++----- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/utilities/RayTracing.java b/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/utilities/RayTracing.java index 3823fddb..f0cbf43e 100644 --- a/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/utilities/RayTracing.java +++ b/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/utilities/RayTracing.java @@ -30,6 +30,12 @@ public abstract class RayTracing { /** Tolerance for time, for checking the abort condition: 1.0 - t <= tol . */ protected double tol = 0.0; + /** Counting the number of steps. Step is incremented before calling step(), and is 0 after set(...). Checking this from within step means to get the current step number, checking after loop gets the number of steps done. */ + protected int step = 0; + + /** Maximum steps that will be done. */ + private int maxSteps = Integer.MAX_VALUE; + public RayTracing(double x0, double y0, double z0, double x1, double y1, double z1){ set(x0, y0, z0, x1, y1, z1); } @@ -38,15 +44,22 @@ public abstract class RayTracing { set(0, 0, 0, 0, 0, 0); } + /** + * After this calling loop is possible. + * @param x0 + * @param y0 + * @param z0 + * @param x1 + * @param y1 + * @param z1 + */ public void set(double x0, double y0, double z0, double x1, double y1, double z1){ -// // TODO: Consider not using end-points at all. // this.x0 = x0; // this.y0 = y0; // this.z0 = z0; // this.x1 = x1; // this.y1 = y1; // this.z1 = z1; -// // Set the "runtime" info. // d = CheckUtils.distance(x0, y0, z0, x1, y1, z1); dX = x1 - x0; dY = y1 - y0; @@ -58,6 +71,7 @@ public abstract class RayTracing { oY = (double) (y0 - blockY); oZ = (double) (z0 - blockZ); t = 0.0; + step = 0; } private static final double tDiff(final double dTotal, final double offset){ @@ -89,6 +103,7 @@ public abstract class RayTracing { tMin = Math.min(tX, Math.min(tY, tZ)); if (tMin == Double.MAX_VALUE || t + tMin > 1.0) tMin = 1.0 - t; // Call step with appropriate arguments. + step ++; if (!step(blockX, blockY, blockZ, oX, oY, oZ, tMin)) break; // || tMin == 0) break; if (t + tMin >= 1.0 - tol) break; // Advance (add to t etc.). @@ -167,12 +182,39 @@ public abstract class RayTracing { changed = true; } t += tMin; - if (!changed){ + if (!changed || step >= maxSteps){ break; } } } + /** + * This is for external use. The field step will be incremented before + * step(...) is called, thus checking it from within step means to get the + * current step number, checking after loop gets the number of steps done. + * + * @return + */ + public int getStepsDone(){ + return step; + } + + /** + * Get the maximal number of steps that loop will do. + * @return + */ + public int getMaxSteps() { + return maxSteps; + } + + /** + * Set the maximal number of steps that loop will do. + * @return + */ + public void setMaxSteps(int maxSteps) { + this.maxSteps = maxSteps; + } + /** * One step in the loop. * @return If to continue loop. diff --git a/NCPCompat/src/test/java/fr/neatmonster/nocheatplus/test/TestRayTracing.java b/NCPCompat/src/test/java/fr/neatmonster/nocheatplus/test/TestRayTracing.java index 6a60caf9..86c817ce 100644 --- a/NCPCompat/src/test/java/fr/neatmonster/nocheatplus/test/TestRayTracing.java +++ b/NCPCompat/src/test/java/fr/neatmonster/nocheatplus/test/TestRayTracing.java @@ -25,24 +25,15 @@ public class TestRayTracing { public CountRayTracing(double x0, double y0, double z0, double x1, double y1, double z1) { super(x0, y0, z0, x1, y1, z1); } - - protected int done = 0; @Override protected boolean step(int blockX, int blockY, int blockZ, double oX, double oY, double oZ, double dT) { - done ++; - if (done > maxSteps(dX, dY, dZ)) { + if (step > maxSteps(dX, dY, dZ)) { System.out.println("[WARNING] Max steps exceeded: " + maxSteps(dX, dY, dZ)); return false; } return true; } - - public int loopCount() { - done = 0; - super.loop(); - return done; - } } public static double[] randomCoords(double max){ @@ -171,7 +162,8 @@ public class TestRayTracing { public static RayTracing checkNumberOfSteps(double[] coords, int steps) { CountRayTracing crt = new CountRayTracing(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]); - int done = crt.loopCount(); + crt.loop(); + int done = crt.getStepsDone(); if (done != steps) doFail("Wrong number of steps: " + done + " instead of " + steps, coords); return crt; } @@ -184,12 +176,10 @@ public class TestRayTracing { public static RayTracing dumpRawRayTracing(final double[] coords) { RayTracing rt = new RayTracing(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]) { - int done = 0; @Override protected boolean step(int blockX, int blockY, int blockZ, double oX, double oY, double oZ, double dT) { dump(blockX, blockY, blockZ, oX, oY, oZ, t, dT); - done ++; - if (done > maxSteps(dX, dY, dZ)){ + if (step > maxSteps(dX, dY, dZ)){ System.out.println("[WARNING] Max steps exceeded: " + maxSteps(dX, dY, dZ)); return false; }