Class SimpleTriangleMesh

java.lang.Object
org.apache.commons.geometry.euclidean.threed.mesh.SimpleTriangleMesh
All Implemented Interfaces:
BoundarySource<PlaneConvexSubset>, BoundarySource3D, Linecastable3D, Mesh<TriangleMesh.Face>, TriangleMesh

public final class SimpleTriangleMesh extends Object implements TriangleMesh
A simple implementation of the TriangleMesh interface. This class ensures that faces always contain 3 valid references into the vertex list but does not enforce that the referenced vertices are unique or that they define a triangle with non-zero size. For example, a mesh could contain a face with 3 vertices that are considered equivalent by the configured precision context. Attempting to call the TriangleMesh.Face.getPolygon() method on such a face results in an exception. The Mesh.Face.definesPolygon() method can be used to determine if a face defines a valid triangle.

Instances of this class are guaranteed to be immutable.

  • Field Details

    • vertices

      private final List<Vector3D> vertices
      Vertices in the mesh.
    • faces

      private final List<int[]> faces
      Faces in the mesh.
    • bounds

      private final Bounds3D bounds
      The bounds of the mesh.
    • precision

      private final org.apache.commons.numbers.core.Precision.DoubleEquivalence precision
      Object used for floating point comparisons.
  • Constructor Details

    • SimpleTriangleMesh

      private SimpleTriangleMesh(List<Vector3D> vertices, List<int[]> faces, Bounds3D bounds, org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)
      Construct a new instance from a vertex list and set of faces. No validation is performed on the input.
      Parameters:
      vertices - vertex list
      faces - face indices list
      bounds - mesh bounds
      precision - precision context used when creating face polygons
  • Method Details

    • vertices

      public Iterable<Vector3D> vertices()
      Get an iterable containing the vertices in the mesh.
      Specified by:
      vertices in interface Mesh<TriangleMesh.Face>
      Returns:
      an iterable containing the vertices in the mesh
    • getVertices

      public List<Vector3D> getVertices()
      Get a list containing all vertices in the mesh.
      Specified by:
      getVertices in interface Mesh<TriangleMesh.Face>
      Returns:
      a list containing all vertices in the mesh
    • getVertexCount

      public int getVertexCount()
      Get the number of vertices in the mesh.
      Specified by:
      getVertexCount in interface Mesh<TriangleMesh.Face>
      Returns:
      the number of vertices in the mesh
    • faces

      public Iterable<TriangleMesh.Face> faces()
      Get an iterable containing all faces in the mesh.
      Specified by:
      faces in interface Mesh<TriangleMesh.Face>
      Returns:
      an iterable containing all faces in the mesh
    • getFaces

      public List<TriangleMesh.Face> getFaces()
      Get a list containing all faces in the mesh.
      Specified by:
      getFaces in interface Mesh<TriangleMesh.Face>
      Returns:
      a list containing all faces in the mesh
    • getFaceCount

      public int getFaceCount()
      Get the number of faces in the mesh.
      Specified by:
      getFaceCount in interface Mesh<TriangleMesh.Face>
      Returns:
      the number of faces in the mesh
    • getFace

      public TriangleMesh.Face getFace(int index)
      Get a face from the mesh by its index.
      Specified by:
      getFace in interface Mesh<TriangleMesh.Face>
      Parameters:
      index - the index of the mesh to retrieve
      Returns:
      the face at the given index
    • getBounds

      public Bounds3D getBounds()
      Get a Bounds3D object defining the axis-aligned box containing all vertices in the boundaries for this instance. Null is returned if any boundary is infinite or no vertices are found.
      Specified by:
      getBounds in interface BoundarySource3D
      Returns:
      the bounding box for this instance or null if no valid bounds could be determined
    • getPrecision

      public org.apache.commons.numbers.core.Precision.DoubleEquivalence getPrecision()
      Get the precision context for the mesh. This context is used during construction of face Triangle3D instances.
      Returns:
      the precision context for the mesh
    • boundaryStream

      public Stream<PlaneConvexSubset> boundaryStream()
      Return a stream containing the boundaries for this instance.
      Specified by:
      boundaryStream in interface BoundarySource<PlaneConvexSubset>
      Returns:
      a stream containing the boundaries for this instance
    • triangleStream

      public Stream<Triangle3D> triangleStream()
      Return the boundaries of this instance as a stream of Triangle3D instances. An IllegalStateException exception is thrown while reading from the stream if any boundary cannot be converted to a triangle (i.e. if it has infinite size).
      Specified by:
      triangleStream in interface BoundarySource3D
      Returns:
      a stream of triangles representing the instance boundaries
      See Also:
    • transform

      public SimpleTriangleMesh transform(Transform<Vector3D> transform)
      Return a new, transformed mesh by applying the given transform to all vertices. Faces and vertex ordering are not affected.
      Specified by:
      transform in interface Mesh<TriangleMesh.Face>
      Specified by:
      transform in interface TriangleMesh
      Parameters:
      transform - transform to apply
      Returns:
      a new, transformed mesh
    • toTriangleMesh

      public SimpleTriangleMesh toTriangleMesh(org.apache.commons.numbers.core.Precision.DoubleEquivalence meshPrecision)
      Return this instance if the given precision context is equal to the current precision context. Otherwise, create a new mesh with the given precision context but the same vertices, faces, and bounds.
      Specified by:
      toTriangleMesh in interface BoundarySource3D
      Parameters:
      meshPrecision - precision context to use when generating face polygons
      Returns:
      a mesh instance with the given precision context and the same mesh structure as the current instance
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • createFaceStream

      private <T> Stream<T> createFaceStream(Function<TriangleMesh.Face,T> fn)
      Create a stream containing the results of applying fn to each face in the mesh.
      Type Parameters:
      T - Stream element type
      Parameters:
      fn - function used to extract the stream values from each face
      Returns:
      a stream containing the results of applying fn to each face in the mesh
    • builder

      public static SimpleTriangleMesh.Builder builder(org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)
      Return a builder for creating new triangle mesh objects.
      Parameters:
      precision - precision object used for floating point comparisons
      Returns:
      a builder for creating new triangle mesh objects
    • from

      public static SimpleTriangleMesh from(Vector3D[] vertices, int[][] faces, org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)
      Construct a new triangle mesh from the given vertices and face indices.
      Parameters:
      vertices - vertices for the mesh
      faces - face indices for the mesh
      precision - precision context used for floating point comparisons
      Returns:
      a new triangle mesh instance
      Throws:
      IllegalArgumentException - if any of the face index arrays does not have exactly 3 elements or if any index is not a valid index into the vertex list
    • from

      public static SimpleTriangleMesh from(Collection<Vector3D> vertices, Collection<int[]> faces, org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)
      Construct a new triangle mesh from the given vertices and face indices.
      Parameters:
      vertices - vertices for the mesh
      faces - face indices for the mesh
      precision - precision context used for floating point comparisons
      Returns:
      a new triangle mesh instance
      Throws:
      IllegalArgumentException - if any of the face index arrays does not have exactly 3 elements or if any index is not a valid index into the vertex list
    • from

      public static SimpleTriangleMesh from(BoundarySource3D boundarySrc, org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)
      Construct a new mesh instance containing all triangles from the given boundary source. Equivalent vertices are reused wherever possible.
      Parameters:
      boundarySrc - boundary source to construct a mesh from
      precision - precision context used for floating point comparisons
      Returns:
      new mesh instance containing all triangles from the given boundary source
      Throws:
      IllegalStateException - if any boundary in the boundary source has infinite size and cannot be converted to triangles