Package toxi.geom.mesh
Class TriangleMesh
java.lang.Object
toxi.geom.mesh.TriangleMesh
- All Implemented Interfaces:
Intersector3D
,Mesh3D
- Direct Known Subclasses:
WETriangleMesh
An extensible class to dynamically build, manipulate & export triangle
meshes. Meshes are built face by face. This implementation automatically
re-uses existing vertices and can generate smooth vertex normals. Vertice and
face lists are directly accessible for speed & convenience.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Default size for face liststatic final int
Default size for vertex liststatic final int
Default stride setting used for serializing mesh properties into arrays.Face listMesh nameVertex buffer & lookup index when adding new faces -
Constructor Summary
ConstructorDescriptionTriangleMesh
(String name) Creates a new mesh instance with initial default buffer sizes.TriangleMesh
(String name, int numV, int numF) Creates a new mesh instance with the given initial buffer sizes. -
Method Summary
Modifier and TypeMethodDescriptionAdds the given 3 points as triangle face to the mesh.Adds the given 3 points as triangle face to the mesh and assigns the given texture coordinates to each vertex.Adds all faces from the given mesh to this one.center
(ReadonlyVec3D origin) Centers the mesh around the given pivot point (the centroid of its AABB).clear()
Clears all counters, and vertex & face buffers.Computes the mesh centroid, the average position of all vertices.Re-calculates all face normals.Computes the smooth vertex normals for the entire mesh.copy()
Creates a deep clone of the mesh.Changes the vertex order of faces such that their normal is facing away from the mesh centroid.Flips the vertex ordering between clockwise and anti-clockwise.Flips all vertices along the Y axis and reverses the vertex ordering of all faces to compensate and keep the direction of normals intact.Computes & returns the axis-aligned bounding box of the mesh.Computes & returns the bounding sphere of the mesh.float[]
Creates an array of unravelled normal coordinates.float[]
getFaceNormalsAsArray
(float[] normals, int offset, int stride) Creates an array of unravelled normal coordinates.getFaces()
int[]
Builds an array of vertex indices of all faces.float[]
Creates an array of unravelled vertex coordinates for all faces using a stride setting of 4, resulting in a serialized version of all mesh vertex coordinates suitable for VBOs.float[]
getMeshAsVertexArray
(float[] verts, int offset, int stride) Creates an array of unravelled vertex coordinates for all faces.float[]
int
Returns the number of triangles used.int
Returns the number of actual vertices used (unique vertices).getRotatedAroundAxis
(Vec3D axis, float theta) getRotatedX
(float theta) getRotatedY
(float theta) getRotatedZ
(float theta) getScaled
(float scale) getTranslated
(Vec3D trans) float[]
getVertexForID
(int id) float[]
Creates an array of unravelled vertex normal coordinates for all faces.float[]
getVertexNormalsAsArray
(float[] normals, int offset, int stride) Creates an array of unravelled vertex normal coordinates for all faces.boolean
intersectsRay
(Ray3D ray) Checks if entity intersects with the given ray.perforateFace
(Face f, float size) Rotates the mesh in such a way so that its "forward" axis is aligned with the given direction.pointTowards
(ReadonlyVec3D dir, ReadonlyVec3D forward) Rotates the mesh in such a way so that its "forward" axis is aligned with the given direction.void
removeFace
(Face f) rotateAroundAxis
(Vec3D axis, float theta) rotateX
(float theta) rotateY
(float theta) rotateZ
(float theta) void
saveAsOBJ
(OutputStream stream) Saves the mesh as OBJ format to the givenOutputStream
.void
Saves the mesh as OBJ format to the given file path.void
void
Saves the mesh as OBJ format by appending it to the given meshOBJWriter
instance.void
final void
saveAsSTL
(OutputStream stream) Saves the mesh as binary STL format to the givenOutputStream
.final void
saveAsSTL
(OutputStream stream, boolean useFlippedY) Saves the mesh as binary STL format to the givenOutputStream
.final void
saveAsSTL
(OutputStream stream, STLWriter stl, boolean useFlippedY) Saves the mesh as binary STL format to the givenOutputStream
and using the suppliedSTLWriter
instance.final void
Saves the mesh as binary STL format to the given file path.final void
Saves the mesh as binary STL format to the given file path.final void
scale
(float scale) scale
(float x, float y, float z) toString()
toWEMesh()
Applies the given matrix transform to all mesh vertices and updates all face normals.Applies the given matrix transform to all mesh vertices.translate
(float x, float y, float z) updateVertex
(Vec3D orig, Vec3D newPos)
-
Field Details
-
DEFAULT_NUM_VERTICES
public static final int DEFAULT_NUM_VERTICESDefault size for vertex list- See Also:
-
DEFAULT_NUM_FACES
public static final int DEFAULT_NUM_FACESDefault size for face list- See Also:
-
DEFAULT_STRIDE
public static final int DEFAULT_STRIDEDefault stride setting used for serializing mesh properties into arrays.- See Also:
-
name
Mesh name -
vertices
Vertex buffer & lookup index when adding new faces -
faces
Face list
-
-
Constructor Details
-
TriangleMesh
public TriangleMesh() -
TriangleMesh
Creates a new mesh instance with initial default buffer sizes.- Parameters:
name
- mesh name
-
TriangleMesh
Creates a new mesh instance with the given initial buffer sizes. These numbers are no limits and the mesh can be smaller or grow later on. They're only used to initialise the underlying collections.- Parameters:
name
- mesh namenumV
- initial vertex buffer sizenumF
- initial face list size
-
-
Method Details
-
addFace
Description copied from interface:Mesh3D
Adds the given 3 points as triangle face to the mesh. The assumed vertex order is anti-clockwise. -
addFace
Description copied from interface:Mesh3D
Adds the given 3 points as triangle face to the mesh and assigns the given texture coordinates to each vertex. The assumed vertex order is anti-clockwise. -
addFace
-
addFace
-
addMesh
Adds all faces from the given mesh to this one. -
center
Description copied from interface:Mesh3D
Centers the mesh around the given pivot point (the centroid of its AABB). Method also updates & returns the new bounding box. -
clear
Clears all counters, and vertex & face buffers. -
computeCentroid
Description copied from interface:Mesh3D
Computes the mesh centroid, the average position of all vertices.- Specified by:
computeCentroid
in interfaceMesh3D
- Returns:
- centre point
-
computeFaceNormals
Re-calculates all face normals.- Specified by:
computeFaceNormals
in interfaceMesh3D
-
computeVertexNormals
Computes the smooth vertex normals for the entire mesh.- Specified by:
computeVertexNormals
in interfaceMesh3D
- Returns:
- itself
-
copy
Creates a deep clone of the mesh. The new mesh name will have "-copy" as suffix.- Returns:
- new mesh instance
-
faceOutwards
Description copied from interface:Mesh3D
Changes the vertex order of faces such that their normal is facing away from the mesh centroid.- Specified by:
faceOutwards
in interfaceMesh3D
- Returns:
- itself
-
flipVertexOrder
Description copied from interface:Mesh3D
Flips the vertex ordering between clockwise and anti-clockwise. Face normals are updated automatically too.- Specified by:
flipVertexOrder
in interfaceMesh3D
- Returns:
- itself
-
flipYAxis
Description copied from interface:Mesh3D
Flips all vertices along the Y axis and reverses the vertex ordering of all faces to compensate and keep the direction of normals intact. -
getBoundingBox
Description copied from interface:Mesh3D
Computes & returns the axis-aligned bounding box of the mesh.- Specified by:
getBoundingBox
in interfaceMesh3D
- Returns:
- bounding box
-
getBoundingSphere
Description copied from interface:Mesh3D
Computes & returns the bounding sphere of the mesh. The origin of the sphere is the mesh's centroid.- Specified by:
getBoundingSphere
in interfaceMesh3D
- Returns:
- bounding sphere
-
getClosestVertexToPoint
- Specified by:
getClosestVertexToPoint
in interfaceMesh3D
-
getFaceNormalsAsArray
public float[] getFaceNormalsAsArray()Creates an array of unravelled normal coordinates. For each vertex the normal vector of its parent face is used. This is a convienence invocation ofgetFaceNormalsAsArray(float[], int, int)
with a default stride = 4.- Returns:
- array of xyz normal coords
-
getFaceNormalsAsArray
public float[] getFaceNormalsAsArray(float[] normals, int offset, int stride) Creates an array of unravelled normal coordinates. For each vertex the normal vector of its parent face is used. This method can be used to translate the internal mesh data structure into a format suitable for OpenGL Vertex Buffer Objects (by choosing stride=4). For more detail, please seegetMeshAsVertexArray(float[], int, int)
- Parameters:
normals
- existing float array or null to automatically create oneoffset
- start index in array to place normalsstride
- stride/alignment setting for individual coordinates (min value = 3)- Returns:
- array of xyz normal coords
- See Also:
-
getFaces
-
getFacesAsArray
public int[] getFacesAsArray()Builds an array of vertex indices of all faces. Each vertex ID corresponds to its position in thevertices
HashMap. The resulting array will be 3 times the face count.- Returns:
- array of vertex indices
-
getIntersectionData
- Specified by:
getIntersectionData
in interfaceIntersector3D
- Returns:
- intersection data parcel
-
getMeshAsVertexArray
public float[] getMeshAsVertexArray()Creates an array of unravelled vertex coordinates for all faces using a stride setting of 4, resulting in a serialized version of all mesh vertex coordinates suitable for VBOs.- Returns:
- float array of vertex coordinates
- See Also:
-
getMeshAsVertexArray
public float[] getMeshAsVertexArray(float[] verts, int offset, int stride) Creates an array of unravelled vertex coordinates for all faces. This method can be used to translate the internal mesh data structure into a format suitable for OpenGL Vertex Buffer Objects (by choosing stride=4). The order of the array will be as follows:- Face 1:
- Vertex #1
- x
- y
- z
- [optional empty indices to match stride setting]
- Vertex #2
- x
- y
- z
- [optional empty indices to match stride setting]
- Vertex #3
- x
- y
- z
- [optional empty indices to match stride setting]
- Vertex #1
- Face 2:
- Vertex #1
- ...etc.
- Parameters:
verts
- an existing target array or null to automatically create oneoffset
- start index in arrtay to place verticesstride
- stride/alignment setting for individual coordinates- Returns:
- array of xyz vertex coords
- Face 1:
-
getNormalsForUniqueVerticesAsArray
public float[] getNormalsForUniqueVerticesAsArray() -
getNumFaces
public int getNumFaces()Description copied from interface:Mesh3D
Returns the number of triangles used.- Specified by:
getNumFaces
in interfaceMesh3D
- Returns:
- face count
-
getNumVertices
public int getNumVertices()Description copied from interface:Mesh3D
Returns the number of actual vertices used (unique vertices).- Specified by:
getNumVertices
in interfaceMesh3D
- Returns:
- vertex count
-
getRotatedAroundAxis
-
getRotatedX
-
getRotatedY
-
getRotatedZ
-
getScaled
-
getScaled
-
getTranslated
-
getUniqueVerticesAsArray
public float[] getUniqueVerticesAsArray() -
getVertexAtPoint
-
getVertexForID
-
getVertexNormalsAsArray
public float[] getVertexNormalsAsArray()Creates an array of unravelled vertex normal coordinates for all faces. Uses default stride = 4.- Returns:
- array of xyz normal coords
- See Also:
-
getVertexNormalsAsArray
public float[] getVertexNormalsAsArray(float[] normals, int offset, int stride) Creates an array of unravelled vertex normal coordinates for all faces. This method can be used to translate the internal mesh data structure into a format suitable for OpenGL Vertex Buffer Objects (by choosing stride=4). For more detail, please seegetMeshAsVertexArray(float[], int, int)
- Parameters:
normals
- existing float array or null to automatically create oneoffset
- start index in array to place normalsstride
- stride/alignment setting for individual coordinates (min value = 3)- Returns:
- array of xyz normal coords
- See Also:
-
getVertices
- Specified by:
getVertices
in interfaceMesh3D
-
init
-
intersectsRay
Description copied from interface:Intersector3D
Checks if entity intersects with the given ray. Further intersection details can then be queried via theIsectData3D
instance returned byIntersector3D.getIntersectionData()
.- Specified by:
intersectsRay
in interfaceIntersector3D
- Parameters:
ray
- ray to check- Returns:
- true, if ray hits the entity
-
perforateFace
-
pointTowards
Rotates the mesh in such a way so that its "forward" axis is aligned with the given direction. This version uses the positive Z-axis as default forward direction.- Parameters:
dir
- new target direction to point in- Returns:
- itself
-
pointTowards
Rotates the mesh in such a way so that its "forward" axis is aligned with the given direction. This version allows to specify the forward direction.- Parameters:
dir
- new target direction to point inforward
- current forward axis- Returns:
- itself
-
removeFace
-
rotateAroundAxis
-
rotateX
-
rotateY
-
rotateZ
-
saveAsOBJ
Saves the mesh as OBJ format by appending it to the given meshOBJWriter
instance.- Parameters:
obj
-
-
saveAsOBJ
-
saveAsOBJ
Saves the mesh as OBJ format to the givenOutputStream
. Currently no texture coordinates are supported or written.- Parameters:
stream
-
-
saveAsOBJ
Saves the mesh as OBJ format to the given file path. Existing files will be overwritten.- Parameters:
path
-
-
saveAsOBJ
-
saveAsSTL
Saves the mesh as binary STL format to the givenOutputStream
.- Parameters:
stream
-- See Also:
-
saveAsSTL
Saves the mesh as binary STL format to the givenOutputStream
. The exported mesh can optionally have it's Y axis flipped by setting the useFlippedY flag to true.- Parameters:
stream
-useFlippedY
-
-
saveAsSTL
Saves the mesh as binary STL format to the givenOutputStream
and using the suppliedSTLWriter
instance. Use this method to export data in a customSTLColorModel
. The exported mesh can optionally have it's Y axis flipped by setting the useFlippedY flag to true.- Parameters:
stream
-stl
-useFlippedY
-
-
saveAsSTL
Saves the mesh as binary STL format to the given file path. Existing files will be overwritten.- Parameters:
fileName
-
-
saveAsSTL
Saves the mesh as binary STL format to the given file path. The exported mesh can optionally have it's Y axis flipped by setting the useFlippedY flag to true. Existing files will be overwritten.- Parameters:
fileName
-useFlippedY
-
-
saveAsSTL
-
scale
-
scale
-
scale
-
setName
-
toString
-
toWEMesh
-
transform
Applies the given matrix transform to all mesh vertices and updates all face normals.- Parameters:
mat
-- Returns:
- itself
-
transform
Applies the given matrix transform to all mesh vertices. If the updateNormals flag is true, all face normals are updated automatically, however vertex normals need a manual update.- Parameters:
mat
-updateNormals
-- Returns:
- itself
-
translate
-
translate
-
updateVertex
-