pythagore/bbox/
range_to_inclusive.rs

1use std::ops::Bound::{Excluded, Included, Unbounded};
2use std::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
3use na::{Point, Scalar};
4
5use crate::{BBox, Intersection, PointBounds};
6use crate::bbox::utils::{min_bound, min_point};
7use crate::traits::DimensionBounds;
8
9/// Builds a bounding box from a range of points
10///
11/// # Example
12/// ```
13/// use std::ops::Bound::{Included, Unbounded};
14/// use nalgebra::point;
15/// use pythagore::BBox;
16///
17/// assert_eq!(
18///     BBox::from(..=point![3, 4]),
19///     BBox::from([
20///        (Unbounded, Included(3)),
21///        (Unbounded, Included(4)),
22///     ])
23/// )
24/// ```
25impl<N: Copy + Scalar, const D: usize> From<RangeToInclusive<Point<N, D>>> for BBox<N, D> {
26    fn from(value: RangeToInclusive<Point<N, D>>) -> Self {
27        let mut ranges = [(Unbounded, Unbounded); D];
28
29        for (idx, range) in ranges.iter_mut().enumerate() {
30            range.1 = Included(unsafe { *value.end.get_unchecked(idx) });
31        }
32
33        BBox::from(ranges)
34    }
35}
36
37impl<N: Copy + Scalar, const D: usize> DimensionBounds<N, D> for RangeToInclusive<Point<N, D>> {
38    type Output = RangeToInclusive<N>;
39
40    #[inline]
41    unsafe fn get_bounds_unchecked(&self, idx: usize) -> Self::Output {
42        ..=*self.end.get_unchecked(idx)
43    }
44}
45
46impl<N: Copy + Scalar, const D: usize> PointBounds<N, D> for RangeToInclusive<Point<N, D>> {
47    #[inline]
48    fn start_point(&self) -> Option<Point<N, D>> {
49        None
50    }
51
52    #[inline]
53    fn end_point(&self) -> Option<Point<N, D>> {
54        Some(self.end)
55    }
56}
57
58impl<N: Copy + PartialOrd + Scalar, const D: usize> Intersection<BBox<N, D>> for RangeToInclusive<Point<N, D>> {
59    type Output = BBox<N, D>;
60
61    #[inline]
62    fn intersection(&self, lhs: &BBox<N, D>) -> Self::Output {
63        lhs.intersection(self)
64    }
65}
66
67impl<N: Copy + PartialOrd + Scalar, const D: usize> Intersection<Range<Point<N, D>>> for RangeToInclusive<Point<N, D>> {
68    type Output = BBox<N, D>;
69
70    fn intersection(&self, lhs: &Range<Point<N, D>>) -> Self::Output {
71        let mut ranges = [(Unbounded, Unbounded); D];
72
73        for (idx, range) in ranges.iter_mut().enumerate() {
74            range.0 = Included(*unsafe { lhs.start.get_unchecked(idx) });
75
76            let rex = unsafe { self.end.get_unchecked(idx) };
77            let lex = unsafe { lhs.end.get_unchecked(idx) };
78
79            range.1 = if rex < lex { Included(*rex) } else { Excluded(*lex) };
80        }
81
82        BBox::from(ranges)
83    }
84}
85
86impl<N: Copy + Scalar, const D: usize> Intersection<RangeFrom<Point<N, D>>> for RangeToInclusive<Point<N, D>> {
87    type Output = RangeInclusive<Point<N, D>>;
88
89    #[inline]
90    fn intersection(&self, lhs: &RangeFrom<Point<N, D>>) -> Self::Output {
91        lhs.start..=self.end
92    }
93}
94
95impl<N: Scalar, const D: usize> Intersection<RangeFull> for RangeToInclusive<Point<N, D>> {
96    type Output = RangeToInclusive<Point<N, D>>;
97
98    #[inline]
99    fn intersection(&self, _: &RangeFull) -> Self::Output {
100        self.clone()
101    }
102}
103
104impl<N: Copy + Default + Ord + Scalar, const D: usize> Intersection<RangeInclusive<Point<N, D>>> for RangeToInclusive<Point<N, D>> {
105    type Output = RangeInclusive<Point<N, D>>;
106
107    #[inline]
108    fn intersection(&self, lhs: &RangeInclusive<Point<N, D>>) -> Self::Output {
109        *lhs.start()..=min_point(&self.end, lhs.end())
110    }
111}
112
113impl<N: Copy + PartialOrd + Scalar, const D: usize> Intersection<RangeTo<Point<N, D>>> for RangeToInclusive<Point<N, D>> {
114    type Output = BBox<N, D>;
115
116    fn intersection(&self, lhs: &RangeTo<Point<N, D>>) -> Self::Output {
117        let mut ranges = [(Unbounded, Unbounded); D];
118
119        for (idx, range) in ranges.iter_mut().enumerate() {
120            let rex = unsafe { self.end.get_unchecked(idx) };
121            let lex = unsafe { lhs.end.get_unchecked(idx) };
122
123            range.1 = if rex < lex { Included(*rex) } else { Excluded(*lex) };
124        }
125
126        BBox::from(ranges)
127    }
128}
129
130impl<N: Copy + Default + Ord + Scalar, const D: usize> Intersection for RangeToInclusive<Point<N, D>> {
131    type Output = RangeToInclusive<Point<N, D>>;
132
133    #[inline]
134    fn intersection(&self, lhs: &RangeToInclusive<Point<N, D>>) -> Self::Output {
135        ..=min_point(&self.end, &lhs.end)
136    }
137}
138
139impl<N: Copy + PartialOrd + Scalar, const D: usize> Intersection<(Bound<Point<N, D>>, Bound<Point<N, D>>)> for RangeToInclusive<Point<N, D>> {
140    type Output = BBox<N, D>;
141
142    fn intersection(&self, lhs: &(Bound<Point<N, D>>, Bound<Point<N, D>>)) -> Self::Output {
143        let mut ranges = [(Unbounded, Unbounded); D];
144
145        for (idx, range) in ranges.iter_mut().enumerate() {
146            let lhs = unsafe { lhs.get_bounds_unchecked(idx) };
147
148            range.0 = lhs.0;
149            range.1 = min_bound(Included(unsafe { *self.end.get_unchecked(idx) }), lhs.1);
150        }
151
152        BBox::from(ranges)
153    }
154}
155
156#[cfg(test)]
157mod tests {
158    use na::point;
159    use super::*;
160
161    #[test]
162    fn test_intersection() {
163        assert_eq!((..=point![10, 15]).intersection(&(point![5, 0]..point![15, 10])), BBox::from([
164            (Included(5), Included(10)),
165            (Included(0), Excluded(10)),
166        ]));
167        assert_eq!((..=point![10, 15]).intersection(&(point![5, 0]..)), point![5, 0]..=point![10, 15]);
168        assert_eq!((..=point![10, 15]).intersection(&(..)), ..=point![10, 15]);
169        assert_eq!((..=point![10, 15]).intersection(&(point![5, 0]..=point![15, 10])), point![5, 0]..=point![10, 10]);
170        assert_eq!((..=point![10, 15]).intersection(&(..point![15, 10])), BBox::from([
171            (Unbounded, Included(10)),
172            (Unbounded, Excluded(10)),
173        ]));
174        assert_eq!((..=point![10, 15]).intersection(&(..=point![15, 10])), ..=point![10, 10]);
175    }
176
177    mod dimension_bounds {
178        use na::point;
179        use super::*;
180
181        #[test]
182        fn test_get_bounds() {
183            assert_eq!((..=point![3, 4]).get_bounds(0), ..=3);
184            assert_eq!((..=point![3, 4]).get_bounds(1), ..=4);
185        }
186    }
187
188    mod point_bounds {
189        use na::point;
190        use super::*;
191
192        #[test]
193        fn test_start_point() {
194            assert_eq!(
195                (..=point![5, 5]).start_point(),
196                None
197            );
198        }
199
200        #[test]
201        fn test_end_point() {
202            assert_eq!(
203                (..=point![5, 5]).end_point(),
204                Some(point![5, 5])
205            );
206        }
207    }
208}