Class Frustum

  • Direct Known Subclasses:
    PickPointFrustum

    public class Frustum
    extends java.lang.Object
    Represents a view frustum composed of six planes: left, right, bottom, top, near far.

    Frustum instances are immutable.

    • Field Detail

      • left

        protected final Plane left
      • right

        protected final Plane right
      • bottom

        protected final Plane bottom
      • top

        protected final Plane top
      • near

        protected final Plane near
      • far

        protected final Plane far
      • allPlanes

        protected final Plane[] allPlanes
        Holds all six frustum planes in an array in the order left, right, bottom, top, near, far.
    • Constructor Detail

      • Frustum

        public Frustum()
        Constructs a frustum two meters wide centered at the origin. Primarily used for testing.
      • Frustum

        public Frustum​(Plane left,
                       Plane right,
                       Plane bottom,
                       Plane top,
                       Plane near,
                       Plane far)
        Create a frustum from six Planes defining the frustum boundaries.

        None of the arguments may be null.

        Parameters:
        near - the near plane
        far - the far plane
        left - the left plane
        right - the right plane
        top - the top plane
        bottom - the bottom plane
        Throws:
        java.lang.IllegalArgumentException - if any argument is null.
    • Method Detail

      • getLeft

        public final Plane getLeft()
        Returns the left plane.
        Returns:
        the left plane.
      • getRight

        public final Plane getRight()
        Returns the right plane.
        Returns:
        the right plane.
      • getBottom

        public final Plane getBottom()
        Returns the bottom plane.
        Returns:
        the bottom plane.
      • getTop

        public final Plane getTop()
        Returns the top plane.
        Returns:
        the top plane.
      • getNear

        public final Plane getNear()
        Returns the near plane.
        Returns:
        the left plane.
      • getFar

        public final Plane getFar()
        Returns the far plane.
        Returns:
        the left plane.
      • getAllPlanes

        public Plane[] getAllPlanes()
        Returns all the planes.
        Returns:
        an array of the frustum planes, in the order left, right, bottom, top, near, far.
      • equals

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

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

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • fromProjectionMatrix

        public static Frustum fromProjectionMatrix​(Matrix projectionMatrix)
        Creates a frustum by extracting the six frustum planes from a projection matrix.
        Parameters:
        projectionMatrix - the projection matrix to extract the frustum planes from.
        Returns:
        a frustum defined by the extracted planes.
        Throws:
        java.lang.IllegalArgumentException - if the projection matrix is null.
      • fromPerspective

        public static Frustum fromPerspective​(Angle horizontalFieldOfView,
                                              int viewportWidth,
                                              int viewportHeight,
                                              double near,
                                              double far)
        Creates a Frustum from a horizontal field-of-view, viewport aspect ratio and distance to near and far depth clipping planes. The near plane must be closer than the far plane, and both near and far values must be positive.
        Parameters:
        horizontalFieldOfView - horizontal field-of-view angle in the range (0, 180)
        viewportWidth - the width of the viewport in screen pixels
        viewportHeight - the height of the viewport in screen pixels
        near - distance to the near depth clipping plane
        far - distance to far depth clipping plane
        Returns:
        Frustum configured from the specified perspective parameters.
        Throws:
        java.lang.IllegalArgumentException - if fov is not in the range (0, 180), if either near or far are negative, or near is greater than or equal to far
      • fromPerspective

        public static Frustum fromPerspective​(double width,
                                              double height,
                                              double near,
                                              double far)
        Creates a Frustum from three sets of parallel clipping planes (a parallel projectionMatrix). In this case, the near and far depth clipping planes may be negative.
        Parameters:
        near - distance to the near depth clipping plane
        far - distance to far depth clipping plane
        width - horizontal dimension of the near clipping plane
        height - vertical dimension of the near clipping plane
        Returns:
        a Frustum configured with the specified perspective parameters.
        Throws:
        java.lang.IllegalArgumentException - if the difference of any plane set (lright - left, top - bottom, far - near) is less than or equal to zero.
      • fromPerspectiveVecs

        public static Frustum fromPerspectiveVecs​(Vec4 vTL,
                                                  Vec4 vTR,
                                                  Vec4 vBL,
                                                  Vec4 vBR,
                                                  double near,
                                                  double far)
        Creates a Frustum from four edge vectors, viewport aspect ratio and distance to near and far planes. The edge vectors connect the near corners of the frustum to the far corners. The near plane must be closer than the far plane, and both planes must be positive.
        Parameters:
        vTL - vector defining the top-left of the frustum
        vTR - vector defining the top-right of the frustum
        vBL - vector defining the bottom-left of the frustum
        vBR - vector defining the bottom-right of the frustum
        near - distance to the near plane
        far - distance to far plane
        Returns:
        Frustum that was created
        Throws:
        java.lang.IllegalArgumentException - if any of the vectors are null, if either near or far are negative, or near is greater than or equal to far
      • intersects

        public boolean intersects​(Extent extent)
        Indicates whether a specified Extent intersects this frustum.
        Parameters:
        extent - the Extent to test.
        Returns:
        true if the extent intersects this frustum, otherwise false.
        Throws:
        java.lang.IllegalArgumentException - if the extent is null.
      • intersectsSegment

        public boolean intersectsSegment​(Vec4 pa,
                                         Vec4 pb)
        Determines whether a line segment intersects this frustum.
        Parameters:
        pa - one end of the segment.
        pb - the other end of the segment.
        Returns:
        true if the segment intersects or is contained in the frustum, otherwise false.
        Throws:
        java.lang.IllegalArgumentException - if either point is null.
      • contains

        public final boolean contains​(Extent extent)
        Indicates whether a specified Extent is fully within this frustum.
        Parameters:
        extent - the Extent to test.
        Returns:
        true if the extent is fully within this frustum, otherwise false.
        Throws:
        java.lang.IllegalArgumentException - if the extent is null.
      • contains

        public final boolean contains​(Vec4 point)
        Indicates whether a specified point is within this frustum.
        Parameters:
        point - the point to test.
        Returns:
        true if the point is within the frustum, otherwise false.
        Throws:
        java.lang.IllegalArgumentException - if the point is null.
      • transformBy

        public Frustum transformBy​(Matrix matrix)
        Returns a copy of this frustum transformed by a specified Matrix.
        Parameters:
        matrix - the Matrix to apply to this frustum.
        Returns:
        a new frustum transformed by the specified matrix.
        Throws:
        java.lang.IllegalArgumentException - if the matrix is null.
      • getCorners

        public Frustum.Corners getCorners()
        Returns the eight corners of this frustum.
        Returns:
        the eight frustum corners.