1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use crate::{extent::IntegerExtent, Extent, ExtentN, Point2, PointN};
use core::ops::{Mul, Range};
use itertools::{iproduct, Product};
pub type Extent2<T> = ExtentN<[T; 2]>;
pub type Extent2i = ExtentN<[i32; 2]>;
pub type Extent2f = ExtentN<[f32; 2]>;
impl<T> Extent<[T; 2]> for Extent2<T>
where
T: Copy + Mul<Output = T>,
{
type VolumeType = T;
fn volume(&self) -> T {
self.shape.x() * self.shape.y()
}
}
pub struct Extent2PointIter<T>
where
Range<T>: Iterator<Item = T>,
{
product_iter: Product<Range<T>, Range<T>>,
}
impl<T> Iterator for Extent2PointIter<T>
where
T: Clone,
Range<T>: Iterator<Item = T>,
{
type Item = Point2<T>;
fn next(&mut self) -> Option<Self::Item> {
self.product_iter.next().map(|(y, x)| PointN([x, y]))
}
}
impl IntegerExtent<[i32; 2]> for Extent2i {
type PointIter = Extent2PointIter<i32>;
fn num_points(&self) -> usize {
self.volume() as usize
}
fn iter_points(&self) -> Self::PointIter {
let lub = self.least_upper_bound();
Extent2PointIter {
product_iter: iproduct!(self.minimum.y()..lub.y(), self.minimum.x()..lub.x()),
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn row_major_extent_iter() {
let extent = Extent2i::from_min_and_shape(PointN([0, 0]), PointN([2, 2]));
let points: Vec<_> = extent.iter_points().collect();
assert_eq!(
points,
vec![
PointN([0, 0]),
PointN([1, 0]),
PointN([0, 1]),
PointN([1, 1]),
]
);
}
}