rscad_csg_bsp/primitives/
cuboid.rs1use crate::*;
2use nalgebra::{Point, Vector3};
3
4#[doc(alias = "box")]
8pub fn cube<T: Number>(size: T) -> Csg<T> {
9 cuboid(size, size, size)
10}
11
12#[doc(alias = "cube")]
16pub fn cuboid<T: Number>(x: T, y: T, z: T) -> Csg<T> {
17 let two = T::one() + T::one();
18 let hx = x / two;
19 let hy = y / two;
20 let hz = z / two;
21
22 let v = [
36 Point::from([-hx, -hy, -hz]), Point::from([hx, -hy, -hz]), Point::from([hx, hy, -hz]), Point::from([-hx, hy, -hz]), Point::from([-hx, -hy, hz]), Point::from([hx, -hy, hz]), Point::from([hx, hy, hz]), Point::from([-hx, hy, hz]), ];
45
46 let faces: [([Point<T, 3>; 4], Vector3<T>, T); 6] = [
48 ([v[3], v[2], v[1], v[0]], -Vector3::z(), hz), ([v[4], v[5], v[6], v[7]], Vector3::z(), hz), ([v[3], v[0], v[4], v[7]], -Vector3::x(), hx), ([v[1], v[2], v[6], v[5]], Vector3::x(), hx), ([v[0], v[1], v[5], v[4]], -Vector3::y(), hy), ([v[2], v[3], v[7], v[6]], Vector3::y(), hy), ];
55
56 let polygons = faces.into_iter().flat_map(|(verts, normal, dist)| {
57 Polygon::from_points_and_plane(verts, Plane::new(normal, dist))
58 });
59
60 Csg::from_polygons(polygons).translate(Vector3::new(hx, hy, hz))
61}