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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! Contains coordinate structs, QuadVec for quadtrees, and OctVec for octrees, as well as their LodVec implementation
use crate::traits::LodVec;
/// A Lod Vector for use in a quadtree
/// It subdivides into 4 children of equal size
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Debug)]
pub struct QuadVec {
/// x position in the quadtree
pub x: u64,
/// y position in the quadtree
pub y: u64,
/// lod depth in the quadtree
/// this is limited, hence we use u8
pub depth: u8,
}
impl QuadVec {
/// creates a new vector from the raw x and y coords
/// # Args
/// * `x` The x position in the tree. Allowed range scales with the depth (doubles as the depth increases by one)
/// * `y` The x position in the tree. Allowed range scales with the depth (doubles as the depth increases by one)
/// * `depth` the lod depth the coord is at. This is soft limited at roughly 60, and the tree might behave weird if it gets higher
#[inline]
pub fn new(x: u64, y: u64, depth: u8) -> Self {
Self { x, y, depth }
}
/// creates a new vector from floating point coords
/// mapped so that (0, 0) is the bottom left corner and (1, 1) is the top right
/// # Args
/// * `x` x coord of the float vector, from 0 to 1
/// * `y` y coord of the float vector, from 0 to 1
/// * `depth` The lod depth of the coord
#[inline]
pub fn from_float_coords(x: f64, y: f64, depth: u8) -> Self {
// scaling factor due to the lod depth
let scale_factor = (1 << depth) as f64;
// and get the actual coord
Self {
x: (x * scale_factor) as u64,
y: (y * scale_factor) as u64,
depth,
}
}
/// converts the coord into float coords
/// Returns a tuple of (x: f64, y: f64) to represent the coordinates, this is the lower left corner
#[inline]
pub fn get_float_coords(self) -> (f64, f64) {
// scaling factor to scale the coords down with
let scale_factor = 1.0 / (1 << self.depth) as f64;
// and the x and y coords
(self.x as f64 * scale_factor, self.y as f64 * scale_factor)
}
/// gets the size the chunk of this lod vector takes up, with the root taking up
#[inline]
pub fn get_size(self) -> f64 {
1.0 / (1 << self.depth) as f64
}
}
impl LodVec for QuadVec {
#[inline]
fn num_children() -> usize {
4
}
#[inline]
fn root() -> Self {
Self {
x: 0,
y: 0,
depth: 0,
}
}
#[inline]
fn get_child(self, index: usize) -> Self {
match index {
0 => QuadVec::new(self.x << 1, self.y << 1, self.depth + 1),
1 => QuadVec::new(self.x << 1, (self.y << 1) + 1, self.depth + 1),
2 => QuadVec::new((self.x << 1) + 1, self.y << 1, self.depth + 1),
_ => QuadVec::new((self.x << 1) + 1, (self.y << 1) + 1, self.depth + 1),
}
}
#[inline]
fn can_subdivide(self, node: Self, detail: u64) -> bool {
// return early if the level of this chunk is too high
if node.depth >= self.depth {
return false;
}
// difference in lod level between the target and the node
let level_difference = self.depth - node.depth;
// minimum corner of the bounding box
let min = (
(node.x << (level_difference + 1))
.saturating_sub(((detail + 1) << level_difference) - (1 << level_difference)),
(node.y << (level_difference + 1))
.saturating_sub(((detail + 1) << level_difference) - (1 << level_difference)),
);
// max as well
let max = (
(node.x << (level_difference + 1))
.saturating_add(((detail + 1) << level_difference) + (1 << level_difference)),
(node.y << (level_difference + 1))
.saturating_add(((detail + 1) << level_difference) + (1 << level_difference)),
);
// local position of the target, which is one lod level higher to allow more detail
let local = (self.x << 1, self.y << 1);
// check if the target is inside of the bounding box
local.0 >= min.0 && local.0 < max.0 && local.1 >= min.1 && local.1 < max.1
}
}
/// A Lod Vector for use in an octree
/// It subdivides into 8 children of equal size
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Debug)]
pub struct OctVec {
/// x position in the octree
pub x: u64,
/// y position in the octree
pub y: u64,
/// z position in the octree
pub z: u64,
/// lod depth in the octree
/// this is limited, hence we use u8
pub depth: u8,
}
impl OctVec {
/// creates a new vector from the raw x and y coords
/// # Args
/// * `x` The x position in the tree. Allowed range scales with the depth (doubles as the depth increases by one)
/// * `y` The y position in the tree. Allowed range scales with the depth (doubles as the depth increases by one)
/// * `z` The z position in the tree. Allowed range scales with the depth (doubles as the depth increases by one)
/// * `depth` the lod depth the coord is at. This is soft limited at roughly 60, and the tree might behave weird if it gets higher
#[inline]
pub fn new(x: u64, y: u64, z: u64, depth: u8) -> Self {
Self { x, y, z, depth }
}
/// creates a new vector from floating point coords
/// mapped so that (0, 0, 0) is the front bottom left corner and (1, 1, 1) is the back top right
/// # Args
/// * `x` x coord of the float vector, from 0 to 1
/// * `y` y coord of the float vector, from 0 to 1
/// * `z` z coord of the float vector, from 0 to 1
/// * `depth` The lod depth of the coord
#[inline]
pub fn from_float_coords(x: f64, y: f64, z: f64, depth: u8) -> Self {
// scaling factor due to the lod depth
let scale_factor = (1 << depth) as f64;
// and get the actual coord
Self {
x: (x * scale_factor) as u64,
y: (y * scale_factor) as u64,
z: (z * scale_factor) as u64,
depth,
}
}
/// converts the coord into float coords
/// Returns a tuple of (x: f64, y: f64, z: f64) to represent the coordinates, at the front bottom left corner
#[inline]
pub fn get_float_coords(self) -> (f64, f64, f64) {
// scaling factor to scale the coords down with
let scale_factor = 1.0 / (1 << self.depth) as f64;
// and the x and y coords
(
self.x as f64 * scale_factor,
self.y as f64 * scale_factor,
self.z as f64 * scale_factor,
)
}
/// gets the size the chunk of this lod vector takes up, with the root taking up
#[inline]
pub fn get_size(self) -> f64 {
1.0 / (1 << self.depth) as f64
}
}
impl LodVec for OctVec {
#[inline]
fn num_children() -> usize {
8
}
#[inline]
fn root() -> Self {
Self {
x: 0,
y: 0,
z: 0,
depth: 0,
}
}
#[inline]
fn get_child(self, index: usize) -> Self {
match index {
0 => Self::new(self.x << 1, self.y << 1, self.z << 1, self.depth + 1),
1 => Self::new(self.x << 1, self.y << 1, (self.z << 1) + 1, self.depth + 1),
2 => Self::new(self.x << 1, (self.y << 1) + 1, self.z << 1, self.depth + 1),
3 => Self::new(
self.x << 1,
(self.y << 1) + 1,
(self.z << 1) + 1,
self.depth + 1,
),
4 => Self::new((self.x << 1) + 1, self.y << 1, self.z << 1, self.depth + 1),
5 => Self::new(
(self.x << 1) + 1,
self.y << 1,
(self.z << 1) + 1,
self.depth + 1,
),
6 => Self::new(
(self.x << 1) + 1,
(self.y << 1) + 1,
self.z << 1,
self.depth + 1,
),
_ => Self::new(
(self.x << 1) + 1,
(self.y << 1) + 1,
(self.z << 1) + 1,
self.depth + 1,
),
}
}
#[inline]
fn can_subdivide(self, node: Self, detail: u64) -> bool {
// return early if the level of this chunk is too high
if node.depth >= self.depth {
return false;
}
// difference in lod level between the target and the node
let level_difference = self.depth - node.depth;
// minimum corner of the bounding box
let min = (
(node.x << (level_difference + 1))
.saturating_sub(((detail + 1) << level_difference) - (1 << level_difference)),
(node.y << (level_difference + 1))
.saturating_sub(((detail + 1) << level_difference) - (1 << level_difference)),
(node.z << (level_difference + 1))
.saturating_sub(((detail + 1) << level_difference) - (1 << level_difference)),
);
// max as well
let max = (
(node.x << (level_difference + 1))
.saturating_add(((detail + 1) << level_difference) + (1 << level_difference)),
(node.y << (level_difference + 1))
.saturating_add(((detail + 1) << level_difference) + (1 << level_difference)),
(node.z << (level_difference + 1))
.saturating_add(((detail + 1) << level_difference) + (1 << level_difference)),
);
// local position of the target
let local = (self.x << 1, self.y << 1, self.z << 1);
// check if the target is inside of the bounding box
local.0 >= min.0
&& local.0 < max.0
&& local.1 >= min.1
&& local.1 < max.1
&& local.2 >= min.2
&& local.2 < max.2
}
}