Class ObjWriter.MeshBuffer

java.lang.Object
org.apache.commons.geometry.io.euclidean.threed.obj.ObjWriter.MeshBuffer
Enclosing class:
ObjWriter

public final class ObjWriter.MeshBuffer extends Object
Class used to produce OBJ mesh content from sequences of facets. As facets are added to the buffer their vertices and normals are converted to OBJ vertex and normal definition strings. Vertices and normals that produce equal definition strings are shared among all of the facets in the buffer. This process converts the facet sequence into a compact mesh suitable for writing as OBJ file content.

Ideally, no vertices or normals would be duplicated in an OBJ file. However, when working with very large geometries it may not be desirable to store values in memory before writing to the output. This is where the batchSize property comes into play. The batchSize represents the maximum number of faces that the buffer will store before automatically flushing its contents to the output and resetting its state. This reduces the amount of memory used by the buffer at the cost of increasing the likelihood of duplicate vertices and/or normals in the output.

  • Field Details

    • batchSize

      private final int batchSize
      Maximum number of faces that will be stored in the buffer before automatically flushing.
    • vertexMap

      private final Map<String,Integer> vertexMap
      Map of vertex definition strings to their local index.
    • normalMap

      private final Map<String,Integer> normalMap
      Map of vertex normals to their local index.
    • faceVertices

      private final List<int[]> faceVertices
      List of local face vertex indices.
    • faceToNormalMap

      private final Map<Integer,Integer> faceToNormalMap
      Map of local face indices to their local normal index.
  • Constructor Details

    • MeshBuffer

      MeshBuffer(int batchSize)
      Construct a new mesh buffer instance with the given batch size.
      Parameters:
      batchSize - batch size; set to -1 to indicate an unlimited size
  • Method Details

    • add

      public void add(FacetDefinition facet)
      Add a facet to this buffer. If batchSize is greater than -1 and the number of currently stored faces is greater than or equal to batchSize, then the buffer content is written to the output and the buffer state is reset.
      Parameters:
      facet - facet to add
      Throws:
      UncheckedIOException - if an I/O error occurs
    • add

      public void add(PlaneConvexSubset boundary)
      Add a boundary to this buffer. If batchSize is greater than -1 and the number of currently stored faces is greater than or equal to batchSize, then the buffer content is written to the output and the buffer state is reset.
      Parameters:
      boundary - boundary to add
      Throws:
      IllegalArgumentException - if the boundary is infinite
      UncheckedIOException - if an I/O error occurs
    • addVertex

      public int addVertex(Vector3D vertex)
      Add a vertex to the buffer.
      Parameters:
      vertex - vertex to add
      Returns:
      the index of the vertex in the buffer
    • addNormal

      public int addNormal(Vector3D normal)
      Add a normal to the buffer.
      Parameters:
      normal - normal to add
      Returns:
      the index of the normal in the buffer
    • flush

      public void flush()
      Flush the buffer content to the output and reset its state.
      Throws:
      UncheckedIOException - if an I/O error occurs
    • addToMap

      private int addToMap(Vector3D vec, Map<String,Integer> map)
      Convert the given vector to on OBJ definition string and add it to the map if not yet present. The mapped index of the vector is returned.
      Parameters:
      vec - vector to add
      map - map to add the vector to
      Returns:
      the index the vector entry is mapped to
    • addFace

      private void addFace(List<Vector3D> vertices, Vector3D normal)
      Add a face to the buffer. If batchSize is greater than -1 and the number of currently stored faces is greater than or equal to batchSize, then the buffer content is written to the output and the buffer state is reset.
      Parameters:
      vertices - face vertices
      normal - face normal; may be null
      Throws:
      UncheckedIOException - if an I/O error occurs
    • reset

      private void reset()
      Reset the buffer state.