Skip to content

Commit 349ab78

Browse files
committed
Formatting
1 parent 3ac3bb2 commit 349ab78

6 files changed

Lines changed: 70 additions & 66 deletions

File tree

fenris-geometry/src/lib.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use std::fmt::Debug;
1111
mod polygon;
1212
mod polytope;
1313
mod primitives;
14+
use crate::util::index_set_nth_power_iter;
1415
pub use polygon::*;
1516
pub use polytope::*;
1617
pub use primitives::*;
17-
use crate::util::{index_set_nth_power_iter};
1818

1919
pub mod polymesh;
2020
pub mod sdf;
@@ -153,10 +153,18 @@ where
153153
{
154154
/// Computes the minimal bounding box which encloses both `this` and `other`.
155155
pub fn enclose(&self, other: &AxisAlignedBoundingBox<T, D>) -> Self {
156-
let min = self.min.iter().zip(&other.min.coords).map(|(a, b)| T::min(*a, *b));
156+
let min = self
157+
.min
158+
.iter()
159+
.zip(&other.min.coords)
160+
.map(|(a, b)| T::min(*a, *b));
157161
let min = OVector::<T, D>::from_iterator(min);
158162

159-
let max = self.max.iter().zip(&other.max.coords).map(|(a, b)| T::max(*a, *b));
163+
let max = self
164+
.max
165+
.iter()
166+
.zip(&other.max.coords)
167+
.map(|(a, b)| T::max(*a, *b));
160168
let max = OVector::<T, D>::from_iterator(max);
161169

162170
AxisAlignedBoundingBox::new(min.into(), max.into())
@@ -242,20 +250,20 @@ where
242250
}
243251

244252
/// Creates an iterator over the corners of the bounding box.
245-
pub fn corners_iter<'a>(&'a self) -> impl 'a + Iterator<Item=OPoint<T, D>>
253+
pub fn corners_iter<'a>(&'a self) -> impl 'a + Iterator<Item = OPoint<T, D>>
246254
where
247-
DefaultAllocator: Allocator<usize, D>
255+
DefaultAllocator: Allocator<usize, D>,
248256
{
249257
// We can enumerate the corners by looking at {0, 1}^D, i.e. the D-th power of the
250258
// set {0, 1}, and associating 0 and 1 with min and max coordinates for the i-th axis.
251-
index_set_nth_power_iter::<D>(2)
252-
.map(move |multi_idx| {
253-
OVector::<T, D>::from_fn(|idx, _| match multi_idx[idx] {
254-
0 => self.min[idx].clone(),
255-
1 => self.max[idx].clone(),
256-
_ => unreachable!()
257-
}).into()
259+
index_set_nth_power_iter::<D>(2).map(move |multi_idx| {
260+
OVector::<T, D>::from_fn(|idx, _| match multi_idx[idx] {
261+
0 => self.min[idx].clone(),
262+
1 => self.max[idx].clone(),
263+
_ => unreachable!(),
258264
})
265+
.into()
266+
})
259267
}
260268

261269
/// Compute the point in the bounding box furthest away from the given point.
@@ -271,22 +279,21 @@ where
271279
#[replace_float_literals(T::from_f64(literal).unwrap())]
272280
pub fn furthest_point_to(&self, point: &OPoint<T, D>) -> OPoint<T, D>
273281
where
274-
DefaultAllocator: Allocator<usize, D>
282+
DefaultAllocator: Allocator<usize, D>,
275283
{
276284
// It turns out that we can choose, along each dimension, the point in the interval
277285
// [a_i, b_i] furthest away from p_i.
278-
point.coords.zip_zip_map(
279-
&self.min.coords,
280-
&self.max.coords,
281-
|p_i, a_i, b_i| {
286+
point
287+
.coords
288+
.zip_zip_map(&self.min.coords, &self.max.coords, |p_i, a_i, b_i| {
282289
let mid = (a_i + b_i) / 2.0;
283290
if p_i < mid {
284291
b_i
285292
} else {
286293
a_i
287294
}
288-
}
289-
).into()
295+
})
296+
.into()
290297
}
291298

292299
/// The squared distance to the point in the bounding box furthest away from the given point.
@@ -297,7 +304,7 @@ where
297304
pub fn max_dist2_to(&self, point: &OPoint<T, D>) -> T
298305
where
299306
// TODO: Use DimAllocator and SmallDim
300-
DefaultAllocator: Allocator<usize, D>
307+
DefaultAllocator: Allocator<usize, D>,
301308
{
302309
(self.furthest_point_to(point) - point).norm_squared()
303310
}
@@ -309,8 +316,8 @@ where
309316
/// Panic behavior is identical to [`max_dist2_to`](Self::max_dist2_to).
310317
pub fn max_dist_to(&self, point: &OPoint<T, D>) -> T
311318
where
312-
// TODO: Use DimAllocator and SmallDim
313-
DefaultAllocator: Allocator<usize, D>
319+
// TODO: Use DimAllocator and SmallDim
320+
DefaultAllocator: Allocator<usize, D>,
314321
{
315322
self.max_dist2_to(point).sqrt()
316323
}

fenris-geometry/src/proptest.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
use crate::Orientation::Counterclockwise;
2-
use crate::{AxisAlignedBoundingBox, AxisAlignedBoundingBox2d, AxisAlignedBoundingBox3d, HalfPlane, LineSegment2d, Orientation, Quad2d, Triangle, Triangle2d, Triangle3d};
3-
use nalgebra::proptest::vector;
4-
use nalgebra::{DimName, Point2, Point3, Unit, Vector2, Vector3, U2, U3, DefaultAllocator, RealField};
2+
use crate::{
3+
AxisAlignedBoundingBox, AxisAlignedBoundingBox2d, AxisAlignedBoundingBox3d, HalfPlane, LineSegment2d, Orientation,
4+
Quad2d, Triangle, Triangle2d, Triangle3d,
5+
};
56
use nalgebra::allocator::Allocator;
7+
use nalgebra::proptest::vector;
8+
use nalgebra::{DefaultAllocator, DimName, Point2, Point3, RealField, Unit, Vector2, Vector3, U2, U3};
69
use proptest::prelude::*;
710

8-
pub fn aabb<D>() -> impl Strategy<Value=AxisAlignedBoundingBox<f64, D>>
11+
pub fn aabb<D>() -> impl Strategy<Value = AxisAlignedBoundingBox<f64, D>>
912
where
1013
D: DimName,
11-
DefaultAllocator: Allocator<f64, D>
14+
DefaultAllocator: Allocator<f64, D>,
1215
{
13-
let value_strategy = -10.0 .. 10.0;
14-
(vector(value_strategy.clone(), D::name()), vector(value_strategy, D::name()))
16+
let value_strategy = -10.0..10.0;
17+
(
18+
vector(value_strategy.clone(), D::name()),
19+
vector(value_strategy, D::name()),
20+
)
1521
.prop_map(|(a, b)| {
1622
let min = a.zip_map(&b, |a_i, b_i| RealField::min(a_i, b_i));
1723
let max = a.zip_map(&b, |a_i, b_i| RealField::max(a_i, b_i));
1824
AxisAlignedBoundingBox::new(min.into(), max.into())
1925
})
2026
}
2127

22-
pub fn aabb2() -> impl Strategy<Value=AxisAlignedBoundingBox2d<f64>> {
28+
pub fn aabb2() -> impl Strategy<Value = AxisAlignedBoundingBox2d<f64>> {
2329
aabb()
2430
}
2531

26-
pub fn aabb3() -> impl Strategy<Value=AxisAlignedBoundingBox3d<f64>> {
32+
pub fn aabb3() -> impl Strategy<Value = AxisAlignedBoundingBox3d<f64>> {
2733
aabb()
2834
}
2935

fenris-geometry/src/sdf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use fenris_traits::Real;
2-
use nalgebra::{Point2, Scalar, Vector2, U2, OPoint};
2+
use nalgebra::{OPoint, Point2, Scalar, Vector2, U2};
33

44
use crate::{AxisAlignedBoundingBox2d, BoundedGeometry};
55
use numeric_literals::replace_float_literals;

fenris-geometry/src/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use fenris_traits::Real;
2-
use nalgebra::{DefaultAllocator, DimName, OVector, UnitVector3, Vector3};
32
use nalgebra::allocator::Allocator;
3+
use nalgebra::{DefaultAllocator, DimName, OVector, UnitVector3, Vector3};
44

55
pub fn compute_orthonormal_vectors_3d<T: Real>(vector: &UnitVector3<T>) -> [UnitVector3<T>; 2] {
66
// Ported from
@@ -90,7 +90,7 @@ macro_rules! assert_line_segments_approx_equal {
9090
/// TODO: Currently only implicitly tested through [`AxisAlignedBoundingBox::corners_iter`].
9191
pub(crate) fn index_set_nth_power_iter<D: DimName>(n: usize) -> impl Iterator<Item = OVector<usize, D>>
9292
where
93-
DefaultAllocator: Allocator<usize, D>
93+
DefaultAllocator: Allocator<usize, D>,
9494
{
9595
let mut multi_idx = OVector::from_element(0);
9696
let mut i = 0;
@@ -111,4 +111,4 @@ where
111111
None
112112
}
113113
})
114-
}
114+
}

fenris-geometry/tests/unit_tests/aabb.rs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use matrixcompare::assert_scalar_eq;
2-
use fenris_geometry::AxisAlignedBoundingBox;
3-
use nalgebra::{DefaultAllocator, DimName, OPoint, point, U2};
4-
use nalgebra::allocator::Allocator;
5-
use proptest::prelude::*;
61
use fenris::allocators::DimAllocator;
72
use fenris_geometry::proptest::{aabb2, aabb3, point2, point3};
3+
use fenris_geometry::AxisAlignedBoundingBox;
4+
use matrixcompare::assert_scalar_eq;
5+
use nalgebra::allocator::Allocator;
86
use nalgebra::distance_squared;
7+
use nalgebra::{point, DefaultAllocator, DimName, OPoint, U2};
8+
use proptest::prelude::*;
99

1010
#[test]
1111
fn aabb_intersects_2d() {
@@ -48,54 +48,44 @@ fn aabb_intersects_2d() {
4848
}
4949

5050
/// Helper function for collecting corners from corner iterator.
51-
fn corners<D: DimName>(aabb:& AxisAlignedBoundingBox<f64, D>) -> Vec<OPoint<f64, D>>
51+
fn corners<D: DimName>(aabb: &AxisAlignedBoundingBox<f64, D>) -> Vec<OPoint<f64, D>>
5252
where
53-
DefaultAllocator: DimAllocator<f64, D>
53+
DefaultAllocator: DimAllocator<f64, D>,
5454
{
5555
aabb.corners_iter().collect()
5656
}
5757

5858
fn compare_unordered_points<D: DimName>(points1: &[OPoint<f64, D>], points2: &[OPoint<f64, D>]) -> bool
5959
where
60-
DefaultAllocator: Allocator<f64, D>
60+
DefaultAllocator: Allocator<f64, D>,
6161
{
6262
assert_eq!(points1.len(), points2.len());
6363
// This is O(n^2) but is acceptable for use in tests here
64-
points1.iter()
65-
.all(|p1| points2.contains(p1))
64+
points1.iter().all(|p1| points2.contains(p1))
6665
}
6766

6867
macro_rules! assert_unordered_eq {
69-
($p1:expr, $p2:expr) => {
70-
{
71-
let eq = compare_unordered_points((&$p1).as_ref(), (&$p2).as_ref());
72-
if !eq {
73-
dbg!(&$p1, &$p2);
74-
assert!(eq, "Point lists do not contain the same points");
75-
}
68+
($p1:expr, $p2:expr) => {{
69+
let eq = compare_unordered_points((&$p1).as_ref(), (&$p2).as_ref());
70+
if !eq {
71+
dbg!(&$p1, &$p2);
72+
assert!(eq, "Point lists do not contain the same points");
7673
}
77-
}
74+
}};
7875
}
7976

80-
8177
#[test]
8278
fn test_aabb_corners_iter() {
83-
8479
// 1D
8580
{
8681
let aabb = AxisAlignedBoundingBox::new(point![3.0], point![4.0]);
87-
assert_eq!(corners(&aabb), vec![ point![3.0 ], point![4.0] ]);
82+
assert_eq!(corners(&aabb), vec![point![3.0], point![4.0]]);
8883
}
8984

9085
// 2D
9186
{
9287
let aabb = AxisAlignedBoundingBox::new(point![3.0, 4.0], point![5.0, 6.0]);
93-
let expected = [
94-
[3.0, 4.0],
95-
[3.0, 6.0],
96-
[5.0, 4.0],
97-
[5.0, 6.0]
98-
].map(OPoint::from);
88+
let expected = [[3.0, 4.0], [3.0, 6.0], [5.0, 4.0], [5.0, 6.0]].map(OPoint::from);
9989
assert_unordered_eq!(corners(&aabb), &expected);
10090
}
10191

@@ -110,8 +100,9 @@ fn test_aabb_corners_iter() {
110100
[4.0, 2.0, 3.0],
111101
[4.0, 2.0, 6.0],
112102
[4.0, 5.0, 3.0],
113-
[4.0, 5.0, 6.0]
114-
].map(OPoint::from);
103+
[4.0, 5.0, 6.0],
104+
]
105+
.map(OPoint::from);
115106
assert_unordered_eq!(corners(&aabb), expected);
116107
}
117108
}
@@ -195,4 +186,4 @@ proptest! {
195186
prop_assert!(aabb.contains_point(&q));
196187
}
197188

198-
}
189+
}

src/allocators.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ where
2121
{
2222
}
2323

24-
pub use fenris_traits::allocators::*;
24+
pub use fenris_traits::allocators::*;

0 commit comments

Comments
 (0)