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:
Closeable
,AutoCloseable
Class for writing OBJ files containing 3D polygon geometries.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal class
Class used to produce OBJ mesh content from sequences of facets. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
Number of normals written to the output.private static final char
Space character.private int
Number of vertices written to the output. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate String
Create the OBJ string representation of the given vector.int
Get the number of vertices written to the output.int
Get the number of vertex normals written to the output.Create a newObjWriter.MeshBuffer
instance with an unlimited batch size, meaning that no vertex definitions are duplicated in the mesh output.meshBuffer
(int batchSize) Create a newObjWriter.MeshBuffer
instance with the given batch size.void
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
(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
(String groupName) Write a group name to the output.private void
writeKeywordLine
(String keyword, String content) Write a line of content prefixed with the given OBJ keyword.void
Write a mesh to the output.void
writeObjectName
(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
(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
(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 Details
-
SPACE
private static final char SPACESpace character.- See Also:
-
vertexCount
private int vertexCountNumber of vertices written to the output. -
normalCount
private int normalCountNumber of normals written to the output.
-
-
Constructor Details
-
ObjWriter
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
Write an OBJ comment with the given value.- Parameters:
comment
- comment to write- Throws:
UncheckedIOException
- if an I/O error occurs
-
writeObjectName
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
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
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
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 givenIndexOutOfBoundsException
- if any vertex index is computed to be outside of the bounds of the elements written so farUncheckedIOException
- 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:
IndexOutOfBoundsException
- if any vertex or normal index is computed to be outside of the bounds of the elements written so farUncheckedIOException
- 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:
IllegalArgumentException
- if fewer than 3 vertex indices are given ornormalIndices
is not null but has a different length thanvertexIndices
IndexOutOfBoundsException
- if any vertex or normal index is computed to be outside of the bounds of the elements written so farUncheckedIOException
- if an I/O error occurs
-
writeBoundaries
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:
IllegalArgumentException
- if any boundary in the argument is infiniteUncheckedIOException
- if an I/O error occurs- See Also:
-
writeBoundaries
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:
IllegalArgumentException
- if any boundary in the argument is infiniteUncheckedIOException
- if an I/O error occurs- See Also:
-
writeMesh
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
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
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:
IllegalArgumentException
- if fewer than 3 vertex indices are given ornormalIndices
is not null but has a different length thanvertexIndices
IndexOutOfBoundsException
- if any vertex or normal index is computed to be outside of the bounds of the elements written so farUncheckedIOException
- if an I/O error occurs
-
createVectorString
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
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
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
Write a line of content prefixed with the given OBJ keyword.- Parameters:
keyword
- OBJ keywordcontent
- line content- Throws:
UncheckedIOException
- if an I/O error occurs
-