Class SimpleTriangleMesh.Builder

  • Enclosing class:
    SimpleTriangleMesh

    public static final class SimpleTriangleMesh.Builder
    extends java.lang.Object
    Builder class for creating mesh instances.
    • Field Detail

      • vertices

        private final java.util.ArrayList<Vector3D> vertices
        List of vertices.
      • vertexIndexMap

        private java.util.Map<Vector3D,​java.lang.Integer> vertexIndexMap
        Map of vertices to their first occurrence in the vertex list.
      • faces

        private final java.util.ArrayList<int[]> faces
        List of face vertex indices.
      • boundsBuilder

        private final Bounds3D.Builder boundsBuilder
        Object used to construct the 3D bounds of the vertex list.
      • precision

        private final org.apache.commons.numbers.core.Precision.DoubleEquivalence precision
        Precision context used for floating point comparisons; this value may be null if vertices are not to be combined in this builder.
      • built

        private boolean built
        Flag set to true once a mesh is constructed from this builder.
    • Constructor Detail

      • Builder

        private Builder​(org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)
        Construct a new builder.
        Parameters:
        precision - precision context used for floating point comparisons; may be null if vertices are not to be combined in this builder.
    • Method Detail

      • useVertex

        public int useVertex​(Vector3D vertex)
        Use a vertex in the constructed mesh. If an equivalent vertex already exist, as determined by the configured Precision.DoubleEquivalence, then the index of the previously added vertex is returned. Otherwise, the given vertex is added to the vertex list and the index of the new entry is returned. This is in contrast with the addVertex(Vector3D), which always adds a new entry to the vertex list.
        Parameters:
        vertex - vertex to use
        Returns:
        the index of the added vertex or an equivalent vertex that was added previously
        See Also:
        addVertex(Vector3D)
      • addVertex

        public int addVertex​(Vector3D vertex)
        Add a vertex directly to the vertex list, returning the index of the added vertex. The vertex is added regardless of whether or not an equivalent vertex already exists in the list. This is in contrast with the useVertex(Vector3D) method, which only adds a new entry to the vertex list if an equivalent one does not already exist.
        Parameters:
        vertex - the vertex to append
        Returns:
        the index of the appended vertex in the vertex list
      • addVertices

        public SimpleTriangleMesh.Builder addVertices​(Vector3D[] newVertices)
        Add a group of vertices directly to the vertex list. No equivalent vertices are reused.
        Parameters:
        newVertices - vertices to append
        Returns:
        this instance
        See Also:
        addVertex(Vector3D)
      • addVertices

        public SimpleTriangleMesh.Builder addVertices​(java.util.Collection<? extends Vector3D> newVertices)
        Add a group of vertices directly to the vertex list. No equivalent vertices are reused.
        Parameters:
        newVertices - vertices to append
        Returns:
        this instance
        See Also:
        addVertex(Vector3D)
      • ensureVertexCapacity

        public SimpleTriangleMesh.Builder ensureVertexCapacity​(int numVertices)
        Ensure that this instance has enough capacity to store at least numVertices number of vertices without reallocating space. This can be used to help improve performance and memory usage when creating meshes with large numbers of vertices.
        Parameters:
        numVertices - the number of vertices to ensure that this instance can contain
        Returns:
        this instance
      • getVertexCount

        public int getVertexCount()
        Get the current number of vertices in this mesh.
        Returns:
        the current number of vertices in this mesh
      • getVertex

        public Vector3D getVertex​(int index)
        Get the vertex at the given index.
        Parameters:
        index - index of the vertex to retrieve
        Returns:
        vertex at the given index
        Throws:
        java.lang.IndexOutOfBoundsException - if the index is out of bounds of the mesh vertex list
      • addFace

        public SimpleTriangleMesh.Builder addFace​(int index1,
                                                  int index2,
                                                  int index3)
        Append a face to this mesh.
        Parameters:
        index1 - index of the first vertex in the face
        index2 - index of the second vertex in the face
        index3 - index of the third vertex in the face
        Returns:
        this instance
        Throws:
        java.lang.IllegalArgumentException - if any of the arguments is not a valid index into the current vertex list
      • addFace

        public SimpleTriangleMesh.Builder addFace​(int[] face)
        Append a face to this mesh.
        Parameters:
        face - array containing the 3 vertex indices defining the face
        Returns:
        this instance
        Throws:
        java.lang.IllegalArgumentException - if face does not contain exactly 3 elements or if any of the vertex indices is not a valid index into the current vertex list
      • addFaces

        public SimpleTriangleMesh.Builder addFaces​(int[][] faceIndices)
        Append a group of faces to this mesh.
        Parameters:
        faceIndices - faces to append
        Returns:
        this 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 current vertex list
      • addFaces

        public SimpleTriangleMesh.Builder addFaces​(java.util.Collection<int[]> faceIndices)
        Append a group of faces to this mesh.
        Parameters:
        faceIndices - faces to append
        Returns:
        this 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 current vertex list
      • addFaceUsingVertices

        public SimpleTriangleMesh.Builder addFaceUsingVertices​(Vector3D p1,
                                                               Vector3D p2,
                                                               Vector3D p3)
        Add a face to this mesh, only adding vertices to the vertex list if equivalent vertices are not found.
        Parameters:
        p1 - first face vertex
        p2 - second face vertex
        p3 - third face vertex
        Returns:
        this instance
        See Also:
        useVertex(Vector3D)
      • addFaceAndVertices

        public SimpleTriangleMesh.Builder addFaceAndVertices​(Vector3D p1,
                                                             Vector3D p2,
                                                             Vector3D p3)
        Add a face and its vertices to this mesh. The vertices are always added to the vertex list, regardless of whether or not equivalent vertices exist in the vertex list.
        Parameters:
        p1 - first face vertex
        p2 - second face vertex
        p3 - third face vertex
        Returns:
        this instance
        See Also:
        addVertex(Vector3D)
      • ensureFaceCapacity

        public SimpleTriangleMesh.Builder ensureFaceCapacity​(int numFaces)
        Ensure that this instance has enough capacity to store at least numFaces number of faces without reallocating space. This can be used to help improve performance and memory usage when creating meshes with large numbers of faces.
        Parameters:
        numFaces - the number of faces to ensure that this instance can contain
        Returns:
        this instance
      • getFaceCount

        public int getFaceCount()
        Get the current number of faces in this mesh.
        Returns:
        the current number of faces in this meshr
      • build

        public SimpleTriangleMesh build()
        Build a triangle mesh containing the vertices and faces in this builder.
        Returns:
        a triangle mesh containing the vertices and faces in this builder
      • getVertexIndexMap

        private java.util.Map<Vector3D,​java.lang.Integer> getVertexIndexMap()
        Get the vertex index map, creating and initializing it if needed.
        Returns:
        the vertex index map
      • addToVertexIndexMap

        private int addToVertexIndexMap​(Vector3D vertex,
                                        int targetIdx,
                                        java.util.Map<? super Vector3D,​java.lang.Integer> map)
        Add a vertex to the given vertex index map. The vertex is inserted and mapped to targetidx if an equivalent vertex does not already exist. The index now associated with the given vertex or its equivalent is returned.
        Parameters:
        vertex - vertex to add
        targetIdx - the index to associate with the vertex if no equivalent vertex has already been mapped
        map - vertex index map
        Returns:
        the index now associated with the given vertex or its equivalent
      • addToVertexList

        private int addToVertexList​(Vector3D vertex)
        Append the given vertex to the end of the vertex list. The index of the vertex is returned.
        Parameters:
        vertex - the vertex to append
        Returns:
        the index of the appended vertex
      • validateVertexIndex

        private int validateVertexIndex​(int idx)
        Throw an exception if the given vertex index is not valid.
        Parameters:
        idx - vertex index to validate
        Returns:
        the validated index
        Throws:
        java.lang.IllegalArgumentException - if the given index is not a valid index into the vertices list
      • validateCanModify

        private void validateCanModify()
        Throw an exception if the builder has been used to construct a mesh instance and can no longer be modified.