Class ObjWriter
- java.lang.Object
-
- org.apache.commons.geometry.io.core.utils.AbstractTextFormatWriter
-
- org.apache.commons.geometry.io.euclidean.threed.obj.ObjWriter
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public final class ObjWriter extends AbstractTextFormatWriter
Class for writing OBJ files containing 3D polygon geometries.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
ObjWriter.MeshBuffer
Class used to produce OBJ mesh content from sequences of facets.
-
Field Summary
Fields Modifier and Type Field Description private int
normalCount
Number of normals written to the output.private static char
SPACE
Space character.private int
vertexCount
Number of vertices written to the output.
-
Constructor Summary
Constructors Constructor Description ObjWriter(java.io.Writer writer)
Create a new instance that writes output with the given writer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.String
createVectorString(Vector3D vec)
Create the OBJ string representation of the given vector.int
getVertexCount()
Get the number of vertices written to the output.int
getVertexNormalCount()
Get the number of vertex normals written to the output.ObjWriter.MeshBuffer
meshBuffer()
Create a newObjWriter.MeshBuffer
instance with an unlimited batch size, meaning that no vertex definitions are duplicated in the mesh output.ObjWriter.MeshBuffer
meshBuffer(int batchSize)
Create a newObjWriter.MeshBuffer
instance with the given batch size.void
writeBoundaries(BoundarySource3D src)
Write the boundaries present in the given boundary source using aObjWriter.MeshBuffer
with an unlimited size.void
writeBoundaries(BoundarySource3D src, int batchSize)
Write the boundaries present in the given boundary source using aObjWriter.MeshBuffer
with the givenbatchSize
.void
writeComment(java.lang.String comment)
Write an OBJ comment with the given value.void
writeFace(int... vertexIndices)
Write a face with the given 0-based vertex indices.void
writeFace(int[] vertexIndices, int normalIndex)
Write a face with the given 0-based vertex indices and 0-based normal index.void
writeFace(int[] vertexIndices, int[] normalIndices)
Write a face with the given vertex and normal indices.private void
writeFaceWithOffsets(int vertexOffset, int[] vertexIndices, int normalOffset, int[] normalIndices)
Write a face with the given offsets and indices.void
writeGroupName(java.lang.String groupName)
Write a group name to the output.private void
writeKeywordLine(java.lang.String keyword, java.lang.String content)
Write a line of content prefixed with the given OBJ keyword.void
writeMesh(Mesh<?> mesh)
Write a mesh to the output.void
writeObjectName(java.lang.String objectName)
Write an object name to the output.int
writeVertex(Vector3D vertex)
Write a vertex and return the 0-based index of the vertex in the output.private int
writeVertexLine(java.lang.String content)
Write a vertex line containing the given string content.int
writeVertexNormal(Vector3D normal)
Write a vertex normal and return the 0-based index of the normal in the output.private int
writeVertexNormalLine(java.lang.String content)
Write a vertex normal line containing the given string content.-
Methods inherited from class org.apache.commons.geometry.io.core.utils.AbstractTextFormatWriter
close, getDoubleFormat, getLineSeparator, getWriter, setDoubleFormat, setLineSeparator, write, write, write, write, writeNewLine
-
-
-
-
Field Detail
-
SPACE
private static final char SPACE
Space character.- See Also:
- Constant Field Values
-
vertexCount
private int vertexCount
Number of vertices written to the output.
-
normalCount
private int normalCount
Number of normals written to the output.
-
-
Method Detail
-
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(java.lang.String comment)
Write an OBJ comment with the given value.- Parameters:
comment
- comment to write- Throws:
java.io.UncheckedIOException
- if an I/O error occurs
-
writeObjectName
public void writeObjectName(java.lang.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:
java.io.UncheckedIOException
- if an I/O error occurs
-
writeGroupName
public void writeGroupName(java.lang.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:
java.io.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:
java.io.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:
java.io.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:
java.lang.IllegalArgumentException
- if fewer than 3 vertex indices are givenjava.lang.IndexOutOfBoundsException
- if any vertex index is computed to be outside of the bounds of the elements written so farjava.io.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 indicesnormalIndex
- 0-based normal index- Throws:
java.lang.IndexOutOfBoundsException
- if any vertex or normal index is computed to be outside of the bounds of the elements written so farjava.io.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. ThenormalIndices
argument may be null, but if present, must contain the same number of indices asvertexIndices
.- Parameters:
vertexIndices
- 0-based vertex indices; may not be nullnormalIndices
- 0-based normal indices; may be null but if present must contain the same number of indices asvertexIndices
- Throws:
java.lang.IllegalArgumentException
- if fewer than 3 vertex indices are given ornormalIndices
is not null but has a different length thanvertexIndices
java.lang.IndexOutOfBoundsException
- if any vertex or normal index is computed to be outside of the bounds of the elements written so farjava.io.UncheckedIOException
- if an I/O error occurs
-
writeBoundaries
public void writeBoundaries(BoundarySource3D src)
Write the boundaries present in the given boundary source using aObjWriter.MeshBuffer
with an unlimited size.- Parameters:
src
- boundary source containing the boundaries to write to the output- Throws:
java.lang.IllegalArgumentException
- if any boundary in the argument is infinitejava.io.UncheckedIOException
- if an I/O error occurs- See Also:
meshBuffer(int)
,writeMesh(Mesh)
-
writeBoundaries
public void writeBoundaries(BoundarySource3D src, int batchSize)
Write the boundaries present in the given boundary source using aObjWriter.MeshBuffer
with the givenbatchSize
.- Parameters:
src
- boundary source containing the boundaries to write to the outputbatchSize
- batch size to use for the mesh buffer; pass-1
to use a buffer of unlimited size- Throws:
java.lang.IllegalArgumentException
- if any boundary in the argument is infinitejava.io.UncheckedIOException
- if an I/O error occurs- See Also:
meshBuffer(int)
,writeMesh(Mesh)
-
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:
java.io.UncheckedIOException
- if an I/O error occurs
-
meshBuffer
public ObjWriter.MeshBuffer meshBuffer()
Create a newObjWriter.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 newObjWriter.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. ThebatchSize
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 valuevertexIndices
- 0-based vertex indices for the facenormalOffset
- normal offset valuenormalIndices
- 0-based normal indices for the face; may be null if no normal are defined for the face- Throws:
java.lang.IllegalArgumentException
- if fewer than 3 vertex indices are given ornormalIndices
is not null but has a different length thanvertexIndices
java.lang.IndexOutOfBoundsException
- if any vertex or normal index is computed to be outside of the bounds of the elements written so farjava.io.UncheckedIOException
- if an I/O error occurs
-
createVectorString
private java.lang.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(java.lang.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:
java.io.UncheckedIOException
- if an I/O error occurs
-
writeVertexNormalLine
private int writeVertexNormalLine(java.lang.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:
java.io.UncheckedIOException
- if an I/O error occurs
-
writeKeywordLine
private void writeKeywordLine(java.lang.String keyword, java.lang.String content)
Write a line of content prefixed with the given OBJ keyword.- Parameters:
keyword
- OBJ keywordcontent
- line content- Throws:
java.io.UncheckedIOException
- if an I/O error occurs
-
-