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
/*
Copyright 2016 Martin Buck

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall
be included all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

//! KdTree https://en.wikipedia.org/wiki/K-d_tree

use std::cmp::Ordering;

use crate::{
    distances_3d::*,
    functions::{dimension_compare, dimension_dist},
    prelude::*,
};

#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// KdTree https://en.wikipedia.org/wiki/K-d_tree
pub struct KdTree<P>
where
    P: Is3D,
{
    root: Option<KdNode<P>>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct KdNode<P>
where
    P: Is3D,
{
    pub left: Option<Box<KdNode<P>>>,
    pub right: Option<Box<KdNode<P>>>,
    pub val: P,
    pub dimension: i8,
}

impl<P> IsTree3D<P> for KdTree<P>
where
    P: Is3D + Clone,
{
    fn size(&self) -> usize {
        match self.root {
            None => 0,
            Some(ref node) => node.size(),
        }
    }

    fn to_pointcloud(&self) -> PointCloud3D<P> {
        let mut result = PointCloud3D::new();
        if let Some(ref node) = self.root {
            node.to_pointcloud_3d(&mut result);
        }
        result
    }

    fn build(&mut self, pc: PointCloud3D<P>) -> Result<()> {
        match pc.len() {
            0 => Err(ErrorKind::TooFewPoints),
            _ => {
                self.root = Some(KdNode::new(0, pc.data));
                Ok(())
            }
        }
    }
}

impl<PSearch, PFind> IsKNearestSearchable<PSearch, PFind> for KdTree<PFind>
where
    PSearch: Is3D,
    PFind: Is3D + Clone,
{
    fn knearest(&self, search: &PSearch, n: usize) -> Vec<PFind> {
        let mut result = Vec::new();
        if n < 1 {
            return result;
        }
        if let Some(ref node) = self.root {
            node.knearest(search, n, &mut result);
        }
        return result;
    }

    fn nearest(&self, search: &PSearch) -> Result<PFind> {
        //@todo implemented on its own, since the code can be faster without vecs
        let result = self.knearest(search, 1);
        match result.len() {
            0 => Err(ErrorKind::TooFewPoints),
            _ => {
                let p = result[0].clone();
                Ok(p)
            }
        }
    }
}

impl<P> IsSphereSearchable<P> for KdTree<P>
where
    P: Is3D + Clone,
{
    fn in_sphere(&self, sphere: &Sphere) -> Vec<P> {
        let mut result = Vec::new();
        if let Some(ref node) = self.root {
            node.in_sphere(sphere, &mut result);
        }
        return result;
    }
}

impl<P> IsBox3DSearchable<P> for KdTree<P>
where
    P: Is3D + Clone,
{
    fn in_box(&self, box_3d: &Box3D) -> Vec<P> {
        let mut result = Vec::new();
        if let Some(ref node) = self.root {
            node.in_box(box_3d, &mut result);
        }
        return result;
    }
}

impl<P> KdNode<P>
where
    P: Is3D,
{
    pub fn size(&self) -> usize {
        let mut result: usize = 0;
        if let Some(ref n) = (&self).left {
            result += n.size();
        }
        result += 1;
        if let Some(ref n) = (&self).right {
            result += n.size();
        }
        result
    }

    fn is_leaf(&self) -> bool {
        self.left.is_none() && self.right.is_none()
    }
}

impl<P> KdNode<P>
where
    P: Is3D + Clone,
{
    pub fn to_pointcloud_3d(&self, pc: &mut PointCloud3D<P>) {
        if let Some(ref n) = (&self).left {
            n.to_pointcloud_3d(pc);
        }
        pc.push(self.val.clone());
        if let Some(ref n) = (&self).right {
            n.to_pointcloud_3d(pc);
        }
    }

    pub fn new(dim: i8, mut pc: Vec<P>) -> KdNode<P> {
        let dimension = dim % 2;
        if pc.len() == 1 {
            return KdNode {
                left: None,
                right: None,
                val: pc[0].clone(),
                dimension: dimension,
            };
        }

        pc.sort_by(|a, b| match dimension {
            0 => a.x().partial_cmp(&b.x()).unwrap_or(Ordering::Equal),
            1 => a.y().partial_cmp(&b.y()).unwrap_or(Ordering::Equal),
            2 => a.z().partial_cmp(&b.z()).unwrap_or(Ordering::Equal),
            _ => Ordering::Equal,
        });
        let median = pc.len() / 2;
        let mut pc_left = Vec::new();
        let mut pc_right = Vec::new();

        let val = pc[median].clone();

        for (i, p) in pc.into_iter().enumerate() {
            if i < median {
                pc_left.push(p);
            } else if i > median {
                pc_right.push(p);
            }
        }

        let left = match pc_left.len() {
            0 => None,
            _ => Some(Box::new(KdNode::new(dimension + 1, pc_left))),
        };

        let right = match pc_right.len() {
            0 => None,
            _ => Some(Box::new(KdNode::new(dimension + 1, pc_right))),
        };

        KdNode {
            left,
            right,
            val,
            dimension,
        }
    }
}

impl<P> KdNode<P>
where
    P: Is3D + Clone,
{
    pub fn knearest<PSearch>(&self, search: &PSearch, n: usize, pc: &mut Vec<P>)
    where
        PSearch: Is3D,
    {
        if pc.len() < n || sqr_dist_3d(search, &self.val) < sqr_dist_3d(search, &pc[&pc.len() - 1])
        {
            pc.push(self.val.clone());
        }

        let comp = dimension_compare(search, &self.val, self.dimension);

        match comp {
            Ok(res) => match res {
                Ordering::Less => {
                    if let Some(ref node) = (&self).left {
                        node.knearest(search, n, pc);
                    }
                }
                _ => {
                    if let Some(ref node) = (&self).right {
                        node.knearest(search, n, pc);
                    }
                }
            },
            Err(_) => {}
        }

        Self::sort_and_limit(pc, search, n);

        let (current_search, current_val) = match self.dimension {
            0 => (search.x(), self.val.x()),
            1 => (search.y(), self.val.y()),
            _ => (search.z(), self.val.z()),
        };

        let distance_best = dist_3d(search, &pc[&pc.len() - 1]);
        let border_left = current_search - distance_best;
        let border_right = current_search + distance_best;

        match comp {
            Ok(res) => match res {
                Ordering::Less => {
                    if let Some(ref node) = (&self).right {
                        if pc.len() < n || border_right >= current_val {
                            node.knearest(search, n, pc);
                        }
                    }
                }
                _ => {
                    if let Some(ref node) = (&self).left {
                        if pc.len() < n || border_left <= current_val {
                            node.knearest(search, n, pc);
                        }
                    }
                }
            },
            Err(_) => {}
        }

        Self::sort_and_limit(pc, search, n);
    }

    pub fn in_sphere(&self, sphere: &Sphere, pc: &mut Vec<P>) {
        if dist_3d(&sphere.center, &self.val) <= sphere.radius.get() {
            pc.push(self.val.clone());
        }

        if self.is_leaf() {
            return;
        }

        let comp = dimension_compare(&sphere.center, &self.val, self.dimension);

        match comp {
            Ok(res) => match res {
                Ordering::Less => {
                    if let Some(ref node) = (&self).left {
                        node.in_sphere(sphere, pc);
                    }
                }
                _ => {
                    if let Some(ref node) = (&self).right {
                        node.in_sphere(sphere, pc);
                    }
                }
            },
            Err(_) => {}
        }

        let (current_search, current_val) = match self.dimension {
            0 => (sphere.x(), self.val.x()),
            1 => (sphere.y(), self.val.y()),
            _ => (sphere.z(), self.val.z()),
        };

        let border_left = current_search - sphere.radius.get();
        let border_right = current_search + sphere.radius.get();

        match comp {
            Ok(res) => match res {
                Ordering::Less => {
                    if let Some(ref node) = (&self).right {
                        if border_right >= current_val {
                            node.in_sphere(sphere, pc);
                        }
                    }
                }
                _ => {
                    if let Some(ref node) = (&self).left {
                        if border_left <= current_val {
                            node.in_sphere(sphere, pc);
                        }
                    }
                }
            },
            Err(_) => {}
        }
    }

    pub fn in_box(&self, box_3d: &Box3D, pc: &mut Vec<P>) {
        if let (Ok(dist_x), Ok(dist_y), Ok(dist_z)) = (
            dimension_dist(&box_3d.center, &self.val, 0),
            dimension_dist(&box_3d.center, &self.val, 1),
            dimension_dist(&box_3d.center, &self.val, 2),
        ) {
            if dist_x <= 0.5 * box_3d.size_x.get()
                && dist_y <= 0.5 * box_3d.size_y.get()
                && dist_z <= 0.5 * box_3d.size_z.get()
            {
                pc.push(self.val.clone());
            }

            if self.is_leaf() {
                return;
            }

            let comp = dimension_compare(&box_3d.center, &self.val, self.dimension);

            match comp {
                Ok(res) => match res {
                    Ordering::Less => {
                        if let Some(ref node) = (&self).left {
                            node.in_box(box_3d, pc);
                        }
                    }
                    _ => {
                        if let Some(ref node) = (&self).right {
                            node.in_box(box_3d, pc);
                        }
                    }
                },
                Err(_) => {}
            }

            let (current_search, current_val, ref current_size) = match self.dimension {
                0 => (box_3d.x(), self.val.x(), &box_3d.size_x),
                1 => (box_3d.y(), self.val.y(), &box_3d.size_y),
                _ => (box_3d.z(), self.val.z(), &box_3d.size_z),
            };

            let border_left = current_search - 0.5 * current_size.get();
            let border_right = current_search + 0.5 * current_size.get();

            match comp {
                Ok(res) => match res {
                    Ordering::Less => {
                        if let Some(ref node) = (&self).right {
                            if border_right >= current_val {
                                node.in_box(box_3d, pc);
                            }
                        }
                    }
                    _ => {
                        if let Some(ref node) = (&self).left {
                            if border_left <= current_val {
                                node.in_box(box_3d, pc);
                            }
                        }
                    }
                },
                Err(_) => {}
            }
        }
    }

    fn sort_and_limit<'a, PSearch, PFind>(pc: &'a mut Vec<PFind>, search: &PSearch, max_size: usize)
    where
        PSearch: Is3D,
        PFind: Is3D + Clone,
    {
        if pc.len() > max_size {
            pc.sort_by(|a, b| {
                sqr_dist_3d(search, a)
                    .partial_cmp(&sqr_dist_3d(search, b))
                    .unwrap_or(Ordering::Equal)
            });
            let mut result: Vec<PFind>;
            result = Vec::new();
            for i in pc.iter().take(max_size) {
                result.push(i.clone());
            }
            *pc = result;
        }
    }
}