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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::path::{Path, PathBuf};

use meshx::algo::Merge;
use rayon::prelude::*;

#[macro_use]
pub mod utils;
#[macro_use]
pub mod attrib;
pub mod config;
pub mod export;
pub mod material;
pub mod mesh;
pub mod texture;

pub use attrib::*;
pub use material::*;
pub use texture::*;
pub use utils::*;

use mesh::{trimesh_f64_to_f32, Mesh};

#[derive(Clone, Copy, Debug)]
pub struct LoadConfig<'a> {
    pub attributes: &'a AttributeInfo,
    pub colors: &'a AttributeInfo,
    pub texcoords: &'a TextureAttributeInfo,
    pub material_attribute: &'a str,
    pub reverse: bool,
    pub invert_tets: bool,
}

pub fn load_meshes(
    mesh_meta: Vec<(String, usize, PathBuf)>,
    config: LoadConfig,
) -> Vec<(String, usize, Mesh, AttribTransfer)> {
    let process_attrib_error = |e| log::warn!("{}, Skipping...", e);
    mesh_meta
        .into_par_iter()
        .filter_map(|(name, frame, path)| {
            load_mesh(&path, config, process_attrib_error)
                .map(|(mesh, attrib_transfer)| (name, frame, mesh, attrib_transfer))
        })
        .collect()
}

pub fn load_mesh(
    path: &Path,
    config: LoadConfig,
    process_attrib_error: impl FnMut(attrib::AttribError),
) -> Option<(Mesh, AttribTransfer)> {
    let polymesh_tris = if let Ok(polymesh) = meshx::io::load_polymesh::<f64, _>(path) {
        trimesh_f64_to_f32(meshx::TriMesh::from(polymesh))
    } else if let Ok(polymesh) = meshx::io::load_polymesh::<f32, _>(path) {
        meshx::TriMesh::<f32>::from(polymesh)
    } else {
        meshx::TriMesh::default()
    };

    let mut tetmesh_tris = if let Ok(tetmesh) = meshx::io::load_tetmesh::<f64, _>(path) {
        trimesh_f64_to_f32(tetmesh.surface_trimesh())
    } else if let Ok(tetmesh) = meshx::io::load_tetmesh::<f32, _>(path) {
        meshx::TriMesh::<f32>::from(tetmesh.surface_trimesh())
    } else {
        meshx::TriMesh::default()
    };

    // Reverse triangles that came from tets. This is faster than actually inverting tets but
    // achieves the same result.
    if config.invert_tets {
        tetmesh_tris.reverse();
    }

    tetmesh_tris.merge(polymesh_tris);
    let mut mesh = Mesh::from(tetmesh_tris);

    if mesh.is_empty() {
        mesh = if let Ok(ptcloud) = meshx::io::load_pointcloud::<f64, _>(path) {
            ptcloud.into()
        } else if let Ok(ptcloud) = meshx::io::load_pointcloud::<f32, _>(path) {
            ptcloud.into()
        } else {
            return None;
        };
    }

    if config.reverse {
        mesh.reverse();
    }

    let attrib_transfer = clean_attributes(
        &mut mesh,
        config.attributes,
        config.colors,
        config.texcoords,
        config.material_attribute,
        process_attrib_error,
    );

    Some((mesh, attrib_transfer))
}

#[cfg(test)]
mod tests {
    use super::*;
    use gltf::Gltf;

    #[test]
    fn box_rotate_obj() {
        let mesh_meta: Vec<_> = (1..=12)
            .map(|frame| {
                let path = format!("./assets/box_rotate_{}.obj", frame);
                (String::from("box_rotate"), frame, PathBuf::from(path))
            })
            .collect();

        let attributes = AttributeInfo::default();
        let colors = AttributeInfo::default();
        let texcoords: TextureAttributeInfo = "{\"uv\": f32}".parse().unwrap();
        let material_attribute = "mtl_id";

        let config = LoadConfig {
            attributes: &attributes,
            colors: &colors,
            texcoords: &texcoords,
            material_attribute,
            reverse: false,
            invert_tets: false,
        };

        let meshes = load_meshes(mesh_meta, config);

        assert!(!meshes.is_empty());
    }

    #[test]
    fn box_triangulated() {
        let mesh_meta: Vec<_> = (1..=1)
            .map(|frame| {
                let path = "./assets/box_triangulated.vtk";
                (String::from("box_triangulated"), frame, PathBuf::from(path))
            })
            .collect();

        let attributes = AttributeInfo::default();
        let colors = AttributeInfo::default();
        let texcoords = TextureAttributeInfo::default();
        let material_attribute = "mtl_id";

        let config = LoadConfig {
            attributes: &attributes,
            colors: &colors,
            texcoords: &texcoords,
            material_attribute,
            reverse: false,
            invert_tets: false,
        };

        let meshes = load_meshes(mesh_meta, config);

        assert!(!meshes.is_empty());
    }

    #[test]
    fn multi() {
        let mut mesh_meta = Vec::new();

        // mesh_meta.extend((1..=12)
        //     .map(|frame| {
        //         let path = format!("./assets/box_rotate_{}.vtk", frame);
        //         (String::from("box_rotate"), frame, PathBuf::from(path))
        //     }));
        // mesh_meta.extend((1..=2)
        //     .map(|frame| {
        //         let path = format!("./assets/tet_{}.vtk", frame);
        //         (String::from("tet"), frame, PathBuf::from(path))
        //     }));
        mesh_meta.extend((1..=1).map(|frame| {
            let path = "./assets/box_triangulated.vtk";
            (String::from("box_triangulated"), frame, PathBuf::from(path))
        }));

        let attributes = "{\"pressure\": f32}".parse().unwrap();
        let colors = AttributeInfo::default();
        let texcoords = TextureAttributeInfo::default();
        let material_attribute = "mtl_id";

        let config = LoadConfig {
            attributes: &attributes,
            colors: &colors,
            texcoords: &texcoords,
            material_attribute,
            reverse: true,
            invert_tets: false,
        };

        let meshes = load_meshes(mesh_meta, config);

        assert!(!meshes.is_empty());

        let dt = 1.0 / 24.0;

        let artifact = "./tests/artifacts/multi_test.glb";

        export::export(meshes, artifact.into(), dt, true, Vec::new(), Vec::new());

        let actual = Gltf::open(artifact).unwrap().blob;

        dbg!(&actual);
    }
}