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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::prelude::{CoordinateType, Vector};
use num_traits::Zero;
#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RegularRepetition<T>
where T: CoordinateType {
a: Vector<T>,
b: Vector<T>,
n: u32,
m: u32,
}
impl<T> RegularRepetition<T>
where T: CoordinateType {
pub fn new(a: Vector<T>, b: Vector<T>, n: u32, m: u32) -> Self {
return RegularRepetition { a, b, n, m };
}
pub fn new_rectilinear(spacing_x: T, spacing_y: T, num_x: u32, num_y: u32) -> Self {
Self::new(Vector::new(spacing_x, T::zero()), Vector::new(T::zero(), spacing_y),
num_x, num_y)
}
pub fn iter(self) -> impl Iterator<Item=Vector<T>> {
let mut current = Vector::zero();
(0..self.m).flat_map(move |_| {
let mut row = current;
current = current + self.b;
(0..self.n).map(move |_| {
let pos = row;
row = row + self.a;
pos
})
})
}
pub fn len(&self) -> usize {
(self.n as usize) * (self.m as usize)
}
}
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct IrregularRepetition<T>
where T: CoordinateType {
offsets: Vec<Vector<T>>
}
impl<T> IrregularRepetition<T>
where T: CoordinateType {
pub fn new(offsets: Vec<Vector<T>>) -> Self {
return IrregularRepetition { offsets };
}
pub fn iter(&self) -> impl Iterator<Item=Vector<T>> + '_ {
self.offsets.iter().copied()
}
pub fn len(&self) -> usize {
self.offsets.len()
}
}
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Repetition<T>
where T: CoordinateType {
Regular(RegularRepetition<T>),
Irregular(IrregularRepetition<T>),
}
impl<T> Repetition<T>
where T: CoordinateType {
pub fn len(&self) -> usize {
match self {
Repetition::Regular(r) => r.len(),
Repetition::Irregular(r) => r.len()
}
}
pub fn iter(&self) -> Box<dyn Iterator<Item=Vector<T>> + '_> {
match self {
Repetition::Regular(r) => Box::new(r.iter()),
Repetition::Irregular(r) => Box::new(r.iter())
}
}
}