tri_mesh/
lib.rs

1//!
2//! This crate contains a [Mesh] struct which represents a triangle mesh.
3//! It is implemented using a half-edge data structure which is efficient for creating, editing, traversing and computing on that mesh.
4//! Also, the mesh can easily be created from and exported into a format that is efficient for visualization.
5//! Finally, operations on the mesh is implemented as methods on the [Mesh] struct, so take a look at that rather long list of methods for a complete overview.
6//!
7
8#![warn(missing_docs)]
9
10pub mod math;
11
12mod mesh;
13pub use mesh::*;
14
15mod operations;
16pub use operations::*;
17
18use thiserror::Error;
19///
20/// Error when performing a mesh operation
21///
22#[derive(Debug, Error)]
23#[allow(missing_docs)]
24pub enum Error {
25    #[error("configuration is not valid: {0}")]
26    ActionWillResultInInvalidMesh(String),
27    #[error("action will produce a non-manifold mesh: {0}")]
28    ActionWillResultInNonManifoldMesh(String),
29    #[error("the mesh has ended up in an invalid state: {0}")]
30    MeshIsInvalid(String),
31}
32
33#[cfg(test)]
34mod test_utility {
35    use super::*;
36    use three_d_asset::{Indices, Positions, TriMesh};
37    /// Creates three connected triangles in `x = [-3, 3]`, `y = [-1, 2]` and `z = 0`
38    /// which covers a square in `x = [-1, 1]`, `y = [-1, 1]` and `z = 0`
39    /// and has a common vertex in `(0, 0, 0)`.
40    pub(crate) fn subdivided_triangle() -> Mesh {
41        TriMesh {
42            indices: Indices::U8(vec![0, 2, 3, 0, 3, 1, 0, 1, 2]),
43            positions: Positions::F64(vec![
44                vec3(0.0, 0.0, 0.0),
45                vec3(-3.0, -1.0, 0.0),
46                vec3(3.0, -1.0, 0.0),
47                vec3(0.0, 2.0, 0.0),
48            ]),
49            ..Default::default()
50        }
51        .into()
52    }
53
54    /// Creates a triangle in `x = [-3, 3]`, `y = [-1, 2]` and `z = 0` which covers a square in `x = [-1, 1]`, `y = [-1, 1]` and `z = 0`.
55    pub(crate) fn triangle() -> Mesh {
56        TriMesh {
57            indices: Indices::U8(vec![0, 1, 2]),
58            positions: Positions::F64(vec![
59                vec3(-3.0, -1.0, 0.0),
60                vec3(3.0, -1.0, 0.0),
61                vec3(0.0, 2.0, 0.0),
62            ]),
63            ..Default::default()
64        }
65        .into()
66    }
67
68    /// Creates a square in `x = [-1, 1]`, `y = [-1, 1]` and `z = 0`.
69    pub(crate) fn square() -> Mesh {
70        TriMesh {
71            indices: Indices::U8(vec![0, 1, 2, 2, 1, 3]),
72            positions: Positions::F64(vec![
73                vec3(-1.0, -1.0, 0.0),
74                vec3(1.0, -1.0, 0.0),
75                vec3(-1.0, 1.0, 0.0),
76                vec3(1.0, 1.0, 0.0),
77            ]),
78            ..Default::default()
79        }
80        .into()
81    }
82
83    pub(crate) fn triangle_strip() -> Mesh {
84        TriMesh {
85            indices: Indices::U8(vec![0, 1, 2, 2, 1, 3, 3, 1, 4, 3, 4, 5]),
86            positions: Positions::F64(vec![
87                vec3(0.0, 0.0, 0.0),
88                vec3(0.0, 0.0, 1.0),
89                vec3(1.0, 0.0, 0.5),
90                vec3(1.0, 0.0, 1.5),
91                vec3(0.0, 0.0, 2.0),
92                vec3(0.0, 0.0, 2.5),
93            ]),
94            ..Default::default()
95        }
96        .into()
97    }
98
99    pub(crate) fn cube() -> Mesh {
100        TriMesh {
101            indices: Indices::U8(vec![
102                0, 1, 2, 0, 2, 3, 4, 7, 6, 4, 6, 5, 0, 4, 5, 0, 5, 1, 1, 5, 6, 1, 6, 2, 2, 6, 7, 2,
103                7, 3, 4, 0, 3, 4, 3, 7,
104            ]),
105            positions: Positions::F64(vec![
106                vec3(1.0, -1.0, -1.0),
107                vec3(1.0, -1.0, 1.0),
108                vec3(-1.0, -1.0, 1.0),
109                vec3(-1.0, -1.0, -1.0),
110                vec3(1.0, 1.0, -1.0),
111                vec3(1.0, 1.0, 1.0),
112                vec3(-1.0, 1.0, 1.0),
113                vec3(-1.0, 1.0, -1.0),
114            ]),
115            ..Default::default()
116        }
117        .into()
118    }
119}