egraphics_core/
triangle.rs1use crate::Error;
2use crate::Error::NoElements;
3use std::mem;
4
5#[derive(Copy, Clone, Debug, PartialEq)]
6pub struct Vector3D {
7 x: f32,
8 y: f32,
9 z: f32,
10}
11
12impl Vector3D {
13 pub fn new(x: f32, y: f32, z: f32) -> Self {
14 Self { x, y, z }
15 }
16}
17
18#[derive(Copy, Clone, Debug, PartialEq)]
19#[repr(C)]
20pub struct Vertex {
21 pub(crate) position: nalgebra::Point3<f32>,
22 pub(crate) color: nalgebra::Point3<f32>,
23}
24
25impl Vertex {
26 pub fn new(position: nalgebra::Point3<f32>, color: nalgebra::Point3<f32>) -> Self {
27 Self { position, color }
28 }
29
30 pub fn position(&self) -> &nalgebra::Point3<f32> {
31 &self.position
32 }
33
34 pub fn color(&self) -> &nalgebra::Point3<f32> {
35 &self.color
36 }
37}
38
39#[derive(Clone, Debug, PartialEq)]
40pub struct Triangle {
41 vertices: Vec<Vertex>,
42}
43
44impl Triangle {
45 pub fn new(vertices: Vec<Vertex>) -> Result<Self, Error> {
46 if vertices.is_empty() {
47 return Err(NoElements);
48 }
49
50 Ok(Self { vertices })
51 }
52
53 pub(crate) fn buffer_length(&self) -> u32 {
54 (self.vertices.len() * mem::size_of::<Vertex>()) as u32
55 }
56
57 pub(crate) fn len(&self) -> usize {
58 self.vertices.len()
59 }
60
61 pub(crate) fn get_vertices(&self) -> &Vec<Vertex> {
62 &self.vertices
63 }
64}
65
66impl From<Vec<nalgebra::Point3<f32>>> for Triangle {
67 fn from(item: Vec<nalgebra::Point3<f32>>) -> Self {
68 let vertices = item
69 .iter()
70 .map(|p| Vertex {
71 position: *p,
72 color: nalgebra::Point3::new(0.5, 0.5, 0.5),
73 })
74 .collect();
75 Triangle { vertices }
76 }
77}