del-geo 0.1.5

2D/3D geometry utility coddes
Documentation
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use num_traits::AsPrimitive;

#[derive(Clone)]
pub struct Node<Real> {
    pub pos: (nalgebra::Vector2::<Real>, usize),
    pub idx_node_left: usize,
    pub idx_node_right: usize,
}

impl<Real> Node<Real>
    where Real: num_traits::Zero
{
    pub fn new() -> Self {
        Node {
            pos: (nalgebra::Vector2::<Real>::new(Real::zero(), Real::zero()), usize::MAX),
            idx_node_left: usize::MAX,
            idx_node_right: usize::MAX,
        }
    }
}


/// construct Kd-tree recursively
/// * `nodes`
/// * `idx_node`
/// * `points`
/// * `idx_point_begin`
/// * `idx_point_end`
/// * `i_depth`
pub fn construct_kdtree<Real>(
    nodes: &mut Vec<Node<Real>>,
    idx_node: usize,
    points: &mut Vec<(nalgebra::Vector2::<Real>, usize)>,
    idx_point_begin: usize,
    idx_point_end: usize,
    i_depth: i32)
    where Real: nalgebra::RealField + Copy
{
    if idx_point_end - idx_point_begin == 1 { // leaf node
        nodes[idx_node].pos = points[idx_point_begin];
        return;
    }

    if i_depth % 2 == 0 { // sort by x-coordinate
        points[idx_point_begin..idx_point_end].sort_by(
            |a, b| a.0.x.partial_cmp(&b.0.x).unwrap());
    } else { // sort by y-coordinate
        points[idx_point_begin..idx_point_end].sort_by(
            |a, b| a.0.y.partial_cmp(&b.0.y).unwrap());
    }

    let idx_point_mid = (idx_point_end - idx_point_begin) / 2 + idx_point_begin; // median point
    nodes[idx_node].pos = points[idx_point_mid];

    if idx_point_begin != idx_point_mid { // there is at least one point smaller than median
        let idx_node_left = nodes.len();
        nodes.resize(nodes.len() + 1, Node::new());
        nodes[idx_node].idx_node_left = idx_node_left;
        construct_kdtree(
            nodes, idx_node_left,
            points, idx_point_begin, idx_point_mid,
            i_depth + 1);
    }
    if idx_point_mid + 1 != idx_point_end { // there is at least one point larger than median
        let idx_node_right = nodes.len();
        nodes.resize(nodes.len() + 1, Node::new());
        nodes[idx_node].idx_node_right = idx_node_right;
        construct_kdtree(
            nodes, idx_node_right,
            points, idx_point_mid + 1, idx_point_end,
            i_depth + 1);
    }
}

pub fn find_edges<Real>(
    xyz: &mut Vec<Real>,
    nodes: &Vec<Node<Real>>,
    idx_node: usize,
    min: nalgebra::Vector2::<Real>,
    max: nalgebra::Vector2::<Real>,
    i_depth: i32)
    where Real: Copy
{
    if idx_node >= nodes.len() { return; }
    let pos = &nodes[idx_node].pos.0;
    if i_depth % 2 == 0 {
        xyz.push(pos[0]);
        xyz.push(min[1]);
        xyz.push(pos[0]);
        xyz.push(max[1]);
        find_edges(xyz, nodes, nodes[idx_node].idx_node_left,
                   min,
                   nalgebra::Vector2::new(pos[0], max[1]),
                   i_depth + 1);
        find_edges(xyz, nodes, nodes[idx_node].idx_node_right,
                   nalgebra::Vector2::new(pos[0], min[1]),
                   max,
                   i_depth + 1);
    } else {
        xyz.push(min[0]);
        xyz.push(pos[1]);
        xyz.push(max[0]);
        xyz.push(pos[1]);
        find_edges(xyz, nodes, nodes[idx_node].idx_node_left,
                   min,
                   nalgebra::Vector2::new(max[0], pos[1]),
                   i_depth + 1);
        find_edges(xyz, nodes, nodes[idx_node].idx_node_right,
                   nalgebra::Vector2::new(min[0], pos[1]),
                   max,
                   i_depth + 1);
    }
}

/// signed distance from axis-aligned bounding box
/// * `pos_in` - where the signed distance is evaluated
/// * `x_min` - bounding box's x-coordinate minimum
/// * `x_max` - bounding box's x-coordinate maximum
/// * `y_min` - bounding box's y-coordinate minimum
/// * `y_max` - bounding box's y-coordinate maximum
/// * signed distance (inside is negative)
fn signed_distance_aabb<Real>(
    pos_in: nalgebra::Vector2::<Real>,
    min0: nalgebra::Vector2::<Real>,
    max0: nalgebra::Vector2::<Real>) -> Real
    where Real: nalgebra::RealField + Copy,
          f64: AsPrimitive<Real>
{
    let half = 0.5_f64.as_();
    let x_center = (max0.x + min0.x) * half;
    let y_center = (max0.y + min0.y) * half;
    let x_dist = (pos_in.x - x_center).abs() - (max0.x - min0.x) * half;
    let y_dist = (pos_in.y - y_center).abs() - (max0.y - min0.y) * half;
    return x_dist.max(y_dist);
}

pub fn nearest<Real>(
    pos_near: &mut (nalgebra::Vector2<Real>, usize),
    pos_in: nalgebra::Vector2<Real>,
    nodes: &Vec<Node<Real>>,
    idx_node: usize,
    min: nalgebra::Vector2<Real>,
    max: nalgebra::Vector2<Real>,
    i_depth: usize)
    where Real: nalgebra::RealField + Copy,
          f64: AsPrimitive<Real>
{
    if idx_node >= nodes.len() { return; } // this node does not exist

    let cur_dist = (pos_near.0 - pos_in).norm();
    if cur_dist < signed_distance_aabb(pos_in, min, max) { return; }

    let pos = nodes[idx_node].pos.0;
    if (pos - pos_in).norm() < cur_dist {
        *pos_near = nodes[idx_node].pos; // update the nearest position
    }

    if i_depth % 2 == 0 { // division in x direction
        if pos_in.x < pos.x {
            nearest(pos_near, pos_in, nodes, nodes[idx_node].idx_node_left,
                    min, nalgebra::Vector2::<Real>::new(pos.x, max.y), i_depth + 1);
            nearest(pos_near, pos_in, nodes, nodes[idx_node].idx_node_right,
                    nalgebra::Vector2::<Real>::new(pos.x, min.y), max, i_depth + 1);
        } else {
            nearest(pos_near, pos_in, nodes, nodes[idx_node].idx_node_right,
                    nalgebra::Vector2::<Real>::new(pos.x, min.y), max, i_depth + 1);
            nearest(pos_near, pos_in, nodes, nodes[idx_node].idx_node_left,
                    min, nalgebra::Vector2::<Real>::new(pos.x, max.y), i_depth + 1);
        }
    } else { // division in y-direction
        if pos_in.y < pos.y {
            nearest(pos_near, pos_in, nodes, nodes[idx_node].idx_node_left,
                    min, nalgebra::Vector2::<Real>::new(max.x, pos.y), i_depth + 1);
            nearest(pos_near, pos_in, nodes, nodes[idx_node].idx_node_right,
                    nalgebra::Vector2::<Real>::new(min.x, pos.y), max, i_depth + 1);
        } else {
            nearest(pos_near, pos_in, nodes, nodes[idx_node].idx_node_right,
                    nalgebra::Vector2::<Real>::new(min.x, pos.y), max, i_depth + 1);
            nearest(pos_near, pos_in, nodes, nodes[idx_node].idx_node_left,
                    min, nalgebra::Vector2::<Real>::new(max.x, pos.y), i_depth + 1);
        }
    }
}

pub fn inside_square<Real>(
    pos_near: &mut Vec<usize>,
    pos_in: nalgebra::Vector2<Real>,
    rad: Real,
    nodes: &Vec<Node<Real>>,
    idx_node: usize,
    min: nalgebra::Vector2<Real>,
    max: nalgebra::Vector2<Real>,
    i_depth: usize)
    where Real: nalgebra::RealField + Copy,
          f64: AsPrimitive<Real>
{
    if idx_node >= nodes.len() { return; } // this node does not exist

    if rad < signed_distance_aabb(pos_in, min, max) { return; }

    let pos = nodes[idx_node].pos.0;
    if (pos.x - pos_in.x).abs() < rad && (pos.y - pos_in.y).abs() < rad {
        pos_near.push(nodes[idx_node].pos.1); // update the nearest position
    }

    if i_depth % 2 == 0 { // division in x direction
        inside_square(pos_near, pos_in, rad, nodes, nodes[idx_node].idx_node_right,
                      nalgebra::Vector2::<Real>::new(pos.x, min.y), max, i_depth + 1);
        inside_square(pos_near, pos_in, rad, nodes, nodes[idx_node].idx_node_left,
                      min, nalgebra::Vector2::<Real>::new(pos.x, max.y), i_depth + 1);
    } else { // division in y-direction
        inside_square(pos_near, pos_in, rad, nodes, nodes[idx_node].idx_node_left, min,
                      nalgebra::Vector2::<Real>::new(max.x, pos.y), i_depth + 1);
        inside_square(pos_near, pos_in, rad, nodes, nodes[idx_node].idx_node_right,
                      nalgebra::Vector2::<Real>::new(min.x, pos.y), max, i_depth + 1);
    }
}

pub struct KdTree2<Real> {
    min: nalgebra::Vector2::<Real>,
    max: nalgebra::Vector2::<Real>,
    nodes: Vec<Node<Real>>,
}

impl<Real> KdTree2<Real>
    where Real: num_traits::Float + nalgebra::RealField,
          f64: AsPrimitive<Real>
{
    pub fn from_matrix(points_: &nalgebra::Matrix2xX::<Real>) -> Self {
        let mut ps = points_
            .column_iter().enumerate()
            .map(|v| (v.1.into_owned(), v.0))
            .collect();
        let mut nodes = Vec::<Node<Real>>::new();
        nodes.resize(1, Node::new());
        construct_kdtree(&mut nodes, 0,
                         &mut ps, 0, points_.shape().1,
                         0);
        use num_traits::Float;
        let min_x = points_.column_iter().fold(Real::nan(), |m, v| Float::min(v.x, m));
        let max_x = points_.column_iter().fold(Real::nan(), |m, v| Float::max(v.x, m));
        let min_y = points_.column_iter().fold(Real::nan(), |m, v| Float::min(v.y, m));
        let max_y = points_.column_iter().fold(Real::nan(), |m, v| Float::max(v.y, m));
        KdTree2 {
            min: nalgebra::Vector2::<Real>::new(min_x, min_y),
            max: nalgebra::Vector2::<Real>::new(max_x, max_y),
            nodes,
        }
    }

    pub fn from_vec(points_: &Vec<nalgebra::Vector2::<Real>>) -> Self {
        let mut nodes = Vec::<Node<Real>>::new();
        let mut ps = points_.iter().enumerate().map(|v| (*v.1, v.0)).collect();
        nodes.resize(1, Node::new());
        construct_kdtree(&mut nodes, 0,
                         &mut ps, 0, points_.len(),
                         0);
        use num_traits::Float;
        let min_x = points_.iter().fold(Real::nan(), |m, v| Float::min(v.x, m));
        let max_x = points_.iter().fold(Real::nan(), |m, v| Float::max(v.x, m));
        let min_y = points_.iter().fold(Real::nan(), |m, v| Float::min(v.y, m));
        let max_y = points_.iter().fold(Real::nan(), |m, v| Float::max(v.y, m));
        KdTree2 {
            min: nalgebra::Vector2::<Real>::new(min_x, min_y),
            max: nalgebra::Vector2::<Real>::new(max_x, max_y),
            nodes,
        }
    }

    pub fn edges(&self) -> Vec<Real> {
        let mut xys = Vec::<Real>::new();
        find_edges(
            &mut xys,
            &self.nodes,
            0,
            self.min, self.max,
            0);
        xys.push(self.min.x);
        xys.push(self.min.y);
        xys.push(self.max.x);
        xys.push(self.min.y);
        //
        xys.push(self.max.x);
        xys.push(self.min.y);
        xys.push(self.max.x);
        xys.push(self.max.y);
        //
        xys.push(self.max.x);
        xys.push(self.max.y);
        xys.push(self.min.x);
        xys.push(self.max.y);
        //
        xys.push(self.min.x);
        xys.push(self.max.y);
        xys.push(self.min.x);
        xys.push(self.min.y);
        xys
    }

    pub fn near(&self, pos_in: nalgebra::Vector2::<Real>) -> usize {
        let vmax = <Real as nalgebra::RealField>::max_value().unwrap();
        let mut pos_near = (nalgebra::Vector2::<Real>::new(vmax, vmax), usize::MAX);
        nearest(&mut pos_near, pos_in, &self.nodes, 0,
                self.min, self.max, 0);
        pos_near.1
    }

    pub fn inside_square(&self, pos_in: nalgebra::Vector2::<Real>, rad: Real) -> Vec<usize>{
        let mut idxs0 = Vec::<usize>::new();
        inside_square(&mut idxs0, pos_in, rad, &self.nodes, 0,
                      self.min, self.max, 0);
        idxs0
    }
}

#[cfg(test)]
mod tests {
    use crate::kdtree2::{KdTree2, Node};
    use num_traits::AsPrimitive;
    use rand::distributions::Standard;
    use rand::Rng;

    fn test_data<Real>(num_xy: usize) -> (Vec<nalgebra::Vector2<Real>>, Vec<Node<Real>>)
        where Real: nalgebra::RealField + 'static + Copy,
              f64: AsPrimitive<Real>,
              Standard: rand::prelude::Distribution<Real>
    {
        let xys = {
            let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed([13_u8; 32]);
            let rad: Real = 0.4_f64.as_();
            let half: Real = 0.4_f64.as_();
            let mut ps = Vec::<nalgebra::Vector2::<Real>>::new();
            for _i in 0..num_xy {
                let x: Real = (rng.gen::<Real>() * 2_f64.as_() - Real::one()) * rad + half;
                let y: Real = (rng.gen::<Real>() * 2_f64.as_() - Real::one()) * rad + half;
                ps.push(nalgebra::Vector2::<Real>::new(x, y));
            }
            ps
        };
        let nodes = {
            let mut nodes = Vec::<crate::kdtree2::Node<Real>>::new();
            let mut ps = xys.iter().enumerate().map(|v| (*v.1, v.0)).collect();
            nodes.resize(1, crate::kdtree2::Node::new());
            crate::kdtree2::construct_kdtree(
                &mut nodes, 0,
                &mut ps, 0, xys.len(),
                0);
            nodes
        };
        (xys, nodes)
    }

    #[test]
    fn check_nearest_raw() {
        use crate::kdtree2::nearest;
        use std::time;
        type Real = f64;
        type Vector = nalgebra::Vector2::<Real>;
        let (xys, nodes) = test_data(10000);
        let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed([13_u8; 32]);
        let time_nearest = time::Instant::now();
        for _ in 0..10000 {
            let p0 = Vector::new(rng.gen::<Real>(), rng.gen::<Real>());
            let mut pos_near = (Vector::new(Real::MAX, Real::MAX), usize::MAX);
            nearest(&mut pos_near, p0, &nodes, 0,
                    Vector::new(0., 0.), Vector::new(1., 1.),
                    0);
        }
        dbg!(time_nearest.elapsed());
        for _ in 0..10000 {
            let p0 = Vector::new(rng.gen::<Real>(), rng.gen::<Real>());
            let mut pos_near = (Vector::new(Real::MAX, Real::MAX), usize::MAX);
            nearest(&mut pos_near, p0, &nodes, 0,
                    Vector::new(0., 0.), Vector::new(1., 1.),
                    0);
            let dist_min = (xys[pos_near.1] - p0).norm();
            for i in 0..xys.len() {
                assert!((xys[i] - p0).norm() >= dist_min);
            }
        }
    }

    #[test]
    fn check_inside_square_raw() {
        use std::time;
        use crate::kdtree2::inside_square;
        type Real = f64;
        type Vector = nalgebra::Vector2::<Real>;
        let (xys, nodes) = test_data(10000);
        let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed([13_u8; 32]);
        let rad: Real = 0.03;
        let time_inside_square = time::Instant::now();
        for _ in 0..10000 {
            let p0 = Vector::new(rng.gen::<Real>(), rng.gen::<Real>());
            let mut pos_near = Vec::<usize>::new();
            inside_square(&mut pos_near, p0, rad, &nodes, 0,
                          Vector::new(0., 0.), Vector::new(1., 1.),
                          0);
        }
        dbg!(time_inside_square.elapsed());
        //
        for _ in 0..10000 {
            let p0 = Vector::new(rng.gen::<Real>(), rng.gen::<Real>());
            let mut idxs0 = Vec::<usize>::new();
            inside_square(&mut idxs0, p0, rad, &nodes, 0,
                          Vector::new(0., 0.), Vector::new(1., 1.),
                          0);
            let idxs1: Vec<usize> = xys.iter().enumerate()
                .filter(|&v| (v.1.x - p0.x).abs() < rad && (v.1.y - p0.y).abs() < rad)
                .map(|v| v.0)
                .collect();
            let idxs1 = std::collections::BTreeSet::from_iter(idxs1.iter());
            let idxs0 = std::collections::BTreeSet::from_iter(idxs0.iter());
            assert_eq!(idxs1, idxs0);
        }
    }

    #[test]
    fn check_nearest_matrix() {
        type Real = f64;
        type Vector = nalgebra::Vector2::<Real>;
        let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed([13_u8; 32]);
        let xys = {
            let mut xys = nalgebra::Matrix2xX::<Real>::zeros(10000);
            xys.iter_mut().for_each(|v| *v = rng.gen::<Real>());
            xys
        };
        let kdtree2 = KdTree2::from_matrix(&xys);
        for _ in 0..10000 {
            let p0 = Vector::new(rng.gen::<Real>(), rng.gen::<Real>());
            let idx = kdtree2.near(p0);
            let dist_min = (xys.column(idx) - p0).norm();
            for col in xys.column_iter() {
                assert!((col - p0).norm() >= dist_min);
            }
        }
    }

    #[test]
    fn check_inside_square_matrix() {
        type Real = f64;
        type Vector = nalgebra::Vector2::<Real>;
        let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed([13_u8; 32]);
        let xys = {
            let mut xys = nalgebra::Matrix2xX::<Real>::zeros(10000);
            xys.iter_mut().for_each(|v| *v = rng.gen::<Real>());
            xys
        };
        let kdtree2 = KdTree2::from_matrix(&xys);
        let rad = 0.01;
        for _ in 0..10000 {
            let p0 = Vector::new(rng.gen::<Real>(), rng.gen::<Real>());
            let idxs0 = kdtree2.inside_square(p0,rad);
            let idxs1: Vec<usize> = xys.column_iter().enumerate()
                .filter(|&v| (v.1.x - p0.x).abs() < rad && (v.1.y - p0.y).abs() < rad)
                .map(|v| v.0)
                .collect();
            let idxs1 = std::collections::BTreeSet::from_iter(idxs1.iter());
            let idxs0 = std::collections::BTreeSet::from_iter(idxs0.iter());
            assert_eq!(idxs1, idxs0);
        }
    }
}