#![allow(clippy::neg_cmp_op_on_partial_ord)]
use crate::core::BufferGeometry;
use super::{Surface, SurfaceTable};
fn tag_uniform(g: &mut BufferGeometry, surface: Surface) {
let tris = super::triangle_count(g);
g.set_surfaces(SurfaceTable::uniform(surface, tris));
}
pub fn tag_sphere(g: &mut BufferGeometry, radius: f32) {
if !(radius > 0.0) {
return;
}
let s = Surface::sphere([0.0; 3], radius as f64)
.with_axis([0.0, 1.0, 0.0])
.with_x_dir([-1.0, 0.0, 0.0]);
tag_uniform(g, s);
}
pub fn tag_torus(g: &mut BufferGeometry, radius: f32, tube: f32) {
if !(radius > 0.0) || !(tube > 0.0) {
return;
}
let s = Surface::torus([0.0; 3], [0.0, 0.0, 1.0], radius as f64, tube as f64)
.with_x_dir([1.0, 0.0, 0.0]);
tag_uniform(g, s);
}
pub fn tag_xy_plane(g: &mut BufferGeometry) {
let s = Surface::plane([0.0; 3], [0.0, 0.0, 1.0]).with_x_dir([1.0, 0.0, 0.0]);
tag_uniform(g, s);
}
pub fn tag_quad(g: &mut BufferGeometry, corners: &[crate::math::Vector3; 4]) {
use crate::geometries::QuadGeometry;
let c: Vec<[f64; 3]> = corners
.iter()
.map(|p| [p.x as f64, p.y as f64, p.z as f64])
.collect();
let sub = |a: [f64; 3], b: [f64; 3]| [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
let size = (0..4)
.map(|i| {
let d = sub(c[(i + 1) % 4], c[i]);
(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt()
})
.fold(0.0f64, f64::max);
let (d0, d1) = (sub(c[2], c[0]), sub(c[3], c[1]));
let area = [
d0[1] * d1[2] - d0[2] * d1[1],
d0[2] * d1[0] - d0[0] * d1[2],
d0[0] * d1[1] - d0[1] * d1[0],
];
let area_len = (area[0] * area[0] + area[1] * area[1] + area[2] * area[2]).sqrt();
if !(size > 0.0) || !(area_len > 1e-12 * size * size) {
return;
}
let surface = if QuadGeometry::is_planar(corners, 1e-6 * size as f32) {
Surface::plane(c[0], area).with_x_dir(sub(c[1], c[0]))
} else {
match crate::nurbs::NurbsSurface::new(
1,
1,
vec![0.0, 0.0, 1.0, 1.0],
vec![0.0, 0.0, 1.0, 1.0],
2,
2,
&[c[0], c[3], c[1], c[2]],
None,
) {
Ok(patch) => Surface::nurbs(patch),
Err(_) => return,
}
};
tag_uniform(g, surface);
}
pub fn tag_box(g: &mut BufferGeometry, width: f32, height: f32, depth: f32) {
let (hx, hy, hz) = (width as f64 * 0.5, height as f64 * 0.5, depth as f64 * 0.5);
let faces = [
([hx, 0.0, 0.0], [1.0, 0.0, 0.0]),
([-hx, 0.0, 0.0], [-1.0, 0.0, 0.0]),
([0.0, hy, 0.0], [0.0, 1.0, 0.0]),
([0.0, -hy, 0.0], [0.0, -1.0, 0.0]),
([0.0, 0.0, hz], [0.0, 0.0, 1.0]),
([0.0, 0.0, -hz], [0.0, 0.0, -1.0]),
];
let surfaces: Vec<Surface> = faces.iter().map(|&(o, n)| Surface::plane(o, n)).collect();
let tris = super::triangle_count(g);
if tris != 12 {
return;
}
if let Some(t) = SurfaceTable::new(surfaces, (0..12u32).map(|i| i / 2).collect()) {
g.set_surfaces(t);
}
}
pub fn tag_cylinder(
g: &mut BufferGeometry,
radius_top: f32,
radius_bottom: f32,
height: f32,
side_triangles: usize,
top_cap_triangles: usize,
bottom_cap_triangles: usize,
) {
let (rt, rb, h) = (radius_top as f64, radius_bottom as f64, height as f64);
if h.abs() < 1e-12 {
return;
}
let half = h * 0.5;
let side = if (rt - rb).abs() < 1e-12 {
if !(rt > 0.0) {
return;
}
Surface::cylinder([0.0; 3], [0.0, 1.0, 0.0], rt)
} else {
let y_apex = half + h * rt / (rb - rt);
let (axis, far_r, far_d) = if rb > rt {
([0.0, -1.0, 0.0], rb, y_apex + half)
} else {
([0.0, 1.0, 0.0], rt, half - y_apex)
};
match Surface::cone_from_rim([0.0, y_apex, 0.0], axis, far_r, far_d) {
Some(c) => c,
None => return,
}
}
.with_x_dir([0.0, 0.0, 1.0]);
let mut surfaces = vec![side];
let mut tri_face = vec![0u32; side_triangles];
if top_cap_triangles > 0 {
surfaces
.push(Surface::plane([0.0, half, 0.0], [0.0, 1.0, 0.0]).with_x_dir([0.0, 0.0, 1.0]));
tri_face.extend(std::iter::repeat_n(
(surfaces.len() - 1) as u32,
top_cap_triangles,
));
}
if bottom_cap_triangles > 0 {
surfaces
.push(Surface::plane([0.0, -half, 0.0], [0.0, -1.0, 0.0]).with_x_dir([0.0, 0.0, 1.0]));
tri_face.extend(std::iter::repeat_n(
(surfaces.len() - 1) as u32,
bottom_cap_triangles,
));
}
if tri_face.len() != super::triangle_count(g) {
return;
}
if let Some(t) = SurfaceTable::new(surfaces, tri_face) {
g.set_surfaces(t);
}
}
pub fn tag_lathe(
g: &mut BufferGeometry,
points: &[crate::math::Vector2],
phi_start: f32,
phi_length: f32,
) {
use crate::nurbs::{construct, NurbsCurve};
if points.len() < 2 || !(phi_length > 0.0) {
return;
}
let (s, c) = (phi_start as f64).sin_cos();
let profile: Vec<[f64; 3]> = points
.iter()
.map(|p| {
let (x, y) = (p.x as f64, p.y as f64);
[x * s, y, x * c]
})
.collect();
let knots = crate::nurbs::uniform_clamped_knots(1, profile.len());
let Ok(curve) = NurbsCurve::new(1, knots, &profile, None) else {
return;
};
let surface = construct::revolve(&curve, [0.0; 3], [0.0, 1.0, 0.0], phi_length as f64);
tag_uniform(g, Surface::nurbs(surface));
}
pub fn tag_tube(
g: &mut BufferGeometry,
centers: &[crate::math::Vector3],
frames: &[(crate::math::Vector3, crate::math::Vector3)],
radius: f32,
) {
use crate::nurbs::construct;
if centers.len() < 2 || centers.len() != frames.len() || !(radius > 0.0) {
return;
}
let sections: Vec<_> = centers
.iter()
.zip(frames)
.map(|(p, (n, b))| {
construct::circle(
[p.x as f64, p.y as f64, p.z as f64],
[-n.x as f64, -n.y as f64, -n.z as f64],
[-b.x as f64, -b.y as f64, -b.z as f64],
radius as f64,
)
})
.collect();
let Ok(surface) = construct::loft(§ions, 3) else {
return;
};
tag_uniform(g, Surface::nurbs(surface));
}
pub fn tag_extrude(
g: &mut BufferGeometry,
contour: &[crate::math::Vector2],
depth: f32,
cap_triangles: usize,
) {
let n = contour.len();
if n < 3 {
return;
}
let d = depth as f64;
let mut surfaces = vec![
Surface::plane([0.0, 0.0, 0.0], [0.0, 0.0, -1.0]).with_x_dir([1.0, 0.0, 0.0]),
Surface::plane([0.0, 0.0, d], [0.0, 0.0, 1.0]).with_x_dir([1.0, 0.0, 0.0]),
];
let mut tri_face: Vec<u32> = Vec::with_capacity(2 * cap_triangles + 2 * n);
for _ in 0..cap_triangles {
tri_face.push(0);
tri_face.push(1);
}
let mut normals: Vec<Option<[f64; 2]>> = Vec::with_capacity(n);
for i in 0..n {
let a = contour[i];
let b = contour[(i + 1) % n];
let (ex, ey) = ((b.x - a.x) as f64, (b.y - a.y) as f64);
let len = (ex * ex + ey * ey).sqrt();
normals.push((len > 1e-12).then(|| [ey / len, -ex / len]));
}
if normals.iter().all(|x| x.is_none()) {
return; }
for i in 0..n {
let a = contour[i];
let nrm = (0..n)
.find_map(|k| normals[(i + n - k) % n])
.expect("at least one edge is non-degenerate");
surfaces.push(
Surface::plane([a.x as f64, a.y as f64, 0.0], [nrm[0], nrm[1], 0.0])
.with_x_dir([0.0, 0.0, 1.0]),
);
let si = (surfaces.len() - 1) as u32;
tri_face.push(si);
tri_face.push(si);
}
if tri_face.len() != super::triangle_count(g) {
return;
}
if let Some(t) = SurfaceTable::new(surfaces, tri_face) {
g.set_surfaces(t);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::geometries::{
BoxGeometry, CircleGeometry, ConeGeometry, CylinderGeometry, PlaneGeometry, SphereGeometry,
TorusGeometry,
};
fn assert_tagged(g: &BufferGeometry, what: &str, scale: f64) {
let t = g
.surface_table()
.unwrap_or_else(|| panic!("{what}: no provenance attached"));
assert_eq!(t.triangle_count(), super::super::triangle_count(g));
let dev = t.max_deviation(g);
assert!(
dev < 1e-5 * scale.max(1.0),
"{what}: a tagged vertex is {dev} from the surface it claims"
);
}
#[test]
fn quad_is_tagged_plane_or_bilinear_patch() {
use crate::geometries::QuadGeometry;
use crate::math::Vector3;
let v = Vector3::new;
let flat = [
v(0.0, 0.0, 0.0),
v(2.0, 0.0, 1.0),
v(2.5, 1.5, 1.25),
v(0.0, 1.0, 0.0),
];
assert!(QuadGeometry::is_planar(&flat, 1e-6));
let g = QuadGeometry::with_segments(flat, 3, 2);
assert_tagged(&g, "planar quad", 3.0);
let s = &g.surface_table().unwrap().surfaces()[0];
assert_eq!(s.kind(), "plane");
let saddle = [
v(0.0, 0.0, 0.0),
v(1.0, 0.0, 1.0),
v(1.0, 1.0, 0.0),
v(0.0, 1.0, 1.0),
];
let g = QuadGeometry::with_segments(saddle, 4, 4);
assert_tagged(&g, "saddle", 1.0);
let s = &g.surface_table().unwrap().surfaces()[0];
assert_eq!(s.kind(), "nurbs");
for (u, w) in [(0.25f32, 0.5f32), (0.8, 0.1)] {
let p = QuadGeometry::point_at(&saddle, u, w);
let q = s.point(u as f64, w as f64);
assert!(
((p.x as f64 - q[0]).powi(2)
+ (p.y as f64 - q[1]).powi(2)
+ (p.z as f64 - q[2]).powi(2))
.sqrt()
< 1e-6
);
}
let line = [
v(0.0, 0.0, 0.0),
v(1.0, 0.0, 0.0),
v(2.0, 0.0, 0.0),
v(3.0, 0.0, 0.0),
];
assert!(QuadGeometry::new(line).surface_table().is_none());
}
#[test]
fn sphere_is_tagged() {
for &(r, w, h) in &[(1.0f32, 32, 16), (5.0, 8, 6), (0.25, 64, 32)] {
let g = SphereGeometry::new(r, w, h);
assert_tagged(&g, "sphere", r as f64);
assert_eq!(g.surface_table().unwrap().surfaces()[0].kind(), "sphere");
}
}
#[test]
fn sphere_parameterization_matches_the_generators_own() {
let g = SphereGeometry::new(1.0, 32, 16);
let s = &g.surface_table().unwrap().surfaces()[0];
let north = s.point(0.0, std::f64::consts::FRAC_PI_2);
assert!(
crate::nurbs::v3::dist(north, [0.0, 1.0, 0.0]) < 1e-12,
"{north:?}"
);
let seam = s.point(0.0, 0.0);
assert!(
crate::nurbs::v3::dist(seam, [-1.0, 0.0, 0.0]) < 1e-12,
"{seam:?}"
);
}
#[test]
fn box_is_tagged_with_six_planes() {
let g = BoxGeometry::new(2.0, 3.0, 4.0);
assert_tagged(&g, "box", 4.0);
let t = g.surface_table().unwrap();
assert_eq!(t.surfaces().len(), 6);
assert!(t.surfaces().iter().all(|s| s.kind() == "plane"));
assert_eq!(t.groups().len(), 6, "each face must own triangles");
for (_, tris) in t.groups() {
assert_eq!(tris.len(), 2);
}
}
#[test]
fn cylinder_is_tagged_as_a_cylinder_plus_two_caps() {
let g = CylinderGeometry::new(2.0, 2.0, 5.0, 24, 2, false, 0.0, std::f32::consts::PI * 2.0);
assert_tagged(&g, "cylinder", 5.0);
let t = g.surface_table().unwrap();
assert_eq!(t.surfaces().len(), 3);
assert_eq!(t.surfaces()[0].kind(), "cylinder");
assert_eq!(t.surfaces()[1].kind(), "plane");
assert_eq!(t.surfaces()[2].kind(), "plane");
}
#[test]
fn an_open_cylinder_has_no_cap_surfaces() {
let g = CylinderGeometry::new(1.0, 1.0, 3.0, 16, 1, true, 0.0, std::f32::consts::PI * 2.0);
assert_tagged(&g, "open cylinder", 3.0);
let t = g.surface_table().unwrap();
assert_eq!(t.surfaces().len(), 1);
assert_eq!(t.surfaces()[0].kind(), "cylinder");
}
#[test]
fn a_cone_recovers_an_apex_that_is_not_on_the_mesh() {
let g = ConeGeometry::new(2.0, 4.0, 24, 1, false, 0.0, std::f32::consts::PI * 2.0);
assert_tagged(&g, "cone", 4.0);
let t = g.surface_table().unwrap();
assert_eq!(t.surfaces()[0].kind(), "cone");
match &t.surfaces()[0] {
Surface::Cone {
apex, half_angle, ..
} => {
assert!(
crate::nurbs::v3::dist(*apex, [0.0, 2.0, 0.0]) < 1e-9,
"{apex:?}"
);
assert!((half_angle - (2.0f64 / 4.0).atan()).abs() < 1e-9);
}
other => panic!("expected a cone, got {}", other.kind()),
}
}
#[test]
fn a_truncated_cone_extrapolates_its_missing_apex() {
let g = CylinderGeometry::new(1.0, 3.0, 4.0, 24, 1, false, 0.0, std::f32::consts::PI * 2.0);
assert_tagged(&g, "truncated cone", 4.0);
let t = g.surface_table().unwrap();
match &t.surfaces()[0] {
Surface::Cone {
apex,
axis,
half_angle,
..
} => {
assert!(
crate::nurbs::v3::dist(*apex, [0.0, 4.0, 0.0]) < 1e-9,
"{apex:?}"
);
assert!(crate::nurbs::v3::dist(*axis, [0.0, -1.0, 0.0]) < 1e-12);
assert!((half_angle - 0.5f64.atan()).abs() < 1e-9, "{half_angle}");
}
other => panic!("expected a cone, got {}", other.kind()),
}
}
#[test]
fn an_inverted_truncated_cone_points_the_other_way() {
let g = CylinderGeometry::new(3.0, 1.0, 4.0, 24, 1, false, 0.0, std::f32::consts::PI * 2.0);
assert_tagged(&g, "inverted truncated cone", 4.0);
match &g.surface_table().unwrap().surfaces()[0] {
Surface::Cone { apex, axis, .. } => {
assert!(
crate::nurbs::v3::dist(*apex, [0.0, -4.0, 0.0]) < 1e-9,
"{apex:?}"
);
assert!(crate::nurbs::v3::dist(*axis, [0.0, 1.0, 0.0]) < 1e-12);
}
other => panic!("expected a cone, got {}", other.kind()),
}
}
#[test]
fn torus_is_tagged() {
let g = TorusGeometry::new(4.0, 1.0, 16, 24, std::f32::consts::PI * 2.0);
assert_tagged(&g, "torus", 5.0);
assert_eq!(g.surface_table().unwrap().surfaces()[0].kind(), "torus");
}
#[test]
fn plane_and_circle_are_tagged() {
let p = PlaneGeometry::with_segments(3.0, 2.0, 4, 3);
assert_tagged(&p, "plane", 3.0);
assert_eq!(p.surface_table().unwrap().surfaces()[0].kind(), "plane");
let c = CircleGeometry::new(2.0, 24, 0.0, std::f32::consts::PI * 2.0);
assert_tagged(&c, "circle", 2.0);
assert_eq!(c.surface_table().unwrap().surfaces()[0].kind(), "plane");
}
#[test]
fn a_partial_cylinder_arc_is_still_tagged() {
let g = CylinderGeometry::new(1.0, 1.0, 2.0, 16, 1, false, 0.3, 1.9);
assert_tagged(&g, "partial cylinder", 2.0);
}
#[cfg(feature = "openscad")]
#[test]
fn the_kernels_plane_hash_agrees_with_provenance_on_planar_faces() {
use crate::brep::triangle_vertices;
use std::collections::HashMap;
let g = BoxGeometry::new(2.0, 3.0, 4.0);
let t = g.surface_table().expect("box is tagged");
let mut hash_of: HashMap<(i64, i64, i64, i64), Vec<usize>> = HashMap::new();
for tri in 0..t.triangle_count() {
let v = triangle_vertices(&g, tri).unwrap();
hash_of
.entry(crate::exact_csg::plane_key(&v))
.or_default()
.push(tri);
}
let mut by_hash: Vec<Vec<usize>> = hash_of.into_values().collect();
let mut by_provenance: Vec<Vec<usize>> =
t.groups().into_iter().map(|(_, tris)| tris).collect();
by_hash.iter_mut().for_each(|v| v.sort_unstable());
by_provenance.iter_mut().for_each(|v| v.sort_unstable());
by_hash.sort();
by_provenance.sort();
assert_eq!(
by_hash, by_provenance,
"the kernel's plane hash and the provenance disagree about a box's faces"
);
}
#[cfg(feature = "openscad")]
#[test]
fn the_kernels_plane_hash_cannot_see_a_curved_face() {
use crate::brep::triangle_vertices;
use std::collections::HashSet;
let g = SphereGeometry::new(1.0, 16, 8);
let t = g.surface_table().expect("sphere is tagged");
let keys: HashSet<_> = (0..t.triangle_count())
.filter_map(|tri| triangle_vertices(&g, tri))
.map(|v| crate::exact_csg::plane_key(&v))
.collect();
assert_eq!(t.groups().len(), 1, "provenance sees one sphere");
assert!(
keys.len() > 50,
"the hash saw {} faces where there is one surface",
keys.len()
);
}
#[test]
fn lathe_tube_and_extrude_are_tagged() {
use crate::geometries::{ExtrudeGeometry, LatheGeometry, TubeGeometry};
use crate::math::{Vector2, Vector3};
let profile = [
Vector2::new(1.0, 0.0),
Vector2::new(2.0, 1.0),
Vector2::new(1.5, 2.5),
Vector2::new(0.5, 3.0),
];
let lathe = LatheGeometry::new(&profile, 24, 0.0, std::f32::consts::PI * 2.0);
assert_tagged(&lathe, "lathe", 3.0);
assert_eq!(lathe.surface_table().unwrap().surfaces()[0].kind(), "nurbs");
let partial = LatheGeometry::new(&profile, 12, 0.3, 1.9);
assert_tagged(&partial, "partial lathe", 3.0);
let path = crate::curves::CatmullRomCurve3::new(vec![
Vector3::new(-3.0, 0.0, 0.0),
Vector3::new(-1.0, 2.0, 1.0),
Vector3::new(1.0, -1.0, -1.0),
Vector3::new(3.0, 0.5, 0.0),
]);
let tube = TubeGeometry::new(&path, 16, 0.4, 12, false);
assert_tagged(&tube, "tube", 3.0);
assert_eq!(tube.surface_table().unwrap().surfaces()[0].kind(), "nurbs");
let mut shape = crate::curves::Shape::new();
shape
.outline
.curve_path
.add(Box::new(crate::curves::LineCurve::new(
Vector2::new(0.0, 0.0),
Vector2::new(2.0, 0.0),
)));
shape
.outline
.curve_path
.add(Box::new(crate::curves::LineCurve::new(
Vector2::new(2.0, 0.0),
Vector2::new(2.0, 1.0),
)));
shape
.outline
.curve_path
.add(Box::new(crate::curves::LineCurve::new(
Vector2::new(2.0, 1.0),
Vector2::new(0.0, 1.0),
)));
shape
.outline
.curve_path
.add(Box::new(crate::curves::LineCurve::new(
Vector2::new(0.0, 1.0),
Vector2::new(0.0, 0.0),
)));
let ex = ExtrudeGeometry::new(&shape, 3.0, 2);
assert_tagged(&ex, "extrude", 3.0);
let t = ex.surface_table().unwrap();
assert!(
t.surfaces().iter().all(|s| s.kind() == "plane"),
"a straight extrusion has no curved face"
);
assert!(t.surfaces().len() >= 3, "two caps and at least one wall");
}
#[test]
fn a_degenerate_lathe_is_left_untagged() {
use crate::geometries::LatheGeometry;
use crate::math::Vector2;
let profile = [Vector2::new(1.0, 0.0), Vector2::new(1.0, 2.0)];
let g = LatheGeometry::new(&profile, 12, 0.0, 0.0);
assert!(g.surface_table().is_none());
}
#[test]
fn a_degenerate_primitive_is_left_untagged_rather_than_mistagged() {
let g = SphereGeometry::new(0.0, 8, 6);
assert!(
g.surface_table().is_none(),
"a zero-radius sphere has no radius to claim"
);
}
}