use super::MeshData3;
use crate::common::arc_segments_for_tol;
use crate::geom3::IsoExtensions3;
use crate::io::read_tc_mesh_from;
use crate::{Iso3, Point3, Result, Vector3};
use std::f64::consts::{FRAC_PI_2, PI, TAU};
fn push_ring(points: &mut Vec<Point3>, radius: f64, z: f64, n: usize) -> u32 {
let start = points.len() as u32;
for i in 0..n {
let angle = TAU * (i as f64) / (n as f64);
points.push(Point3::new(radius * angle.cos(), radius * angle.sin(), z));
}
start
}
fn band_faces(faces: &mut Vec<[u32; 3]>, lower: u32, upper: u32, n: usize) {
let n = n as u32;
for i in 0..n {
let j = (i + 1) % n;
faces.push([lower + i, lower + j, upper + j]);
faces.push([lower + i, upper + j, upper + i]);
}
}
fn fan_faces(faces: &mut Vec<[u32; 3]>, center: u32, ring: u32, n: usize, up: bool) {
let n = n as u32;
for i in 0..n {
let j = (i + 1) % n;
if up {
faces.push([center, ring + i, ring + j]);
} else {
faces.push([center, ring + j, ring + i]);
}
}
}
impl MeshData3 {
pub fn create_box(length: f64, width: f64, height: f64) -> Self {
let hx = length / 2.0;
let hy = width / 2.0;
let hz = height / 2.0;
let points = vec![
Point3::new(-hx, -hy, -hz),
Point3::new(hx, -hy, -hz),
Point3::new(hx, hy, -hz),
Point3::new(-hx, hy, -hz),
Point3::new(-hx, -hy, hz),
Point3::new(hx, -hy, hz),
Point3::new(hx, hy, hz),
Point3::new(-hx, hy, hz),
];
let faces = vec![
[0, 2, 1], [0, 3, 2],
[4, 5, 6], [4, 6, 7],
[0, 1, 5], [0, 5, 4],
[1, 2, 6], [1, 6, 5],
[2, 3, 7], [2, 7, 6],
[3, 0, 4], [3, 4, 7],
];
Self::new(points, faces).expect("A box tessellation is always a valid mesh")
}
pub fn create_sphere(radius: f64, tol: f64) -> Result<Self> {
let n_theta = arc_segments_for_tol(radius, TAU, tol / 2.0)?;
let n_phi = arc_segments_for_tol(radius, PI, tol / 2.0)?;
let mut points = Vec::with_capacity(n_theta * (n_phi - 1) + 2);
let mut faces = Vec::with_capacity(2 * n_theta * (n_phi - 1));
let north = points.len() as u32;
points.push(Point3::new(0.0, 0.0, radius));
let mut rings = Vec::with_capacity(n_phi - 1);
for k in 1..n_phi {
let phi = PI * (k as f64) / (n_phi as f64);
let (sin, cos) = phi.sin_cos();
rings.push(push_ring(&mut points, radius * sin, radius * cos, n_theta));
}
let south = points.len() as u32;
points.push(Point3::new(0.0, 0.0, -radius));
fan_faces(&mut faces, north, rings[0], n_theta, true);
for w in rings.windows(2) {
band_faces(&mut faces, w[1], w[0], n_theta);
}
fan_faces(&mut faces, south, rings[rings.len() - 1], n_theta, false);
Ok(Self::new(points, faces).expect("A sphere tessellation is always a valid mesh"))
}
pub fn create_cylinder(radius: f64, height: f64, tol: f64) -> Result<Self> {
let n = arc_segments_for_tol(radius, TAU, tol)?;
let hz = height / 2.0;
let mut points = Vec::with_capacity(2 * n + 2);
let mut faces = Vec::with_capacity(4 * n);
let bottom = push_ring(&mut points, radius, -hz, n);
let top = push_ring(&mut points, radius, hz, n);
band_faces(&mut faces, bottom, top, n);
let bottom_center = points.len() as u32;
points.push(Point3::new(0.0, 0.0, -hz));
fan_faces(&mut faces, bottom_center, bottom, n, false);
let top_center = points.len() as u32;
points.push(Point3::new(0.0, 0.0, hz));
fan_faces(&mut faces, top_center, top, n, true);
Ok(Self::new(points, faces).expect("A cylinder tessellation is always a valid mesh"))
}
pub fn create_cone(radius: f64, height: f64, tol: f64) -> Result<Self> {
let n = arc_segments_for_tol(radius, TAU, tol)?;
let hz = height / 2.0;
let mut points = Vec::with_capacity(n + 2);
let mut faces = Vec::with_capacity(2 * n);
let base = push_ring(&mut points, radius, -hz, n);
let apex = points.len() as u32;
points.push(Point3::new(0.0, 0.0, hz));
for i in 0..n as u32 {
let j = (i + 1) % n as u32;
faces.push([base + i, base + j, apex]);
}
let base_center = points.len() as u32;
points.push(Point3::new(0.0, 0.0, -hz));
fan_faces(&mut faces, base_center, base, n, false);
Ok(Self::new(points, faces).expect("A cone tessellation is always a valid mesh"))
}
pub fn create_circle(radius: f64, tol: f64) -> Result<Self> {
let n = arc_segments_for_tol(radius, TAU, tol)?;
let mut points = Vec::with_capacity(n + 1);
let mut faces = Vec::with_capacity(n);
points.push(Point3::origin());
let ring = push_ring(&mut points, radius, 0.0, n);
fan_faces(&mut faces, 0, ring, n, true);
Ok(Self::new(points, faces).expect("A triangle fan is always a valid mesh"))
}
}
impl MeshData3 {
pub fn create_capsule(p0: &Point3, p1: &Point3, radius: f64, tol: f64) -> Result<Self> {
let v = *p1 - *p0;
let pc = *p0 + v / 2.0;
let transform = Iso3::from_basis_zx(&v, &Vector3::x(), Some(pc))
.or_else(|_| Iso3::from_basis_zy(&v, &Vector3::y(), Some(pc)))
.map_err(|_| "The two ends of a capsule must not be coincident")?;
let n_theta = arc_segments_for_tol(radius, TAU, tol / 2.0)?;
let n_cap = arc_segments_for_tol(radius, FRAC_PI_2, tol / 2.0)?;
let hz = v.norm() / 2.0;
let mut points = Vec::with_capacity(2 * n_cap * n_theta + 2);
let mut faces = Vec::with_capacity(4 * n_cap * n_theta);
let north = points.len() as u32;
points.push(Point3::new(0.0, 0.0, hz + radius));
let mut rings = Vec::with_capacity(2 * n_cap);
for k in 1..=n_cap {
let phi = FRAC_PI_2 * (k as f64) / (n_cap as f64);
let (sin, cos) = phi.sin_cos();
rings.push(push_ring(
&mut points,
radius * sin,
hz + radius * cos,
n_theta,
));
}
for k in (1..=n_cap).rev() {
let phi = FRAC_PI_2 * (k as f64) / (n_cap as f64);
let (sin, cos) = phi.sin_cos();
rings.push(push_ring(
&mut points,
radius * sin,
-hz - radius * cos,
n_theta,
));
}
let south = points.len() as u32;
points.push(Point3::new(0.0, 0.0, -hz - radius));
fan_faces(&mut faces, north, rings[0], n_theta, true);
for w in rings.windows(2) {
band_faces(&mut faces, w[1], w[0], n_theta);
}
fan_faces(&mut faces, south, rings[rings.len() - 1], n_theta, false);
let mut mesh =
Self::new(points, faces).expect("A capsule tessellation is always a valid mesh");
mesh.transform_in_place(&transform);
Ok(mesh)
}
pub fn create_cylinder_between(
p0: &Point3,
p1: &Point3,
radius: f64,
tol: f64,
) -> Result<Self> {
let v = *p1 - *p0;
let pc = *p0 + v / 2.0;
let transform = Iso3::from_basis_zx(&v, &Vector3::x(), Some(pc))
.or_else(|_| Iso3::from_basis_zy(&v, &Vector3::y(), Some(pc)))
.map_err(|_| "The two ends of a cylinder must not be coincident")?;
let mut mesh = Self::create_cylinder(radius, v.norm(), tol)?;
mesh.transform_in_place(&transform);
Ok(mesh)
}
pub fn create_rect_beam_between(
p0: &Point3,
p1: &Point3,
width: f64,
height: f64,
up: &Vector3,
) -> Result<Self> {
let v = *p1 - *p0;
let pc = *p0 + v / 2.0;
let transform = Iso3::from_basis_zy(&v, up, Some(pc))?;
let mut mesh = Self::create_box(width, height, v.norm());
mesh.transform_in_place(&transform);
Ok(mesh)
}
}
impl MeshData3 {
pub fn stanford_bunny_res4() -> Self {
embedded_mesh(include_bytes!(
"../../../../tests/data/stanford_bun_4.tcmesh"
))
}
pub fn stanford_bunny_res3() -> Self {
embedded_mesh(include_bytes!(
"../../../../tests/data/stanford_bun_3.tcmesh"
))
}
pub fn stanford_bunny_res2() -> Self {
embedded_mesh(include_bytes!(
"../../../../tests/data/stanford_bun_2.tcmesh"
))
}
}
fn embedded_mesh(bytes: &[u8]) -> MeshData3 {
read_tc_mesh_from(&mut { bytes }).expect("embedded fixture must decode")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::geom3::Mesh3;
use crate::na;
use approx::assert_relative_eq;
fn signed_volume(data: &MeshData3) -> f64 {
data.faces()
.iter()
.map(|f| {
let p0 = data.points()[f[0] as usize].coords;
let p1 = data.points()[f[1] as usize].coords;
let p2 = data.points()[f[2] as usize].coords;
p0.dot(&p1.cross(&p2)) / 6.0
})
.sum()
}
fn assert_watertight(data: &MeshData3) {
let mesh = Mesh3::from_data(data.clone(), true).expect("primitive must build");
let edges = mesh.compute_edges().expect("primitive must be manifold");
assert!(edges.boundary_loops.is_empty());
}
#[test]
fn a_circle_respects_the_chordal_tolerance() {
for &tol in &[0.02, 1.0e-3] {
let radius = 2.5;
let circle = MeshData3::create_circle(radius, tol).unwrap();
for f in circle.faces() {
let a = circle.points()[f[1] as usize];
let b = circle.points()[f[2] as usize];
let mid = na::center(&a, &b);
let sag = radius - mid.coords.norm();
assert!(sag >= 0.0, "chord midpoint outside the circle");
assert!(sag <= tol + 1.0e-12, "sag {sag} exceeds tol {tol}");
}
}
}
#[test]
fn a_circle_never_gets_fewer_than_eight_segments() {
let circle = MeshData3::create_circle(1.0, 10.0).unwrap();
assert_eq!(circle.face_count(), 8);
assert_eq!(circle.point_count(), 9);
}
#[test]
fn a_circle_is_flat_open_and_faces_up() {
let circle = MeshData3::create_circle(1.0, 1.0e-3).unwrap();
let mesh = Mesh3::from_data(circle.clone(), false).unwrap();
let edges = mesh.compute_edges().unwrap();
assert_eq!(edges.boundary_loops.len(), 1);
for n in circle.compute_face_normals().unwrap() {
assert_relative_eq!(n.dot(&Vector3::z()), 1.0, epsilon = 1.0e-12);
}
}
#[test]
fn a_circle_rejects_invalid_arguments() {
assert!(MeshData3::create_circle(1.0, 0.0).is_err());
assert!(MeshData3::create_circle(1.0, f64::NAN).is_err());
assert!(MeshData3::create_circle(0.0, 0.1).is_err());
}
#[test]
fn a_box_has_positive_and_exact_volume() {
let bx = MeshData3::create_box(2.0, 3.0, 4.0);
assert_relative_eq!(signed_volume(&bx), 24.0, epsilon = 1.0e-12);
}
#[test]
fn a_box_is_watertight() {
assert_watertight(&MeshData3::create_box(2.0, 3.0, 4.0));
}
#[test]
fn a_beam_between_is_watertight_and_spans_the_points() {
let p0 = Point3::new(1.0, 2.0, 3.0);
let p1 = Point3::new(4.0, 6.0, 3.0);
let beam = MeshData3::create_rect_beam_between(&p0, &p1, 1.0, 2.0, &Vector3::z()).unwrap();
assert_watertight(&beam);
assert_relative_eq!(signed_volume(&beam), 1.0 * 2.0 * 5.0, epsilon = 1.0e-12);
let center = beam
.points()
.iter()
.fold(Vector3::zeros(), |acc, p| acc + p.coords)
/ beam.point_count() as f64;
assert_relative_eq!(center.x, 2.5, epsilon = 1.0e-12);
assert_relative_eq!(center.y, 4.0, epsilon = 1.0e-12);
assert_relative_eq!(center.z, 3.0, epsilon = 1.0e-12);
}
#[test]
fn a_cone_has_the_requested_radius_and_full_height() {
let cone = MeshData3::create_cone(2.0, 10.0, 1.0e-3).unwrap();
let max_z = cone.points().iter().map(|p| p.z).fold(f64::MIN, f64::max);
let min_z = cone.points().iter().map(|p| p.z).fold(f64::MAX, f64::min);
assert_relative_eq!(max_z, 5.0, epsilon = 1.0e-12);
assert_relative_eq!(min_z, -5.0, epsilon = 1.0e-12);
let max_r = cone
.points()
.iter()
.map(|p| p.x.hypot(p.y))
.fold(f64::MIN, f64::max);
assert_relative_eq!(max_r, 2.0, epsilon = 1.0e-12);
let on_axis = cone
.points()
.iter()
.filter(|p| p.x.hypot(p.y) < 1.0e-12 && p.z > 0.0)
.count();
assert_eq!(on_axis, 1);
}
#[test]
fn a_cone_respects_the_chordal_tolerance() {
for &tol in &[0.02, 1.0e-3] {
let radius = 2.5;
let cone = MeshData3::create_cone(radius, 4.0, tol).unwrap();
let n = cone.point_count() - 2;
for i in 0..n {
let a = cone.points()[i];
let b = cone.points()[(i + 1) % n];
let mid = na::center(&a, &b);
let sag = radius - mid.x.hypot(mid.y);
assert!(sag >= 0.0, "chord midpoint outside the base circle");
assert!(sag <= tol + 1.0e-12, "sag {sag} exceeds tol {tol}");
}
}
}
#[test]
fn a_cone_never_gets_fewer_than_eight_segments() {
let cone = MeshData3::create_cone(1.0, 2.0, 10.0).unwrap();
assert_eq!(cone.point_count(), 8 + 2);
assert_eq!(cone.face_count(), 8 * 2);
}
#[test]
fn a_cone_is_watertight_and_wound_outward() {
let cone = MeshData3::create_cone(2.0, 10.0, 1.0e-4).unwrap();
assert_watertight(&cone);
let volume = signed_volume(&cone);
let exact = std::f64::consts::PI * 4.0 * 10.0 / 3.0;
assert!(volume > 0.0, "winding is inside out");
assert!(volume <= exact, "volume {volume} exceeds the true {exact}");
assert_relative_eq!(volume, exact, max_relative = 1.0e-3);
}
#[test]
fn a_cone_rejects_invalid_arguments() {
assert!(MeshData3::create_cone(1.0, 2.0, 0.0).is_err());
assert!(MeshData3::create_cone(0.0, 2.0, 0.1).is_err());
}
#[test]
fn a_cylinder_has_the_requested_radius_and_full_height() {
let cyl = MeshData3::create_cylinder(2.0, 10.0, 1.0e-3).unwrap();
for p in cyl.points() {
assert_relative_eq!(p.z.abs(), 5.0, epsilon = 1.0e-12);
let r = p.x.hypot(p.y);
assert!(
r == 0.0 || (r - 2.0).abs() < 1.0e-12,
"unexpected radius {r}"
);
}
}
#[test]
fn a_cylinder_respects_the_chordal_tolerance() {
for &tol in &[0.02, 1.0e-3] {
let radius = 2.5;
let cyl = MeshData3::create_cylinder(radius, 4.0, tol).unwrap();
let n = (cyl.point_count() - 2) / 2;
for i in 0..n {
let a = cyl.points()[i];
let b = cyl.points()[(i + 1) % n];
let mid = na::center(&a, &b);
let sag = radius - mid.x.hypot(mid.y);
assert!(sag >= 0.0, "chord midpoint outside the cylinder");
assert!(sag <= tol + 1.0e-12, "sag {sag} exceeds tol {tol}");
}
}
}
#[test]
fn a_cylinder_never_gets_fewer_than_eight_segments() {
let cyl = MeshData3::create_cylinder(1.0, 2.0, 10.0).unwrap();
assert_eq!(cyl.point_count(), 8 * 2 + 2);
assert_eq!(cyl.face_count(), 8 * 4);
}
#[test]
fn a_cylinder_is_watertight_and_wound_outward() {
let cyl = MeshData3::create_cylinder(2.0, 10.0, 1.0e-4).unwrap();
assert_watertight(&cyl);
let volume = signed_volume(&cyl);
let exact = std::f64::consts::PI * 4.0 * 10.0;
assert!(volume > 0.0, "winding is inside out");
assert!(volume <= exact, "volume {volume} exceeds the true {exact}");
assert_relative_eq!(volume, exact, max_relative = 1.0e-3);
}
#[test]
fn a_cylinder_rejects_invalid_arguments() {
assert!(MeshData3::create_cylinder(1.0, 2.0, 0.0).is_err());
assert!(MeshData3::create_cylinder(0.0, 2.0, 0.1).is_err());
}
#[test]
fn a_cylinder_between_spans_the_two_points() {
let p0 = Point3::new(1.0, 2.0, 3.0);
let p1 = Point3::new(1.0, 2.0, 9.0);
let cyl = MeshData3::create_cylinder_between(&p0, &p1, 0.5, 1.0e-3).unwrap();
let min_z = cyl.points().iter().map(|p| p.z).fold(f64::MAX, f64::min);
let max_z = cyl.points().iter().map(|p| p.z).fold(f64::MIN, f64::max);
assert_relative_eq!(min_z, 3.0, epsilon = 1.0e-12);
assert_relative_eq!(max_z, 9.0, epsilon = 1.0e-12);
for p in cyl.points() {
let r = (p.x - 1.0).hypot(p.y - 2.0);
assert!(
r < 1.0e-12 || (r - 0.5).abs() < 1.0e-12,
"off the tube: {r}"
);
}
assert_watertight(&cyl);
}
#[test]
fn a_cylinder_between_needs_two_distinct_points() {
let p0 = Point3::new(1.0, 2.0, 3.0);
assert!(MeshData3::create_cylinder_between(&p0, &p0, 0.5, 1.0e-3).is_err());
}
#[test]
fn a_beam_between_needs_an_up_which_is_not_parallel_to_the_segment() {
let p0 = Point3::origin();
let p1 = Point3::new(0.0, 0.0, 10.0);
assert!(MeshData3::create_rect_beam_between(&p0, &p1, 1.0, 2.0, &Vector3::y()).is_ok());
assert!(MeshData3::create_rect_beam_between(&p0, &p1, 1.0, 2.0, &Vector3::z()).is_err());
}
#[test]
fn no_primitive_arrives_with_attributes() {
assert!(!MeshData3::create_box(1.0, 1.0, 1.0).has_attrs());
assert!(!MeshData3::create_sphere(1.0, 1.0e-3).unwrap().has_attrs());
assert!(
!MeshData3::create_cylinder(1.0, 2.0, 1.0e-3)
.unwrap()
.has_attrs()
);
assert!(
!MeshData3::create_cone(1.0, 2.0, 1.0e-3)
.unwrap()
.has_attrs()
);
assert!(!MeshData3::create_circle(1.0, 1.0e-3).unwrap().has_attrs());
assert!(!MeshData3::stanford_bunny_res4().has_attrs());
}
#[test]
fn a_sphere_respects_the_tolerance() {
for &tol in &[0.05, 1.0e-3] {
let radius = 2.5;
let sphere = MeshData3::create_sphere(radius, tol).unwrap();
let mut worst: f64 = 0.0;
for f in sphere.faces() {
let a = sphere.points()[f[0] as usize];
let b = sphere.points()[f[1] as usize];
let c = sphere.points()[f[2] as usize];
let centroid = Point3::from((a.coords + b.coords + c.coords) / 3.0);
for p in [
centroid,
na::center(&a, &b),
na::center(&b, &c),
na::center(&c, &a),
] {
let sag = radius - p.coords.norm();
assert!(sag >= -1.0e-12, "a facet point sits outside the sphere");
worst = worst.max(sag);
}
}
assert!(
worst <= tol + 1.0e-12,
"worst sag {worst} exceeds tol {tol}"
);
}
}
#[test]
fn a_sphere_never_gets_fewer_than_the_floor_counts() {
let sphere = MeshData3::create_sphere(1.0, 10.0).unwrap();
assert_eq!(sphere.point_count(), 8 * 3 + 2);
assert_eq!(sphere.face_count(), 8 * 2 + 2 * 8 * 2);
}
#[test]
fn a_sphere_has_its_poles_on_z() {
let radius = 3.0;
let sphere = MeshData3::create_sphere(radius, 1.0e-3).unwrap();
for p in sphere.points() {
assert_relative_eq!(p.coords.norm(), radius, epsilon = 1.0e-12);
}
let on_axis = sphere
.points()
.iter()
.filter(|p| p.x.hypot(p.y) < 1.0e-12)
.count();
assert_eq!(on_axis, 2);
let max_z = sphere.points().iter().map(|p| p.z).fold(f64::MIN, f64::max);
let min_z = sphere.points().iter().map(|p| p.z).fold(f64::MAX, f64::min);
assert_relative_eq!(max_z, radius, epsilon = 1.0e-12);
assert_relative_eq!(min_z, -radius, epsilon = 1.0e-12);
}
#[test]
fn a_sphere_is_watertight_and_wound_outward() {
let radius = 2.0;
let sphere = MeshData3::create_sphere(radius, 1.0e-4).unwrap();
assert_watertight(&sphere);
let volume = signed_volume(&sphere);
let exact = 4.0 / 3.0 * std::f64::consts::PI * radius.powi(3);
assert!(volume > 0.0, "winding is inside out");
assert!(volume <= exact, "volume {volume} exceeds the true {exact}");
assert_relative_eq!(volume, exact, max_relative = 1.0e-3);
}
#[test]
fn a_sphere_rejects_invalid_arguments() {
assert!(MeshData3::create_sphere(1.0, 0.0).is_err());
assert!(MeshData3::create_sphere(1.0, f64::NAN).is_err());
assert!(MeshData3::create_sphere(0.0, 0.1).is_err());
}
#[test]
fn a_capsule_respects_the_tolerance() {
for &tol in &[0.05, 1.0e-3] {
let radius = 2.5;
let hz = 4.0;
let p0 = Point3::new(0.0, 0.0, -hz);
let p1 = Point3::new(0.0, 0.0, hz);
let capsule = MeshData3::create_capsule(&p0, &p1, radius, tol).unwrap();
let surface_distance = |p: &Point3| {
let z = p.z.clamp(-hz, hz);
(p - Point3::new(0.0, 0.0, z)).norm()
};
let mut worst: f64 = 0.0;
for f in capsule.faces() {
let a = capsule.points()[f[0] as usize];
let b = capsule.points()[f[1] as usize];
let c = capsule.points()[f[2] as usize];
let centroid = Point3::from((a.coords + b.coords + c.coords) / 3.0);
for p in [
centroid,
na::center(&a, &b),
na::center(&b, &c),
na::center(&c, &a),
] {
let sag = radius - surface_distance(&p);
assert!(sag >= -1.0e-12, "a facet point sits outside the capsule");
worst = worst.max(sag);
}
}
assert!(
worst <= tol + 1.0e-12,
"worst sag {worst} exceeds tol {tol}"
);
}
}
#[test]
fn a_capsule_never_gets_fewer_than_the_floor_counts() {
let p0 = Point3::origin();
let p1 = Point3::new(0.0, 0.0, 4.0);
let capsule = MeshData3::create_capsule(&p0, &p1, 1.0, 10.0).unwrap();
assert_eq!(capsule.point_count(), 8 * 4 + 2);
assert_eq!(capsule.face_count(), 8 * 2 + 3 * 8 * 2);
}
#[test]
fn a_capsule_spans_the_two_points_plus_a_radius_at_each_end() {
let p0 = Point3::new(1.0, 2.0, 3.0);
let p1 = Point3::new(1.0, 2.0, 9.0);
let radius = 0.5;
let capsule = MeshData3::create_capsule(&p0, &p1, radius, 1.0e-3).unwrap();
let min_z = capsule
.points()
.iter()
.map(|p| p.z)
.fold(f64::MAX, f64::min);
let max_z = capsule
.points()
.iter()
.map(|p| p.z)
.fold(f64::MIN, f64::max);
assert_relative_eq!(min_z, 3.0 - radius, epsilon = 1.0e-12);
assert_relative_eq!(max_z, 9.0 + radius, epsilon = 1.0e-12);
assert_watertight(&capsule);
}
#[test]
fn a_capsule_is_watertight_and_wound_outward() {
let radius = 2.0;
let length = 10.0;
let p0 = Point3::new(0.0, 0.0, -length / 2.0);
let p1 = Point3::new(0.0, 0.0, length / 2.0);
let capsule = MeshData3::create_capsule(&p0, &p1, radius, 1.0e-4).unwrap();
assert_watertight(&capsule);
let volume = signed_volume(&capsule);
let exact = std::f64::consts::PI * radius.powi(2) * (length + 4.0 / 3.0 * radius);
assert!(volume > 0.0, "winding is inside out");
assert!(volume <= exact, "volume {volume} exceeds the true {exact}");
assert_relative_eq!(volume, exact, max_relative = 1.0e-3);
}
#[test]
fn a_capsule_rejects_invalid_arguments() {
let p0 = Point3::origin();
let p1 = Point3::new(0.0, 0.0, 4.0);
assert!(MeshData3::create_capsule(&p0, &p1, 1.0, 0.0).is_err());
assert!(MeshData3::create_capsule(&p0, &p1, 0.0, 0.1).is_err());
assert!(MeshData3::create_capsule(&p0, &p0, 1.0, 0.1).is_err());
}
#[test]
fn the_embedded_bunnies_have_their_documented_sizes() {
assert_eq!(MeshData3::stanford_bunny_res4().point_count(), 453);
assert_eq!(MeshData3::stanford_bunny_res4().face_count(), 948);
assert_eq!(MeshData3::stanford_bunny_res3().point_count(), 1889);
assert_eq!(MeshData3::stanford_bunny_res2().point_count(), 8171);
}
}