Kernel: pole-free six-patch spheres; vendored truck-shapeops with robust meshing and nearest-parameter loop projection (sphere and box booleans work); least-squares sphere classification

Oracle: 25/29 (14 and 15 now match Fusion).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AS5Tn3xieewUSg3uxrMVdL
This commit is contained in:
bherbruckandClaude Opus 5.5 committed 2026-10-07 21:30:18 +00:00
1 parent 7ef3426cb3
commit 043a542e29
34 files changed
+6228 -43

No files matched your search

+151
View File
@@ -0,0 +1,151 @@
use super::*;
use truck_geometry::prelude::IntersectionCurve;
use truck_meshalgo::prelude::*;
use truck_topology::*;
/// Only solids consisting of faces whose surface is implemented this trait can be used for set operations.
pub trait ShapeOpsSurface:
ParametricSurface3D
+ ParameterDivision2D
+ SearchParameter<D2, Point = Point3>
+ SearchNearestParameter<D2, Point = Point3>
+ Invertible
+ Send
+ Sync {
}
impl<S> ShapeOpsSurface for S where S: ParametricSurface3D
+ ParameterDivision2D
+ SearchParameter<D2, Point = Point3>
+ SearchNearestParameter<D2, Point = Point3>
+ Invertible
+ Send
+ Sync
{
}
/// Only solids consisting of edges whose curve is implemented this trait can be used for set operations.
pub trait ShapeOpsCurve<S: ShapeOpsSurface>:
ParametricCurve3D
+ ParameterDivision1D<Point = Point3>
+ Cut
+ Invertible
+ From<IntersectionCurve<PolylineCurve<Point3>, S>>
+ SearchParameter<D1, Point = Point3>
+ SearchNearestParameter<D1, Point = Point3>
+ Send
+ Sync {
}
impl<C, S: ShapeOpsSurface> ShapeOpsCurve<S> for C where C: ParametricCurve3D
+ ParameterDivision1D<Point = Point3>
+ Cut
+ Invertible
+ From<IntersectionCurve<PolylineCurve<Point3>, S>>
+ SearchParameter<D1, Point = Point3>
+ SearchNearestParameter<D1, Point = Point3>
+ Send
+ Sync
{
}
fn process_one_pair_of_shells<C: ShapeOpsCurve<S>, S: ShapeOpsSurface>(
shell0: &Shell<Point3, C, S>,
shell1: &Shell<Point3, C, S>,
tol: f64,
) -> Option<[Shell<Point3, C, S>; 2]> {
nonpositive_tolerance!(tol);
// SolveCraft: robust meshing (boundaries from earlier operations lie on surfaces only
// within tolerance).
let poly_shell0 = shell0.robust_triangulation(tol);
let poly_shell1 = shell1.robust_triangulation(tol);
let loops_store::LoopsStoreQuadruple {
geom_loops_store0: loops_store0,
geom_loops_store1: loops_store1,
..
} = crate::tr!(loops_store::create_loops_stores(shell0, &poly_shell0, shell1, &poly_shell1, tol), "loops stores");
let mut cls0 = crate::tr!(divide_face::divide_faces(shell0, &loops_store0, tol), "divide faces 0");
cls0.integrate_by_component();
let mut cls1 = crate::tr!(divide_face::divide_faces(shell1, &loops_store1, tol), "divide faces 1");
cls1.integrate_by_component();
let [mut and0, mut or0, unknown0] = cls0.and_or_unknown();
unknown0.into_iter().try_for_each(|face| {
let pt = face.boundaries()[0].vertex_iter().next().unwrap().point();
let dir = hash::take_one_unit(pt);
let count = poly_shell1.iter().try_fold(0, |count, face| {
let poly = face.surface()?;
Some(count + poly.signed_crossing_faces(pt, dir))
})?;
if count >= 1 {
and0.push(face);
} else {
or0.push(face);
}
Some(())
})?;
let [mut and1, mut or1, unknown1] = cls1.and_or_unknown();
unknown1.into_iter().try_for_each(|face| {
let pt = face.boundaries()[0].vertex_iter().next().unwrap().point();
let dir = hash::take_one_unit(pt);
let count = poly_shell0.iter().try_fold(0, |count, face| {
let poly = face.surface()?;
Some(count + poly.signed_crossing_faces(pt, dir))
})?;
if count >= 1 {
and1.push(face);
} else {
or1.push(face);
}
Some(())
})?;
and0.append(&mut and1);
or0.append(&mut or1);
Some([and0, or0])
}
/// AND operation between two solids.
pub fn and<C: ShapeOpsCurve<S>, S: ShapeOpsSurface>(
solid0: &Solid<Point3, C, S>,
solid1: &Solid<Point3, C, S>,
tol: f64,
) -> Option<Solid<Point3, C, S>> {
let mut iter0 = solid0.boundaries().iter();
let mut iter1 = solid1.boundaries().iter();
let shell0 = iter0.next().unwrap();
let shell1 = iter1.next().unwrap();
let [mut and_shell, _] = process_one_pair_of_shells(shell0, shell1, tol)?;
for shell in iter0 {
let [res, _] = process_one_pair_of_shells(&and_shell, shell, tol)?;
and_shell = res;
}
for shell in iter1 {
let [res, _] = process_one_pair_of_shells(&and_shell, shell, tol)?;
and_shell = res;
}
let boundaries = and_shell.connected_components();
Some(Solid::new(boundaries))
}
/// OR operation between two solids.
pub fn or<C: ShapeOpsCurve<S>, S: ShapeOpsSurface>(
solid0: &Solid<Point3, C, S>,
solid1: &Solid<Point3, C, S>,
tol: f64,
) -> Option<Solid<Point3, C, S>> {
let mut iter0 = solid0.boundaries().iter();
let mut iter1 = solid1.boundaries().iter();
let shell0 = iter0.next().unwrap();
let shell1 = iter1.next().unwrap();
let [_, mut or_shell] = process_one_pair_of_shells(shell0, shell1, tol)?;
for shell in iter0 {
let [_, res] = process_one_pair_of_shells(&or_shell, shell, tol)?;
or_shell = res;
}
for shell in iter1 {
let [_, res] = process_one_pair_of_shells(&or_shell, shell, tol)?;
or_shell = res;
}
let boundaries = or_shell.connected_components();
Some(Solid::new(boundaries))
}
#[cfg(test)]
mod tests;
@@ -0,0 +1,21 @@
use truck_meshalgo::prelude::*;
use truck_modeling::*;
#[test]
fn punched_cube() {
let v = builder::vertex(Point3::origin());
let e = builder::tsweep(&v, Vector3::unit_x());
let f = builder::tsweep(&e, Vector3::unit_y());
let cube = builder::tsweep(&f, Vector3::unit_z());
let v = builder::vertex(Point3::new(0.5, 0.25, -0.5));
let w = builder::rsweep(&v, Point3::new(0.5, 0.5, 0.0), Vector3::unit_z(), Rad(7.0));
let f = builder::try_attach_plane(&[w]).unwrap();
let mut cylinder = builder::tsweep(&f, Vector3::unit_z() * 2.0);
cylinder.not();
let and = crate::and(&cube, &cylinder, 0.05).unwrap();
let poly = and.triangulation(0.01).to_polygon();
let file = std::fs::File::create("punched-cube.obj").unwrap();
obj::write(&poly, file).unwrap();
}