use std::f64::consts::{FRAC_PI_2, PI, TAU};
use springrep::{
blend::fillet,
boolean::{difference, unify, union},
check,
geom::{Curve, Surface},
loft::{Section, loft},
math::{Frame, Vec2, Vec3},
measure,
profile::{NamedSegment, Profile},
sweep::extrude,
topo::{Brep, EdgeId},
};
fn up(z: f64) -> Frame {
Frame::new(Vec3::new(0.0, 0.0, z), Vec3::Z, Vec3::X).unwrap()
}
fn line(a: (f64, f64), b: (f64, f64), name: &str) -> NamedSegment {
NamedSegment::line(Vec2::new(a.0, a.1), Vec2::new(b.0, b.1), name)
}
fn arc(center: (f64, f64), radius: f64, start: f64, sweep: f64, name: &str) -> NamedSegment {
NamedSegment::arc(Vec2::new(center.0, center.1), radius, start, sweep, name)
}
fn sloped_block() -> Brep {
let block = extrude(
&Profile::rect(Vec2::ZERO, 20.0, 20.0),
&up(0.0),
10.0,
"block",
)
.unwrap();
let sections =
[(4.0, 14.0, 16.0), (11.0, -14.0, 16.0)].map(|(z, x0, x1): (f64, f64, f64)| Section {
profile: Profile::rect(Vec2::new(0.5 * (x0 + x1), 0.0), x1 - x0, 30.0),
frame: up(z),
});
difference(&block, &loft(§ions, true, "slope").unwrap()).unwrap()
}
fn standing(profile: &Profile, id: &str) -> Brep {
union(
&sloped_block(),
&extrude(profile, &up(0.0), 14.0, id).unwrap(),
)
.unwrap()
}
fn sloped_rod(boss: bool) -> Brep {
let circle = Profile::circle(Vec2::ZERO, 6.0);
if boss {
standing(&circle, "rod")
} else {
let rod = extrude(&circle, &up(0.0), 12.0, "rod").unwrap();
difference(&sloped_block(), &rod).unwrap()
}
}
fn from_lifted_circle(p: Vec3, offset: f64) -> f64 {
let centre = |phi: f64| {
let (x, y) = (4.0 * phi.cos(), 4.0 * phi.sin());
Vec3::new(x, y, 7.5 - x / 4.0 + offset)
};
let (mut lo, mut hi) = (p.y.atan2(p.x) - 0.6, p.y.atan2(p.x) + 0.6);
for _ in 0..200 {
let (a, c) = (lo + (hi - lo) / 3.0, hi - (hi - lo) / 3.0);
if centre(a).dist(p) < centre(c).dist(p) {
hi = c;
} else {
lo = a;
}
}
centre(0.5 * (lo + hi)).dist(p)
}
fn along(b: &Brep, e: EdgeId) -> Vec<Vec3> {
let x = b.edge(e);
let curve = x.curve.as_ref().unwrap();
(0..=16)
.map(|k| curve.eval(x.range.0 + (x.range.1 - x.range.0) * k as f64 / 16.0))
.collect()
}
#[test]
fn a_rolling_ball_rounds_a_rod_through_a_slope() {
let r = 1.0;
let lean = (1.0f64 + 1.0 / 16.0).sqrt();
for boss in [false, true] {
let b = fillet(&sloped_rod(boss), &[("rod.side", "slope.side")], r, "rim").unwrap();
let b = unify(&b).unwrap();
assert!(check::check(&b).is_empty());
let plain = measure::volume(&unify(&sloped_rod(boss)).unwrap());
let filled = measure::volume(&b);
assert!(if boss { filled > plain } else { filled < plain });
let offset = if boss { r * lean } else { -r * lean };
let f = b
.face_ids()
.find(|&f| b.face(f).names.iter().any(|n| n.feature == "rim"))
.expect("the blend face");
let Surface::Nurbs(s) = &b.face(f).surface else {
panic!("the blend is a NURBS surface");
};
let ((u0, u1), (v0, v1)) = s.domain();
let on_plane = |p: Vec3| ((p.z + p.x / 4.0 - 7.5) / lean).abs() < 1e-8;
let on_rod = |p: Vec3| (p.x.hypot(p.y) - 3.0).abs() < 1e-8;
for i in 0..200 {
let u = u0 + (u1 - u0) * i as f64 / 200.0;
for j in 0..=8 {
let p = s.eval(u, v0 + (v1 - v0) * j as f64 / 8.0);
assert!(
(from_lifted_circle(p, offset) - r).abs() < 1e-8,
"boss {boss}: {p:?}"
);
}
let (a, c) = (s.eval(u, v0), s.eval(u, v1));
assert!(
on_plane(a) && on_rod(c) || on_rod(a) && on_plane(c),
"boss {boss}: {a:?} {c:?}"
);
}
}
}
fn sloped_slot() -> Brep {
let slot = Profile::outline(vec![
line((-4.0, -3.0), (4.0, -3.0), "mid_v_min"),
arc((4.0, 0.0), 3.0, -FRAC_PI_2, PI, "e2_side"),
line((4.0, 3.0), (-4.0, 3.0), "mid_v_max"),
arc((-4.0, 0.0), 3.0, FRAC_PI_2, PI, "e1_side"),
]);
standing(&slot, "slot")
}
#[test]
fn a_rolling_ball_rounds_a_slot_through_a_slope_across_its_tangent_faces() {
let r = 1.0;
let lean = (1.0f64 + 1.0 / 16.0).sqrt();
let b = fillet(
&sloped_slot(),
&[("slot.mid_v_min", "slope.side")],
r,
"foot",
)
.unwrap();
let b = unify(&b).unwrap();
assert!(check::check(&b).is_empty());
let plain = measure::volume(&unify(&sloped_slot()).unwrap());
assert!(measure::volume(&b) > plain);
let lift = |x: f64, y: f64| Vec3::new(x, y, 7.5 - x / 4.0 + r * lean);
let to_segment = |p: Vec3, a: Vec3, c: Vec3| {
let d = c - a;
let t = ((p - a).dot(d) / d.dot(d)).clamp(0.0, 1.0);
(a + d * t).dist(p)
};
let to_arc = |p: Vec3, cx: f64, from: f64| {
let at = |phi: f64| lift(cx + 4.0 * phi.cos(), 4.0 * phi.sin());
let (mut lo, mut hi) = (from, from + PI);
for _ in 0..200 {
let (a, c) = (lo + (hi - lo) / 3.0, hi - (hi - lo) / 3.0);
if at(a).dist(p) < at(c).dist(p) {
hi = c;
} else {
lo = a;
}
}
at(0.5 * (lo + hi)).dist(p)
};
let from_centres = |p: Vec3| {
to_segment(p, lift(-4.0, 4.0), lift(4.0, 4.0))
.min(to_segment(p, lift(-4.0, -4.0), lift(4.0, -4.0)))
.min(to_arc(p, 4.0, -FRAC_PI_2))
.min(to_arc(p, -4.0, FRAC_PI_2))
};
let on_slope = |p: Vec3| ((p.z + p.x / 4.0 - 7.5) / lean).abs() < 1e-8;
let on_slot = |p: Vec3| {
let wall = if p.x.abs() <= 4.0 {
p.y.abs() - 3.0
} else {
(p.x.abs() - 4.0).hypot(p.y) - 3.0
};
wall.abs() < 1e-8
};
let blends: Vec<_> = b
.face_ids()
.filter(|&f| b.face(f).names.iter().any(|n| n.feature == "foot"))
.collect();
assert!(blends.len() >= 4, "{} blend faces", blends.len());
for f in blends {
let Surface::Nurbs(s) = &b.face(f).surface else {
panic!("the blend is a NURBS surface");
};
let ((u0, u1), (v0, v1)) = s.domain();
for i in 0..=40 {
let u = u0 + (u1 - u0) * i as f64 / 40.0;
for j in 0..=8 {
let p = s.eval(u, v0 + (v1 - v0) * j as f64 / 8.0);
assert!((from_centres(p) - r).abs() < 1e-8, "{p:?}");
}
let (a, c) = (s.eval(u, v0), s.eval(u, v1));
assert!(
on_slot(a) && on_slope(c) || on_slope(a) && on_slot(c),
"{a:?} {c:?}"
);
}
}
}
fn sloped_d() -> Brep {
let d = Profile::outline(vec![
arc((0.0, 0.0), 3.0, FRAC_PI_2, PI, "round_side"),
line((0.0, -3.0), (0.0, 3.0), "back_u_min"),
]);
standing(&d, "d")
}
#[test]
fn a_rolling_ball_round_an_open_curved_edge_is_trimmed_by_the_face_at_each_end() {
let r = 1.0;
let lean = (1.0f64 + 1.0 / 16.0).sqrt();
let b = fillet(&sloped_d(), &[("d.round_side", "slope.side")], r, "foot").unwrap();
let b = unify(&b).unwrap();
assert!(check::check(&b).is_empty());
let plain = measure::volume(&unify(&sloped_d()).unwrap());
assert!(measure::volume(&b) > plain);
let blends: Vec<_> = b
.face_ids()
.filter(|&f| b.face(f).names.iter().any(|n| n.feature == "foot"))
.collect();
assert!(!blends.is_empty());
let mut on_back = 0;
for f in blends {
let Surface::Nurbs(s) = &b.face(f).surface else {
panic!("the blend is a NURBS surface");
};
let ((u0, u1), (v0, v1)) = s.domain();
for i in 0..=40 {
let u = u0 + (u1 - u0) * i as f64 / 40.0;
for j in 0..=8 {
let p = s.eval(u, v0 + (v1 - v0) * j as f64 / 8.0);
if p.x <= 0.0 {
assert!((from_lifted_circle(p, r * lean) - r).abs() < 1e-8, "{p:?}");
}
}
}
for &lp in &b.face(f).loops {
for &c in &b.lp(lp).coedges {
let e = b.coedge(c).edge;
let points = along(&b, e);
let on_plane = points.iter().all(|p| p.x.abs() < 1e-8);
let at_back = points.iter().any(|p| p.x.abs() < 1e-3);
let spline = matches!(b.edge(e).curve, Some(Curve::BSpline(_)));
if at_back && !spline {
continue;
}
if on_plane {
on_back += 1;
}
}
}
}
assert!(on_back >= 2, "{on_back} trim edges on the back");
}
fn sloped_bitten() -> Brep {
let (round, bite) = (2.4f64.atan2(1.8), 2.4f64.atan2(3.2));
let boss = Profile::outline(vec![
arc((0.0, 0.0), 3.0, round, TAU - 2.0 * round, "round_side"),
arc((5.0, 0.0), 4.0, bite - PI, -2.0 * bite, "bite_side"),
]);
standing(&boss, "boss")
}
#[test]
fn a_rolling_ball_round_an_open_curved_edge_ends_on_a_cylinder() {
let r = 1.0;
let lean = (1.0f64 + 1.0 / 16.0).sqrt();
let b = fillet(
&sloped_bitten(),
&[("boss.round_side", "slope.side")],
r,
"foot",
)
.unwrap();
let b = unify(&b).unwrap();
assert!(check::check(&b).is_empty());
let plain = measure::volume(&unify(&sloped_bitten()).unwrap());
assert!(measure::volume(&b) > plain);
let off_back = |p: Vec3| (p.x - 5.0).hypot(p.y) - 4.0;
let mut on_back = 0;
for f in b.face_ids() {
if !b.face(f).names.iter().any(|n| n.feature == "foot") {
continue;
}
let Surface::Nurbs(s) = &b.face(f).surface else {
panic!("the blend is a NURBS surface");
};
let ((u0, u1), (v0, v1)) = s.domain();
for i in 0..=40 {
let u = u0 + (u1 - u0) * i as f64 / 40.0;
for j in 0..=8 {
let p = s.eval(u, v0 + (v1 - v0) * j as f64 / 8.0);
if off_back(p) >= 0.0 {
assert!((from_lifted_circle(p, r * lean) - r).abs() < 1e-8, "{p:?}");
}
}
}
for &lp in &b.face(f).loops {
for &c in &b.lp(lp).coedges {
let e = b.coedge(c).edge;
if !matches!(b.edge(e).curve, Some(Curve::BSpline(_))) {
continue;
}
if along(&b, e).iter().all(|&p| off_back(p).abs() < 1e-8) {
on_back += 1;
}
}
}
}
assert!(on_back >= 2, "{on_back} trim edges on the back");
}