diff --git a/src/main/java/com/syncleus/spangraph/spacetime/OctBox.java b/src/main/java/com/syncleus/spangraph/spacetime/OctBox.java index e5f5f0c6a10f2645f8ddfce9ab6e7c79dc29cc07..cd531f5cbd48e7eabca014d66670f6f4f1858fcf 100644 --- a/src/main/java/com/syncleus/spangraph/spacetime/OctBox.java +++ b/src/main/java/com/syncleus/spangraph/spacetime/OctBox.java @@ -9,7 +9,7 @@ import java.util.List; import java.util.function.Consumer; -public class OctBox<V extends XYZ> extends AABB implements Shape3D { +public class OctBox<V extends XYZ> extends BB implements Shape3D { /** * alternative tree recursion limit, number of world units when cells are @@ -17,9 +17,6 @@ public class OctBox<V extends XYZ> extends AABB implements Shape3D { */ protected Vec3D resolution; - /** - * - */ public final OctBox parent; protected OctBox[] children; @@ -83,9 +80,9 @@ public class OctBox<V extends XYZ> extends AABB implements Shape3D { } public boolean belowResolution() { - return extent.x <= resolution.x || - extent.y <= resolution.y || - extent.z <= resolution.z; + return extent.x() <= resolution.x || + extent.y() <= resolution.y || + extent.z() <= resolution.z; } /** * Adds a new point/particle to the tree structure. All points are stored @@ -111,12 +108,12 @@ public class OctBox<V extends XYZ> extends AABB implements Shape3D { if (children == null) { children = new OctBox[8]; } - int octant = getOctantID(p, min); + int octant = getOctantID(p); if (children[octant] == null) { - Vec3D off = min.add(new Vec3D( - (octant & 1) != 0 ? extent.x : 0, - (octant & 2) != 0 ? extent.y : 0, - (octant & 4) != 0 ? extent.z : 0)); + Vec3D off = new Vec3D( + minX() + ((octant & 1) != 0 ? extent.x() : 0), + minY() + ((octant & 2) != 0 ? extent.y() : 0), + minZ() + ((octant & 4) != 0 ? extent.z() : 0)); children[octant] = new OctBox(this, off, extent.scale(0.5f)); } @@ -179,7 +176,7 @@ public class OctBox<V extends XYZ> extends AABB implements Shape3D { // if not a leaf node... if (p.isInAABB(this)) { if (children!=null) { - int octant = getOctantID(p, this); + int octant = getOctantID(p); if (children[octant] != null) { return children[octant].getLeafForPoint(p); } @@ -212,20 +209,20 @@ public class OctBox<V extends XYZ> extends AABB implements Shape3D { * @return octant index */ protected final int getOctantID(final Vec3D plocal) { - final Vec3D h = this.extent; + final XYZ h = this.extent; - return (plocal.x >= h.x ? 1 : 0) + (plocal.y >= h.y ? 2 : 0) - + (plocal.z >= h.z ? 4 : 0); + return (plocal.x >= h.x() ? 1 : 0) + (plocal.y >= h.y() ? 2 : 0) + + (plocal.z >= h.z() ? 4 : 0); } /** computes getOctantID for the point subtracted by another point, * without needing to allocate a temporary object */ - private int getOctantID(final XYZ p, final Vec3D s) { - final Vec3D h = this.extent; - return ((p.x() - s.x) >= h.x ? 1 : 0) + ((p.y() - s.y) >= h.y? 2 : 0) - + ((p.z() - s.z) >= h.z ? 4 : 0); + private int getOctantID(final XYZ p) { + //final XYZ h = this.extent; + return ((p.x() - x) >= 0 ? 1 : 0) + ((p.y() - y) >= 0 ? 2 : 0) + + ((p.z() - z) >= 0 ? 4 : 0); } diff --git a/src/main/java/toxi/geom/AABB.java b/src/main/java/toxi/geom/AABB.java index a7468fe318b09cc333da0b06930e20c480223a31..4e752480dd58b52a256405c19b81ec9dae2ab22d 100644 --- a/src/main/java/toxi/geom/AABB.java +++ b/src/main/java/toxi/geom/AABB.java @@ -70,24 +70,23 @@ public class AABB extends BB implements Shape3D { * Creates a new instance from centre point and uniform extent in all * directions. * - * @param pos - * @param extent + * @param center + * @param extent half size, radius */ - public AABB(roVec3D pos, float extent) { - super(pos); - setExtent(new Vec3D(extent, extent, extent)); + public AABB(roVec3D center, float extent) { + this(center, new Vec3D(extent, extent, extent)); } /** * Creates a new instance from centre point and extent * - * @param pos + * @param center * @param extent * box dimensions (the box will be double the size in each * direction) */ - public AABB(XYZ pos, roVec3D extent) { - super(pos); + public AABB(XYZ center, Vec3D extent) { + super(center); setExtent(extent); } @@ -368,8 +367,8 @@ public class AABB extends BB implements Shape3D { * new box size * @return itself, for method chaining */ - public BB setExtent(roVec3D extent) { - this.extent = extent.copy(); + public BB setExtent(Vec3D extent) { + this.extent = extent; return updateBounds(); } diff --git a/src/main/java/toxi/geom/BB.java b/src/main/java/toxi/geom/BB.java index 231b467987ae33d3672d229078a298ca747093f4..1e25c4750d5fa73fa0d63cfa018ccb554a1cf951 100644 --- a/src/main/java/toxi/geom/BB.java +++ b/src/main/java/toxi/geom/BB.java @@ -21,6 +21,11 @@ public abstract class BB extends Vec3D { super(v); } + public BB(Vec3D center, Vec3D radius) { + super(center); + this.extent = (radius); + } + /** * Creates a new instance from two vectors specifying opposite corners of * the box @@ -159,11 +164,11 @@ public abstract class BB extends Vec3D { } - float minX() { return x - extent.x; } - float maxX() { return x + extent.x; } - float minY() { return y - extent.y; } - float maxY() { return y + extent.y; } - float minZ() { return z - extent.z; } - float maxZ() { return z + extent.z; } + public float minX() { return x - extent.x(); } + public float maxX() { return x + extent.x(); } + public float minY() { return y - extent.y(); } + public float maxY() { return y + extent.y(); } + public float minZ() { return z - extent.z(); } + public float maxZ() { return z + extent.z(); } } diff --git a/src/main/java/toxi/geom/Vec3D.java b/src/main/java/toxi/geom/Vec3D.java index 6d650ef1bd123654853ab5936e1e5bffa97e9ed1..99929221255e5ceec30b324644a2acb8f1f5aaad 100644 --- a/src/main/java/toxi/geom/Vec3D.java +++ b/src/main/java/toxi/geom/Vec3D.java @@ -920,13 +920,6 @@ public class Vec3D implements Comparable<roVec3D>, roVec3D { return this; } - public final float magnitude() { - return (float) Math.sqrt(x * x + y * y + z * z); - } - - public final float magSquared() { - return x * x + y * y + z * z; - } /** * Max self. diff --git a/src/main/java/toxi/geom/XYZ.java b/src/main/java/toxi/geom/XYZ.java index 7e61b40b646b7bb3b2b8503a182facc8015d4d34..5ade023f6f40fc07f23e6199dd58a6f8a264f32c 100644 --- a/src/main/java/toxi/geom/XYZ.java +++ b/src/main/java/toxi/geom/XYZ.java @@ -85,5 +85,17 @@ public interface XYZ { return box.contains(this); } + default public float magnitude() { + return (float) Math.sqrt(magSquared()); + } + + default public float magSquared() { + final float x = x(); + final float y = y(); + final float z = z(); + return x*x + y*y + z*z; + } + + default XYZ copy() { return new Vec3D(this); } } diff --git a/src/main/java/toxi/geom/roVec3D.java b/src/main/java/toxi/geom/roVec3D.java index ebe2f31b5006168ebe9612abc9bebc9c38077217..0bc43522bca73d3a9679b65e4174fb1b1ec84848 100644 --- a/src/main/java/toxi/geom/roVec3D.java +++ b/src/main/java/toxi/geom/roVec3D.java @@ -397,21 +397,7 @@ public interface roVec3D extends XYZ { */ public boolean isZeroVector(); - /** - * Calculates the magnitude/eucledian length of the vector. - * - * @return vector length - */ - public float magnitude(); - /** - * Calculates only the squared magnitude/length of the vector. Useful for - * inverse square law applications and/or for speed reasons or if the real - * eucledian distance is not required (e.g. sorting). - * - * @return squared magnitude (x^2 + y^2 + z^2) - */ - public float magSquared();