java.lang.Object
org.apache.commons.geometry.io.core.utils.AbstractTextFormatWriter
org.apache.commons.geometry.io.euclidean.threed.obj.ObjWriter
All Implemented Interfaces:
Closeable, AutoCloseable

public final class ObjWriter extends AbstractTextFormatWriter
Class for writing OBJ files containing 3D polygon geometries.
  • Field Details

    • SPACE

      private static final char SPACE
      Space character.
      See Also:
    • vertexCount

      private int vertexCount
      Number of vertices written to the output.
    • normalCount

      private int normalCount
      Number of normals written to the output.
  • Constructor Details

    • ObjWriter

      public ObjWriter(Writer writer)
      Create a new instance that writes output with the given writer.
      Parameters:
      writer - writer used to write output
  • Method Details

    • getVertexCount

      public int getVertexCount()
      Get the number of vertices written to the output.
      Returns:
      the number of vertices written to the output.
    • getVertexNormalCount

      public int getVertexNormalCount()
      Get the number of vertex normals written to the output.
      Returns:
      the number of vertex normals written to the output.
    • writeComment

      public void writeComment(String comment)
      Write an OBJ comment with the given value.
      Parameters:
      comment - comment to write
      Throws:
      UncheckedIOException - if an I/O error occurs
    • writeObjectName

      public void writeObjectName(String objectName)
      Write an object name to the output. This is metadata for the file and does not affect the geometry, although it may affect how the file content is read by other programs.
      Parameters:
      objectName - the name to write
      Throws:
      UncheckedIOException - if an I/O error occurs
    • writeGroupName

      public void writeGroupName(String groupName)
      Write a group name to the output. This is metadata for the file and does not affect the geometry, although it may affect how the file content is read by other programs.
      Parameters:
      groupName - the name to write
      Throws:
      UncheckedIOException - if an I/O error occurs
    • writeVertex

      public int writeVertex(Vector3D vertex)
      Write a vertex and return the 0-based index of the vertex in the output.
      Parameters:
      vertex - vertex to write
      Returns:
      0-based index of the written vertex
      Throws:
      UncheckedIOException - if an I/O error occurs
    • writeVertexNormal

      public int writeVertexNormal(Vector3D normal)
      Write a vertex normal and return the 0-based index of the normal in the output.
      Parameters:
      normal - normal to write
      Returns:
      0-based index of the written normal
      Throws:
      UncheckedIOException - if an I/O error occurs
    • writeFace

      public void writeFace(int... vertexIndices)
      Write a face with the given 0-based vertex indices.
      Parameters:
      vertexIndices - 0-based vertex indices for the face
      Throws:
      IllegalArgumentException - if fewer than 3 vertex indices are given
      IndexOutOfBoundsException - if any vertex index is computed to be outside of the bounds of the elements written so far
      UncheckedIOException - if an I/O error occurs
    • writeFace

      public void writeFace(int[] vertexIndices, int normalIndex)
      Write a face with the given 0-based vertex indices and 0-based normal index. The normal index is applied to all face vertices.
      Parameters:
      vertexIndices - 0-based vertex indices
      normalIndex - 0-based normal index
      Throws:
      IndexOutOfBoundsException - if any vertex or normal index is computed to be outside of the bounds of the elements written so far
      UncheckedIOException - if an I/O error occurs
    • writeFace

      public void writeFace(int[] vertexIndices, int[] normalIndices)
      Write a face with the given vertex and normal indices. Indices are 0-based. The normalIndices argument may be null, but if present, must contain the same number of indices as vertexIndices.
      Parameters:
      vertexIndices - 0-based vertex indices; may not be null
      normalIndices - 0-based normal indices; may be null but if present must contain the same number of indices as vertexIndices
      Throws:
      IllegalArgumentException - if fewer than 3 vertex indices are given or normalIndices is not null but has a different length than vertexIndices
      IndexOutOfBoundsException - if any vertex or normal index is computed to be outside of the bounds of the elements written so far
      UncheckedIOException - if an I/O error occurs
    • writeBoundaries

      public void writeBoundaries(BoundarySource3D src)
      Write the boundaries present in the given boundary source using a ObjWriter.MeshBuffer with an unlimited size.
      Parameters:
      src - boundary source containing the boundaries to write to the output
      Throws:
      IllegalArgumentException - if any boundary in the argument is infinite
      UncheckedIOException - if an I/O error occurs
      See Also:
    • writeBoundaries

      public void writeBoundaries(BoundarySource3D src, int batchSize)
      Write the boundaries present in the given boundary source using a ObjWriter.MeshBuffer with the given batchSize.
      Parameters:
      src - boundary source containing the boundaries to write to the output
      batchSize - batch size to use for the mesh buffer; pass -1 to use a buffer of unlimited size
      Throws:
      IllegalArgumentException - if any boundary in the argument is infinite
      UncheckedIOException - if an I/O error occurs
      See Also:
    • writeMesh

      public void writeMesh(Mesh<?> mesh)
      Write a mesh to the output. All vertices and faces are written exactly as found. For example, if a vertex is duplicated in the argument, it will also be duplicated in the output.
      Parameters:
      mesh - the mesh to write
      Throws:
      UncheckedIOException - if an I/O error occurs
    • meshBuffer

      public ObjWriter.MeshBuffer meshBuffer()
      Create a new ObjWriter.MeshBuffer instance with an unlimited batch size, meaning that no vertex definitions are duplicated in the mesh output. This produces the most compact mesh but at the most of higher memory usage during writing.
      Returns:
      new mesh buffer instance
    • meshBuffer

      public ObjWriter.MeshBuffer meshBuffer(int batchSize)
      Create a new ObjWriter.MeshBuffer instance with the given batch size. The batch size determines how many faces will be stored in the buffer before being flushed. Faces stored in the buffer share duplicate vertices, reducing the number of vertices required in the file. The batchSize is therefore a trade-off between higher memory usage (high batch size) and a higher probability of duplicate vertices present in the output (low batch size). A batch size of -1 indicates an unlimited batch size.
      Parameters:
      batchSize - number of faces to store in the buffer before automatically flushing to the output
      Returns:
      new mesh buffer instance
    • writeFaceWithOffsets

      private void writeFaceWithOffsets(int vertexOffset, int[] vertexIndices, int normalOffset, int[] normalIndices)
      Write a face with the given offsets and indices. The offsets are added to each index before being written.
      Parameters:
      vertexOffset - vertex offset value
      vertexIndices - 0-based vertex indices for the face
      normalOffset - normal offset value
      normalIndices - 0-based normal indices for the face; may be null if no normal are defined for the face
      Throws:
      IllegalArgumentException - if fewer than 3 vertex indices are given or normalIndices is not null but has a different length than vertexIndices
      IndexOutOfBoundsException - if any vertex or normal index is computed to be outside of the bounds of the elements written so far
      UncheckedIOException - if an I/O error occurs
    • createVectorString

      private String createVectorString(Vector3D vec)
      Create the OBJ string representation of the given vector.
      Parameters:
      vec - vector to convert to a string
      Returns:
      string representation of the given vector
    • writeVertexLine

      private int writeVertexLine(String content)
      Write a vertex line containing the given string content.
      Parameters:
      content - vertex string content
      Returns:
      the 0-based index of the added vertex
      Throws:
      UncheckedIOException - if an I/O error occurs
    • writeVertexNormalLine

      private int writeVertexNormalLine(String content)
      Write a vertex normal line containing the given string content.
      Parameters:
      content - vertex normal string content
      Returns:
      the 0-based index of the added vertex normal
      Throws:
      UncheckedIOException - if an I/O error occurs
    • writeKeywordLine

      private void writeKeywordLine(String keyword, String content)
      Write a line of content prefixed with the given OBJ keyword.
      Parameters:
      keyword - OBJ keyword
      content - line content
      Throws:
      UncheckedIOException - if an I/O error occurs