|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use crate::bounding_volume::AABB; |
| 4 | +use crate::math::{Isometry, Point, Real, Vector}; |
| 5 | +use crate::partitioning::QBVH; |
| 6 | +use crate::shape::composite_shape::SimdCompositeShape; |
| 7 | +use crate::shape::TriMesh; |
| 8 | +use crate::shape::{Shape, Triangle, TypedSimdCompositeShape}; |
| 9 | + |
| 10 | +#[derive(Clone)] |
| 11 | +#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] |
| 12 | +/// A scaled [`TriMesh`] |
| 13 | +pub struct ScaledTriMesh { |
| 14 | + /// The underlying triangle mesh |
| 15 | + trimesh: Arc<TriMesh>, |
| 16 | + /// Scaling factors for each dimension |
| 17 | + // This could easily be expanded into an arbitrary transform, if a use case arises. |
| 18 | + scaling_factors: Vector<Real>, |
| 19 | + quadtree: QBVH<u32>, |
| 20 | +} |
| 21 | + |
| 22 | +impl ScaledTriMesh { |
| 23 | + /// Creates a triangle mesh by scaling `trimesh` along each axis |
| 24 | + pub fn new(trimesh: Arc<TriMesh>, scaling_factors: Vector<Real>) -> Self { |
| 25 | + // Future work: Would it be more efficient to scale trimesh.quadtree rather than building a |
| 26 | + // new one from scratch? |
| 27 | + let data = trimesh.triangles().enumerate().map(|(i, tri)| { |
| 28 | + let aabb = scale_tri(&tri, &scaling_factors).local_aabb(); |
| 29 | + (i as u32, aabb) |
| 30 | + }); |
| 31 | + let mut quadtree = QBVH::new(); |
| 32 | + quadtree.clear_and_rebuild(data, 0.0); |
| 33 | + Self { |
| 34 | + trimesh, |
| 35 | + scaling_factors, |
| 36 | + quadtree, |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// The underlying unscaled trimesh |
| 41 | + pub fn trimesh(&self) -> &Arc<TriMesh> { |
| 42 | + &self.trimesh |
| 43 | + } |
| 44 | + |
| 45 | + /// Scaling factors used to derive this shape from the underlying trimesh |
| 46 | + pub fn scaling_factors(&self) -> Vector<Real> { |
| 47 | + self.scaling_factors |
| 48 | + } |
| 49 | + |
| 50 | + /// Compute the axis-aligned bounding box of this triangle mesh. |
| 51 | + pub fn aabb(&self, pos: &Isometry<Real>) -> AABB { |
| 52 | + self.quadtree.root_aabb().transform_by(pos) |
| 53 | + } |
| 54 | + |
| 55 | + /// Gets the local axis-aligned bounding box of this triangle mesh. |
| 56 | + pub fn local_aabb(&self) -> &AABB { |
| 57 | + self.quadtree.root_aabb() |
| 58 | + } |
| 59 | + |
| 60 | + /// The acceleration structure used by this triangle-mesh. |
| 61 | + pub fn quadtree(&self) -> &QBVH<u32> { |
| 62 | + &self.quadtree |
| 63 | + } |
| 64 | + |
| 65 | + /// An iterator through all the scaled triangles of this mesh. |
| 66 | + pub fn triangles(&self) -> impl ExactSizeIterator<Item = Triangle> + '_ { |
| 67 | + self.trimesh |
| 68 | + .triangles() |
| 69 | + .map(move |tri| scale_tri(&tri, &self.scaling_factors)) |
| 70 | + } |
| 71 | + |
| 72 | + /// Get the `i`-th scaled triangle of this mesh. |
| 73 | + pub fn triangle(&self, i: u32) -> Triangle { |
| 74 | + scale_tri(&self.trimesh.triangle(i), &self.scaling_factors) |
| 75 | + } |
| 76 | + |
| 77 | + /// The vertex buffer of this mesh. |
| 78 | + pub fn vertices(&self) -> impl ExactSizeIterator<Item = Point<Real>> + '_ { |
| 79 | + self.trimesh |
| 80 | + .vertices() |
| 81 | + .iter() |
| 82 | + .map(move |v| v.coords.component_mul(&self.scaling_factors).into()) |
| 83 | + } |
| 84 | + |
| 85 | + /// The index buffer of this mesh. |
| 86 | + pub fn indices(&self) -> &[[u32; 3]] { |
| 87 | + self.trimesh.indices() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +fn scale_tri(tri: &Triangle, factors: &Vector<Real>) -> Triangle { |
| 92 | + Triangle { |
| 93 | + a: tri.a.coords.component_mul(&factors).into(), |
| 94 | + b: tri.b.coords.component_mul(&factors).into(), |
| 95 | + c: tri.c.coords.component_mul(&factors).into(), |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl SimdCompositeShape for ScaledTriMesh { |
| 100 | + fn map_part_at(&self, i: u32, f: &mut dyn FnMut(Option<&Isometry<Real>>, &dyn Shape)) { |
| 101 | + let tri = self.triangle(i); |
| 102 | + f(None, &tri) |
| 103 | + } |
| 104 | + |
| 105 | + fn quadtree(&self) -> &QBVH<u32> { |
| 106 | + &self.quadtree |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl TypedSimdCompositeShape for ScaledTriMesh { |
| 111 | + type PartShape = Triangle; |
| 112 | + type PartId = u32; |
| 113 | + |
| 114 | + #[inline(always)] |
| 115 | + fn map_typed_part_at( |
| 116 | + &self, |
| 117 | + i: u32, |
| 118 | + mut f: impl FnMut(Option<&Isometry<Real>>, &Self::PartShape), |
| 119 | + ) { |
| 120 | + let tri = self.triangle(i); |
| 121 | + f(None, &tri) |
| 122 | + } |
| 123 | + |
| 124 | + #[inline(always)] |
| 125 | + fn map_untyped_part_at(&self, i: u32, mut f: impl FnMut(Option<&Isometry<Real>>, &dyn Shape)) { |
| 126 | + let tri = self.triangle(i); |
| 127 | + f(None, &tri) |
| 128 | + } |
| 129 | + |
| 130 | + fn typed_quadtree(&self) -> &QBVH<u32> { |
| 131 | + &self.quadtree |
| 132 | + } |
| 133 | +} |
0 commit comments