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
/*  SPDX-License-Identifier:LGPL-2.0-only
 *  Rust code Copyright (c) 2023 lacklustr@protonmail.com https://github.com/eadf
 *
 *  This file is ported from code inside of OpenCAMlib:
 *  Copyright (c) 2010-2011 Anders Wallin (anders.e.e.wallin "at" gmail.com).
 *  (see https://github.com/aewallin/opencamlib).
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 2.1 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

use super::*;
use std::fmt;
#[cfg(feature = "glam")]
use glam::{DVec2, Vec2};

#[cfg(feature = "glam")]
impl PointTrait for Vec2 {
    type PScalar = f32;
    #[inline(always)]
    fn x(&self) -> Self::PScalar {
        self.x
    }
    #[inline(always)]
    fn y(&self) -> Self::PScalar {
        self.y
    }
    #[inline(always)]
    fn set_x(&mut self, x: Self::PScalar) {
        self.x = x;
    }
    #[inline(always)]
    fn set_y(&mut self, y: Self::PScalar) {
        self.y = y;
    }
    #[inline(always)]
    fn at(&self, index: u8) -> Self::PScalar {
        match index {
            0 => self.x,
            1 => self.y,
            _ => unreachable!(),
        }
    }
    #[inline(always)]
    fn at_mut(&mut self, index: u8) -> &mut Self::PScalar {
        match index {
            0 => &mut self.x,
            1 => &mut self.y,
            _ => unreachable!(),
        }
    }

    const DIMENSION: u8 = 2;
}

#[cfg(feature = "glam")]
impl PointTrait for DVec2 {
    type PScalar = f64;
    #[inline(always)]
    fn x(&self) -> Self::PScalar {
        self.x
    }
    #[inline(always)]
    fn y(&self) -> Self::PScalar {
        self.y
    }
    #[inline(always)]
    fn set_x(&mut self, x: Self::PScalar) {
        self.x = x;
    }
    #[inline(always)]
    fn set_y(&mut self, y: Self::PScalar) {
        self.y = y;
    }

    #[inline(always)]
    fn at(&self, index: u8) -> Self::PScalar {
        match index {
            0 => self.x,
            1 => self.y,
            _ => unreachable!(),
        }
    }
    #[inline(always)]
    fn at_mut(&mut self, index: u8) -> &mut Self::PScalar {
        match index {
            0 => &mut self.x,
            1 => &mut self.y,
            _ => unreachable!(),
        }
    }

    const DIMENSION: u8 = 2;
}

impl<P: PointTrait> Default for KDTree<P> {
    fn default() -> Self {
        Self {
            root: None,
            rect: None,
        }
    }
}

impl<P: PointTrait> Debug for KDTree<P> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(ref root_node) = self.root {
            writeln!(f, "KDTree(")?;
            root_node.format_node(f, 0)?;
            writeln!(f, ")")
        } else {
            writeln!(f, "KDTree()")
        }
    }
}