Class AABBox


  • public class AABBox
    extends Object
    Axis Aligned Bounding Box. Defined by two 3D coordinates (low and high) The low being the the lower left corner of the box, and the high being the upper right corner of the box.

    A few references for collision detection, intersections:

     http://www.realtimerendering.com/intersections.html
     http://www.codercorner.com/RayAABB.cpp
     http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter0.htm
     http://realtimecollisiondetection.net/files/levine_swept_sat.txt
     

    • Constructor Summary

      Constructors 
      Constructor Description
      AABBox()
      Create an Axis Aligned bounding box (AABBox) with the inverse low/high, allowing the next resize(float, float, float) command to hit.
      AABBox​(float[] low, float[] high)
      Create a AABBox defining the low and high
      AABBox​(float lx, float ly, float lz, float hx, float hy, float hz)
      Create an AABBox specifying the coordinates of the low and high
      AABBox​(AABBox src)
      Create an AABBox copying all values from the given one
    • Constructor Detail

      • AABBox

        public AABBox​(AABBox src)
        Create an AABBox copying all values from the given one
        Parameters:
        src - the box value to be used for the new instance
      • AABBox

        public AABBox​(float lx,
                      float ly,
                      float lz,
                      float hx,
                      float hy,
                      float hz)
        Create an AABBox specifying the coordinates of the low and high
        Parameters:
        lx - min x-coordinate
        ly - min y-coordnate
        lz - min z-coordinate
        hx - max x-coordinate
        hy - max y-coordinate
        hz - max z-coordinate
      • AABBox

        public AABBox​(float[] low,
                      float[] high)
        Create a AABBox defining the low and high
        Parameters:
        low - min xyz-coordinates
        high - max xyz-coordinates
    • Method Detail

      • getHigh

        public final Vec3f getHigh()
        Get the max xyz-coordinates
        Returns:
        max xyz coordinates
      • getLow

        public final Vec3f getLow()
        Get the min xyz-coordinates
        Returns:
        min xyz coordinates
      • copy

        public final AABBox copy​(AABBox src)
        Copy given AABBox 'src' values to this AABBox.
        Parameters:
        src - source AABBox
        Returns:
        this AABBox for chaining
      • setSize

        public final AABBox setSize​(float[] low,
                                    float[] high)
        Set size of the AABBox specifying the coordinates of the low and high.
        Parameters:
        low - min xyz-coordinates
        high - max xyz-coordinates
        Returns:
        this AABBox for chaining
      • setSize

        public final AABBox setSize​(float lx,
                                    float ly,
                                    float lz,
                                    float hx,
                                    float hy,
                                    float hz)
        Set size of the AABBox specifying the coordinates of the low and high.
        Parameters:
        lx - min x-coordinate
        ly - min y-coordnate
        lz - min z-coordinate
        hx - max x-coordinate
        hy - max y-coordinate
        hz - max z-coordinate
        Returns:
        this AABBox for chaining
      • setSize

        public final AABBox setSize​(Vec3f low,
                                    Vec3f high)
        Set size of the AABBox specifying the coordinates of the low and high.
        Parameters:
        low - min xyz-coordinates
        high - max xyz-coordinates
        Returns:
        this AABBox for chaining
      • resize

        public final AABBox resize​(AABBox newBox)
        Resize the AABBox to encapsulate another AABox
        Parameters:
        newBox - AABBox to be encapsulated in
        Returns:
        this AABBox for chaining
      • resize

        public final AABBox resize​(AABBox newBox,
                                   com.jogamp.graph.geom.plane.AffineTransform t,
                                   Vec3f tmpV3)
        Resize the AABBox to encapsulate another AABox, which will be transformed on the fly first.
        Parameters:
        newBox - AABBox to be encapsulated in
        t - the AffineTransform applied on newBox on the fly
        tmpV3 - temporary storage
        Returns:
        this AABBox for chaining
      • resize

        public final AABBox resize​(float x,
                                   float y,
                                   float z)
        Resize the AABBox to encapsulate the passed xyz-coordinates.
        Parameters:
        x - x-axis coordinate value
        y - y-axis coordinate value
        z - z-axis coordinate value
        Returns:
        this AABBox for chaining
      • resize

        public final AABBox resize​(float[] xyz,
                                   int offset)
        Resize the AABBox to encapsulate the passed xyz-coordinates.
        Parameters:
        xyz - xyz-axis coordinate values
        offset - of the array
        Returns:
        this AABBox for chaining
      • resize

        public final AABBox resize​(float[] xyz)
        Resize the AABBox to encapsulate the passed xyz-coordinates.
        Parameters:
        xyz - xyz-axis coordinate values
        Returns:
        this AABBox for chaining
      • resize

        public final AABBox resize​(Vec3f xyz)
        Resize the AABBox to encapsulate the passed xyz-coordinates.
        Parameters:
        xyz - xyz-axis coordinate values
        Returns:
        this AABBox for chaining
      • contains

        public final boolean contains​(float x,
                                      float y)
        Check if the x & y coordinates are bounded/contained by this AABBox
        Parameters:
        x - x-axis coordinate value
        y - y-axis coordinate value
        Returns:
        true if x belong to (low.x, high.x) and y belong to (low.y, high.y)
      • contains

        public final boolean contains​(float x,
                                      float y,
                                      float z)
        Check if the xyz coordinates are bounded/contained by this AABBox.
        Parameters:
        x - x-axis coordinate value
        y - y-axis coordinate value
        z - z-axis coordinate value
        Returns:
        true if x belong to (low.x, high.x) and y belong to (low.y, high.y) and z belong to (low.z, high.z)
      • intersects2DRegion

        public final boolean intersects2DRegion​(float x,
                                                float y,
                                                float w,
                                                float h)
        Check if there is a common region between this AABBox and the passed 2D region irrespective of z range
        Parameters:
        x - lower left x-coord
        y - lower left y-coord
        w - width
        h - hight
        Returns:
        true if this AABBox might have a common region with this 2D region
      • intersectsRay

        public final boolean intersectsRay​(Ray ray)
        Check if Ray intersects this bounding box.

        Versions uses the SAT[1], testing 6 axes. Original code for OBBs from MAGIC. Rewritten for AABBs and reorganized for early exits[2].

         [1] SAT = Separating Axis Theorem
         [2] http://www.codercorner.com/RayAABB.cpp
         
        Parameters:
        ray -
        Returns:
      • getRayIntersection

        public final Vec3f getRayIntersection​(Vec3f result,
                                              Ray ray,
                                              float epsilon,
                                              boolean assumeIntersection)
        Return intersection of a Ray with this bounding box, or null if none exist.

        • Original code by Andrew Woo, from "Graphics Gems", Academic Press, 1990 [2]
        • Optimized code by Pierre Terdiman, 2000 (~20-30% faster on my Celeron 500)
        • Epsilon value added by Klaus Hartmann.

        Method is based on the requirements:

        • the integer representation of 0.0f is 0x00000000
        • the sign bit of the float is the most significant one

        Report bugs: p.terdiman@codercorner.com (original author)

         [1] http://www.codercorner.com/RayAABB.cpp
         [2] http://tog.acm.org/resources/GraphicsGems/gems/RayBox.c
         
        Parameters:
        result - vec3
        ray -
        epsilon -
        assumeIntersection - if true, method assumes an intersection, i.e. by pre-checking via intersectsRay(Ray). In this case method will not validate a possible non-intersection and just computes coordinates.
        Returns:
        float[3] result of intersection coordinates, or null if none exists
      • getSize

        public final float getSize()
        Get the size of this AABBox where the size is represented by the length of the vector between low and high.
        Returns:
        a float representing the size of the AABBox
      • getCenter

        public final Vec3f getCenter()
        Get the Center of this AABBox
        Returns:
        the xyz-coordinates of the center of the AABBox
      • scale

        public final AABBox scale​(float size)
        Scale this AABBox by a constant around fixed center

        high and low is recomputed by scaling its distance to fixed center.

        Parameters:
        size - a constant float value
        Returns:
        this AABBox for chaining
        See Also:
        #scale2(float, float[])
      • scale2

        public final AABBox scale2​(float size)
        Scale this AABBox by a constant, recomputing center

        high and low is scaled and center recomputed.

        Parameters:
        size - a constant float value
        Returns:
        this AABBox for chaining
        See Also:
        #scale(float, float[])
      • translate

        public final AABBox translate​(float dx,
                                      float dy,
                                      float dz)
        Translate this AABBox by a float[3] vector
        Parameters:
        dx - the translation x-component
        dy - the translation y-component
        dz - the translation z-component
        t - the float[3] translation vector
        Returns:
        this AABBox for chaining
      • translate

        public final AABBox translate​(Vec3f t)
        Translate this AABBox by a float[3] vector
        Parameters:
        t - the float[3] translation vector
        Returns:
        this AABBox for chaining
      • rotate

        public final AABBox rotate​(Quaternion quat)
        Rotate this AABBox by a float[3] vector
        Parameters:
        quat - the Quaternion used for rotation
        Returns:
        this AABBox for chaining
      • getMinX

        public final float getMinX()
      • getMinY

        public final float getMinY()
      • getMinZ

        public final float getMinZ()
      • getMaxX

        public final float getMaxX()
      • getMaxY

        public final float getMaxY()
      • getMaxZ

        public final float getMaxZ()
      • getWidth

        public final float getWidth()
      • getHeight

        public final float getHeight()
      • getDepth

        public final float getDepth()
      • equals

        public final boolean equals​(Object obj)
        Overrides:
        equals in class Object
      • hashCode

        public final int hashCode()
        Overrides:
        hashCode in class Object
      • mapToWindow

        public AABBox mapToWindow​(AABBox result,
                                  Matrix4f mat4PMv,
                                  Recti viewport,
                                  boolean useCenterZ)
        Assume this bounding box as being in object space and compute the window bounding box.

        If useCenterZ is true, only 4 mapObjToWinCoords operations are made on points [1..4] using getCenter()'s z-value. Otherwise 8 mapObjToWinCoords operation on all 8 points are performed.

          .z() ------ [4]
           |          |
           |          |
          .y() ------ [3]
         
        Parameters:
        mat4PMv - [projection] x [modelview] matrix, i.e. P x Mv
        viewport - viewport rectangle
        useCenterZ -
        vec3Tmp0 - 3 component vector for temp storage
        vec4Tmp1 - 4 component vector for temp storage
        vec4Tmp2 - 4 component vector for temp storage
        Returns: