brep_render/engine_state/plane_pick.rs
1use super::*;
2
3// ===========================================================================
4// Construction PLANES as ordinary pick candidates.
5//
6// A construction plane / DATUM base plane is drawn by the datum WIDGET, not by
7// the scene, so `pick::pick` — which walks the render scene's solids — can never
8// see one. Before this module a plane was reachable only through the independent
9// `datum_pick` widget path, and ONLY when the geometry pick missed: any face,
10// edge or vertex under the pointer won outright and the plane was unreachable
11// (the reported "I can't select a plane if there is any other geometry under the
12// pointer"). Here the plane cards are turned into ordinary
13// [`pick::PickCandidate`]s and merged into the SAME ranked list every selection
14// path consumes, so a plane competes for a pick like any other entity and shows
15// up in the pick-list popup like any face.
16//
17// HOW A PLANE COMPETES — by KIND, not by depth. The candidate list is sorted
18// category-major in the pick-list order
19//
20// VERTEX > EDGE > FACE > PLANE > SOLID > COMPONENT
21//
22// (the `PickKind` discriminant order), nearest-first within a category. So a
23// plane ranks straight after faces: any face under the cursor still out-priorities
24// it, and the plane cannot swallow a click meant for the model — while it is
25// nonetheless IN the list, one row down, reachable through anything. Pure depth
26// ordering was rejected precisely because it trades one complaint for its mirror
27// image: a plane card in front of the model would win every click near it, and a
28// plane behind the model would still lose at a grazing angle.
29//
30// WHAT COUNTS AS "ON" A PLANE — the drawn rectangle. A construction plane is
31// mathematically infinite, but only the card the renderer draws is pickable
32// ([`crate::widgets::WidgetRegistry::datum_plane_hits`] → `DatumPlane::hit_point`,
33// the same live-camera `half()` extent `build_main_overlay` draws with — never a
34// bake, so the pickable region tracks the drawn one across a zoom). Either face
35// of the card picks: a plane seen from behind is still a legitimate selection.
36//
37// DATUMS RIDE ALONG. `refresh_construction_datums` feeds D (datum) and P (plane)
38// features through the one `{planes:[…]}` channel — a DATUM contributes its three
39// base planes (`{id}:XY|XZ|YZ`), a PLANE its single frame (`{id}`) — so both are
40// `PickKind::Plane` candidates with no extra machinery. Datum AXES are not fed by
41// that path and stay out of the pick list.
42// ===========================================================================
43
44impl EngineState {
45 /// EVERY pick candidate under CSS-pixel `(x, y)`: the scene's
46 /// vertices/edges/faces/solids ([`pick::pick`]) PLUS the construction PLANE
47 /// cards the pointer ray crosses, ranked together category-major
48 /// (VERTEX > EDGE > FACE > PLANE > SOLID > COMPONENT), nearest first within a
49 /// category.
50 ///
51 /// This is the RAW list (no selection filter); the filter-honoring callers are
52 /// [`Self::candidates_filtered_at`] (the pick-list popup) and
53 /// [`Self::pick_top_at`] (every single-hit pick).
54 pub fn pick_candidates_at(&self, x: f64, y: f64) -> Vec<pick::PickCandidate> {
55 let mut out = pick::pick(&self.scene, &self.camera, x, y, &self.pick_options());
56 // Appended AFTER `pick`'s own `MAX_CANDIDATES` truncation, so a plane is
57 // never dropped by a crowd of faces under the same pixel.
58 out.extend(self.plane_candidates_at(x, y));
59 sort_pick_candidates(&mut out);
60 out
61 }
62
63 /// The construction PLANE candidates under CSS-pixel `(x, y)` — one per DRAWN
64 /// datum-plane card the pointer ray crosses, carrying the datum FRAME name
65 /// (`Pl`, `Datum:XY`) and the world hit point. `solid` is empty (a plane has no
66 /// owning solid) and `screen_dist` is 0 (a card hit is exact, like a face hit).
67 ///
68 /// Hidden planes are absent by construction: the widget feed
69 /// ([`Self::refresh_construction_datums`]) only carries the planes that are
70 /// actually drawn, so a Scene-tree-hidden or rolled-back plane cannot be picked.
71 pub(super) fn plane_candidates_at(&self, x: f64, y: f64) -> Vec<pick::PickCandidate> {
72 let cam = gizmo_camera(&self.camera);
73 let (_, _, forward) = self.camera.basis();
74 self.widgets
75 .datum_plane_hits(&cam, x as f32, y as f32)
76 .into_iter()
77 .map(|(name, point)| {
78 let position = [point[0] as f64, point[1] as f64, point[2] as f64];
79 pick::PickCandidate {
80 kind: pick::PickKind::Plane,
81 name,
82 solid: String::new(),
83 depth: crate::view::dot3(
84 crate::view::sub3(position, self.camera.eye),
85 forward,
86 ),
87 screen_dist: 0.0,
88 position,
89 }
90 })
91 .collect()
92 }
93
94 /// The TOP-ranked candidate under `(x, y)` whose kind `kinds` admits — the
95 /// planes-aware replacement for [`pick::pick_filtered`] (which only sees the
96 /// render scene). An EMPTY `kinds` means any kind, matching `pick_filtered`.
97 ///
98 /// `DATUM` is accepted as an alias of `PLANE` so a reference field whose schema
99 /// spells its construction kind `["DATUM"]` picks the same plane cards as
100 /// `["PLANE"]` (`ref_select_click` passes the field's RAW strings here).
101 pub(super) fn pick_top_at(
102 &self,
103 x: f64,
104 y: f64,
105 kinds: &[String],
106 ) -> Option<pick::PickCandidate> {
107 self.pick_candidates_at(x, y)
108 .into_iter()
109 // EMPTY = any kind, [`pick::pick_filtered`]'s rule (the callers that
110 // mean "select nothing" guard on an empty list BEFORE calling).
111 .find(|c| kinds.is_empty() || self.candidate_admitted(kinds, c))
112 }
113
114 /// Does `kinds` admit this CANDIDATE? [`kind_admitted`] plus the one
115 /// distinction the picker cannot make on its own: a committed SKETCH is drawn
116 /// as a synthesized sheet solid, so it and a real body arrive with the same
117 /// [`pick::PickKind::Solid`]. `SolidDisplay::is_sketch` splits them, and the
118 /// candidate then answers to `"SKETCH"` or to `"SOLID"` — never to both.
119 ///
120 /// Only the whole-object candidate is reclassified. A sketch's planar FACE and
121 /// its drawn EDGES keep their own kinds, which is what the profile fields
122 /// (`["SKETCH","FACE"]`, picking a sketch through its sheet face) and the path
123 /// fields (`["EDGE"]` on path sweep, picking a sketch curve) rely on.
124 pub(super) fn candidate_admitted(
125 &self,
126 kinds: &[String],
127 candidate: &pick::PickCandidate,
128 ) -> bool {
129 if candidate.kind == pick::PickKind::Solid && self.candidate_is_sketch(candidate) {
130 return kinds.iter().any(|k| k.eq_ignore_ascii_case("SKETCH"));
131 }
132 kind_admitted(kinds, candidate.kind)
133 }
134
135 /// The kind LABEL a candidate should present under — `PickKind::as_str`,
136 /// except that a committed-sketch sheet reads `"SKETCH"` rather than the
137 /// `"SOLID"` its `PickKind` carries. The pick-list popup and the published
138 /// candidate JSON both use it, so what a row calls itself matches the filter
139 /// lane that admitted it.
140 pub fn candidate_kind_label(&self, candidate: &pick::PickCandidate) -> &'static str {
141 if candidate.kind == pick::PickKind::Solid && self.candidate_is_sketch(candidate) {
142 return "SKETCH";
143 }
144 candidate.kind.as_str()
145 }
146
147 /// Whether a candidate's owning scene solid is a committed-sketch SHEET.
148 pub(super) fn candidate_is_sketch(&self, candidate: &pick::PickCandidate) -> bool {
149 let name = if candidate.solid.is_empty() {
150 candidate.name.as_str()
151 } else {
152 candidate.solid.as_str()
153 };
154 self.scene
155 .solid(name)
156 .is_some_and(|solid| solid.is_sketch)
157 }
158}
159
160/// Does `kinds` LIST `kind`? Case-insensitive, with `DATUM` as an alias of
161/// `PLANE`. Kind-only, so it cannot tell a committed-sketch sheet from a real
162/// body — [`EngineState::candidate_admitted`] is the candidate-aware wrapper every
163/// picking path actually calls. STRICT: an empty list admits nothing (the "empty = any kind"
164/// convenience belongs to the single-hit [`EngineState::pick_top_at`] alone — the
165/// pick-LIST builder passes an empty list to mean "no sub-entity kind is
166/// admitted", e.g. a COMPONENT-only filter).
167pub(super) fn kind_admitted(kinds: &[String], kind: pick::PickKind) -> bool {
168 let name = kind.as_str();
169 let datum_alias = kind == pick::PickKind::Plane;
170 kinds
171 .iter()
172 .any(|k| k.eq_ignore_ascii_case(name) || (datum_alias && k.eq_ignore_ascii_case("DATUM")))
173}
174
175/// The ONE candidate ordering every pick path shares: category-major in the
176/// pick-list order (`PickKind`'s discriminant order IS that order —
177/// VERTEX > EDGE > FACE > PLANE > SOLID > COMPONENT), nearest (smallest depth)
178/// first within a category, then by screen distance. Stable, so equal-depth
179/// entries keep the ray ranker's order.
180pub(super) fn sort_pick_candidates(candidates: &mut [pick::PickCandidate]) {
181 candidates.sort_by(|a, b| {
182 (a.kind as u8)
183 .cmp(&(b.kind as u8))
184 .then(a.depth.total_cmp(&b.depth))
185 .then(a.screen_dist.total_cmp(&b.screen_dist))
186 });
187}
188
189// BREP private tests: 20410a60ba5f25fb