Class LoaderOBJ

java.lang.Object
com.irurueta.geometry.io.Loader
com.irurueta.geometry.io.LoaderOBJ
All Implemented Interfaces:
Closeable, AutoCloseable

public class LoaderOBJ extends Loader
Loads an OBJ file. If a LoaderListenerOBJ is provided, this class might also attempt to load the associated material file if available.
  • Field Details

    • DEFAULT_MAX_VERTICES_IN_CHUNK

      public static final int DEFAULT_MAX_VERTICES_IN_CHUNK
      Constant defining the default value of maximum number of vertices to keep in a chunk. This is 65535, which corresponds to the maximum value allowed by graphical layer such as OpenGL when working with Vertex Buffer Objects.
      See Also:
    • MIN_MAX_VERTICES_IN_CHUNK

      public static final int MIN_MAX_VERTICES_IN_CHUNK
      Minimum allowed value for maximum number of vertices in chunk, which is one.
      See Also:
    • DEFAULT_ALLOW_DUPLICATE_VERTICES_IN_CHUNK

      public static final boolean DEFAULT_ALLOW_DUPLICATE_VERTICES_IN_CHUNK
      Constant indicating that duplicated vertices are allowed by default, which allows faster loading.
      See Also:
    • DEFAULT_MAX_STREAM_POSITIONS

      public static final int DEFAULT_MAX_STREAM_POSITIONS
      Maximum number of stream positions to be cached by default.
      See Also:
    • MIN_STREAM_POSITIONS

      public static final int MIN_STREAM_POSITIONS
      Minimum allowed number of stream positions.
      See Also:
    • PROGRESS_DELTA

      public static final float PROGRESS_DELTA
      Amount of progress variation (1%) used to notify progress.
      See Also:
    • DEFAULT_CONTINUE_IF_TRIANGULATION_ERROR

      public static final boolean DEFAULT_CONTINUE_IF_TRIANGULATION_ERROR
      Indicates that loading should continue even if triangulation of some polygons fails.
      See Also:
    • USEMTL

      private static final String USEMTL
      Identifies materials.
      See Also:
    • loaderIterator

      private LoaderOBJ.LoaderIteratorOBJ loaderIterator
      Iterator to load OBJ file data in small chunks. Usually data is divided in chunks that can be directly loaded by graphic layers such as OpenGL.
    • maxVerticesInChunk

      private int maxVerticesInChunk
      Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
    • allowDuplicateVerticesInChunk

      private boolean allowDuplicateVerticesInChunk
      To allow faster file loading, it might be allowed to repeat points in a chunk. When representing data graphically, this has no visual. consequences but chunks will take up more memory. This value represents a trade-off between loading speed and memory usage.
    • maxStreamPositions

      private long maxStreamPositions
      Maximum number of file stream positions to be cached. This class keeps a cache of positions in the file to allow faster file loading at the expense of larger memory usage. If the geometry of a file reuses a large number of points, keeping a large cache will increase the speed of loading a file, otherwise the impact of this parameter will be low. The default value will work fine for most cases.
    • comments

      private final List<String> comments
      List containing comments contained in the file.
    • materials

      private Set<Material> materials
      Collection of materials contained in the material's file associated to an OBJ file.
    • continueIfTriangulationError

      private boolean continueIfTriangulationError
      Determines if file loading should continue even if the triangulation of a polygon fails. The triangulation of a polygon might fail if the polygon is degenerate or has invalid numerical values such as NaN of infinity. If true, loading will continue but the result will lack the polygons that failed.
  • Constructor Details

    • LoaderOBJ

      public LoaderOBJ()
      Constructor.
    • LoaderOBJ

      public LoaderOBJ(int maxVerticesInChunk)
      Constructor.
      Parameters:
      maxVerticesInChunk - Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      Throws:
      IllegalArgumentException - if maximum number of vertices allowed in a chunk is lower than 1.
    • LoaderOBJ

      public LoaderOBJ(int maxVerticesInChunk, boolean allowDuplicateVerticesInChunk)
      Constructor.
      Parameters:
      maxVerticesInChunk - Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      allowDuplicateVerticesInChunk - indicates if repeated vertices in a chunk are allowed to provide faster file loading. When representing data graphically, this has no visual consequences but chunks will take up more memory.
      Throws:
      IllegalArgumentException - if maximum number of vertices allowed in a chunk is lower than 1.
    • LoaderOBJ

      public LoaderOBJ(int maxVerticesInChunk, boolean allowDuplicateVerticesInChunk, long maxStreamPositions)
      Constructor.
      Parameters:
      maxVerticesInChunk - Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      allowDuplicateVerticesInChunk - indicates if repeated vertices in a chunk are allowed to provide faster file loading. When representing data graphically, this has no visual consequences but chunks will take up more memory.
      maxStreamPositions - Maximum number of file stream positions to be cached.
      Throws:
      IllegalArgumentException - if maximum number of vertices allowed in a chunk is lower than 1.
    • LoaderOBJ

      public LoaderOBJ(File f) throws IOException
      Constructor.
      Parameters:
      f - file to be loaded.
      Throws:
      IOException - if an I/O error occurs.
    • LoaderOBJ

      public LoaderOBJ(File f, int maxVerticesInChunk) throws IOException
      Constructor.
      Parameters:
      f - file to be loaded.
      maxVerticesInChunk - Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      Throws:
      IllegalArgumentException - if maximum number of vertices allowed in a chunk is lower than 1.
      IOException - if an I/O error occurs.
    • LoaderOBJ

      public LoaderOBJ(File f, int maxVerticesInChunk, boolean allowDuplicateVerticesInChunk) throws IOException
      Constructor.
      Parameters:
      f - file to be loaded.
      maxVerticesInChunk - Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      allowDuplicateVerticesInChunk - indicates if repeated vertices in a chunk are allowed to provide faster file loading. When representing data graphically, this has no visual consequences but chunks will take up more memory.
      Throws:
      IllegalArgumentException - if maximum number of vertices allowed in a chunk is lower than 1.
      IOException - if an I/O error occurs.
    • LoaderOBJ

      public LoaderOBJ(File f, int maxVerticesInChunk, boolean allowDuplicateVerticesInChunk, long maxStreamPositions) throws IOException
      Constructor.
      Parameters:
      f - file to be loaded.
      maxVerticesInChunk - Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      allowDuplicateVerticesInChunk - indicates if repeated vertices in a chunk are allowed to provide faster file loading. When representing data graphically, this has no visual consequences but chunks will take up more memory.
      maxStreamPositions - Maximum number of file stream positions to be cached.
      Throws:
      IllegalArgumentException - if maximum number of vertices allowed in a chunk is lower than 1.
      IOException - if an I/O error occurs.
    • LoaderOBJ

      public LoaderOBJ(LoaderListener listener)
      Constructor.
      Parameters:
      listener - listener to be notified of loading progress and when loading process starts or finishes.
    • LoaderOBJ

      public LoaderOBJ(LoaderListener listener, int maxVerticesInChunk)
      Constructor.
      Parameters:
      listener - listener to be notified of loading progress and when loading process starts or finishes.
      maxVerticesInChunk - Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      Throws:
      IllegalArgumentException - if maximum number of vertices allowed in a chunk is lower than 1.
    • LoaderOBJ

      public LoaderOBJ(LoaderListener listener, int maxVerticesInChunk, boolean allowDuplicateVerticesInChunk)
      Constructor.
      Parameters:
      listener - listener to be notified of loading progress and when loading process starts or finishes.
      maxVerticesInChunk - Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      allowDuplicateVerticesInChunk - indicates if repeated vertices in a chunk are allowed to provide faster file loading. When representing data graphically, this has no visual consequences but chunks will take up more memory.
      Throws:
      IllegalArgumentException - if maximum number of vertices allowed in a chunk is lower than 1.
    • LoaderOBJ

      public LoaderOBJ(LoaderListener listener, int maxVerticesInChunk, boolean allowDuplicateVerticesInChunk, long maxStreamPositions)
      Constructor.
      Parameters:
      listener - listener to be notified of loading progress and when loading process starts or finishes.
      maxVerticesInChunk - Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      allowDuplicateVerticesInChunk - indicates if repeated vertices in a chunk are allowed to provide faster file loading. When representing data graphically, this has no visual consequences but chunks will take up more memory.
      maxStreamPositions - Maximum number of file stream positions to be cached.
      Throws:
      IllegalArgumentException - if maximum number of vertices allowed in a chunk is lower than 1.
    • LoaderOBJ

      public LoaderOBJ(File f, LoaderListener listener) throws IOException
      Constructor.
      Parameters:
      f - file to be loaded.
      listener - listener to be notified of loading progress and when loading process starts or finishes.
      Throws:
      IOException - if an I/O error occurs.
    • LoaderOBJ

      public LoaderOBJ(File f, LoaderListener listener, int maxVerticesInChunk) throws IOException
      Constructor.
      Parameters:
      f - file to be loaded.
      listener - listener to be notified of loading progress and when loading process starts or finishes.
      maxVerticesInChunk - Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      Throws:
      IllegalArgumentException - if maximum number of vertices allowed in a chunk is lower than 1.
      IOException - if an I/O error occurs.
    • LoaderOBJ

      public LoaderOBJ(File f, LoaderListener listener, int maxVerticesInChunk, boolean allowDuplicateVerticesInChunk) throws IOException
      Constructor.
      Parameters:
      f - file to be loaded.
      listener - listener to be notified of loading progress and when loading process starts or finishes.
      maxVerticesInChunk - Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      allowDuplicateVerticesInChunk - indicates if repeated vertices in a chunk are allowed to provide faster file loading. When representing data graphically, this has no visual consequences but chunks will take up more memory.
      Throws:
      IllegalArgumentException - if maximum number of vertices allowed in a chunk is lower than 1.
      IOException - if an I/O error occurs.
    • LoaderOBJ

      public LoaderOBJ(File f, LoaderListener listener, int maxVerticesInChunk, boolean allowDuplicateVerticesInChunk, long maxStreamPositions) throws IOException
      Constructor.
      Parameters:
      f - file to be loaded.
      listener - listener to be notified of loading progress and when loading process starts or finishes.
      maxVerticesInChunk - Maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      allowDuplicateVerticesInChunk - indicates if repeated vertices in a chunk are allowed to provide faster file loading. When representing data graphically, this has no visual consequences but chunks will take up more memory.
      maxStreamPositions - Maximum number of file stream positions to be cached.
      Throws:
      IllegalArgumentException - if maximum number of vertices allowed in a chunk is lower than 1.
      IOException - if an I/O error occurs.
  • Method Details

    • getMaxVerticesInChunk

      public int getMaxVerticesInChunk()
      Returns maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      Returns:
      maximum number of vertices allowed in a chunk.
    • setMaxVerticesInChunk

      public void setMaxVerticesInChunk(int maxVerticesInChunk) throws LockedException
      Sets maximum number of vertices allowed in a chunk. Once this value is exceeded when loading a file, a new chunk of data is created.
      Parameters:
      maxVerticesInChunk - maximum allowed number of vertices to be set.
      Throws:
      IllegalArgumentException - if provided value is lower than 1.
      LockedException - if this loader is currently loading a file.
    • areDuplicateVerticesInChunkAllowed

      public boolean areDuplicateVerticesInChunkAllowed()
      Returns boolean indicating if repeated vertices in a chunk are allowed to provide faster file loading. When representing data graphically, this has no visual consequences but chunks will take up more memory.
      Returns:
      true if duplicate vertices are allowed, false otherwise.
    • setAllowDuplicateVerticesInChunk

      public void setAllowDuplicateVerticesInChunk(boolean allow) throws LockedException
      Sets boolean indicating if repeated vertices in a chunk are allowed to provide faster file loading. When representing data graphically, this has no visual consequences but chunks will take up more memory.
      Parameters:
      allow - true if duplicate vertices are allowed, false otherwise.
      Throws:
      LockedException - if this loader is currently loading a file.
    • getMaxStreamPositions

      public long getMaxStreamPositions()
      Returns maximum number of file stream positions to be cached. This class keeps a cache of positions in the file to allow faster file loading at the expense of larger memory usage. If the geometry of a file reuses a large number of points, keeping a large cache will increase the speed of loading a file, otherwise the impact of this parameter will be low. The default value will work fine for most cases.
      Returns:
      maximum number of file stream positions to be cached.
    • setMaxStreamPositions

      public void setMaxStreamPositions(long maxStreamPositions) throws LockedException
      Sets maximum number of file stream positions to be cached. This class keeps a cache of positions in the file to allow faster file loading at the expense of larger memory usage. If the geometry of a file reuses a large number of points, keeping a large cache will increase the speed of loading a file, otherwise the impact of this parameter will be low. The default value will work fine for most cases.
      Parameters:
      maxStreamPositions - maximum number of file stream positions to be set.
      Throws:
      IllegalArgumentException - if provided value is lower than 1.
      LockedException - if this loader is currently loading a file.
    • isContinueIfTriangulationError

      public boolean isContinueIfTriangulationError()
      Returns boolean indicating if file loading should continue even if the triangulation of a polygon fails. The triangulation of a polygon might fail if the polygon is degenerate or has invalid numerical values such as NaN of infinity.
      Returns:
      If true, loading will continue but the result will lack the polygons that failed.
    • setContinueIfTriangulationError

      public void setContinueIfTriangulationError(boolean continueIfTriangulationError)
      Sets boolean indicating if file loading should continue even if the triangulation of a polygon fails. The triangulation of a polygon might fail if the polygon is degenerate or has invalid numerical values such as NaN or infinity.
      Parameters:
      continueIfTriangulationError - if ture, loading will continue but the result will lack the polygons that failed.
    • getComments

      public List<String> getComments()
      Returns a list of the comments contained in the file.
      Returns:
      list of the comments contained in the file.
    • getMaterials

      public Set<Material> getMaterials()
      Gets collection of materials contained in the materials file associated to an OBJ file.
      Returns:
      collection of material.
    • isReady

      public boolean isReady()
      If loader is ready to start loading a file. This is true once a file has been provided.
      Specified by:
      isReady in class Loader
      Returns:
      true if ready to start loading a file, false otherwise.
    • getMeshFormat

      public MeshFormat getMeshFormat()
      Returns mesh format supported by this class, which is MESH_FORMAT_OBJ.
      Specified by:
      getMeshFormat in class Loader
      Returns:
      mesh format supported by this class.
    • isValidFile

      public boolean isValidFile() throws LockedException, IOException
      Determines if provided file is a valid file that can be read by this loader.
      Specified by:
      isValidFile in class Loader
      Returns:
      true if file is valid, false otherwise.
      Throws:
      LockedException - raised if this instance is already locked.
      IOException - if an I/O error occurs..
    • load

      Starts the loading process of provided file. This method returns a LoaderIterator to start the iterative process to load a file in small chunks of data.
      Specified by:
      load in class Loader
      Returns:
      a loader iterator to read the file in a step-by-step process.
      Throws:
      LockedException - raised if this instance is already locked.
      NotReadyException - raised if this instance is not yet ready.
      IOException - if an I/O error occurs.
      LoaderException - if file is corrupted or cannot be interpreted.
    • internalSetMaxVerticesInChunk

      private void internalSetMaxVerticesInChunk(int maxVerticesInChunk)
      Internal method to set maximum number of vertices allowed in a chunk. This method is reused both in the constructor and in the setter of maximum number of vertices allowed in a chunk.
      Parameters:
      maxVerticesInChunk - maximum allowed number of vertices to be set.
      Throws:
      IllegalArgumentException - if provided value is lower than 1.
    • internalSetMaxStreamPositions

      private void internalSetMaxStreamPositions(long maxStreamPositions)
      Internal method to set maximum number of file stream positions to be cached. This method is reused both in the constructor and in the setter of maximum number stream positions.
      Parameters:
      maxStreamPositions - maximum number of file stream positions to be cached.
      Throws:
      IllegalArgumentException - if provided value is lower than 1.