Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for STL model loading/deserialization #35

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ bmp = ["image/bmp"]

obj = ["wavefront_obj"]
gltf = ["dep:gltf"]
stl = ["dep:stl_io"]

pcd = ["pcd-rs"]

Expand All @@ -47,6 +48,7 @@ thiserror = "1"
reqwest = {version = "0.11", optional = true, default-features = false }
gltf = { version = "1", optional = true, features=["KHR_materials_ior", "KHR_materials_transmission"] }
wavefront_obj = { version = "10", optional = true }
stl_io = { version = "0.8.2", optional = true }
image = { version = "0.24", optional = true, default-features = false}
pcd-rs = { version = "0.10", optional = true, features = ["derive"] }
data-url = {version = "0.3", optional = true }
Expand Down
10 changes: 10 additions & 0 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pub use saver::*;
#[cfg(feature = "obj")]
mod obj;

#[cfg(feature = "stl")]
mod stl;

#[cfg(feature = "gltf")]
mod gltf;

Expand Down Expand Up @@ -181,6 +184,13 @@ impl Deserialize for crate::Scene {
#[cfg(feature = "obj")]
obj::deserialize_obj(raw_assets, &path)
}
"stl" => {
#[cfg(not(feature = "stl"))]
return Err(Error::FeatureMissing("stl".to_string()));

#[cfg(feature = "stl")]
stl::deserialize_stl(raw_assets, &path)
}
"pcd" => {
#[cfg(not(feature = "pcd"))]
return Err(Error::FeatureMissing("pcd".to_string()));
Expand Down
77 changes: 77 additions & 0 deletions src/io/stl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::path::PathBuf;

use crate::{Node, Positions, Result, TriMesh};

use crate::{io::RawAssets, Scene};

use cgmath::Vector3;

pub fn deserialize_stl(raw_assets: &mut RawAssets, path: &PathBuf) -> Result<Scene> {
let stl_bytes = raw_assets.remove(path)?;
let mut stl_bytes = std::io::Cursor::new(stl_bytes.to_vec());
let stl = stl_io::read_stl(&mut stl_bytes)?;

let positions = stl
.vertices
.iter()
.map(|vertex| Vector3 {
x: vertex[0],
y: vertex[1],
z: vertex[2],
})
.collect();

let mut indices = Vec::with_capacity(stl.faces.len() * 3);
let mut normals = Vec::with_capacity(stl.faces.len());
for face in stl.faces {
let face_indices = face.vertices;
indices.push(face_indices[0] as u32);
indices.push(face_indices[1] as u32);
indices.push(face_indices[2] as u32);

normals.push(Vector3 {
x: face.normal[0],
y: face.normal[1],
z: face.normal[2],
});
}

let mesh = TriMesh {
positions: Positions::F32(positions),
indices: crate::Indices::U32(indices),
normals: Some(normals),
tangents: None,
uvs: None,
colors: None,
};

// STL files contain only one object, so only one node
let node = Node {
geometry: Some(crate::Geometry::Triangles(mesh)),
..Default::default()
};

Ok(Scene {
// stl_io does not expose the name it seems, so using path instead
name: path.to_str().unwrap_or("default").to_owned(),
children: vec![node],
materials: vec![],
})
}

#[cfg(test)]
mod test {
#[test]
pub fn deserialize_stl_ascii() {
let model: crate::Model = crate::io::load_and_deserialize("test_data/cube.stl").unwrap();
assert_eq!(model.geometries.len(), 1);
assert_eq!(model.materials.len(), 0);
}

#[test]
pub fn deserialize_stl_binary() {
let model: crate::Model = crate::io::load_and_deserialize("test_data/suzanne.stl").unwrap();
assert_eq!(model.geometries.len(), 1);
assert_eq!(model.materials.len(), 0);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ pub enum Error {
#[error("error while parsing an .pcd file")]
Pcd(#[from] pcd_rs::anyhow::Error),

#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(not(target_arch = "wasm32"), feature = "stl"))]
#[error("io error")]
IO(#[from] std::io::Error),
#[cfg(feature = "gltf")]
Expand Down
86 changes: 86 additions & 0 deletions test_data/cube.stl
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
solid Exported from Blender-4.0.2
facet normal 0.000000 0.000000 1.000000
outer loop
vertex 1.000000 1.000000 1.000000
vertex -1.000000 1.000000 1.000000
vertex -1.000000 -1.000000 1.000000
endloop
endfacet
facet normal 0.000000 0.000000 1.000000
outer loop
vertex 1.000000 1.000000 1.000000
vertex -1.000000 -1.000000 1.000000
vertex 1.000000 -1.000000 1.000000
endloop
endfacet
facet normal 0.000000 -1.000000 0.000000
outer loop
vertex 1.000000 -1.000000 -1.000000
vertex 1.000000 -1.000000 1.000000
vertex -1.000000 -1.000000 1.000000
endloop
endfacet
facet normal 0.000000 -1.000000 0.000000
outer loop
vertex 1.000000 -1.000000 -1.000000
vertex -1.000000 -1.000000 1.000000
vertex -1.000000 -1.000000 -1.000000
endloop
endfacet
facet normal -1.000000 0.000000 0.000000
outer loop
vertex -1.000000 -1.000000 -1.000000
vertex -1.000000 -1.000000 1.000000
vertex -1.000000 1.000000 1.000000
endloop
endfacet
facet normal -1.000000 0.000000 0.000000
outer loop
vertex -1.000000 -1.000000 -1.000000
vertex -1.000000 1.000000 1.000000
vertex -1.000000 1.000000 -1.000000
endloop
endfacet
facet normal 0.000000 0.000000 -1.000000
outer loop
vertex -1.000000 1.000000 -1.000000
vertex 1.000000 1.000000 -1.000000
vertex 1.000000 -1.000000 -1.000000
endloop
endfacet
facet normal 0.000000 0.000000 -1.000000
outer loop
vertex -1.000000 1.000000 -1.000000
vertex 1.000000 -1.000000 -1.000000
vertex -1.000000 -1.000000 -1.000000
endloop
endfacet
facet normal 1.000000 0.000000 0.000000
outer loop
vertex 1.000000 1.000000 -1.000000
vertex 1.000000 1.000000 1.000000
vertex 1.000000 -1.000000 1.000000
endloop
endfacet
facet normal 1.000000 0.000000 0.000000
outer loop
vertex 1.000000 1.000000 -1.000000
vertex 1.000000 -1.000000 1.000000
vertex 1.000000 -1.000000 -1.000000
endloop
endfacet
facet normal 0.000000 1.000000 0.000000
outer loop
vertex -1.000000 1.000000 -1.000000
vertex -1.000000 1.000000 1.000000
vertex 1.000000 1.000000 1.000000
endloop
endfacet
facet normal 0.000000 1.000000 0.000000
outer loop
vertex -1.000000 1.000000 -1.000000
vertex 1.000000 1.000000 1.000000
vertex 1.000000 1.000000 -1.000000
endloop
endfacet
endsolid Exported from Blender-4.0.2
Binary file added test_data/suzanne.stl
Binary file not shown.
Loading