Skip to content

Commit a5e5497

Browse files
committed
Replace deprecated nalgebra VectorN with const generics SVector
1 parent aacbc64 commit a5e5497

File tree

2 files changed

+35
-50
lines changed

2 files changed

+35
-50
lines changed

splashsurf_lib/src/aabb.rs

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,30 @@
33
use std::fmt;
44
use std::fmt::Debug;
55

6-
use nalgebra::allocator::Allocator;
7-
use nalgebra::{DefaultAllocator, DimName, VectorN, U2, U3};
6+
use nalgebra::SVector;
87
use rayon::prelude::*;
98

109
use crate::{Real, ThreadSafe};
1110

1211
/// Type representing an axis aligned bounding box in arbitrary dimensions
1312
#[derive(Clone, Eq, PartialEq)]
14-
pub struct AxisAlignedBoundingBox<R: Real, D: DimName>
15-
where
16-
DefaultAllocator: Allocator<R, D>,
17-
{
18-
min: VectorN<R, D>,
19-
max: VectorN<R, D>,
13+
pub struct AxisAlignedBoundingBox<R: Real, const D: usize> {
14+
min: SVector<R, D>,
15+
max: SVector<R, D>,
2016
}
2117

2218
/// Convenience type alias for an AABB in two dimensions
23-
pub type AxisAlignedBoundingBox2d<R> = AxisAlignedBoundingBox<R, U2>;
19+
pub type AxisAlignedBoundingBox2d<R> = AxisAlignedBoundingBox<R, 2>;
2420
/// Convenience type alias for an AABB in three dimensions
25-
pub type AxisAlignedBoundingBox3d<R> = AxisAlignedBoundingBox<R, U3>;
21+
pub type AxisAlignedBoundingBox3d<R> = AxisAlignedBoundingBox<R, 3>;
2622

27-
impl<R, D> AxisAlignedBoundingBox<R, D>
23+
impl<R, const D: usize> AxisAlignedBoundingBox<R, D>
2824
where
2925
R: Real,
30-
D: DimName,
31-
DefaultAllocator: Allocator<R, D>,
32-
VectorN<R, D>: ThreadSafe,
26+
SVector<R, D>: ThreadSafe,
3327
{
3428
/// Constructs the smallest AABB fitting around all the given points, parallel version
35-
pub fn par_from_points(points: &[VectorN<R, D>]) -> Self {
29+
pub fn par_from_points(points: &[SVector<R, D>]) -> Self {
3630
if points.is_empty() {
3731
Self::zeros()
3832
} else if points.len() == 1 {
@@ -59,35 +53,33 @@ where
5953
}
6054
}
6155

62-
impl<R, D> AxisAlignedBoundingBox<R, D>
56+
impl<R, const D: usize> AxisAlignedBoundingBox<R, D>
6357
where
6458
R: Real,
65-
D: DimName,
66-
DefaultAllocator: Allocator<R, D>,
6759
{
6860
/// Constructs a degenerate AABB with min and max set to zero
6961
#[inline(always)]
7062
pub fn zeros() -> Self {
71-
Self::from_point(VectorN::zeros())
63+
Self::from_point(SVector::zeros())
7264
}
7365

7466
/// Constructs an AABB with the given min and max bounding points
7567
#[inline(always)]
76-
pub fn new(min: VectorN<R, D>, max: VectorN<R, D>) -> Self {
68+
pub fn new(min: SVector<R, D>, max: SVector<R, D>) -> Self {
7769
Self { min, max }
7870
}
7971

8072
/// Constructs a degenerate AABB with zero extents centered at the given point
8173
#[inline(always)]
82-
pub fn from_point(point: VectorN<R, D>) -> Self {
74+
pub fn from_point(point: SVector<R, D>) -> Self {
8375
Self {
8476
min: point.clone(),
8577
max: point,
8678
}
8779
}
8880

8981
/// Constructs the smallest AABB fitting around all the given points
90-
pub fn from_points(points: &[VectorN<R, D>]) -> Self {
82+
pub fn from_points(points: &[SVector<R, D>]) -> Self {
9183
let mut point_iter = points.iter();
9284
if let Some(first_point) = point_iter.next().cloned() {
9385
let mut aabb = Self::from_point(first_point);
@@ -104,7 +96,6 @@ where
10496
pub fn try_convert<T>(&self) -> Option<AxisAlignedBoundingBox<T, D>>
10597
where
10698
T: Real,
107-
DefaultAllocator: Allocator<T, D>,
10899
{
109100
Some(AxisAlignedBoundingBox::new(
110101
T::try_convert_vec_from(&self.min)?,
@@ -114,19 +105,19 @@ where
114105

115106
/// Returns the min coordinate of the bounding box
116107
#[inline(always)]
117-
pub fn min(&self) -> &VectorN<R, D> {
108+
pub fn min(&self) -> &SVector<R, D> {
118109
&self.min
119110
}
120111

121112
/// Returns the max coordinate of the bounding box
122113
#[inline(always)]
123-
pub fn max(&self) -> &VectorN<R, D> {
114+
pub fn max(&self) -> &SVector<R, D> {
124115
&self.max
125116
}
126117

127118
/// Returns whether the AABB is consistent, i.e. `aabb.min()[i] <= aabb.max()[i]` for all `i`
128119
pub fn is_consistent(&self) -> bool {
129-
for i in 0..D::dim() {
120+
for i in 0..D {
130121
if !(self.min[i] <= self.max[i]) {
131122
return false;
132123
}
@@ -136,7 +127,7 @@ where
136127

137128
/// Returns whether the AABB is degenerate in any dimension, i.e. `aabb.min()[i] == aabb.max()[i]` for any `i`
138129
pub fn is_degenerate(&self) -> bool {
139-
for i in 0..D::dim() {
130+
for i in 0..D {
140131
if self.min[i] == self.max[i] {
141132
return true;
142133
}
@@ -146,7 +137,7 @@ where
146137

147138
/// Returns the extents of the bounding box (vector connecting min and max point of the box)
148139
#[inline(always)]
149-
pub fn extents(&self) -> VectorN<R, D> {
140+
pub fn extents(&self) -> SVector<R, D> {
150141
&self.max - &self.min
151142
}
152143

@@ -167,13 +158,13 @@ where
167158
}
168159

169160
/// Returns the geometric centroid of the AABB (mean of the corner points)
170-
pub fn centroid(&self) -> VectorN<R, D> {
161+
pub fn centroid(&self) -> SVector<R, D> {
171162
&self.min + (self.extents() / (R::one() + R::one()))
172163
}
173164

174165
/// Checks if the given point is inside of the AABB, the AABB is considered to be half-open to its max coordinate
175-
pub fn contains_point(&self, point: &VectorN<R, D>) -> bool {
176-
for i in 0..D::dim() {
166+
pub fn contains_point(&self, point: &SVector<R, D>) -> bool {
167+
for i in 0..D {
177168
if point[i] < self.min[i] || point[i] >= self.max[i] {
178169
return false;
179170
}
@@ -183,7 +174,7 @@ where
183174
}
184175

185176
/// Translates the AABB by the given vector
186-
pub fn translate(&mut self, vector: &VectorN<R, D>) {
177+
pub fn translate(&mut self, vector: &SVector<R, D>) {
187178
self.min += vector;
188179
self.max += vector;
189180
}
@@ -204,24 +195,24 @@ where
204195

205196
/// Enlarges this AABB to the smallest AABB enclosing both itself and another AABB
206197
pub fn join(&mut self, other: &Self) {
207-
for i in 0..D::dim() {
198+
for i in 0..D {
208199
self.min[i] = self.min[i].min(other.min[i]);
209200
self.max[i] = self.max[i].max(other.max[i]);
210201
}
211202
}
212203

213204
/// Enlarges this AABB to the smallest AABB enclosing both itself and another point
214-
pub fn join_with_point(&mut self, point: &VectorN<R, D>) {
215-
for i in 0..D::dim() {
205+
pub fn join_with_point(&mut self, point: &SVector<R, D>) {
206+
for i in 0..D {
216207
self.min[i] = self.min[i].min(point[i]);
217208
self.max[i] = self.max[i].max(point[i]);
218209
}
219210
}
220211

221212
/// Grows this AABB uniformly in all directions by the given scalar margin (i.e. adding the margin to min/max extents)
222213
pub fn grow_uniformly(&mut self, margin: R) {
223-
self.min = &self.min - &VectorN::repeat(margin);
224-
self.max = &self.max + &VectorN::repeat(margin);
214+
self.min = &self.min - &SVector::repeat(margin);
215+
self.max = &self.max + &SVector::repeat(margin);
225216
}
226217

227218
/// Returns the smallest cubical AABB with the same center that encloses this AABB
@@ -230,19 +221,17 @@ where
230221
let half_max_extent = self.max_extent() / (R::one() + R::one());
231222

232223
let mut cube = Self::new(
233-
VectorN::repeat(half_max_extent.neg()),
234-
VectorN::repeat(half_max_extent),
224+
SVector::repeat(half_max_extent.neg()),
225+
SVector::repeat(half_max_extent),
235226
);
236227
cube.translate(&center);
237228
cube
238229
}
239230
}
240231

241-
impl<R, D> Debug for AxisAlignedBoundingBox<R, D>
232+
impl<R, const D: usize> Debug for AxisAlignedBoundingBox<R, D>
242233
where
243234
R: Real,
244-
D: DimName,
245-
DefaultAllocator: Allocator<R, D>,
246235
{
247236
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
248237
write!(

splashsurf_lib/src/traits.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use std::fmt::{Debug, Display};
22
use std::hash::Hash;
33

44
use bitflags::_core::ops::{AddAssign, MulAssign, SubAssign};
5-
use nalgebra::allocator::Allocator;
6-
use nalgebra::{DefaultAllocator, DimName, RealField, VectorN};
5+
use nalgebra::{RealField, SVector};
76
use num::{Bounded, CheckedAdd, CheckedMul, CheckedSub, FromPrimitive, Integer, ToPrimitive};
87

98
/// Convenience trait that combines `Send` and `Sync`
@@ -52,15 +51,12 @@ pub trait Real: RealField + FromPrimitive + ToPrimitive + Debug + Default + Thre
5251
Some(T::from_f64(self.to_f64()?)?)
5352
}
5453

55-
fn try_convert_vec_from<R, D>(vec: &VectorN<R, D>) -> Option<VectorN<Self, D>>
54+
fn try_convert_vec_from<R, const D: usize>(vec: &SVector<R, D>) -> Option<SVector<Self, D>>
5655
where
5756
R: Real,
58-
D: DimName,
59-
DefaultAllocator: Allocator<R, D>,
60-
DefaultAllocator: Allocator<Self, D>,
6157
{
62-
let mut converted = VectorN::<Self, D>::zeros();
63-
for i in 0..D::dim() {
58+
let mut converted = SVector::<Self, D>::zeros();
59+
for i in 0..D {
6460
converted[i] = vec[i].try_convert()?
6561
}
6662
Some(converted)

0 commit comments

Comments
 (0)