|
9 | 9 | use num::Bounded; |
10 | 10 |
|
11 | 11 | #[cfg(all(feature = "dim3", not(feature = "std")))] |
12 | | -use na::ComplexField; // for .sin_cos() |
| 12 | +use na::ComplexField; |
| 13 | +// for .sin_cos() |
13 | 14 |
|
14 | 15 | use crate::query::{Ray, RayCast}; |
15 | 16 | #[cfg(feature = "rkyv")] |
@@ -143,10 +144,18 @@ impl Aabb { |
143 | 144 | Self::new(center - half_extents, center + half_extents) |
144 | 145 | } |
145 | 146 |
|
146 | | - /// Creates a new `Aabb` from a set of points. |
147 | | - pub fn from_points<'a, I>(pts: I) -> Self |
| 147 | + /// Creates a new `Aabb` from a set of point references. |
| 148 | + pub fn from_points_ref<'a, I>(pts: I) -> Self |
148 | 149 | where |
149 | 150 | I: IntoIterator<Item = &'a Point<Real>>, |
| 151 | + { |
| 152 | + super::aabb_utils::local_point_cloud_aabb(pts.into_iter().copied()) |
| 153 | + } |
| 154 | + |
| 155 | + /// Creates a new `Aabb` from a set of points. |
| 156 | + pub fn from_points<I>(pts: I) -> Self |
| 157 | + where |
| 158 | + I: IntoIterator<Item = Point<Real>>, |
150 | 159 | { |
151 | 160 | super::aabb_utils::local_point_cloud_aabb(pts) |
152 | 161 | } |
@@ -174,6 +183,28 @@ impl Aabb { |
174 | 183 | return extents.x * extents.y * extents.z; |
175 | 184 | } |
176 | 185 |
|
| 186 | + /// In 3D, returns the half-area. In 2D returns the half-perimeter of the AABB. |
| 187 | + pub fn half_area_or_perimeter(&self) -> Real { |
| 188 | + #[cfg(feature = "dim2")] |
| 189 | + return self.half_perimeter(); |
| 190 | + #[cfg(feature = "dim3")] |
| 191 | + return self.half_area(); |
| 192 | + } |
| 193 | + |
| 194 | + /// The half perimeter of this `Aabb`. |
| 195 | + #[cfg(feature = "dim2")] |
| 196 | + pub fn half_perimeter(&self) -> Real { |
| 197 | + let extents = self.extents(); |
| 198 | + extents.x + extents.y |
| 199 | + } |
| 200 | + |
| 201 | + /// The half area of this `Aabb`. |
| 202 | + #[cfg(feature = "dim3")] |
| 203 | + pub fn half_area(&self) -> Real { |
| 204 | + let extents = self.extents(); |
| 205 | + extents.x * (extents.y + extents.z) + extents.y * extents.z |
| 206 | + } |
| 207 | + |
177 | 208 | /// The extents of this `Aabb`. |
178 | 209 | #[inline] |
179 | 210 | pub fn extents(&self) -> Vector<Real> { |
@@ -251,6 +282,15 @@ impl Aabb { |
251 | 282 | true |
252 | 283 | } |
253 | 284 |
|
| 285 | + /// Computes the distance between the origin and this AABB. |
| 286 | + pub fn distance_to_origin(&self) -> Real { |
| 287 | + self.mins |
| 288 | + .coords |
| 289 | + .sup(&-self.maxs.coords) |
| 290 | + .sup(&Vector::zeros()) |
| 291 | + .norm() |
| 292 | + } |
| 293 | + |
254 | 294 | /// Does this AABB intersects an AABB `aabb2` moving at velocity `vel12` relative to `self`? |
255 | 295 | #[inline] |
256 | 296 | pub fn intersects_moving_aabb(&self, aabb2: &Self, vel12: Vector<Real>) -> bool { |
|
0 commit comments