use std::f64::consts::PI;
use springrep::{
boolean::{difference, intersection, union},
check,
math::{Frame, Point3, Transform, Vec3},
measure, primitive, step,
topo::Brep,
};
fn teeth() -> (Brep, Brep) {
let read = |bytes: &[u8], name: &str| {
let import = step::read(bytes, name).unwrap();
assert!(import.rejected.is_empty(), "{:?}", import.rejected);
import.brep
};
(
read(include_bytes!("fixtures/grazing-tooth-a.step"), "a"),
read(include_bytes!("fixtures/grazing-tooth-b.step"), "b"),
)
}
#[test]
fn two_flanks_that_graze_share_the_same_sliver_whichever_is_first() {
let (a, b) = teeth();
let (one, other) = (intersection(&a, &b).unwrap(), intersection(&b, &a).unwrap());
assert!(check::check(&one).is_empty() && check::check(&other).is_empty());
let (v, w) = (measure::volume(&one), measure::volume(&other));
assert!((2.0e-6..3.0e-6).contains(&v), "{v}");
assert!((v - w).abs() < 1e-3 * v, "{v} against {w}");
}
#[test]
fn a_shaft_a_hair_off_its_bores_axis_shares_the_crescent_between_them() {
let shaft = primitive::cylinder(
&Frame::new(Vec3::new(0.0, 0.0, -5.0), Vec3::Z, Vec3::X).unwrap(),
2.0,
20.0,
"shaft",
)
.unwrap();
let outer = primitive::cylinder(&Frame::WORLD, 2.9, 8.0, "outer").unwrap();
let bore = primitive::cylinder(
&Frame::new(Vec3::new(0.0, 0.0, -1.0), Vec3::Z, Vec3::X).unwrap(),
2.0,
10.0,
"bore",
)
.unwrap();
let ring = difference(&outer, &bore).unwrap();
for (d, tilt) in [(4e-4, 0.0), (1e-5, 0.0), (4e-4, 1.6e-5)] {
let mut placed = ring.clone();
placed.transform(&Transform::rotation(
Point3::new(0.0, 0.0, 4.0),
Vec3::X,
tilt,
));
placed.transform(&Transform::translation(Vec3::new(0.0, d, 0.0)));
let shared = intersection(&shaft, &placed).unwrap_or_else(|e| panic!("{d} {tilt}: {e}"));
assert!(check::check(&shared).is_empty(), "{d} {tilt}");
let want = 2.0 * 2.0 * d * 8.0;
let got = measure::volume(&shared);
assert!(
(got - want).abs() < 1e-5 * want,
"{d} {tilt}: {got} against {want}"
);
}
}
#[test]
fn a_rib_a_hair_into_the_notch_it_stands_in_only_touches_it() {
let along = |r: f64| {
let a = 1.25 * PI;
Vec3::new(r * a.cos(), r * a.sin(), 0.0)
};
let out = Vec3::new(1.0, 1.0, 0.0).unit().unwrap();
let disc = primitive::cylinder(
&Frame::new(Vec3::ZERO, Vec3::Z, out).unwrap(),
42.5,
16.0,
"disc",
)
.unwrap();
let notch = primitive::cylinder(
&Frame::new(along(43.0 + 3.16e-9) - Vec3::Z, Vec3::Z, -out).unwrap(),
2.0,
18.0,
"notch",
)
.unwrap();
let notched = difference(&disc, ¬ch).unwrap();
let rib = primitive::cylinder(
&Frame::new(along(42.95), Vec3::Z, -Vec3::X).unwrap(),
1.95,
20.0,
"rib",
)
.unwrap();
let shared = intersection(¬ched, &rib).unwrap();
assert!(check::check(&shared).is_empty());
assert!(
measure::volume(&shared).abs() < 1e-12,
"{}",
measure::volume(&shared)
);
}
#[test]
fn a_press_fit_pin_a_hair_off_its_bores_axis_shares_the_ring_of_its_interference() {
let plate = primitive::block(
&Frame::new(Vec3::new(-10.0, 0.0, -10.0), Vec3::Y, Vec3::Z).unwrap(),
Vec3::new(20.0, 20.0, 4.7),
"plate",
)
.unwrap();
let bore = primitive::cylinder(
&Frame::new(Vec3::new(0.0, -1.0, 0.0), Vec3::Y, Vec3::X).unwrap(),
3.95,
10.0,
"bore",
)
.unwrap();
let holed = difference(&plate, &bore).unwrap();
let o = Vec3::new(3e-7, 0.0, 0.0);
let chamfer = primitive::cone(
&Frame::new(o, Vec3::Y, Vec3::X).unwrap(),
3.5,
4.0,
0.5,
"pin",
)
.unwrap();
let shank = primitive::cylinder(
&Frame::new(o + Vec3::new(0.0, 0.5, 0.0), Vec3::Y, Vec3::X).unwrap(),
4.0,
20.0,
"pin",
)
.unwrap();
let pin = union(&chamfer, &shank).unwrap();
let shared = intersection(&holed, &pin).unwrap();
assert!(check::check(&shared).is_empty());
let want = PI * (16.0 - 15.6025) * 4.2 + PI * (0.05f64.powi(3) / 3.0 + 3.95 * 0.0025);
let got = measure::volume(&shared);
assert!((got - want).abs() < 1e-6 * want, "{got} against {want}");
}
#[test]
fn an_insert_turned_a_hair_off_its_holes_axis_shares_the_crescent_between_them() {
let plate = primitive::block(
&Frame::new(Vec3::new(-10.0, 0.0, -10.0), Vec3::Y, Vec3::Z).unwrap(),
Vec3::new(20.0, 20.0, 8.0),
"plate",
)
.unwrap();
let bore = primitive::cylinder(
&Frame::new(Vec3::new(0.0, -1.0, 0.0), Vec3::Y, Vec3::X).unwrap(),
2.9,
10.0,
"bore",
)
.unwrap();
let mouth = primitive::cone(
&Frame::new(Vec3::new(0.0, -0.5, 0.0), Vec3::Y, Vec3::X).unwrap(),
3.9,
2.9,
1.0,
"mouth",
)
.unwrap();
let holed = difference(&difference(&plate, &bore).unwrap(), &mouth).unwrap();
let d = 5.2e-6;
for lift in [0.0, -4.2e-6, 0.3] {
let insert = primitive::cylinder(
&Frame::new(
Vec3::new(0.0, 0.5 + lift, d),
Vec3::new(2e-10, 1.0, 0.0),
-Vec3::X,
)
.unwrap(),
2.9,
6.0,
"insert",
)
.unwrap();
let shared = intersection(&holed, &insert).unwrap_or_else(|e| panic!("{lift}: {e}"));
assert!(check::check(&shared).is_empty(), "{lift}");
let want = 2.0 * 2.9 * d * 6.0;
let got = measure::volume(&shared);
assert!(
(got - want).abs() < 1e-3 * want,
"{lift}: {got} against {want}"
);
}
}
#[test]
fn an_insert_a_hair_off_its_holes_axis_with_its_end_a_hair_into_the_chamfer_is_valid() {
let plate = primitive::block(
&Frame::new(Vec3::new(-10.0, 0.0, -10.0), Vec3::Y, Vec3::Z).unwrap(),
Vec3::new(20.0, 20.0, 8.0),
"plate",
)
.unwrap();
let bore = primitive::cylinder(
&Frame::new(Vec3::new(0.0, -1.0, 0.0), Vec3::Y, Vec3::X).unwrap(),
2.9,
10.0,
"bore",
)
.unwrap();
let mouth = primitive::cone(
&Frame::new(Vec3::new(0.0, -0.5, 0.0), Vec3::Y, Vec3::X).unwrap(),
3.9,
2.9,
1.0,
"mouth",
)
.unwrap();
let holed = difference(&difference(&plate, &bore).unwrap(), &mouth).unwrap();
let below = primitive::block(
&Frame::new(Vec3::new(-10.0, -1.0, -10.0), Vec3::Z, Vec3::X).unwrap(),
Vec3::new(20.0, 12.0, 12.0),
"below",
)
.unwrap();
let d = 5.2e-6;
let (s, c) = 5f64.to_radians().sin_cos();
let insert = primitive::cylinder(
&Frame::new(Vec3::new(d * c, 0.5 - 4.2e-6, d * s), Vec3::Y, -Vec3::X).unwrap(),
2.9,
6.0,
"insert",
)
.unwrap();
let insert = intersection(&insert, &below).unwrap();
let shared = intersection(&holed, &insert).unwrap();
assert!(check::check(&shared).is_empty());
let got = measure::volume(&shared);
assert!(got.abs() <= 2.0 * 2.9 * d * 6.0, "{got}");
}
#[test]
fn a_plane_a_hair_off_parallel_to_a_holes_axis_cuts_it_in_two_lines() {
let plate = primitive::block(&Frame::WORLD, Vec3::new(10.0, 10.0, 4.0), "plate").unwrap();
let tilt = 5.7e-10;
let hole = primitive::cylinder(
&Frame::new(
Vec3::new(5.0, 5.0, -1.0),
Vec3::new(0.0, tilt, 1.0),
Vec3::X,
)
.unwrap(),
1.6,
6.0,
"hole",
)
.unwrap();
let holed = difference(&plate, &hole).unwrap();
let below = primitive::block(
&Frame::new(Vec3::new(-1.0, -1.0, -1.0), Vec3::Z, Vec3::X).unwrap(),
Vec3::new(12.0, 6.5, 6.0),
"below",
)
.unwrap();
let cut = intersection(&holed, &below).unwrap();
assert!(check::check(&cut).is_empty());
let (r, d): (f64, f64) = (1.6, 0.5);
let segment = r * r * (d / r).acos() - d * (r * r - d * d).sqrt();
let want = 220.0 - 4.0 * (PI * r * r - segment);
let got = measure::volume(&cut);
assert!((got - want).abs() < 1e-9 * want, "{got} against {want}");
}