bevy_copperfield 0.2.0

Procedural mesh editor, based on Half-Edge-Mesh datastructure
Documentation
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use std::collections::BTreeMap;

use bevy::prelude::{Mat3, Vec2, Vec3};
use slotmap::SecondaryMap;

use super::{selection::Selection, traversal::Traversal, HalfEdgeId, StackVec, VertexId};

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum AttributeKind {
    Positions,
    Normals,
    UVs,
    Indices,
    Creases,
    UVSeams,
}


pub type AttributeStore<K, T> = SecondaryMap<K, T>;
pub enum AttributeValues {
    VertexU32(AttributeStore<VertexId, u32>),
    VertexVec3(AttributeStore<VertexId, Vec3>),
    VertexVec2(AttributeStore<VertexId, Vec2>),
    VertexBool(AttributeStore<VertexId, bool>),
    EdgeVec2(AttributeStore<HalfEdgeId, Vec2>),
    EdgeVec3(AttributeStore<HalfEdgeId, Vec3>),
    EdgeBool(AttributeStore<HalfEdgeId, bool>),
}

impl AttributeValues {
    pub fn as_vertices_u32(&self) -> &AttributeStore<VertexId, u32> {
        match self {
            Self::VertexU32(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }

    pub fn as_vertices_u32_mut(&mut self) -> &mut AttributeStore<VertexId, u32> {
        match self {
            Self::VertexU32(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }

    pub fn as_vertices_vec2(&self) -> &AttributeStore<VertexId, Vec2> {
        match self {
            Self::VertexVec2(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }

    pub fn as_vertices_vec2_mut(&mut self) -> &mut AttributeStore<VertexId, Vec2> {
        match self {
            Self::VertexVec2(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }

    pub fn as_vertices_vec3(&self) -> &AttributeStore<VertexId, Vec3> {
        match self {
            Self::VertexVec3(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }

    pub fn as_vertices_vec3_mut(&mut self) -> &mut AttributeStore<VertexId, Vec3> {
        match self {
            Self::VertexVec3(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }

    pub fn as_vertices_bool(&self) -> &AttributeStore<VertexId, bool> {
        match self {
            Self::VertexBool(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }

    pub fn as_vertices_bool_mut(&mut self) -> &mut AttributeStore<VertexId, bool> {
        match self {
            Self::VertexBool(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }

    pub fn as_edge_vec2(&self) -> &AttributeStore<HalfEdgeId, Vec2> {
        match self {
            Self::EdgeVec2(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }
    pub fn as_edge_vec2_mut(&mut self) -> &mut AttributeStore<HalfEdgeId, Vec2> {
        match self {
            Self::EdgeVec2(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }
    pub fn as_edge_vec3(&self) -> &AttributeStore<HalfEdgeId, Vec3> {
        match self {
            Self::EdgeVec3(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }
    pub fn as_edge_vec3_mut(&mut self) -> &mut AttributeStore<HalfEdgeId, Vec3> {
        match self {
            Self::EdgeVec3(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }
    pub fn as_edge_bool(&self) -> &AttributeStore<HalfEdgeId, bool> {
        match self {
            Self::EdgeBool(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }
    pub fn as_edge_bool_mut(&mut self) -> &mut AttributeStore<HalfEdgeId, bool> {
        match self {
            Self::EdgeBool(v) => v,
            _ => panic!("Unexpected attribute kind")
        }
    }
}

pub type Attributes = BTreeMap<AttributeKind, AttributeValues>;


impl From<AttributeStore<VertexId, u32>> for AttributeValues {
    fn from(value: AttributeStore<VertexId, u32>) -> Self {
        Self::VertexU32(value)
    }
}

impl From<AttributeStore<VertexId, Vec3>> for AttributeValues {
    fn from(value: AttributeStore<VertexId, Vec3>) -> Self {
        Self::VertexVec3(value)
    }
}

impl From<AttributeStore<HalfEdgeId, Vec2>> for AttributeValues {
    fn from(value: AttributeStore<HalfEdgeId, Vec2>) -> Self {
        Self::EdgeVec2(value)
    }
}

impl From<AttributeStore<HalfEdgeId, Vec3>> for AttributeValues {
    fn from(value: AttributeStore<HalfEdgeId, Vec3>) -> Self {
        Self::EdgeVec3(value)
    }
}

pub trait TraversalQueries{
    /// Get position of the associated vertex
    fn position(&self) -> Vec3;
    /// Uses AttributeKind::Creases to tell if the vertex is supposed to use smooth normals or sharp
    fn is_smooth_normals(&self) -> bool;
    /// Uses AttributeKind::UVSeams to tell if the vertex is supposed to use smooth normals or sharp
    fn is_uv_seam(&self) -> bool;
    /// Returns how sharp this edge is by taking a dot product of adjacent face normals.
    fn sharpness(&self) -> f32;
    /// Returns UV coordinates of the associated vertex.
    fn uv(&self) -> Vec2;
    /// Use earcutr to triangulage a polygon. Panics if the normal can't be calculated or 
    /// polygon can't be triangulated. Returns an array of vertices, in CCW winding, three for each triangle.
    fn triangulate(&self) -> Vec<VertexId>;
}

pub trait SelectionQueries {
    /// Get the average of all vertices of this face
    fn calculate_centroid(&self) -> Vec3;
    /// Fit a plane to a collection of points using least squares method.
	/// Fast, and accurate to within a few degrees.
	/// Returns None if the points do not span a plane.
	/// https://www.ilikebigbits.com/2015_03_04_plane_from_points.html
    fn calculate_normal(&self) -> Option<Vec3>;
    /// Calculate full 3x3 covariance matrix, excluding symmetries
    fn calculate_covariance(&self) -> Mat3;
}

impl<'m> TraversalQueries for Traversal<'m> {
    fn position(&self) -> Vec3 {
        let vertex = self.vertex();
        let values = self.mesh.attribute(&super::attributes::AttributeKind::Positions).expect("Vertices don't have position attribute.");
        values.as_vertices_vec3().get(vertex).copied().unwrap()
    }

    fn uv(&self) -> Vec2 {
        let values = self.mesh.attribute(&super::attributes::AttributeKind::UVs).expect("Vertices don't have UV attribute.");
        values.as_edge_vec2().get(**self).copied().unwrap()
    }

    fn is_smooth_normals(&self) -> bool {
        if let Some(store) = self.mesh.attribute(&super::attributes::AttributeKind::Creases) {
            store.as_vertices_bool().get(self.vertex()).copied().unwrap_or(self.mesh.is_smooth)
        } else {
            self.mesh.is_smooth
        }
    }

    fn is_uv_seam(&self) -> bool {
        if let Some(store) = self.mesh.attribute(&super::attributes::AttributeKind::UVSeams) {
            store.as_edge_bool().get(**self).copied().unwrap_or(false)
        } else {
            false
        }
    }
    
    fn sharpness(&self) -> f32 {
        let n = self.calculate_normal();
        let n_other = self.twin().calculate_normal();
        1.0 - match (n, n_other) { // invert cosine so 1.0 is "sharp" and 0 is "flat"
            (Some(n), Some(n_other)) => {
                n.dot(n_other).abs() / n.length() / n_other.length() // return cos() of angle between normals
            },
            _ => 1.0 // cos(0) = 1.0
        }
    }

    fn triangulate(&self) -> Vec<VertexId> {
        let normal = self.calculate_normal().unwrap();
        let u = (self.position() - self.calculate_centroid()).normalize();
        let v = u.cross(normal);
        let mut vertex_ids = StackVec::new();
        let vertices = self.iter_loop().flat_map(|p| {vertex_ids.push(p.vertex()); let p = p.position(); [p.dot(u), p.dot(v)]}).collect::<Vec<_>>();
        let result = earcutr::earcut(&vertices, &[], 2).unwrap();
        // earcutr by default returns CW order winding. Call Iterator::rev() to change it to CCW
        result.into_iter().map(|idx| vertex_ids[idx]).rev().collect()
    }
}

impl<'m> SelectionQueries for Traversal<'m> {
    fn calculate_centroid(&self) -> Vec3 {
        let (sum, count) = self.iter_loop().fold((Vec3::ZERO, 0.0), |acc, i| (acc.0 + i.position(), acc.1 + 1.0));
        sum / count
    }

    /// Fit a plane to a collection of points.
	/// Fast, and accurate to within a few degrees.
	/// Returns None if the points do not span a plane.
	/// https://www.ilikebigbits.com/2017_09_25_plane_from_points_2.html
    fn calculate_normal(&self) -> Option<Vec3> {
        let points = self
        .iter_loop()
        .map(|f| f.position()).collect::<StackVec<_>>();
        let n = points.len() as f32;
        let mut sum = Vec3{x:0.0, y:0.0, z:0.0};
        for &p in &points {
            sum += p;
        }
        let centroid = sum * (1.0 / n);
    
        // Calculate full 3x3 covariance matrix, excluding symmetries:
        let mut xx = 0.0; let mut xy = 0.0; let mut xz = 0.0;
        let mut yy = 0.0; let mut yz = 0.0; let mut zz = 0.0;

        let a = centroid - points[0];
        let b = centroid - points[1];
        let simple_normal = a.cross(b);
    
        for p in points {
            let r = p - centroid;
            xx += r.x * r.x;
            xy += r.x * r.y;
            xz += r.x * r.z;
            yy += r.y * r.y;
            yz += r.y * r.z;
            zz += r.z * r.z;
        }
    
        xx /= n;
        xy /= n;
        xz /= n;
        yy /= n;
        yz /= n;
        zz /= n;
    
        let mut weighted_dir = Vec3{x: 0.0, y: 0.0, z: 0.0};
    
        {
            let det_x = yy*zz - yz*yz;
            let axis_dir = Vec3{
                x: det_x,
                y: xz*yz - xy*zz,
                z: xy*yz - xz*yy,
            };
            let mut weight = det_x * det_x;
            if weighted_dir.dot(axis_dir) < 0.0 { weight = -weight; }
            weighted_dir += axis_dir * weight;
        }
    
        {
            let det_y = xx*zz - xz*xz;
            let axis_dir = Vec3{
                x: xz*yz - xy*zz,
                y: det_y,
                z: xy*xz - yz*xx,
            };
            let mut weight = det_y * det_y;
            if weighted_dir.dot(axis_dir) < 0.0 { weight = -weight; }
            weighted_dir += axis_dir * weight;
        }
    
        {
            let det_z = xx*yy - xy*xy;
            let axis_dir = Vec3{
                x: xy*yz - xz*yy,
                y: xy*xz - yz*xx,
                z: det_z,
            };
            let mut weight = det_z * det_z;
            if weighted_dir.dot(axis_dir) < 0.0 { weight = -weight; }
            weighted_dir += axis_dir * weight;
        }
    
        let normal = weighted_dir.normalize();
        if normal.is_finite() {
            let sign = simple_normal.dot(normal).signum();
            Some(sign*normal)
        } else {
            None
        }
    }

    fn calculate_covariance(&self) -> Mat3 {
        let centroid = self.calculate_centroid();
        self.iter_loop().fold(Mat3::ZERO, |acc, i| {
            let r = Mat3::from_cols(i.position() - centroid, Vec3::ZERO, Vec3::ZERO);
            acc + r * r.transpose()
        })
    }

}

impl<'m> SelectionQueries for Selection<'m> {
    fn calculate_centroid(&self) -> Vec3 {
        // We keep count since we don't know ahead of time the amount of face edges
        let (sum, count) = self.iter().map(|t| t.calculate_centroid()).fold((Vec3::ZERO, 0.0), |acc, i| (acc.0 + i, acc.1 + 1.0));
        sum / count
    }
    fn calculate_covariance(&self) -> Mat3 {
        self.iter().map(|t| t.calculate_covariance()).sum()
    }
    fn calculate_normal(&self) -> Option<Vec3> {
        let (sum, count) = self.iter().filter_map(|t| t.calculate_normal()).fold((Vec3::ZERO, 0.0), |acc, i| (acc.0 + i, acc.1 + 1.0));
        if count == 0.0 {
            None
        } else {
            Some(sum / count)
        }
    }
}


#[cfg(test)]
mod tests {
    use bevy::prelude::{Mat3, Vec3};

    #[test]
    fn test_quad() {
        let v1 = Vec3::new(0.0, 0.875, -0.25);  //  v1 === v4
        let v2 = Vec3::new(-0.25, 0.875, 0.0);  //   |      |
        let v3 = Vec3::new(0.0, 0.875, 0.25);   //   |      |
        let v4 = Vec3::new(0.25, 0.875, 0.0);   //  v2 === v3
        let n = (v1 - v2).cross(v3 - v2).normalize();
        let n2 = (v3 - v4).cross(v1 - v4).normalize();
        assert_eq!(n, n2);
    }   

    #[test]
    fn test_matrix() {
        let v1 = Vec3::new(0.0, 0.875, -0.25);  //  v1 === v4
        let v2 = Vec3::new(-0.25, 0.875, 0.0);  //   |      |
        let v3 = Vec3::new(0.0, 0.875, 0.25);   //   |      |
        let v4 = Vec3::new(0.25, 0.875, 0.0);   //  v2 === v3
        let centroid = 0.25*(v1 + v2 + v3 + v4);
        println!("Centroid: {centroid:?}");
        let r1 = v1 - centroid;
        let r2 = v2 - centroid;
        let r3 = v3 - centroid;
        let r4 = v4 - centroid;
        let m1 = Mat3::from_cols(r1, Vec3::ZERO, Vec3::ZERO);
        let m2 = Mat3::from_cols(r2, Vec3::ZERO, Vec3::ZERO);
        let m3 = Mat3::from_cols(r3, Vec3::ZERO, Vec3::ZERO);
        let m4 = Mat3::from_cols(r4, Vec3::ZERO, Vec3::ZERO);
        let covariance = m1 * m1.transpose() + m2 * m2.transpose() + m3 * m3.transpose() + m4 * m4.transpose();
        // covariance.y_axis.x = 0.0;
        // covariance.z_axis.x = 0.0;
        // covariance.z_axis.y = 0.0;
        let mut other_way = Mat3::ZERO;
        other_way.x_axis.x = r1.x * r1.x + r2.x * r2.x + r3.x * r3.x + r4.x * r4.x;
        other_way.x_axis.y = r1.x * r1.y + r2.x * r2.y + r3.x * r3.y + r4.x * r4.y;
        other_way.x_axis.z = r1.x * r1.z + r2.x * r2.z + r3.x * r3.z + r4.x * r4.z;
        other_way.y_axis.y = r1.y * r1.y + r2.y * r2.y + r3.y * r3.y + r4.y * r4.y;
        other_way.y_axis.z = r1.y * r1.z + r2.y * r2.z + r3.y * r3.z + r4.y * r4.z;
        other_way.z_axis.z = r1.z * r1.z + r2.z * r2.z + r3.z * r3.z + r4.z * r4.z;
        println!("Covariance: {covariance:?} other: {other_way:?}");
        assert_eq!(covariance, other_way);

		let det_x = other_way.y_axis.y * other_way.z_axis.z - other_way.y_axis.z * other_way.y_axis.z;
		let det_y = other_way.x_axis.x * other_way.z_axis.z - other_way.x_axis.z * other_way.x_axis.z;
		let det_z = other_way.x_axis.x * other_way.y_axis.y - other_way.x_axis.y * other_way.x_axis.y;

        let cdx = covariance.y_axis.cross(covariance.z_axis);
        let cdy = covariance.z_axis.cross(covariance.x_axis);
        let cdz = covariance.y_axis.cross(covariance.y_axis);

        println!("cdy: {cdy:?}");
        assert_eq!(det_x, cdx.x);
        assert_eq!(det_y, cdy.y);
        assert_eq!(det_z, cdz.x);

        let dir = Vec3::new(det_x,other_way.x_axis.z * other_way.y_axis.z - other_way.x_axis.y * other_way.z_axis.z,other_way.x_axis.y * other_way.y_axis.z - other_way.x_axis.z * other_way.y_axis.y);
        assert_eq!(dir, cdx);
        let dir = Vec3::new(other_way.x_axis.z * other_way.y_axis.z - other_way.x_axis.y * other_way.z_axis.z, det_y, other_way.x_axis.y * other_way.x_axis.z - other_way.y_axis.z * other_way.x_axis.x);
        assert_eq!(dir, cdy);
        let dir = Vec3::new(other_way.x_axis.y * other_way.y_axis.z - other_way.x_axis.z * other_way.y_axis.y, other_way.x_axis.y * other_way.x_axis.z - other_way.y_axis.z * other_way.x_axis.x, det_z);
        assert_eq!(dir, cdz);

    }
}