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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use super::CellIndex;
use crate::{
Direction, Resolution,
coord::{CoordCube, CoordIJK, LocalIJK},
error::LocalIjError,
index::bits,
};
use core::cmp::max;
/// Iterator over a children cell index at a given resolution.
pub struct Children {
/// Starting cell resolution.
parent_resolution: Resolution,
/// Targeted cell resolution.
target_resolution: Resolution,
/// Iterator scratch space, used to build cell index iteratively
scratchpad: u64,
/// Number of cell index to skip for pentagonal index, there is one per
/// resolution.
skip_count: i16,
/// Remaining children at the targeted resolution.
count: u64,
}
impl Children {
/// Returns an iterator over the children cell index at the given
/// resolution.
pub fn new(index: CellIndex, resolution: Resolution) -> Self {
Self {
parent_resolution: index.resolution(),
target_resolution: resolution,
scratchpad: get_starting_state(index, resolution),
skip_count: if index.is_pentagon() {
i16::from(u8::from(resolution))
} else {
-1
},
count: index.children_count(resolution),
}
}
/// Increment the direction at `resolution` and return it.
fn next_direction(&mut self, resolution: Resolution) -> u8 {
// Shift the 1 to apply it on the right direction.
let one = 1 << resolution.direction_offset();
// Add one to the direction.
//
// Note that if the direction was 7 (unused) this automatically reset
// the direction to [`Direction::CENTER`] (wraparound) AND increment the
// direction of lower resolution (thanks to carry propagation).
self.scratchpad += one;
bits::get_direction(self.scratchpad, resolution)
}
}
impl Iterator for Children {
type Item = CellIndex;
fn next(&mut self) -> Option<CellIndex> {
// No more children, we're done.
if self.count == 0 {
return None;
}
// Extract the current index, to return it.
let index = CellIndex::new_unchecked(self.scratchpad);
self.count -= 1;
// Prepare the next iteration, if any, by incrementing the scratchpad.
if self.count != 0 {
for resolution in Resolution::range(
self.parent_resolution,
self.target_resolution,
)
.rev()
{
// Move to the next direction value.
let direction = self.next_direction(resolution);
// First K axe of each resolution is skipped for pentagonal
// index.
if self.skip_count == i16::from(resolution)
&& direction == u8::from(Direction::K)
{
self.next_direction(resolution);
self.skip_count -= 1;
}
// If we have exhausted this resolution, move to the lower one.
if Direction::try_from(direction).is_err() {
self.scratchpad =
bits::clr_direction(self.scratchpad, resolution);
continue;
}
break;
}
}
Some(index)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let count = usize::try_from(self.count).unwrap_or(usize::MAX);
(count, Some(count))
}
}
impl ExactSizeIterator for Children {}
// -----------------------------------------------------------------------------
/// Return the starting state for the listing process.
fn get_starting_state(index: CellIndex, resolution: Resolution) -> u64 {
let parent_resolution = index.resolution();
// Compute the range of resolution to iterate over.
//
// e.g. if we list children for cell index at resolution 2 to resolution 6
// we need to iterate of 4 resolution (resolutions 3, 4, 5 and 6).
let range =
usize::from(resolution).saturating_sub(parent_resolution.into());
let mut scratchpad = u64::from(index);
// If we have resolution between current and targeted one we clear their
// directions.
if range != 0 {
// Mask with the right number of bit to cover the directions.
let mask = (1 << (range * h3o_bit::DIRECTION_BITSIZE)) - 1;
// Mask offset required to clear the directions.
let offset = resolution.direction_offset();
// Clear directions.
scratchpad &= !(mask << offset);
// Set resolution.
scratchpad = bits::set_resolution(scratchpad, resolution);
}
scratchpad
}
// -----------------------------------------------------------------------------
/// Iterator over a children cell index at a given resolution.
#[derive(Debug, Clone)]
pub struct GridPathCells {
/// Starting cell .
anchor: CellIndex,
/// Starting coordinate.
start: CoordCube,
// Path length.
distance: i32,
// Current position in the path.
n: i32,
/// Translation offset for the i component.
i_step: f64,
/// Translation offset for the j component.
j_step: f64,
/// Translation offset for the k component.
k_step: f64,
}
impl GridPathCells {
/// Returns an iterator over the children cell index at the given
/// resolution.
pub fn new(start: CellIndex, end: CellIndex) -> Result<Self, LocalIjError> {
let anchor = start;
// Get IJK coords for the start and end.
let src = start.to_local_ijk(start)?;
let dst = end.to_local_ijk(start)?;
let distance = src.coord().distance(dst.coord());
// Convert IJK to cube coordinates suitable for linear interpolation
let start = CoordCube::from(*src.coord());
let end = CoordCube::from(*dst.coord());
let (i_step, j_step, k_step) = if distance == 0 {
(0., 0., 0.)
} else {
(
f64::from(end.i - start.i) / f64::from(distance),
f64::from(end.j - start.j) / f64::from(distance),
f64::from(end.k - start.k) / f64::from(distance),
)
};
Ok(Self {
anchor,
start,
distance,
n: 0,
i_step,
j_step,
k_step,
})
}
}
impl Iterator for GridPathCells {
type Item = Result<CellIndex, LocalIjError>;
fn next(&mut self) -> Option<Self::Item> {
(self.n <= self.distance).then(|| {
let coord = self.start.translate((
self.i_step * f64::from(self.n),
self.j_step * f64::from(self.n),
self.k_step * f64::from(self.n),
));
self.n += 1;
// Convert cube -> ijk -> h3 index
let local_ijk = LocalIJK {
anchor: self.anchor,
coord: CoordIJK::from(coord),
};
CellIndex::try_from(local_ijk)
})
}
fn size_hint(&self) -> (usize, Option<usize>) {
let count = usize::try_from(max(self.distance - self.n, 0))
.unwrap_or(usize::MAX);
(count, Some(count))
}
}
impl ExactSizeIterator for GridPathCells {}