fidget_mesh/lib.rs
1//! Octree construction and meshing
2//!
3//! This module implements
4//! [Manifold Dual Contouring](https://people.engr.tamu.edu/schaefer/research/dualsimp_tvcg.pdf),
5//! to generate a triangle mesh from an implicit surface (or anything
6//! implementing [`Shape`](fidget_core::shape::Shape)).
7//!
8//! The resulting meshes should be
9//! - Manifold
10//! - Watertight
11//! - Preserving sharp features (corners / edges)
12//!
13//! However, they may contain self-intersections, and are not guaranteed to
14//! catch thin features (below the sampling grid resolution).
15//!
16//! The resulting [`Mesh`] objects can be written out as STL files.
17//!
18//! Here's a full example, meshing a sphere:
19//!
20//! ```
21//! use fidget_core::{
22//! context::Tree,
23//! vm::VmShape
24//! };
25//! use fidget_mesh::{Octree, Settings};
26//!
27//! let radius_squared = Tree::x().square()
28//! + Tree::y().square()
29//! + Tree::z().square();
30//! let tree: Tree = radius_squared.sqrt() - 0.6;
31//! let shape = VmShape::from(tree);
32//! let settings = Settings {
33//! depth: 4,
34//! ..Default::default()
35//! };
36//! let o = Octree::build(&shape, &settings).unwrap();
37//! let mesh = o.walk_dual();
38//!
39//! // Open a file to write, e.g.
40//! // let mut f = std::fs::File::create("out.stl")?;
41//! # let mut f = vec![];
42//! mesh.write_stl(&mut f)?;
43//! # Ok::<(), std::io::Error>(())
44//! ```
45
46mod builder;
47mod cell;
48mod codegen;
49mod dc;
50mod frame;
51mod octree;
52mod output;
53mod qef;
54
55use fidget_core::render::{CancelToken, ThreadPool};
56
57#[doc(hidden)]
58pub mod types;
59
60// Re-export the main Octree type as public
61pub use octree::Octree;
62
63////////////////////////////////////////////////////////////////////////////////
64
65/// An indexed 3D mesh
66#[derive(Default, Debug)]
67pub struct Mesh {
68 /// Triangles, as indexes into [`self.vertices`](Self::vertices)
69 pub triangles: Vec<nalgebra::Vector3<usize>>,
70 /// Vertex positions
71 pub vertices: Vec<nalgebra::Vector3<f32>>,
72}
73
74impl Mesh {
75 /// Builds a new mesh
76 pub fn new() -> Self {
77 Self::default()
78 }
79}
80
81/// Settings when building an octree and mesh
82pub struct Settings<'a> {
83 /// Depth to recurse in the octree
84 pub depth: u8,
85
86 /// Viewport to provide a world-to-model transform
87 pub world_to_model: nalgebra::Matrix4<f32>,
88
89 /// Thread pool to use for rendering
90 ///
91 /// If this is `None`, then rendering is done in a single thread; otherwise,
92 /// the provided pool is used.
93 pub threads: Option<&'a ThreadPool>,
94
95 /// Token to cancel rendering
96 pub cancel: CancelToken,
97}
98
99impl Default for Settings<'_> {
100 fn default() -> Self {
101 Self {
102 depth: 3,
103 world_to_model: nalgebra::Matrix4::identity(),
104 threads: Some(&ThreadPool::Global),
105 cancel: CancelToken::new(),
106 }
107 }
108}