use ogeom_core::{OgeomResult, Tolerances, ogeom_bail};
use ogeom_geom::{Curve, SurfaceGeometry};
use ogeom_math::{Circle, Direction, Ellipse, Frame, Point, Vector};
#[derive(Debug, Clone, PartialEq)]
pub enum Meeting {
Apart,
Touching(Vec<Point>),
Along(Vec<Curve>),
Same,
}
pub fn surface_surface(
a: &SurfaceGeometry,
b: &SurfaceGeometry,
tol: Tolerances,
) -> OgeomResult<Meeting> {
use SurfaceGeometry as S;
match (a, b) {
(S::Plane(p), S::Plane(q)) => Ok(plane_plane(p.plane(), q.plane(), tol)),
(S::Plane(p), S::Sphere(s)) => Ok(plane_sphere(p.plane(), s.sphere(), tol)),
(S::Sphere(s), S::Plane(p)) => Ok(plane_sphere(p.plane(), s.sphere(), tol)),
(S::Plane(p), S::Cylinder(c)) => plane_cylinder(p.plane(), c.cylinder(), tol),
(S::Cylinder(c), S::Plane(p)) => plane_cylinder(p.plane(), c.cylinder(), tol),
(S::Sphere(x), S::Sphere(y)) => Ok(sphere_sphere(x.sphere(), y.sphere(), tol)),
(S::Cylinder(x), S::Cylinder(y)) => coaxial_cylinders(x.cylinder(), y.cylinder(), tol),
(S::Cylinder(c), S::Sphere(s)) => coaxial_cylinder_sphere(c.cylinder(), s.sphere(), tol),
(S::Sphere(s), S::Cylinder(c)) => coaxial_cylinder_sphere(c.cylinder(), s.sphere(), tol),
(S::Plane(p), S::Torus(t)) => axial_plane_torus(p.plane(), t.torus(), tol),
(S::Torus(t), S::Plane(p)) => axial_plane_torus(p.plane(), t.torus(), tol),
(S::Cylinder(c), S::Torus(t)) => coaxial_cylinder_torus(c.cylinder(), t.torus(), tol),
(S::Torus(t), S::Cylinder(c)) => coaxial_cylinder_torus(c.cylinder(), t.torus(), tol),
(S::Torus(x), S::Torus(y)) => coaxial_tori(x.torus(), y.torus(), tol),
(S::Plane(p), S::Cone(c)) => plane_cone(p.plane(), c.cone(), tol),
(S::Cone(c), S::Plane(p)) => plane_cone(p.plane(), c.cone(), tol),
(S::Cylinder(x), S::Cone(c)) => coaxial_cylinder_cone(x.cylinder(), c.cone(), tol),
(S::Cone(c), S::Cylinder(x)) => coaxial_cylinder_cone(x.cylinder(), c.cone(), tol),
(S::Cone(x), S::Cone(y)) => coaxial_cones(x.cone(), y.cone(), tol),
_ => ogeom_bail!(
NotDone,
"this pair of surfaces has no closed-form intersection; it needs \
the general marching intersector, which is gated on the benchmark \
these cases provide the ground truth for"
),
}
}
fn plane_plane(a: ogeom_math::Plane, b: ogeom_math::Plane, tol: Tolerances) -> Meeting {
let along = a.normal().dot(b.normal());
if (along.abs() - 1.0).abs() <= tol.angular() {
return if a.distance_to(b.origin()) <= tol.confusion() {
Meeting::Same
} else {
Meeting::Apart
};
}
let Ok(direction) = Direction::from_cross(a.normal().vector(), b.normal().vector(), tol) else {
return Meeting::Apart;
};
let (da, db) = (
a.normal().dot_vector(a.origin().to_vector()),
b.normal().dot_vector(b.origin().to_vector()),
);
let (na, nb) = (a.normal().vector(), b.normal().vector());
let dot = na.dot(nb);
let denominator = dot.mul_add(-dot, 1.0);
if denominator.abs() <= tol.angular() {
return Meeting::Apart;
}
let ca = da.mul_add(1.0, -(db * dot)) / denominator;
let cb = db.mul_add(1.0, -(da * dot)) / denominator;
let through = Point::from_vector(na * ca + nb * cb);
Meeting::Along(vec![line_through(through, direction)])
}
fn plane_sphere(plane: ogeom_math::Plane, sphere: ogeom_math::Sphere, tol: Tolerances) -> Meeting {
let gap = plane.signed_distance_to(sphere.centre());
let reach = gap.abs();
if reach > sphere.radius() + tol.confusion() {
return Meeting::Apart;
}
let foot = plane.project(sphere.centre());
if (reach - sphere.radius()).abs() <= tol.confusion() {
return Meeting::Touching(vec![foot]);
}
let radius = sphere
.radius()
.mul_add(sphere.radius(), -(gap * gap))
.max(0.0)
.sqrt();
match circle_on(foot, plane.normal(), radius, tol) {
Some(circle) => Meeting::Along(vec![circle]),
None => Meeting::Touching(vec![foot]),
}
}
fn plane_cylinder(
plane: ogeom_math::Plane,
cylinder: ogeom_math::Cylinder,
tol: Tolerances,
) -> OgeomResult<Meeting> {
let axis = cylinder.axis();
let along = plane.normal().dot(axis.direction);
if along.abs() <= tol.angular() {
let gap = plane.signed_distance_to(axis.location);
let reach = gap.abs();
if reach > cylinder.radius() + tol.confusion() {
return Ok(Meeting::Apart);
}
let offset = cylinder
.radius()
.mul_add(cylinder.radius(), -(gap * gap))
.max(0.0)
.sqrt();
let foot = plane.project(axis.location);
let sideways =
Direction::from_cross(plane.normal().vector(), axis.direction.vector(), tol)?;
if offset <= tol.confusion() {
return Ok(Meeting::Along(vec![line_through(foot, axis.direction)]));
}
return Ok(Meeting::Along(vec![
line_through(foot + sideways.vector() * offset, axis.direction),
line_through(foot - sideways.vector() * offset, axis.direction),
]));
}
let centre = intersect_axis_plane(axis, plane, tol)?;
if (along.abs() - 1.0).abs() <= tol.angular() {
return Ok(
match circle_on(centre, plane.normal(), cylinder.radius(), tol) {
Some(circle) => Meeting::Along(vec![circle]),
None => Meeting::Apart,
},
);
}
let minor = cylinder.radius();
let major = minor / along.abs();
let minor_direction =
Direction::from_cross(plane.normal().vector(), axis.direction.vector(), tol)?;
let major_direction =
Direction::from_cross(minor_direction.vector(), plane.normal().vector(), tol)?;
let frame = Frame::from_axes(
centre,
major_direction,
minor_direction,
plane.normal(),
tol,
)?;
Ok(Meeting::Along(vec![
ogeom_geom::EllipseCurve::new(Ellipse::new(frame, major, minor, tol)?).into(),
]))
}
fn sphere_sphere(a: ogeom_math::Sphere, b: ogeom_math::Sphere, tol: Tolerances) -> Meeting {
let between = b.centre() - a.centre();
let distance = between.magnitude();
if distance <= tol.confusion() {
return if (a.radius() - b.radius()).abs() <= tol.confusion() {
Meeting::Same
} else {
Meeting::Apart
};
}
let (ra, rb) = (a.radius(), b.radius());
if distance > ra + rb + tol.confusion() || distance < (ra - rb).abs() - tol.confusion() {
return Meeting::Apart;
}
let Ok(direction) = Direction::new(between, tol) else {
return Meeting::Apart;
};
let reach = distance.mul_add(distance, ra.mul_add(ra, -(rb * rb))) / (2.0 * distance);
let centre = a.centre() + direction.vector() * reach;
let squared = ra.mul_add(ra, -(reach * reach));
if squared <= tol.confusion() * tol.confusion() {
return Meeting::Touching(vec![centre]);
}
match circle_on(centre, direction, squared.max(0.0).sqrt(), tol) {
Some(circle) => Meeting::Along(vec![circle]),
None => Meeting::Touching(vec![centre]),
}
}
fn coaxial_cylinders(
a: ogeom_math::Cylinder,
b: ogeom_math::Cylinder,
tol: Tolerances,
) -> OgeomResult<Meeting> {
if !a.axis().is_coaxial(b.axis(), tol) {
if (a.radius() - b.radius()).abs() <= tol.confusion() {
let (da, db) = (a.axis().direction.vector(), b.axis().direction.vector());
let normal = da.cross(db);
if normal.magnitude() > tol.angular() {
let (pa, pb) = (a.axis().location, b.axis().location);
let w = pb - pa;
let dd = da.dot(db);
let denom = dd.mul_add(-dd, 1.0);
let s = dd.mul_add(-db.dot(w), da.dot(w)) / denom;
let t = dd.mul_add(da.dot(w), -db.dot(w)) / denom;
let on_a = pa + da * s;
let on_b = pb + db * t;
if on_a.distance(on_b) <= tol.confusion() {
let centre = on_a;
let mut curves = Vec::new();
for m in [da - db, da + db] {
if m.magnitude() <= tol.angular() {
continue;
}
let plane =
ogeom_math::Plane::through(centre, ogeom_math::Direction::new(m, tol)?);
if let Meeting::Along(mut found) = plane_cylinder(plane, a, tol)? {
curves.append(&mut found);
}
}
if !curves.is_empty() {
return Ok(Meeting::Along(curves));
}
}
}
}
ogeom_bail!(
NotDone,
"two cylinders that do not share an axis meet in a quartic space \
curve, which needs the general marching intersector"
);
}
Ok(if (a.radius() - b.radius()).abs() <= tol.confusion() {
Meeting::Same
} else {
Meeting::Apart
})
}
fn coaxial_cylinder_sphere(
cylinder: ogeom_math::Cylinder,
sphere: ogeom_math::Sphere,
tol: Tolerances,
) -> OgeomResult<Meeting> {
let axis = cylinder.axis();
if axis.distance_to(sphere.centre()) > tol.confusion() {
ogeom_bail!(
NotDone,
"a sphere off a cylinder's axis meets it in a quartic space curve, \
which needs the general marching intersector"
);
}
let (r, radius) = (cylinder.radius(), sphere.radius());
if r > radius + tol.confusion() {
return Ok(Meeting::Apart);
}
if (r - radius).abs() <= tol.confusion() {
let centre = sphere.centre();
return Ok(match circle_on(centre, axis.direction, r, tol) {
Some(circle) => Meeting::Along(vec![circle]),
None => Meeting::Apart,
});
}
let reach = radius.mul_add(radius, -(r * r)).max(0.0).sqrt();
let mut out = Vec::with_capacity(2);
for side in [reach, -reach] {
let centre = sphere.centre() + axis.direction.vector() * side;
if let Some(circle) = circle_on(centre, axis.direction, r, tol) {
out.push(circle);
}
}
Ok(if out.is_empty() {
Meeting::Apart
} else {
Meeting::Along(out)
})
}
fn axial_plane_torus(
plane: ogeom_math::Plane,
torus: ogeom_math::Torus,
tol: Tolerances,
) -> OgeomResult<Meeting> {
let axis = torus.axis();
let along = plane.normal().dot(axis.direction);
if along.abs() <= tol.angular()
&& plane.signed_distance_to(axis.location).abs() <= tol.confusion()
{
return Ok(meridians(plane, torus, tol));
}
if (along.abs() - 1.0).abs() > tol.angular() {
ogeom_bail!(
NotDone,
"a plane oblique to a torus's axis, or parallel to it and off it, \
meets it in a quartic, which needs the general marching \
intersector"
);
}
let height = -plane.signed_distance_to(axis.location) * along.signum();
let minor = torus.minor_radius();
if height.abs() > minor + tol.confusion() {
return Ok(Meeting::Apart);
}
let centre = axis.location + axis.direction.vector() * height;
if (height.abs() - minor).abs() <= tol.confusion() {
return Ok(
match circle_on(centre, axis.direction, torus.major_radius(), tol) {
Some(circle) => Meeting::Along(vec![circle]),
None => Meeting::Apart,
},
);
}
let spread = minor.mul_add(minor, -(height * height)).max(0.0).sqrt();
let circles: Vec<Curve> = [torus.major_radius() + spread, torus.major_radius() - spread]
.into_iter()
.filter_map(|radius| circle_on(centre, axis.direction, radius, tol))
.collect();
Ok(if circles.is_empty() {
Meeting::Apart
} else {
Meeting::Along(circles)
})
}
fn meridians(plane: ogeom_math::Plane, torus: ogeom_math::Torus, tol: Tolerances) -> Meeting {
let axis = torus.axis();
let normal = plane.normal();
let Ok(out) = Direction::from_cross(axis.direction.vector(), normal.vector(), tol) else {
return Meeting::Apart;
};
let circles: Vec<Curve> = [out.vector(), -out.vector()]
.into_iter()
.filter_map(|radial| {
let centre = axis.location + radial * torus.major_radius();
let x = Direction::new(radial, tol).ok()?;
let frame = Frame::new(centre, normal, x, tol).ok()?;
let circle = Circle::new(frame, torus.minor_radius(), tol).ok()?;
Some(ogeom_geom::CircleCurve::new(circle).into())
})
.collect();
Meeting::Along(circles)
}
fn coaxial_cylinder_torus(
cylinder: ogeom_math::Cylinder,
torus: ogeom_math::Torus,
tol: Tolerances,
) -> OgeomResult<Meeting> {
if !cylinder.axis().is_coaxial(torus.axis(), tol) {
ogeom_bail!(
NotDone,
"a cylinder off a torus's axis meets it in a quartic space curve, \
which needs the general marching intersector"
);
}
let axis = torus.axis();
let reach = (cylinder.radius() - torus.major_radius()).abs();
let minor = torus.minor_radius();
if reach > minor + tol.confusion() {
return Ok(Meeting::Apart);
}
if (reach - minor).abs() <= tol.confusion() {
return Ok(
match circle_on(axis.location, axis.direction, cylinder.radius(), tol) {
Some(circle) => Meeting::Along(vec![circle]),
None => Meeting::Apart,
},
);
}
let rise = minor.mul_add(minor, -(reach * reach)).max(0.0).sqrt();
let circles: Vec<Curve> = [rise, -rise]
.into_iter()
.filter_map(|height| {
circle_on(
axis.location + axis.direction.vector() * height,
axis.direction,
cylinder.radius(),
tol,
)
})
.collect();
Ok(if circles.is_empty() {
Meeting::Apart
} else {
Meeting::Along(circles)
})
}
fn plane_cone(
plane: ogeom_math::Plane,
cone: ogeom_math::Cone,
tol: Tolerances,
) -> OgeomResult<Meeting> {
let axis = cone.axis();
let along = plane.normal().dot(axis.direction);
if (along.abs() - 1.0).abs() > tol.angular() {
ogeom_bail!(
NotDone,
"a plane oblique to a cone's axis meets it in a conic, which \
needs the general marching intersector"
);
}
let height = -plane.signed_distance_to(axis.location) * along.signum();
let radius = cone.radius_at(height);
if radius.abs() <= tol.confusion() {
return Ok(Meeting::Touching(vec![cone.apex()]));
}
if radius < 0.0 {
ogeom_bail!(
NotDone,
"the plane crosses the cone past its apex, where the chart runs \
mirrored; that configuration needs the general machinery"
);
}
let centre = axis.location + axis.direction.vector() * height;
Ok(match cone_parallel(&cone, centre, radius, tol) {
Some(circle) => Meeting::Along(vec![circle]),
None => Meeting::Apart,
})
}
fn coaxial_cylinder_cone(
cylinder: ogeom_math::Cylinder,
cone: ogeom_math::Cone,
tol: Tolerances,
) -> OgeomResult<Meeting> {
if !cylinder.axis().is_coaxial(cone.axis(), tol) {
ogeom_bail!(
NotDone,
"a cylinder off a cone's axis meets it in a curve only the \
general marching intersector can trace"
);
}
let axis = cone.axis();
let slope = cone.half_angle().tan();
let height = (cylinder.radius() - cone.reference_radius()) / slope;
Ok(
match cone_parallel(
&cone,
axis.location + axis.direction.vector() * height,
cylinder.radius(),
tol,
) {
Some(circle) => Meeting::Along(vec![circle]),
None => Meeting::Apart,
},
)
}
fn coaxial_cones(
a: ogeom_math::Cone,
b: ogeom_math::Cone,
tol: Tolerances,
) -> OgeomResult<Meeting> {
if !a.axis().is_coaxial(b.axis(), tol) {
ogeom_bail!(
NotDone,
"two cones that do not share an axis meet in a curve only the \
general marching intersector can trace"
);
}
let axis = a.axis();
let lift = (b.axis().location - a.axis().location).dot(axis.direction.vector());
let (slope_a, slope_b) = (a.half_angle().tan(), b.half_angle().tan());
let (ref_a, ref_b) = (
a.reference_radius(),
slope_b.mul_add(-lift, b.reference_radius()),
);
if (slope_a - slope_b).abs() <= tol.angular() {
return Ok(if (ref_a - ref_b).abs() <= tol.confusion() {
Meeting::Same
} else {
Meeting::Apart
});
}
let height = (ref_b - ref_a) / (slope_a - slope_b);
let radius = a.radius_at(height);
if radius.abs() <= tol.confusion() {
return Ok(Meeting::Touching(vec![a.apex()]));
}
if radius < 0.0 {
ogeom_bail!(
NotDone,
"two coaxial cones that meet only past their apexes, where the \
charts run mirrored, need the general machinery"
);
}
Ok(
match cone_parallel(
&a,
axis.location + axis.direction.vector() * height,
radius,
tol,
) {
Some(circle) => Meeting::Along(vec![circle]),
None => Meeting::Apart,
},
)
}
fn cone_parallel(
cone: &ogeom_math::Cone,
centre: Point,
radius: f64,
tol: Tolerances,
) -> Option<Curve> {
if radius <= tol.confusion() {
return None;
}
let frame = cone.frame();
let placed = Frame::new(centre, frame.z(), frame.x(), tol).ok()?;
Some(ogeom_geom::CircleCurve::new(Circle::new(placed, radius, tol).ok()?).into())
}
fn coaxial_tori(
a: ogeom_math::Torus,
b: ogeom_math::Torus,
tol: Tolerances,
) -> OgeomResult<Meeting> {
if !a.axis().is_coaxial(b.axis(), tol) {
ogeom_bail!(
NotDone,
"two tori that do not share an axis meet in a curve only the \
general marching intersector can trace"
);
}
let axis = a.axis();
let lift = (b.axis().location - a.axis().location).dot(axis.direction.vector());
if (a.major_radius() - b.major_radius()).abs() <= tol.confusion()
&& lift.abs() <= tol.confusion()
&& (a.minor_radius() - b.minor_radius()).abs() <= tol.confusion()
{
return Ok(Meeting::Same);
}
let (ca, cb) = (
ogeom_math::Point2::new(a.major_radius(), 0.0),
ogeom_math::Point2::new(b.major_radius(), lift),
);
let between = cb - ca;
let distance = between.magnitude();
let (ra, rb) = (a.minor_radius(), b.minor_radius());
if distance <= tol.confusion() {
return Ok(Meeting::Apart);
}
if distance > ra + rb + tol.confusion() || distance < (ra - rb).abs() - tol.confusion() {
return Ok(Meeting::Apart);
}
let along = distance.mul_add(distance, ra.mul_add(ra, -(rb * rb))) / (2.0 * distance);
let squared = ra.mul_add(ra, -(along * along));
let direction = between * (1.0 / distance);
let foot = ca + direction * along;
let mut profile_points = Vec::new();
if squared <= tol.confusion() * tol.confusion() {
profile_points.push(foot);
} else {
let offset = ogeom_math::Vector2::new(-direction.y, direction.x) * squared.max(0.0).sqrt();
profile_points.push(foot + offset);
profile_points.push(foot - offset);
}
let circles: Vec<Curve> = profile_points
.into_iter()
.filter_map(|p| {
circle_on(
axis.location + axis.direction.vector() * p.y,
axis.direction,
p.x,
tol,
)
})
.collect();
Ok(if circles.is_empty() {
Meeting::Apart
} else {
Meeting::Along(circles)
})
}
fn intersect_axis_plane(
axis: ogeom_math::Axis,
plane: ogeom_math::Plane,
tol: Tolerances,
) -> OgeomResult<Point> {
let along = plane.normal().dot(axis.direction);
if along.abs() <= tol.angular() {
ogeom_bail!(Domain, "the axis runs along the plane and never crosses it");
}
let t = -plane.signed_distance_to(axis.location) / along;
Ok(axis.location + axis.direction.vector() * t)
}
fn circle_on(centre: Point, normal: Direction, radius: f64, tol: Tolerances) -> Option<Curve> {
if radius <= tol.confusion() {
return None;
}
let reference = if normal.vector().cross(Vector::X).magnitude() > 0.5 {
Vector::X
} else {
Vector::Y
};
let x = Direction::from_cross(normal.vector(), reference, tol).ok()?;
let frame = Frame::new(centre, normal, x, tol).ok()?;
Some(ogeom_geom::CircleCurve::new(Circle::new(frame, radius, tol).ok()?).into())
}
fn line_through(through: Point, direction: Direction) -> Curve {
ogeom_geom::LineCurve::new(ogeom_math::Axis::new(through, direction)).into()
}