use cadrum::{DVec3, Error, Solid};
fn hollow_cube() -> Result<Solid, Error> {
let cube = Solid::cube(8.0, 8.0, 8.0);
let top = cube.iter_face().last().expect("cube has faces");
cube.shell(-1.0, [top])
}
fn sealed_cube() -> Result<Solid, Error> {
let cube = Solid::cube(8.0, 8.0, 8.0);
cube.shell(-1.0, std::iter::empty::<&cadrum::Face>())
}
fn halved_shelled_torus(thickness: f64) -> Result<Solid, Error> {
let torus = Solid::torus(6.0, 2.0, DVec3::Y);
let cutter = Solid::half_space(DVec3::ZERO, -DVec3::Z);
let cutter_face_ids: std::collections::HashSet<u64> =
cutter.iter_face().map(|f| f.id()).collect();
let half: Solid = (&torus * &cutter).build()?;
let from_cutter: std::collections::HashSet<u64> = half
.iter_history()
.filter_map(|[post, src]| cutter_face_ids.contains(&src).then_some(post))
.collect();
half.shell(thickness, half.iter_face().filter(|f| from_cutter.contains(&f.id())))
}
fn main() -> Result<(), Error> {
let example_name = std::path::Path::new(file!()).file_stem().unwrap().to_str().unwrap();
let result = [
hollow_cube()?.color("#d0a878"),
sealed_cube()?.color("#6fbf73").translate(DVec3::Y * 10.0),
halved_shelled_torus(1.0)?.color("#ff5e00").translate(DVec3::X * 18.0),
halved_shelled_torus(-1.0)?.color("#0052ff").translate(DVec3::X * 18.0 + DVec3::Y * 10.0),
];
Solid::write_step(&result, &mut std::fs::File::create(format!("{example_name}.step")).unwrap())?;
let scene = Solid::mesh(&result, 0.2)?.scene(DVec3::new(1.0, 1.0, 2.0), DVec3::Z, true, true);
scene.write_svg(&mut std::fs::File::create(format!("{example_name}.svg")).unwrap())?;
scene.write_png([640, 640], &mut std::fs::File::create(format!("{example_name}.png")).unwrap())?;
println!("wrote {example_name}.step / {example_name}.svg / {example_name}.png");
Ok(())
}