rscad_core/objects/three_dimensional/
cube.rs1use 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), glam::DVec3::new(self.x, 0.0, 0.0), glam::DVec3::new(self.x, self.y, 0.0), glam::DVec3::new(0.0, self.y, 0.0), glam::DVec3::new(0.0, 0.0, self.z), glam::DVec3::new(self.x, 0.0, self.z), glam::DVec3::new(self.x, self.y, self.z), glam::DVec3::new(0.0, self.y, self.z), ]
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 [self.blf(), self.brf(), self.brb(), self.blb()],
90 [self.tlf(), self.trf(), self.trb(), self.tlb()],
92 [self.blf(), self.brf(), self.trf(), self.tlf()],
94 [self.blb(), self.brb(), self.trb(), self.tlb()],
96 [self.blf(), self.blb(), self.tlb(), self.tlf()],
98 [self.brf(), self.brb(), self.trb(), self.trf()],
100 ]
101 }
102
103 pub const fn indices_array(&self) -> [[usize; 4]; 6] {
104 [
105 [0, 1, 2, 3],
107 [4, 5, 6, 7],
109 [0, 1, 5, 4],
111 [3, 2, 6, 7],
113 [0, 3, 7, 4],
115 [1, 2, 6, 5],
117 ]
118 }
119}
120
121impl Object for Cube {}