use super::*;
use crate::{measure, profile::Profile, sweep, topo::FaceName};
fn cube() -> Brep {
let p = Profile::rect(Vec2::ZERO, 10.0, 10.0);
sweep::extrude(&p, &Frame::WORLD, 10.0, "box").unwrap()
}
fn face(b: &Brep, name: &str) -> FaceId {
let n = FaceName::new("box", name);
b.face_ids()
.find(|&f| b.face(f).names.contains(&n))
.unwrap()
}
fn frame(b: &Brep, f: FaceId) -> Frame {
plane_of(&b.face(f).surface).unwrap()
}
#[test]
fn raising_the_top_stretches_the_sides() {
let b = cube();
let top = face(&b, "end");
let fr = frame(&b, top).translated(Vec3::Z * 5.0);
let out = move_planes(&b, &HashMap::from([(top, fr)])).unwrap();
assert!((measure::volume(&out) - 1500.0).abs() < 1e-9);
assert_eq!(out.face_count(), 6);
}
#[test]
fn tilting_a_side_about_its_foot_takes_off_a_wedge() {
let b = cube();
let side = face(&b, "u_max");
let old = frame(&b, side);
let n = old.z;
let tilted = (n * 10.0 + Vec3::Z * 2.0).unit().unwrap();
let foot = old.origin - Vec3::Z * old.origin.z;
let fr = Frame::new(foot, tilted, old.x).unwrap();
let out = move_planes(&b, &HashMap::from([(side, fr)])).unwrap();
assert!((measure::volume(&out) - 900.0).abs() < 1e-9);
}
#[test]
fn a_draft_past_the_apex_is_refused() {
let p = Profile::rect(Vec2::ZERO, 20.0, 20.0);
let b = sweep::extrude(&p, &Frame::WORLD, 10.0, "box").unwrap();
let drafted = |a: f64| {
let mut moved = HashMap::new();
for name in ["u_min", "u_max", "v_min", "v_max"] {
let f = face(&b, name);
let fr = frame(&b, f);
let n = if b.face(f).reversed { -fr.z } else { fr.z };
let tilted = n * a.cos() + Vec3::Z * a.sin();
let z = if b.face(f).reversed { -tilted } else { tilted };
let foot = fr.origin - Vec3::Z * fr.origin.z;
moved.insert(f, Frame::new(foot, z, fr.x).unwrap());
}
move_planes(&b, &moved)
};
let top = 20.0 - 20.0 * 0.5f64.tan();
let want = 10.0 / 3.0 * (400.0 + top * top + 20.0 * top);
assert!((measure::volume(&drafted(0.5).unwrap()) - want).abs() < 1e-9);
for a in [std::f64::consts::FRAC_PI_4 + 1e-6, 0.79, 0.9] {
assert!(matches!(drafted(a), Err(Error::InvalidResult(_))), "{a}");
}
}
#[test]
fn a_curved_face_at_a_moved_corner_is_unsupported() {
let p = Profile::circle(Vec2::ZERO, 10.0);
let b = sweep::extrude(&p, &Frame::WORLD, 10.0, "box").unwrap();
let top = face(&b, "end");
let fr = frame(&b, top).translated(Vec3::Z * 5.0);
assert!(matches!(
move_planes(&b, &HashMap::from([(top, fr)])),
Err(Error::Unsupported(_))
));
}
#[test]
fn a_tip_moved_through_the_other_tip_is_refused() {
let pts = [
(0.0, 0.0),
(30.0, 0.0),
(30.0, 10.0),
(22.0, 10.0),
(22.0, 6.0),
(6.0, 6.0),
(6.0, 24.0),
(24.0, 24.0),
(24.0, 20.0),
(28.0, 20.0),
(28.0, 24.0),
(30.0, 24.0),
(30.0, 30.0),
(0.0, 30.0),
]
.map(|(x, y)| Vec2::new(x, y));
let names: Vec<String> = (0..pts.len()).map(|i| format!("e{i}")).collect();
let names: Vec<&str> = names.iter().map(String::as_str).collect();
let b = sweep::extrude(&Profile::polygon(&pts, &names), &Frame::WORLD, 5.0, "box").unwrap();
let tip = face(&b, "e2");
let raised = |dy: f64| {
let fr = frame(&b, tip).translated(Vec3::Y * dy);
move_planes(&b, &HashMap::from([(tip, fr)]))
};
let v0 = measure::volume(&b);
assert!((measure::volume(&raised(5.0).unwrap()) - v0 - 200.0).abs() < 1e-9);
assert!(matches!(raised(12.0), Err(Error::InvalidResult(_))));
}
#[test]
fn a_spline_walled_prism_offset_inward_holds_steiners_area() {
let pts: Vec<Vec2> = (0..24)
.map(|k| {
let a = std::f64::consts::TAU * k as f64 / 24.0;
Vec2::new(12.0 * a.cos(), 8.0 * a.sin())
})
.collect();
let p = Profile::points(&pts, crate::profile::PointsInterp::PeriodicSpline, "wall").unwrap();
let b = sweep::extrude(&p, &Frame::WORLD, 10.0, "can").unwrap();
let area = measure::volume(&b) / 10.0;
let perimeter = (measure::area(&b) - 2.0 * area) / 10.0;
let by: HashMap<FaceId, f64> = b.face_ids().map(|f| (f, -1.0)).collect();
let moved = offset_faces(&b, &by).unwrap();
assert!(check::check(&moved).is_empty());
let want = (area - perimeter + std::f64::consts::PI) * 8.0;
let got = measure::volume(&moved);
assert!((got - want).abs() < 1e-8 * want, "{got} for {want}");
}
#[test]
fn a_spline_sphere_octant_offset_is_the_octant_of_the_grown_sphere() {
let s = crate::geom::octant(5.0);
let crate::geom::Surface::Nurbs(n) = &s else {
panic!("the octant is a spline")
};
let ((u0, u1), (v0, v1)) = n.domain();
let off = offset_fitted(&s, 0.5, ((u0, u1), (v0, v1))).unwrap();
let mut worst = 0.0f64;
for i in 0..=37 {
for j in 0..=37 {
let (u, v) = (
u0 + (u1 - u0) * i as f64 / 37.0,
v0 + (v1 - v0) * j as f64 / 37.0,
);
worst = worst.max((off.eval(u, v).norm() - 5.5).abs());
}
}
assert!(worst <= 2.0 * OFFSET_FIT, "{worst:e}");
}
fn drafted_box(walls: &[&str]) -> Result<Brep> {
let p = Profile::rect(Vec2::ZERO, 4.0, 6.0);
let b = sweep::extrude(&p, &Frame::WORLD, 4.0, "box").unwrap();
let faces: Vec<FaceId> = walls.iter().map(|w| face(&b, w)).collect();
draft(&b, &faces, &Frame::WORLD, std::f64::consts::FRAC_PI_4)
}
#[test]
fn walls_drafted_until_they_meet_close_over_the_top() {
let out = drafted_box(&["u_min", "u_max"]).unwrap();
assert!(check::check(&out).is_empty());
assert!((measure::volume(&out) - 24.0).abs() < 1e-9);
assert_eq!(out.face_count(), 5);
}
#[test]
fn all_walls_drafted_until_two_meet_make_a_hipped_roof() {
let out = drafted_box(&["u_min", "u_max", "v_min", "v_max"]).unwrap();
assert!(check::check(&out).is_empty());
assert!((measure::volume(&out) - 56.0 / 3.0).abs() < 1e-9);
assert_eq!(out.face_count(), 5);
}
#[test]
fn a_step_raised_level_with_the_top_closes_the_riser() {
let pts = [
(0.0, 0.0),
(10.0, 0.0),
(10.0, 2.0),
(4.0, 2.0),
(4.0, 6.0),
(0.0, 6.0),
]
.map(|(x, y)| Vec2::new(x, y));
let names = ["e0", "e1", "e2", "e3", "e4", "e5"];
let b = sweep::extrude(&Profile::polygon(&pts, &names), &Frame::WORLD, 3.0, "box").unwrap();
let out = offset_faces(&b, &HashMap::from([(face(&b, "e2"), 4.0)])).unwrap();
assert!(check::check(&out).is_empty());
assert!((measure::volume(&out) - 180.0).abs() < 1e-9);
assert_eq!(out.face_count(), 6);
}
#[test]
fn rounds_offset_past_their_radius_close_to_sharp_corners() {
use crate::profile::{NamedSegment, ProfileLoop, Segment};
use std::f64::consts::{FRAC_PI_2, PI};
let (w, d, r) = (10.0, 8.0, 1.0);
let (x0, y0, x1, y1) = (-w / 2.0, -d / 2.0, w / 2.0, d / 2.0);
let line = |a: (f64, f64), b: (f64, f64), n: &str| {
NamedSegment::new(
Segment::Line {
a: Vec2::new(a.0, a.1),
b: Vec2::new(b.0, b.1),
},
n,
)
};
let arc = |c: (f64, f64), start: f64, n: &str| {
NamedSegment::new(
Segment::Arc {
center: Vec2::new(c.0, c.1),
radius: r,
start,
sweep: FRAC_PI_2,
},
n,
)
};
let p = Profile {
loops: vec![ProfileLoop {
segments: vec![
line((x0 + r, y0), (x1 - r, y0), "s0"),
arc((x1 - r, y0 + r), -FRAC_PI_2, "r0"),
line((x1, y0 + r), (x1, y1 - r), "s1"),
arc((x1 - r, y1 - r), 0.0, "r1"),
line((x1 - r, y1), (x0 + r, y1), "s2"),
arc((x0 + r, y1 - r), FRAC_PI_2, "r2"),
line((x0, y1 - r), (x0, y0 + r), "s3"),
arc((x0 + r, y0 + r), PI, "r3"),
],
}],
};
let b = sweep::extrude(&p, &Frame::WORLD, 6.0, "box").unwrap();
let by: HashMap<FaceId, f64> = b.face_ids().map(|f| (f, -1.5)).collect();
let out = offset_faces(&b, &by).unwrap();
assert!(check::check(&out).is_empty());
assert!((measure::volume(&out) - 105.0).abs() < 1e-9);
assert_eq!(out.face_count(), 6);
}
#[test]
fn a_wall_moved_across_its_slot_closes_it() {
let pts = [
(0.0, 0.0),
(10.0, 0.0),
(10.0, 6.0),
(7.0, 6.0),
(7.0, 2.0),
(3.0, 2.0),
(3.0, 6.0),
(0.0, 6.0),
]
.map(|(x, y)| Vec2::new(x, y));
let names = ["e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"];
let b = sweep::extrude(&Profile::polygon(&pts, &names), &Frame::WORLD, 3.0, "box").unwrap();
let out = offset_faces(&b, &HashMap::from([(face(&b, "e5"), 4.0)])).unwrap();
assert!(check::check(&out).is_empty());
assert!((measure::volume(&out) - 180.0).abs() < 1e-9);
assert_eq!(out.face_count(), 6);
}
fn ball(f: &Frame, r: f64) -> Brep {
use crate::profile::{NamedSegment, ProfileLoop, Segment};
let seg = |segment, name: &str| NamedSegment::new(segment, name);
let skin = Segment::Arc {
center: Vec2::ZERO,
radius: r,
start: -std::f64::consts::FRAC_PI_2,
sweep: std::f64::consts::PI,
};
let axis = Segment::Line {
a: Vec2::new(0.0, r),
b: Vec2::new(0.0, -r),
};
let half = Profile {
loops: vec![ProfileLoop {
segments: vec![seg(skin, "skin"), seg(axis, "axis")],
}],
};
let meridian = Frame::new(f.origin, -f.y, f.x).unwrap();
sweep::revolve(&half, &meridian, f.origin, f.z, TAU, "sph").unwrap()
}
fn turned_frames() -> Vec<Frame> {
(0..200)
.map(|k| {
let a = k as f64 * 0.731;
let z = Vec3::new(
a.sin() * (1.7 * a).cos(),
a.sin() * (1.7 * a).sin(),
a.cos(),
);
let x = Vec3::new((2.3 * a).cos(), (2.3 * a).sin(), 0.4);
Frame::new(Vec3::new(1.5, -2.0, 3.0), z, x).unwrap()
})
.collect()
}
#[test]
fn a_hole_moved_further_than_its_radius_keeps_its_rims_turning_its_way() {
use crate::profile::{NamedSegment, ProfileLoop, Segment};
let mut p = Profile::rect(Vec2::new(6.0, 4.0), 12.0, 8.0);
p.loops.push(ProfileLoop {
segments: vec![NamedSegment::new(
Segment::Circle {
center: Vec2::new(6.0, 4.0),
radius: 1.8,
ccw: false,
},
"hole",
)],
});
let b = sweep::extrude(&p, &Frame::WORLD, 3.0, "box").unwrap();
let hole = face(&b, "hole");
let moved = b
.face(hole)
.surface
.transformed(&crate::math::Transform::translation(Vec3::new(
-3.4, -1.1, 0.0,
)));
let out = move_faces(&b, &HashMap::from([(hole, moved)])).unwrap();
assert!(check::check(&out).is_empty());
let want = 3.0 * (96.0 - std::f64::consts::PI * 1.8 * 1.8);
assert!((measure::volume(&out) - want).abs() < 1e-9 * want);
}
#[test]
fn a_ball_offset_keeps_its_seam_apart_in_any_frame() {
let (r, d): (f64, f64) = (5.358245536894633, 0.8586557324583324);
let want = 4.0 / 3.0 * std::f64::consts::PI * (r + d).powi(3);
for (k, f) in turned_frames().iter().enumerate() {
let b = ball(f, r);
let by: HashMap<FaceId, f64> = b.face_ids().map(|f| (f, d)).collect();
let grown = offset_faces(&b, &by).unwrap_or_else(|e| panic!("frame {k}: {e}"));
let got = measure::volume(&grown);
assert!(
(got - want).abs() < 1e-6 * want,
"frame {k}: {got} against {want}"
);
}
}