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 java.lang.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. ThebatchSize
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 Summary
Fields Modifier and Type Field Description private int
batchSize
Maximum number of faces that will be stored in the buffer before automatically flushing.private java.util.Map<java.lang.Integer,java.lang.Integer>
faceToNormalMap
Map of local face indices to their local normal index.private java.util.List<int[]>
faceVertices
List of local face vertex indices.private java.util.Map<java.lang.String,java.lang.Integer>
normalMap
Map of vertex normals to their local index.private java.util.Map<java.lang.String,java.lang.Integer>
vertexMap
Map of vertex definition strings to their local index.
-
Constructor Summary
Constructors Constructor Description MeshBuffer(int batchSize)
Construct a new mesh buffer instance with the given batch size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(PlaneConvexSubset boundary)
Add a boundary to this buffer.void
add(FacetDefinition facet)
Add a facet to this buffer.private void
addFace(java.util.List<Vector3D> vertices, Vector3D normal)
Add a face to the buffer.int
addNormal(Vector3D normal)
Add a normal to the buffer.private int
addToMap(Vector3D vec, java.util.Map<java.lang.String,java.lang.Integer> map)
Convert the given vector to on OBJ definition string and add it to the map if not yet present.int
addVertex(Vector3D vertex)
Add a vertex to the buffer.void
flush()
Flush the buffer content to the output and reset its state.private void
reset()
Reset the buffer state.
-
-
-
Field Detail
-
batchSize
private final int batchSize
Maximum number of faces that will be stored in the buffer before automatically flushing.
-
vertexMap
private final java.util.Map<java.lang.String,java.lang.Integer> vertexMap
Map of vertex definition strings to their local index.
-
normalMap
private final java.util.Map<java.lang.String,java.lang.Integer> normalMap
Map of vertex normals to their local index.
-
faceVertices
private final java.util.List<int[]> faceVertices
List of local face vertex indices.
-
faceToNormalMap
private final java.util.Map<java.lang.Integer,java.lang.Integer> faceToNormalMap
Map of local face indices to their local normal index.
-
-
Method Detail
-
add
public void add(FacetDefinition facet)
Add a facet to this buffer. IfbatchSize
is greater than-1
and the number of currently stored faces is greater than or equal tobatchSize
, then the buffer content is written to the output and the buffer state is reset.- Parameters:
facet
- facet to add- Throws:
java.io.UncheckedIOException
- if an I/O error occurs
-
add
public void add(PlaneConvexSubset boundary)
Add a boundary to this buffer. IfbatchSize
is greater than-1
and the number of currently stored faces is greater than or equal tobatchSize
, then the buffer content is written to the output and the buffer state is reset.- Parameters:
boundary
- boundary to add- Throws:
java.lang.IllegalArgumentException
- if the boundary is infinitejava.io.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:
java.io.UncheckedIOException
- if an I/O error occurs
-
addToMap
private int addToMap(Vector3D vec, java.util.Map<java.lang.String,java.lang.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 addmap
- map to add the vector to- Returns:
- the index the vector entry is mapped to
-
addFace
private void addFace(java.util.List<Vector3D> vertices, Vector3D normal)
Add a face to the buffer. IfbatchSize
is greater than-1
and the number of currently stored faces is greater than or equal tobatchSize
, then the buffer content is written to the output and the buffer state is reset.- Parameters:
vertices
- face verticesnormal
- face normal; may be null- Throws:
java.io.UncheckedIOException
- if an I/O error occurs
-
reset
private void reset()
Reset the buffer state.
-
-