use crate::exact_csg::{boolean as exact_boolean, BooleanOutcome, Op as ExactOp};
use crate::math::{Matrix4, Vector3};
use crate::{
compute_vertex_normals, BoxGeometry, BufferAttribute, BufferGeometry, CsgBrush, CsgEvaluator,
CylinderGeometry, SphereGeometry, ADDITION, INTERSECTION, SUBTRACTION,
};
pub mod scad;
pub mod dsl;
pub mod export;
pub mod freecad;
pub mod schematic;
#[cfg(not(target_arch = "wasm32"))]
pub mod animate;
pub mod frame;
pub mod mechanism;
pub use mechanism::{ContinuumSpec, MechanismSpec, TendonSpec};
pub use scad::{
parse_scad_mechanism, parse_scad_mechanism_at, parse_scad_mechanism_file,
parse_scad_mechanism_file_at,
};
pub const DEFAULT_FN: usize = 32;
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum Solid {
Leaf(BufferGeometry),
Transform { matrix: Matrix4, child: Box<Solid> },
Union(Vec<Solid>),
Difference(Vec<Solid>),
Intersection(Vec<Solid>),
Colored { rgba: [f32; 4], child: Box<Solid> },
}
pub fn cube(size: [f32; 3]) -> Solid {
Solid::Leaf(BoxGeometry::new(size[0], size[1], size[2]))
}
pub fn sphere(r: f32) -> Solid {
sphere_fn(r, DEFAULT_FN)
}
pub fn sphere_fn(r: f32, fn_: usize) -> Solid {
let ws = fn_.max(3);
let hs = (fn_ / 2).max(2);
Solid::Leaf(SphereGeometry::new(r, ws, hs))
}
pub fn cylinder(h: f32, r: f32) -> Solid {
frustum(h, r, r, DEFAULT_FN)
}
pub fn cone(h: f32, r1: f32, r2: f32) -> Solid {
frustum(h, r1, r2, DEFAULT_FN)
}
pub fn frustum(h: f32, r1: f32, r2: f32, fn_: usize) -> Solid {
Solid::Leaf(CylinderGeometry::single_precision(
r2,
r1,
h,
fn_.max(3),
1,
false,
0.0,
std::f32::consts::TAU,
))
}
pub fn polyhedron(points: &[[f32; 3]], faces: &[Vec<u32>]) -> Solid {
let mut positions: Vec<f32> = Vec::new();
for face in faces {
if face.len() < 3 {
continue;
}
for k in 1..face.len() - 1 {
for &vi in &[face[0], face[k], face[k + 1]] {
let p = points[vi as usize];
positions.extend_from_slice(&[p[0], p[1], p[2]]);
}
}
}
let mut vol = 0.0f64;
for t in positions.chunks_exact(9) {
let f = |i: usize| t[i] as f64;
vol += f(0) * (f(4) * f(8) - f(5) * f(7))
+ f(1) * (f(5) * f(6) - f(3) * f(8))
+ f(2) * (f(3) * f(7) - f(4) * f(6));
}
if vol < 0.0 {
for t in positions.chunks_exact_mut(9) {
for k in 0..3 {
t.swap(3 + k, 6 + k);
}
}
}
let mut g = BufferGeometry::new();
g.set_attribute("position", BufferAttribute::new(positions, 3));
compute_vertex_normals(&mut g);
Solid::Leaf(g)
}
pub fn linear_extrude(height: f32, outline: &[[f32; 2]]) -> Solid {
Solid::Leaf(extrude_polygon(outline, height))
}
pub fn linear_extrude_holes(height: f32, outline: &[[f32; 2]], holes: &[Vec<[f32; 2]>]) -> Solid {
let mut solid = linear_extrude(height, outline);
let margin = (height.abs()).max(0.01);
for hole in holes {
if hole.len() < 3 {
continue;
}
let punch = linear_extrude(height + 2.0 * margin, hole).translate([0.0, 0.0, -margin]);
solid = solid.difference(punch);
}
solid
}
pub fn rotate_extrude(angle: f32, profile: &[[f32; 2]]) -> Solid {
rotate_extrude_fn(angle, profile, DEFAULT_FN)
}
pub fn rotate_extrude_fn(angle: f32, profile: &[[f32; 2]], segments: usize) -> Solid {
let pts: Vec<crate::math::Vector2> = profile
.iter()
.map(|p| crate::math::Vector2::new(p[0], p[1]))
.collect();
Solid::Leaf(crate::LatheGeometry::new(
&pts,
segments.max(3),
0.0,
angle.clamp(0.0, std::f32::consts::TAU),
))
}
fn signed_area(pts: &[[f32; 2]]) -> f32 {
let n = pts.len();
let mut a = 0.0;
for i in 0..n {
let p = pts[i];
let q = pts[(i + 1) % n];
a += p[0] * q[1] - q[0] * p[1];
}
a * 0.5
}
fn extrude_polygon(outline: &[[f32; 2]], height: f32) -> BufferGeometry {
if outline.len() < 3 {
return BufferGeometry::new();
}
let mut ring = outline.to_vec();
if signed_area(&ring) < 0.0 {
ring.reverse(); }
let pts2: Vec<crate::math::Vector2> = ring
.iter()
.map(|p| crate::math::Vector2::new(p[0], p[1]))
.collect();
let n = pts2.len();
let cap = crate::curves::earcut::earcut(&pts2, &[]);
let mut positions: Vec<f32> = Vec::new();
let mut normals: Vec<f32> = Vec::new();
let mut indices: Vec<u32> = Vec::new();
for p in &pts2 {
positions.extend_from_slice(&[p.x, p.y, 0.0]);
normals.extend_from_slice(&[0.0, 0.0, -1.0]);
}
for p in &pts2 {
positions.extend_from_slice(&[p.x, p.y, height]);
normals.extend_from_slice(&[0.0, 0.0, 1.0]);
}
for t in cap.chunks_exact(3) {
indices.extend_from_slice(&[t[0], t[2], t[1]]); let off = n as u32;
indices.extend_from_slice(&[off + t[0], off + t[1], off + t[2]]); }
for i in 0..n {
let a = pts2[i];
let b = pts2[(i + 1) % n];
let (ex, ey) = (b.x - a.x, b.y - a.y);
let len = (ex * ex + ey * ey).sqrt().max(1e-8);
let (nx, ny) = (ey / len, -ex / len); let base = (positions.len() / 3) as u32;
positions.extend_from_slice(&[a.x, a.y, 0.0]);
positions.extend_from_slice(&[b.x, b.y, 0.0]);
positions.extend_from_slice(&[b.x, b.y, height]);
positions.extend_from_slice(&[a.x, a.y, height]);
for _ in 0..4 {
normals.extend_from_slice(&[nx, ny, 0.0]);
}
indices.extend_from_slice(&[base, base + 1, base + 2, base, base + 2, base + 3]);
}
let mut g = BufferGeometry::new();
g.set_attribute("position", BufferAttribute::new(positions, 3));
g.set_attribute("normal", BufferAttribute::new(normals, 3));
g.set_index(indices);
g
}
impl Solid {
pub fn translate(self, t: [f32; 3]) -> Solid {
self.transformed(Matrix4::translation(Vector3::new(t[0], t[1], t[2])))
}
pub fn scale(self, s: [f32; 3]) -> Solid {
self.transformed(Matrix4::scale(Vector3::new(s[0], s[1], s[2])))
}
pub fn rotate_x(self, rad: f32) -> Solid {
self.transformed(rot_x(rad))
}
pub fn rotate_y(self, rad: f32) -> Solid {
self.transformed(rot_y(rad))
}
pub fn rotate_z(self, rad: f32) -> Solid {
self.transformed(rot_z(rad))
}
pub fn transform(self, m: Matrix4) -> Solid {
self.transformed(m)
}
pub fn rotate(self, deg: [f32; 3]) -> Solid {
self.rotate_x(deg[0].to_radians())
.rotate_y(deg[1].to_radians())
.rotate_z(deg[2].to_radians())
}
pub fn rotate_axis(self, deg: f32, axis: [f32; 3]) -> Solid {
let (x, y, z) = (axis[0], axis[1], axis[2]);
let len = (x * x + y * y + z * z).sqrt();
if len < 1e-9 {
return self;
}
let (x, y, z) = (x / len, y / len, z / len);
let (s, c) = deg.to_radians().sin_cos();
let t = 1.0 - c;
let mut m = Matrix4::identity();
m.elements = [
t * x * x + c,
t * x * y + s * z,
t * x * z - s * y,
0.0, t * x * y - s * z,
t * y * y + c,
t * y * z + s * x,
0.0, t * x * z + s * y,
t * y * z - s * x,
t * z * z + c,
0.0, 0.0,
0.0,
0.0,
1.0, ];
self.transformed(m)
}
pub fn mirror(self, normal: [f32; 3]) -> Solid {
let (x, y, z) = (normal[0], normal[1], normal[2]);
let len = (x * x + y * y + z * z).sqrt();
if len < 1e-9 {
return self;
}
let (nx, ny, nz) = (x / len, y / len, z / len);
let mut m = Matrix4::identity();
m.elements = [
1.0 - 2.0 * nx * nx,
-2.0 * nx * ny,
-2.0 * nx * nz,
0.0,
-2.0 * nx * ny,
1.0 - 2.0 * ny * ny,
-2.0 * ny * nz,
0.0,
-2.0 * nx * nz,
-2.0 * ny * nz,
1.0 - 2.0 * nz * nz,
0.0,
0.0,
0.0,
0.0,
1.0,
];
self.transformed(m)
}
fn transformed(self, m: Matrix4) -> Solid {
match self {
Solid::Transform { matrix, child } => Solid::Transform {
matrix: m.multiply(&matrix),
child,
},
other => Solid::Transform {
matrix: m,
child: Box::new(other),
},
}
}
pub fn union(self, other: Solid) -> Solid {
match self {
Solid::Union(mut xs) => {
xs.push(other);
Solid::Union(xs)
}
s => Solid::Union(vec![s, other]),
}
}
pub fn difference(self, other: Solid) -> Solid {
match self {
Solid::Difference(mut xs) => {
xs.push(other);
Solid::Difference(xs)
}
s => Solid::Difference(vec![s, other]),
}
}
pub fn intersection(self, other: Solid) -> Solid {
match self {
Solid::Intersection(mut xs) => {
xs.push(other);
Solid::Intersection(xs)
}
s => Solid::Intersection(vec![s, other]),
}
}
pub fn to_geometry(self) -> BufferGeometry {
run_csg(move || {
let mut ev = CsgEvaluator::new();
self.evaluate(&mut ev)
})
}
pub fn evaluate(self, ev: &mut CsgEvaluator) -> BufferGeometry {
eval_solid(self, ev)
}
pub fn to_geometry_exact(self) -> BufferGeometry {
run_csg(move || eval_exact(self))
}
pub fn to_stl(self) -> Vec<u8> {
geometry_to_stl(&self.to_geometry_exact())
}
pub fn to_obj(self) -> String {
export::geometry_to_obj(&self.to_geometry_exact())
}
pub fn to_off(self) -> String {
export::geometry_to_off(&self.to_geometry_exact())
}
pub fn to_ply(self) -> String {
export::geometry_to_ply(&self.to_geometry_exact())
}
pub fn to_3mf(self) -> Vec<u8> {
export::geometry_to_3mf(&self.to_geometry_exact())
}
pub fn to_glb(self) -> Vec<u8> {
export::geometry_to_glb(&self.to_geometry_exact())
}
pub fn to_fcstd(self) -> Vec<u8> {
freecad::geometry_to_fcstd(&self.to_geometry_exact(), "Model")
}
pub fn to_fcstd_with(
self,
opts: &freecad::FcstdWriteOptions,
) -> Result<(Vec<u8>, freecad::FcstdWriteReport), freecad::FcstdError> {
freecad::geometry_to_fcstd_with(&self.to_geometry_exact(), "Model", opts)
}
pub fn to_fcstd_parts(self) -> Vec<u8> {
freecad::parts_to_fcstd(&self.parts(), "Model")
}
pub fn color(self, rgb: [f32; 3]) -> Solid {
self.color_rgba([rgb[0], rgb[1], rgb[2], 1.0])
}
pub fn color_rgba(self, rgba: [f32; 4]) -> Solid {
Solid::Colored {
rgba,
child: Box::new(self),
}
}
pub fn color_named(self, name: &str) -> Solid {
match css_color(name) {
Some(rgba) => self.color_rgba(rgba),
None => self,
}
}
pub fn parts(self) -> Vec<ScadPart> {
self.into_parts(true)
}
pub fn parts_float(self) -> Vec<ScadPart> {
self.into_parts(false)
}
fn into_parts(self, exact: bool) -> Vec<ScadPart> {
let mut split = Vec::new();
split_colors(self, None, &mut split);
let mut groups: Vec<(Option<[f32; 4]>, Vec<Solid>)> = Vec::new();
for (color, solid) in split {
match groups.iter_mut().find(|(c, _)| same_color(*c, color)) {
Some((_, solids)) => solids.push(solid),
None => groups.push((color, vec![solid])),
}
}
run_csg(move || {
let mut out = Vec::with_capacity(groups.len());
for (color, solids) in groups {
let geometry = if solids.len() <= 48 {
let Some(solid) = merge_group(solids) else {
continue;
};
if exact {
eval_exact(solid)
} else {
eval_solid(solid, &mut CsgEvaluator::new())
}
} else {
let mut geoms: Vec<BufferGeometry> = Vec::with_capacity(solids.len());
for solid in solids {
let g = if exact {
eval_exact(solid)
} else {
eval_solid(solid, &mut CsgEvaluator::new())
};
if g.attributes.get("position").is_some_and(|a| !a.array.is_empty()) {
geoms.push(g);
}
}
if geoms.is_empty() {
continue;
}
let mut acc = geoms.remove(0);
for g in geoms {
acc = concat_geometry(&acc, &g);
}
acc
};
if geometry
.attributes
.get("position")
.is_none_or(|a| a.array.is_empty())
{
continue;
}
out.push(ScadPart { geometry, color });
}
out
})
}
}
fn merge_group(mut solids: Vec<Solid>) -> Option<Solid> {
match solids.len() {
0 => None,
1 => solids.pop(),
_ => Some(Solid::Union(solids)),
}
}
#[derive(Debug, Clone)]
pub struct ScadPart {
pub geometry: BufferGeometry,
pub color: Option<[f32; 4]>,
}
impl ScadPart {
pub fn rgba_or(&self, default: [f32; 4]) -> [f32; 4] {
self.color.unwrap_or(default)
}
pub fn is_transparent(&self) -> bool {
self.color.is_some_and(|c| c[3] < 0.999)
}
}
fn same_color(a: Option<[f32; 4]>, b: Option<[f32; 4]>) -> bool {
match (a, b) {
(None, None) => true,
(Some(x), Some(y)) => x.iter().zip(y.iter()).all(|(p, q)| (p - q).abs() < 1e-6),
_ => false,
}
}
fn split_colors(solid: Solid, color: Option<[f32; 4]>, out: &mut Vec<(Option<[f32; 4]>, Solid)>) {
match solid {
Solid::Colored { rgba, child } => split_colors(*child, Some(rgba), out),
Solid::Union(xs) => {
for x in xs {
split_colors(x, color, out);
}
}
Solid::Transform { matrix, child } => {
let mut inner = Vec::new();
split_colors(*child, color, &mut inner);
for (c, s) in inner {
out.push((
c,
Solid::Transform {
matrix,
child: Box::new(s),
},
));
}
}
Solid::Difference(xs) => split_boolean(xs, color, out, false),
Solid::Intersection(xs) => split_boolean(xs, color, out, true),
leaf => out.push((color, leaf)),
}
}
fn split_boolean(
xs: Vec<Solid>,
color: Option<[f32; 4]>,
out: &mut Vec<(Option<[f32; 4]>, Solid)>,
intersect: bool,
) {
let mut it = xs.into_iter();
let Some(head) = it.next() else { return };
let rest: Vec<Solid> = it.collect();
let rest_bounds: Vec<Option<Bounds>> = rest.iter().map(solid_bounds).collect();
let mut pieces = Vec::new();
split_colors(head, color, &mut pieces);
'piece: for (c, s) in pieces {
let piece_bounds = solid_bounds(&s);
let mut operands = vec![s];
for (operand, bounds) in rest.iter().zip(rest_bounds.iter()) {
let misses = match (piece_bounds, bounds) {
(Some(a), Some(b)) => !overlaps(a, *b),
_ => false,
};
if misses {
if intersect {
continue 'piece; }
continue; }
operands.push(operand.clone());
}
let solid = if operands.len() == 1 {
operands.into_iter().next().expect("len 1")
} else if intersect {
Solid::Intersection(operands)
} else {
Solid::Difference(operands)
};
out.push((c, solid));
}
}
type Bounds = ([f32; 3], [f32; 3]);
fn overlaps(a: Bounds, b: Bounds) -> bool {
(0..3).all(|i| a.0[i] <= b.1[i] && b.0[i] <= a.1[i])
}
fn solid_bounds(s: &Solid) -> Option<Bounds> {
match s {
Solid::Leaf(g) => {
let (min, max) = geometry_bounds(g);
(min[0] <= max[0]).then_some((min, max))
}
Solid::Colored { child, .. } => solid_bounds(child),
Solid::Transform { matrix, child } => {
let (min, max) = solid_bounds(child)?;
let mut lo = [f32::MAX; 3];
let mut hi = [f32::MIN; 3];
for i in 0..8 {
let corner = Vector3::new(
if i & 1 == 0 { min[0] } else { max[0] },
if i & 2 == 0 { min[1] } else { max[1] },
if i & 4 == 0 { min[2] } else { max[2] },
);
let p = corner.apply_matrix4(matrix);
for (k, v) in [p.x, p.y, p.z].into_iter().enumerate() {
lo[k] = lo[k].min(v);
hi[k] = hi[k].max(v);
}
}
Some((lo, hi))
}
Solid::Union(xs) => xs.iter().filter_map(solid_bounds).reduce(|a, b| {
let mut lo = [0.0; 3];
let mut hi = [0.0; 3];
for i in 0..3 {
lo[i] = a.0[i].min(b.0[i]);
hi[i] = a.1[i].max(b.1[i]);
}
(lo, hi)
}),
Solid::Difference(xs) => xs.first().and_then(solid_bounds),
Solid::Intersection(xs) => xs.iter().filter_map(solid_bounds).reduce(|a, b| {
let mut lo = [0.0; 3];
let mut hi = [0.0; 3];
for i in 0..3 {
lo[i] = a.0[i].max(b.0[i]);
hi[i] = a.1[i].min(b.1[i]);
}
(lo, hi)
}),
}
}
pub fn css_color(name: &str) -> Option<[f32; 4]> {
let name = name.trim();
if let Some(hex) = name.strip_prefix('#') {
let byte = |i: usize| u8::from_str_radix(&hex[i..i + 2], 16).ok();
let nib = |i: usize| u8::from_str_radix(&hex[i..i + 1], 16).ok().map(|v| v * 17);
let (r, g, b, a) = match hex.len() {
3 => (nib(0)?, nib(1)?, nib(2)?, 255),
4 => (nib(0)?, nib(1)?, nib(2)?, nib(3)?),
6 => (byte(0)?, byte(2)?, byte(4)?, 255),
8 => (byte(0)?, byte(2)?, byte(4)?, byte(6)?),
_ => return None,
};
return Some([
r as f32 / 255.0,
g as f32 / 255.0,
b as f32 / 255.0,
a as f32 / 255.0,
]);
}
let lower = name.to_ascii_lowercase();
let rgb = CSS_COLORS
.iter()
.find(|(n, _)| *n == lower)
.map(|(_, rgb)| *rgb)?;
Some([
((rgb >> 16) & 0xFF) as f32 / 255.0,
((rgb >> 8) & 0xFF) as f32 / 255.0,
(rgb & 0xFF) as f32 / 255.0,
1.0,
])
}
#[rustfmt::skip]
const CSS_COLORS: &[(&str, u32)] = &[
("aliceblue", 0xF0F8FF), ("antiquewhite", 0xFAEBD7), ("aqua", 0x00FFFF),
("aquamarine", 0x7FFFD4), ("azure", 0xF0FFFF), ("beige", 0xF5F5DC),
("bisque", 0xFFE4C4), ("black", 0x000000), ("blanchedalmond", 0xFFEBCD),
("blue", 0x0000FF), ("blueviolet", 0x8A2BE2), ("brown", 0xA52A2A),
("burlywood", 0xDEB887), ("cadetblue", 0x5F9EA0), ("chartreuse", 0x7FFF00),
("chocolate", 0xD2691E), ("coral", 0xFF7F50), ("cornflowerblue", 0x6495ED),
("cornsilk", 0xFFF8DC), ("crimson", 0xDC143C), ("cyan", 0x00FFFF),
("darkblue", 0x00008B), ("darkcyan", 0x008B8B), ("darkgoldenrod", 0xB8860B),
("darkgray", 0xA9A9A9), ("darkgreen", 0x006400), ("darkgrey", 0xA9A9A9),
("darkkhaki", 0xBDB76B), ("darkmagenta", 0x8B008B), ("darkolivegreen", 0x556B2F),
("darkorange", 0xFF8C00), ("darkorchid", 0x9932CC), ("darkred", 0x8B0000),
("darksalmon", 0xE9967A), ("darkseagreen", 0x8FBC8F), ("darkslateblue", 0x483D8B),
("darkslategray", 0x2F4F4F), ("darkslategrey", 0x2F4F4F), ("darkturquoise", 0x00CED1),
("darkviolet", 0x9400D3), ("deeppink", 0xFF1493), ("deepskyblue", 0x00BFFF),
("dimgray", 0x696969), ("dimgrey", 0x696969), ("dodgerblue", 0x1E90FF),
("firebrick", 0xB22222), ("floralwhite", 0xFFFAF0), ("forestgreen", 0x228B22),
("fuchsia", 0xFF00FF), ("gainsboro", 0xDCDCDC), ("ghostwhite", 0xF8F8FF),
("gold", 0xFFD700), ("goldenrod", 0xDAA520), ("gray", 0x808080),
("green", 0x008000), ("greenyellow", 0xADFF2F), ("grey", 0x808080),
("honeydew", 0xF0FFF0), ("hotpink", 0xFF69B4), ("indianred", 0xCD5C5C),
("indigo", 0x4B0082), ("ivory", 0xFFFFF0), ("khaki", 0xF0E68C),
("lavender", 0xE6E6FA), ("lavenderblush", 0xFFF0F5), ("lawngreen", 0x7CFC00),
("lemonchiffon", 0xFFFACD), ("lightblue", 0xADD8E6), ("lightcoral", 0xF08080),
("lightcyan", 0xE0FFFF), ("lightgoldenrodyellow", 0xFAFAD2), ("lightgray", 0xD3D3D3),
("lightgreen", 0x90EE90), ("lightgrey", 0xD3D3D3), ("lightpink", 0xFFB6C1),
("lightsalmon", 0xFFA07A), ("lightseagreen", 0x20B2AA), ("lightskyblue", 0x87CEFA),
("lightslategray", 0x778899), ("lightslategrey", 0x778899), ("lightsteelblue", 0xB0C4DE),
("lightyellow", 0xFFFFE0), ("lime", 0x00FF00), ("limegreen", 0x32CD32),
("linen", 0xFAF0E6), ("magenta", 0xFF00FF), ("maroon", 0x800000),
("mediumaquamarine", 0x66CDAA), ("mediumblue", 0x0000CD), ("mediumorchid", 0xBA55D3),
("mediumpurple", 0x9370DB), ("mediumseagreen", 0x3CB371), ("mediumslateblue", 0x7B68EE),
("mediumspringgreen", 0x00FA9A), ("mediumturquoise", 0x48D1CC), ("mediumvioletred", 0xC71585),
("midnightblue", 0x191970), ("mintcream", 0xF5FFFA), ("mistyrose", 0xFFE4E1),
("moccasin", 0xFFE4B5), ("navajowhite", 0xFFDEAD), ("navy", 0x000080),
("oldlace", 0xFDF5E6), ("olive", 0x808000), ("olivedrab", 0x6B8E23),
("orange", 0xFFA500), ("orangered", 0xFF4500), ("orchid", 0xDA70D6),
("palegoldenrod", 0xEEE8AA), ("palegreen", 0x98FB98), ("paleturquoise", 0xAFEEEE),
("palevioletred", 0xDB7093), ("papayawhip", 0xFFEFD5), ("peachpuff", 0xFFDAB9),
("peru", 0xCD853F), ("pink", 0xFFC0CB), ("plum", 0xDDA0DD),
("powderblue", 0xB0E0E6), ("purple", 0x800080), ("red", 0xFF0000),
("rosybrown", 0xBC8F8F), ("royalblue", 0x4169E1), ("saddlebrown", 0x8B4513),
("salmon", 0xFA8072), ("sandybrown", 0xF4A460), ("seagreen", 0x2E8B57),
("seashell", 0xFFF5EE), ("sienna", 0xA0522D), ("silver", 0xC0C0C0),
("skyblue", 0x87CEEB), ("slateblue", 0x6A5ACD), ("slategray", 0x708090),
("slategrey", 0x708090), ("snow", 0xFFFAFA), ("springgreen", 0x00FF7F),
("steelblue", 0x4682B4), ("tan", 0xD2B48C), ("teal", 0x008080),
("thistle", 0xD8BFD8), ("tomato", 0xFF6347), ("turquoise", 0x40E0D0),
("violet", 0xEE82EE), ("wheat", 0xF5DEB3), ("white", 0xFFFFFF),
("whitesmoke", 0xF5F5F5), ("yellow", 0xFFFF00), ("yellowgreen", 0x9ACD32),
];
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Origin {
#[default]
AsModelled,
Center,
CenterOnFloor,
}
pub fn geometry_bounds(g: &BufferGeometry) -> ([f32; 3], [f32; 3]) {
let mut lo = [f32::INFINITY; 3];
let mut hi = [f32::NEG_INFINITY; 3];
if let Some(a) = g.get_attribute("position") {
for v in a.array.chunks_exact(3) {
for k in 0..3 {
lo[k] = lo[k].min(v[k]);
hi[k] = hi[k].max(v[k]);
}
}
}
(lo, hi)
}
pub fn set_origin(g: &mut BufferGeometry, o: Origin) {
if o == Origin::AsModelled {
return;
}
let (lo, hi) = geometry_bounds(g);
if !lo[0].is_finite() {
return;
}
let d = match o {
Origin::AsModelled => return,
Origin::Center => [
-(lo[0] + hi[0]) / 2.0,
-(lo[1] + hi[1]) / 2.0,
-(lo[2] + hi[2]) / 2.0,
],
Origin::CenterOnFloor => [-(lo[0] + hi[0]) / 2.0, -(lo[1] + hi[1]) / 2.0, -lo[2]],
};
if let Some(a) = g.attributes.get_mut("position") {
for v in a.array.chunks_exact_mut(3) {
for k in 0..3 {
v[k] += d[k];
}
}
}
}
pub fn geometry_to_stl(g: &BufferGeometry) -> Vec<u8> {
if g.get_attribute("position").is_none() {
return Vec::new();
}
crate::exporters::write_stl_binary(&[crate::exporters::ExportObject::mesh("", g.clone())])
}
pub fn solid(s: Solid) -> Solid {
s
}
pub fn union_all(solids: Vec<Solid>) -> Solid {
Solid::Union(solids)
}
pub fn hull(solids: Vec<Solid>) -> Solid {
let mut pts = Vec::new();
for s in &solids {
pts.extend(scad::solid_points(s));
}
scad::hull3d(&pts).unwrap_or(Solid::Union(solids))
}
pub fn difference_all(solids: Vec<Solid>) -> Solid {
Solid::Difference(solids)
}
pub fn intersection_all(solids: Vec<Solid>) -> Solid {
Solid::Intersection(solids)
}
#[cfg(not(target_arch = "wasm32"))]
fn install_quiet_csg_hook() {
use std::sync::Once;
static HOOK: Once = Once::new();
HOOK.call_once(|| {
let prev = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
if std::thread::current().name() != Some("threers-csg") {
prev(info);
}
}));
});
}
#[cfg(not(target_arch = "wasm32"))]
fn run_csg<T: Send + 'static + Default>(f: impl FnOnce() -> T + Send + 'static) -> T {
install_quiet_csg_hook();
std::thread::Builder::new()
.name("threers-csg".into())
.stack_size(1024 * 1024 * 1024)
.spawn(move || {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).unwrap_or_default()
})
.expect("spawn csg worker thread")
.join()
.unwrap_or_default()
}
#[cfg(target_arch = "wasm32")]
fn run_csg<T: Default>(f: impl FnOnce() -> T + std::panic::UnwindSafe) -> T {
std::panic::catch_unwind(f).unwrap_or_default()
}
fn float_boolean(acc: BufferGeometry, cg: BufferGeometry, op: u8) -> BufferGeometry {
let fallback = acc.clone();
std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
let mut a = CsgBrush::new(acc);
a.prepare_geometry();
let mut b = CsgBrush::new(cg);
b.prepare_geometry();
evaluate_shared(&mut CsgEvaluator::new(), &mut a, &mut b, op)
}))
.unwrap_or(fallback)
}
fn evaluate_shared(
ev: &mut CsgEvaluator,
a: &mut CsgBrush,
b: &mut CsgBrush,
op: u8,
) -> BufferGeometry {
let requested = ev.attributes.clone();
ev.attributes
.retain(|k| a.geometry.get_attribute(k).is_some() && b.geometry.get_attribute(k).is_some());
let out = ev.evaluate(a, b, op);
ev.attributes = requested;
out
}
fn eval_solid(s: Solid, ev: &mut CsgEvaluator) -> BufferGeometry {
match s {
Solid::Leaf(g) => g,
Solid::Transform { matrix, child } => {
let mut g = eval_solid(*child, ev);
bake_matrix(&mut g, &matrix);
g
}
Solid::Union(xs) => fold(xs, ev, ADDITION),
Solid::Difference(xs) => fold(xs, ev, SUBTRACTION),
Solid::Intersection(xs) => fold(xs, ev, INTERSECTION),
Solid::Colored { child, .. } => eval_solid(*child, ev),
}
}
fn fold(children: Vec<Solid>, ev: &mut CsgEvaluator, op: u8) -> BufferGeometry {
let mut it = children.into_iter();
let seed = match it.next() {
Some(s) => eval_solid(s, ev),
None => return BufferGeometry::new(),
};
let mut acc = CsgBrush::new(seed);
acc.prepare_geometry();
for child in it {
let mut b = CsgBrush::new(eval_solid(child, ev));
b.prepare_geometry();
let out = evaluate_shared(ev, &mut acc, &mut b, op);
acc = CsgBrush::new(out);
acc.prepare_geometry();
}
acc.geometry
}
fn eval_exact(s: Solid) -> BufferGeometry {
match s {
Solid::Leaf(g) => g,
Solid::Transform { matrix, child } => {
let mut g = eval_exact(*child);
bake_matrix(&mut g, &matrix);
g
}
Solid::Union(xs) => fold_exact(xs, ExactOp::Union, ADDITION),
Solid::Difference(xs) => fold_exact(xs, ExactOp::Difference, SUBTRACTION),
Solid::Intersection(xs) => fold_exact(xs, ExactOp::Intersection, INTERSECTION),
Solid::Colored { child, .. } => eval_exact(*child),
}
}
fn geom_bounds(g: &BufferGeometry) -> Option<([f32; 3], [f32; 3])> {
let pos = g.get_attribute("position")?;
if pos.array.is_empty() {
return None;
}
let (mut lo, mut hi) = ([f32::MAX; 3], [f32::MIN; 3]);
for v in pos.array.chunks_exact(3) {
for k in 0..3 {
lo[k] = lo[k].min(v[k]);
hi[k] = hi[k].max(v[k]);
}
}
Some((lo, hi))
}
fn bounds_overlap(a: ([f32; 3], [f32; 3]), b: ([f32; 3], [f32; 3])) -> bool {
const EPS: f32 = 1e-4;
(0..3).all(|k| a.0[k] - EPS <= b.1[k] && b.0[k] - EPS <= a.1[k])
}
pub fn concat_geometry(a: &BufferGeometry, b: &BufferGeometry) -> BufferGeometry {
let tri = |g: &BufferGeometry| -> Vec<f32> {
let Some(pos) = g.get_attribute("position") else {
return Vec::new();
};
match &g.index {
Some(ix) => ix
.iter()
.flat_map(|&i| {
let i = i as usize * 3;
[pos.array[i], pos.array[i + 1], pos.array[i + 2]]
})
.collect(),
None => pos.array.clone(),
}
};
let mut positions = tri(a);
positions.extend(tri(b));
let mut g = BufferGeometry::new();
g.set_attribute("position", BufferAttribute::new(positions, 3));
compute_vertex_normals(&mut g);
g
}
fn union_geometries(mut parts: Vec<BufferGeometry>, float_op: u8) -> BufferGeometry {
parts.retain(|g| {
g.get_attribute("position")
.is_some_and(|p| !p.array.is_empty())
});
if parts.is_empty() {
return BufferGeometry::new();
}
while parts.len() > 1 {
let mut next: Vec<BufferGeometry> = Vec::with_capacity(parts.len().div_ceil(2));
let mut it = parts.into_iter();
while let Some(a) = it.next() {
match it.next() {
None => next.push(a),
Some(b) => {
let disjoint = match (geom_bounds(&a), geom_bounds(&b)) {
(Some(ba), Some(bb)) => !bounds_overlap(ba, bb),
_ => false,
};
next.push(if disjoint {
concat_geometry(&a, &b)
} else {
boolean_or_fallback(a, b, ExactOp::Union, float_op)
});
}
}
}
parts = next;
}
parts.pop().unwrap_or_default()
}
fn boolean_or_fallback(
acc: BufferGeometry,
cg: BufferGeometry,
exact_op: ExactOp,
float_op: u8,
) -> BufferGeometry {
#[cfg(feature = "manifold")]
if let Some(g) = crate::exact_csg::manifold_backend::boolean(&acc, &cg, exact_op) {
return g;
}
match exact_boolean(&acc, &cg, exact_op) {
BooleanOutcome::Exact(g) => g,
BooleanOutcome::NeedsArrangement => {
let g = float_boolean(acc, cg, float_op);
let tris = crate::exact_csg::triangles(&g);
if crate::exact_csg::is_closed_manifold(&tris) {
g
} else if let Some(fixed) = crate::exact_csg::repair_geometry(&g) {
fixed
} else {
crate::exact_csg::note_unverified_boolean();
log::warn!(
"exact CSG declined this boolean and the float fallback left a \
non-watertight mesh ({} triangles) that could not be healed — \
the result is NOT exact and may be self-intersecting",
tris.len()
);
g
}
}
}
}
fn difference_once(
acc: BufferGeometry,
cg: BufferGeometry,
exact_op: ExactOp,
float_op: u8,
) -> BufferGeometry {
#[cfg(feature = "manifold")]
if let Some(g) = crate::exact_csg::manifold_backend::boolean(&acc, &cg, exact_op) {
return g;
}
match exact_boolean(&acc, &cg, exact_op) {
BooleanOutcome::Exact(g) => g,
BooleanOutcome::NeedsArrangement => {
let g = float_boolean(acc, cg, float_op);
let tris = crate::exact_csg::triangles(&g);
if crate::exact_csg::is_closed_manifold(&tris) {
g
} else if let Some(fixed) = crate::exact_csg::repair_geometry(&g) {
fixed
} else {
crate::exact_csg::note_unverified_boolean();
log::warn!(
"exact CSG declined this boolean and the float fallback left a \
non-watertight mesh ({} triangles) that could not be healed — \
the result is NOT exact and may be self-intersecting",
tris.len()
);
g
}
}
}
}
fn fold_exact(children: Vec<Solid>, exact_op: ExactOp, float_op: u8) -> BufferGeometry {
if matches!(exact_op, ExactOp::Union) {
return union_geometries(children.into_iter().map(eval_exact).collect(), float_op);
}
let mut it = children.into_iter();
let mut acc = match it.next() {
Some(s) => eval_exact(s),
None => return BufferGeometry::new(),
};
if matches!(exact_op, ExactOp::Difference) {
let cutters: Vec<BufferGeometry> = it.map(eval_exact).collect();
if cutters.len() > 1 {
let merged = union_geometries(cutters, ADDITION);
return difference_once(acc, merged, exact_op, float_op);
}
for cg in cutters {
acc = difference_once(acc, cg, exact_op, float_op);
}
return acc;
}
for child in it {
let cg = eval_exact(child);
acc = difference_once(acc, cg, exact_op, float_op);
}
acc
}
pub(crate) fn bake_matrix(geom: &mut BufferGeometry, m: &Matrix4) {
#[cfg(feature = "brep")]
let moved_table = geom.surface_table().and_then(|t| t.transform(m));
if let Some(attr) = geom.attributes.get_mut("position") {
for v in attr.array.chunks_exact_mut(3) {
let p = Vector3::new(v[0], v[1], v[2]).apply_matrix4(m);
v[0] = p.x;
v[1] = p.y;
v[2] = p.z;
}
}
if m.determinant() < 0.0 {
reverse_winding(geom);
}
geom.bounding_box = None;
geom.bounding_sphere = None;
compute_vertex_normals(geom);
#[cfg(feature = "brep")]
if let Some(t) = moved_table {
geom.set_surfaces(t);
}
}
fn reverse_winding(geom: &mut BufferGeometry) {
if let Some(idx) = geom.index.as_mut() {
for t in idx.chunks_exact_mut(3) {
t.swap(0, 2);
}
} else if let Some(attr) = geom.attributes.get_mut("position") {
for tri in attr.array.chunks_exact_mut(9) {
for k in 0..3 {
tri.swap(k, 6 + k);
}
}
}
}
fn rot_x(a: f32) -> Matrix4 {
let (s, c) = a.sin_cos();
let mut m = Matrix4::identity();
m.elements = [
1.0, 0.0, 0.0, 0.0, 0.0, c, s, 0.0, 0.0, -s, c, 0.0, 0.0, 0.0, 0.0, 1.0, ];
m
}
fn rot_y(a: f32) -> Matrix4 {
let (s, c) = a.sin_cos();
let mut m = Matrix4::identity();
m.elements = [
c, 0.0, -s, 0.0, 0.0, 1.0, 0.0, 0.0, s, 0.0, c, 0.0, 0.0, 0.0, 0.0, 1.0, ];
m
}
fn rot_z(a: f32) -> Matrix4 {
let (s, c) = a.sin_cos();
let mut m = Matrix4::identity();
m.elements = [
c, s, 0.0, 0.0, -s, c, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, ];
m
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cube_leaf_has_positions() {
let g = cube([2.0, 2.0, 2.0]).to_geometry();
assert!(g.attributes.get("position").unwrap().count() > 0);
}
fn vert_count(p: &ScadPart) -> usize {
p.geometry.attributes.get("position").unwrap().array.len() / 3
}
#[test]
fn css_colors_resolve_by_name_and_hex() {
assert_eq!(css_color("red"), Some([1.0, 0.0, 0.0, 1.0]));
assert_eq!(css_color(" SteelBlue "), css_color("steelblue"));
assert_eq!(css_color("#0f0"), Some([0.0, 1.0, 0.0, 1.0]));
assert_eq!(css_color("#00ff00"), Some([0.0, 1.0, 0.0, 1.0]));
let half = css_color("#00ff0080").unwrap();
assert!((half[3] - 128.0 / 255.0).abs() < 1e-6);
assert_eq!(css_color("chartreusey"), None);
assert_eq!(css_color("#12345"), None);
}
#[test]
fn an_uncolored_model_is_a_single_part() {
let s = cube([10.0, 10.0, 10.0]).union(cube([10.0, 10.0, 10.0]).translate([5.0, 0.0, 0.0]));
let parts = s.clone().parts();
assert_eq!(parts.len(), 1, "no color() means one part");
assert!(parts[0].color.is_none());
assert_eq!(vert_count(&parts[0]), {
let g = s.to_geometry_exact();
g.attributes.get("position").unwrap().array.len() / 3
});
}
#[test]
fn color_splits_a_union_into_parts() {
let s = cube([10.0, 10.0, 10.0]).color_named("red").union(
cube([4.0, 4.0, 4.0])
.translate([20.0, 0.0, 0.0])
.color([0.0, 0.0, 1.0]),
);
let parts = s.parts();
assert_eq!(parts.len(), 2);
assert_eq!(parts[0].color, Some([1.0, 0.0, 0.0, 1.0]));
assert_eq!(parts[1].color, Some([0.0, 0.0, 1.0, 1.0]));
}
#[test]
fn same_color_pieces_merge_into_one_part() {
let s = cube([10.0, 10.0, 10.0]).color_named("red").union(
cube([4.0, 4.0, 4.0])
.translate([20.0, 0.0, 0.0])
.color_named("red"),
);
assert_eq!(s.parts().len(), 1, "one color, one part");
}
#[test]
fn a_difference_applies_to_every_colored_piece_of_its_body() {
let body = cube([20.0, 20.0, 20.0]).color_named("red").union(
cube([20.0, 20.0, 20.0])
.translate([30.0, 0.0, 0.0])
.color_named("blue"),
);
let cut = body.difference(cylinder(60.0, 4.0));
let parts = cut.parts();
assert_eq!(parts.len(), 2);
assert_eq!(parts[0].color, Some([1.0, 0.0, 0.0, 1.0]));
assert_eq!(parts[1].color, Some([0.0, 0.0, 1.0, 1.0]));
let plain_cube = vert_count(&cube([20.0, 20.0, 20.0]).parts()[0]);
assert!(
vert_count(&parts[0]) > plain_cube,
"the red part should carry the bore: {} vs {plain_cube}",
vert_count(&parts[0])
);
assert_eq!(vert_count(&parts[1]), plain_cube, "blue was not in the way");
}
#[test]
fn an_unknown_color_name_leaves_the_subtree_untagged() {
let s = cube([1.0, 1.0, 1.0]).color_named("not-a-color");
assert!(matches!(s, Solid::Leaf(_)), "no Colored wrapper");
assert!(s.parts()[0].color.is_none());
}
#[test]
fn alpha_marks_a_part_transparent() {
let opaque = cube([1.0, 1.0, 1.0]).color([1.0, 0.0, 0.0]);
assert!(!opaque.parts()[0].is_transparent());
let glass = cube([1.0, 1.0, 1.0]).color_rgba([1.0, 0.0, 0.0, 0.4]);
let parts = glass.parts();
assert!(parts[0].is_transparent());
assert_eq!(parts[0].rgba_or([0.0; 4]), [1.0, 0.0, 0.0, 0.4]);
}
#[test]
fn color_is_invisible_to_the_csg_kernel() {
let plain = cube([10.0, 10.0, 10.0])
.difference(sphere(6.0))
.to_geometry_exact();
let tinted = cube([10.0, 10.0, 10.0])
.color_named("red")
.difference(sphere(6.0).color_named("blue"))
.to_geometry_exact();
assert_eq!(
plain.attributes.get("position").unwrap().array.len(),
tinted.attributes.get("position").unwrap().array.len(),
"color() must not change the mesh"
);
}
#[test]
fn difference_produces_geometry() {
let g = cube([3.0, 3.0, 3.0]).difference(sphere(1.0)).to_geometry();
assert!(g.draw_count() > 0);
assert_eq!(g.draw_count() % 3, 0);
}
#[test]
fn union_and_intersection_nonempty() {
let u = cube([1.0, 1.0, 1.0])
.union(cube([1.0, 1.0, 1.0]).translate([0.5, 0.0, 0.0]))
.to_geometry();
assert!(u.draw_count() > 0);
let i = cube([2.0, 2.0, 2.0])
.intersection(sphere(1.3))
.to_geometry();
assert!(i.draw_count() > 0);
}
#[test]
fn translate_moves_bounds() {
let mut a = cube([2.0, 2.0, 2.0]).to_geometry();
let mut b = cube([2.0, 2.0, 2.0])
.translate([10.0, 0.0, 0.0])
.to_geometry();
let ca = a.compute_bounding_box().center();
let cb = b.compute_bounding_box().center();
assert!(
(cb.x - ca.x - 10.0).abs() < 1e-3,
"translate should shift +10 in x"
);
}
#[test]
fn chained_booleans_flatten() {
let s = cube([1.0, 1.0, 1.0])
.union(cube([1.0, 1.0, 1.0]))
.union(cube([1.0, 1.0, 1.0]));
match s {
Solid::Union(ref xs) => assert_eq!(xs.len(), 3),
_ => panic!("expected a flat 3-ary union"),
}
}
fn signed_volume(g: &BufferGeometry) -> f32 {
let pos = &g.attributes.get("position").unwrap().array;
let read = |i: u32| {
let j = i as usize * 3;
Vector3::new(pos[j], pos[j + 1], pos[j + 2])
};
let mut vol = 0.0;
let mut tri = |a: Vector3, b: Vector3, c: Vector3| vol += a.dot(b.cross(c));
if let Some(idx) = &g.index {
for t in idx.chunks_exact(3) {
tri(read(t[0]), read(t[1]), read(t[2]));
}
} else {
for k in 0..pos.len() / 9 {
let b = (k * 3) as u32;
tri(read(b), read(b + 1), read(b + 2));
}
}
vol / 6.0
}
#[test]
fn linear_extrude_square_is_a_box() {
let sq = [[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5], [-0.5, 0.5]];
let mut g = linear_extrude(2.0, &sq).to_geometry();
assert!(
(signed_volume(&g) - 2.0).abs() < 1e-4,
"outward winding + volume"
);
let bb = g.compute_bounding_box();
assert!((bb.min.z - 0.0).abs() < 1e-6 && (bb.max.z - 2.0).abs() < 1e-6);
}
#[test]
fn linear_extrude_accepts_clockwise_input() {
let cw = [[-0.5, -0.5], [-0.5, 0.5], [0.5, 0.5], [0.5, -0.5]];
let g = linear_extrude(1.0, &cw).to_geometry();
assert!(signed_volume(&g) > 0.0);
}
#[test]
fn linear_extrude_works_as_csg_operand() {
let sq = [[-2.0, -2.0], [2.0, -2.0], [2.0, 2.0], [-2.0, 2.0]];
let g = linear_extrude(3.0, &sq)
.difference(cylinder(10.0, 1.0).rotate_x(std::f32::consts::FRAC_PI_2))
.to_geometry();
assert!(g.draw_count() > 0 && g.draw_count().is_multiple_of(3));
}
#[test]
fn linear_extrude_with_hole_has_correct_volume() {
let outer = [[-2.0, -2.0], [2.0, -2.0], [2.0, 2.0], [-2.0, 2.0]];
let hole = vec![[-1.0, -1.0], [1.0, -1.0], [1.0, 1.0], [-1.0, 1.0]];
let g = linear_extrude_holes(2.0, &outer, &[hole]).to_geometry();
assert!(
(signed_volume(&g) - 24.0).abs() < 1e-3,
"outward winding + hole volume"
);
}
#[test]
fn linear_extrude_with_two_holes_volume_and_operand() {
let outer = [[-4.0, -2.0], [4.0, -2.0], [4.0, 2.0], [-4.0, 2.0]]; let h1 = vec![[-3.0, -1.0], [-1.0, -1.0], [-1.0, 1.0], [-3.0, 1.0]]; let h2 = vec![[1.0, -1.0], [3.0, -1.0], [3.0, 1.0], [1.0, 1.0]]; let plate = linear_extrude_holes(2.0, &outer, &[h1, h2]);
let g = plate.clone().to_geometry();
assert!((signed_volume(&g).abs() - (32.0 - 4.0 - 4.0) * 2.0).abs() < 1e-3);
let combined = plate
.union(sphere(1.0).translate([0.0, 0.0, 1.0]))
.to_geometry();
assert!(combined.draw_count() > 0 && combined.draw_count().is_multiple_of(3));
}
#[test]
fn rotate_extrude_full_is_nonempty() {
let profile = [[2.0, -0.5], [3.0, -0.5], [3.0, 0.5], [2.0, 0.5]];
let g = rotate_extrude(std::f32::consts::TAU, &profile).to_geometry();
assert!(g.draw_count() > 0);
}
#[test]
fn polyhedron_tetrahedron() {
let pts = [
[1.0, 1.0, 1.0],
[-1.0, -1.0, 1.0],
[-1.0, 1.0, -1.0],
[1.0, -1.0, -1.0],
];
let faces = vec![vec![0, 1, 2], vec![0, 3, 1], vec![0, 2, 3], vec![1, 3, 2]];
let g = polyhedron(&pts, &faces).to_geometry();
assert_eq!(g.attributes.get("position").unwrap().count(), 12);
}
}
#[cfg(test)]
mod exact_integration {
use super::*;
use crate::BufferGeometry;
fn svol(g: &BufferGeometry) -> f64 {
let pos = &g.attributes.get("position").unwrap().array;
let v = |i: usize| {
[
pos[i * 3] as f64,
pos[i * 3 + 1] as f64,
pos[i * 3 + 2] as f64,
]
};
let tri = |a: [f64; 3], b: [f64; 3], c: [f64; 3]| {
a[0] * (b[1] * c[2] - b[2] * c[1])
+ a[1] * (b[2] * c[0] - b[0] * c[2])
+ a[2] * (b[0] * c[1] - b[1] * c[0])
};
let mut s = 0.0;
if let Some(idx) = &g.index {
for t in idx.chunks_exact(3) {
s += tri(v(t[0] as usize), v(t[1] as usize), v(t[2] as usize));
}
} else {
for k in 0..pos.len() / 9 {
s += tri(v(k * 3), v(k * 3 + 1), v(k * 3 + 2));
}
}
s / 6.0
}
#[test]
fn exact_path_matches_float_on_oblique_boxes() {
let a = || cube([2.0, 2.0, 2.0]);
let b = || {
cube([2.0, 2.0, 2.0])
.rotate_x(0.6)
.rotate_y(0.4)
.translate([0.7, 0.5, 0.3])
};
for (name, exact, float) in [
(
"diff",
a().difference(b()).to_geometry_exact(),
a().difference(b()).to_geometry(),
),
(
"union",
a().union(b()).to_geometry_exact(),
a().union(b()).to_geometry(),
),
(
"inter",
a().intersection(b()).to_geometry_exact(),
a().intersection(b()).to_geometry(),
),
] {
let (ve, vf) = (svol(&exact).abs(), svol(&float).abs());
assert!(
(ve - vf).abs() / vf < 1e-2,
"{name}: exact {ve:.4} vs float {vf:.4}"
);
}
}
fn tri_count(g: &BufferGeometry) -> usize {
match &g.index {
Some(idx) => idx.len() / 3,
None => g
.attributes
.get("position")
.map(|a| a.count() / 3)
.unwrap_or(0),
}
}
#[test]
fn stl_export_roundtrips_through_the_loader() {
let solid = cube([3.0, 2.0, 1.0]).difference(sphere(0.7));
let mut geom = solid.to_geometry();
let stl = geometry_to_stl(&geom);
assert_eq!(stl.len(), 84 + tri_count(&geom) * 50, "binary STL size");
let mut back = crate::StlLoader::parse_binary(&stl);
assert_eq!(
tri_count(&geom),
tri_count(&back),
"triangle count survives"
);
let (ba, bb) = (geom.compute_bounding_box(), back.compute_bounding_box());
for k in 0..3 {
let (amin, bmin) = (
[ba.min.x, ba.min.y, ba.min.z][k],
[bb.min.x, bb.min.y, bb.min.z][k],
);
let (amax, bmax) = (
[ba.max.x, ba.max.y, ba.max.z][k],
[bb.max.x, bb.max.y, bb.max.z][k],
);
assert!(
(amin - bmin).abs() < 1e-4 && (amax - bmax).abs() < 1e-4,
"bounds axis {k}"
);
}
}
#[test]
fn exact_path_handles_the_full_bracket() {
let g = cube([40.0, 24.0, 4.0])
.difference(
cylinder(20.0, 2.5)
.rotate_x(std::f32::consts::FRAC_PI_2)
.translate([-12.0, 0.0, 0.0]),
)
.difference(
cylinder(20.0, 2.5)
.rotate_x(std::f32::consts::FRAC_PI_2)
.translate([12.0, 0.0, 0.0]),
)
.union(sphere(5.0).translate([0.0, 0.0, 2.0]))
.to_geometry_exact();
assert!(g.draw_count() > 0 && g.draw_count().is_multiple_of(3));
}
#[test]
fn union_fold_of_many_curved_solids_stays_exact() {
let mut part = cube([40.0, 40.0, 10.0]);
for i in 0..4 {
let x = -15.0 + i as f32 * 10.0;
part = part.union(sphere_fn(3.0, 32).translate([x, 0.0, 4.0]));
}
let g = part.to_geometry_exact();
let tris = crate::exact_csg::triangles(&g);
assert!(
crate::exact_csg::is_closed_manifold(&tris),
"union fold produced a non-watertight mesh ({} triangles)",
tris.len()
);
assert!(
tris.len() < 5_000,
"triangle blow-up: {} triangles",
tris.len()
);
}
#[test]
fn exact_boolean_is_deterministic() {
let build = || {
let mut part = cube([40.0, 40.0, 10.0]);
for i in 0..4 {
let x = -15.0 + i as f32 * 10.0;
part = part.union(sphere_fn(3.0, 32).translate([x, 0.0, 4.0]));
}
part.to_geometry_exact()
};
let (a, b) = (build(), build());
assert_eq!(
a.get_attribute("position").unwrap().array,
b.get_attribute("position").unwrap().array,
"two evaluations of the same solid disagree"
);
}
#[test]
fn difference_against_filleted_block_stays_exact() {
use std::f32::consts::FRAC_PI_2;
let (r, h) = (7.0f32, 40.0f32);
let o = 50.0 - r;
let post = |x: f32, y: f32| {
frustum(h, r, r, 32)
.rotate_x(FRAC_PI_2)
.translate([x, y, h / 2.0])
};
let block = hull(vec![post(o, o), post(-o, o), post(-o, -o), post(o, -o)]);
let slab = |r: f32, z: f32| {
frustum(0.5, r, r, 8)
.rotate_x(FRAC_PI_2)
.translate([0.0, 0.0, z])
};
let well = hull(vec![slab(42.0, h + 1.75), slab(35.0, h - 25.75)]);
let mut g = block.difference(well).to_geometry_exact();
let tris = crate::exact_csg::triangles(&g);
assert!(
crate::exact_csg::is_closed_manifold(&tris),
"difference produced a non-watertight mesh ({} triangles)",
tris.len()
);
let bb = g.compute_bounding_box();
assert!(
bb.max.z <= h + 1e-3,
"result exceeds the block: z max {}",
bb.max.z
);
}
}
#[allow(clippy::too_many_arguments)]
pub fn build_printer(
bed_x: f32,
bed_y: f32,
z_travel: f32,
ext: f32,
gantry_z: f32,
carriage: f32,
bed_pos: f32,
) -> Vec<(BufferGeometry, [f32; 3])> {
use std::f32::consts::{FRAC_PI_2, PI};
let c_ext = [0.75, 0.77, 0.81];
let c_motor = [0.10, 0.10, 0.12];
let c_metal = [0.85, 0.87, 0.90];
let c_lead = [0.80, 0.66, 0.28];
let c_bed = [0.14, 0.15, 0.19];
let c_part = [0.20, 0.55, 0.95];
let c_belt = [0.05, 0.05, 0.06];
let c_print = [0.93, 0.49, 0.18];
let c_box = [0.18, 0.20, 0.25];
let c_dark = [0.12, 0.13, 0.16];
let beam = |l: f32, w: f32| cube([l, w, w]).translate([l / 2.0, 0.0, 0.0]);
let cz = |h: f32, r: f32| {
frustum(h, r, r, 48)
.rotate_x(FRAC_PI_2)
.translate([0.0, 0.0, h / 2.0])
};
let taper = |h: f32, r1: f32, r2: f32| {
cone(h, r1, r2)
.rotate_x(FRAC_PI_2)
.translate([0.0, 0.0, h / 2.0])
};
let mut parts: Vec<(BufferGeometry, [f32; 3])> = Vec::new();
macro_rules! p {
($s:expr, $c:expr) => {
parts.push(($s.to_geometry(), $c));
};
}
macro_rules! nema {
($len:expr, $place:expr) => {{
let (b, len, place) = (42.3f32, $len, $place);
p!(
place(cube([b, b, len]).translate([0.0, 0.0, len / 2.0])),
c_motor
);
p!(
place(cube([b - 4.0, b - 4.0, 2.0]).translate([0.0, 0.0, len + 1.0])),
c_dark
); p!(
place(cz(2.5, 11.0).translate([0.0, 0.0, len + 2.0])),
c_metal
); p!(
place(cz(22.0, 2.5).translate([0.0, 0.0, len + 4.0])),
c_metal
); for sx in [-1.0f32, 1.0] {
for sy in [-1.0f32, 1.0] {
p!(
place(cz(3.0, 2.2).translate([sx * 15.5, sy * 15.5, len + 2.0])),
c_metal
); }
}
p!(place(cz(4.0, 15.0).translate([0.0, 0.0, -4.0])), c_dark); }};
}
let (xl, xr, yb) = (-30.0f32, bed_x + 30.0, bed_y + 30.0);
let (fw, fd, z0) = (xr - xl, yb + 30.0, ext / 2.0);
let yf = yb - ext;
p!(beam(fw, ext).translate([xl, -30.0, z0]), c_ext); p!(beam(fw, ext).translate([xl, yb, z0]), c_ext); p!(
beam(fd, ext).rotate_z(FRAC_PI_2).translate([xl, -30.0, z0]),
c_ext
); p!(
beam(fd, ext).rotate_z(FRAC_PI_2).translate([xr, -30.0, z0]),
c_ext
); p!(
beam(z_travel, ext)
.rotate_y(-FRAC_PI_2)
.translate([xl, yb, z0]),
c_ext
); p!(
beam(z_travel, ext)
.rotate_y(-FRAC_PI_2)
.translate([xr, yb, z0]),
c_ext
); p!(beam(fw, ext).translate([xl, yb, z_travel]), c_ext); for (fx, fy) in [(xl, -30.0), (xr, -30.0), (xl, yb), (xr, yb)] {
p!(cz(6.0, ext * 0.55).translate([fx, fy, -6.0]), c_dark); }
nema!(40.0, |s: Solid| s.translate([xl, yf, ext + 2.0]));
p!(cz(12.0, 5.0).translate([xl, yf, ext + 46.0]), c_metal); p!(
cz((z_travel - ext - 60.0).max(10.0), 3.5).translate([xl, yf, ext + 58.0]),
c_lead
); p!(
cz((z_travel - ext).max(10.0), 3.0).translate([xr, yf, ext]),
c_metal
);
p!(beam(fw, ext).translate([xl, yf, gantry_z]), c_ext); p!(
cube([ext + 10.0, ext + 12.0, 26.0]).translate([xl, yf, gantry_z]),
c_part
); p!(
cube([ext + 10.0, ext + 12.0, 26.0]).translate([xr, yf, gantry_z]),
c_part
); p!(
cube([fw - 24.0, 2.0, 5.0]).translate([bed_x / 2.0, yf - 12.0, gantry_z + ext / 2.0 - 2.0]),
c_belt
); let cx = xl + carriage.clamp(0.0, bed_x);
p!(
cube([32.0, 26.0, 30.0]).translate([cx, yf - 12.0, gantry_z]),
c_part
); p!(
cz(6.0, 5.0)
.rotate_y(FRAC_PI_2)
.translate([xl - 7.0, yf, gantry_z]),
c_metal
); p!(
cz(16.0, 8.0).translate([cx, yf - 18.0, gantry_z - 24.0]),
c_metal
); p!(
cube([16.0, 14.0, 12.0]).translate([cx, yf - 18.0, gantry_z - 26.0]),
c_box
); p!(
taper(9.0, 4.0, 1.2)
.rotate_x(PI)
.translate([cx, yf - 18.0, gantry_z - 34.0]),
c_metal
); nema!(34.0, |s: Solid| s.rotate_y(-FRAC_PI_2).translate([
xr + 10.0,
yf,
gantry_z
])); nema!(28.0, |s: Solid| s.rotate_x(-FRAC_PI_2).translate([
cx,
yf - 2.0,
gantry_z + 8.0
]));
let yc = bed_y / 2.0 + bed_pos;
p!(
cube([bed_x + 10.0, bed_y + 10.0, 3.0]).translate([bed_x / 2.0, yc, ext + 10.0]),
c_bed
); p!(
cube([bed_x * 0.72, bed_y * 0.72, 4.0]).translate([bed_x / 2.0, yc, ext + 5.0]),
c_metal
); for sx in [-1.0f32, 1.0] {
for sy in [-1.0f32, 1.0] {
p!(
cz(7.0, 4.0).translate([
bed_x / 2.0 + sx * bed_x * 0.33,
yc + sy * bed_y * 0.33,
ext - 1.0
]),
c_metal
); }
}
p!(
cube([2.0, fd - 24.0, 5.0]).translate([bed_x / 2.0, (fd - 24.0) / 2.0 - 30.0, ext + 2.0]),
c_belt
); nema!(38.0, |s: Solid| s.rotate_x(-FRAC_PI_2).translate([
bed_x / 2.0,
-30.0,
ext + 2.0
]));
p!(
cube([70.0, 40.0, 46.0]).translate([xr + 34.0, bed_y / 2.0, 24.0]),
c_box
); p!(
cz(16.0, 42.0).rotate_y(FRAC_PI_2).translate([
bed_x / 2.0 + 60.0,
yb + 6.0,
z_travel + 30.0
]),
c_print
); p!(
cz(80.0, 6.0).rotate_y(FRAC_PI_2).translate([
bed_x / 2.0 - 30.0,
yb + 6.0,
z_travel + 30.0
]),
c_metal
); p!(
beam(46.0, ext)
.rotate_y(-FRAC_PI_2)
.translate([bed_x / 2.0 - 30.0, yb, z_travel]),
c_ext
);
p!(
cz(26.0, 15.0).translate([bed_x / 2.0, yc, ext + 12.0]),
c_print
);
parts
}
pub fn build_nema17() -> Vec<(BufferGeometry, [f32; 3])> {
use std::f32::consts::FRAC_PI_2;
let c_cap = [0.80, 0.82, 0.86];
let c_stack = [0.09, 0.09, 0.11];
let c_rod = [0.55, 0.57, 0.61];
let c_metal = [0.88, 0.90, 0.93];
let c_hole = [0.04, 0.04, 0.05];
let c_conn = [0.10, 0.10, 0.12];
let wires = [
[0.85, 0.15, 0.15],
[0.15, 0.70, 0.25],
[0.20, 0.45, 0.92],
[0.85, 0.80, 0.20],
];
let cz = |h: f32, r: f32| {
frustum(h, r, r, 48)
.rotate_x(FRAC_PI_2)
.translate([0.0, 0.0, h / 2.0])
};
let wy = |h: f32, r: f32, x: f32, y0: f32, z: f32| {
frustum(h, r, r, 18).translate([x, y0 - h / 2.0, z])
};
let mut parts: Vec<(BufferGeometry, [f32; 3])> = Vec::new();
macro_rules! p {
($s:expr, $c:expr) => {
parts.push(($s.to_geometry(), $c));
};
}
let b = 42.3; let cap = 8.0; let body = 42.0; let inset = 2.2;
p!(cube([b, b, cap]).translate([0.0, 0.0, cap / 2.0]), c_cap); p!(
cube([b - 2.0 * inset, b - 2.0 * inset, body - 2.0 * cap + 6.0]).translate([
0.0,
0.0,
body / 2.0
]),
c_stack
);
p!(
cube([b, b, cap]).translate([0.0, 0.0, body - cap / 2.0]),
c_cap
);
let tr = b / 2.0 - 3.3;
for sx in [-1.0f32, 1.0] {
for sy in [-1.0f32, 1.0] {
p!(cz(body, 2.0).translate([sx * tr, sy * tr, 0.0]), c_rod);
}
}
for sx in [-1.0f32, 1.0] {
for sy in [-1.0f32, 1.0] {
p!(
cz(3.5, 2.6).translate([sx * 15.5, sy * 15.5, body - 3.0]),
c_hole
);
}
}
p!(cz(2.5, 11.0).translate([0.0, 0.0, body]), c_metal);
p!(cz(24.0, 2.5).translate([0.0, 0.0, body + 2.0]), c_metal);
p!(
cube([15.0, 5.0, 8.0]).translate([0.0, -b / 2.0 - 1.5, cap + 2.0]),
c_conn
);
for (i, wc) in wires.iter().enumerate() {
let x = (i as f32 - 1.5) * 3.2;
p!(wy(22.0, 1.0, x, -b / 2.0 - 3.5, cap + 2.0), *wc);
}
parts
}