Skip to main content

kernel_api/
chain.rs

1//! What a chain build answers: the solid and its mesh, the preview, and the
2//! error naming the failing op.
3
4use serde::{Deserialize, Serialize};
5use thiserror::Error;
6
7use crate::*;
8
9/// Error from executing a body's solid-op chain, attributed to the failing
10/// step so the caller can mark the matching feature.
11#[derive(Debug, Clone, Default, Error)]
12#[error("op {op_index}: {message}")]
13pub struct ChainError {
14    pub op_index: usize,
15    pub message: String,
16    /// The edges a refused fillet or chamfer took, and those it could not
17    /// round or bevel.
18    pub edges: EdgeRefusal,
19}
20
21/// The edges of a fillet or chamfer the kernel refused.
22#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
23pub struct EdgeRefusal {
24    /// Every edge the op took, in its order, tangent chains followed: what
25    /// it rounds without the failing ones is these less those.
26    pub request: Vec<EdgeProbe>,
27    /// The edges that fail.
28    pub failing: Vec<FailingEdge>,
29    /// The largest share of the asked size at which every edge of the
30    /// request builds, when it does at some: the size times it fits.
31    #[serde(default)]
32    pub fits: Option<f64>,
33}
34
35/// An edge a fillet or chamfer could not round or bevel: refused alone, or
36/// in a group of the op's edges meeting at a vertex.
37#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
38pub struct FailingEdge {
39    /// Its place among the edges the op rounds, from zero.
40    pub index: usize,
41    /// How many edges the op rounds.
42    pub of: usize,
43    /// A pick that names it in the body's own frame: a point halfway
44    /// along, the way it runs there and its faces' names.
45    pub probe: EdgeProbe,
46    /// Points along it in the body's own frame, to draw it by.
47    pub points: Vec<[f64; 3]>,
48    /// Why the kernel refused it.
49    pub reason: String,
50    /// The largest share of the asked size at which it builds on its own,
51    /// when it does at some.
52    #[serde(default)]
53    pub fits: Option<f64>,
54}
55
56/// Result of executing a body's solid-op chain.
57#[derive(Debug, Clone, Default, Serialize, Deserialize)]
58pub struct SolidBuildResult {
59    /// Native-format shape snapshot of the final solid (for later
60    /// re-tessellation, persistence, and downstream booleans).
61    pub brep_blob: Vec<u8>,
62    /// Render mesh of the final solid.
63    pub mesh: TriMesh,
64    /// Axis-aligned bounds in millimetres.
65    pub bounds_mm: Option<([f32; 3], [f32; 3])>,
66    /// What the feature being edited does, when the build was asked for it.
67    pub preview: Option<Box<FeaturePreview>>,
68    /// The answers to the chain's probes, in the order they were asked.
69    pub probes: Vec<Result<ProbeAnswer, String>>,
70    /// The curves the chain's projections came to, in the order they were
71    /// asked.
72    #[serde(default)]
73    pub projections: Vec<Result<Vec<ProjectedEdge>, String>>,
74    /// What an op built but should be told: a reference whose name was
75    /// lost, found again by where it was picked. In op order.
76    #[serde(default)]
77    pub warnings: Vec<OpWarning>,
78    /// The same for the probes, by their order in `probes`: a face or edge
79    /// found by where it was picked after its name was lost.
80    #[serde(default)]
81    pub probe_warnings: Vec<OpWarning>,
82    /// The same for the projections, by their order in `projections`.
83    #[serde(default)]
84    pub projection_warnings: Vec<OpWarning>,
85    /// The largest size the previewed fillet or chamfer builds at, when the
86    /// build previewed one: what a drag of its size stops at.
87    #[serde(default)]
88    pub size_limit: Option<SizeLimit>,
89}
90
91/// The largest size a fillet or chamfer builds at on the solid before it.
92#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
93pub struct SizeLimit {
94    /// The op it is the limit of.
95    pub op_index: usize,
96    /// [`SolidOp::size_limit_key`] of the op it was worked out for.
97    pub key: u64,
98    /// Its first size at the limit, mm: a fillet's radius, a chamfer's
99    /// distance (with two distances, the first at their common scale).
100    /// `None` where no size short of the solid's own fails.
101    pub largest: Option<f64>,
102    /// A round or chamfer set back by distances, or an automatic one: where
103    /// its first edge is read and each face's own limit, the least over its
104    /// edges. `None` for any other op.
105    #[serde(default)]
106    pub sides: Option<RoundSides>,
107}
108
109/// The two faces beside the first edge of a round of distances, as a drag
110/// reads them: the face its first distance is on, then the other.
111#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
112pub struct RoundSides {
113    /// A point on the edge, in the body's frame.
114    pub at: [f64; 3],
115    pub sides: [RoundSide; 2],
116}
117
118/// One face beside a rounded edge.
119#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
120pub struct RoundSide {
121    /// The way the face runs from the edge at `RoundSides::at`, square to
122    /// the edge: where a round's contact on it moves as it grows.
123    pub direction: [f64; 3],
124    /// The farthest the contact goes before running off the face, mm;
125    /// `None` where it does not run off short of the solid's size.
126    pub largest: Option<f64>,
127}
128
129/// A warning on one op of a chain that built (or, in
130/// [`SolidBuildResult::probe_warnings`] and `projection_warnings`, on one
131/// probe or projection, `op_index` then being its place in their list).
132#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
133pub struct OpWarning {
134    pub op_index: usize,
135    pub message: String,
136}
137
138/// What a feature being edited does to its body, beside the body without
139/// it: the view while it is edited.
140#[derive(Debug, Clone, Default, Serialize, Deserialize)]
141pub struct FeaturePreview {
142    /// The body to show meanwhile: as it stood before a feature that adds,
143    /// after one that cuts. None when an adding feature is the body's
144    /// first.
145    pub shown: Option<Box<SolidBuildResult>>,
146    /// The feature's own tool solid, meshed: the material it adds or takes.
147    pub tool: TriMesh,
148    /// The feature cuts.
149    pub cuts: bool,
150}