Skip to content

Commit

Permalink
Various cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 11, 2024
1 parent ed7bd02 commit 0edadec
Show file tree
Hide file tree
Showing 22 changed files with 1,034 additions and 487 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* text=auto eol=lf
tests/fixtures/** -text
1 change: 0 additions & 1 deletion .github/.cspell/project-dictionary.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
assimp
binormal
bitangent
brep
Expand Down
6 changes: 4 additions & 2 deletions .github/.cspell/rust-dependencies.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// This file is @generated by spell-check.sh.
// It is not intended for manual editing.

indexmap
assimp
fastrand
memchr
quickcheck
roxmltree
rustc
walkdir
35 changes: 31 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,41 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy,rustfmt
- run: cargo test --workspace --all-features --exclude example

features:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Install cargo-hack
uses: taiki-e/install-action@cargo-hack
- run: cargo hack build --workspace --ignore-private --feature-powerset
- run: cargo hack build --workspace --ignore-private --feature-powerset --rust-version

fmt:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo fmt --all --check

clippy:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo clippy --workspace --all-features --all-targets
- run: cargo hack build --workspace --ignore-private --feature-powerset
- run: cargo test --workspace --all-features

docs:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo doc --workspace --all-features

spell-check:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target
Cargo.lock
tests/fixtures/assimp/*
26 changes: 16 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "mesh-loader"
version = "0.0.2"
version = "0.1.0"
edition = "2021"
rust-version = "1.61"
license = "Apache-2.0"
repository = "https://github.com/openrr/mesh-loader"
keywords = ["asset", "mesh", "stl", "collada"] # TODO: "obj"
Expand All @@ -11,19 +12,15 @@ description = """
Fast parser for 3D-model-formats.
"""

[workspace]
resolver = "2"
members = ["example"]

[features]
default = ["stl", "collada"]

# STL (.stl)
# https://en.wikipedia.org/wiki/STL_(file_format)
stl = ["rustc-hash"]
stl = ["memchr"]
# COLLADA (.dae)
# https://en.wikipedia.org/wiki/COLLADA
collada = ["indexmap", "roxmltree"]
collada = ["roxmltree"]
# TODO
# # Wavefront OBJ (.obj)
# # https://en.wikipedia.org/wiki/Wavefront_.obj_file
Expand All @@ -32,20 +29,29 @@ collada = ["indexmap", "roxmltree"]
[dependencies]
# Used in all formats.
fast-float = "0.2"
memchr = "2.4"

# Used in STL parsing.
rustc-hash = { version = "1.1", optional = true }
memchr = { version = "2.4", optional = true }

# Used in COLLADA parsing.
indexmap = { version = "2.0.0", optional = true, features = ["std"] }
roxmltree = { version = "0.19", optional = true }

[dev-dependencies]
anyhow = "1"
assimp = "0.3"
duct = "0.13"
fastrand = "2"
fs-err = "2"
quickcheck = { default-features = false, git = "https://github.com/taiki-e/quickcheck.git", branch = "dev" } # https://github.com/BurntSushi/quickcheck/pull/304 + https://github.com/BurntSushi/quickcheck/pull/282 + lower MSRV
walkdir = "2"

[lints]
workspace = true

[workspace]
resolver = "2"
members = ["example"]

[workspace.lints.rust]
missing_debug_implementations = "warn"
# missing_docs = "warn" # TODO
Expand Down
4 changes: 1 addition & 3 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ mesh-loader = { path = ".." }

anyhow = "1"
clap = { version = "3", features = ["derive"] }
kiss3d = "0.35"
tracing = { version = "0.1", default-features = false, features = ["std"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["ansi", "env-filter"] }
kiss3d = { git = "https://github.com/sebcrozet/kiss3d.git", rev = "da70cd0", features = ["vertex_index_u32"] }

[lints]
workspace = true
121 changes: 65 additions & 56 deletions example/examples/kiss3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use anyhow::{bail, Result};
use clap::Parser;
use kiss3d::{light::Light, nalgebra as na, scene::SceneNode, window::Window};
use na::{Translation3, UnitQuaternion, Vector3};
use tracing::debug;

#[derive(Debug, Parser)]
struct Args {
Expand All @@ -38,9 +37,8 @@ impl FromStr for Scale {
}

fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let args = Args::parse();
debug!(?args);
eprintln!("args={args:?}");
let path = &args.path;
let scale = Vector3::new(args.scale.0, args.scale.1, args.scale.2);

Expand Down Expand Up @@ -73,18 +71,18 @@ fn main() -> Result<()> {
}

fn add_stl(window: &mut Window, path: &Path, scale: na::Vector3<f32>) -> io::Result<SceneNode> {
let stl = mesh_loader::stl::from_slice(&fs::read(path)?)?;
let stl = mesh_loader::Mesh::merge(mesh_loader::stl::from_slice(&fs::read(path)?)?.meshes);
eprintln!(
"name={:?},vertices={},faces={}",
stl.name,
stl.vertices.len(),
stl.faces.len()
);
let mesh = kiss3d::resource::Mesh::new(
stl.vertices.into_iter().map(Into::into).collect(),
stl.faces
.into_iter()
.map(|f| {
na::Point3::new(
f[0].try_into().unwrap(),
f[1].try_into().unwrap(),
f[2].try_into().unwrap(),
)
})
.map(|f| na::Point3::new(f[0], f[1], f[2]))
.collect(),
Some(stl.normals.into_iter().map(Into::into).collect()),
None,
Expand All @@ -94,51 +92,62 @@ fn add_stl(window: &mut Window, path: &Path, scale: na::Vector3<f32>) -> io::Res
Ok(window.add_mesh(mesh, scale))
}

fn add_collada(window: &mut Window, path: &Path, scale: na::Vector3<f32>) -> Result<SceneNode> {
fn add_collada(window: &mut Window, path: &Path, scale: na::Vector3<f32>) -> io::Result<SceneNode> {
let mut base = window.add_group();
let collada = mesh_loader::collada::from_str(&fs::read_to_string(path)?)?;
for mesh in collada.meshes {
debug!(
"name={},vertices={},normals={},texcoords0={},texcoords1={},faces={}",
mesh.name,
mesh.vertices.len(),
mesh.normals.len(),
mesh.texcoords[0].len(),
mesh.texcoords[1].len(),
mesh.faces.len()
);
let positions = mesh.vertices.iter().map(|&v| na::Point3::from(v)).collect();
let normals = if mesh.normals.is_empty() {
None
} else {
Some(mesh.normals.iter().map(|&v| na::Vector3::from(v)).collect())
};
let texcoords = if mesh.texcoords[0].is_empty() {
None
} else {
Some(
mesh.texcoords[0]
.iter()
.map(|&v| na::Point2::from(v))
.collect(),
)
};
let faces = mesh
.faces
.iter()
.map(|v| na::Point3::new(v[0] as u16, v[1] as u16, v[2] as u16))
.collect();
let mut _scene = base.add_mesh(
Rc::new(RefCell::new(kiss3d::resource::Mesh::new(
positions, faces, normals, texcoords, false,
))),
scale,
);

// TODO(material)
// if let Some(path) = materials.get(0) {
// scene.set_texture_from_file(path, path.to_str().unwrap());
// }
}
let collada = mesh_loader::Mesh::merge(
mesh_loader::collada::from_str(&fs::read_to_string(path)?)?.meshes,
);
eprintln!(
"name={:?},vertices={},normals={},texcoords0={},texcoords1={},faces={}",
collada.name,
collada.vertices.len(),
collada.normals.len(),
collada.texcoords[0].len(),
collada.texcoords[1].len(),
collada.faces.len()
);
let positions = collada
.vertices
.iter()
.map(|&v| na::Point3::from(v))
.collect();
let normals = if collada.normals.is_empty() {
None
} else {
Some(
collada
.normals
.iter()
.map(|&v| na::Vector3::from(v))
.collect(),
)
};
let texcoords = if collada.texcoords[0].is_empty() {
None
} else {
Some(
collada.texcoords[0]
.iter()
.map(|&v| na::Point2::from(v))
.collect(),
)
};
let faces = collada
.faces
.iter()
.map(|v| na::Point3::new(v[0], v[1], v[2]))
.collect();
let mut _scene = base.add_mesh(
Rc::new(RefCell::new(kiss3d::resource::Mesh::new(
positions, faces, normals, texcoords, false,
))),
scale,
);

// TODO(material)
// if let Some(path) = materials.get(0) {
// scene.set_texture_from_file(path, path.to_str().unwrap());
// }

Ok(base)
}
Loading

0 comments on commit 0edadec

Please sign in to comment.