brep_kernel/feature_pipeline/component.rs
1//! The scene COMPONENT concept — assemblies build-spec §3 / §10 item 2.
2//!
3//! A component is a rigid group of solids inserted by an ACOMP feature (one per
4//! placed instance). Its record carries the owning feature id, the library part
5//! name, the rigid instance pose, the fixed (grounded) flag, and an opaque source
6//! metadata slot; its member solids are ordinary scene-resident solids whose
7//! entity names are NAMESPACED with the owning feature id.
8//!
9//! # Namespacing — the prefix wraps, nothing else moves
10//!
11//! Every entity name of a member solid (the solid name, every `face.name`, every
12//! `edge.name` — the only named entities in this kernel; vertices carry no names)
13//! is prefixed with `{component_id}:` AT THE COMPONENT BOUNDARY, i.e. when the
14//! sub-part's solids enter the assembly scene:
15//!
16//! ```text
17//! Extrude1_top -> ACOMP2:Extrude1_top
18//! Extrude1|Extrude1_top[0]-> ACOMP2:Extrude1|Extrude1_top[0]
19//! ```
20//!
21//! INSIDE the namespace the deterministic-naming rules apply UNCHANGED: the
22//! names arrive final from the sub-part's own `register_added` passes
23//! (`ensure_unique_face_names` + `stamp_derived_edge_names`) and are NEVER
24//! re-derived here. Re-running the derived-edge pass after prefixing would
25//! rewrite `ACOMP2:A|B[0]` from the prefixed face names (different sort, embedded
26//! prefixes) — do not "fix" this by routing members through `register_added`.
27//! Two instances of the same part therefore never collide (distinct feature ids),
28//! and a nested assembly's already-namespaced members chain naturally:
29//! `ACOMP1:Extrude1_top` wrapped by `ACOMP3` becomes `ACOMP3:ACOMP1:Extrude1_top`.
30//!
31//! # The feature fence (build-spec §3)
32//!
33//! Solids from ordinary modeling features never belong to a component and behave
34//! exactly as before. Component geometry is valid input for constraints and
35//! sketch attach/project ONLY; modeling features must cheaply REJECT it —
36//! [`SceneMap::is_component_owned`] / [`reject_component_references`] are that
37//! predicate (Wave 2 wires the checks into feature resolution).
38
39use std::collections::BTreeMap;
40
41use crate::feature_pipeline::features::common::{collect_edge_names, collect_face_names};
42use crate::feature_pipeline::{AddedSolid, FeatureDescriptor, PortRecord, SceneMap};
43use crate::{transform_brep, AffineTransform, BrepSolid};
44
45// ===========================================================================
46// The component record
47// ===========================================================================
48
49/// One scene component: an ACOMP instance's identity, pose, and member solids.
50/// Rides the [`crate::feature_pipeline::FeatureResult`] `components` side-channel
51/// into [`SceneMap::apply`], exactly like profiles/frames — the scene's component
52/// set is rebuilt from feature results every history run (a projected view,
53/// never an owning data structure).
54#[derive(Debug, Clone)]
55pub struct ComponentRecord {
56 /// The owning ACOMP feature id — also the namespace prefix segment.
57 pub id: String,
58 /// The display / parts-library part name (`M4-bolt` in `M4-bolt (ACOMP3)`).
59 pub part_name: String,
60 /// The rigid instance pose (part-local snapshot space -> assembly space),
61 /// already baked into the member geometry. Kept so a pose UPDATE can apply
62 /// the delta `new · old⁻¹` and so the solver can read the current pose.
63 pub transform: AffineTransform,
64 /// Grounded flag (the feature's `isFixed`; the solver never moves it).
65 pub fixed: bool,
66 /// Opaque source metadata slot (`sourceKey` / `sourceSignature` / …) for the
67 /// parts-library + update-components lanes; the scene never interprets it.
68 pub source: serde_json::Value,
69 /// Member solid scene names, already namespaced (`{id}:{part solid name}`).
70 pub solids: Vec<String>,
71 /// The part's harness PORT ids, namespaced (`{id}:{part port id}`) — the
72 /// wire-harness entities a placed part carries. Their records live in
73 /// [`SceneMap::ports`]; the re-pose lane moves them with the members.
74 pub ports: Vec<String>,
75}
76
77/// The scene's component set, keyed by owning feature id. `BTreeMap` so
78/// iteration (structure tree, BOM) is deterministic.
79pub type ComponentMap = BTreeMap<String, ComponentRecord>;
80
81// ===========================================================================
82// Namespacing
83// ===========================================================================
84
85/// Wrap one entity name with a component prefix: `{component_id}:{name}`.
86pub fn namespaced(component_id: &str, name: &str) -> String {
87 format!("{component_id}:{name}")
88}
89
90/// Prefix every NAMED face/edge of a member solid with the component id (the
91/// solid's own scene name is prefixed by the caller — solids carry their name in
92/// [`AddedSolid`], not on the BREP). Unnamed entities stay unnamed (they are not
93/// scene-resolvable either way).
94fn namespace_solid_names(solid: &mut BrepSolid, component_id: &str) {
95 for shell in &mut solid.shells {
96 for face in &mut shell.faces {
97 if let Some(name) = &face.name {
98 face.name = Some(namespaced(component_id, name));
99 }
100 }
101 }
102 for edge in &mut solid.edges {
103 if let Some(name) = &edge.name {
104 edge.name = Some(namespaced(component_id, name));
105 }
106 }
107}
108
109/// A component id must be a plain feature id: non-empty, no `:` (the namespace
110/// delimiter) and no `|` (the topology-vs-authored name discriminator).
111fn validate_component_id(id: &str) -> Result<(), String> {
112 if id.is_empty() {
113 return Err("component id must not be empty".into());
114 }
115 if id.contains(':') || id.contains('|') {
116 return Err(format!("component id '{id}' must not contain ':' or '|'"));
117 }
118 Ok(())
119}
120
121// ===========================================================================
122// Rigid-transform helpers (row-major 4x4, the AffineTransform layout)
123// ===========================================================================
124
125/// Require a RIGID map (orthonormal rotation rows, determinant +1). Components
126/// move as rigid bodies — a scaling/shearing/reflecting instance pose is a
127/// modeling error, rejected loudly (no fallback).
128fn require_rigid(transform: &AffineTransform) -> Result<(), String> {
129 let m = &transform.elements;
130 let rows = [[m[0], m[1], m[2]], [m[4], m[5], m[6]], [m[8], m[9], m[10]]];
131 for i in 0..3 {
132 for j in i..3 {
133 let dot: f64 = (0..3).map(|k| rows[i][k] * rows[j][k]).sum();
134 let expected = if i == j { 1.0 } else { 0.0 };
135 if (dot - expected).abs() > 1e-8 {
136 return Err("component transform must be rigid (rotation + translation)".into());
137 }
138 }
139 }
140 if (transform.determinant3() - 1.0).abs() > 1e-8 {
141 return Err("component transform must be rigid (no reflection/scale)".into());
142 }
143 Ok(())
144}
145
146/// Row-major product `a · b` (apply `b` first, then `a`).
147fn compose(a: &AffineTransform, b: &AffineTransform) -> Result<AffineTransform, String> {
148 let (ma, mb) = (&a.elements, &b.elements);
149 let mut out = [0.0f64; 16];
150 for row in 0..4 {
151 for col in 0..4 {
152 out[row * 4 + col] = (0..4)
153 .map(|k| ma[row * 4 + k] * mb[k * 4 + col])
154 .sum();
155 }
156 }
157 AffineTransform::new(out)
158}
159
160// ===========================================================================
161// Create / re-pose
162// ===========================================================================
163
164/// Build a component from a set of PART-LOCAL solids: rigid-pose each member
165/// with `transform`, namespace its entity names with `id`, and register it as a
166/// scene-resident solid. Returns the component record plus one [`AddedSolid`]
167/// per member — the owning ACOMP feature pushes both into its `FeatureResult`
168/// (`added` + `components`), and the record's handles are then owned by that
169/// feature's history-cache entry like any other feature output.
170///
171/// `members` are `(solid name, part-local BREP)` pairs exactly as the sub-part's
172/// history produced them (deterministic names final; see the module doc — the
173/// prefix wraps them, nothing is re-derived). Two-phase: every member is posed
174/// BEFORE anything registers, so a failure never leaks a half-built component.
175// Contract surface for the Wave-2 ACOMP feature; exercised by this module's tests.
176#[allow(dead_code)]
177pub fn create_component(
178 id: &str,
179 part_name: &str,
180 fixed: bool,
181 source: serde_json::Value,
182 transform: AffineTransform,
183 members: Vec<(String, BrepSolid)>,
184) -> Result<(ComponentRecord, Vec<AddedSolid>), String> {
185 validate_component_id(id)?;
186 require_rigid(&transform)?;
187
188 // Phase 1 (fallible): pose + namespace every member.
189 let mut posed: Vec<(String, BrepSolid)> = Vec::with_capacity(members.len());
190 for (member_name, solid) in &members {
191 let mut body = transform_brep(solid, transform, false)
192 .map_err(|error| format!("component '{id}': member '{member_name}': {error}"))?;
193 namespace_solid_names(&mut body, id);
194 posed.push((namespaced(id, member_name), body));
195 }
196
197 // Phase 2 (infallible): register.
198 let mut added = Vec::with_capacity(posed.len());
199 let mut solids = Vec::with_capacity(posed.len());
200 for (name, body) in posed {
201 let face_names = collect_face_names(&body);
202 let edge_names = collect_edge_names(&body);
203 let handle = crate::register_solid_value(body);
204 solids.push(name.clone());
205 added.push(AddedSolid {
206 handle,
207 name,
208 face_names,
209 edge_names,
210 ..AddedSolid::default()
211 });
212 }
213
214 Ok((
215 ComponentRecord {
216 id: id.to_string(),
217 part_name: part_name.to_string(),
218 transform,
219 fixed,
220 source,
221 solids,
222 ports: Vec::new(),
223 },
224 added,
225 ))
226}
227
228/// Pose a part-local port record by a rigid transform: the base point maps as
229/// a point, the direction by the rotation part alone.
230pub fn pose_port(record: &PortRecord, transform: &AffineTransform) -> PortRecord {
231 let point = transform.point(record.point);
232 let tip = transform.point(record.point.add(record.direction));
233 let direction = tip.sub(point).normalized().unwrap_or(record.direction);
234 PortRecord {
235 point,
236 direction,
237 ..record.clone()
238 }
239}
240
241/// Attach a part's ports to a placed component: each port's id AND label are
242/// wrapped `{component}:{…}` (two instances of one part must not read alike in
243/// the harness panel) and its record is posed by the component transform. The
244/// record lists the ids so [`update_component_transform`] moves them with the
245/// members; the posed records are returned for the feature to publish.
246pub fn attach_component_ports(
247 record: &mut ComponentRecord,
248 ports: &BTreeMap<String, PortRecord>,
249) -> Vec<(String, PortRecord)> {
250 let mut posed = Vec::with_capacity(ports.len());
251 for (local_id, port) in ports {
252 let id = namespaced(&record.id, local_id);
253 let mut port = pose_port(port, &record.transform);
254 port.label = namespaced(&record.id, &port.label);
255 record.ports.push(id.clone());
256 posed.push((id, port));
257 }
258 posed
259}
260
261/// Re-pose a component to a NEW absolute rigid transform: applies the delta
262/// `new · old⁻¹` to every member solid IN PLACE (same handles — a rigid map
263/// keeps every topology id and name, so the scene's face/edge refs stay valid)
264/// and updates the record.
265///
266/// This is the INTERACTIVE lane (move-gizmo drag, solver preview). The
267/// AUTHORITATIVE pose lives on the owning ACOMP feature's `inputParams`
268/// transform: a commit must write it back there (the solver pose write-back),
269/// which dirties the feature's fingerprint so the next history run re-executes
270/// it from the snapshot at the committed pose. Without the write-back, a clean
271/// cache replay would serve the mutated geometry against a stale descriptor.
272///
273/// Two-phase: every member transform is computed before anything commits, so a
274/// failure never leaves the component half-posed.
275// Contract surface for the Wave-2/3 move + solver lanes; exercised by tests.
276#[allow(dead_code)]
277pub fn update_component_transform(
278 scene: &mut SceneMap,
279 id: &str,
280 new_transform: AffineTransform,
281) -> Result<(), String> {
282 require_rigid(&new_transform)?;
283 let record = scene
284 .components
285 .get(id)
286 .ok_or_else(|| format!("unknown component '{id}'"))?;
287 let delta = compose(&new_transform, &record.transform.rigid_inverse()?)?;
288 let port_ids = record.ports.clone();
289
290 // Phase 1 (fallible): resolve + re-pose every member.
291 let mut posed: Vec<(u32, BrepSolid)> = Vec::with_capacity(record.solids.len());
292 for name in &record.solids {
293 let handle = scene
294 .solids
295 .get(name)
296 .copied()
297 .ok_or_else(|| format!("component '{id}': member '{name}' is not scene-resident"))?;
298 let body = crate::with_registered_solid_str(handle, |solid| {
299 transform_brep(solid, delta, false)
300 })
301 .map_err(|error| format!("component '{id}': member '{name}': {error}"))?;
302 posed.push((handle, body));
303 }
304
305 // Phase 2: commit under the existing handles, then update the record.
306 for (handle, body) in posed {
307 crate::replace_registered_solid(handle, body)?;
308 }
309 scene
310 .components
311 .get_mut(id)
312 .expect("record fetched above")
313 .transform = new_transform;
314
315 // The component's ports (and their published entities) follow the members,
316 // so a solve that moves a part moves the harness network it carries.
317 for port_id in port_ids {
318 let Some(port) = scene.ports.get(&port_id) else {
319 continue;
320 };
321 let posed = pose_port(port, &delta);
322 crate::feature_pipeline::features::port::place_scene_port(scene, &port_id, posed)?;
323 }
324 Ok(())
325}
326
327/// The feature fence (build-spec §3): fail with a clear message when any of
328/// `names` resolves to component-owned geometry. Modeling features that consume
329/// solids/faces/edges call this over their reference names before operating —
330/// cross-part feature linking is out of scope and must be rejected cleanly,
331/// never silently applied.
332pub fn reject_component_references<'a>(
333 scene: &SceneMap,
334 names: impl IntoIterator<Item = &'a str>,
335) -> Result<(), String> {
336 for name in names {
337 if let Some(record) = scene.owning_component(name) {
338 return Err(format!(
339 "'{name}' belongs to assembly component '{}' — modeling features cannot consume component geometry",
340 record.id
341 ));
342 }
343 }
344 Ok(())
345}
346
347/// The fence at the DISPATCH altitude — the one place every feature passes
348/// through (`execute_feature`), so no per-feature checks are scattered. A
349/// reference here is what the pipeline already defines it to be (the
350/// `scan_consumed` cache-dependency walk): any descriptor string that
351/// exact-matches a scene name. Any such string owned by a component fails the
352/// feature before it executes.
353///
354/// Exempt (the two ALLOWED in-context uses, build-spec §3 — attach + project):
355/// SKETCH (plane attach + edge projection), DATUM/PLANE (a datum derived from
356/// component geometry), ACOMP itself (the component's own feature, wired by
357/// the parts-library lane), and the two wire-harness construction features —
358/// PORT (its `directionRef` is a connector face on a placed component: the
359/// harness workbench's whole point) and SPLINE (its anchors attach to ports).
360/// Assembly constraints are not history features, so they never reach this
361/// fence. The exemption is type-level, which is safe because none of the
362/// exempt types carries a `boolean` param or operand-solid references —
363/// re-examine if such a param is ever added to one of them.
364pub fn enforce_reference_fence(
365 feature_type: &str,
366 descriptor: &FeatureDescriptor,
367 scene: &SceneMap,
368) -> Result<(), String> {
369 // Componentless scenes skip the walk entirely — the byte-identical
370 // guarantee for every existing modeling document.
371 if scene.components.is_empty() {
372 return Ok(());
373 }
374 // Verbatim dispatch alias strings (the dispatch matches exact spellings;
375 // "ASSEMBLY COMPONENT" is ACOMP's long dispatch alias in assembly_component.rs).
376 if matches!(
377 feature_type,
378 "S" | "SKETCH"
379 | "D"
380 | "DATUM"
381 | "DATIUM"
382 | "P"
383 | "PLANE"
384 | "ACOMP"
385 | "ASSEMBLY COMPONENT"
386 | "PORT"
387 | "SP"
388 | "SPLINE"
389 ) {
390 return Ok(());
391 }
392 if let Some(name) = first_component_owned(&descriptor.input_params, scene)
393 .or_else(|| first_component_owned(&descriptor.persistent_data, scene))
394 {
395 return reject_component_references(scene, [name]);
396 }
397 Ok(())
398}
399
400/// The first string anywhere in `value` (recursively, both param sources — the
401/// `scan_consumed` walk shape) that names component-owned geometry.
402fn first_component_owned<'a>(
403 value: &'a serde_json::Value,
404 scene: &SceneMap,
405) -> Option<&'a str> {
406 match value {
407 serde_json::Value::String(text) => {
408 let trimmed = text.trim();
409 (!trimmed.is_empty() && scene.is_component_owned(trimmed)).then_some(trimmed)
410 }
411 serde_json::Value::Array(items) => {
412 items.iter().find_map(|item| first_component_owned(item, scene))
413 }
414 serde_json::Value::Object(map) => {
415 map.values().find_map(|item| first_component_owned(item, scene))
416 }
417 _ => None,
418 }
419}
420
421// ===========================================================================
422// SceneMap component surface
423// ===========================================================================
424
425impl SceneMap {
426 /// Resolve a component record by its owning feature id (exact match).
427 /// Contract surface for the Wave-2 constraint/selection lanes.
428 #[allow(dead_code)]
429 pub fn resolve_component(&self, id: &str) -> Option<&ComponentRecord> {
430 self.components.get(id)
431 }
432
433 /// The component owning an entity name, if any: the BARE component id itself
434 /// (a COMPONENT-type selection), or the name's OUTERMOST namespace prefix
435 /// (`ACOMP3:ACOMP1:X` is owned by `ACOMP3` at this scene level; `ACOMP1` is
436 /// the nested assembly's internal structure). Membership is decided against
437 /// the registered component set, never syntactically — authored names like
438 /// `S1:PROFILE` have a first segment that is a sketch id, not a component id.
439 pub fn owning_component(&self, name: &str) -> Option<&ComponentRecord> {
440 if let Some(record) = self.components.get(name) {
441 return Some(record);
442 }
443 let (prefix, _) = name.split_once(':')?;
444 self.components.get(prefix)
445 }
446
447 /// The feature-fence predicate: does this entity name belong to a component?
448 /// Cheap (one map probe + one prefix probe); the dispatch fence
449 /// ([`enforce_reference_fence`]) uses it to REJECT component geometry as
450 /// modeling-feature input (build-spec §3).
451 pub fn is_component_owned(&self, name: &str) -> bool {
452 self.owning_component(name).is_some()
453 }
454
455 /// Iterate the scene's components in deterministic (id) order — the
456 /// structure-tree / BOM projection surface.
457 #[allow(dead_code)]
458 pub fn iter_components(&self) -> impl Iterator<Item = &ComponentRecord> {
459 self.components.values()
460 }
461
462 /// A component's member solids as `(scene name, resident handle)`, in the
463 /// record's member order. A member missing from the solid map (never the
464 /// case for an intact scene) is skipped.
465 #[allow(dead_code)]
466 pub fn component_solids(&self, id: &str) -> Vec<(String, u32)> {
467 let Some(record) = self.components.get(id) else {
468 return Vec::new();
469 };
470 record
471 .solids
472 .iter()
473 .filter_map(|name| self.solids.get(name).map(|&handle| (name.clone(), handle)))
474 .collect()
475 }
476
477 /// A component's grounded flag (`None` for an unknown id).
478 #[allow(dead_code)]
479 pub fn component_fixed(&self, id: &str) -> Option<bool> {
480 self.components.get(id).map(|record| record.fixed)
481 }
482
483 /// Set a component's grounded flag. Scene-view state only — the
484 /// authoritative flag is the owning feature's `isFixed` (the Fixed
485 /// constraint / tree action writes THERE; this keeps the live scene in
486 /// sync between runs). Returns false for an unknown id.
487 #[allow(dead_code)]
488 pub fn set_component_fixed(&mut self, id: &str, fixed: bool) -> bool {
489 match self.components.get_mut(id) {
490 Some(record) => {
491 record.fixed = fixed;
492 true
493 }
494 None => false,
495 }
496 }
497}
498
499// BREP private tests: 31356d3bcc31b5e3