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 java.lang.Object implements TriangleMesh
A simple implementation of theTriangleMeshinterface. 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 theTriangleMesh.Face.getPolygon()method on such a face results in an exception. TheMesh.Face.definesPolygon()method can be used to determine if a face defines a valid triangle.Instances of this class are guaranteed to be immutable.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSimpleTriangleMesh.BuilderBuilder class for creating mesh instances.private classSimpleTriangleMesh.FaceIterator<T>Internal class for iterating through the mesh faces and extracting a value from each.private static classSimpleTriangleMesh.FuzzyVectorComparatorComparator used to sort vectors using non-strict ("fuzzy") comparisons.private classSimpleTriangleMesh.SimpleTriangleFaceInternal implementation ofTriangleMesh.Face.-
Nested classes/interfaces inherited from interface org.apache.commons.geometry.euclidean.threed.mesh.TriangleMesh
TriangleMesh.Face
-
-
Field Summary
Fields Modifier and Type Field Description private Bounds3DboundsThe bounds of the mesh.private java.util.List<int[]>facesFaces in the mesh.private org.apache.commons.numbers.core.Precision.DoubleEquivalenceprecisionObject used for floating point comparisons.private java.util.List<Vector3D>verticesVertices in the mesh.
-
Constructor Summary
Constructors Modifier Constructor Description privateSimpleTriangleMesh(java.util.List<Vector3D> vertices, java.util.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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.stream.Stream<PlaneConvexSubset>boundaryStream()Return a stream containing the boundaries for this instance.static SimpleTriangleMesh.Builderbuilder(org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)Return a builder for creating new triangle mesh objects.private <T> java.util.stream.Stream<T>createFaceStream(java.util.function.Function<TriangleMesh.Face,T> fn)Create a stream containing the results of applyingfnto each face in the mesh.java.lang.Iterable<TriangleMesh.Face>faces()Get an iterable containing all faces in the mesh.static SimpleTriangleMeshfrom(java.util.Collection<Vector3D> vertices, java.util.Collection<int[]> faces, org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)Construct a new triangle mesh from the given vertices and face indices.static SimpleTriangleMeshfrom(BoundarySource3D boundarySrc, org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)Construct a new mesh instance containing all triangles from the given boundary source.static SimpleTriangleMeshfrom(Vector3D[] vertices, int[][] faces, org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)Construct a new triangle mesh from the given vertices and face indices.Bounds3DgetBounds()Get aBounds3Dobject defining the axis-aligned box containing all vertices in the boundaries for this instance.TriangleMesh.FacegetFace(int index)Get a face from the mesh by its index.intgetFaceCount()Get the number of faces in the mesh.java.util.List<TriangleMesh.Face>getFaces()Get a list containing all faces in the mesh.org.apache.commons.numbers.core.Precision.DoubleEquivalencegetPrecision()Get the precision context for the mesh.intgetVertexCount()Get the number of vertices in the mesh.java.util.List<Vector3D>getVertices()Get a list containing all vertices in the mesh.java.lang.StringtoString()SimpleTriangleMeshtoTriangleMesh(org.apache.commons.numbers.core.Precision.DoubleEquivalence meshPrecision)Return this instance if the given precision context is equal to the current precision context.SimpleTriangleMeshtransform(Transform<Vector3D> transform)Return a new, transformed mesh by applying the given transform to all vertices.java.util.stream.Stream<Triangle3D>triangleStream()Return the boundaries of this instance as a stream ofTriangle3Dinstances.java.lang.Iterable<Vector3D>vertices()Get an iterable containing the vertices in the mesh.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.geometry.euclidean.threed.BoundarySource3D
linecast, linecastFirst, toList, toTree
-
Methods inherited from interface org.apache.commons.geometry.euclidean.threed.line.Linecastable3D
linecast, linecastFirst
-
-
-
-
Field Detail
-
vertices
private final java.util.List<Vector3D> vertices
Vertices in the mesh.
-
faces
private final java.util.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 Detail
-
SimpleTriangleMesh
private SimpleTriangleMesh(java.util.List<Vector3D> vertices, java.util.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 listfaces- face indices listbounds- mesh boundsprecision- precision context used when creating face polygons
-
-
Method Detail
-
vertices
public java.lang.Iterable<Vector3D> vertices()
Get an iterable containing the vertices in the mesh.- Specified by:
verticesin interfaceMesh<TriangleMesh.Face>- Returns:
- an iterable containing the vertices in the mesh
-
getVertices
public java.util.List<Vector3D> getVertices()
Get a list containing all vertices in the mesh.- Specified by:
getVerticesin interfaceMesh<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:
getVertexCountin interfaceMesh<TriangleMesh.Face>- Returns:
- the number of vertices in the mesh
-
faces
public java.lang.Iterable<TriangleMesh.Face> faces()
Get an iterable containing all faces in the mesh.- Specified by:
facesin interfaceMesh<TriangleMesh.Face>- Returns:
- an iterable containing all faces in the mesh
-
getFaces
public java.util.List<TriangleMesh.Face> getFaces()
Get a list containing all faces in the mesh.- Specified by:
getFacesin interfaceMesh<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:
getFaceCountin interfaceMesh<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:
getFacein interfaceMesh<TriangleMesh.Face>- Parameters:
index- the index of the mesh to retrieve- Returns:
- the face at the given index
-
getBounds
public Bounds3D getBounds()
Get aBounds3Dobject 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:
getBoundsin interfaceBoundarySource3D- 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 faceTriangle3Dinstances.- Returns:
- the precision context for the mesh
-
boundaryStream
public java.util.stream.Stream<PlaneConvexSubset> boundaryStream()
Return a stream containing the boundaries for this instance.- Specified by:
boundaryStreamin interfaceBoundarySource<PlaneConvexSubset>- Returns:
- a stream containing the boundaries for this instance
-
triangleStream
public java.util.stream.Stream<Triangle3D> triangleStream()
Return the boundaries of this instance as a stream ofTriangle3Dinstances. AnIllegalStateExceptionexception 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:
triangleStreamin interfaceBoundarySource3D- Returns:
- a stream of triangles representing the instance boundaries
- See Also:
PlaneSubset.toTriangles()
-
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:
transformin interfaceMesh<TriangleMesh.Face>- Specified by:
transformin interfaceTriangleMesh- 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:
toTriangleMeshin interfaceBoundarySource3D- 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 java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
createFaceStream
private <T> java.util.stream.Stream<T> createFaceStream(java.util.function.Function<TriangleMesh.Face,T> fn)
Create a stream containing the results of applyingfnto 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
fnto 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 meshfaces- face indices for the meshprecision- precision context used for floating point comparisons- Returns:
- a new triangle mesh instance
- Throws:
java.lang.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(java.util.Collection<Vector3D> vertices, java.util.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 meshfaces- face indices for the meshprecision- precision context used for floating point comparisons- Returns:
- a new triangle mesh instance
- Throws:
java.lang.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 fromprecision- precision context used for floating point comparisons- Returns:
- new mesh instance containing all triangles from the given boundary source
- Throws:
java.lang.IllegalStateException- if any boundary in the boundary source has infinite size and cannot be converted to triangles
-
-