use super::MeshData3;
use crate::geom3::IsoExtensions3;
use crate::io::{deflate_bytes, u_bytes_to_mesh_data};
use crate::{Iso3, Point3, Result, Vector3};
use parry3d_f64::shape;
impl MeshData3 {
pub fn create_box(length: f64, width: f64, height: f64) -> Self {
let bx = shape::Cuboid::new(Vector3::new(length / 2.0, width / 2.0, height / 2.0));
let (points, faces) = bx.to_trimesh();
Self::new(points, faces).expect("A cuboid tessellation is always a valid mesh")
}
pub fn create_sphere(radius: f64, n_theta: usize, n_phi: usize) -> Self {
let sphere = shape::Ball::new(radius);
let (points, faces) = sphere.to_trimesh(n_theta as u32, n_phi as u32);
Self::new(points, faces).expect("A ball tessellation is always a valid mesh")
}
pub fn create_cylinder(radius: f64, height: f64, steps: usize) -> Self {
let cyl = shape::Cylinder::new(height / 2.0, radius);
let (points, faces) = cyl.to_trimesh(steps as u32);
Self::new(points, faces).expect("A cylinder tessellation is always a valid mesh")
}
pub fn create_cone(radius: f64, height: f64, steps: usize) -> Self {
let cone = shape::Cone::new(height / 2.0, radius);
let (points, faces) = cone.to_trimesh(steps as u32);
Self::new(points, faces).expect("A cone tessellation is always a valid mesh")
}
pub fn create_circle(radius: f64, segments: usize) -> Self {
use std::f64::consts::TAU;
let mut points = Vec::with_capacity(segments + 1);
let mut faces = Vec::with_capacity(segments);
points.push(Point3::origin());
for i in 0..segments {
let angle = TAU * (i as f64) / (segments as f64);
points.push(Point3::new(radius * angle.cos(), radius * angle.sin(), 0.0));
}
for i in 0..segments {
let a = (i + 1) as u32;
let b = ((i + 1) % segments + 1) as u32;
faces.push([0u32, a, b]);
}
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,
n_theta: usize,
n_phi: usize,
) -> Self {
let capsule = shape::Capsule::new(*p0, *p1, radius);
let (points, faces) = capsule.to_trimesh(n_theta as u32, n_phi as u32);
Self::new(points, faces).expect("A capsule tessellation is always a valid mesh")
}
pub fn create_cylinder_between(p0: &Point3, p1: &Point3, radius: f64, steps: usize) -> Self {
let v = *p1 - *p0;
let pc = *p0 + v / 2.0;
let cyl = shape::Cylinder::new(v.norm() / 2.0, radius);
let transform = Iso3::from_basis_yz(&v, &Vector3::z(), Some(pc))
.unwrap_or(Iso3::from_basis_yx(&v, &Vector3::x(), Some(pc)).unwrap());
let (points, faces) = cyl.to_trimesh(steps as u32);
let mut mesh =
Self::new(points, faces).expect("A cylinder tessellation is always a valid mesh");
mesh.transform_in_place(&transform);
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 box_geom = shape::Cuboid::new(Vector3::new(width / 2.0, height / 2.0, v.norm() / 2.0));
let transform = Iso3::from_basis_zy(&v, up, Some(pc))?;
let (points, faces) = box_geom.to_trimesh();
let mut mesh = Self::new(points, faces).expect("A cuboid tessellation is always valid");
mesh.transform_in_place(&transform);
Ok(mesh)
}
}
impl MeshData3 {
pub fn stanford_bunny_res4() -> Self {
let bytes = include_bytes!("../../../../tests/data/stanford_bun_4.umesh.gz");
u_bytes_to_mesh_data(&deflate_bytes(bytes).unwrap()).unwrap()
}
pub fn stanford_bunny_res3() -> Self {
let bytes = include_bytes!("../../../../tests/data/stanford_bun_3.umesh.gz");
u_bytes_to_mesh_data(&deflate_bytes(bytes).unwrap()).unwrap()
}
pub fn stanford_bunny_res2() -> Self {
let bytes = include_bytes!("../../../../tests/data/stanford_bun_2.umesh.gz");
u_bytes_to_mesh_data(&deflate_bytes(bytes).unwrap()).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_relative_eq;
#[test]
fn a_cone_has_the_requested_radius_and_full_height() {
let cone = MeshData3::create_cone(2.0, 10.0, 32);
let max_y = cone.points().iter().map(|p| p.y).fold(f64::MIN, f64::max);
let min_y = cone.points().iter().map(|p| p.y).fold(f64::MAX, f64::min);
assert_relative_eq!(max_y, 5.0, epsilon = 1.0e-12);
assert_relative_eq!(min_y, -5.0, epsilon = 1.0e-12);
let max_r = cone
.points()
.iter()
.map(|p| p.x.hypot(p.z))
.fold(f64::MIN, f64::max);
assert_relative_eq!(max_r, 2.0, epsilon = 1.0e-12);
}
#[test]
fn a_cylinder_has_the_requested_radius_and_full_height() {
let cyl = MeshData3::create_cylinder(2.0, 10.0, 32);
let max_y = cyl.points().iter().map(|p| p.y).fold(f64::MIN, f64::max);
assert_relative_eq!(max_y, 5.0, epsilon = 1.0e-12);
for p in cyl.points() {
assert_relative_eq!(p.x.hypot(p.z), 2.0, epsilon = 1.0e-12);
}
}
#[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, 16);
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() {
assert_relative_eq!((p.x - 1.0).hypot(p.y - 2.0), 0.5, epsilon = 1.0e-12);
}
}
#[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).attrs().is_empty());
assert!(MeshData3::create_sphere(1.0, 8, 8).attrs().is_empty());
assert!(MeshData3::create_cylinder(1.0, 2.0, 8).attrs().is_empty());
assert!(MeshData3::create_cone(1.0, 2.0, 8).attrs().is_empty());
assert!(MeshData3::create_circle(1.0, 8).attrs().is_empty());
assert!(MeshData3::stanford_bunny_res4().attrs().is_empty());
}
#[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);
}
}