A Java library that enables you to read Object File Format (*.OFF) files and resources.
The Object File Format allows one to store 3D models into simple plain text format. Though not as popular as the alternative OBJ file format, it is still used in some applications. Files of this format generally look something like the following.
OFF
4 2 0
-1.0 1.0 0.0
-1.0 -1.0 0.0
1.0 -1.0 0.0
1.0 1.0 0.0
3 0 1 2
3 0 2 3
You can find more information and sample models at these links:
Using this library is meant to be easy and straightforward. All you need to do is instantiate an
OffLoader
and pass it an InputStream
to your OFF resource.
Example:
// Open a stream to your OFF resource
try (InputStream in = new FileInputStream("example.off")) {
// Create an OffLoader and parse the resource
final IOffLoader loader = new OffLoader();
final OffObject object = loader.load(in);
// Use the model representation to get some basic info
System.out.println(MessageFormat.format(
"OFF object loaded with {0} vertices and {1} faces.",
object.getVertices().size(),
object.getFaces().size()));
}
The idea behind the library's API is that you end up with an OffObject
Java model representation of the resource that is parsed. By navigating through the model, you could easily reconstruct the 3D model. Additionally, the Java model is mutable, so you are able to make corrections to the 3D object.
Example:
try (InputStream in = new FileInputStream("example.off")) {
final IOffLoader loader = new OffLoader();
final OffObject object = loader.load(in);
System.out.println("OFF Object with faces:");
int faceIndex = 0;
for (OffFace face : object.getFaces()) {
faceIndex++;
System.out.println(MessageFormat.format(
"\tFace with vertices:", faceIndex));
for (Integer reference : face.getVertexReferences()) {
final OffVertex vertex = object.getVertex(reference);
System.out.println(MessageFormat.format(
"\t\tVertex with coordinates: ({0}, {1}, {2})", vertex.x, vertex.y, vertex.z));
}
}
}
Even though this project relies on Maven for packaging, it has not been published to the central Maven repository. Following are a number of approaches to get the library imported in your project.
An amazing web page that allows one to import Maven projects directly from GitHub. It is ideal for publishing new and small projects like this one.
One only needs to add the following configuration in their pom.xml
file to get the library included.
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.mokiat</groupId>
<artifactId>java-data-off</artifactId>
<version>v2.0.1</version>
</dependency>
</dependencies>
JitPack works with other packaging frameworks as well. Check the official webpage for more information.
If JitPack
is not an option for your use case, then you could package the jar
files into your project. They are available for download from the Releases section of the repository.
You can use a set of commands to import the jar
files into your local Maven repository. Following are two available approaches. (I find the first one to do the job)