Skip to main content

monstertruck_mesh/
lib.rs

1//! Polygon mesh data structures (triangle/quad/n-gon) with OBJ and STL I/O.
2
3#![cfg_attr(not(debug_assertions), deny(warnings))]
4#![deny(clippy::all, rust_2018_idioms)]
5#![warn(
6    missing_docs,
7    missing_debug_implementations,
8    trivial_casts,
9    trivial_numeric_casts,
10    unsafe_code,
11    unstable_features,
12    unused_import_braces,
13    unused_qualifications
14)]
15
16use array_macro::array;
17use serde::{Deserialize, Serialize};
18
19/// re-export `monstertruck_core`.
20pub mod base {
21    pub use monstertruck_core::{
22        assert_near, assert_near2, bounding_box::BoundingBox, cgmath64::*, hash, hash::HashGen,
23        prop_assert_near, prop_assert_near2, tolerance::*,
24    };
25    pub use monstertruck_traits::*;
26}
27pub use base::*;
28
29/// Attribution container for polygon mesh
30pub trait Attributes<V> {
31    /// attribution
32    type Output;
33    /// get attribution corresponding to vertex
34    fn get(&self, vertex: V) -> Option<Self::Output>;
35}
36
37/// transform attributions
38pub trait TransformedAttributes: Clone {
39    /// transform by `trans`.
40    fn transform_by(&mut self, trans: Matrix4);
41    /// transformed attributions by `trans`.
42    fn transformed(&self, trans: Matrix4) -> Self;
43}
44
45/// standard attributions
46#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
47pub struct StandardAttributes {
48    /// positions
49    pub positions: Vec<Point3>,
50    /// texture uv coordinates
51    pub uv_coords: Vec<Vector2>,
52    /// normals at vertices
53    pub normals: Vec<Vector3>,
54}
55
56/// standard attribution
57#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
58pub struct StandardAttribute {
59    /// position
60    pub position: Point3,
61    /// texture uv coordinate
62    pub uv_coord: Option<Vector2>,
63    /// normal at vertex
64    pub normal: Option<Vector3>,
65}
66
67/// Index vertex of a face of the polygon mesh
68#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug, Serialize, Deserialize)]
69pub struct StandardVertex {
70    /// index of vertex's position
71    pub pos: usize,
72    /// index of vertex's texture coordinate
73    pub uv: Option<usize>,
74    /// index of vertex's normal
75    pub nor: Option<usize>,
76}
77
78/// Faces of polygon mesh
79///
80/// To optimize for the case where the polygon mesh consists only triangles and quadrangle,
81/// there are vectors which consist by each triangles and quadrilaterals, internally.
82#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
83pub struct Faces<V = StandardVertex> {
84    tri_faces: Vec<[V; 3]>,
85    quad_faces: Vec<[V; 4]>,
86    other_faces: Vec<Vec<V>>,
87}
88
89/// Polygon mesh
90///
91/// The polygon data is held in a method compliant with wavefront obj.
92/// Position, uv (texture) coordinates, and normal vectors are held in separate arrays,
93/// and each face vertex accesses those values by an indices triple.
94#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
95pub struct PolygonMesh<V = StandardVertex, A = StandardAttributes> {
96    attributes: A,
97    faces: Faces<V>,
98}
99
100/// structured quadrangle mesh
101#[derive(Clone, Debug, Serialize)]
102pub struct StructuredMesh {
103    positions: Vec<Vec<Point3>>,
104    uv_division: Option<(Vec<f64>, Vec<f64>)>,
105    normals: Option<Vec<Vec<Vector3>>>,
106}
107
108/// polyline curve
109#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
110pub struct PolylineCurve<P>(pub Vec<P>);
111
112impl<S> ParameterBoundary2D<S> for PolylineCurve<Point3> {}
113
114impl<P: monstertruck_core::DeterministicContentHash> monstertruck_core::DeterministicContentHash
115    for PolylineCurve<P>
116{
117    fn content_hash<H: std::hash::Hasher>(&self, state: &mut H) { self.0.content_hash(state); }
118}
119
120mod attributes;
121/// Defines errors
122pub mod errors;
123mod expand;
124/// Defines triangle
125pub mod faces;
126mod meshing_shape;
127/// wavefront obj I/O
128pub mod obj;
129/// Defines [`PolygonMeshEditor`](./polygon_mesh/struct.PolygonMeshEditor.html).
130pub mod polygon_mesh;
131/// Defines generalized polyline curve.
132pub mod polyline_curve;
133/// STL I/O
134pub mod stl;
135mod structured_mesh;