Skip to main content

brep_render/engine_state/
transform_gizmo.rs

1use super::*;
2
3/// Which of the two gizmos is currently armed for the feature. `Transform` shows
4/// the move/rotate gizmo; `Dimension` shows the on-canvas draggable dimension
5/// annotations (feature-dimensions FD-1). Expanding a feature arms `Dimension`;
6/// the viewport sphere/center toggle flips to `Transform` and back. The two modes
7/// are EXCLUSIVE — the transform gizmo never renders in `Dimension` mode and
8/// vice-versa.
9#[derive(Default, Clone, Copy, PartialEq, Eq, Debug)]
10pub enum GizmoMode {
11    /// Neither gizmo armed (`feature_id` is `None`).
12    #[default]
13    None,
14    /// The move/rotate transform gizmo is armed.
15    Transform,
16    /// The dimension-annotation gizmo is armed (FD-1).
17    Dimension,
18}
19
20/// The transform-controls gizmo controller state (see the impl block below).
21#[derive(Default)]
22pub struct TransformArm {
23    /// The feature id whose gizmo is armed (for EITHER mode), or `None`
24    /// (disarmed → `mode == None`).
25    pub(super) feature_id: Option<String>,
26    /// Which gizmo is armed for `feature_id` (transform vs dimension). `None`
27    /// exactly when `feature_id` is `None`.
28    pub(super) mode: GizmoMode,
29    /// The live handle drag, captured on pointer-down over a gizmo handle.
30    pub(super) drag: Option<TransformDrag>,
31    /// When the armed subject is ONE SPLINE ANCHOR rather than the feature's
32    /// `transform` param: the anchor index into `persistentData.spline.points`
33    /// of `feature_id` (an SP feature). The same gizmo, press / drag / release
34    /// and live-follow apply; only the pose read and write-back differ (see
35    /// `spline_edit.rs`).
36    pub(super) anchor: Option<usize>,
37    /// When the armed subject is ONE DECLARED CONNECTION POINT: its part-local
38    /// address (`J1.VCC`). Exclusive with `feature_id` — a connection point is
39    /// data, not a feature, so there is no history row to hold — and checked
40    /// FIRST everywhere the pose is read or written, before any lookup that
41    /// goes through the history. See the section at the foot of this file.
42    pub(super) port: Option<String>,
43}
44
45/// A grab snapshot for an in-flight transform-gizmo drag: the grabbed handle,
46/// the grab screen point, and the feature's pose AT GRAB. Every drag move
47/// resolves an absolute delta from this grab (against the pinned grab-time
48/// frame) and re-applies it to `start`, so the drag never accumulates error.
49#[derive(Clone, Copy)]
50pub(super) struct TransformDrag {
51    handle: u32,
52    sx: f32,
53    sy: f32,
54    start: TransformPose,
55    /// The delta this drag has resolved SO FAR, as the handle resolved it —
56    /// the readout's source. Taken from the drag rather than differenced out of
57    /// the pose, because the pose is the only thing the two are agreed on: a
58    /// rotation composes onto a quaternion and comes back as an Euler triple
59    /// that a subtraction cannot turn into "42.5 degrees about Y" again. `None`
60    /// until the pointer has actually moved off the grab point.
61    last: Option<TransformDelta>,
62}
63
64/// A TRS pose read from / written to a feature's `inputParams.transform`
65/// (`rotation` in DEGREES, intrinsic XYZ Euler order — the kernel `transform_bake`
66/// convention, `M = T·R·S`).
67#[derive(Clone, Copy, PartialEq, Debug)]
68pub(super) struct TransformPose {
69    pub(super) position: [f64; 3],
70    pub(super) rotation_deg: [f64; 3],
71    pub(super) scale: [f64; 3],
72}
73
74/// A world-space delta shared by feature and component gizmo controllers.
75#[derive(Clone, Copy, PartialEq, Debug)]
76pub(super) enum TransformDelta {
77    /// World-space translation, added to `position`.
78    Translate([f64; 3]),
79    /// World-axis rotation, pre-multiplied onto the pose's orientation.
80    Rotate { axis: [f64; 3], radians: f64 },
81}
82
83/// Decode widget drag JSON; unknown kinds and invalid JSON produce no delta.
84pub(super) fn parse_drag_delta(json: &str) -> Option<TransformDelta> {
85    let value: serde_json::Value = serde_json::from_str(json).ok()?;
86    match value.get("kind").and_then(|k| k.as_str()) {
87        Some("translate") => Some(TransformDelta::Translate(
88            crate::json_support::vec3_or(value.get("world"), [0.0; 3]),
89        )),
90        Some("rotate") => Some(TransformDelta::Rotate {
91            axis: crate::json_support::vec3_or(value.get("axisWorld"), [0.0; 3]),
92            radians: value.get("radians").and_then(|n| n.as_f64()).unwrap_or(0.0),
93        }),
94        _ => None,
95    }
96}
97
98impl EngineState {
99    /// Whether the TRANSFORM gizmo (move/rotate) is armed for ANY feature. False
100    /// in dimension mode — the two ◎ modes are exclusive, so the transform gizmo
101    /// arms/handles never render while dimensions are shown.
102    pub fn transform_armed(&self) -> bool {
103        matches!(self.transform_gizmo.mode, GizmoMode::Transform)
104    }
105
106    /// The transform-gizmo-armed feature id (empty unless in transform mode).
107    pub fn transform_armed_feature(&self) -> String {
108        if self.transform_armed() {
109            self.transform_gizmo.feature_id.clone().unwrap_or_default()
110        } else {
111            String::new()
112        }
113    }
114
115    /// The gizmo as the headed verifier sees it: the mode, the armed feature,
116    /// the spline anchor index when the gizmo sits on one, and `origin` — the
117    /// DRAWN move/rotate widget's frame origin (`null` when no widget is
118    /// drawn), which is the proof a handle is on screen, unlike any
119    /// params-derived pose.
120    pub fn gizmo_state_json(&self) -> String {
121        let mode = match self.transform_gizmo.mode {
122            GizmoMode::None => "none",
123            GizmoMode::Transform => "transform",
124            GizmoMode::Dimension => "dimension",
125        };
126        serde_json::json!({
127            "mode": mode,
128            "armed": self.transform_armed(),
129            "feature": self.transform_gizmo.feature_id,
130            "anchor": self.transform_gizmo.anchor,
131            "portPoint": self.transform_gizmo.port,
132            "origin": self.widgets.transform_origin(),
133            // A Transform Face turns about its stored `pivot`, and the gizmo
134            // sits at `pivot + position` — so the two coincide only while the
135            // motion is zero. The moment the user drags, the point the rotation
136            // actually happens about is no longer under the handles and was
137            // nowhere on screen at all. Published so the viewport can mark it.
138            "pivot": self.armed_face_transform_pivot(),
139        })
140        .to_string()
141    }
142
143    /// The armed feature's Transform Face pivot in WORLD space, or `None` when
144    /// the armed feature is not a Transform Face (every other transform feature
145    /// turns about its own origin, which the gizmo is already on).
146    pub fn armed_face_transform_pivot(&self) -> Option<[f64; 3]> {
147        let id = self.transform_gizmo.feature_id.as_deref()?;
148        let index = self.history.index_of(id)?;
149        if !self.is_face_transform_feature(index) {
150            return None;
151        }
152        let params = self.history.feature_params(index)?;
153        let env = brep_kernel::Env::build(&self.history.expressions(), &self.history.configurator()).ok();
154        self.face_transform_pivot(index, &params, env.as_ref())
155    }
156
157    /// What the gizmo drag is WORTH, for the chip the viewport draws on it —
158    /// and, when the motion refused, why it refused, said about the user's
159    /// model rather than about the kernel's internals.
160    ///
161    /// A gizmo drag used to be the one edit in the app that reported nothing:
162    /// the model followed the pointer, the number went into `transform`, the
163    /// form's `Transform` section is collapsed by default, and so the distance
164    /// the user had just dragged appeared NOWHERE on screen. The Extrude
165    /// distance arrow carries its value in a chip on the geometry; this is the
166    /// same answer for the gizmo.
167    ///
168    /// The readout is live in two states, and silent otherwise:
169    ///
170    /// * **while a handle is being dragged** — the distance or angle so far.
171    /// * **while the armed feature is REFUSING** — because that is the state
172    ///   the user is left in after letting go of a refused drag, and it is the
173    ///   state that most needs saying. The gizmo tracks the pointer while the
174    ///   geometry stays at its last good answer, so on a refusal the handles
175    ///   end up somewhere the model is not — measured at 15.4 mm INSIDE the
176    ///   solid — and without this the only signal was a red banner in a dock on
177    ///   the far side of the window from the pointer.
178    ///
179    /// `origin` is where to draw it: the gizmo's own frame origin, which is
180    /// where the pointer is.
181    pub fn transform_readout_json(&self) -> String {
182        let quiet = serde_json::json!({ "active": false }).to_string();
183        if !self.transform_armed() {
184            return quiet;
185        }
186        let Some(origin) = self.widgets.transform_origin() else {
187            return quiet;
188        };
189
190        // The refusal, if this feature is refusing — the armed feature's entry
191        // in the last run's report, re-said around the faces it names.
192        let refusal = self.armed_face_transform_refusal();
193        let drag = self.transform_gizmo.drag.as_ref();
194        let delta = drag.and_then(|d| d.last);
195
196        // Silent unless something is happening: no drag in flight and no
197        // refusal on the board means the model is simply sitting there.
198        if delta.is_none() && refusal.is_none() {
199            return quiet;
200        }
201
202        // What the handle resolved, formatted the way a dimension label is —
203        // `compact_decimal(_, 4)`, so the chip and the form agree digit for
204        // digit rather than rounding to different numbers.
205        let (kind, label, value, unit, text) = match delta {
206            Some(TransformDelta::Translate(world)) => {
207                let distance = (world[0] * world[0] + world[1] * world[1] + world[2] * world[2]).sqrt();
208                let axis = dominant_axis_label(&world);
209                let shown = crate::formatting::compact_decimal(distance, 4);
210                (
211                    "translate",
212                    axis.to_string(),
213                    distance,
214                    "mm",
215                    format!("{axis}  {shown} mm"),
216                )
217            }
218            Some(TransformDelta::Rotate { axis, radians }) => {
219                let degrees = radians.to_degrees();
220                let label = dominant_axis_label(&axis);
221                let shown = crate::formatting::compact_decimal(degrees, 4);
222                (
223                    "rotate",
224                    label.to_string(),
225                    degrees,
226                    "deg",
227                    format!("{label}  {shown}\u{00b0}"),
228                )
229            }
230            // Refusing, but not mid-drag (the user has let go, or typed the
231            // value): the chip carries the refusal alone.
232            None => ("none", String::new(), 0.0, "", String::new()),
233        };
234
235        let mut out = serde_json::json!({
236            "active": true,
237            "kind": kind,
238            "axis": label,
239            "value": value,
240            "unit": unit,
241            "text": text,
242            "origin": origin,
243            "dragging": drag.is_some(),
244            "refused": refusal.is_some(),
245        });
246        if let Some(refusal) = refusal {
247            out["motion"] = serde_json::json!(refusal.motion);
248            out["reason"] = serde_json::json!(refusal.reason);
249            out["hint"] = serde_json::json!(refusal.hint);
250            out["kernel"] = serde_json::json!(refusal.kernel);
251        }
252        out.to_string()
253    }
254
255    /// The armed feature's refusal, re-said about the model — `None` when it is
256    /// not a Transform Face, or is not refusing. The face names come from the
257    /// feature's own `params.faces`, which is the thing the kernel's message
258    /// does not carry and the app does.
259    fn armed_face_transform_refusal(&self) -> Option<crate::face_transform_help::FaceTransformRefusal> {
260        let id = self.transform_gizmo.feature_id.as_deref()?;
261        let index = self.history.index_of(id)?;
262        if !self.is_face_transform_feature(index) {
263            return None;
264        }
265        let report: serde_json::Value = serde_json::from_str(&self.history_report).ok()?;
266        // The kernel records each failure as `"<feature id>: <message>"`; the
267        // delimiter after the exact id is what stops a shorter id matching a
268        // longer one (`TF1` vs `TF10`).
269        let prefix = format!("{id}: ");
270        let message = report
271            .get("featureErrors")?
272            .as_array()?
273            .iter()
274            .filter_map(serde_json::Value::as_str)
275            .find(|entry| entry.starts_with(&prefix))
276            .map(|entry| &entry[prefix.len()..])?;
277        let params = self.history.feature_params(index)?;
278        let faces = brep_kernel::reference_names(params.get("faces"));
279        let facts = crate::face_transform_help::RefusalFacts::from_report(&report, id);
280        Some(crate::face_transform_help::explain_refusal(message, facts, &faces))
281    }
282
283    /// Whether the TRANSFORM gizmo is armed for THIS feature.
284    pub fn transform_armed_for(&self, feature_id: &str) -> bool {
285        self.transform_armed() && self.transform_gizmo.feature_id.as_deref() == Some(feature_id)
286    }
287
288    /// Arm the TRANSFORM gizmo for `feature_id` and feed it at the feature's
289    /// transform frame. Re-arming a different feature moves the gizmo to it.
290    /// Clears any dimension overlay (the modes are exclusive).
291    ///
292    /// An ACOMP (assembly component) feature ROUTES to the component Move gizmo
293    /// instead: its `transform` is the component pose (`{translate,
294    /// rotateEulerDeg}`, a different shape), its gizmo attaches at the member
295    /// bbox center, commits on release, and refuses fixed instances — the
296    /// generic position/rotationEuler gizmo must never write its keys into an
297    /// ACOMP's params. This covers the history panel's arm-on-expand path too.
298    pub fn arm_transform(&mut self, feature_id: &str) {
299        let is_acomp = self
300            .history
301            .index_of(feature_id)
302            .and_then(|index| self.history.feature_type(index))
303            .is_some_and(|ty| super::components::is_acomp_feature_type(&ty));
304        if is_acomp {
305            self.component_move_arm(feature_id);
306            return;
307        }
308        self.component_move_reset();
309        self.transform_gizmo.feature_id = Some(feature_id.to_string());
310        self.transform_gizmo.mode = GizmoMode::Transform;
311        self.transform_gizmo.drag = None;
312        self.transform_gizmo.anchor = None;
313        self.transform_gizmo.port = None;
314        self.clear_feature_dimension_overlay();
315        self.sync_transform_gizmo();
316    }
317
318    /// Disarm: hide BOTH gizmos + drop any in-flight drag. Also resets the
319    /// component Move gizmo (the widget slot is shared, so a disarm clears
320    /// whichever controller was feeding it).
321    pub fn disarm_transform(&mut self) {
322        self.component_move_reset();
323        self.transform_gizmo.feature_id = None;
324        self.transform_gizmo.mode = GizmoMode::None;
325        self.transform_gizmo.drag = None;
326        self.transform_gizmo.anchor = None;
327        self.transform_gizmo.port = None;
328        let _ = self.widgets.set_transform_json("null");
329        self.clear_feature_dimension_overlay();
330        self.dirty = true;
331    }
332
333    /// The armed feature's current TRS pose (from its `inputParams.transform`),
334    /// or `None` when disarmed / the feature vanished.
335    fn armed_pose(&self) -> Option<TransformPose> {
336        // A connection point has no history row, so this must come before
337        // every lookup that needs one.
338        if let Some(address) = self.transform_gizmo.port.clone() {
339            return self.port_point_pose(&address);
340        }
341        let id = self.transform_gizmo.feature_id.as_deref()?;
342        if let Some(row) = self.pmi_report.as_ref().and_then(|r| r.annotation(id)) {
343            if self.pmi_active_view.as_deref()
344                != self
345                    .pmi_state()
346                    .find_annotation(id)
347                    .map(|(v, _)| v.id.as_str())
348            {
349                return None;
350            }
351            if let brep_kernel::PmiGeometry::Explode {
352                center,
353                translate,
354                rotate_deg,
355                scale,
356                ..
357            } = &row.geometry
358            {
359                return Some(TransformPose {
360                    position: std::array::from_fn(|i| center[i] + translate[i]),
361                    rotation_deg: *rotate_deg,
362                    scale: *scale,
363                });
364            }
365        }
366        let index = self.history.index_of(id)?;
367        if let Some(anchor) = self.transform_gizmo.anchor {
368            // A spline anchor: its position + the intrinsic-XYZ Euler of its
369            // stored axis triad (see `spline_edit.rs`).
370            let (position, rotation_deg) = self.spline_anchor_pose(index, anchor)?;
371            return Some(TransformPose {
372                position,
373                rotation_deg,
374                scale: [1.0, 1.0, 1.0],
375            });
376        }
377        let params = self.history.feature_params(index)?;
378        let transform = params.get("transform");
379        // Build the history's expression environment ONCE for the vectors: a
380        // transform or pivot component may be an expression string.
381        let env =
382            brep_kernel::Env::build(&self.history.expressions(), &self.history.configurator()).ok();
383        let env = env.as_ref();
384        let pivot = if self.is_xform_feature(index) {
385            self.xform_pivot(index, &params)
386        } else if self.is_face_transform_feature(index) {
387            // No faces, no pivot, no gizmo: there is nothing to put it on yet.
388            self.face_transform_pivot(index, &params, env)?
389        } else {
390            [0.0; 3]
391        };
392        let position = read_pose_vec3(env, transform, "position", [0.0; 3]);
393        Some(TransformPose {
394            position: std::array::from_fn(|axis| position[axis] + pivot[axis]),
395            rotation_deg: read_pose_vec3(env, transform, "rotationEuler", [0.0, 0.0, 0.0]),
396            scale: read_pose_vec3(env, transform, "scale", [1.0, 1.0, 1.0]),
397        })
398    }
399
400    fn is_xform_feature(&self, index: usize) -> bool {
401        self.history.feature_type(index).is_some_and(|ty| {
402            ty.eq_ignore_ascii_case("XFORM") || ty.eq_ignore_ascii_case("TRANSFORM")
403        })
404    }
405
406    /// Whether the feature at `index` is a Transform Face — the feature whose
407    /// motion turns about a STORED `pivot` param.
408    pub fn is_face_transform_feature(&self, index: usize) -> bool {
409        self.history
410            .feature_type(index)
411            .is_some_and(|ty| is_face_transform_type(&ty))
412    }
413
414    /// The point a Transform Face turns about: its stored `pivot`, or — while
415    /// that is still null — the kernel's default, the selection's centre over
416    /// the scene the feature runs against. `None` when neither exists (no face
417    /// resolves), which is what keeps the gizmo off an empty selection.
418    fn face_transform_pivot(
419        &self,
420        index: usize,
421        params: &serde_json::Value,
422        env: Option<&brep_kernel::Env>,
423    ) -> Option<[f64; 3]> {
424        match params.get("pivot") {
425            Some(pivot) if !pivot.is_null() => Some(read_pose_vec3(env, Some(params), "pivot", [0.0; 3])),
426            _ => self.face_transform_default_pivot(index, params),
427        }
428    }
429
430    /// Whether the Transform Face `feature_id` has a pivot to put its gizmo on —
431    /// a stored one, or faces that resolve to a default. The history panel arms
432    /// the gizmo on the frame this first holds.
433    pub fn face_transform_has_pivot(&self, feature_id: &str) -> bool {
434        let Some(index) = self.history.index_of(feature_id) else {
435            return false;
436        };
437        let Some(params) = self.history.feature_params(index) else {
438            return false;
439        };
440        self.is_face_transform_feature(index) && self.face_transform_pivot(index, &params, None).is_some()
441    }
442
443    /// Transform Face's DEFAULT pivot for `params.faces`: the prefix BEFORE the
444    /// feature is replayed (warm, like [`Self::xform_pivot`]'s) and handed to
445    /// [`brep_kernel::face_transform_pivot`] — the same resolution and centre the
446    /// feature itself falls back to, so the stored value and the kernel agree by
447    /// construction.
448    fn face_transform_default_pivot(
449        &self,
450        index: usize,
451        params: &serde_json::Value,
452    ) -> Option<[f64; 3]> {
453        let names = brep_kernel::reference_names(params.get("faces"));
454        if names.is_empty() {
455            return None;
456        }
457        let request: HistoryRequest =
458            serde_json::from_value(self.history.request_before(index)?).ok()?;
459        let _trace = crate::run_trace::span("face_transform_pivot");
460        let result = self.execute_plugin_history(&request);
461        brep_kernel::face_transform_pivot(&result.results, &names)
462    }
463
464    /// STORE a Transform Face's pivot into the params about to be written for
465    /// the feature at `index`: when this write changes the face selection
466    /// without also setting a pivot, or leaves the pivot null, `pivot` becomes the
467    /// new selection's centre (null again when nothing resolves). A pivot the
468    /// write sets itself is kept. Stored rather than derived per run, so the
469    /// rotation centre is a number in the document: a re-run, a gizmo drag, or an
470    /// upstream edit never moves it. Every lane that writes feature params calls
471    /// this before the write (`update_feature_params`, the batch lane, the
472    /// reference picker's Finish).
473    pub(crate) fn stamp_face_transform_pivot(&self, index: usize, params: &mut serde_json::Value) {
474        if !self.is_face_transform_feature(index) {
475            return;
476        }
477        let before = self.history.feature_params(index).unwrap_or(serde_json::Value::Null);
478        let faces_changed = brep_kernel::reference_names(before.get("faces"))
479            != brep_kernel::reference_names(params.get("faces"));
480        let pivot_set_here = before.get("pivot") != params.get("pivot");
481        let unset = params.get("pivot").is_none_or(serde_json::Value::is_null);
482        if !(unset || (faces_changed && !pivot_set_here)) {
483            return;
484        }
485        let pivot = self.face_transform_default_pivot(index, params);
486        if let Some(object) = params.as_object_mut() {
487            object.insert(
488                "pivot".into(),
489                pivot.map_or(serde_json::Value::Null, |pivot| serde_json::json!(pivot)),
490            );
491        }
492    }
493
494    /// XFORM rotates each selected solid about its own source vertex-bbox center.
495    /// Anchor the shared controls to the first resolved solid's pivot; the same
496    /// translation/rotation delta still applies to every selected solid.
497    fn xform_pivot(&self, index: usize, params: &serde_json::Value) -> [f64; 3] {
498        if !params["pivot"]
499            .as_str()
500            .is_some_and(|p| p.eq_ignore_ascii_case("BBOX_CENTER"))
501        {
502            return [0.0; 3];
503        }
504        // Replay the warm current prefix, then fold only results BEFORE XFORM.
505        // Using the transformed scene bbox would make the pivot drift on rotation.
506        let Ok(request) = serde_json::from_value::<HistoryRequest>(self.run_request_value())
507        else {
508            return [0.0; 3];
509        };
510        let _trace = crate::run_trace::span("xform_pivot");
511        let result = self.execute_plugin_history(&request);
512        let mut handles = HashMap::new();
513        for feature in result.results.iter().take(index) {
514            for name in &feature.removed {
515                handles.remove(name);
516            }
517            for solid in &feature.added {
518                handles.insert(solid.name.clone(), solid.handle);
519            }
520        }
521        let Some(refs) = params["solids"].as_array() else {
522            return [0.0; 3];
523        };
524        for reference in refs {
525            let Some(name) = reference.as_str().or_else(|| reference["name"].as_str()) else {
526                continue;
527            };
528            let Some(handle) = handles.get(name.trim()) else {
529                continue;
530            };
531            if let Ok(center) = brep_kernel::transform_pivot_native(*handle) {
532                return center;
533            }
534        }
535        [0.0; 3]
536    }
537
538    /// (Re)feed the widget gizmo at the armed feature's frame: origin =
539    /// `position`, axes = the feature's rotated basis (intrinsic XYZ Euler order, matching
540    /// the kernel bake). Auto-disarms if the feature vanished. Called every drag
541    /// frame from `transform_drag_to` so the widget tracks the moving pose live
542    /// (Fix 3); the drag delta resolves against the frozen grab frame, so this
543    /// re-sync never feeds back into the drag math.
544    pub fn sync_transform_gizmo(&mut self) {
545        // Only the TRANSFORM mode feeds the move/rotate widget; in dimension mode
546        // the widget stays hidden (the annotations render as an overlay instead).
547        if !matches!(self.transform_gizmo.mode, GizmoMode::Transform) {
548            return;
549        }
550        let Some(pose) = self.armed_pose() else {
551            self.disarm_transform();
552            return;
553        };
554        let _ = self
555            .widgets
556            .set_transform_json(&transform_frame_json(&pose));
557        self.dirty = true;
558    }
559
560    /// Whether the feature schema exposes the shared Transform controls.
561    /// Capability belongs to the schema, not to which default-valued fields
562    /// happen to have been serialized in the model.
563    pub fn feature_has_transform(&self, feature_id: &str) -> bool {
564        self.history
565            .index_of(feature_id)
566            .and_then(|i| self.history.feature_type(i))
567            .and_then(|ty| self.feature_schema(&ty))
568            .is_some_and(|schema| schema["inputParamsSchema"]["transform"]["type"] == "transform")
569    }
570
571    /// The armed gizmo origin projected to VIEWPORT-LOCAL px (the center handle
572    /// sits here). The history panel publishes it so the headed verifier can
573    /// locate + drag the gizmo. `None` when disarmed / not projectable (the ONE
574    /// [`crate::view::ViewCamera::projectable`] policy — ortho always projects).
575    pub fn transform_gizmo_anchor(&self) -> Option<(f64, f64)> {
576        let pose = self.armed_pose()?;
577        let (sx, sy, _) = self.camera.project(pose.position);
578        self.camera.projectable(pose.position).then_some((sx, sy))
579    }
580
581    /// The transform gizmo's axis-end labels as JSON:
582    /// `[{ text:"XC"|"YC"|"ZC", rgb:[r,g,b], world:[x,y,z] }]`. The app projects
583    /// each `world` point and draws the colored egui label just past the matching
584    /// cone tip (X=red, Y=green, Z=blue). `[]` unless the TRANSFORM gizmo is armed.
585    pub fn transform_axis_labels_json(&self) -> String {
586        if !matches!(self.transform_gizmo.mode, GizmoMode::Transform) {
587            return "[]".to_string();
588        }
589        let Some(pose) = self.armed_pose() else {
590            return "[]".to_string();
591        };
592        let euler = [
593            pose.rotation_deg[0].to_radians(),
594            pose.rotation_deg[1].to_radians(),
595            pose.rotation_deg[2].to_radians(),
596        ];
597        let axes = [
598            normalize3(rotate_euler_xyz_f64([1.0, 0.0, 0.0], euler)),
599            normalize3(rotate_euler_xyz_f64([0.0, 1.0, 0.0], euler)),
600            normalize3(rotate_euler_xyz_f64([0.0, 0.0, 1.0], euler)),
601        ];
602        // Just past the cone tip, screen-constant.
603        let gap_px = 12.0_f64;
604        let dist =
605            (brep_gizmos::transform::PX_AXIS_LEN as f64 + gap_px) * self.camera.world_per_pixel();
606        let labels: [(&str, [f32; 3]); 3] = [
607            ("XC", [0.92, 0.26, 0.28]), // red
608            ("YC", [0.30, 0.78, 0.36]), // green
609            ("ZC", [0.30, 0.52, 0.98]), // blue
610        ];
611        let out: Vec<serde_json::Value> = (0..3)
612            .map(|i| {
613                let o = pose.position;
614                let a = axes[i];
615                serde_json::json!({
616                    "text": labels[i].0,
617                    "rgb": labels[i].1,
618                    "world": [o[0] + a[0] * dist, o[1] + a[1] * dist, o[2] + a[2] * dist],
619                })
620            })
621            .collect();
622        serde_json::to_string(&out).unwrap_or_else(|_| "[]".to_string())
623    }
624
625    /// A press point for every AXIS ARROW and ROTATION GRAB of the drawn
626    /// transform gizmo, in VIEWPORT-LOCAL px, keyed `axis:x|y|z` and
627    /// `ring:x|y|z` (a ring is named by the frame axis it turns about). Read off
628    /// the SAME regions [`Self::transform_pick`] tests — three quarters of the way
629    /// along an arrow's capsule spine, a grab ball's centre — and kept only where
630    /// a press there picks THAT handle, so a published key can never grab a
631    /// neighbour (an arrow foreshortened onto a ring ball, say). Empty while no
632    /// transform widget is drawn. The centre handle is `gizmo/anchor`'s.
633    pub fn transform_handle_points(&self) -> Vec<(&'static str, (f64, f64))> {
634        use brep_gizmos::hit_region::HitShape;
635        use brep_gizmos::transform as gizmo;
636        let cam = gizmo_camera(&self.camera);
637        self.widgets
638            .transform_hit_regions(&cam)
639            .into_iter()
640            .filter_map(|(handle, shape)| {
641                let key = match handle {
642                    gizmo::HANDLE_AXIS_X => "axis:x",
643                    gizmo::HANDLE_AXIS_Y => "axis:y",
644                    gizmo::HANDLE_AXIS_Z => "axis:z",
645                    gizmo::HANDLE_RING_X => "ring:x",
646                    gizmo::HANDLE_RING_Y => "ring:y",
647                    gizmo::HANDLE_RING_Z => "ring:z",
648                    _ => return None,
649                };
650                let [x, y] = match shape {
651                    HitShape::Capsule { a, b, .. } => {
652                        [a[0] + 0.75 * (b[0] - a[0]), a[1] + 0.75 * (b[1] - a[1])]
653                    }
654                    HitShape::Circle { c, .. } => c,
655                };
656                let point = (f64::from(x), f64::from(y));
657                (self.transform_pick(point.0, point.1) == handle).then_some((key, point))
658            })
659            .collect()
660    }
661
662    /// Viewport-local pick regions for the debug overlay, shared by feature and
663    /// component controllers. The widget feed determines visibility; hidden
664    /// widgets return `[]`. The app offsets these circles/capsules by `rect.min`.
665    pub fn transform_hit_areas_json(&self) -> String {
666        let cam = gizmo_camera(&self.camera);
667        let regions = self.widgets.transform_hit_regions(&cam);
668        super::camera_widgets::hit_shapes_json(regions.iter().map(|(_, shape)| shape))
669    }
670
671    /// Begin a gizmo drag at viewport px `(x, y)` when the gizmo is armed AND a
672    /// handle is under the pointer. Returns whether a handle was grabbed — the
673    /// viewport routes the drag to the gizmo (not the camera) when `true`; a press
674    /// on empty space returns `false` and still orbits.
675    pub fn transform_press(&mut self, x: f64, y: f64) -> bool {
676        // Only grabbable in TRANSFORM mode — in dimension mode the dimension
677        // handles own the pointer (routed by the app), and disarmed grabs nothing.
678        if !matches!(self.transform_gizmo.mode, GizmoMode::Transform) {
679            return false;
680        }
681        let handle = self.transform_pick(x, y);
682        if handle == 0 {
683            return false;
684        }
685        let Some(pose) = self.armed_pose() else {
686            return false;
687        };
688        self.widgets.set_transform_active(handle);
689        self.transform_gizmo.drag = Some(TransformDrag {
690            handle,
691            sx: x as f32,
692            sy: y as f32,
693            start: pose,
694            last: None,
695        });
696        self.dirty = true;
697        true
698    }
699
700    /// Whether a gizmo handle drag is in flight.
701    pub fn transform_dragging(&self) -> bool {
702        self.transform_gizmo.drag.is_some()
703    }
704
705    /// Continue the in-flight gizmo drag to viewport px `(cx, cy)`: resolve the
706    /// world delta from the grab (against the frozen grab-time frame), apply it to
707    /// the grab pose, write it back into the feature's `transform`, and re-run so
708    /// the model follows live. Then re-sync the VISIBLE gizmo to the moved pose so
709    /// the widget tracks the pointer in real time (Fix 3) — the delta stays
710    /// anchored to `drag.start`, so this visual sync never feeds back on itself.
711    pub fn transform_drag_to(&mut self, cx: f64, cy: f64) {
712        let Some(drag) = self.transform_gizmo.drag else {
713            return;
714        };
715        let Some(delta) = self.resolve_transform_delta(&drag, cx, cy) else {
716            return;
717        };
718        let pose = apply_transform_delta(&drag.start, &delta);
719        // Remember what the handle resolved, so the readout can say it. The
720        // drag is re-resolved from `drag.start` every frame, so this is the
721        // WHOLE motion since the grab, not an increment to accumulate.
722        if let Some(live) = self.transform_gizmo.drag.as_mut() {
723            live.last = Some(delta);
724        }
725        self.write_armed_pose(&pose);
726        // Live-follow: re-feed the widget frame to the just-written pose. NB
727        // `finish_apply` skips this while a drag is in flight (drag.is_some()), so
728        // the sync happens here. The active-handle gold highlight survives (the
729        // widget only clears it on a `null` feed).
730        self.sync_transform_gizmo();
731    }
732
733    /// End the drag: clear the active-handle highlight + re-sync the gizmo to the
734    /// feature's final (moved) pose (unpin the frame).
735    pub fn transform_release(&mut self) {
736        if self.transform_gizmo.drag.take().is_some() {
737            self.widgets.set_transform_active(0);
738            // ONE UNDO STEP PER DRAG. `set_feature_params` coalesces on
739            // `param:{index}` so that the hundred writes a single drag makes do
740            // not become a hundred undo entries — but nothing ended the run, so
741            // the coalescing went on across drags too: two drags of the same
742            // feature were ONE entry, and a single undo after a refused drag
743            // threw away the good drag before it as well (measured
744            // 2026-09-21 — 0.9254 then 1.8508, one undo, straight to zero).
745            // Ending the run here is the same thing the sheet label drag does
746            // on release, and it is what makes "undo the drag that refused,
747            // keep the one before it" work.
748            self.history.break_coalescing();
749            self.sync_transform_gizmo();
750        }
751    }
752
753    /// Resolve against the grab-time frame so live widget updates cannot
754    /// feed back into the drag calculation.
755    fn resolve_transform_delta(
756        &self,
757        drag: &TransformDrag,
758        cx: f64,
759        cy: f64,
760    ) -> Option<TransformDelta> {
761        let cam = gizmo_camera(&self.camera);
762        let frame_json = transform_frame_json(&drag.start);
763        let json = self.widgets.transform_drag_json_with_frame(
764            &cam,
765            &frame_json,
766            drag.handle,
767            drag.sx,
768            drag.sy,
769            cx as f32,
770            cy as f32,
771        );
772        parse_drag_delta(&json)
773    }
774
775    /// Write `pose` into the armed feature's `inputParams.transform.{position,
776    /// rotationEuler}` (preserving every other field, incl. `scale`) and re-run.
777    fn write_armed_pose(&mut self, pose: &TransformPose) {
778        if let Some(address) = self.transform_gizmo.port.clone() {
779            self.write_port_point_pose(&address, pose);
780            return;
781        }
782        let Some(id) = self.transform_gizmo.feature_id.clone() else {
783            return;
784        };
785        let pmi = self.pmi_state();
786        if let Some((_, annotation)) = pmi.find_annotation(&id) {
787            if annotation.kind == "explode" {
788                let center = self
789                    .pmi_report
790                    .as_ref()
791                    .and_then(|r| r.annotation(&id))
792                    .and_then(|r| match &r.geometry {
793                        brep_kernel::PmiGeometry::Explode { center, .. } => Some(*center),
794                        _ => None,
795                    });
796                if let Some(center) = center {
797                    let mut params = annotation.params.clone();
798                    params["transform"] = serde_json::json!({ "position": std::array::from_fn::<_, 3, _>(|i| pose.position[i] - center[i]), "rotationEuler": pose.rotation_deg, "scale": pose.scale });
799                    let _ = self.pmi_update_annotation(&id, &params.to_string());
800                }
801                return;
802            }
803        }
804        let Some(index) = self.history.index_of(&id) else {
805            return;
806        };
807        if let Some(anchor) = self.transform_gizmo.anchor {
808            self.write_spline_anchor_pose(&id, anchor, pose.position, pose.rotation_deg);
809            return;
810        }
811        let mut params = self
812            .history
813            .feature_params(index)
814            .unwrap_or_else(|| serde_json::json!({}));
815        let pivot = if self.is_xform_feature(index) {
816            self.xform_pivot(index, &params)
817        } else if self.is_face_transform_feature(index) {
818            let env =
819                brep_kernel::Env::build(&self.history.expressions(), &self.history.configurator())
820                    .ok();
821            let Some(pivot) = self.face_transform_pivot(index, &params, env.as_ref()) else {
822                return;
823            };
824            pivot
825        } else {
826            [0.0; 3]
827        };
828        let position: [f64; 3] = std::array::from_fn(|axis| pose.position[axis] - pivot[axis]);
829        // Ensure `transform` is an object, then set the two edited vectors.
830        if !params
831            .get("transform")
832            .map(|t| t.is_object())
833            .unwrap_or(false)
834        {
835            if let Some(object) = params.as_object_mut() {
836                object.insert("transform".into(), serde_json::json!({}));
837            }
838        }
839        if let Some(transform) = params.get_mut("transform").and_then(|t| t.as_object_mut()) {
840            transform.insert("position".into(), serde_json::json!(position));
841            transform.insert("rotationEuler".into(), serde_json::json!(pose.rotation_deg));
842        }
843        let _ = self.update_feature_params(&id, &params.to_string());
844    }
845}
846
847/// Name the axis a world vector lies along — `X`, `Y` or `Z` when it lies along
848/// one to within a thousandth of its length, and the neutral `\u{0394}` when it
849/// does not.
850///
851/// The neutral case is not a fallback, it is the honest answer: the X ARROW OF
852/// A TURNED FRAME points along the turned X, which has two non-zero world
853/// components, and calling that motion "X" would name a world axis the face is
854/// not moving along. A zero-length vector is `\u{0394}` too — there is no axis
855/// to name yet.
856fn dominant_axis_label(v: &[f64; 3]) -> &'static str {
857    let length = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt();
858    if length < 1e-12 {
859        return "\u{0394}";
860    }
861    const NAMES: [&str; 3] = ["X", "Y", "Z"];
862    for axis in 0..3 {
863        if v[axis].abs() >= length * 0.999 {
864            return NAMES[axis];
865        }
866    }
867    "\u{0394}"
868}
869
870/// The type strings a Transform Face answers to — the kernel dispatch's alias
871/// set, case-insensitively.
872pub fn is_face_transform_type(feature_type: &str) -> bool {
873    matches!(
874        feature_type.trim().to_ascii_uppercase().as_str(),
875        "TF" | "TRANSFORM FACE" | "TRANSFORMFACE"
876    )
877}
878
879/// Apply translation or world-axis rotation to the grab pose, retaining scale.
880fn apply_transform_delta(start: &TransformPose, delta: &TransformDelta) -> TransformPose {
881    match delta {
882        TransformDelta::Translate(world) => TransformPose {
883            position: [
884                start.position[0] + world[0],
885                start.position[1] + world[1],
886                start.position[2] + world[2],
887            ],
888            ..*start
889        },
890        TransformDelta::Rotate { axis, radians } => {
891            let q0 = quat_from_euler_xyz_deg(start.rotation_deg);
892            let dq = quat_from_axis_angle(*axis, *radians);
893            let nq = quat_mul(dq, q0);
894            TransformPose {
895                rotation_deg: euler_xyz_deg_from_quat(nq),
896                ..*start
897            }
898        }
899    }
900}
901
902/// Read a `[x, y, z]` from a transform sub-field (numbers only; missing / short
903/// arrays keep the per-index default).
904fn read_pose_vec3(
905    env: Option<&brep_kernel::Env>,
906    transform: Option<&serde_json::Value>,
907    key: &str,
908    default: [f64; 3],
909) -> [f64; 3] {
910    let array = transform
911        .and_then(|t| t.get(key))
912        .and_then(|v| v.as_array());
913    let mut out = default;
914    if let Some(array) = array {
915        for (index, slot) in out.iter_mut().enumerate() {
916            match array.get(index) {
917                Some(serde_json::Value::Number(number)) => {
918                    if let Some(number) = number.as_f64() {
919                        *slot = number;
920                    }
921                }
922                // A component may be an EXPRESSION (`"boxW/2"`) — the kernel
923                // evaluates it when it builds, so the gizmo must arm at the same
924                // place. Reading it as 0 parked the handles at the world origin
925                // while the solid sat elsewhere, and the first drag wrote that
926                // wrong origin back as a literal, teleporting the solid.
927                Some(serde_json::Value::String(source)) => {
928                    if let Some(number) = env
929                        .and_then(|env| env.eval(source).ok())
930                        .filter(|number| number.is_finite())
931                    {
932                        *slot = number;
933                    }
934                }
935                _ => {}
936            }
937        }
938    }
939    out
940}
941
942/// The gizmo frame feed (`set_transform_json` shape) for a pose: origin =
943/// `position`, axes = the feature's rotated basis (intrinsic XYZ Euler order, matching
944/// the kernel bake), with the center free-move handle shown.
945fn transform_frame_json(pose: &TransformPose) -> String {
946    let euler = [
947        pose.rotation_deg[0].to_radians(),
948        pose.rotation_deg[1].to_radians(),
949        pose.rotation_deg[2].to_radians(),
950    ];
951    let x = normalize3(rotate_euler_xyz_f64([1.0, 0.0, 0.0], euler));
952    let y = normalize3(rotate_euler_xyz_f64([0.0, 1.0, 0.0], euler));
953    let z = normalize3(rotate_euler_xyz_f64([0.0, 0.0, 1.0], euler));
954    serde_json::json!({
955        "origin": pose.position,
956        "x": x,
957        "y": y,
958        "z": z,
959        "showCenter": true,
960    })
961    .to_string()
962}
963
964pub(super) fn normalize3(v: [f64; 3]) -> [f64; 3] {
965    let length = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt();
966    if length < 1e-12 {
967        [0.0, 0.0, 1.0]
968    } else {
969        [v[0] / length, v[1] / length, v[2] / length]
970    }
971}
972
973/// Apply an intrinsic XYZ Euler (radians) to a vector — the EXACT matrix the kernel
974/// bake (`transform_bake` / `datum::rotate_euler_xyz`) uses, so the fed gizmo
975/// frame aligns with the baked solid.
976pub(crate) fn rotate_euler_xyz_f64(v: [f64; 3], euler: [f64; 3]) -> [f64; 3] {
977    let (c1, s1) = (euler[0].cos(), euler[0].sin());
978    let (c2, s2) = (euler[1].cos(), euler[1].sin());
979    let (c3, s3) = (euler[2].cos(), euler[2].sin());
980    let m00 = c2 * c3;
981    let m01 = -c2 * s3;
982    let m02 = s2;
983    let m10 = c1 * s3 + c3 * s1 * s2;
984    let m11 = c1 * c3 - s1 * s2 * s3;
985    let m12 = -c2 * s1;
986    let m20 = s1 * s3 - c1 * c3 * s2;
987    let m21 = c3 * s1 + c1 * s2 * s3;
988    let m22 = c1 * c2;
989    [
990        m00 * v[0] + m01 * v[1] + m02 * v[2],
991        m10 * v[0] + m11 * v[1] + m12 * v[2],
992        m20 * v[0] + m21 * v[1] + m22 * v[2],
993    ]
994}
995
996// --- quaternion helpers (ported from CombinedTransformControls) -----
997
998pub(super) type Quat = [f64; 4]; // [x, y, z, w]
999
1000pub(super) fn quat_from_axis_angle(axis: [f64; 3], angle: f64) -> Quat {
1001    let n = normalize3(axis);
1002    let half = angle * 0.5;
1003    let s = half.sin();
1004    [n[0] * s, n[1] * s, n[2] * s, half.cos()]
1005}
1006
1007/// Quaternion from an intrinsic XYZ Euler (degrees in).
1008pub(super) fn quat_from_euler_xyz_deg(deg: [f64; 3]) -> Quat {
1009    let (c1, s1) = (
1010        (deg[0].to_radians() * 0.5).cos(),
1011        (deg[0].to_radians() * 0.5).sin(),
1012    );
1013    let (c2, s2) = (
1014        (deg[1].to_radians() * 0.5).cos(),
1015        (deg[1].to_radians() * 0.5).sin(),
1016    );
1017    let (c3, s3) = (
1018        (deg[2].to_radians() * 0.5).cos(),
1019        (deg[2].to_radians() * 0.5).sin(),
1020    );
1021    [
1022        s1 * c2 * c3 + c1 * s2 * s3,
1023        c1 * s2 * c3 - s1 * c2 * s3,
1024        c1 * c2 * s3 + s1 * s2 * c3,
1025        c1 * c2 * c3 - s1 * s2 * s3,
1026    ]
1027}
1028
1029/// Quaternion product `a * b`.
1030pub(super) fn quat_mul(a: Quat, b: Quat) -> Quat {
1031    [
1032        a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
1033        a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0],
1034        a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3],
1035        a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2],
1036    ]
1037}
1038
1039/// Intrinsic XYZ Euler from a quaternion (via the rotation matrix) → the
1040/// 'XYZ' Euler in DEGREES. Uses the SAME matrix element naming as
1041/// `rotate_euler_xyz_f64`, so the round-trip is consistent with the kernel bake.
1042pub(super) fn euler_xyz_deg_from_quat(q: Quat) -> [f64; 3] {
1043    let [x, y, z, w] = q;
1044    let (x2, y2, z2) = (x + x, y + y, z + z);
1045    let (xx, xy, xz) = (x * x2, x * y2, x * z2);
1046    let (yy, yz, zz) = (y * y2, y * z2, z * z2);
1047    let (wx, wy, wz) = (w * x2, w * y2, w * z2);
1048    // Rotation matrix elements (m<row><col> naming).
1049    let m11 = 1.0 - (yy + zz);
1050    let m12 = xy - wz;
1051    let m13 = xz + wy;
1052    let m22 = 1.0 - (xx + zz);
1053    let m23 = yz - wx;
1054    let m32 = yz + wx;
1055    let m33 = 1.0 - (xx + yy);
1056    let ey = m13.clamp(-1.0, 1.0).asin();
1057    let (ex, ez) = if m13.abs() < 0.9999999 {
1058        ((-m23).atan2(m33), (-m12).atan2(m11))
1059    } else {
1060        (m32.atan2(m22), 0.0)
1061    };
1062    [ex.to_degrees(), ey.to_degrees(), ez.to_degrees()]
1063}
1064
1065// ===========================================================================
1066// The gizmo on a CONNECTION POINT
1067// ===========================================================================
1068//
1069// A connection point is not a feature: it has no history row and no id, so
1070// every read and write above that goes through `history.index_of` misses it.
1071// What it has instead is a SEAT — the frame its declared `transform` is an
1072// offset in (`brep_kernel::ports`) — and the gizmo lane is exactly that seat
1073// applied and un-applied:
1074//
1075//   pose  = seat  o  declaration      (what the handles are drawn at)
1076//   write = seat⁻¹ o  dragged pose    (what the block is given back)
1077//
1078// The seat comes from the LAST APPLIED RUN's report and the declaration from
1079// the block as it stands NOW. That split is deliberate and it is what keeps a
1080// drag smooth: `set_ports_block` re-runs on the NEXT frame, so a pose read
1081// from the run's resolved position would lag the pointer by a frame and
1082// oscillate. The seat does not move while the offset is dragged — only the
1083// reference geometry moves it — so reading it from the last run is exact.
1084
1085impl EngineState {
1086    /// Whether the transform gizmo is armed on a connection point, and which.
1087    pub fn transform_armed_port_point(&self) -> Option<&str> {
1088        self.transform_armed()
1089            .then(|| self.transform_gizmo.port.as_deref())
1090            .flatten()
1091    }
1092
1093    /// Arm the TRANSFORM gizmo on the connection point at `address`, or refuse
1094    /// and say why ([`Self::port_point_gizmo_refusal`]).
1095    pub fn arm_transform_for_port_point(&mut self, address: &str) -> bool {
1096        if let Some(reason) = self.port_point_gizmo_refusal(address) {
1097            self.push_notice(format!("no handles on {address}: {reason}"));
1098            return false;
1099        }
1100        self.component_move_reset();
1101        self.transform_gizmo.feature_id = None;
1102        self.transform_gizmo.anchor = None;
1103        self.transform_gizmo.port = Some(address.to_string());
1104        self.transform_gizmo.mode = GizmoMode::Transform;
1105        self.transform_gizmo.drag = None;
1106        self.clear_feature_dimension_overlay();
1107        self.sync_transform_gizmo();
1108        true
1109    }
1110
1111    /// Why the gizmo will not arm on this point, in words, or `None` when it
1112    /// will. There are two reasons, and both are cases where the handles would
1113    /// LIE — draw somewhere a drag cannot write to:
1114    ///
1115    /// * the point **maps down**. Its placement is its target's: the kernel's
1116    ///   `resolve_point` returns on `mapsTo` before it ever reads the
1117    ///   transform, so handles would sit on the descendant's geometry and a
1118    ///   drag would write numbers nothing reads, leaving the point where it
1119    ///   was while the handles walked away from it;
1120    /// * an **expression** places it. The gizmo writes numbers, and replacing
1121    ///   `width/2` with `12.5` because a user dragged something is a loss the
1122    ///   document cannot show them. Typing in the panel still works, which is
1123    ///   where an expression belongs.
1124    ///
1125    /// Public because the panel must say the same thing the arm decides rather
1126    /// than inferring a reason from the gizmo not being armed: it is drawn a
1127    /// moment BEFORE the arm runs, so an inference would flash a refusal on
1128    /// the frame a perfectly armable point is selected.
1129    pub fn port_point_gizmo_refusal(&self, address: &str) -> Option<String> {
1130        let point = self.declared_point_value(address)?;
1131        if let Some(target) = point
1132            .get("mapsTo")
1133            .and_then(serde_json::Value::as_str)
1134            .map(str::trim)
1135            .filter(|target| !target.is_empty())
1136        {
1137            return Some(format!(
1138                "it maps to '{target}', and that point's placement is this one's"
1139            ));
1140        }
1141        let transform = point.get("transform")?;
1142        for key in ["position", "rotationEuler"] {
1143            let Some(components) = transform.get(key).and_then(serde_json::Value::as_array) else {
1144                continue;
1145            };
1146            for component in components {
1147                if let Some(expression) = component.as_str() {
1148                    return Some(format!(
1149                        "'{expression}' places it, and a drag writes numbers"
1150                    ));
1151                }
1152            }
1153        }
1154        None
1155    }
1156
1157    /// The seat a connection point's placement is an offset in: the last
1158    /// applied run's, or the WORLD seat for a point no run has resolved yet
1159    /// (one just added, which must still be draggable).
1160    fn port_point_seat(&self, address: &str) -> brep_kernel::Frame {
1161        self.port_point_row(address)
1162            .map(|row| row.seat.to_frame())
1163            .unwrap_or_else(brep_kernel::world_seat)
1164    }
1165
1166    /// The armed connection point's pose in WORLD space — the seat applied to
1167    /// the declaration, per this section's opening note.
1168    pub(super) fn port_point_pose(&self, address: &str) -> Option<TransformPose> {
1169        let point = self.declared_point_value(address)?;
1170        let transform = point.get("transform");
1171        let local_position = read_pose_vec3(None, transform, "position", [0.0; 3]);
1172        let local_rotation = read_pose_vec3(None, transform, "rotationEuler", [0.0; 3]);
1173        let seat = self.port_point_seat(address);
1174        let world_axes = super::spline_edit::axes_from_euler_deg(local_rotation)
1175            .map(|axis| seat_axis(&seat, axis));
1176        Some(TransformPose {
1177            position: array3(brep_kernel::seat_point(&seat, vec3(local_position))),
1178            rotation_deg: super::spline_edit::euler_xyz_deg_from_axes(
1179                world_axes[0],
1180                world_axes[1],
1181                world_axes[2],
1182            ),
1183            scale: [1.0, 1.0, 1.0],
1184        })
1185    }
1186
1187    /// Write a dragged WORLD pose back as the point's offset in its seat.
1188    /// Coalesced per address, so a whole drag is one undo step.
1189    pub(super) fn write_port_point_pose(&mut self, address: &str, pose: &TransformPose) {
1190        let seat = self.port_point_seat(address);
1191        let position = array3(brep_kernel::unseat_point(&seat, vec3(pose.position)));
1192        let local_axes = super::spline_edit::axes_from_euler_deg(pose.rotation_deg)
1193            .map(|axis| unseat_axis(&seat, axis));
1194        let rotation = super::spline_edit::euler_xyz_deg_from_axes(
1195            local_axes[0],
1196            local_axes[1],
1197            local_axes[2],
1198        );
1199        let key = format!("port-point-pose:{address}");
1200        self.edit_port_point(address, Some(&key), |point| {
1201            point.insert(
1202                "transform".into(),
1203                serde_json::json!({ "position": position, "rotationEuler": rotation }),
1204            );
1205        });
1206    }
1207}
1208
1209fn vec3(a: [f64; 3]) -> brep_kernel::Vec3 {
1210    brep_kernel::Vec3::new(a[0], a[1], a[2])
1211}
1212
1213fn array3(v: brep_kernel::Vec3) -> [f64; 3] {
1214    [v.x, v.y, v.z]
1215}
1216
1217/// A local axis carried out through the seat's basis.
1218fn seat_axis(seat: &brep_kernel::Frame, axis: [f64; 3]) -> [f64; 3] {
1219    array3(brep_kernel::seat_vector(seat, vec3(axis)))
1220}
1221
1222/// The same axis read back in the seat's basis.
1223fn unseat_axis(seat: &brep_kernel::Frame, axis: [f64; 3]) -> [f64; 3] {
1224    array3(brep_kernel::unseat_vector(seat, vec3(axis)))
1225}
1226