ncollide3d/procedural/
cuboid.rs

1#[cfg(feature = "dim2")]
2use super::Polyline;
3#[cfg(feature = "dim3")]
4use super::{IndexBuffer, TriMesh};
5use crate::math::{Point, Vector};
6use na;
7#[cfg(feature = "dim3")]
8use na::Point2;
9use simba::scalar::RealField;
10
11/**
12 * Generates a cuboid shape with a split index buffer.
13 *
14 * # Arguments:
15 * * `extents` - the extents of the cuboid.
16 */
17#[cfg(feature = "dim3")]
18pub fn cuboid<N: RealField + Copy>(extents: &Vector<N>) -> TriMesh<N> {
19    let mut cuboid = unit_cuboid();
20    cuboid.scale_by(extents);
21
22    cuboid
23}
24
25/**
26 * Generates a cuboid shape with a split index buffer.
27 *
28 * The cuboid is centered at the origin, and has its half extents set to 0.5.
29 */
30#[cfg(feature = "dim3")]
31pub fn unit_cuboid<N: RealField + Copy>() -> TriMesh<N> {
32    let mut coords = Vec::with_capacity(8);
33    let mut uvs = Vec::with_capacity(4);
34    let mut normals = Vec::with_capacity(6);
35    let mut faces = Vec::with_capacity(12);
36
37    let _0_5: N = na::convert(0.5);
38    let m0_5 = -_0_5;
39    let _1 = N::one();
40    let m1 = -_1;
41    let _0 = N::zero();
42
43    coords.push(Point::new(m0_5, m0_5, _0_5));
44    coords.push(Point::new(m0_5, m0_5, m0_5));
45    coords.push(Point::new(_0_5, m0_5, m0_5));
46    coords.push(Point::new(_0_5, m0_5, _0_5));
47    coords.push(Point::new(m0_5, _0_5, _0_5));
48    coords.push(Point::new(m0_5, _0_5, m0_5));
49    coords.push(Point::new(_0_5, _0_5, m0_5));
50    coords.push(Point::new(_0_5, _0_5, _0_5));
51
52    uvs.push(Point2::new(_0, _1));
53    uvs.push(Point2::new(_1, _1));
54    uvs.push(Point2::new(_0, _0));
55    uvs.push(Point2::new(_1, _0));
56
57    normals.push(Vector::new(m1, _0, _0));
58    normals.push(Vector::new(_0, _0, m1));
59    normals.push(Vector::new(_1, _0, _0));
60    normals.push(Vector::new(_0, _0, _1));
61    normals.push(Vector::new(_0, m1, _0));
62    normals.push(Vector::new(_0, _1, _0));
63
64    faces.push(Point::new(
65        Point::new(4, 0, 0),
66        Point::new(5, 0, 1),
67        Point::new(0, 0, 2),
68    ));
69    faces.push(Point::new(
70        Point::new(5, 0, 1),
71        Point::new(1, 0, 3),
72        Point::new(0, 0, 2),
73    ));
74
75    faces.push(Point::new(
76        Point::new(5, 1, 0),
77        Point::new(6, 1, 1),
78        Point::new(1, 1, 2),
79    ));
80    faces.push(Point::new(
81        Point::new(6, 1, 1),
82        Point::new(2, 1, 3),
83        Point::new(1, 1, 2),
84    ));
85
86    faces.push(Point::new(
87        Point::new(6, 2, 1),
88        Point::new(7, 2, 0),
89        Point::new(3, 2, 2),
90    ));
91    faces.push(Point::new(
92        Point::new(2, 2, 3),
93        Point::new(6, 2, 1),
94        Point::new(3, 2, 2),
95    ));
96
97    faces.push(Point::new(
98        Point::new(7, 3, 1),
99        Point::new(4, 3, 0),
100        Point::new(0, 3, 2),
101    ));
102    faces.push(Point::new(
103        Point::new(3, 3, 3),
104        Point::new(7, 3, 1),
105        Point::new(0, 3, 2),
106    ));
107
108    faces.push(Point::new(
109        Point::new(0, 4, 2),
110        Point::new(1, 4, 0),
111        Point::new(2, 4, 1),
112    ));
113    faces.push(Point::new(
114        Point::new(3, 4, 3),
115        Point::new(0, 4, 2),
116        Point::new(2, 4, 1),
117    ));
118
119    faces.push(Point::new(
120        Point::new(7, 5, 3),
121        Point::new(6, 5, 1),
122        Point::new(5, 5, 0),
123    ));
124    faces.push(Point::new(
125        Point::new(4, 5, 2),
126        Point::new(7, 5, 3),
127        Point::new(5, 5, 0),
128    ));
129
130    TriMesh::new(
131        coords,
132        Some(normals),
133        Some(uvs),
134        Some(IndexBuffer::Split(faces)),
135    )
136}
137
138/// The contour of a cuboid lying on the x-y plane.
139#[cfg(feature = "dim2")]
140pub fn rectangle<N: RealField + Copy>(extents: &Vector<N>) -> Polyline<N> {
141    let mut rectangle = unit_rectangle();
142
143    rectangle.scale_by(extents);
144
145    rectangle
146}
147
148/// The contour of a unit cuboid lying on the x-y plane.
149#[cfg(feature = "dim2")]
150pub fn unit_rectangle<N: RealField + Copy>() -> Polyline<N> {
151    let _0_5: N = na::convert(0.5);
152    let m0_5: N = -_0_5;
153
154    let mut p_ul = Point::origin();
155    let mut p_ur = Point::origin();
156    let mut p_dl = Point::origin();
157    let mut p_dr = Point::origin();
158
159    p_dl[0] = m0_5;
160    p_dl[1] = m0_5;
161    p_dr[0] = _0_5;
162    p_dr[1] = m0_5;
163    p_ur[0] = _0_5;
164    p_ur[1] = _0_5;
165    p_ul[0] = m0_5;
166    p_ul[1] = _0_5;
167
168    Polyline::new(vec![p_ur, p_ul, p_dl, p_dr], None)
169}