Class TriangleMesh

java.lang.Object
toxi.geom.mesh.TriangleMesh
All Implemented Interfaces:
Intersector3D, Mesh3D
Direct Known Subclasses:
WETriangleMesh

public class TriangleMesh extends Object implements Mesh3D, Intersector3D
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 Details

    • DEFAULT_NUM_VERTICES

      public static final int DEFAULT_NUM_VERTICES
      Default size for vertex list
      See Also:
    • DEFAULT_NUM_FACES

      public static final int DEFAULT_NUM_FACES
      Default size for face list
      See Also:
    • DEFAULT_STRIDE

      public static final int DEFAULT_STRIDE
      Default stride setting used for serializing mesh properties into arrays.
      See Also:
    • name

      public String name
      Mesh name
    • vertices

      public LinkedHashMap<Vec3D,Vertex> vertices
      Vertex buffer & lookup index when adding new faces
    • faces

      public ArrayList<Face> faces
      Face list
  • Constructor Details

    • TriangleMesh

      public TriangleMesh()
    • TriangleMesh

      public TriangleMesh(String name)
      Creates a new mesh instance with initial default buffer sizes.
      Parameters:
      name - mesh name
    • TriangleMesh

      public TriangleMesh(String name, int numV, int numF)
      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 name
      numV - initial vertex buffer size
      numF - initial face list size
  • Method Details

    • addFace

      public TriangleMesh addFace(Vec3D a, Vec3D b, Vec3D c)
      Description copied from interface: Mesh3D
      Adds the given 3 points as triangle face to the mesh. The assumed vertex order is anti-clockwise.
      Specified by:
      addFace in interface Mesh3D
    • addFace

      public TriangleMesh addFace(Vec3D a, Vec3D b, Vec3D c, Vec2D uvA, Vec2D uvB, Vec2D uvC)
      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.
      Specified by:
      addFace in interface Mesh3D
      Returns:
      itself
    • addFace

      public TriangleMesh addFace(Vec3D a, Vec3D b, Vec3D c, Vec3D n)
      Specified by:
      addFace in interface Mesh3D
    • addFace

      public TriangleMesh addFace(Vec3D a, Vec3D b, Vec3D c, Vec3D n, Vec2D uvA, Vec2D uvB, Vec2D uvC)
      Specified by:
      addFace in interface Mesh3D
    • addMesh

      public TriangleMesh addMesh(Mesh3D m)
      Adds all faces from the given mesh to this one.
      Specified by:
      addMesh in interface Mesh3D
      Parameters:
      m - source mesh instance
    • center

      public AABB center(ReadonlyVec3D origin)
      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.
      Specified by:
      center in interface Mesh3D
      Parameters:
      origin - new centroid or null (defaults to {0,0,0})
    • clear

      public TriangleMesh clear()
      Clears all counters, and vertex & face buffers.
      Specified by:
      clear in interface Mesh3D
    • computeCentroid

      public Vec3D computeCentroid()
      Description copied from interface: Mesh3D
      Computes the mesh centroid, the average position of all vertices.
      Specified by:
      computeCentroid in interface Mesh3D
      Returns:
      centre point
    • computeFaceNormals

      public TriangleMesh computeFaceNormals()
      Re-calculates all face normals.
      Specified by:
      computeFaceNormals in interface Mesh3D
    • computeVertexNormals

      public TriangleMesh computeVertexNormals()
      Computes the smooth vertex normals for the entire mesh.
      Specified by:
      computeVertexNormals in interface Mesh3D
      Returns:
      itself
    • copy

      public TriangleMesh copy()
      Creates a deep clone of the mesh. The new mesh name will have "-copy" as suffix.
      Returns:
      new mesh instance
    • faceOutwards

      public TriangleMesh 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 interface Mesh3D
      Returns:
      itself
    • flipVertexOrder

      public TriangleMesh 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 interface Mesh3D
      Returns:
      itself
    • flipYAxis

      public TriangleMesh 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.
      Specified by:
      flipYAxis in interface Mesh3D
      Returns:
      itself
    • getBoundingBox

      public AABB getBoundingBox()
      Description copied from interface: Mesh3D
      Computes & returns the axis-aligned bounding box of the mesh.
      Specified by:
      getBoundingBox in interface Mesh3D
      Returns:
      bounding box
    • getBoundingSphere

      public Sphere 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 interface Mesh3D
      Returns:
      bounding sphere
    • getClosestVertexToPoint

      public Vertex getClosestVertexToPoint(ReadonlyVec3D p)
      Specified by:
      getClosestVertexToPoint in interface Mesh3D
    • 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 of getFaceNormalsAsArray(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 see getMeshAsVertexArray(float[], int, int)
      Parameters:
      normals - existing float array or null to automatically create one
      offset - start index in array to place normals
      stride - stride/alignment setting for individual coordinates (min value = 3)
      Returns:
      array of xyz normal coords
      See Also:
    • getFaces

      public List<Face> getFaces()
      Specified by:
      getFaces in interface Mesh3D
    • getFacesAsArray

      public int[] getFacesAsArray()
      Builds an array of vertex indices of all faces. Each vertex ID corresponds to its position in the vertices HashMap. The resulting array will be 3 times the face count.
      Returns:
      array of vertex indices
    • getIntersectionData

      public IsectData3D getIntersectionData()
      Specified by:
      getIntersectionData in interface Intersector3D
      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]
      • Face 2:
        • Vertex #1
        • ...etc.
      Parameters:
      verts - an existing target array or null to automatically create one
      offset - start index in arrtay to place vertices
      stride - stride/alignment setting for individual coordinates
      Returns:
      array of xyz vertex coords
    • getNormalsForUniqueVerticesAsArray

      public float[] getNormalsForUniqueVerticesAsArray()
    • getNumFaces

      public int getNumFaces()
      Description copied from interface: Mesh3D
      Returns the number of triangles used.
      Specified by:
      getNumFaces in interface Mesh3D
      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 interface Mesh3D
      Returns:
      vertex count
    • getRotatedAroundAxis

      public TriangleMesh getRotatedAroundAxis(Vec3D axis, float theta)
    • getRotatedX

      public TriangleMesh getRotatedX(float theta)
    • getRotatedY

      public TriangleMesh getRotatedY(float theta)
    • getRotatedZ

      public TriangleMesh getRotatedZ(float theta)
    • getScaled

      public TriangleMesh getScaled(float scale)
    • getScaled

      public TriangleMesh getScaled(Vec3D scale)
    • getTranslated

      public TriangleMesh getTranslated(Vec3D trans)
    • getUniqueVerticesAsArray

      public float[] getUniqueVerticesAsArray()
    • getVertexAtPoint

      public Vertex getVertexAtPoint(Vec3D v)
    • getVertexForID

      public Vertex getVertexForID(int id)
    • 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 see getMeshAsVertexArray(float[], int, int)
      Parameters:
      normals - existing float array or null to automatically create one
      offset - start index in array to place normals
      stride - stride/alignment setting for individual coordinates (min value = 3)
      Returns:
      array of xyz normal coords
      See Also:
    • getVertices

      public Collection<Vertex> getVertices()
      Specified by:
      getVertices in interface Mesh3D
    • init

      public TriangleMesh init(String name, int numV, int numF)
      Specified by:
      init in interface Mesh3D
    • intersectsRay

      public boolean intersectsRay(Ray3D ray)
      Description copied from interface: Intersector3D
      Checks if entity intersects with the given ray. Further intersection details can then be queried via the IsectData3D instance returned by Intersector3D.getIntersectionData().
      Specified by:
      intersectsRay in interface Intersector3D
      Parameters:
      ray - ray to check
      Returns:
      true, if ray hits the entity
    • perforateFace

      public Triangle3D perforateFace(Face f, float size)
    • pointTowards

      public TriangleMesh pointTowards(ReadonlyVec3D dir)
      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

      public TriangleMesh pointTowards(ReadonlyVec3D dir, ReadonlyVec3D forward)
      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 in
      forward - current forward axis
      Returns:
      itself
    • removeFace

      public void removeFace(Face f)
    • rotateAroundAxis

      public TriangleMesh rotateAroundAxis(Vec3D axis, float theta)
    • rotateX

      public TriangleMesh rotateX(float theta)
    • rotateY

      public TriangleMesh rotateY(float theta)
    • rotateZ

      public TriangleMesh rotateZ(float theta)
    • saveAsOBJ

      public void saveAsOBJ(OBJWriter obj)
      Saves the mesh as OBJ format by appending it to the given mesh OBJWriter instance.
      Parameters:
      obj -
    • saveAsOBJ

      public void saveAsOBJ(OBJWriter obj, boolean saveNormals)
    • saveAsOBJ

      public void saveAsOBJ(OutputStream stream)
      Saves the mesh as OBJ format to the given OutputStream. Currently no texture coordinates are supported or written.
      Parameters:
      stream -
    • saveAsOBJ

      public void saveAsOBJ(String path)
      Saves the mesh as OBJ format to the given file path. Existing files will be overwritten.
      Parameters:
      path -
    • saveAsOBJ

      public void saveAsOBJ(String path, boolean saveNormals)
    • saveAsSTL

      public final void saveAsSTL(OutputStream stream)
      Saves the mesh as binary STL format to the given OutputStream.
      Parameters:
      stream -
      See Also:
    • saveAsSTL

      public final void saveAsSTL(OutputStream stream, boolean useFlippedY)
      Saves the mesh as binary STL format to the given OutputStream. The exported mesh can optionally have it's Y axis flipped by setting the useFlippedY flag to true.
      Parameters:
      stream -
      useFlippedY -
    • saveAsSTL

      public final void saveAsSTL(OutputStream stream, STLWriter stl, boolean useFlippedY)
      Saves the mesh as binary STL format to the given OutputStream and using the supplied STLWriter instance. Use this method to export data in a custom STLColorModel. The exported mesh can optionally have it's Y axis flipped by setting the useFlippedY flag to true.
      Parameters:
      stream -
      stl -
      useFlippedY -
    • saveAsSTL

      public final void saveAsSTL(String fileName)
      Saves the mesh as binary STL format to the given file path. Existing files will be overwritten.
      Parameters:
      fileName -
    • saveAsSTL

      public final void saveAsSTL(String fileName, boolean useFlippedY)
      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

      public final void saveAsSTL(String fileName, STLWriter stl, boolean useFlippedY)
    • scale

      public TriangleMesh scale(float scale)
    • scale

      public TriangleMesh scale(float x, float y, float z)
    • scale

      public TriangleMesh scale(Vec3D scale)
    • setName

      public TriangleMesh setName(String name)
      Specified by:
      setName in interface Mesh3D
    • toString

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

      public WETriangleMesh toWEMesh()
    • transform

      public TriangleMesh transform(Matrix4x4 mat)
      Applies the given matrix transform to all mesh vertices and updates all face normals.
      Parameters:
      mat -
      Returns:
      itself
    • transform

      public TriangleMesh transform(Matrix4x4 mat, boolean updateNormals)
      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

      public TriangleMesh translate(float x, float y, float z)
    • translate

      public TriangleMesh translate(Vec3D trans)
    • updateVertex

      public TriangleMesh updateVertex(Vec3D orig, Vec3D newPos)