Class Box

  • All Implemented Interfaces:
    Extent, Renderable

    public class Box
    extends java.lang.Object
    implements Extent, Renderable
    An arbitrarily oriented box, typically used as a oriented bounding volume for a collection of points or shapes. A Box is defined by three orthogonal axes and two positions along each of those axes. Each of the positions specifies the location of a box side along the respective axis. The three axes are named by convention "R", "S" and "T", and are ordered by decreasing length -- R is the longest axis, followed by S and then T.

    The static class field, ProjectionHullTable, defines a table of all possible vertex combinations representing a Box's 2D convex hull in screen coordinates. The index to this table is a 6-bit code, where each bit denotes whether one of the Box's six planes faces the View. This code is organized as follows:

    Bit Mapping
    Bit543210
    Codeleftrightbackfrontbottomtop

    Since at most three of a Box's planes can be visible at one time, there are a total of 26 unique vertex combinations that define a Box's 2D convex hull in the viewport. Index codes that represent a valid combination of planes facing the View result in an array of 4 or 6 integers (depending on whether one, two or three planes face the View), where each element in the array is an index for one of the Box's eight vertices as follows:

    Index Mapping
    Index0 1234567
    Vertexbottom-lower-leftbottom-lower-right bottom-upper-rightbottom-upper-lefttop-lower-lefttop-lower-right top-upper-righttop-upper-left

    The vertices are organized so that they appear in counter-clockwise order on the screen. Index codes that represent an invalid combination of planes facing the View map to null.

    • Field Detail

      • ProjectionHullTable

        protected static final int[][] ProjectionHullTable
      • bottomCenter

        public Vec4 bottomCenter
      • topCenter

        public Vec4 topCenter
      • center

        protected final Vec4 center
      • r

        protected final Vec4 r
      • s

        protected final Vec4 s
      • t

        protected final Vec4 t
      • ru

        protected final Vec4 ru
      • su

        protected final Vec4 su
      • tu

        protected final Vec4 tu
      • rLength

        protected final double rLength
      • sLength

        protected final double sLength
      • tLength

        protected final double tLength
      • planes

        protected final Plane[] planes
    • Constructor Detail

      • Box

        public Box​(Vec4[] axes,
                   double rMin,
                   double rMax,
                   double sMin,
                   double sMax,
                   double tMin,
                   double tMax)
        Construct a box from three specified unit axes and the locations of the box faces relative to those axes. The box faces are specified by two scalar locations along each axis, each location indicating a face. The non-unit length of an axis is the distance between its respective two locations. The longest side is specified first, followed by the second longest side and then the shortest side.

        The axes are normally principal axes computed from a collection of points in order to form an oriented bounding volume. See WWMath.computePrincipalAxes(Iterable).

        Note: No check is made to ensure the order of the face locations.

        Parameters:
        axes - the unit-length axes.
        rMin - the location along the first axis corresponding to the left-most box side relative to the axis.
        rMax - the location along the first axis corresponding to the right-most box side relative to the axis.
        sMin - the location along the second axis corresponding to the left-most box side relative to the axis.
        sMax - the location along the second axis corresponding to the right-most box side relative to the axis.
        tMin - the location along the third axis corresponding to the left-most box side relative to the axis.
        tMax - the location along the third axis corresponding to the right-most box side relative to the axis.
        Throws:
        java.lang.IllegalArgumentException - if the axes array or one of its entries is null.
      • Box

        public Box​(Vec4 point)
        Construct a unit-length cube centered at a specified point.
        Parameters:
        point - the center of the cube.
        Throws:
        java.lang.IllegalArgumentException - if the point is null.
    • Method Detail

      • getCenter

        public Vec4 getCenter()
        Returns the box's center point.
        Specified by:
        getCenter in interface Extent
        Returns:
        the box's center point.
      • getBottomCenter

        public Vec4 getBottomCenter()
        Returns the point corresponding to the center of the box side left-most along the R (first) axis.
        Returns:
        the bottom-center point.
      • getTopCenter

        public Vec4 getTopCenter()
        Returns the point corresponding to the center of the box side right-most along the R (first) axis.
        Returns:
        the top-center point.
      • getRAxis

        public Vec4 getRAxis()
        Returns the R (first) axis. The axis length is the distance between the box sides perpendicular to the axis.
        Returns:
        the R axis.
      • getSAxis

        public Vec4 getSAxis()
        Returns the S (second) axis. The axis length is the distance between the box sides perpendicular to the axis.
        Returns:
        the S axis.
      • getTAxis

        public Vec4 getTAxis()
        Returns the T (third) axis. The axis length is the distance between the box sides perpendicular to the axis.
        Returns:
        the T axis.
      • getUnitRAxis

        public Vec4 getUnitRAxis()
        Returns the R (first) axis in unit length.
        Returns:
        the unit R axis.
      • getUnitSAxis

        public Vec4 getUnitSAxis()
        Returns the S (second) axis in unit length.
        Returns:
        the unit S axis.
      • getUnitTAxis

        public Vec4 getUnitTAxis()
        Returns the T (third) axis in unit length.
        Returns:
        the unit T axis.
      • getCorners

        public Vec4[] getCorners()
        Returns the eight corners of the box.
        Returns:
        the eight box corners in the order bottom-lower-left, bottom-lower-right, bottom-upper-right, bottom-upper-left, top-lower-left, top-lower-right, top-upper-right, top-upper-left.
      • getPlanes

        public Plane[] getPlanes()
        Returns the six planes of the box. The plane normals are directed outwards from the box.
        Returns:
        the six box planes in the order R-min, R-max, S-min, S-max, T-min, T-max.
      • getRLength

        public double getRLength()
        Returns the length of the R axis.
        Returns:
        the length of the R axis.
      • getSLength

        public double getSLength()
        Returns the length of the S axis.
        Returns:
        the length of the S axis.
      • getTLength

        public double getTLength()
        Returns the length of the T axis.
        Returns:
        the length of the T axis.
      • getDiameter

        public double getDiameter()
        Returns the effective diameter of the box as if it were a sphere. The length returned is the square root of the sum of the squares of axis lengths.
        Specified by:
        getDiameter in interface Extent
        Returns:
        the effective diameter of the box.
      • getRadius

        public double getRadius()
        Returns the effective radius of the box as if it were a sphere. The length returned is half the square root of the sum of the squares of axis lengths.
        Specified by:
        getRadius in interface Extent
        Returns:
        the effective radius of the box.
      • translate

        public Box translate​(Vec4 point)
      • computeBoundingBox

        public static Box computeBoundingBox​(java.lang.Iterable<? extends Vec4> points)
        Compute a Box that bounds a specified list of points. Principal axes are computed for the points and used to form a Box.
        Parameters:
        points - the points for which to compute a bounding volume.
        Returns:
        the bounding volume, with axes lengths consistent with the conventions described in the Box class overview.
        Throws:
        java.lang.IllegalArgumentException - if the point list is null or empty.
      • computeBoundingBox

        public static Box computeBoundingBox​(BufferWrapper coordinates,
                                             int stride)
        Computes a Box that bounds a specified buffer of points. Principal axes are computed for the points and used to form a Box.

        The buffer must contain XYZ coordinate tuples which are either tightly packed or offset by the specified stride. The stride specifies the number of buffer elements between the first coordinate of consecutive tuples. For example, a stride of 3 specifies that each tuple is tightly packed as XYZXYZXYZ, whereas a stride of 5 specifies that there are two elements between each tuple as XYZabXYZab (the elements "a" and "b" are ignored). The stride must be at least 3. If the buffer's length is not evenly divisible into stride-sized tuples, this ignores the remaining elements that follow the last complete tuple.

        Parameters:
        coordinates - the buffer containing the point coordinates for which to compute a bounding volume.
        stride - the number of elements between the first coordinate of consecutive points. If stride is 3, this interprets the buffer has having tightly packed XYZ coordinate tuples.
        Returns:
        the bounding volume, with axes lengths consistent with the conventions described in the Box class overview.
        Throws:
        java.lang.IllegalArgumentException - if the buffer is null or empty, or if the stride is less than three.
      • union

        public static Box union​(java.lang.Iterable<? extends Box> iterable)
        Computes a Box that represents the union of one or more Boxes. If the iterable has two or more non-null Boxes, the returned encloses the Boxes. In this case axes and center point of the returned Box may differ from the Boxes. If the iterable has only one non-null Box, this returns that sole Box. This returns null if the iterable is empty or contains only null references.
        Parameters:
        iterable - an iterable of Boxes to enclose.
        Returns:
        a new Box that encloses the specified iterable of Boxes.
        Throws:
        java.lang.IllegalArgumentException - if the iterable is null.
      • intersects

        public boolean intersects​(Frustum frustum)
        Determines whether or not this Extent intersects frustum. Returns true if any part of these two objects intersect, including the case where either object wholly contains the other, false otherwise.
        Specified by:
        intersects in interface Extent
        Parameters:
        frustum - the Frustum with which to test for intersection.
        Returns:
        true if there is an intersection, false otherwise.
      • getEffectiveRadius2

        protected double getEffectiveRadius2​(Plane plane)
        Returns the effective radius of this box relative to a specified plane, using only this box's S and T axes. This is an optimization available when using the effective radius to test the distance from a plane to the line segment along this box's R axis, as is done in this class' intersects(Frustum) method. See Lengyel, 2 Ed, Section 7.2.4.
        Parameters:
        plane - the plane in question.
        Returns:
        the effective radius of this box relative to the specified plane, using only this box's S and T axes to determine the effective radius.
      • getEffectiveRadius

        public double getEffectiveRadius​(Plane plane)
        Computes the effective radius of the extent relative to a specified plane.
        Specified by:
        getEffectiveRadius in interface Extent
        Parameters:
        plane - the plane.
        Returns:
        the effective radius, or 0 if the plane is null.
      • intersectsAt

        protected double intersectsAt​(Plane plane,
                                      double effectiveRadius,
                                      Vec4[] endpoints)
      • intersects

        public boolean intersects​(Plane plane)
        Calculate whether or not this Extent is intersected by plane.
        Specified by:
        intersects in interface Extent
        Parameters:
        plane - the Plane with which to test for intersection.
        Returns:
        true if plane is found to intersect this Extent.
      • intersects

        protected double intersects​(Plane plane,
                                    double effectiveRadius)
      • intersect

        public Intersection[] intersect​(Line line)
        Computes the intersections of this extent with line. The returned array may be either null or of zero length if no intersections are discovered. It does not contain null elements. Tangential intersections are marked as such. line is considered to have infinite length in both directions.
        Specified by:
        intersect in interface Extent
        Parameters:
        line - the Line with which to intersect this Extent.
        Returns:
        an array of intersections representing all the points where line enters or leave this Extent.
      • intersects

        public boolean intersects​(Line line)
        Determines whether or not line intersects this Extent. This method may be faster than checking the size of the array returned by intersect(Line). Implementing methods must ensure that this method returns true if and only if intersect(Line) returns a non-null array containing at least one element.
        Specified by:
        intersects in interface Extent
        Parameters:
        line - the Line with which to test for intersection.
        Returns:
        true if an intersection is found, false otherwise.
      • getProjectedArea

        public double getProjectedArea​(View view)
        Computes the area in square pixels of this Extent after it is projected into the specified view's viewport. The returned value is the screen area that this Extent covers in the infinite plane defined by the view's viewport. This area is not limited to the size of the view's viewport, and portions of this Extent are not clipped by the view's frustum.

        This returns Double.POSITIVE_INFINITY if the view's eye point is inside this Extent, or if any portion of this Extent is behind the eye point. In either case, this Extent has no finite projection on the view.

        Specified by:
        getProjectedArea in interface Extent
        Parameters:
        view - the View for which to compute a projected screen area.
        Returns:
        the projected screen area of this Extent in square pixels, or Double.POSITIVE_INFINITY if the view's eye point is inside this Extent or part of this Extent is behind the view's eye point.
      • computeProjectionHullCode

        protected int computeProjectionHullCode​(View view)
        Computes an index into the ProjectionHullTable for this Box given the specified view. The returned integer is a 6-bit code, where each bit denotes whether one of this Box's six planes faces the View. See the documentation for ProjectionHullTable for details.

        If the view is inside this Box, this returns 0 indicating that none of this Box's planes face the view.

        Parameters:
        view - the View to compute a lookup index for.
        Returns:
        an integer who's first 6 bits define an index into the ProjectionHullTable.
      • render

        public void render​(DrawContext dc)
        Draws a representation of the Box.
        Specified by:
        render in interface Renderable
        Parameters:
        dc - the DrawContext to be used.
        See Also:
        DrawContext
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object