use std::collections::HashMap;
use std::f64::consts::{PI, TAU};
use springrep::{
Error,
blend::{chamfer, fillet},
boolean::{difference, intersection, union},
check,
edit::offset_faces,
math::{Frame, Vec2, Vec3},
measure,
mesh::{self, MeshOptions},
profile::{NamedSegment, Profile, ProfileLoop, Segment},
sweep::{extrude, revolve},
topo::{Brep, EdgeId},
};
fn block(id: &str, lo: [f64; 3], hi: [f64; 3]) -> Brep {
let c = Vec2::new(0.5 * (lo[0] + hi[0]), 0.5 * (lo[1] + hi[1]));
let frame = Frame::new(Vec3::new(0.0, 0.0, lo[2]), Vec3::Z, Vec3::X).unwrap();
extrude(
&Profile::rect(c, hi[0] - lo[0], hi[1] - lo[1]),
&frame,
hi[2] - lo[2],
id,
)
.unwrap()
}
fn rod(id: &str, base: [f64; 3], axis: [f64; 3], r: f64, len: f64) -> Brep {
let frame = Frame::from_axis(Vec3::from_array(base), Vec3::from_array(axis)).unwrap();
extrude(&Profile::circle(Vec2::ZERO, 2.0 * r), &frame, len, id).unwrap()
}
fn ball(id: &str, centre: [f64; 3], r: f64) -> Brep {
let seg = |segment: Segment, name: &str| NamedSegment::new(segment, name);
let p = Profile {
loops: vec![ProfileLoop {
segments: vec![
seg(
Segment::Arc {
center: Vec2::ZERO,
radius: r,
start: -PI / 2.0,
sweep: PI,
},
"surface",
),
seg(
Segment::Line {
a: Vec2::new(0.0, r),
b: Vec2::new(0.0, -r),
},
"axis",
),
],
}],
};
let c = Vec3::from_array(centre);
let frame = Frame::new(c, Vec3::new(0.0, -1.0, 0.0), Vec3::X).unwrap();
revolve(&p, &frame, c, Vec3::Z, TAU, id).unwrap()
}
fn cone(id: &str, apex: [f64; 3], up: f64, r: f64, h: f64) -> Brep {
let seg = |a: [f64; 2], b: [f64; 2], name: &str| {
NamedSegment::new(
Segment::Line {
a: Vec2::new(a[0], a[1]),
b: Vec2::new(b[0], b[1]),
},
name,
)
};
let p = Profile {
loops: vec![ProfileLoop {
segments: vec![
seg([0.0, 0.0], [r, up * h], "side"),
seg([r, up * h], [0.0, up * h], "base"),
seg([0.0, up * h], [0.0, 0.0], "axis"),
],
}],
};
let c = Vec3::from_array(apex);
let frame = Frame::new(c, Vec3::new(0.0, -1.0, 0.0), Vec3::X).unwrap();
let mut b = revolve(&p, &frame, c, Vec3::Z, TAU, id).unwrap();
if measure::volume(&b) < 0.0 {
b = revolve(&p, &frame, c, -Vec3::Z, TAU, id).unwrap();
}
b
}
fn cone_measures(r: f64, h: f64) -> (f64, f64) {
(
PI * r * r * h / 3.0,
PI * r * r + PI * r * (r * r + h * h).sqrt(),
)
}
fn assert_touching(b: &Brep, volume: f64, area: f64, betti: [u64; 3]) {
let issues = check::check(b);
assert!(issues.is_empty(), "{issues:?}");
assert_eq!(b.solid_ids().count(), 1, "touching pieces are one solid");
let (v, a) = (measure::volume(b), measure::area(b));
assert!(
(v - volume).abs() <= 1e-9 * volume,
"volume {v} vs {volume}"
);
assert!((a - area).abs() <= 1e-9 * area, "area {a} vs {area}");
assert_eq!(measure::betti(b), betti);
let m = mesh::tessellate(b, &COARSE).unwrap();
for s in b.solid_ids() {
let r = mesh::report(b, &m, s);
assert!(r.watertight(), "{r:?}");
}
}
const COARSE: MeshOptions = MeshOptions {
linear: 0.2,
angular: 0.5,
};
fn four_face_edges(b: &Brep) -> Vec<EdgeId> {
let mut uses: HashMap<EdgeId, usize> = HashMap::new();
for c in b.coedge_ids() {
*uses.entry(b.coedge(c).edge).or_default() += 1;
}
let mut out: Vec<EdgeId> = uses
.into_iter()
.filter(|&(_, n)| n == 4)
.map(|(e, _)| e)
.collect();
out.sort();
out
}
#[test]
fn blocks_touching_along_an_edge_unite_round_it() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 10.0]);
let b = block("b", [10.0, 10.0, 0.0], [20.0, 20.0, 10.0]);
let u = union(&a, &b).unwrap();
assert_touching(&u, 2000.0, 1200.0, [1, 0, 0]);
assert_eq!(four_face_edges(&u).len(), 1);
}
#[test]
fn blocks_touching_at_a_corner_unite_there() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 10.0]);
let b = block("b", [10.0, 10.0, 10.0], [20.0, 20.0, 20.0]);
let u = union(&a, &b).unwrap();
assert_touching(&u, 2000.0, 1200.0, [1, 0, 0]);
assert_eq!(
u.vertex_ids()
.filter(|&v| u.vertex(v).point == Vec3::new(10.0, 10.0, 10.0))
.count(),
1
);
}
#[test]
fn a_rod_lying_on_a_box_face_unites_along_the_line() {
let a = block("a", [0.0, 0.0, 0.0], [20.0, 10.0, 10.0]);
let c = rod("c", [5.0, 5.0, 12.0], [1.0, 0.0, 0.0], 2.0, 10.0);
let u = union(&a, &c).unwrap();
let rod_area = 2.0 * PI * 4.0 + 2.0 * PI * 2.0 * 10.0;
assert_touching(&u, 2000.0 + PI * 4.0 * 10.0, 1000.0 + rod_area, [1, 0, 0]);
assert_eq!(four_face_edges(&u).len(), 1);
}
#[test]
fn a_box_less_an_inscribed_cylinder_is_four_corners_in_a_ring() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 8.0]);
let c = rod("c", [5.0, 5.0, -1.0], [0.0, 0.0, 1.0], 5.0, 10.0);
let d = difference(&a, &c).unwrap();
let area = 2.0 * (100.0 - PI * 25.0) + 4.0 * 80.0 + 2.0 * PI * 5.0 * 8.0;
assert_touching(&d, 800.0 - PI * 25.0 * 8.0, area, [1, 1, 0]);
assert_eq!(four_face_edges(&d).len(), 4);
}
#[test]
fn a_block_less_a_bore_flush_to_its_top_touches_itself_along_a_line() {
let a = block("a", [0.0, 0.0, 0.0], [12.0, 10.0, 8.0]);
let c = rod("c", [-1.0, 5.0, 5.0], [1.0, 0.0, 0.0], 3.0, 14.0);
let d = difference(&a, &c).unwrap();
let area = 2.0 * (120.0 + 96.0 + 80.0) - 2.0 * PI * 9.0 + 2.0 * PI * 3.0 * 12.0;
assert_touching(&d, 960.0 - PI * 9.0 * 12.0, area, [1, 1, 0]);
assert_eq!(four_face_edges(&d).len(), 1);
}
#[test]
fn a_rod_inside_a_box_against_a_wall_leaves_a_void_touching_it() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 10.0]);
let c = rod("c", [2.0, 5.0, 2.0], [0.0, 0.0, 1.0], 2.0, 6.0);
let d = difference(&a, &c).unwrap();
let area = 600.0 + 2.0 * PI * 4.0 + 2.0 * PI * 2.0 * 6.0;
assert_touching(&d, 1000.0 - PI * 4.0 * 6.0, area, [1, 0, 1]);
assert_eq!(four_face_edges(&d).len(), 1);
}
#[test]
fn a_cube_less_a_ball_touching_its_six_faces_keeps_the_void() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 10.0]);
let s = ball("s", [5.0, 5.0, 5.0], 5.0);
let d = difference(&a, &s).unwrap();
assert_touching(
&d,
1000.0 - 4.0 / 3.0 * PI * 125.0,
600.0 + 4.0 * PI * 25.0,
[1, 0, 1],
);
}
#[test]
fn a_touching_result_combined_with_itself_keeps_its_contact() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 10.0]);
let b = block("b", [10.0, 10.0, 0.0], [20.0, 20.0, 10.0]);
let u = union(&a, &b).unwrap();
for x in [intersection(&u, &u).unwrap(), union(&u, &u).unwrap()] {
assert_touching(&x, 2000.0, 1200.0, [1, 0, 0]);
assert_eq!(four_face_edges(&x).len(), 1);
}
}
#[test]
fn balls_touching_at_a_point_unite_there() {
let a = ball("a", [5.0, 0.0, 0.0], 5.0);
let b = ball("b", [15.0, 0.0, 0.0], 5.0);
let u = union(&a, &b).unwrap();
let (v, s) = (4.0 / 3.0 * PI * 125.0, 4.0 * PI * 25.0);
assert_touching(&u, 2.0 * v, 2.0 * s, [1, 0, 0]);
let at = Vec3::new(10.0, 0.0, 0.0);
assert_eq!(
u.vertex_ids()
.filter(|&v| u.vertex(v).point.dist(at) < 1e-9)
.count(),
1
);
}
#[test]
fn a_ball_on_a_box_face_unites_at_a_point() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 10.0]);
let s = ball("s", [5.0, 5.0, 13.0], 3.0);
let u = union(&a, &s).unwrap();
let (v, area) = (4.0 / 3.0 * PI * 27.0, 4.0 * PI * 9.0);
assert_touching(&u, 1000.0 + v, 600.0 + area, [1, 0, 0]);
}
#[test]
fn balls_touching_at_a_point_combine_as_inputs() {
let a = ball("a", [5.0, 0.0, 0.0], 5.0);
let b = ball("b", [15.0, 0.0, 0.0], 5.0);
let u = union(&a, &b).unwrap();
let (v, s) = (4.0 / 3.0 * PI * 125.0, 4.0 * PI * 25.0);
for x in [intersection(&u, &u).unwrap(), union(&u, &u).unwrap()] {
assert_touching(&x, 2.0 * v, 2.0 * s, [1, 0, 0]);
}
let c = ball("c", [25.0, 0.0, 0.0], 5.0);
assert_touching(&union(&u, &c).unwrap(), 3.0 * v, 3.0 * s, [1, 0, 0]);
let slab = block("s", [9.0, -6.0, -6.0], [11.0, 6.0, 6.0]);
let d = difference(&u, &slab).unwrap();
assert!(check::check(&d).is_empty());
assert_eq!(d.solid_ids().count(), 2);
assert_eq!(measure::betti(&d), [2, 0, 0]);
let cap = PI * (3.0 * 5.0 - 1.0) / 3.0;
assert!((measure::volume(&d) - 2.0 * (v - cap)).abs() < 1e-9 * v);
}
#[test]
fn a_cone_standing_on_its_apex_unites_with_the_face_under_it() {
let a = block("a", [-10.0, -10.0, -10.0], [10.0, 10.0, 0.0]);
let c = cone("c", [0.0, 0.0, 0.0], 1.0, 3.0, 6.0);
let u = union(&a, &c).unwrap();
let (v, s) = cone_measures(3.0, 6.0);
assert_touching(&u, 4000.0 + v, 1600.0 + s, [1, 0, 0]);
}
#[test]
fn cones_touching_apex_to_apex_unite_there() {
let a = cone("a", [0.0, 0.0, 0.0], 1.0, 3.0, 6.0);
let b = cone("b", [0.0, 0.0, 0.0], -1.0, 2.0, 5.0);
let u = union(&a, &b).unwrap();
let (va, sa) = cone_measures(3.0, 6.0);
let (vb, sb) = cone_measures(2.0, 5.0);
assert_touching(&u, va + vb, sa + sb, [1, 0, 0]);
}
#[test]
fn a_cone_on_a_ball_unites_at_its_apex() {
let a = ball("a", [0.0, 0.0, -4.0], 4.0);
let c = cone("c", [0.0, 0.0, 0.0], 1.0, 3.0, 6.0);
let u = union(&a, &c).unwrap();
let (v, s) = cone_measures(3.0, 6.0);
assert_touching(
&u,
4.0 / 3.0 * PI * 64.0 + v,
4.0 * PI * 16.0 + s,
[1, 0, 0],
);
}
#[test]
fn filling_the_corner_between_touching_blocks_makes_one_manifold_piece() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 10.0]);
let b = block("b", [10.0, 10.0, 0.0], [20.0, 20.0, 10.0]);
let u = union(&a, &b).unwrap();
let c = block("c", [5.0, 5.0, 0.0], [15.0, 15.0, 10.0]);
let filled = union(&u, &c).unwrap();
let area = 2.0 * 250.0 + 10.0 * 80.0;
assert_touching(&filled, 2500.0, area, [1, 0, 0]);
assert!(four_face_edges(&filled).is_empty());
}
#[test]
fn cutting_the_contact_away_parts_the_pieces() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 10.0]);
let b = block("b", [10.0, 10.0, 0.0], [20.0, 20.0, 10.0]);
let u = union(&a, &b).unwrap();
let c = block("c", [8.0, 8.0, -1.0], [12.0, 12.0, 11.0]);
let d = difference(&u, &c).unwrap();
assert!(check::check(&d).is_empty());
assert_eq!(d.solid_ids().count(), 2);
assert!((measure::volume(&d) - 2.0 * 960.0).abs() < 1e-9);
assert!(four_face_edges(&d).is_empty());
}
#[test]
fn cutting_across_the_contact_keeps_it_where_it_is_left() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 10.0]);
let b = block("b", [10.0, 10.0, 0.0], [20.0, 20.0, 10.0]);
let u = union(&a, &b).unwrap();
let c = block("c", [-1.0, -1.0, 5.0], [21.0, 21.0, 11.0]);
let d = difference(&u, &c).unwrap();
assert_touching(&d, 1000.0, 2.0 * (200.0 + 200.0), [1, 0, 0]);
assert_eq!(four_face_edges(&d).len(), 1);
}
#[test]
fn a_touching_result_with_its_contact_cut_into() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 8.0]);
let c = rod("c", [5.0, 5.0, -1.0], [0.0, 0.0, 1.0], 5.0, 10.0);
let ring = difference(&a, &c).unwrap();
let slot = block("s", [9.0, 4.0, -1.0], [11.0, 6.0, 9.0]);
let d = difference(&ring, &slot).unwrap();
assert!(check::check(&d).is_empty());
assert_eq!(d.solid_ids().count(), 1);
assert_eq!(measure::betti(&d), [1, 0, 0]);
assert_eq!(four_face_edges(&d).len(), 3);
}
fn refused_as_touching(r: Result<Brep, Error>) {
match r {
Err(Error::Unsupported(why)) => assert!(why.contains("touch"), "{why}"),
r => panic!(
"expected a refusal, got {:?}",
r.map(|b| measure::volume(&b))
),
}
}
#[test]
fn a_blend_along_a_contact_is_refused() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 10.0]);
let b = block("b", [10.0, 10.0, 0.0], [20.0, 20.0, 10.0]);
let u = union(&a, &b).unwrap();
refused_as_touching(fillet(&u, &[("a.u_max", "a.v_max")], 1.0, "f"));
refused_as_touching(chamfer(&u, &[("a.u_max", "a.v_max")], 1.0, "f"));
let f = fillet(&u, &[("a.u_min", "a.v_min")], 1.0, "f").unwrap();
assert!(check::check(&f).is_empty());
}
#[test]
fn moving_a_face_at_a_contact_is_refused() {
let a = block("a", [0.0, 0.0, 0.0], [10.0, 10.0, 10.0]);
let b = block("b", [10.0, 10.0, 0.0], [20.0, 20.0, 10.0]);
let u = union(&a, &b).unwrap();
let face = |name: &str| {
u.face_ids()
.find(|&f| {
u.face(f)
.names
.iter()
.any(|n| n.feature == "a" && n.face == name)
})
.unwrap()
};
refused_as_touching(offset_faces(&u, &HashMap::from([(face("u_max"), 1.0)])));
let moved = offset_faces(&u, &HashMap::from([(face("u_min"), 1.0)])).unwrap();
assert!(check::check(&moved).is_empty());
assert!((measure::volume(&moved) - 2100.0).abs() < 1e-9);
}
#[test]
fn a_face_a_rod_touches_through_a_hole_splits_round_the_hole() {
let holed = difference(
&block("block", [0.0, 0.0, 0.0], [40.0, 20.0, 20.0]),
&rod("hole", [20.0, -1.0, 10.0], [0.0, 1.0, 0.0], 2.0, 22.0),
)
.unwrap();
let standing = rod("rod", [20.0, -3.0, 5.0], [0.0, 0.0, 1.0], 3.0, 15.0);
let touched = union(&holed, &standing).unwrap();
assert!(check::check(&touched).is_empty());
let left = block("left", [-1.0, -10.0, -1.0], [30.0, 30.0, 21.0]);
let kept = intersection(&touched, &left).unwrap();
assert!(check::check(&kept).is_empty());
assert_eq!(kept.solid_ids().count(), 1);
let want = 12_000.0 - 80.0 * PI + 135.0 * PI;
let got = measure::volume(&kept);
assert!((got - want).abs() < 1e-6 * want, "{got}");
}