drm-gfx 0.2.1

3D graphics rendering for direct rendering manager with optional tokio thread support
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use embedded_graphics_core::pixelcolor::{Bgr888, WebColors};
use log::error;
use nalgebra::{Point3, Similarity3, UnitQuaternion, Vector3};

#[derive(Debug, PartialEq)]
pub enum RenderMode {
    Points,
    Lines,
    Solid,
    SolidLightDir(Vector3<f32>),
}
#[derive(Debug, Default)]
pub struct Geometry<'a> {
    pub vertices: &'a [[f32; 3]],
    pub faces: &'a [[usize; 3]],
    pub colors: &'a [Bgr888],
    pub lines: &'a [[usize; 2]],
    pub normals: &'a [[f32; 3]],
}

impl Geometry<'_> {
    fn check_validity(&self) -> bool {
        if self.vertices.is_empty() {
            error!("Vertices are empty");
            return false;
        }

        for face in self.faces {
            if face[0] >= self.vertices.len()
                || face[1] >= self.vertices.len()
                || face[2] >= self.vertices.len()
            {
                error!("Face vertices are out of bounds");
                return false;
            }
        }

        for line in self.lines {
            if line[0] >= self.vertices.len() || line[1] >= self.vertices.len() {
                error!("Line vertices are out of bounds");
                return false;
            }
        }

        if !self.colors.is_empty()
            && (((self.faces.is_empty() && self.lines.is_empty())
                && self.colors.len() != self.vertices.len())
                || (!self.faces.is_empty() && self.colors.len() != self.faces.len())
                || (!self.lines.is_empty() && self.colors.len() != self.lines.len()))
        {
            error!("Colors are not the same length as vertices|faces|lines");
            return false;
        }

        true
    }

    pub fn lines_from_faces(faces: &[[usize; 3]]) -> Vec<(usize, usize)> {
        let mut lines = Vec::new();
        for face in faces {
            for line in &[(face[0], face[1]), (face[1], face[2]), (face[2], face[0])] {
                let (a, b) = if line.0 < line.1 {
                    (line.0, line.1)
                } else {
                    (line.1, line.0)
                };
                if !lines.contains(&(a, b)) {
                    lines.push((a, b));
                }
            }
        }

        lines
    }
}

pub struct K3dMesh<'a> {
    pub similarity: Similarity3<f32>,
    pub model_matrix: nalgebra::Matrix4<f32>,

    pub color: Bgr888,
    pub render_mode: RenderMode,
    pub geometry: Geometry<'a>,
}

impl K3dMesh<'_> {
    pub fn new(geometry: Geometry) -> K3dMesh {
        assert!(geometry.check_validity());
        let sim = Similarity3::new(Vector3::new(0.0, 0.0, 0.0), nalgebra::zero(), 1.0);
        K3dMesh {
            model_matrix: sim.to_homogeneous(),
            similarity: sim,
            color: Bgr888::CSS_WHITE,
            render_mode: RenderMode::Points,
            geometry,
        }
    }

    pub fn set_color(&mut self, color: Bgr888) {
        self.color = color;
    }

    pub fn set_render_mode(&mut self, mode: RenderMode) {
        self.render_mode = mode;
    }

    pub fn set_position(&mut self, x: f32, y: f32, z: f32) {
        self.similarity.isometry.translation.x = x;
        self.similarity.isometry.translation.y = y;
        self.similarity.isometry.translation.z = z;
        self.update_model_matrix();
    }

    pub fn get_position(&self) -> Point3<f32> {
        self.similarity.isometry.translation.vector.into()
    }

    pub fn set_attitude(&mut self, roll: f32, pitch: f32, yaw: f32) {
        self.similarity.isometry.rotation = UnitQuaternion::from_euler_angles(roll, pitch, yaw);
        self.update_model_matrix();
    }

    pub fn set_target(&mut self, target: Point3<f32>) {
        let view = Similarity3::look_at_rh(
            &self.similarity.isometry.translation.vector.into(),
            &target,
            &Vector3::y(),
            1.0,
        );

        self.similarity = view;
        self.update_model_matrix();
    }

    pub fn set_scale(&mut self, s: f32) {
        if s == 0.0 {
            return;
        }
        self.similarity.set_scaling(s);
        self.update_model_matrix();
    }

    fn update_model_matrix(&mut self) {
        self.model_matrix = self.similarity.to_homogeneous();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use nalgebra::Vector3;

    #[ignore = "Vertx color interpolation not supported yet"]
    #[test]
    fn test_geometry_check_validity_valid_data_0() {
        let vertices = &[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
        let faces = &[[0, 1, 2]];
        let colors = &[Bgr888::CSS_RED, Bgr888::CSS_GREEN, Bgr888::CSS_BLUE];
        let lines = &[[0, 1]];
        let normals = &[[0.0, 0.0, 1.0]];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        assert!(geometry.check_validity());
    }

    #[test]
    fn test_geometry_check_validity_valid_data_1() {
        let vertices = &[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
        let faces = &[[0, 1, 2]];
        let colors = &[Bgr888::CSS_RED];
        let colors_bad = &[Bgr888::CSS_RED, Bgr888::CSS_GREEN, Bgr888::CSS_BLUE];
        let lines = &[[0, 1]];
        let normals = &[[0.0, 0.0, 1.0]];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        assert!(geometry.check_validity());

        let geometry_bad = Geometry {
            vertices,
            faces,
            colors: colors_bad,
            lines,
            normals,
        };
        assert!(!geometry_bad.check_validity());
    }

    #[test]
    fn test_geometry_check_validity_empty_vertices() {
        let vertices: &[[f32; 3]] = &[];
        let faces: &[[usize; 3]] = &[];
        let colors: &[Bgr888] = &[];
        let lines: &[[usize; 2]] = &[];
        let normals: &[[f32; 3]] = &[];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        assert!(!geometry.check_validity());
    }

    #[test]
    fn test_geometry_check_validity_invalid_face_indices() {
        let vertices = &[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]]; // Only 2 vertices
        let faces = &[[0, 1, 2]]; // Index 2 is out of bounds
        let colors = &[];
        let lines = &[];
        let normals = &[];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        assert!(!geometry.check_validity());
    }

    #[test]
    fn test_geometry_check_validity_invalid_line_indices() {
        let vertices = &[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]]; // Only 2 vertices
        let faces = &[];
        let colors = &[];
        let lines = &[[0, 2]]; // Index 2 is out of bounds
        let normals = &[];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        assert!(!geometry.check_validity());
    }

    #[test]
    fn test_geometry_check_validity_mismatched_colors() {
        let vertices = &[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
        let faces = &[];
        let colors = &[Bgr888::CSS_RED, Bgr888::CSS_GREEN]; // Only 2 colors for 3 vertices
        let lines = &[];
        let normals = &[];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        assert!(!geometry.check_validity());
    }

    #[test]
    fn test_lines_from_faces() {
        // Two triangular faces sharing an edge
        let faces = &[
            [0, 1, 2], // First triangle
            [2, 1, 3], // Second triangle sharing edge 1-2
        ];

        let lines = Geometry::lines_from_faces(faces);

        // Expected unique edges:
        // (0,1), (1,2), (2,0) from first triangle
        // (1,3), (3,2) from second triangle (edge 1-2 already counted)
        // All edges should be sorted (smaller index first)
        assert_eq!(lines.len(), 5);
        assert!(lines.contains(&(0, 1)));
        assert!(lines.contains(&(0, 2)));
        assert!(lines.contains(&(1, 2)));
        assert!(lines.contains(&(1, 3)));
        assert!(lines.contains(&(2, 3)));
    }

    #[test]
    fn test_mesh_creation() {
        let vertices = &[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
        let faces = &[[0, 1, 2]];
        let colors = &[];
        let lines = &[];
        let normals = &[];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        let mesh = K3dMesh::new(geometry);

        // Check default values
        assert_eq!(mesh.render_mode, RenderMode::Points);
        assert_eq!(mesh.color, Bgr888::CSS_WHITE);

        // Position should be at origin
        let position = mesh.get_position();
        assert_relative_eq!(position.x, 0.0);
        assert_relative_eq!(position.y, 0.0);
        assert_relative_eq!(position.z, 0.0);
    }

    #[test]
    fn test_set_color() {
        let vertices = &[[0.0, 0.0, 0.0]];
        let faces = &[];
        let colors = &[];
        let lines = &[];
        let normals = &[];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        let mut mesh = K3dMesh::new(geometry);
        mesh.set_color(Bgr888::CSS_RED);

        assert_eq!(mesh.color, Bgr888::CSS_RED);
    }

    #[test]
    fn test_set_render_mode() {
        let vertices = &[[0.0, 0.0, 0.0]];
        let faces = &[];
        let colors = &[];
        let lines = &[];
        let normals = &[];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        let mut mesh = K3dMesh::new(geometry);

        // Test setting to Lines mode
        mesh.set_render_mode(RenderMode::Lines);
        assert_eq!(mesh.render_mode, RenderMode::Lines);

        // Test setting to Solid mode
        mesh.set_render_mode(RenderMode::Solid);
        assert_eq!(mesh.render_mode, RenderMode::Solid);

        // Test setting to SolidLightDir mode
        let light_dir = Vector3::new(0.0, 1.0, 0.0);
        mesh.set_render_mode(RenderMode::SolidLightDir(light_dir));
        match mesh.render_mode {
            RenderMode::SolidLightDir(dir) => {
                assert_relative_eq!(dir.x, light_dir.x);
                assert_relative_eq!(dir.y, light_dir.y);
                assert_relative_eq!(dir.z, light_dir.z);
            }
            _ => panic!("Expected SolidLightDir render mode"),
        }
    }

    #[test]
    fn test_set_position() {
        let vertices = &[[0.0, 0.0, 0.0]];
        let faces = &[];
        let colors = &[];
        let lines = &[];
        let normals = &[];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        let mut mesh = K3dMesh::new(geometry);

        // Set position to (1.0, 2.0, 3.0)
        mesh.set_position(1.0, 2.0, 3.0);

        // Check position
        let position = mesh.get_position();
        assert_relative_eq!(position.x, 1.0);
        assert_relative_eq!(position.y, 2.0);
        assert_relative_eq!(position.z, 3.0);

        // Check that model matrix was updated
        // The translation components should be in the last column
        assert_relative_eq!(mesh.model_matrix[(0, 3)], 1.0);
        assert_relative_eq!(mesh.model_matrix[(1, 3)], 2.0);
        assert_relative_eq!(mesh.model_matrix[(2, 3)], 3.0);
    }

    #[test]
    fn test_set_attitude() {
        let vertices = &[[0.0, 0.0, 0.0]];
        let faces = &[];
        let colors = &[];
        let lines = &[];
        let normals = &[];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        let mut mesh = K3dMesh::new(geometry);

        // Set rotation angles in radians (45° roll, 30° pitch, 60° yaw)
        let roll = std::f32::consts::PI / 4.0; // 45°
        let pitch = std::f32::consts::PI / 6.0; // 30°
        let yaw = std::f32::consts::PI / 3.0; // 60°

        mesh.set_attitude(roll, pitch, yaw);

        // Verify the rotation was applied by checking that model_matrix has changed
        // We don't check exact values, just that it's not identity anymore
        assert_ne!(mesh.model_matrix, nalgebra::Matrix4::identity());
    }

    #[test]
    fn test_set_scale() {
        let vertices = &[[0.0, 0.0, 0.0]];
        let faces = &[];
        let colors = &[];
        let lines = &[];
        let normals = &[];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        let mut mesh = K3dMesh::new(geometry);

        // Set scale to 2.0
        mesh.set_scale(2.0);

        // Check that the scale was applied to the similarity transform
        assert_relative_eq!(mesh.similarity.scaling(), 2.0);

        // The first 3 diagonal elements of the matrix should be scaled by 2.0
        assert_relative_eq!(mesh.model_matrix[(0, 0)], 2.0);
        assert_relative_eq!(mesh.model_matrix[(1, 1)], 2.0);
        assert_relative_eq!(mesh.model_matrix[(2, 2)], 2.0);

        // Test that setting scale to 0.0 is ignored
        mesh.set_scale(0.0);
        assert_relative_eq!(mesh.similarity.scaling(), 2.0); // Should still be 2.0
    }

    #[test]
    fn test_set_target() {
        let vertices = &[[0.0, 0.0, 0.0]];
        let faces = &[];
        let colors = &[];
        let lines = &[];
        let normals = &[];

        let geometry = Geometry {
            vertices,
            faces,
            colors,
            lines,
            normals,
        };

        let mut mesh = K3dMesh::new(geometry);

        // First set position away from origin
        mesh.set_position(0.0, 0.0, 5.0);

        // Set target point at origin
        let target = Point3::new(0.0, 0.0, 0.0);
        mesh.set_target(target);

        // This should have created a "look at" transform
        // The mesh should be looking toward the origin
        // We can't easily test the exact rotation values, but we can verify
        // that the model matrix was updated
        assert_ne!(mesh.model_matrix, nalgebra::Matrix4::identity());

        // Note: the set_target function uses look_at_rh which appears to negate the z position
        // This is expected behavior from the implementation
        let position = mesh.get_position();
        assert_relative_eq!(position.z, -5.0);
    }
}