brep_kernel/feature_pipeline/pmi/
resolve.rs1use crate::feature_pipeline::SceneMap;
18use crate::{
19 resolve_edge_selection, resolve_face_selection, resolve_vertex_selection, SelectionGeometry,
20 Vec3,
21};
22
23
24pub fn v3(a: [f64; 3]) -> Vec3 {
25 Vec3::new(a[0], a[1], a[2])
26}
27
28pub fn a3(v: Vec3) -> [f64; 3] {
29 [v.x, v.y, v.z]
30}
31
32fn parse_triple(text: &str) -> Option<Vec3> {
34 let mut parts = text.split(',').map(|part| part.trim().parse::<f64>());
35 let x = parts.next()?.ok()?;
36 let y = parts.next()?.ok()?;
37 let z = parts.next()?.ok()?;
38 if parts.next().is_some() {
39 return None;
40 }
41 Some(Vec3::new(x, y, z))
42}
43
44pub fn resolve_reference(scene: &SceneMap, name: &str) -> Result<SelectionGeometry, String> {
46 let name = name.trim();
47 if name.is_empty() {
48 return Err("empty reference".into());
49 }
50 if let Some((solid_name, coords)) = name.split_once('@') {
52 let handle = scene
53 .resolve_solid(solid_name)
54 .ok_or_else(|| format!("vertex ref '{name}': unknown solid '{solid_name}'"))?;
55 let position = parse_triple(coords)
56 .ok_or_else(|| format!("vertex ref '{name}': position must be 'x,y,z' numbers"))?;
57 return crate::with_registered_solid_str(handle, |solid| {
58 Ok(resolve_vertex_selection(solid, position))
59 })?
60 .map_err(|error| format!("vertex ref '{name}': {error}"));
61 }
62 if let Some(face) = scene.resolve_face(name) {
63 return crate::with_registered_solid_str(face.handle, |solid| {
64 Ok(resolve_face_selection(solid, face.face_id))
65 })?
66 .map_err(|error| format!("face '{name}': {error}"));
67 }
68 if let Some(edge) = scene.resolve_edge(name) {
69 return crate::with_registered_solid_str(edge.handle, |solid| {
70 Ok(resolve_edge_selection(solid, edge.edge_id))
71 })?
72 .map_err(|error| format!("edge '{name}': {error}"));
73 }
74 if let Some(frame) = scene.resolve_frame(name) {
75 return Ok(SelectionGeometry::Plane {
76 origin: frame.origin,
77 normal: frame.z_axis,
78 });
79 }
80 if let Some(handle) = scene.resolve_solid(name) {
81 let center = solid_bbox_center(handle)?;
82 return Ok(SelectionGeometry::Point { position: center });
83 }
84 Err(format!("'{name}' is not in the model"))
85}
86
87pub fn edge_endpoints(scene: &SceneMap, name: &str) -> Result<(Vec3, Vec3), String> {
90 let edge = scene
91 .resolve_edge(name)
92 .ok_or_else(|| format!("'{name}' is not an edge"))?;
93 crate::with_registered_solid_str(edge.handle, |solid| {
94 let record = solid
95 .edges
96 .iter()
97 .find(|record| record.id == edge.edge_id)
98 .ok_or_else(|| format!("edge '{name}' has no record"))?;
99 let start = record.curve.evaluate(record.t0)?;
100 let end = record.curve.evaluate(record.t1)?;
101 Ok((start, end))
102 })
103}
104
105pub fn is_straight_edge(geometry: &SelectionGeometry) -> bool {
107 matches!(geometry, SelectionGeometry::Line { .. })
108}
109
110pub fn solid_bbox_center(handle: u32) -> Result<Vec3, String> {
113 let (low, high) = solid_bbox(handle)?;
114 Ok(Vec3::new(
115 (low.x + high.x) * 0.5,
116 (low.y + high.y) * 0.5,
117 (low.z + high.z) * 0.5,
118 ))
119}
120
121pub fn solid_bbox(handle: u32) -> Result<(Vec3, Vec3), String> {
123 crate::with_registered_solid_str(handle, |solid| {
124 let mut low = Vec3::new(f64::INFINITY, f64::INFINITY, f64::INFINITY);
125 let mut high = Vec3::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
126 let mut any = false;
127 let mut include = |point: Vec3| {
128 low = Vec3::new(low.x.min(point.x), low.y.min(point.y), low.z.min(point.z));
129 high = Vec3::new(high.x.max(point.x), high.y.max(point.y), high.z.max(point.z));
130 any = true;
131 };
132 for vertex in &solid.vertices {
133 include(vertex.point);
134 }
135 for shell in &solid.shells {
136 for face in &shell.faces {
137 for row in &face.surface.control_points {
138 for control in row {
139 include(control.point()?);
140 }
141 }
142 }
143 }
144 if !any {
145 return Err("solid has no geometry".into());
146 }
147 Ok((low, high))
148 })
149}
150
151pub fn direction_of(geometry: &SelectionGeometry) -> Option<Vec3> {
154 match geometry {
155 SelectionGeometry::Plane { normal, .. } => Some(*normal),
156 SelectionGeometry::Line { direction, .. } | SelectionGeometry::Axis { direction, .. } => {
157 Some(*direction)
158 }
159 SelectionGeometry::Circle { axis, .. } => Some(*axis),
160 SelectionGeometry::Sphere { .. } | SelectionGeometry::Point { .. } => None,
161 }
162}
163
164pub fn class_of(geometry: &SelectionGeometry) -> &'static str {
166 match geometry {
167 SelectionGeometry::Plane { .. } => "plane",
168 SelectionGeometry::Axis { .. } => "axis",
169 SelectionGeometry::Sphere { .. } => "sphere",
170 SelectionGeometry::Circle { .. } => "circle",
171 SelectionGeometry::Line { .. } => "line",
172 SelectionGeometry::Point { .. } => "point",
173 }
174}
175
176pub enum LinearSide {
181 Plane { origin: Vec3, normal: Vec3 },
182 Line { origin: Vec3, direction: Vec3 },
183 Point(Vec3),
184}
185
186pub fn classify_linear(geometry: &SelectionGeometry) -> LinearSide {
187 match *geometry {
188 SelectionGeometry::Plane { origin, normal } => LinearSide::Plane { origin, normal },
189 SelectionGeometry::Line { origin, direction }
190 | SelectionGeometry::Axis {
191 origin, direction, ..
192 } => LinearSide::Line { origin, direction },
193 SelectionGeometry::Circle { center, .. } | SelectionGeometry::Sphere { center, .. } => {
194 LinearSide::Point(center)
195 }
196 SelectionGeometry::Point { position } => LinearSide::Point(position),
197 }
198}
199
200fn unit(v: Vec3) -> Result<Vec3, String> {
201 v.normalized().map_err(|_| "degenerate direction".to_string())
202}
203
204fn foot_on_line(point: Vec3, origin: Vec3, direction: Vec3) -> Vec3 {
206 let d = direction;
207 let t = point.sub(origin).dot(d) / d.dot(d).max(1e-300);
208 origin.add(d.scale(t))
209}
210
211fn foot_on_plane(point: Vec3, origin: Vec3, normal: Vec3) -> Vec3 {
213 let n = normal;
214 let h = point.sub(origin).dot(n) / n.dot(n).max(1e-300);
215 point.sub(n.scale(h))
216}
217
218pub fn linear_between(first: &SelectionGeometry, second: &SelectionGeometry) -> Result<(Vec3, Vec3), String> {
223 use LinearSide::*;
224 match (classify_linear(first), classify_linear(second)) {
225 (Point(p), Point(q)) => Ok((p, q)),
226 (Point(p), Line { origin, direction }) => Ok((p, foot_on_line(p, origin, direction))),
227 (Line { origin, direction }, Point(q)) => Ok((foot_on_line(q, origin, direction), q)),
228 (Point(p), Plane { origin, normal }) => Ok((p, foot_on_plane(p, origin, normal))),
229 (Plane { origin, normal }, Point(q)) => Ok((foot_on_plane(q, origin, normal), q)),
230 (
231 Line {
232 origin: oa,
233 direction: da,
234 },
235 Line {
236 origin: ob,
237 direction: db,
238 },
239 ) => {
240 let da = unit(da)?;
241 let db = unit(db)?;
242 let cross = da.cross(db);
243 if cross.length() < 1e-7 {
244 return Ok((oa, foot_on_line(oa, ob, db)));
246 }
247 let w = oa.sub(ob);
249 let a = da.dot(da);
250 let b = da.dot(db);
251 let c = db.dot(db);
252 let d = da.dot(w);
253 let e = db.dot(w);
254 let denom = a * c - b * b;
255 let s = (b * e - c * d) / denom;
256 let t = (a * e - b * d) / denom;
257 Ok((oa.add(da.scale(s)), ob.add(db.scale(t))))
258 }
259 (
260 Plane {
261 origin: oa,
262 normal: na,
263 },
264 Plane {
265 origin: ob,
266 normal: nb,
267 },
268 ) => {
269 let na = unit(na)?;
270 let nb = unit(nb)?;
271 if na.cross(nb).length() > 1e-6 {
272 return Err("the two planes are not parallel — a linear dimension needs parallel planes (use an angle dimension)".into());
273 }
274 Ok((oa, foot_on_plane(oa, ob, nb)))
275 }
276 (
277 Plane {
278 origin: po,
279 normal,
280 },
281 Line { origin, direction },
282 ) => {
283 if unit(normal)?.dot(unit(direction)?).abs() > 1e-6 {
284 return Err("the line is not parallel to the plane".into());
285 }
286 Ok((foot_on_plane(origin, po, normal), origin))
287 }
288 (
289 Line { origin, direction },
290 Plane {
291 origin: po,
292 normal,
293 },
294 ) => {
295 if unit(normal)?.dot(unit(direction)?).abs() > 1e-6 {
296 return Err("the line is not parallel to the plane".into());
297 }
298 Ok((origin, foot_on_plane(origin, po, normal)))
299 }
300 }
301}
302
303pub fn perpendicular_in_plane(axis: Vec3, preferred: Vec3) -> Vec3 {
306 let axis = axis.normalized().unwrap_or(Vec3::new(0.0, 0.0, 1.0));
307 let planar = preferred.sub(axis.scale(preferred.dot(axis)));
308 match planar.normalized() {
309 Ok(v) if planar.length() > 1e-9 => v,
310 _ => axis
311 .perpendicular()
312 .unwrap_or(Vec3::new(1.0, 0.0, 0.0)),
313 }
314}