Skip to content
michidkPublic

About

A simple .off file parser

Topics

Resources

Stars

6 stars

Watchers

1 watching

Forks

Latest commit

 

History

40 Commits

Folders and files

Repository files navigation

off-rs - A simple .off file parser

CI Crates.io docs.rs License

Parses .off (Object File Format) files. This implementation follows this spec from the Princeton Shape Benchmark.

Sample .off file:

# this file header has to be the first instruction
OFF
# cube.off
# A cube

# 8 vertices, 6 faces, 12 edges
8 6 12

# vertex coordinates: x, y, z
  1.632993   0.000000   1.154701
  0.000000   1.632993   1.154701
 -1.632993   0.000000   1.154701
  0.000000  -1.632993   1.154701
  1.632993   0.000000  -1.154701
  0.000000   1.632993  -1.154701
 -1.632993   0.000000  -1.154701
  0.000000  -1.632993  -1.154701

# face indices & RGBA color data: n, v1, v2, v3, v4, r, g, b, a
  4  0 1 2 3  1.000 0.000 0.000 0.75
  4  7 4 0 3  0.300 0.400 0.000 0.75
  4  4 5 1 0  0.200 0.500 0.100 0.75
  4  5 6 2 1  0.100 0.600 0.200 0.75
  4  3 2 6 7  0.000 0.700 0.300 0.75
  4  6 5 4 7  0.000 1.000 0.000 0.75

This cube.off file is parsed using off-rs in this example.

Usage

let off_string = r#"
OFF
3 1
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
3  0 1 2  255 0 0 # red
"#;

let mesh = off_rs::parse(
    off_string,
    Default::default() // optional ParserOptions
);

println!("{:#?}", mesh);

Will return a structure like this:

Mesh {
    vertices: [
        Vertex {
            position: Position {
                x: 1.0,
                y: 0.0,
                z: 0.0,
            },
            color: None,
        },
        ...
    faces: [
        Face {
            vertices: [
                0,
                1,
                2,
                3,
            ],
            color: Some(
                Color {
                    red: 1.0,
                    green: 0.0,
                    blue: 0.0,
                    alpha: 1.0,
                },
            ),
        },
        ...

Writing

A Mesh can be written back to the .off format:

use off_rs::writer::options::Options;
use off_rs::parser::color_format::ColorFormat;

let mesh = off_rs::from_path("cube.off", Default::default())?;

// as a string
let off_string = off_rs::to_off_string(&mesh, Options::default())?;

// to a file (or any `std::io::Write` using `off_rs::write`)
off_rs::to_path(&mesh, "copy.off", Options {
    color_format: ColorFormat::RGBInteger,
})?;

Colors are written in the configured color format. Formats without an alpha channel drop the alpha value, and integer formats round to the nearest u8. Meshes that can not be parsed again (faces with less than three vertices or with vertex indices that do not exist) are rejected with an error.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

About

A simple .off file parser

Topics

Resources

Stars

6 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages