Commits

Michael Ludwig committed 963994a

Move aabb extent computing into a public method on Vector3 because that is the result type.

  • Participants
  • Parent commits 78a749a

Comments (0)

Files changed (2)

ferox-math/src/main/java/com/ferox/math/Vector3.java

     public Vector3 normalize() {
         return normalize(this);
     }
+    
+    /**
+     * Compute the corner of the box that is most extended along the given
+     * direction vector and store it in this vector. This will return one of the
+     * 8 possible corners of the box depending on the signs of the vector, it
+     * will not return points along the edge or face of the box.
+     * 
+     * @param bounds The bounds
+     * @param dir The query direction vector
+     * @return This vector, updated to hold the far extent
+     * @throws NullPointerException if bounds or dir is null
+     */
+    public Vector3 farExtent(@Const AxisAlignedBox bounds, @Const Vector3 dir) {
+        return extent(bounds.min, bounds.max, dir);
+    }
+    
+    /**
+     * Compute the corner of the box that is least extended along the given
+     * direction vector and store it in this vector. This is equivalent to
+     * calling {@link #farExtent(AxisAlignedBox, Vector3)} with a negated
+     * direction vector but avoids computing the negation.
+     * 
+     * @param bounds The bounds
+     * @param dir The query direction vector
+     * @return This vector, updated to hold the near extent
+     * @throws NullPointerException if bounds or dir is null
+     */
+    public Vector3 nearExtent(@Const AxisAlignedBox bounds, @Const Vector3 dir) {
+        return extent(bounds.max, bounds.min, dir);
+    }
+    
+    private Vector3 extent(@Const Vector3 min, @Const Vector3 max, @Const Vector3 dir) {
+        x = (dir.x > 0 ? max.x : min.x);
+        y = (dir.y > 0 ? max.y : min.y);
+        z = (dir.z > 0 ? max.z : min.z);
+        return this;
+    }
 
     /**
      * Set the vector coordinate at index to the given value. index must be one

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

         for (int i = Frustum.NUM_PLANES - 1; i >= 0; i--) {
             if (planeState == null || planeState.isTestRequired(i)) {
                 p = getFrustumPlane(plane);
-                extent(bounds, p, false, temp);
+                // set temp to the normal of the plane then compute the extent
+                // in-place; this is safe but we'll have to reset temp to the
+                // normal later if needed
+                temp.set(p.x, p.y, p.z).farExtent(bounds, temp);
                 distMax = Plane.getSignedDistance(p, temp, true);
                 
                 if (distMax < 0) {
                 } else {
                     // the point closest to the plane is in front of the plane,
                     // but we need to check the farthest away point
-
-                    extent(bounds, p, true, temp);
+                    
+                    // make sure to reset temp to the normal before computing
+                    // the near extent in-place
+                    temp.set(p.x, p.y, p.z).nearExtent(bounds, temp);
                     distMin = Plane.getSignedDistance(p, temp, true);
                     
                     if (distMin < 0) {
         return result;
     }
     
-    private void extent(@Const AxisAlignedBox bounds, @Const Vector4 plane, boolean reverseDir, Vector3 result) {
-        Vector3 sourceMin = (reverseDir ? bounds.max : bounds.min);
-        Vector3 sourceMax = (reverseDir ? bounds.min : bounds.max);
-        
-        if (plane.x > 0) {
-            if (plane.y > 0) {
-                if (plane.z > 0)
-                    result.set(sourceMax.x, sourceMax.y, sourceMax.z);
-                else
-                    result.set(sourceMax.x, sourceMax.y, sourceMin.z);
-            } else {
-                if (plane.z > 0)
-                    result.set(sourceMax.x, sourceMin.y, sourceMax.z);
-                else
-                    result.set(sourceMax.x, sourceMin.y, sourceMin.z);
-            }
-        } else {
-            if (plane.y > 0) {
-                if (plane.z > 0)
-                    result.set(sourceMin.x, sourceMax.y, sourceMax.z);
-                else
-                    result.set(sourceMin.x, sourceMax.y, sourceMin.z);
-            } else {
-                if (plane.z > 0)
-                    result.set(sourceMin.x, sourceMin.y, sourceMax.z);
-                else
-                    result.set(sourceMin.x, sourceMin.y, sourceMin.z);
-            }
-        }
-    }
-    
     /*
      * Update the plane instances returned by getFrustumPlane() to reflect any
      * changes to the frustum's local parameters or orientation. Also update the