-
-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathdecomp.rs
More file actions
106 lines (93 loc) · 3.27 KB
/
Copy pathdecomp.rs
File metadata and controls
106 lines (93 loc) · 3.27 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use kiss3d::parry3d::shape::TriMesh;
use kiss3d::parry3d::transformation;
use kiss3d::parry3d::transformation::vhacd::VHACDParameters;
use kiss3d::prelude::*;
use rand::random;
use std::env;
use std::path::Path;
use std::str::FromStr;
use web_time::Instant;
fn usage(exe_name: &str) {
println!("Usage: {} obj_file scale clusters concavity", exe_name);
println!();
println!("Options:");
println!(" obj_file - the obj file to decompose.");
println!(" scale - the scale to apply to the displayed model.");
println!(" concavity - the maximum concavity accepted by the decomposition.");
}
#[kiss3d::main]
async fn main() {
/*
* Parse arguments.
*/
let mut args = env::args();
let exname = args.next().unwrap();
if args.len() != 3 {
usage(&exname[..]);
return;
}
let path = &args.next().unwrap()[..];
let scale: f32 = FromStr::from_str(&args.next().unwrap()[..]).unwrap();
let concavity: f32 = FromStr::from_str(&args.next().unwrap()[..]).unwrap();
let scale = Vec3::splat(scale);
/*
* Create the window.
*/
let mut window = Window::new("Kiss3d: convex decomposition").await;
let mut camera = OrbitCamera3d::new(Vec3::new(0.0, 0.0, 5.0), Vec3::ZERO);
let mut scene = SceneNode3d::empty();
scene
.add_light(Light::point(100.0))
.set_position(Vec3::new(0.0, 10.0, 10.0));
/*
* Convex decomposition.
*/
let obj_path = Path::new(path);
let mtl_path = Path::new("none");
let teapot = obj::parse_file(obj_path, mtl_path, "none").unwrap();
scene
.add_obj(obj_path, mtl_path, scale)
.set_surface_rendering_activation(false);
let mut total_time = 0.0f64;
for &(_, ref mesh, _) in teapot.iter() {
match mesh.to_render_mesh() {
Some(mut trimesh) => {
trimesh.split_index_buffer(true);
let idx: Vec<[u32; 3]> = trimesh
.indices
.as_split()
.iter()
.map(|idx| [idx[0][0], idx[1][0], idx[2][0]])
.collect();
let coords = trimesh.coords.clone();
let begin = Instant::now();
let params = VHACDParameters {
concavity,
..VHACDParameters::default()
};
let decomp = transformation::vhacd::VHACD::decompose(¶ms, &coords, &idx, true);
let elapsed = begin.elapsed();
total_time =
elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1000000000.0;
for (vtx, idx) in decomp.compute_exact_convex_hulls(&coords, &idx) {
let r = random();
let g = random();
let b = random();
if let Ok(trimesh) = TriMesh::new(vtx, idx) {
scene
.add_trimesh(trimesh, scale, true)
.set_color(Color::new(r, g, b, 1.0));
}
}
}
None => {}
}
}
println!("Decomposition time: {}", total_time);
/*
*
* Rendering.
*
*/
while window.render_3d(&mut scene, &mut camera).await {}
}