Package com.irurueta.geometry.io
Class LoaderOBJ.LoaderIteratorOBJ
java.lang.Object
com.irurueta.geometry.io.LoaderOBJ.LoaderIteratorOBJ
- All Implemented Interfaces:
LoaderIterator
- Enclosing class:
- LoaderOBJ
Loader iterator in charge of loading file data in small chunks.
Usually data is divided in chunks small enough that can be directly
loaded by graphical layers such as OpenGL (which has a limit of 65535
indices when using Vertex Buffer Objects, which increase graphical
performance).
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate float[]Array containing vertices coordinates to be added to current chunk of data.private floatX coordinate of the latest point that has been read.private floatY coordinate of the latest point that has been read.private floatZ coordinate of the latest point that has been read.private StringName of current material of data being loaded.private longIndex of current face (i.e. polygon) that has been loaded.private MaterialOBJReference to current material of data being loaded.private longContains position where file is currently being loaded.private longPosition of first face (i.e. polygon) in the file.private booleanIndicates if first face has been found.private longIndicates location of first material in the file.private booleanIndicates if first material has been found.private longPosition of first normal coordinate in the file.private booleanIndicates if first normal coordinate has been found.private longPosition of first texture coordinate in the file.private booleanIndicates if first texture coordinate has been found.private longPosition of first vertex in the file.private booleanIndicates if first vertex position has been found.private booleanIndicates if indices have been loaded and must be added to current chunk being loaded.private intNumber of indices stored in chunk.private int[]Array containing indices to be added to current chunk of data.private intSize of indices stored in chunk.private LoaderIteratorListenerReference to the listener of this loader iterator.private final LoaderOBJReference to loader loading OBJ file.private MaterialLoaderOBJReference to material loader in charge of loading the associated MTL of file to this OBJ file.private booleanIndicates if materials have been loaded and must be added to current chunk being loaded.private floatX coordinate of the maximum point forming the bounding box in a chunk of data.private floatY coordinate of the maximum point forming the bounding box in a chunk of data.private floatZ coordinate of the maximum point forming the bounding box in a chunk of data.private floatX coordinate of the minimum point forming the bounding box in a chunk of data.private floatY coordinate of the minimum point forming the bounding box in a chunk of data.private floatZ coordinate of the minimum point forming the bounding box in a chunk of data.private intNormal index in the file of the latest point that has been read.private booleanIndicates if normals have been loaded and must be added to current chunk being loaded.private float[]Array containing normal coordinates to be added to current chunk of data.Map to relate normals coordinates indices in a file respect to chunk indices.Map to cache normals coordinates positions in a file.private longNormal coordinate position in file.private longNumber of faces (i.e. polygons) that have been loaded in current chunk.private longNumber of normals that have been loaded in current chunk.private longNumber of texture coordinates that have been loaded in current chunk.private longNumber of vertices that have been loaded in current chunk.private floatX coordinate of the latest point normal that has been read.private floatY coordinate of the latest point normal that has been read.private floatZ coordinate of the latest point normal that has been read.private long[]Array containing normal indices as they appear in the OBJ file.private long[]Array containing texture indices as they appear in the OBJ file.private long[]Array containing vertex indices as they appear in the OBJ file.private booleanIndicates if texture coordinates have been loaded and must be added to current chunk being loaded.private float[]Array containing texture coordinates to be added to current chunk of data.Map to relate texture coordinates indices in a file respect to chunk indices.Map to cache texture coordinates positions in a file.private longTexture coordinate position in file.private intTexture index in the file of the latest point that has been read.private floatU texture coordinate of the latest point that has been read.private floatV texture coordinate of the latest point that has been read.private intVertex index in the file of the latest point that has been read.Map to relate vertex indices in a file respect to chunk indices.private longVertex position in file.private booleanIndicates if vertices have been loaded and must be added to current chunk being loaded.private intNumber of vertices stored in chunk.Map to cache vertex positions in a file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddExistingVertexToChunk(int existingIndex) Adds index to current chunk of data referring to a previously existing vertex in the chunk.private voidAdds data of last vertex being loaded to current chunk of data as a new vertex.private voidaddNormalPositionToMap(long originalIndex, long streamPosition) Add normal coordinate to cache of file positions.private voidaddTextureCoordPositionToMap(long originalIndex, long streamPosition) Add texture coordinate position to cache of file positions.private voidaddVertexPositionToMap(long originalIndex, long streamPosition) Add vertex position to cache of file positions.buildTriangulatedIndices(List<VertexOBJ> vertices) Internal method to decompose an array of vertices forming a polygon in a set of arrays of vertices corresponding to triangles after triangulation of the polygon.voidfetchNormal(long index) Fetches normal data in the file using provided index.voidfetchTexture(long index) Fetches texture data in the file using provided index.voidfetchVertex(long index) Fetches vertex data in the file using provided index.getFaceValues(String[] values) This method reads a line containing face (i.e. polygon) indices of vertices and fetches those vertices coordinates and associated data such as texture coordinates or normal coordinates.booleanhasNext()Indicates if there is another chunk of data to be loaded.private voidIncreases size of arrays of data.private voidInitializes arrays forming current chunk of data.next()Loads and returns next chunk of data, if available.private intsearchNormalIndexInChunk(long originalIndex) Searches normal index in current chunk of data by using the index used in the OBJ file.private intsearchTextureCoordIndexInChunk(long originalIndex) Searches texture index in current chunk of data by using the index used in the OBJ file.private intsearchVertexIndexInChunk(long originalIndex) Searches vertex index in current chunk of data by using the index used in the OBJ file.voidsetListener(LoaderIteratorListener listener) Method to set listener of this loader iterator.private voidsetUp()Setups loader iterator.private voidTrims arrays of data to reduce size of arrays to fit chunk data.
-
Field Details
-
loader
Reference to loader loading OBJ file. -
coordX
private float coordXX coordinate of the latest point that has been read. -
coordY
private float coordYY coordinate of the latest point that has been read. -
coordZ
private float coordZZ coordinate of the latest point that has been read. -
textureU
private float textureUU texture coordinate of the latest point that has been read. U coordinate refers to the horizontal axis in the texture image and usually is a normalized value between 0.0 and 1.0. Larger values can be used to repeat textures, negative values can be used to reverse textures. -
textureV
private float textureVV texture coordinate of the latest point that has been read. V coordinate refers to the vertical axis in the texture image and usually is a normalized value between 0.0 and 1.0. Larger values can be used to repeat textures, negative values can be used to reverse textures. -
nX
private float nXX coordinate of the latest point normal that has been read. -
nY
private float nYY coordinate of the latest point normal that has been read. -
nZ
private float nZZ coordinate of the latest point normal that has been read. -
vertexIndex
private int vertexIndexVertex index in the file of the latest point that has been read. -
textureIndex
private int textureIndexTexture index in the file of the latest point that has been read. -
normalIndex
private int normalIndexNormal index in the file of the latest point that has been read. -
minX
private float minXX coordinate of the minimum point forming the bounding box in a chunk of data. This value will be updated while the chunk is being filled. -
minY
private float minYY coordinate of the minimum point forming the bounding box in a chunk of data. This value will be updated while the chunk is being filled. -
minZ
private float minZZ coordinate of the minimum point forming the bounding box in a chunk of data. This value will be updated while the chunk is being filled. -
maxX
private float maxXX coordinate of the maximum point forming the bounding box in a chunk of data. This value will be updated while the chunk is being filled. -
maxY
private float maxYY coordinate of the maximum point forming the bounding box in a chunk of data. This value will be updated while the chunk is being filled. -
maxZ
private float maxZZ coordinate of the maximum point forming the bounding box in a chunk of data. This value will be updated while the chunk is being filled. -
verticesAvailable
private boolean verticesAvailableIndicates if vertices have been loaded and must be added to current chunk being loaded. -
textureAvailable
private boolean textureAvailableIndicates if texture coordinates have been loaded and must be added to current chunk being loaded. -
normalsAvailable
private boolean normalsAvailableIndicates if normals have been loaded and must be added to current chunk being loaded. -
indicesAvailable
private boolean indicesAvailableIndicates if indices have been loaded and must be added to current chunk being loaded. -
materialsAvailable
private boolean materialsAvailableIndicates if materials have been loaded and must be added to current chunk being loaded. -
numberOfVertices
private long numberOfVerticesNumber of vertices that have been loaded in current chunk. -
numberOfTextureCoords
private long numberOfTextureCoordsNumber of texture coordinates that have been loaded in current chunk. -
numberOfNormals
private long numberOfNormalsNumber of normals that have been loaded in current chunk. -
numberOfFaces
private long numberOfFacesNumber of faces (i.e. polygons) that have been loaded in current chunk. -
currentFace
private long currentFaceIndex of current face (i.e. polygon) that has been loaded. -
firstVertexStreamPosition
private long firstVertexStreamPositionPosition of first vertex in the file. This is stored to reduce fetching time when parsing the OBJ file. -
firstVertexStreamPositionAvailable
private boolean firstVertexStreamPositionAvailableIndicates if first vertex position has been found. -
firstTextureCoordStreamPosition
private long firstTextureCoordStreamPositionPosition of first texture coordinate in the file. This is stored to reduce fetching time when parsing the OBJ file. -
firstTextureCoordStreamPositionAvailable
private boolean firstTextureCoordStreamPositionAvailableIndicates if first texture coordinate has been found. -
firstNormalStreamPosition
private long firstNormalStreamPositionPosition of first normal coordinate in the file. This is stored to reduce fetching time when parsing the OBJ file. -
firstNormalStreamPositionAvailable
private boolean firstNormalStreamPositionAvailableIndicates if first normal coordinate has been found. -
firstFaceStreamPosition
private long firstFaceStreamPositionPosition of first face (i.e. polygon) in the file. This is stored to reduce fetching time when parsing the OBJ file. -
firstFaceStreamPositionAvailable
private boolean firstFaceStreamPositionAvailableIndicates if first face has been found. -
firstMaterialStreamPosition
private long firstMaterialStreamPositionIndicates location of first material in the file. This is stored to reduce fetching time when parsing the OBJ file. -
firstMaterialStreamPositionAvailable
private boolean firstMaterialStreamPositionAvailableIndicates if first material has been found. -
currentStreamPosition
private long currentStreamPositionContains position where file is currently being loaded. -
listener
Reference to the listener of this loader iterator. This listener will be notified when the loading process finishes so that resources can be freed. -
coordsInChunkArray
private float[] coordsInChunkArrayArray containing vertices coordinates to be added to current chunk of data. -
textureCoordsInChunkArray
private float[] textureCoordsInChunkArrayArray containing texture coordinates to be added to current chunk of data. -
normalsInChunkArray
private float[] normalsInChunkArrayArray containing normal coordinates to be added to current chunk of data. -
indicesInChunkArray
private int[] indicesInChunkArrayArray containing indices to be added to current chunk of data. Notice that these indices are not the original indices appearing in the file. Instead, they are indices referring to data in current chunk, accounting for duplicate points, etc. This way, indices in a chunk can be directly used to draw the chunk of data by the graphical layer. -
originalVertexIndicesInChunkArray
private long[] originalVertexIndicesInChunkArrayArray containing vertex indices as they appear in the OBJ file. These indices are only used to fetch data, they will never appear in resulting chunk of data. -
originalTextureIndicesInChunkArray
private long[] originalTextureIndicesInChunkArrayArray containing texture indices as they appear in the OBJ file. These indices are only used to fetch data, they will never appear in resulting chunk of data. -
originalNormalIndicesInChunkArray
private long[] originalNormalIndicesInChunkArrayArray containing normal indices as they appear in the OBJ file. These indices are only used to fetch data, they will never appear in resulting chunk of data. -
vertexIndicesMap
Map to relate vertex indices in a file respect to chunk indices. -
textureCoordsIndicesMap
Map to relate texture coordinates indices in a file respect to chunk indices. -
normalsIndicesMap
Map to relate normals coordinates indices in a file respect to chunk indices. -
verticesStreamPositionMap
Map to cache vertex positions in a file. -
textureCoordsStreamPositionMap
Map to cache texture coordinates positions in a file. -
normalsStreamPositionMap
Map to cache normals coordinates positions in a file. -
verticesInChunk
private int verticesInChunkNumber of vertices stored in chunk. -
indicesInChunk
private int indicesInChunkNumber of indices stored in chunk. -
indicesInChunkSize
private int indicesInChunkSizeSize of indices stored in chunk. -
vertexStreamPosition
private long vertexStreamPositionVertex position in file. -
textureCoordStreamPosition
private long textureCoordStreamPositionTexture coordinate position in file. -
normalStreamPosition
private long normalStreamPositionNormal coordinate position in file. -
currentChunkMaterialName
Name of current material of data being loaded. -
currentMaterial
Reference to current material of data being loaded. -
materialLoader
Reference to material loader in charge of loading the associated MTL of file to this OBJ file.
-
-
Constructor Details
-
LoaderIteratorOBJ
Constructor.- Parameters:
loader- reference to loader loading binary file.- Throws:
IOException- if an I/O error occurs.LoaderException- if file data is corrupt or cannot be understood.
-
-
Method Details
-
setListener
Method to set listener of this loader iterator. This listener will be notified when the loading process finishes.- Parameters:
listener- listener of this loader iterator.
-
hasNext
public boolean hasNext()Indicates if there is another chunk of data to be loaded.- Specified by:
hasNextin interfaceLoaderIterator- Returns:
- true if there is another chunk of data, false otherwise.
-
next
Loads and returns next chunk of data, if available.- Specified by:
nextin interfaceLoaderIterator- Returns:
- next chunk of data.
- Throws:
NotAvailableException- thrown if no more data is available.LoaderException- if file data is corrupt or cannot be understood.IOException- if an I/O error occurs.
-
fetchVertex
Fetches vertex data in the file using provided index. Index refers to indices contained in OBJ file.- Parameters:
index- index corresponding to vertex being fetched.- Throws:
LoaderException- if data is corrupted or cannot be understood.IOException- if an I/O error occurs.
-
fetchTexture
Fetches texture data in the file using provided index. Index refers to indices contained in OBJ file.- Parameters:
index- index corresponding to texture being fetched.- Throws:
LoaderException- if data is corrupted or cannot be understood.IOException- if an I/O error occurs.
-
fetchNormal
Fetches normal data in the file using provided index. Index refers to indices contained in OBJ file.- Parameters:
index- index corresponding to normal being fetched.- Throws:
LoaderException- if data is corrupted or cannot be understood.IOException- if an I/O error occurs.
-
buildTriangulatedIndices
private Set<String[]> buildTriangulatedIndices(List<VertexOBJ> vertices) throws com.irurueta.geometry.TriangulatorException Internal method to decompose an array of vertices forming a polygon in a set of arrays of vertices corresponding to triangles after triangulation of the polygon. This method is used to triangulate polygons with more than 3 vertices contained in the file.- Parameters:
vertices- list of vertices forming a polygon to be triangulated.- Returns:
- a set containing arrays of indices of vertices (in string format) corresponding to the triangles forming the polygon after the triangulation.
- Throws:
com.irurueta.geometry.TriangulatorException- if triangulation fails (because polygon is degenerate or contains invalid values such as NaN or infinity).
-
getFaceValues
This method reads a line containing face (i.e. polygon) indices of vertices and fetches those vertices coordinates and associated data such as texture coordinates or normal coordinates.- Parameters:
values- a string containing vertex indices forming a polygon. Note that indices refer to the values contained in OBJ file, not the indices in the chunk of data.- Returns:
- a list of vertices forming a face (i.e, polygon).
- Throws:
IOException- if an I/O error occurs.LoaderException- if loading fails because data is corrupted or cannot be interpreted.
-
initChunkArrays
private void initChunkArrays()Initializes arrays forming current chunk of data. -
searchVertexIndexInChunk
private int searchVertexIndexInChunk(long originalIndex) Searches vertex index in current chunk of data by using the index used in the OBJ file. This method searches within the cached indices which relate indices in the chunk of data respect to indices in the OBJ file.- Parameters:
originalIndex- vertex index used in the OBJ file.- Returns:
- vertex index used in current chunk of data or -1 if not found.
-
searchTextureCoordIndexInChunk
private int searchTextureCoordIndexInChunk(long originalIndex) Searches texture index in current chunk of data by using the index used in the OBJ file. This method searches within the cached indices which relate indices in the chunk of data respect to indices in the OBJ file.- Parameters:
originalIndex- texture index used in the OBJ file.- Returns:
- texture index used in current chunk of data or -1 if not found.
-
searchNormalIndexInChunk
private int searchNormalIndexInChunk(long originalIndex) Searches normal index in current chunk of data by using the index used in the OBJ file. This method searches within the cached indices which relate indices in the chunk of data respect to indices in the OBJ file.- Parameters:
originalIndex- normal index used in the OBJ file.- Returns:
- normal index used in current chunk of data or -1 if not found.
-
addVertexPositionToMap
private void addVertexPositionToMap(long originalIndex, long streamPosition) Add vertex position to cache of file positions.- Parameters:
originalIndex- vertex index used in OBJ file.streamPosition- stream position where vertex is located.
-
addTextureCoordPositionToMap
private void addTextureCoordPositionToMap(long originalIndex, long streamPosition) Add texture coordinate position to cache of file positions.- Parameters:
originalIndex- texture coordinate index used in OBJ file.streamPosition- stream position where texture coordinate is located.
-
addNormalPositionToMap
private void addNormalPositionToMap(long originalIndex, long streamPosition) Add normal coordinate to cache of file positions.- Parameters:
originalIndex- normal coordinate index used in OBJ file.streamPosition- stream position where normal coordinate is located.
-
addNewVertexDataToChunk
private void addNewVertexDataToChunk()Adds data of last vertex being loaded to current chunk of data as a new vertex. -
addExistingVertexToChunk
private void addExistingVertexToChunk(int existingIndex) Adds index to current chunk of data referring to a previously existing vertex in the chunk.- Parameters:
existingIndex- index of vertex that already exists in the chunk.
-
increaseIndicesArraySize
private void increaseIndicesArraySize()Increases size of arrays of data. This method is called when needed. -
trimArrays
private void trimArrays()Trims arrays of data to reduce size of arrays to fit chunk data. This method is loaded just before copying data to chunk being returned. -
setUp
Setups loader iterator. This method is called when constructing this iterator.- Throws:
IOException- if an I/O error occurs.LoaderException- if data is corrupted or cannot be understood.
-