Michael Ludwig avatar Michael Ludwig committed 69f143a

Fix boundary cases in spatial index structures when bounds exactly equal maximum extent of index.

Comments (0)

Files changed (2)

ferox-math/src/main/java/com/ferox/math/bounds/Octree.java

 
     protected int hashCellX(@Const Vector3 v) {
         // we add widthOffset to the coordinate value to get values into a positive-only range
-        return (int) ((v.x + widthOffset) * widthScaleFactor);
+        return clamp((int) ((v.x + widthOffset) * widthScaleFactor));
     }
 
     protected int hashCellY(@Const Vector3 v) {
-        return (int) ((v.y + heightOffset) * heightScaleFactor);
+        return clamp((int) ((v.y + heightOffset) * heightScaleFactor));
     }
 
     protected int hashCellZ(@Const Vector3 v) {
-        return (int) ((v.z + depthOffset) * depthScaleFactor);
+        return clamp((int) ((v.z + depthOffset) * depthScaleFactor));
     }
 
     protected int hash(int cellX, int cellY, int cellZ) {
         return cellX + maxCellDimension * cellY + maxCellDimension * maxCellDimension * cellZ;
     }
 
+    protected int clamp(int discrete) {
+        return Math.max(0, Math.min(maxCellDimension - 1, discrete));
+    }
+
     private void updateBounds(AxisAlignedBox bounds, int index) {
         int realIndex = index * 6;
         bounds.min.set(aabbs, realIndex);

ferox-math/src/main/java/com/ferox/math/bounds/QuadTree.java

         }
 
         // hash x/z of bounds and do spatial hash query over intersecting cells
-        int minX = Math.max(0, hashCellX(bounds.min));
-        int minY = Math.max(0, hashCellY(bounds.min));
-        int maxX = Math.min(maxCellDimension - 1, hashCellX(bounds.max));
-        int maxY = Math.min(maxCellDimension - 1, hashCellY(bounds.max));
+        int minX = hashCellX(bounds.min);
+        int minY = hashCellY(bounds.min);
+        int maxX = hashCellX(bounds.max);
+        int maxY = hashCellY(bounds.max);
 
         int query = ++queryIdCounter;
         AxisAlignedBox itemBounds = new AxisAlignedBox();
 
     protected int hashCellX(@Const Vector3 v) {
         // we add widthOffset to the coordinate value to get values into a positive-only range
-        return (int) ((getFirstDimension(v) + widthOffset) * widthScaleFactor);
+        return clamp((int) ((getFirstDimension(v) + widthOffset) * widthScaleFactor));
     }
 
     protected int hashCellY(@Const Vector3 v) {
-        return (int) ((getSecondDimension(v) + heightOffset) * heightScaleFactor);
+        return clamp((int) ((getSecondDimension(v) + heightOffset) * heightScaleFactor));
+    }
+
+    protected int clamp(int discrete) {
+        return Math.max(0, Math.min(maxCellDimension - 1, discrete));
     }
 
     protected int hash(int cellX, int cellY) {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.