Skip to main content

brep_render/
metadata.rs

1//! The Properties-panel DATA layer (UI comes later): a name-keyed metadata
2//! store, per-entity measurements, and object provenance — all engine-native,
3//! all crossing the R3 boundary as plain JSON/scalars.
4//!
5//! # Metadata store ([`MetadataStore`])
6//!
7//! `object name → { attribute → value }`, keyed by the KERNEL OBJECT NAME (a
8//! solid / face / edge name), **not** a feature id. This loose coupling is the
9//! whole point: a record survives feature edits, rollback and re-tessellation as
10//! long as the object's name persists (edge/face names ARE propagated through
11//! booleans, splits and welds by the kernel). Values are strings; the well-known
12//! `density` attribute (mass units per mm³) drives a solid's weight. The store is
13//! persisted WITH the model — [`crate::engine_state::EngineState::history_request_json`]
14//! folds it in as a top-level `metadata` field and
15//! [`crate::engine_state::EngineState::set_history_json`] lifts it back out, so it
16//! round-trips through save/open.
17//!
18//! # Measurements + provenance
19//!
20//! The [`EngineState`] methods below resolve an object NAME to its kind (solid /
21//! face / edge, via the scene) and return the right measurements from the
22//! kernel's exact integrators (volume, surface area, arc length), plus the
23//! feature that produced the object (provenance, via the history's per-feature
24//! output solids). Units are millimetres (the kernel length convention).
25
26use crate::engine_state::EngineState;
27use brep_kernel::COLOR_METADATA_KEY;
28use crate::runner::{MeasureKind, MeasureQuery};
29use serde_json::Value;
30use std::collections::BTreeMap;
31
32/// The default density (mass per unit volume) when an object carries no
33/// `density` metadata: unit density, so `weight == volume`.
34pub const DEFAULT_DENSITY: f64 = 1.0;
35
36/// The name-keyed metadata store: `object name → { attribute → value }`, string
37/// values, deterministic iteration (a `BTreeMap` so the persisted JSON is
38/// stable). Empty records are never retained (removing the last attribute drops
39/// the whole record).
40#[derive(Debug, Clone, Default)]
41pub struct MetadataStore {
42    entries: BTreeMap<String, BTreeMap<String, String>>,
43    /// Names the content: every mutation takes a fresh process-wide number, and
44    /// a clone (an undo snapshot) carries its content's number with it, so two
45    /// equal stamps always hold equal records.
46    stamp: u64,
47}
48
49static STAMPS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
50
51impl MetadataStore {
52    pub fn new() -> Self {
53        Self::default()
54    }
55
56    /// Whether the store holds no records at all.
57    pub fn is_empty(&self) -> bool {
58        self.entries.is_empty()
59    }
60
61    /// The content stamp: unmoved means unchanged (see the field).
62    pub fn stamp(&self) -> u64 {
63        self.stamp
64    }
65
66    fn touch(&mut self) {
67        self.stamp = STAMPS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
68    }
69
70    /// Set (or overwrite) one attribute of `name`'s record. An empty object name
71    /// or key is ignored (no phantom records).
72    pub fn set_attribute(&mut self, name: &str, key: &str, value: &str) {
73        if name.is_empty() || key.is_empty() {
74            return;
75        }
76        self.touch();
77        self.entries
78            .entry(name.to_string())
79            .or_default()
80            .insert(key.to_string(), value.to_string());
81    }
82
83    /// Remove one attribute of `name`'s record, dropping the record if it becomes
84    /// empty. Returns whether the attribute existed.
85    pub fn remove_attribute(&mut self, name: &str, key: &str) -> bool {
86        let Some(record) = self.entries.get_mut(name) else {
87            return false;
88        };
89        let existed = record.remove(key).is_some();
90        if record.is_empty() {
91            self.entries.remove(name);
92        }
93        if existed {
94            self.touch();
95        }
96        existed
97    }
98
99    /// One attribute's value (`None` if the object or key is unknown).
100    pub fn attribute(&self, name: &str, key: &str) -> Option<&str> {
101        self.entries.get(name)?.get(key).map(String::as_str)
102    }
103
104    /// One object's whole record (empty map if the object has no metadata).
105    pub fn record(&self, name: &str) -> BTreeMap<String, String> {
106        self.entries.get(name).cloned().unwrap_or_default()
107    }
108
109    /// The whole store (all records), read-only.
110    pub fn all(&self) -> &BTreeMap<String, BTreeMap<String, String>> {
111        &self.entries
112    }
113
114    /// The resolved density (mass per mm³) for `name`: its `density` attribute
115    /// parsed as a positive finite number, else [`DEFAULT_DENSITY`].
116    pub fn density(&self, name: &str) -> f64 {
117        self.attribute(name, "density")
118            .and_then(|value| value.trim().parse::<f64>().ok())
119            .filter(|density| density.is_finite() && *density > 0.0)
120            .unwrap_or(DEFAULT_DENSITY)
121    }
122
123    /// Drop every record (a part load replaces the store wholesale).
124    pub fn clear(&mut self) {
125        self.entries.clear();
126        self.touch();
127    }
128
129    /// One object's record as a JSON object `{ key: value, ... }` (`{}` when the
130    /// object has no metadata).
131    pub fn record_json(&self, name: &str) -> String {
132        Value::Object(
133            self.record(name)
134                .into_iter()
135                .map(|(key, value)| (key, Value::String(value)))
136                .collect(),
137        )
138        .to_string()
139    }
140
141    /// The whole store as a JSON value `{ name: { key: value, ... }, ... }` — the
142    /// persisted shape and the whole-store getter.
143    pub fn to_json(&self) -> Value {
144        Value::Object(
145            self.entries
146                .iter()
147                .map(|(name, record)| {
148                    let object = record
149                        .iter()
150                        .map(|(key, value)| (key.clone(), Value::String(value.clone())))
151                        .collect();
152                    (name.clone(), Value::Object(object))
153                })
154                .collect(),
155        )
156    }
157
158    /// The whole store as a JSON string (the whole-store getter's string form).
159    pub fn to_json_string(&self) -> String {
160        self.to_json().to_string()
161    }
162
163    /// Replace the whole store from a persisted `metadata` value (or `None`, which
164    /// clears it — loading a part with no metadata). Non-string leaf values are
165    /// coerced to their JSON text so a legacy document never fails the load.
166    pub fn load_json(&mut self, value: Option<&Value>) {
167        self.entries.clear();
168        self.touch();
169        let Some(Value::Object(objects)) = value else {
170            return;
171        };
172        for (name, record) in objects {
173            let Value::Object(attributes) = record else {
174                continue;
175            };
176            let map: BTreeMap<String, String> = attributes
177                .iter()
178                .map(|(key, value)| (key.clone(), value_to_string(value)))
179                .collect();
180            if !map.is_empty() {
181                self.entries.insert(name.clone(), map);
182            }
183        }
184    }
185}
186
187/// Coerce a persisted metadata leaf to a string value (strings verbatim, `null`
188/// to empty, everything else to its JSON text).
189fn value_to_string(value: &Value) -> String {
190    match value {
191        Value::String(text) => text.clone(),
192        Value::Null => String::new(),
193        other => other.to_string(),
194    }
195}
196
197/// The kind an object NAME resolves to in the current scene.
198#[derive(Debug, Clone, Copy, PartialEq, Eq)]
199enum ObjectKind {
200    Solid,
201    Face,
202    Edge,
203}
204
205// --- EngineState: metadata store API (a SEPARATE impl block, appended, so
206//     concurrent edits to the primary block don't conflict) -------------------
207impl EngineState {
208    /// One object's metadata record as JSON `{ key: value, ... }` (`{}` if none).
209    pub fn object_metadata_json(&self, name: &str) -> String {
210        self.metadata.record_json(name)
211    }
212
213    /// Set (or overwrite) one metadata attribute of an object by NAME. String
214    /// value; the well-known `density` key drives the object's weight and the
215    /// well-known `color` key drives its shaded colour.
216    pub fn set_metadata_attribute(&mut self, name: &str, key: &str, value: &str) {
217        self.metadata.set_attribute(name, key, value);
218        // A metadata edit (notably `density`) feeds the object-info output but does
219        // NOT trigger a history rerun, so drop this object's cached info so a
220        // re-selection re-measures with the new attribute.
221        self.info_cache.remove(name);
222        // `color` is the one attribute the VIEWPORT reads, and there is no rerun
223        // to carry it through — push it to the display right here so recolouring
224        // a body is immediate.
225        if key == COLOR_METADATA_KEY {
226            self.sync_colors_from_metadata();
227        }
228    }
229
230    /// Remove one metadata attribute of an object. Returns whether it existed.
231    pub fn remove_metadata_attribute(&mut self, name: &str, key: &str) -> bool {
232        let existed = self.metadata.remove_attribute(name, key);
233        // Same rerun-less invalidation as `set_metadata_attribute`.
234        self.info_cache.remove(name);
235        if existed && key == COLOR_METADATA_KEY {
236            self.sync_colors_from_metadata();
237        }
238        existed
239    }
240
241    /// Re-derive the display scene's colours from the metadata store — the ONE
242    /// call that connects the durable `color` attribute to the renderer.
243    ///
244    /// Every colour the viewport shows comes from here: a STEP import's stamped
245    /// body/face colours, a colour typed or picked in the Info window, a colour
246    /// restored from a saved document. The store is the authority; the display's
247    /// `color_override` fields are a derived cache of it, rebuilt after every
248    /// history apply (`EngineState::finish_apply`), on every document load, on
249    /// a metadata edit, and whenever the display setting flips.
250    ///
251    /// [`crate::style::RenderSettings::override_model_colors`] is honoured HERE
252    /// rather than in the renderer, and it is deliberately a READ of the store,
253    /// never a write: ticking the box resolves every colour to `None` so the
254    /// viewport falls back to `faceColorMode`, while the stored attributes stay
255    /// exactly as they were. Unticking restores them from the same records.
256    ///
257    /// Returns whether anything changed, and marks the engine dirty only then —
258    /// the no-op case must stay free, since this runs after every single run.
259    pub fn sync_colors_from_metadata(&mut self) -> bool {
260        // Disjoint field borrows: the closure reads `self.metadata` while
261        // `self.scene` is borrowed mutably.
262        let ignore = self.settings.override_model_colors;
263        let metadata = &self.metadata;
264        let changed = self.scene.apply_metadata_colors(|name| {
265            if ignore {
266                return None;
267            }
268            metadata
269                .attribute(name, COLOR_METADATA_KEY)
270                .and_then(crate::style::parse_css_hex)
271                // A derived BOARD body has a colour of its own and no producing
272                // feature to have coloured it, so without this fallback the
273                // pass would resolve it to `None` and the board would draw in
274                // the name-hashed default — green substrate and copper traces
275                // are the whole point of drawing it. A stored attribute still
276                // wins, so a user may recolour the board like anything else.
277                .or_else(|| crate::engine_state::board_default_color(name))
278        });
279        if changed {
280            self.dirty = true;
281        }
282        changed
283    }
284
285    /// The whole metadata store as JSON `{ name: { key: value } }`.
286    pub fn metadata_json(&self) -> String {
287        self.metadata.to_json_string()
288    }
289}
290
291// --- EngineState: measurements + provenance (SEPARATE impl block) ------------
292impl EngineState {
293    /// Resolve an object NAME to `(kind, owning solid name)` via the display
294    /// scene: a solid resolves to itself; a face/edge resolves to its owning
295    /// solid. `None` for an empty or unknown name (vertices carry no kernel name).
296    fn resolve_object(&self, name: &str) -> Option<(ObjectKind, String)> {
297        if name.is_empty() {
298            return None;
299        }
300        if self.scene.solid(name).is_some() {
301            return Some((ObjectKind::Solid, name.to_string()));
302        }
303        for solid in self.scene.solids() {
304            if solid.faces.iter().any(|face| face.name == name) {
305                return Some((ObjectKind::Face, solid.name.clone()));
306            }
307        }
308        for solid in self.scene.solids() {
309            if solid.edges.iter().any(|edge| edge.name == name) {
310                return Some((ObjectKind::Edge, solid.name.clone()));
311            }
312        }
313        None
314    }
315
316    /// The feature that an object ORIGINATES from, as `(feature id, feature type)`.
317    /// For a FACE/EDGE this is the feature that first gave it its name (its true
318    /// origin, from the eager `entity_origin` first-writer map) — NOT the owning
319    /// solid's last producer — so "Edit owning feature" rolls back to where the
320    /// entity was born. For a SOLID it's the solid's producer (last writer, the
321    /// eager `provenance` map). `None` if the name is unknown or has no known
322    /// producer. Reads only the eager maps the last run shipped, so it never re-runs
323    /// the history — the freeze side-door `context_bar` hit every selected frame is
324    /// now O(1).
325    pub fn creating_feature(&self, name: &str) -> Option<(String, String)> {
326        let Some((kind, owner)) = self.resolve_object(name) else {
327            // A datum / construction PLANE (or one plane of a datum) is not a
328            // resident solid/face/edge — it's a named frame. Its producer is the
329            // D/P feature that emitted the frame (same fallback object_info_json
330            // uses), so "Edit owning feature" resolves for a lone plane pick too.
331            return self.datum_feature_for_name(name);
332        };
333        let id = match kind {
334            // A face/edge resolves to its ORIGIN. Fall back to the owning solid's
335            // producer when the name is somehow absent from `entity_origin` (keeps
336            // the "Edit owning feature" button from vanishing).
337            ObjectKind::Face | ObjectKind::Edge => self
338                .entity_origin
339                .get(name)
340                .cloned()
341                .or_else(|| self.provenance.get(&owner).cloned())?,
342            // A solid keeps its existing last-writer producer semantics.
343            ObjectKind::Solid => self.provenance.get(&owner)?.clone(),
344        };
345        Some((id.clone(), self.feature_type_of(&id)))
346    }
347
348    /// Whether the object `name` (a solid, or a face/edge owned by a solid) sits
349    /// on a SHEET-METAL body — its owning display solid carries the sheet-metal
350    /// marker the pipeline stamped from the resident handle's `SheetTree`. Reads
351    /// only the display scene, so it is O(1) and thread-safe (no `SheetTree`
352    /// thread-local touched on the UI thread — [`crate::scene::SolidDisplay::
353    /// is_sheet_metal`]). The gate for the sheet-metal edit features (SM Flange /
354    /// Fillet / Chamfer). `false` for an unknown name or a synthesized sketch
355    /// sheet (no resident handle, no tree).
356    pub fn is_sheet_metal_object(&self, name: &str) -> bool {
357        self.resolve_object(name)
358            .and_then(|(_, owner)| self.scene.solid(&owner))
359            .is_some_and(|solid| solid.is_sheet_metal)
360    }
361
362    /// A feature's type token by id (empty string if the id is not in the history).
363    fn feature_type_of(&self, id: &str) -> String {
364        self.history
365            .index_of(id)
366            .and_then(|index| self.history.feature_type(index))
367            .unwrap_or_default()
368    }
369
370    /// The `{ id, type }` provenance JSON for an object by NAME (or `null` if it has
371    /// no known producer). Delegates to [`creating_feature`](Self::creating_feature)
372    /// so a face/edge reports its ORIGIN (first-writer) and a solid its producer
373    /// (last-writer) — one resolver, so the Info tab and the context bar agree.
374    fn creating_feature_value(&self, name: &str) -> Value {
375        match self.creating_feature(name) {
376            Some((id, ty)) => serde_json::json!({ "id": id, "type": ty }),
377            None => Value::Null,
378        }
379    }
380
381    /// The full Properties-panel info for an object by NAME: its resolved kind,
382    /// the right measurements, and provenance. Units are millimetres.
383    ///
384    /// - **Solid** — `{ ok, name, kind:"solid", volume, surfaceArea,
385    ///   edgeLengthTotal, density, weight, creatingFeature }`, where
386    ///   `weight = density · volume` and `density` comes from the object's
387    ///   metadata (default [`DEFAULT_DENSITY`]).
388    /// - **Face** — `{ ok, name, kind:"face", solid, surfaceType, area,
389    ///   edgeLengthTotal, creatingFeature }` (`edgeLengthTotal` = its boundary
390    ///   edges; `surfaceType` = the carrier-surface classification, e.g.
391    ///   `"Plane"`/`"Cylinder"`/`"Cone"`/`"Sphere"`/`"Torus"`/`"NURBS"`), plus
392    ///   the carrier's dimensions when it has them: `radius` + `diameter`
393    ///   (cylinder, sphere), `halfAngleDeg` (cone), `majorRadius` +
394    ///   `minorRadius` (torus).
395    /// - **Edge** — `{ ok, name, kind:"edge", solid, length, creatingFeature }`,
396    ///   plus `curveType` (`"Circle"`/`"Arc"`), `radius`, `diameter` and an
397    ///   arc's `sweepDeg` when the edge lies on a circle.
398    ///
399    /// `{ ok:false, name, message }` for an empty/unknown name, a non-resident
400    /// solid, or a kernel measurement failure.
401    ///
402    /// The real solid/face/edge MEASUREMENT is routed to the [`HistoryRunner`] (so
403    /// the warm-registry runner answers it, never the potentially-cold main side)
404    /// and CACHED here keyed by name — fired once per selection, served from the
405    /// cache every subsequent frame. For the synchronous
406    /// [`InlineRunner`](crate::runner::InlineRunner) the submit → `pump_queries`
407    /// resolves same-call, so this returns the merged JSON immediately and stays
408    /// byte-identical to the pre-seam in-process result; a background
409    /// [`ThreadRunner`](crate::runner::ThreadRunner) returns a `pending` placeholder
410    /// for the frame(s) until its reply lands (drained by `pump_queries`). The cache
411    /// is invalidated on any geometry change (`apply_run_output`) or metadata edit
412    /// (`set_metadata_attribute`).
413    ///
414    /// [`HistoryRunner`]: crate::runner::HistoryRunner
415    pub fn object_info_json(&mut self, name: &str) -> String {
416        let Some((kind, owner)) = self.resolve_object(name) else {
417            // A construction datum/plane carries no resident geometry (no volume /
418            // area / length), so it never resolves as a solid/face/edge. Return a
419            // graceful minimal record — name + kind + creating feature — rather than
420            // erroring, so the Properties Info tab renders for a selected datum and
421            // its name flows into the (name-keyed) Metadata tab.
422            if let Some((feature_id, feature_type)) = self.datum_feature_for_name(name) {
423                let kind_label = if feature_type == "P" { "plane" } else { "datum" };
424                return serde_json::json!({
425                    "ok": true,
426                    "name": name,
427                    "kind": kind_label,
428                    "creatingFeature": { "id": feature_id, "type": feature_type },
429                })
430                .to_string();
431            }
432            return serde_json::json!({
433                "ok": false, "name": name, "message": "unknown object",
434            })
435            .to_string();
436        };
437        // A committed-sketch SHEET is a scene solid with NO kernel handle, so the
438        // handle-based measurement path can't serve it. Measure straight off its
439        // synthesized display (planar-mesh triangle areas / edge polylines) and
440        // report `kind:"sketch"` — no volume. Handle-less ⇒ synchronous, no query.
441        if let Some(solid) = self.scene.solid(&owner) {
442            if solid.is_sketch {
443                return self.sketch_info_json(name, kind, solid);
444            }
445        }
446
447        // Real geometry: serve from the info cache, else fire a measurement query at
448        // the runner (deduped: never submit a second query for a name already in
449        // flight), pump, and return the resolved JSON — or a pending placeholder.
450        if let Some(cached) = self.info_cache.get(name) {
451            return cached.clone();
452        }
453        if !self.pending_query.values().any(|pending| pending == name) {
454            self.next_query_id += 1;
455            let id = self.next_query_id;
456            let measure_kind = match kind {
457                ObjectKind::Solid => MeasureKind::Solid,
458                ObjectKind::Face => MeasureKind::Face,
459                ObjectKind::Edge => MeasureKind::Edge,
460            };
461            let density = self.metadata.density(name);
462            self.pending_query.insert(id, name.to_string());
463            self.runner.submit_query(MeasureQuery {
464                id,
465                kind: measure_kind,
466                owner,
467                entity: name.to_string(),
468                density,
469            });
470        }
471        self.pump_queries();
472        match self.info_cache.get(name) {
473            Some(resolved) => resolved.clone(),
474            None => serde_json::json!({ "ok": false, "name": name, "pending": true }).to_string(),
475        }
476    }
477
478    /// Drain every completed measurement reply from the runner and fold it into the
479    /// name-keyed info cache — the query counterpart of [`EngineState::pump`]. Called
480    /// once per frame from `pump` AND synchronously from
481    /// [`Self::object_info_json`] (so the Inline runner resolves same-call). Each
482    /// reply is MERGED with the main-injected `name` + `creatingFeature` (from the
483    /// eager provenance) into the final object-info JSON.
484    pub fn pump_queries(&mut self) {
485        while let Some(reply) = self.runner.poll_query() {
486            let Some(name) = self.pending_query.remove(&reply.id) else {
487                // A reply whose request was invalidated (a rerun cleared the pending
488                // set) — drop it; the re-selection re-queries against fresh geometry.
489                continue;
490            };
491            let fragment: Value = serde_json::from_str(&reply.result).unwrap_or(Value::Null);
492            let merged = self.merge_info(&name, &fragment);
493            self.info_cache.insert(name, merged);
494        }
495    }
496
497    /// Merge a runner measurement FRAGMENT with the main-side `name` +
498    /// `creatingFeature` into the final object-info JSON, preserving the exact field
499    /// ORDER the pre-seam `object_info_json` emitted (so the output is
500    /// byte-identical): `ok`, then `name`, then the fragment's remaining fields in
501    /// order (`kind`, the measurements — or `message` on error), then
502    /// `creatingFeature` (only when `ok`, since an error record carries none).
503    fn merge_info(&self, name: &str, fragment: &Value) -> String {
504        let object = fragment.as_object();
505        let ok = object
506            .and_then(|map| map.get("ok"))
507            .and_then(Value::as_bool)
508            .unwrap_or(false);
509        let mut merged = serde_json::Map::new();
510        merged.insert("ok".to_string(), Value::Bool(ok));
511        merged.insert("name".to_string(), Value::String(name.to_string()));
512        if let Some(map) = object {
513            for (key, value) in map {
514                if key == "ok" {
515                    continue;
516                }
517                merged.insert(key.clone(), value.clone());
518            }
519        }
520        if ok {
521            merged.insert(
522                "creatingFeature".to_string(),
523                // Resolve by ENTITY NAME (not `owner`): a face/edge reports its
524                // ORIGIN, a solid its producer — the same correction the context bar
525                // gets, so the Info tab's `creatingFeature` agrees.
526                self.creating_feature_value(name),
527            );
528        }
529        Value::Object(merged).to_string()
530    }
531
532    /// Properties info for a committed-sketch SHEET object (the sheet solid, its
533    /// planar face, or a boundary edge), measured off the synthesized display — a
534    /// sketch carries no resident kernel geometry. `kind:"sketch"` for the whole
535    /// sheet (area + total edge length, NO volume); `"face"` for its planar face;
536    /// `"edge"` for a boundary edge. Provenance is the sketch feature itself.
537    fn sketch_info_json(
538        &self,
539        name: &str,
540        kind: ObjectKind,
541        solid: &crate::scene::SolidDisplay,
542    ) -> String {
543        let creating = serde_json::json!({
544            "id": solid.name,
545            "type": self.feature_type_of(&solid.name),
546        });
547        let edge_total: f64 = solid.edges.iter().map(|e| polyline_length(&e.polyline)).sum();
548        match kind {
549            ObjectKind::Solid => serde_json::json!({
550                "ok": true,
551                "name": name,
552                "kind": "sketch",
553                "area": sheet_mesh_area(solid),
554                "edgeLengthTotal": edge_total,
555                "creatingFeature": creating,
556            })
557            .to_string(),
558            ObjectKind::Face => serde_json::json!({
559                "ok": true,
560                "name": name,
561                "kind": "face",
562                "solid": solid.name,
563                // A committed sketch's sheet face is planar by construction.
564                "surfaceType": "Plane",
565                "area": sheet_mesh_area(solid),
566                "edgeLengthTotal": edge_total,
567                "creatingFeature": creating,
568            })
569            .to_string(),
570            ObjectKind::Edge => {
571                let length = solid
572                    .edges
573                    .iter()
574                    .find(|e| e.name == name)
575                    .map(|e| polyline_length(&e.polyline))
576                    .unwrap_or(0.0);
577                serde_json::json!({
578                    "ok": true,
579                    "name": name,
580                    "kind": "edge",
581                    "solid": solid.name,
582                    "length": length,
583                    "creatingFeature": creating,
584                })
585                .to_string()
586            }
587        }
588    }
589}
590
591/// Total surface area (mm²) of a synthesized sheet's planar display mesh — the
592/// sum of its triangle areas.
593fn sheet_mesh_area(solid: &crate::scene::SolidDisplay) -> f64 {
594    let p = &solid.mesh.positions;
595    solid
596        .mesh
597        .indices
598        .chunks_exact(3)
599        .map(|t| {
600            let a = p[t[0] as usize];
601            let b = p[t[1] as usize];
602            let c = p[t[2] as usize];
603            let ab = [
604                (b[0] - a[0]) as f64,
605                (b[1] - a[1]) as f64,
606                (b[2] - a[2]) as f64,
607            ];
608            let ac = [
609                (c[0] - a[0]) as f64,
610                (c[1] - a[1]) as f64,
611                (c[2] - a[2]) as f64,
612            ];
613            let cross = [
614                ab[1] * ac[2] - ab[2] * ac[1],
615                ab[2] * ac[0] - ab[0] * ac[2],
616                ab[0] * ac[1] - ab[1] * ac[0],
617            ];
618            (cross[0] * cross[0] + cross[1] * cross[1] + cross[2] * cross[2]).sqrt() * 0.5
619        })
620        .sum()
621}
622
623/// Arc length (mm) of a sampled edge polyline — the sum of its segment lengths.
624fn polyline_length(polyline: &[[f32; 3]]) -> f64 {
625    polyline
626        .windows(2)
627        .map(|w| {
628            let d = [
629                (w[1][0] - w[0][0]) as f64,
630                (w[1][1] - w[0][1]) as f64,
631                (w[1][2] - w[0][2]) as f64,
632            ];
633            (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt()
634        })
635        .sum()
636}
637