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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! See [Mesh](crate::mesh::Mesh).

use crate::mesh::*;

impl Mesh {
    ///
    /// Constructs a new [Mesh] from a [three_d_asset::TriMesh] which can either be manually constructed or loaded via the [three_d_asset::io] module.
    ///
    /// # Examples
    /// ```no_run
    /// # use tri_mesh::*;
    /// let model: three_d_asset::Model =
    ///     three_d_asset::io::load_and_deserialize("cube.obj").expect("Failed loading asset");
    /// let mesh = Mesh::new(&model.geometries[0]);
    /// ```
    ///
    /// ```
    /// # use tri_mesh::*;
    /// let mesh = Mesh::new(&three_d_asset::TriMesh::sphere(4));
    /// ```
    ///
    /// ```
    /// # use tri_mesh::*;
    /// let mesh = Mesh::new(&three_d_asset::TriMesh {
    ///     positions: three_d_asset::Positions::F64(vec![vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), vec3(0.0, 0.0, 1.0)]),
    ///     ..Default::default()
    /// });
    /// ```
    ///
    pub fn new(input: &three_d_asset::TriMesh) -> Self {
        let no_vertices = input.vertex_count();
        let no_faces = input.triangle_count();
        let indices = input
            .indices
            .to_u32()
            .unwrap_or((0..no_faces as u32 * 3).collect::<Vec<_>>());
        let positions = input.positions.to_f64();
        let mesh = Mesh {
            connectivity_info: ConnectivityInfo::new(no_vertices, no_faces),
        };

        // Create vertices
        for i in 0..no_vertices {
            mesh.connectivity_info.new_vertex(positions[i]);
        }

        let mut twins = HashMap::<(VertexID, VertexID), HalfEdgeID>::new();
        fn sort(a: VertexID, b: VertexID) -> (VertexID, VertexID) {
            if a < b {
                (a, b)
            } else {
                (b, a)
            }
        }

        // Create faces and twin connectivity
        for face in 0..no_faces {
            let v0 = indices[face * 3];
            let v1 = indices[face * 3 + 1];
            let v2 = indices[face * 3 + 2];

            let face = mesh.connectivity_info.create_face(
                unsafe { VertexID::new(v0) },
                unsafe { VertexID::new(v1) },
                unsafe { VertexID::new(v2) },
            );

            // mark twin halfedges
            let mut walker = mesh.walker_from_face(face);
            for _ in 0..3 {
                let vertex_id = walker.vertex_id().unwrap();
                walker.as_next();
                let key = sort(vertex_id, walker.vertex_id().unwrap());
                if let Some(twin) = twins.get(&key) {
                    mesh.connectivity_info
                        .set_halfedge_twin(walker.halfedge_id().unwrap(), *twin);
                } else {
                    twins.insert(key, walker.halfedge_id().unwrap());
                }
            }
        }
        for halfedge in mesh.connectivity_info.halfedge_iterator() {
            if mesh
                .connectivity_info
                .halfedge(halfedge)
                .unwrap()
                .twin
                .is_none()
            {
                let vertex = mesh
                    .walker_from_halfedge(halfedge)
                    .as_previous()
                    .vertex_id()
                    .unwrap();
                mesh.connectivity_info.set_halfedge_twin(
                    halfedge,
                    mesh.connectivity_info
                        .new_halfedge(Some(vertex), None, None),
                );
            }
        }

        mesh
    }

    ///
    /// Exports the [Mesh] into a [three_d_asset::TriMesh] that contain the raw buffer data.
    /// The [three_d_asset::TriMesh] can then for example be visualized or saved to disk (using the [three_d_asset::io] module).
    ///
    pub fn export(&self) -> three_d_asset::TriMesh {
        use three_d_asset::{Indices, Positions, TriMesh};
        let vertices: Vec<VertexID> = self.vertex_iter().collect();
        let mut indices = Vec::with_capacity(self.no_faces() * 3);
        for face_id in self.face_iter() {
            for halfedge_id in self.face_halfedge_iter(face_id) {
                let vertex_id = self.walker_from_halfedge(halfedge_id).vertex_id().unwrap();
                let index = vertices.iter().position(|v| v == &vertex_id).unwrap();
                indices.push(index as u32);
            }
        }
        TriMesh {
            indices: Indices::U32(indices),
            positions: Positions::F64(
                self.vertex_iter()
                    .map(|vertex_id| self.vertex_position(vertex_id))
                    .collect::<Vec<_>>(),
            ),
            normals: Some(
                self.vertex_iter()
                    .map(|vertex_id| self.vertex_normal(vertex_id).cast::<f32>().unwrap())
                    .collect::<Vec<_>>(),
            ),
            ..Default::default()
        }
    }
}

impl From<three_d_asset::TriMesh> for Mesh {
    fn from(mesh: three_d_asset::TriMesh) -> Self {
        Self::new(&mesh)
    }
}

impl From<&three_d_asset::TriMesh> for Mesh {
    fn from(mesh: &three_d_asset::TriMesh) -> Self {
        Self::new(mesh)
    }
}

impl From<Mesh> for three_d_asset::TriMesh {
    fn from(mesh: Mesh) -> Self {
        mesh.export()
    }
}

impl From<&Mesh> for three_d_asset::TriMesh {
    fn from(mesh: &Mesh) -> Self {
        mesh.export()
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use three_d_asset::{Positions, TriMesh};

    #[test]
    fn test_from_obj() {
        let source = b"o Cube
        v 1.000000 -1.000000 -1.000000
        v 1.000000 -1.000000 1.000000
        v -1.000000 -1.000000 1.000000
        v -1.000000 -1.000000 -1.000000
        v 1.000000 1.000000 -1.000000
        v 0.999999 1.000000 1.000001
        v -1.000000 1.000000 1.000000
        v -1.000000 1.000000 -1.000000
        f 1 2 3
        f 1 3 4
        f 5 8 7
        f 5 7 6
        f 1 5 6
        f 1 6 2
        f 2 6 7
        f 2 7 3
        f 3 7 8
        f 3 8 4
        f 5 1 4
        f 5 4 8"
            .to_vec();
        let mut raw_assets = three_d_asset::io::RawAssets::new();
        raw_assets.insert("cube.obj", source);
        let mut model: three_d_asset::Model = raw_assets.deserialize(".obj").unwrap();
        let mesh: Mesh = model.geometries.remove(0).into();
        assert_eq!(mesh.no_faces(), 12);
        assert_eq!(mesh.no_vertices(), 8);
        mesh.is_valid().unwrap();
    }

    #[test]
    fn test_indexed_export() {
        let mesh: Mesh = TriMesh::cylinder(16).into();
        let m: TriMesh = (&mesh).into();
        m.validate().unwrap();

        assert_eq!(m.triangle_count(), mesh.no_faces());
        assert_eq!(m.vertex_count(), mesh.no_vertices());

        let positions = m.positions.to_f64();
        let normals = m.normals.as_ref().unwrap();
        m.for_each_triangle(|i0, i1, i2| {
            let id0 = unsafe { VertexID::new(i0 as u32) };
            let id1 = unsafe { VertexID::new(i1 as u32) };
            let id2 = unsafe { VertexID::new(i2 as u32) };
            assert!(positions[i0].distance(mesh.vertex_position(id0)) < 0.001);
            assert!(positions[i1].distance(mesh.vertex_position(id1)) < 0.001);
            assert!(positions[i2].distance(mesh.vertex_position(id2)) < 0.001);
            assert!(normals[i0].distance(mesh.vertex_normal(id0).cast::<f32>().unwrap()) < 0.001);
            assert!(normals[i1].distance(mesh.vertex_normal(id1).cast::<f32>().unwrap()) < 0.001);
            assert!(normals[i2].distance(mesh.vertex_normal(id2).cast::<f32>().unwrap()) < 0.001);
        });
    }

    #[test]
    fn test_new_from_positions() {
        let mesh: Mesh = TriMesh {
            positions: Positions::F64(vec![
                vec3(0.0, 0.0, 0.0),
                vec3(1.0, 0.0, 0.0),
                vec3(0.0, 0.0, 1.0),
                vec3(0.0, 1.0, 1.0),
                vec3(1.0, 0.0, 1.0),
                vec3(0.0, 1.0, 0.0),
                vec3(0.0, 0.0, 1.0),
                vec3(1.0, 0.0, 1.0),
                vec3(0.0, 1.0, 1.0),
            ]),
            ..Default::default()
        }
        .into();

        assert_eq!(9, mesh.no_vertices());
        assert_eq!(3, mesh.no_faces());
        mesh.is_valid().unwrap();
    }
}