ifc_geometry/lower/
layers.rs1use axiolid_core::{BooleanOperator, Plane3, Point3, Vec3};
40use axiolid_model::{GeometryNode, NodeId, SolidOperation};
41use axiolid_primitive::HalfSpace;
42use ifc_model::EntityId;
43
44use crate::error::GeometryResult;
45use crate::lower::net::{
46 degenerate, lower_body_in_host_frame, place, solid_parts, NetLowering, NetOptions,
47};
48use crate::lower::LoweringSession;
49
50const UNIT_TOLERANCE: f64 = 1e-9;
52
53#[derive(Debug, Clone, PartialEq)]
55#[non_exhaustive]
56pub struct LayerCut {
57 pub normal: [f64; 3],
59 pub boundaries: Vec<f64>,
62}
63
64impl LayerCut {
65 #[must_use]
67 pub fn new(normal: [f64; 3], boundaries: Vec<f64>) -> Self {
68 Self { normal, boundaries }
69 }
70}
71
72impl From<&crate::LayerBoundaries> for LayerCut {
73 fn from(bounds: &crate::LayerBoundaries) -> Self {
74 Self::new(bounds.normal, bounds.boundaries.clone())
75 }
76}
77
78#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
80#[non_exhaustive]
81pub enum LayerBody {
82 #[default]
84 Gross,
85 Net(NetOptions),
87}
88
89#[derive(Debug, Clone, PartialEq, Eq)]
91#[non_exhaustive]
92pub struct LayeredLowering {
93 pub body: NodeId,
96 pub net: Option<NetLowering>,
98 pub parts: Vec<Option<NodeId>>,
100}
101
102pub fn lower_product_layers(
117 session: &mut LoweringSession<'_>,
118 product: EntityId,
119 cut: &LayerCut,
120 body: LayerBody,
121) -> GeometryResult<Option<LayeredLowering>> {
122 let thick = validate(session, product, cut)?;
123 let net = match body {
124 LayerBody::Gross => None,
125 LayerBody::Net(options) => Some(options),
126 };
127 let Some(local) = lower_body_in_host_frame(session, product, net)? else {
128 return Ok(None);
129 };
130 let heads = solid_parts(session, local.local, product)?;
131
132 let plane = |boundary: f64| Plane3 {
133 origin: Point3::from_array(cut.normal.map(|n| n * boundary)),
134 normal: Vec3::from_array(cut.normal),
135 };
136 let mut tools: Vec<[Option<NodeId>; 2]> = vec![[None; 2]; cut.boundaries.len()];
138 let mut tool = |session: &mut LoweringSession<'_>, boundary: usize, above: bool| {
139 let slot = &mut tools[boundary][usize::from(above)];
140 if let Some(node) = *slot {
141 return Ok(node);
142 }
143 let node = session.node_for(
144 product,
145 GeometryNode::HalfSpace(HalfSpace {
146 boundary: plane(cut.boundaries[boundary]),
147 agreement: above,
148 }),
149 )?;
150 *slot = Some(node);
151 GeometryResult::Ok(node)
152 };
153
154 let mut parts = vec![None; cut.boundaries.len() - 1];
155 for (position, &layer) in thick.iter().enumerate() {
156 let mut cut_heads = heads.clone();
157 let mut sides = Vec::with_capacity(2);
161 if position > 0 {
162 sides.push((layer, false));
163 }
164 if position + 1 < thick.len() {
165 sides.push((layer + 1, true));
166 }
167 for (boundary, above) in sides {
168 let node = tool(session, boundary, above)?;
169 for head in &mut cut_heads {
170 *head = session.node_for(
171 product,
172 GeometryNode::SolidOperation(SolidOperation::Boolean {
173 left: *head,
174 right: node,
175 operator: BooleanOperator::Difference,
176 }),
177 )?;
178 }
179 }
180 let layer_node = match cut_heads.as_slice() {
181 [single] => *single,
182 _ => session.node_for(product, GeometryNode::Collection(cut_heads))?,
183 };
184 parts[layer] = Some(place(session, layer_node, local.to_world, product)?);
185 }
186
187 let body_node = local.net.as_ref().map_or(local.gross, |net| net.root);
188 Ok(Some(LayeredLowering {
189 body: body_node,
190 net: local.net,
191 parts,
192 }))
193}
194
195fn validate(
197 session: &LoweringSession<'_>,
198 product: EntityId,
199 cut: &LayerCut,
200) -> GeometryResult<Vec<usize>> {
201 let invalid = |detail: &str| degenerate(session, product, detail);
202 let length = cut.normal.iter().map(|n| n * n).sum::<f64>().sqrt();
203 if !cut.normal.iter().all(|n| n.is_finite()) || (length - 1.0).abs() > UNIT_TOLERANCE {
204 return Err(invalid(
205 "the layer cut's normal is not a finite unit vector",
206 ));
207 }
208 if cut.boundaries.len() < 2 {
209 return Err(invalid("a layer cut needs at least two boundaries"));
210 }
211 if !cut.boundaries.iter().all(|b| b.is_finite()) {
212 return Err(invalid("a layer cut boundary is not finite"));
213 }
214 if cut.boundaries.windows(2).any(|pair| pair[1] < pair[0]) {
215 return Err(invalid("the layer cut's boundaries are not ascending"));
216 }
217 let thick: Vec<usize> = cut
218 .boundaries
219 .windows(2)
220 .enumerate()
221 .filter(|(_, pair)| pair[1] > pair[0])
222 .map(|(index, _)| index)
223 .collect();
224 if thick.is_empty() {
225 return Err(invalid("no layer of the cut has thickness"));
226 }
227 Ok(thick)
228}