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 theTriangleMesh
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 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 class
SimpleTriangleMesh.Builder
Builder class for creating mesh instances.private class
SimpleTriangleMesh.FaceIterator<T>
Internal class for iterating through the mesh faces and extracting a value from each.private static class
SimpleTriangleMesh.FuzzyVectorComparator
Comparator used to sort vectors using non-strict ("fuzzy") comparisons.private class
SimpleTriangleMesh.SimpleTriangleFace
Internal 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 Bounds3D
bounds
The bounds of the mesh.private java.util.List<int[]>
faces
Faces in the mesh.private org.apache.commons.numbers.core.Precision.DoubleEquivalence
precision
Object used for floating point comparisons.private java.util.List<Vector3D>
vertices
Vertices in the mesh.
-
Constructor Summary
Constructors Modifier Constructor Description 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.
-
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.Builder
builder(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 applyingfn
to each face in the mesh.java.lang.Iterable<TriangleMesh.Face>
faces()
Get an iterable containing all faces in the mesh.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.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.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.Bounds3D
getBounds()
Get aBounds3D
object defining the axis-aligned box containing all vertices in the boundaries for this instance.TriangleMesh.Face
getFace(int index)
Get a face from the mesh by its index.int
getFaceCount()
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.DoubleEquivalence
getPrecision()
Get the precision context for the mesh.int
getVertexCount()
Get the number of vertices in the mesh.java.util.List<Vector3D>
getVertices()
Get a list containing all vertices in the mesh.java.lang.String
toString()
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.SimpleTriangleMesh
transform(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 ofTriangle3D
instances.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:
vertices
in 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:
getVertices
in 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:
getVertexCount
in 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:
faces
in 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:
getFaces
in 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:
getFaceCount
in 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:
getFace
in interfaceMesh<TriangleMesh.Face>
- Parameters:
index
- the index of the mesh to retrieve- Returns:
- the face at the given index
-
getBounds
public Bounds3D getBounds()
Get aBounds3D
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 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 faceTriangle3D
instances.- 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:
boundaryStream
in 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 ofTriangle3D
instances. AnIllegalStateException
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 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:
transform
in interfaceMesh<TriangleMesh.Face>
- Specified by:
transform
in 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:
toTriangleMesh
in 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:
toString
in 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 applyingfn
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 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
-
-