use std::f64::consts::{PI, TAU};
use springrep::{
blend::{EdgeName, chamfer, fillet_named},
boolean::{difference, unify, union},
check,
math::{Frame, Vec2, Vec3},
measure,
mesh::{self, MeshOptions},
profile::Profile,
sweep::{extrude, revolve},
topo::Brep,
};
const THROUGH: f64 = 1000.0;
fn up(z: f64) -> Frame {
Frame::new(Vec3::new(0.0, 0.0, z), Vec3::Z, Vec3::X).unwrap()
}
fn block(width: f64) -> Brep {
extrude(
&Profile::rect(Vec2::ZERO, width, width),
&up(0.0),
10.0,
"block",
)
.unwrap()
}
fn watertight(b: &Brep) -> bool {
let opt = MeshOptions {
linear: 0.01,
angular: 0.2,
};
let mesh = mesh::tessellate(b, &opt).expect("the result meshes");
let solid = b.solid_ids().next().expect("one solid");
mesh::report(b, &mesh, solid).watertight()
}
fn faces_of(b: &Brep, kind: &str) -> usize {
b.face_ids()
.filter(|&f| b.face(f).surface.kind() == kind)
.count()
}
#[test]
fn a_chamfer_bevels_the_named_edge() {
let cube = extrude(&Profile::rect(Vec2::ZERO, 4.0, 4.0), &up(0.0), 4.0, "block").unwrap();
let b = chamfer(&cube, &[("block.end", "block.u_max")], 1.0, "ch").unwrap();
let b = unify(&b).unwrap();
let got = measure::volume(&b);
assert!((got - 62.0).abs() < 1e-9 * 62.0, "{got}");
assert_eq!(b.name_counts().get("ch.bevel_0"), Some(&1));
}
#[test]
fn a_chamfer_picks_a_revolved_shafts_edge_by_its_profile_names() {
let rz = Frame::new(Vec3::ZERO, -Vec3::Y, Vec3::X).unwrap();
let outline = Profile::polygon(
&[
Vec2::new(0.0, 0.0),
Vec2::new(10.0, 0.0),
Vec2::new(10.0, 10.0),
Vec2::new(0.0, 10.0),
],
&["face_a", "journal", "face_b", "axis"],
);
let shaft = revolve(&outline, &rz, Vec3::ZERO, rz.y, TAU, "shaft").unwrap();
let b = chamfer(&shaft, &[("shaft.face_a", "shaft.journal")], 1.0, "break").unwrap();
let b = unify(&b).unwrap();
let want = 1000.0 * PI - 2.0 * PI * (10.0 - 1.0 / 3.0) * 0.5;
assert!((measure::volume(&b) - want).abs() < 1e-9 * want);
let names = b.name_counts();
for n in ["shaft.face_a", "shaft.journal", "break.bevel_0"] {
assert_eq!(names.get(n), Some(&1), "{n} in {names:?}");
}
}
fn keyed_bore(chamfered: bool) -> Brep {
let plate = extrude(
&Profile::rect(Vec2::ZERO, 20.0, 20.0),
&up(0.0),
10.0,
"plate",
)
.unwrap();
let bore = extrude(
&Profile::named_circle(Vec2::ZERO, 8.0, "wall"),
&up(0.0),
THROUGH,
"bore",
)
.unwrap();
let key = extrude(
&Profile::rect(Vec2::new(4.25, 0.0), 1.5, 2.0),
&up(0.0),
10.0,
"key",
)
.unwrap();
let mut b = difference(&difference(&plate, &bore).unwrap(), &key).unwrap();
if chamfered {
b = chamfer(&b, &[("bore.wall", "plate.start")], 0.5, "break").unwrap();
}
unify(&b).unwrap()
}
#[test]
fn a_chamfer_round_most_of_a_bore_stops_at_the_keyway_walls() {
let removed = measure::volume(&keyed_bore(false)) - measure::volume(&keyed_bore(true));
let n = 2000;
let f = |r: f64| (4.5 - r) * r * (TAU - 2.0 * (1.0 / r).asin());
let h = 0.5 / n as f64;
let mut want = f(4.0) + f(4.5);
for i in 1..n {
want += f(4.0 + h * i as f64) * if i % 2 == 1 { 4.0 } else { 2.0 };
}
want *= h / 3.0;
assert!((removed - want).abs() < 1e-9 * want, "{removed} vs {want}");
}
fn overhang() -> Brep {
let boss = extrude(
&Profile::rect(Vec2::new(10.0, 0.0), 10.0, 10.0),
&up(10.0),
5.0,
"o",
)
.unwrap();
union(&block(20.0), &boss).unwrap()
}
#[test]
fn an_exact_edge_picks_one_of_the_edges_two_surfaces_meet_in() {
let cut = 20.0 * (1.0 - PI / 4.0);
for (edges, want) in [
(
[
("o.v_max", "o.start"),
("o.v_min", "o.start"),
("block.u_max", "block.end"),
],
4500.0 - cut,
),
(
[
("block.end", "o.v_max"),
("block.end", "o.v_min"),
("o.start", "block.u_max"),
],
4500.0 + cut,
),
] {
let exact = edges.map(|(a, b)| EdgeName { a, b, exact: true });
let b = unify(&fillet_named(&overhang(), &exact, 1.0, "f").unwrap()).unwrap();
assert!(check::check(&b).is_empty());
assert!(watertight(&b));
let got = measure::volume(&b);
assert!(
(got - want).abs() < 1e-12 * want,
"{edges:?}: volume {got} for {want}"
);
assert_eq!(faces_of(&b, "cylinder"), if want < 4500.0 { 4 } else { 3 });
}
}