OBJ: Wavefront Triangle Meshes
Overview
OBJ (Wavefront) is the most widely supported ASCII mesh format in 3D graphics and printing. It stores vertices (v x y z) and faces (f i j k, 1-indexed), with optional texture coordinates (vt), normals (vn), materials, and groups. Rasmah writes and reads the geometric core natively:
write_obj(mesh, io/path)— writesv/frecords.read_obj(io/path)— readsv/f, ignoresvt/vn/materials/groups, and fan-triangulates polygonal faces. Negative (f -1 -2 -3) andv/vt/vnindices are understood.
A mesh to exchange
OBJ operates on TriangleMesh. We build one from an SDF with marching cubes:
using Rasmah
mesh = surface_mesh(sphere(1.0); n=48)
println(size(mesh.vertices, 2), " vertices, ", size(mesh.faces, 2), " triangles")
vtk(mesh)2562 vertices, 5120 triangles
Writing and reading back
The round-trip preserves the shared-vertex connectivity exactly (OBJ indices are already shared, so unlike STL there is nothing to re-weld):
mktempdir() do dir
path = joinpath(dir, "sphere.obj")
write_obj(mesh, path)
println("wrote ", filesize(path), " bytes")
m2 = read_obj(path)
println("faces: ", size(m2.faces, 2), " (original ", size(mesh.faces, 2), ")")
println("area: ", round(surface_area(m2); digits=4),
" (original ", round(surface_area(mesh); digits=4), ")")
endwrote 234098 bytes
faces: 5120 (original 5120)
area: 12.5514 m^2 (original 12.5514 m^2)
Polygonal faces and relative indices
OBJ faces are not limited to triangles. read_obj fan-triangulates a polygon f 1 2 3 4 into (1,2,3), (1,3,4), and negative indices count back from the vertex added so far:
quad = """
v 0 0 0
v 1 0 0
v 1 1 0
v 0 1 0
f 1 2 3 4
"""
m = read_obj(IOBuffer(quad))
println(size(m.faces, 2), " triangles (a quad fan-triangulated), area ", surface_area(m))2 triangles (a quad fan-triangulated), area 1.0 m^2
What OBJ does not carry
OBJ stores only the mesh. It has no notion of the exact surface, the SDF, or the feature history — a box and a sphere triangulated to the same triangles are indistinguishable. For the exact model use STEP; for the construction recipe use .rasmah.
Next steps
- PLY: Polygon File Format — the binary/ASCII counterpart with typed properties.
- Converting Representations — what survives each mesh/geometry round-trip and what does not.
