Merge pull request #472 from hannobraun/export

Extract `fj-export` from `fj-app`
This commit is contained in:
Hanno Braun 2022-04-13 13:59:49 +02:00 committed by GitHub
commit aafe282902
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 25 deletions

15
Cargo.lock generated
View File

@ -698,6 +698,7 @@ dependencies = [
"clap",
"figment",
"fj",
"fj-export",
"fj-host",
"fj-interop",
"fj-kernel",
@ -708,7 +709,6 @@ dependencies = [
"parry3d-f64",
"serde",
"thiserror",
"threemf",
"tracing",
"tracing-subscriber",
"wgpu",
@ -716,6 +716,15 @@ dependencies = [
"winit",
]
[[package]]
name = "fj-export"
version = "0.5.0"
dependencies = [
"fj-interop",
"fj-math",
"threemf",
]
[[package]]
name = "fj-host"
version = "0.5.0"
@ -2543,9 +2552,9 @@ dependencies = [
[[package]]
name = "threemf"
version = "0.2.0"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0c98800ca8044fa88359515e47c1e2a432dd463b7cfb0764307d5e643e6cc93"
checksum = "02a12818d477c54ac38deb1bd8e222036385b594c020f02097914b6ac81cfff3"
dependencies = [
"thiserror",
"zip",

View File

@ -18,7 +18,6 @@ futures = "0.3.21"
nalgebra = "0.30.0"
parry3d-f64 = "0.8.0"
thiserror = "1.0.30"
threemf = "0.2.0"
tracing = "0.1.33"
wgpu = "0.12.0"
wgpu_glyph = "0.16.0"
@ -36,6 +35,10 @@ features = ["env", "toml"]
version = "0.5.0"
path = "../fj"
[dependencies.fj-export]
version = "0.5.0"
path = "../fj-export"
[dependencies.fj-host]
version = "0.5.0"
path = "../fj-host"

View File

@ -9,6 +9,7 @@ use std::path::PathBuf;
use std::time::Instant;
use anyhow::anyhow;
use fj_export::export;
use fj_host::{Model, Parameters};
use fj_operations::shape_processor::ShapeProcessor;
use futures::executor::block_on;
@ -65,27 +66,7 @@ fn main() -> anyhow::Result<()> {
let shape = model.load_once(&parameters)?;
let shape = shape_processor.process(&shape);
let vertices =
shape.mesh.vertices().map(|vertex| vertex.into()).collect();
let indices: Vec<_> = shape.mesh.indices().collect();
let triangles = indices
.chunks(3)
.map(|triangle| {
[
triangle[0] as usize,
triangle[1] as usize,
triangle[2] as usize,
]
})
.collect();
let mesh = threemf::TriangleMesh {
vertices,
triangles,
};
threemf::write(path, &mesh)?;
export(&shape.mesh, &path)?;
return Ok(());
}

23
fj-export/Cargo.toml Normal file
View File

@ -0,0 +1,23 @@
[package]
name = "fj-export"
version = "0.5.0"
edition = "2021"
description = "The world needs another CAD program."
readme = "../README.md"
repository = "https://github.com/hannobraun/fornjot"
license = "0BSD"
keywords = ["cad", "programmatic", "code-cad"]
categories = ["encoding"]
[dependencies]
threemf = "0.3.0"
[dependencies.fj-interop]
version = "0.5.0"
path = "../fj-interop"
[dependencies.fj-math]
version = "0.5.0"
path = "../fj-math"

39
fj-export/src/lib.rs Normal file
View File

@ -0,0 +1,39 @@
//! Exporting Fornjot models to external files
#![deny(missing_docs)]
use std::path::Path;
use fj_interop::mesh::Mesh;
use fj_math::Point;
/// Export the provided mesh to the file at the given path
///
/// Currently only 3MF is supported as an export format. The file extension of
/// the provided path is ignored.
pub fn export(mesh: &Mesh<Point<3>>, path: &Path) -> Result<(), Error> {
let vertices = mesh.vertices().map(|vertex| vertex.into()).collect();
let indices: Vec<_> = mesh.indices().collect();
let triangles = indices
.chunks(3)
.map(|triangle| {
[
triangle[0] as usize,
triangle[1] as usize,
triangle[2] as usize,
]
})
.collect();
let mesh = threemf::TriangleMesh {
vertices,
triangles,
};
threemf::write(path, &mesh)?;
Ok(())
}
pub use threemf::Error;