1use ogeom_core::{OgeomResult, Tolerances, ogeom_bail};
35use ogeom_geom::{Curve, SurfaceGeometry};
36use ogeom_math::{Circle, Direction, Ellipse, Frame, Point, Vector};
37
38#[derive(Debug, Clone, PartialEq)]
40pub enum Meeting {
41 Apart,
43 Touching(Vec<Point>),
49 Along(Vec<Curve>),
51 Same,
58}
59
60pub fn surface_surface(
69 a: &SurfaceGeometry,
70 b: &SurfaceGeometry,
71 tol: Tolerances,
72) -> OgeomResult<Meeting> {
73 use SurfaceGeometry as S;
74 match (a, b) {
75 (S::Plane(p), S::Plane(q)) => Ok(plane_plane(p.plane(), q.plane(), tol)),
76 (S::Plane(p), S::Sphere(s)) => Ok(plane_sphere(p.plane(), s.sphere(), tol)),
77 (S::Sphere(s), S::Plane(p)) => Ok(plane_sphere(p.plane(), s.sphere(), tol)),
78 (S::Plane(p), S::Cylinder(c)) => plane_cylinder(p.plane(), c.cylinder(), tol),
79 (S::Cylinder(c), S::Plane(p)) => plane_cylinder(p.plane(), c.cylinder(), tol),
80 (S::Sphere(x), S::Sphere(y)) => Ok(sphere_sphere(x.sphere(), y.sphere(), tol)),
81 (S::Cylinder(x), S::Cylinder(y)) => coaxial_cylinders(x.cylinder(), y.cylinder(), tol),
82 (S::Cylinder(c), S::Sphere(s)) => coaxial_cylinder_sphere(c.cylinder(), s.sphere(), tol),
83 (S::Sphere(s), S::Cylinder(c)) => coaxial_cylinder_sphere(c.cylinder(), s.sphere(), tol),
84 (S::Plane(p), S::Torus(t)) => axial_plane_torus(p.plane(), t.torus(), tol),
85 (S::Torus(t), S::Plane(p)) => axial_plane_torus(p.plane(), t.torus(), tol),
86 (S::Cylinder(c), S::Torus(t)) => coaxial_cylinder_torus(c.cylinder(), t.torus(), tol),
87 (S::Torus(t), S::Cylinder(c)) => coaxial_cylinder_torus(c.cylinder(), t.torus(), tol),
88 (S::Torus(x), S::Torus(y)) => coaxial_tori(x.torus(), y.torus(), tol),
89 (S::Plane(p), S::Cone(c)) => plane_cone(p.plane(), c.cone(), tol),
90 (S::Cone(c), S::Plane(p)) => plane_cone(p.plane(), c.cone(), tol),
91 (S::Cylinder(x), S::Cone(c)) => coaxial_cylinder_cone(x.cylinder(), c.cone(), tol),
92 (S::Cone(c), S::Cylinder(x)) => coaxial_cylinder_cone(x.cylinder(), c.cone(), tol),
93 (S::Cone(x), S::Cone(y)) => coaxial_cones(x.cone(), y.cone(), tol),
94 _ => ogeom_bail!(
95 NotDone,
96 "this pair of surfaces has no closed-form intersection; it needs \
97 the general marching intersector, which is gated on the benchmark \
98 these cases provide the ground truth for"
99 ),
100 }
101}
102
103fn plane_plane(a: ogeom_math::Plane, b: ogeom_math::Plane, tol: Tolerances) -> Meeting {
105 let along = a.normal().dot(b.normal());
106 if (along.abs() - 1.0).abs() <= tol.angular() {
107 return if a.distance_to(b.origin()) <= tol.confusion() {
110 Meeting::Same
111 } else {
112 Meeting::Apart
113 };
114 }
115 let Ok(direction) = Direction::from_cross(a.normal().vector(), b.normal().vector(), tol) else {
118 return Meeting::Apart;
119 };
120 let (da, db) = (
121 a.normal().dot_vector(a.origin().to_vector()),
122 b.normal().dot_vector(b.origin().to_vector()),
123 );
124 let (na, nb) = (a.normal().vector(), b.normal().vector());
125 let dot = na.dot(nb);
126 let denominator = dot.mul_add(-dot, 1.0);
127 if denominator.abs() <= tol.angular() {
128 return Meeting::Apart;
129 }
130 let ca = da.mul_add(1.0, -(db * dot)) / denominator;
131 let cb = db.mul_add(1.0, -(da * dot)) / denominator;
132 let through = Point::from_vector(na * ca + nb * cb);
133 Meeting::Along(vec![line_through(through, direction)])
134}
135
136fn plane_sphere(plane: ogeom_math::Plane, sphere: ogeom_math::Sphere, tol: Tolerances) -> Meeting {
138 let gap = plane.signed_distance_to(sphere.centre());
139 let reach = gap.abs();
140 if reach > sphere.radius() + tol.confusion() {
141 return Meeting::Apart;
142 }
143 let foot = plane.project(sphere.centre());
144 if (reach - sphere.radius()).abs() <= tol.confusion() {
145 return Meeting::Touching(vec![foot]);
146 }
147 let radius = sphere
150 .radius()
151 .mul_add(sphere.radius(), -(gap * gap))
152 .max(0.0)
153 .sqrt();
154 match circle_on(foot, plane.normal(), radius, tol) {
155 Some(circle) => Meeting::Along(vec![circle]),
156 None => Meeting::Touching(vec![foot]),
157 }
158}
159
160fn plane_cylinder(
166 plane: ogeom_math::Plane,
167 cylinder: ogeom_math::Cylinder,
168 tol: Tolerances,
169) -> OgeomResult<Meeting> {
170 let axis = cylinder.axis();
171 let along = plane.normal().dot(axis.direction);
172
173 if along.abs() <= tol.angular() {
176 let gap = plane.signed_distance_to(axis.location);
177 let reach = gap.abs();
178 if reach > cylinder.radius() + tol.confusion() {
179 return Ok(Meeting::Apart);
180 }
181 let offset = cylinder
183 .radius()
184 .mul_add(cylinder.radius(), -(gap * gap))
185 .max(0.0)
186 .sqrt();
187 let foot = plane.project(axis.location);
188 let sideways =
189 Direction::from_cross(plane.normal().vector(), axis.direction.vector(), tol)?;
190 if offset <= tol.confusion() {
191 return Ok(Meeting::Along(vec![line_through(foot, axis.direction)]));
193 }
194 return Ok(Meeting::Along(vec![
195 line_through(foot + sideways.vector() * offset, axis.direction),
196 line_through(foot - sideways.vector() * offset, axis.direction),
197 ]));
198 }
199
200 let centre = intersect_axis_plane(axis, plane, tol)?;
202 if (along.abs() - 1.0).abs() <= tol.angular() {
203 return Ok(
204 match circle_on(centre, plane.normal(), cylinder.radius(), tol) {
205 Some(circle) => Meeting::Along(vec![circle]),
206 None => Meeting::Apart,
207 },
208 );
209 }
210
211 let minor = cylinder.radius();
214 let major = minor / along.abs();
215 let minor_direction =
218 Direction::from_cross(plane.normal().vector(), axis.direction.vector(), tol)?;
219 let major_direction =
220 Direction::from_cross(minor_direction.vector(), plane.normal().vector(), tol)?;
221 let frame = Frame::from_axes(
222 centre,
223 major_direction,
224 minor_direction,
225 plane.normal(),
226 tol,
227 )?;
228 Ok(Meeting::Along(vec![
229 ogeom_geom::EllipseCurve::new(Ellipse::new(frame, major, minor, tol)?).into(),
230 ]))
231}
232
233fn sphere_sphere(a: ogeom_math::Sphere, b: ogeom_math::Sphere, tol: Tolerances) -> Meeting {
235 let between = b.centre() - a.centre();
236 let distance = between.magnitude();
237 if distance <= tol.confusion() {
238 return if (a.radius() - b.radius()).abs() <= tol.confusion() {
239 Meeting::Same
240 } else {
241 Meeting::Apart
243 };
244 }
245 let (ra, rb) = (a.radius(), b.radius());
246 if distance > ra + rb + tol.confusion() || distance < (ra - rb).abs() - tol.confusion() {
247 return Meeting::Apart;
248 }
249 let Ok(direction) = Direction::new(between, tol) else {
250 return Meeting::Apart;
251 };
252 let reach = distance.mul_add(distance, ra.mul_add(ra, -(rb * rb))) / (2.0 * distance);
254 let centre = a.centre() + direction.vector() * reach;
255 let squared = ra.mul_add(ra, -(reach * reach));
256 if squared <= tol.confusion() * tol.confusion() {
257 return Meeting::Touching(vec![centre]);
258 }
259 match circle_on(centre, direction, squared.max(0.0).sqrt(), tol) {
260 Some(circle) => Meeting::Along(vec![circle]),
261 None => Meeting::Touching(vec![centre]),
262 }
263}
264
265fn coaxial_cylinders(
271 a: ogeom_math::Cylinder,
272 b: ogeom_math::Cylinder,
273 tol: Tolerances,
274) -> OgeomResult<Meeting> {
275 if !a.axis().is_coaxial(b.axis(), tol) {
276 if (a.radius() - b.radius()).abs() <= tol.confusion() {
283 let (da, db) = (a.axis().direction.vector(), b.axis().direction.vector());
284 let normal = da.cross(db);
285 if normal.magnitude() > tol.angular() {
286 let (pa, pb) = (a.axis().location, b.axis().location);
287 let w = pb - pa;
290 let dd = da.dot(db);
291 let denom = dd.mul_add(-dd, 1.0);
292 let s = dd.mul_add(-db.dot(w), da.dot(w)) / denom;
293 let t = dd.mul_add(da.dot(w), -db.dot(w)) / denom;
294 let on_a = pa + da * s;
295 let on_b = pb + db * t;
296 if on_a.distance(on_b) <= tol.confusion() {
297 let centre = on_a;
298 let mut curves = Vec::new();
299 for m in [da - db, da + db] {
300 if m.magnitude() <= tol.angular() {
301 continue;
302 }
303 let plane =
304 ogeom_math::Plane::through(centre, ogeom_math::Direction::new(m, tol)?);
305 if let Meeting::Along(mut found) = plane_cylinder(plane, a, tol)? {
306 curves.append(&mut found);
307 }
308 }
309 if !curves.is_empty() {
310 return Ok(Meeting::Along(curves));
311 }
312 }
313 }
314 }
315 ogeom_bail!(
316 NotDone,
317 "two cylinders that do not share an axis meet in a quartic space \
318 curve, which needs the general marching intersector"
319 );
320 }
321 Ok(if (a.radius() - b.radius()).abs() <= tol.confusion() {
322 Meeting::Same
323 } else {
324 Meeting::Apart
326 })
327}
328
329fn coaxial_cylinder_sphere(
331 cylinder: ogeom_math::Cylinder,
332 sphere: ogeom_math::Sphere,
333 tol: Tolerances,
334) -> OgeomResult<Meeting> {
335 let axis = cylinder.axis();
336 if axis.distance_to(sphere.centre()) > tol.confusion() {
337 ogeom_bail!(
338 NotDone,
339 "a sphere off a cylinder's axis meets it in a quartic space curve, \
340 which needs the general marching intersector"
341 );
342 }
343 let (r, radius) = (cylinder.radius(), sphere.radius());
344 if r > radius + tol.confusion() {
345 return Ok(Meeting::Apart);
346 }
347 if (r - radius).abs() <= tol.confusion() {
348 let centre = sphere.centre();
351 return Ok(match circle_on(centre, axis.direction, r, tol) {
352 Some(circle) => Meeting::Along(vec![circle]),
353 None => Meeting::Apart,
354 });
355 }
356 let reach = radius.mul_add(radius, -(r * r)).max(0.0).sqrt();
358 let mut out = Vec::with_capacity(2);
359 for side in [reach, -reach] {
360 let centre = sphere.centre() + axis.direction.vector() * side;
361 if let Some(circle) = circle_on(centre, axis.direction, r, tol) {
362 out.push(circle);
363 }
364 }
365 Ok(if out.is_empty() {
366 Meeting::Apart
367 } else {
368 Meeting::Along(out)
369 })
370}
371
372fn axial_plane_torus(
384 plane: ogeom_math::Plane,
385 torus: ogeom_math::Torus,
386 tol: Tolerances,
387) -> OgeomResult<Meeting> {
388 let axis = torus.axis();
389 let along = plane.normal().dot(axis.direction);
390 if (along.abs() - 1.0).abs() > tol.angular() {
391 ogeom_bail!(
392 NotDone,
393 "a plane oblique or parallel to a torus's axis meets it in a \
394 quartic, which needs the general marching intersector"
395 );
396 }
397 let height = -plane.signed_distance_to(axis.location) * along.signum();
399 let minor = torus.minor_radius();
400 if height.abs() > minor + tol.confusion() {
401 return Ok(Meeting::Apart);
402 }
403 let centre = axis.location + axis.direction.vector() * height;
404 if (height.abs() - minor).abs() <= tol.confusion() {
405 return Ok(
407 match circle_on(centre, axis.direction, torus.major_radius(), tol) {
408 Some(circle) => Meeting::Along(vec![circle]),
409 None => Meeting::Apart,
410 },
411 );
412 }
413 let spread = minor.mul_add(minor, -(height * height)).max(0.0).sqrt();
416 let circles: Vec<Curve> = [torus.major_radius() + spread, torus.major_radius() - spread]
417 .into_iter()
418 .filter_map(|radius| circle_on(centre, axis.direction, radius, tol))
419 .collect();
420 Ok(if circles.is_empty() {
421 Meeting::Apart
422 } else {
423 Meeting::Along(circles)
424 })
425}
426
427fn coaxial_cylinder_torus(
430 cylinder: ogeom_math::Cylinder,
431 torus: ogeom_math::Torus,
432 tol: Tolerances,
433) -> OgeomResult<Meeting> {
434 if !cylinder.axis().is_coaxial(torus.axis(), tol) {
435 ogeom_bail!(
436 NotDone,
437 "a cylinder off a torus's axis meets it in a quartic space curve, \
438 which needs the general marching intersector"
439 );
440 }
441 let axis = torus.axis();
442 let reach = (cylinder.radius() - torus.major_radius()).abs();
443 let minor = torus.minor_radius();
444 if reach > minor + tol.confusion() {
445 return Ok(Meeting::Apart);
446 }
447 if (reach - minor).abs() <= tol.confusion() {
448 return Ok(
450 match circle_on(axis.location, axis.direction, cylinder.radius(), tol) {
451 Some(circle) => Meeting::Along(vec![circle]),
452 None => Meeting::Apart,
453 },
454 );
455 }
456 let rise = minor.mul_add(minor, -(reach * reach)).max(0.0).sqrt();
457 let circles: Vec<Curve> = [rise, -rise]
458 .into_iter()
459 .filter_map(|height| {
460 circle_on(
461 axis.location + axis.direction.vector() * height,
462 axis.direction,
463 cylinder.radius(),
464 tol,
465 )
466 })
467 .collect();
468 Ok(if circles.is_empty() {
469 Meeting::Apart
470 } else {
471 Meeting::Along(circles)
472 })
473}
474
475fn plane_cone(
484 plane: ogeom_math::Plane,
485 cone: ogeom_math::Cone,
486 tol: Tolerances,
487) -> OgeomResult<Meeting> {
488 let axis = cone.axis();
489 let along = plane.normal().dot(axis.direction);
490 if (along.abs() - 1.0).abs() > tol.angular() {
491 ogeom_bail!(
492 NotDone,
493 "a plane oblique to a cone's axis meets it in a conic, which \
494 needs the general marching intersector"
495 );
496 }
497 let height = -plane.signed_distance_to(axis.location) * along.signum();
499 let radius = cone.radius_at(height);
500 if radius.abs() <= tol.confusion() {
501 return Ok(Meeting::Touching(vec![cone.apex()]));
504 }
505 if radius < 0.0 {
506 ogeom_bail!(
510 NotDone,
511 "the plane crosses the cone past its apex, where the chart runs \
512 mirrored; that configuration needs the general machinery"
513 );
514 }
515 let centre = axis.location + axis.direction.vector() * height;
516 Ok(match cone_parallel(&cone, centre, radius, tol) {
517 Some(circle) => Meeting::Along(vec![circle]),
518 None => Meeting::Apart,
519 })
520}
521
522fn coaxial_cylinder_cone(
532 cylinder: ogeom_math::Cylinder,
533 cone: ogeom_math::Cone,
534 tol: Tolerances,
535) -> OgeomResult<Meeting> {
536 if !cylinder.axis().is_coaxial(cone.axis(), tol) {
537 ogeom_bail!(
538 NotDone,
539 "a cylinder off a cone's axis meets it in a curve only the \
540 general marching intersector can trace"
541 );
542 }
543 let axis = cone.axis();
544 let slope = cone.half_angle().tan();
545 let height = (cylinder.radius() - cone.reference_radius()) / slope;
546 Ok(
547 match cone_parallel(
548 &cone,
549 axis.location + axis.direction.vector() * height,
550 cylinder.radius(),
551 tol,
552 ) {
553 Some(circle) => Meeting::Along(vec![circle]),
554 None => Meeting::Apart,
555 },
556 )
557}
558
559fn coaxial_cones(
566 a: ogeom_math::Cone,
567 b: ogeom_math::Cone,
568 tol: Tolerances,
569) -> OgeomResult<Meeting> {
570 if !a.axis().is_coaxial(b.axis(), tol) {
571 ogeom_bail!(
572 NotDone,
573 "two cones that do not share an axis meet in a curve only the \
574 general marching intersector can trace"
575 );
576 }
577 let axis = a.axis();
578 let lift = (b.axis().location - a.axis().location).dot(axis.direction.vector());
581 let (slope_a, slope_b) = (a.half_angle().tan(), b.half_angle().tan());
582 let (ref_a, ref_b) = (
583 a.reference_radius(),
584 slope_b.mul_add(-lift, b.reference_radius()),
585 );
586 if (slope_a - slope_b).abs() <= tol.angular() {
587 return Ok(if (ref_a - ref_b).abs() <= tol.confusion() {
589 Meeting::Same
590 } else {
591 Meeting::Apart
592 });
593 }
594 let height = (ref_b - ref_a) / (slope_a - slope_b);
598 let radius = a.radius_at(height);
599 if radius.abs() <= tol.confusion() {
600 return Ok(Meeting::Touching(vec![a.apex()]));
602 }
603 if radius < 0.0 {
604 ogeom_bail!(
605 NotDone,
606 "two coaxial cones that meet only past their apexes, where the \
607 charts run mirrored, need the general machinery"
608 );
609 }
610 Ok(
611 match cone_parallel(
612 &a,
613 axis.location + axis.direction.vector() * height,
614 radius,
615 tol,
616 ) {
617 Some(circle) => Meeting::Along(vec![circle]),
618 None => Meeting::Apart,
619 },
620 )
621}
622
623fn cone_parallel(
625 cone: &ogeom_math::Cone,
626 centre: Point,
627 radius: f64,
628 tol: Tolerances,
629) -> Option<Curve> {
630 if radius <= tol.confusion() {
631 return None;
632 }
633 let frame = cone.frame();
634 let placed = Frame::new(centre, frame.z(), frame.x(), tol).ok()?;
635 Some(ogeom_geom::CircleCurve::new(Circle::new(placed, radius, tol).ok()?).into())
636}
637
638fn coaxial_tori(
645 a: ogeom_math::Torus,
646 b: ogeom_math::Torus,
647 tol: Tolerances,
648) -> OgeomResult<Meeting> {
649 if !a.axis().is_coaxial(b.axis(), tol) {
650 ogeom_bail!(
651 NotDone,
652 "two tori that do not share an axis meet in a curve only the \
653 general marching intersector can trace"
654 );
655 }
656 let axis = a.axis();
657 let lift = (b.axis().location - a.axis().location).dot(axis.direction.vector());
658 if (a.major_radius() - b.major_radius()).abs() <= tol.confusion()
659 && lift.abs() <= tol.confusion()
660 && (a.minor_radius() - b.minor_radius()).abs() <= tol.confusion()
661 {
662 return Ok(Meeting::Same);
663 }
664 let (ca, cb) = (
667 ogeom_math::Point2::new(a.major_radius(), 0.0),
668 ogeom_math::Point2::new(b.major_radius(), lift),
669 );
670 let between = cb - ca;
671 let distance = between.magnitude();
672 let (ra, rb) = (a.minor_radius(), b.minor_radius());
673 if distance <= tol.confusion() {
674 return Ok(Meeting::Apart);
677 }
678 if distance > ra + rb + tol.confusion() || distance < (ra - rb).abs() - tol.confusion() {
679 return Ok(Meeting::Apart);
680 }
681 let along = distance.mul_add(distance, ra.mul_add(ra, -(rb * rb))) / (2.0 * distance);
682 let squared = ra.mul_add(ra, -(along * along));
683 let direction = between * (1.0 / distance);
684 let foot = ca + direction * along;
685 let mut profile_points = Vec::new();
686 if squared <= tol.confusion() * tol.confusion() {
687 profile_points.push(foot);
688 } else {
689 let offset = ogeom_math::Vector2::new(-direction.y, direction.x) * squared.max(0.0).sqrt();
690 profile_points.push(foot + offset);
691 profile_points.push(foot - offset);
692 }
693 let circles: Vec<Curve> = profile_points
694 .into_iter()
695 .filter_map(|p| {
696 circle_on(
697 axis.location + axis.direction.vector() * p.y,
698 axis.direction,
699 p.x,
700 tol,
701 )
702 })
703 .collect();
704 Ok(if circles.is_empty() {
705 Meeting::Apart
706 } else {
707 Meeting::Along(circles)
708 })
709}
710
711fn intersect_axis_plane(
713 axis: ogeom_math::Axis,
714 plane: ogeom_math::Plane,
715 tol: Tolerances,
716) -> OgeomResult<Point> {
717 let along = plane.normal().dot(axis.direction);
718 if along.abs() <= tol.angular() {
719 ogeom_bail!(Domain, "the axis runs along the plane and never crosses it");
720 }
721 let t = -plane.signed_distance_to(axis.location) / along;
722 Ok(axis.location + axis.direction.vector() * t)
723}
724
725fn circle_on(centre: Point, normal: Direction, radius: f64, tol: Tolerances) -> Option<Curve> {
727 if radius <= tol.confusion() {
728 return None;
729 }
730 let reference = if normal.vector().cross(Vector::X).magnitude() > 0.5 {
732 Vector::X
733 } else {
734 Vector::Y
735 };
736 let x = Direction::from_cross(normal.vector(), reference, tol).ok()?;
737 let frame = Frame::new(centre, normal, x, tol).ok()?;
738 Some(ogeom_geom::CircleCurve::new(Circle::new(frame, radius, tol).ok()?).into())
739}
740
741fn line_through(through: Point, direction: Direction) -> Curve {
743 ogeom_geom::LineCurve::new(ogeom_math::Axis::new(through, direction)).into()
744}