//! Draw two union-ed tori
//!
//! >>> cargo run --example tori
//!
//! Writes an .stl file to target/stl/heart.stl
use crater::{
    bounding::BoundingBox,
    csg::{
        algebra::CSGAlgebra,
        marching_cubes::{marching_cubes, MarchingCubesParams},
        surfaces::{IntoSurface, Surface3D},
        transformations::RotateAbout,
    },
    mesh::MeshCollection,
    utils::Parallel,
};

fn main() {
    let z_torus = -(Surface3D::Torus { r1: 1.0, r2: 2.0 }).into_surface();
    let y_torus = -(Surface3D::Torus { r1: 1.0, r2: 2.0 })
        .into_surface()
        .transform(RotateAbout(0, 2, std::f64::consts::FRAC_PI_2));
    // Run marching cubes to render this shape
    let resolution = 50;
    let bound = 3.0;
    let params = MarchingCubesParams {
        region: z_torus | y_torus,
        bounds: BoundingBox {
            min: [-bound, -bound, -bound],
            max: [bound, bound, bound],
        },
        resolution: (resolution, resolution, resolution),
        algebra: CSGAlgebra::default(),
    };
    let mesh: MeshCollection = marching_cubes::<Parallel>(&params).into();
    let mut path_buf = std::path::PathBuf::from("./target/test_renderings/");
    path_buf.push("tori");
    path_buf.set_extension("stl");
    std::fs::create_dir_all(path_buf.parent().expect("Failed to get parent directory"))
        .expect("Failed to create directory");
    mesh.write_stl(path_buf.to_str().unwrap())
        .expect("Failed to write csg.stl");
}