Skip to main content

rscad_core/objects/three_dimensional/
cube.rs

1use crate::prelude_::*;
2
3#[derive(Clone, Copy, Debug, Default)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5#[cfg_attr(feature = "reflect", derive(bevy_reflect::Reflect))]
6pub struct Cube {
7    pub x: f64,
8    pub y: f64,
9    pub z: f64,
10}
11
12pub trait CubeInit {
13    fn cube(self) -> Cube;
14}
15
16impl<T: Number> CubeInit for [T; 1] {
17    fn cube(self) -> Cube {
18        let [size] = self;
19        let size = size.to_f64();
20        Cube {
21            x: size,
22            y: size,
23            z: size,
24        }
25    }
26}
27
28impl<T: Number> CubeInit for [T; 3] {
29    fn cube(self) -> Cube {
30        let [x, y, z] = self;
31        Cube {
32            x: x.to_f64(),
33            y: y.to_f64(),
34            z: z.to_f64(),
35        }
36    }
37}
38
39impl Cube {
40    pub fn new(args: impl CubeInit) -> Self {
41        args.cube()
42    }
43
44    pub const fn vertices_array(&self) -> [glam::DVec3; 8] {
45        [
46            glam::DVec3::new(0.0, 0.0, 0.0),          // bottom left front
47            glam::DVec3::new(self.x, 0.0, 0.0),       // bottom right front
48            glam::DVec3::new(self.x, self.y, 0.0),    // bottom right back
49            glam::DVec3::new(0.0, self.y, 0.0),       // bottom left back
50            glam::DVec3::new(0.0, 0.0, self.z),       // top left front
51            glam::DVec3::new(self.x, 0.0, self.z),    // top right front
52            glam::DVec3::new(self.x, self.y, self.z), // top right back
53            glam::DVec3::new(0.0, self.y, self.z),    // top left back
54        ]
55    }
56
57    pub const fn blf(&self) -> glam::DVec3 {
58        self.vertices_array()[0]
59    }
60    pub const fn brf(&self) -> glam::DVec3 {
61        self.vertices_array()[1]
62    }
63    pub const fn brb(&self) -> glam::DVec3 {
64        self.vertices_array()[2]
65    }
66    pub const fn blb(&self) -> glam::DVec3 {
67        self.vertices_array()[3]
68    }
69    pub const fn tlf(&self) -> glam::DVec3 {
70        self.vertices_array()[4]
71    }
72    pub const fn trf(&self) -> glam::DVec3 {
73        self.vertices_array()[5]
74    }
75    pub const fn trb(&self) -> glam::DVec3 {
76        self.vertices_array()[6]
77    }
78    pub const fn tlb(&self) -> glam::DVec3 {
79        self.vertices_array()[7]
80    }
81
82    pub fn vertices(&self) -> core::array::IntoIter<glam::DVec3, 8> {
83        self.vertices_array().into_iter()
84    }
85
86    pub const fn faces_array(&self) -> [[glam::DVec3; 4]; 6] {
87        [
88            // bottom face
89            [self.blf(), self.brf(), self.brb(), self.blb()],
90            // top face
91            [self.tlf(), self.trf(), self.trb(), self.tlb()],
92            // front face
93            [self.blf(), self.brf(), self.trf(), self.tlf()],
94            // back face
95            [self.blb(), self.brb(), self.trb(), self.tlb()],
96            // left face
97            [self.blf(), self.blb(), self.tlb(), self.tlf()],
98            // right face
99            [self.brf(), self.brb(), self.trb(), self.trf()],
100        ]
101    }
102
103    pub const fn indices_array(&self) -> [[usize; 4]; 6] {
104        [
105            // bottom face
106            [0, 1, 2, 3],
107            // top face
108            [4, 5, 6, 7],
109            // front face
110            [0, 1, 5, 4],
111            // back face
112            [3, 2, 6, 7],
113            // left face
114            [0, 3, 7, 4],
115            // right face
116            [1, 2, 6, 5],
117        ]
118    }
119}
120
121impl Object for Cube {}