Class LoaderOBJ.LoaderIteratorOBJ

java.lang.Object
com.irurueta.geometry.io.LoaderOBJ.LoaderIteratorOBJ
All Implemented Interfaces:
LoaderIterator
Enclosing class:
LoaderOBJ

private class LoaderOBJ.LoaderIteratorOBJ extends Object implements LoaderIterator
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 Details

    • loader

      private final LoaderOBJ loader
      Reference to loader loading OBJ file.
    • coordX

      private float coordX
      X coordinate of the latest point that has been read.
    • coordY

      private float coordY
      Y coordinate of the latest point that has been read.
    • coordZ

      private float coordZ
      Z coordinate of the latest point that has been read.
    • textureU

      private float textureU
      U 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 textureV
      V 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 nX
      X coordinate of the latest point normal that has been read.
    • nY

      private float nY
      Y coordinate of the latest point normal that has been read.
    • nZ

      private float nZ
      Z coordinate of the latest point normal that has been read.
    • vertexIndex

      private int vertexIndex
      Vertex index in the file of the latest point that has been read.
    • textureIndex

      private int textureIndex
      Texture index in the file of the latest point that has been read.
    • normalIndex

      private int normalIndex
      Normal index in the file of the latest point that has been read.
    • minX

      private float minX
      X 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 minY
      Y 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 minZ
      Z 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 maxX
      X 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 maxY
      Y 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 maxZ
      Z 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 verticesAvailable
      Indicates if vertices have been loaded and must be added to current chunk being loaded.
    • textureAvailable

      private boolean textureAvailable
      Indicates if texture coordinates have been loaded and must be added to current chunk being loaded.
    • normalsAvailable

      private boolean normalsAvailable
      Indicates if normals have been loaded and must be added to current chunk being loaded.
    • indicesAvailable

      private boolean indicesAvailable
      Indicates if indices have been loaded and must be added to current chunk being loaded.
    • materialsAvailable

      private boolean materialsAvailable
      Indicates if materials have been loaded and must be added to current chunk being loaded.
    • numberOfVertices

      private long numberOfVertices
      Number of vertices that have been loaded in current chunk.
    • numberOfTextureCoords

      private long numberOfTextureCoords
      Number of texture coordinates that have been loaded in current chunk.
    • numberOfNormals

      private long numberOfNormals
      Number of normals that have been loaded in current chunk.
    • numberOfFaces

      private long numberOfFaces
      Number of faces (i.e. polygons) that have been loaded in current chunk.
    • currentFace

      private long currentFace
      Index of current face (i.e. polygon) that has been loaded.
    • firstVertexStreamPosition

      private long firstVertexStreamPosition
      Position of first vertex in the file. This is stored to reduce fetching time when parsing the OBJ file.
    • firstVertexStreamPositionAvailable

      private boolean firstVertexStreamPositionAvailable
      Indicates if first vertex position has been found.
    • firstTextureCoordStreamPosition

      private long firstTextureCoordStreamPosition
      Position of first texture coordinate in the file. This is stored to reduce fetching time when parsing the OBJ file.
    • firstTextureCoordStreamPositionAvailable

      private boolean firstTextureCoordStreamPositionAvailable
      Indicates if first texture coordinate has been found.
    • firstNormalStreamPosition

      private long firstNormalStreamPosition
      Position of first normal coordinate in the file. This is stored to reduce fetching time when parsing the OBJ file.
    • firstNormalStreamPositionAvailable

      private boolean firstNormalStreamPositionAvailable
      Indicates if first normal coordinate has been found.
    • firstFaceStreamPosition

      private long firstFaceStreamPosition
      Position of first face (i.e. polygon) in the file. This is stored to reduce fetching time when parsing the OBJ file.
    • firstFaceStreamPositionAvailable

      private boolean firstFaceStreamPositionAvailable
      Indicates if first face has been found.
    • firstMaterialStreamPosition

      private long firstMaterialStreamPosition
      Indicates location of first material in the file. This is stored to reduce fetching time when parsing the OBJ file.
    • firstMaterialStreamPositionAvailable

      private boolean firstMaterialStreamPositionAvailable
      Indicates if first material has been found.
    • currentStreamPosition

      private long currentStreamPosition
      Contains position where file is currently being loaded.
    • listener

      private LoaderIteratorListener 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[] coordsInChunkArray
      Array containing vertices coordinates to be added to current chunk of data.
    • textureCoordsInChunkArray

      private float[] textureCoordsInChunkArray
      Array containing texture coordinates to be added to current chunk of data.
    • normalsInChunkArray

      private float[] normalsInChunkArray
      Array containing normal coordinates to be added to current chunk of data.
    • indicesInChunkArray

      private int[] indicesInChunkArray
      Array 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[] originalVertexIndicesInChunkArray
      Array 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[] originalTextureIndicesInChunkArray
      Array 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[] originalNormalIndicesInChunkArray
      Array 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

      private final TreeMap<Long,Integer> vertexIndicesMap
      Map to relate vertex indices in a file respect to chunk indices.
    • textureCoordsIndicesMap

      private final TreeMap<Long,Integer> textureCoordsIndicesMap
      Map to relate texture coordinates indices in a file respect to chunk indices.
    • normalsIndicesMap

      private final TreeMap<Long,Integer> normalsIndicesMap
      Map to relate normals coordinates indices in a file respect to chunk indices.
    • verticesStreamPositionMap

      private final TreeMap<Long,Long> verticesStreamPositionMap
      Map to cache vertex positions in a file.
    • textureCoordsStreamPositionMap

      private final TreeMap<Long,Long> textureCoordsStreamPositionMap
      Map to cache texture coordinates positions in a file.
    • normalsStreamPositionMap

      private final TreeMap<Long,Long> normalsStreamPositionMap
      Map to cache normals coordinates positions in a file.
    • verticesInChunk

      private int verticesInChunk
      Number of vertices stored in chunk.
    • indicesInChunk

      private int indicesInChunk
      Number of indices stored in chunk.
    • indicesInChunkSize

      private int indicesInChunkSize
      Size of indices stored in chunk.
    • vertexStreamPosition

      private long vertexStreamPosition
      Vertex position in file.
    • textureCoordStreamPosition

      private long textureCoordStreamPosition
      Texture coordinate position in file.
    • normalStreamPosition

      private long normalStreamPosition
      Normal coordinate position in file.
    • currentChunkMaterialName

      private String currentChunkMaterialName
      Name of current material of data being loaded.
    • currentMaterial

      private MaterialOBJ currentMaterial
      Reference to current material of data being loaded.
    • materialLoader

      private MaterialLoaderOBJ materialLoader
      Reference to material loader in charge of loading the associated MTL of file to this OBJ file.
  • Constructor Details

  • Method Details

    • setListener

      public void setListener(LoaderIteratorListener listener)
      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:
      hasNext in interface LoaderIterator
      Returns:
      true if there is another chunk of data, false otherwise.
    • next

      Loads and returns next chunk of data, if available.
      Specified by:
      next in interface LoaderIterator
      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

      public void fetchVertex(long index) throws LoaderException, IOException
      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

      public void fetchTexture(long index) throws LoaderException, IOException
      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

      public void fetchNormal(long index) throws LoaderException, IOException
      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

      private List<VertexOBJ> getFaceValues(String[] values) throws IOException, LoaderException
      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

      private void setUp() throws IOException, LoaderException
      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.