-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
55 lines (45 loc) · 2.54 KB
/
main.rs
File metadata and controls
55 lines (45 loc) · 2.54 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
///
/// brain_export -- neuroformats.rs example application that exports a brain mesh with vertex colors based on the sulcal depth.
///
/// This file is part of neuroformats.rs, see https://github.com/dfsp-spirit/neuroformats-rs
///
/// To run this application, run 'cargo run --release' in the examples/brain_export directory.
fn main() {
println!("=====[ brain_export -- neuroformats.rs mesh export example application ]=====");
let lh_surf_file = "../../resources/subjects_dir/subject1/surf/lh.white";
let rh_surf_file = "../../resources/subjects_dir/subject1/surf/rh.white";
let lh_sulc_file = "../../resources/subjects_dir/subject1/surf/lh.sulc";
let rh_sulc_file = "../../resources/subjects_dir/subject1/surf/rh.sulc";
println!("Reading meshes...");
let lh_surf = neuroformats::read_surf(lh_surf_file).unwrap();
let rh_surf = neuroformats::read_surf(rh_surf_file).unwrap();
println!(
"Computing vertex colors using viridis colormap based on sulcal depth per-vertex values..."
);
let lh_colors = lh_surf.colors_from_curv_file(lh_sulc_file).unwrap();
let rh_colors = rh_surf.colors_from_curv_file(rh_sulc_file).unwrap();
println!("Constructing and centering merged mesh and colors from both hemisperes......");
let mut brain = lh_surf.mesh.merge(&rh_surf.mesh);
brain.move_to(brain.center().unwrap()); // center the mesh at origin
// merge the colors
let brain_colors = lh_colors
.iter()
.chain(rh_colors.iter())
.copied()
.collect::<Vec<_>>();
// get path of current directory as &path::Path
let current_dir = std::env::current_dir().unwrap();
const EXPORT_FILE: &str = "brainmesh_sulc.ply";
let export_path = current_dir.join(EXPORT_FILE);
let export_path = export_path.to_str().unwrap();
let ply_repr = brain.to_ply(Some(&brain_colors));
std::fs::write(export_path, ply_repr).expect("Unable to write vertex-colored PLY mesh file");
println!("Exported vertex-colored PLY mesh to: {}", export_path);
let gltf_repr = brain.to_gltf(Some(&brain_colors));
let gltf_export_path = current_dir.join("brainmesh_sulc.gltf");
let gltf_export_path = gltf_export_path.to_str().unwrap();
std::fs::write(gltf_export_path, gltf_repr)
.expect("Unable to write vertex-colored GLTF mesh file");
println!("Exported vertex-colored GLTF mesh to: {}", gltf_export_path);
println!("Note: You can view the meshes with a mesh viewer software like Blender or MeshLab. If you have MeshLab installed, just run: `meshlab {}`", EXPORT_FILE);
}