Skip to main content

kernel_api/
mesh_op.rs

1//! Mesh edits as plain data: the steps of a mesh body's history and what
2//! each reports.
3
4use serde::{Deserialize, Serialize};
5
6use crate::*;
7
8/// Part of a mesh, by where its points are: a face is in it when all
9/// three of its corners are.
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11pub enum MeshRegion {
12    All,
13    Box {
14        min: [f64; 3],
15        max: [f64; 3],
16    },
17    Ball {
18        centre: [f64; 3],
19        radius: f64,
20    },
21    /// Vertices of the history's start mesh, by number (a marking).
22    Vertices(Vec<u32>),
23}
24
25/// One step of a mesh's history, in the mesh's own frame.
26#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
27#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
28pub enum MeshOp {
29    /// Pieces whose bounding box's diagonal is shorter than this removed.
30    RemoveIslands { smaller_than: f64 },
31    /// The region's faces removed, the hole left open.
32    Delete { region: MeshRegion },
33    /// The region's faces turned over.
34    Flip { region: MeshRegion },
35    /// Fill open rims whose longest span is at most `holes_up_to`, or the
36    /// nearest such rim to `near` when given. Existing vertices stay fixed.
37    Fill {
38        holes_up_to: f64,
39        #[serde(default)]
40        near: Option<[f64; 3]>,
41        #[serde(default)]
42        fairing: MeshFairing,
43    },
44    /// Project the region onto local quadrics, with a bounded movement.
45    Smooth {
46        region: MeshRegion,
47        bumps_up_to: f64,
48        move_at_most: f64,
49        #[serde(default)]
50        keep_edges_deg: Option<f64>,
51    },
52    /// Move faces by a normal distance; a marking gets a joining wall.
53    Offset { region: MeshRegion, distance: f64 },
54    /// The boolean `operation` of the closed mesh (first) and a bounded
55    /// solid in the mesh's frame, the solid meshed to `chord` first. An
56    /// open mesh, and an operand it does not meet, are refused.
57    Boolean {
58        operand: MeshOperand,
59        operation: BooleanOp,
60        chord: f64,
61    },
62    /// A closed mesh hollowed: its surface kept, an inner wall `thickness`
63    /// inside it bounding a sealed cavity. An open sheet thickened to the
64    /// `side` given, rim walls closing it.
65    Shell {
66        thickness: f64,
67        #[serde(default)]
68        side: MeshSide,
69    },
70    /// Decimate and measure both ways at the 99.9th percentile. The
71    /// triangle limit takes priority over the tolerance; the report says
72    /// how far the resulting surface lies from its input.
73    Reduce {
74        tolerance: f64,
75        triangle_limit: usize,
76    },
77    /// Cut by the plane through `origin` square to `normal`, the side
78    /// `normal` points to kept when `keep_above`; with `close`, every loop
79    /// of the cut capped flat, gaps in its rim up to `bridge` closed.
80    Cut {
81        origin: [f64; 3],
82        normal: [f64; 3],
83        keep_above: bool,
84        close: bool,
85        bridge: f64,
86    },
87}
88
89/// A bounded solid a mesh boolean takes, as it stood when the step was
90/// made.
91#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
92pub enum MeshOperand {
93    /// A solid in the kernel's native text format, moved by the row-major
94    /// rigid `transform` into the frame it is used in.
95    Shape {
96        brep: String,
97        transform: [[f64; 4]; 4],
98    },
99    /// A primitive at its placement.
100    Primitive {
101        kind: PrimitiveKind,
102        placement: Placement,
103    },
104}
105
106/// Which side of an open sheet a shell thickens: inward is against the
107/// way its triangles face.
108#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
109pub enum MeshSide {
110    Inward,
111    Outward,
112    #[default]
113    Both,
114}
115
116/// A solid trimmed where a scan breaks off: the scan's triangles lying
117/// deeper than `depth` inside the solid are the break, which is extended
118/// past the solid, thickened by `gap` to each side and cut away. Both in
119/// the solid's frame.
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct TrimRequest {
122    /// The solid, in the kernel's native format.
123    pub solid: Vec<u8>,
124    pub scan: TriMesh,
125    pub depth: f64,
126    pub gap: f64,
127    /// Keep what lies on the side of the break away from the scan (the
128    /// piece); otherwise the side toward it.
129    pub away: bool,
130}
131
132/// A trimmed solid and how it meets the scan.
133#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
134pub struct TrimAnswer {
135    /// The trimmed solid, as a `.brep` file holds it.
136    pub brep: Vec<u8>,
137    /// Its volume, mm³.
138    pub volume: f64,
139    /// The area of its faces that lie along the break, mm².
140    pub mating_area: f64,
141    /// The distance from the break's points to the mating faces: median
142    /// and the range holding the middle 90 %, mm.
143    pub gap_median: f64,
144    pub gap_range: [f64; 2],
145}
146
147/// How a hole's added vertices settle.
148#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
149pub enum MeshFairing {
150    #[default]
151    Curved,
152    Flat,
153}
154
155/// Measurements made by a mesh step, in millimetres.
156#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
157pub enum MeshStepReport {
158    Cut {
159        capped_loops: usize,
160        bridged: usize,
161        open_loops: usize,
162    },
163    /// `volume` in mm³; `cut_triangles` the result's triangles the other
164    /// operand cut.
165    Boolean {
166        cut_triangles: usize,
167        volume: f64,
168    },
169    /// `hollow` for a closed part, otherwise a sheet thickened.
170    Shell {
171        hollow: bool,
172        volume: f64,
173    },
174    Smooth {
175        max: f64,
176        rms: f64,
177    },
178    Reduce {
179        within_share: f64,
180        p999: f64,
181        furthest: [f64; 3],
182        max: f64,
183        rms: f64,
184        target_met: bool,
185    },
186}
187
188/// What one step of a mesh history did.
189#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
190pub struct MeshStep {
191    /// Faces after it.
192    pub faces: usize,
193    pub note: String,
194    #[serde(default)]
195    pub report: Option<MeshStepReport>,
196}
197
198/// A mesh history built: the mesh at its end, what each step did, and the
199/// step that failed, when one did (the mesh is then the one before it).
200#[derive(Debug, Clone, Default)]
201pub struct MeshChainAnswer {
202    pub mesh: TriMesh,
203    /// The start vertex behind each displayed vertex, where the kernel
204    /// preserves it. Added surface has no source vertex.
205    pub sources: Vec<Option<u32>>,
206    pub steps: Vec<MeshStep>,
207    pub failed: Option<(usize, String)>,
208}