-
Notifications
You must be signed in to change notification settings - Fork 7
/
boolean.rs
51 lines (43 loc) · 1.66 KB
/
boolean.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use baby_shark::{
io::stl::{StlReader, StlWriter},
mesh::polygon_soup::data_structure::PolygonSoup,
voxel::prelude::*,
};
use nalgebra_glm::Vec3;
use std::path::Path;
fn main() {
let voxel_size = 0.2;
// Read bunny mesh
let bunny_mesh: PolygonSoup<f32> = StlReader::new()
.read_stl_from_file(Path::new("./assets/bunny.stl"))
.expect("Read mesh");
// Convert bunny mesh to volume
let bunny_volume = MeshToVolume::default()
.with_voxel_size(voxel_size)
.convert(&bunny_mesh)
.unwrap();
// Create a volume of boxes
let builder = VolumeBuilder::default().with_voxel_size(voxel_size);
let mut boxes = Volume::with_voxel_size(voxel_size);
for x in (-25..26).step_by(3) {
let x = x as f32;
let next_box = builder.cuboid(Vec3::new(x, -20.0, 0.0), Vec3::new(x + 1.0, 20.0, 50.0));
boxes = boxes.union(next_box);
}
// Perform boolean operations
let intersection_volume = bunny_volume.clone().intersect(boxes.clone());
let union_volume = bunny_volume.clone().union(boxes.clone());
let subtraction_volume = bunny_volume.subtract(boxes);
write_volume_to_stl(&intersection_volume, "intersection.stl");
write_volume_to_stl(&union_volume, "union.stl");
write_volume_to_stl(&subtraction_volume, "subtraction.stl");
}
fn write_volume_to_stl(volume: &Volume, path: &str) {
let vertices = MarchingCubesMesher::default()
.with_voxel_size(volume.voxel_size())
.mesh(volume);
let mesh = PolygonSoup::from_vertices(vertices);
StlWriter::new()
.write_stl_to_file(&mesh, Path::new(path))
.expect("Should write mesh to STL");
}