krakel/
impls.rs

1/*  SPDX-License-Identifier:LGPL-2.1-only
2 *  Rust code Copyright (c) 2023 lacklustr@protonmail.com https://github.com/eadf
3 *
4 *  This file is ported from code inside of OpenCAMlib:
5 *  Copyright (c) 2010-2011 Anders Wallin (anders.e.e.wallin "at" gmail.com).
6 *  (see https://github.com/aewallin/opencamlib).
7 *
8 *  This program is free software: you can redistribute it and/or modify
9 *  it under the terms of the GNU Lesser General Public License as published by
10 *  the Free Software Foundation, either version 2.1 of the License, or
11 *  (at your option) any later version.
12 *
13 *  This program is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *  GNU Lesser General Public License for more details.
17 *
18 *  You should have received a copy of the GNU Lesser General Public License
19 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22use super::*;
23use std::fmt;
24
25#[cfg(feature = "cgmath")]
26#[cfg(feature = "vector-traits")]
27use vector_traits::cgmath;
28
29#[cfg(feature = "glam")]
30#[cfg(feature = "vector-traits")]
31use vector_traits::{
32    glam::{DVec2, Vec2},
33    prelude::glam_ext::Vec2A,
34};
35
36#[cfg(feature = "glam")]
37#[cfg(feature = "vector-traits")]
38impl PointTrait for Vec2 {
39    type PScalar = f32;
40    #[inline(always)]
41    fn x(&self) -> Self::PScalar {
42        self.x
43    }
44    #[inline(always)]
45    fn y(&self) -> Self::PScalar {
46        self.y
47    }
48    #[inline(always)]
49    fn set_x(&mut self, x: Self::PScalar) {
50        self.x = x;
51    }
52    #[inline(always)]
53    fn set_y(&mut self, y: Self::PScalar) {
54        self.y = y;
55    }
56    #[inline(always)]
57    fn at(&self, index: u8) -> Self::PScalar {
58        match index {
59            0 => self.x,
60            1 => self.y,
61            _ => unreachable!(),
62        }
63    }
64    #[inline(always)]
65    fn at_mut(&mut self, index: u8) -> &mut Self::PScalar {
66        match index {
67            0 => &mut self.x,
68            1 => &mut self.y,
69            _ => unreachable!(),
70        }
71    }
72
73    const DIMENSION: u8 = 2;
74}
75
76#[cfg(feature = "glam")]
77#[cfg(feature = "vector-traits")]
78impl PointTrait for Vec2A {
79    type PScalar = f32;
80    #[inline(always)]
81    fn x(&self) -> Self::PScalar {
82        self.0.x
83    }
84    #[inline(always)]
85    fn y(&self) -> Self::PScalar {
86        self.0.y
87    }
88    #[inline(always)]
89    fn set_x(&mut self, x: Self::PScalar) {
90        self.0.x = x;
91    }
92    #[inline(always)]
93    fn set_y(&mut self, y: Self::PScalar) {
94        self.0.y = y;
95    }
96    #[inline(always)]
97    fn at(&self, index: u8) -> Self::PScalar {
98        match index {
99            0 => self.0.x,
100            1 => self.0.y,
101            _ => unreachable!(),
102        }
103    }
104    #[inline(always)]
105    fn at_mut(&mut self, index: u8) -> &mut Self::PScalar {
106        match index {
107            0 => &mut self.0.x,
108            1 => &mut self.0.y,
109            _ => unreachable!(),
110        }
111    }
112
113    const DIMENSION: u8 = 2;
114}
115
116#[cfg(feature = "cgmath")]
117#[cfg(feature = "vector-traits")]
118impl PointTrait for cgmath::Vector2<f32> {
119    type PScalar = f32;
120    #[inline(always)]
121    fn x(&self) -> Self::PScalar {
122        self.x
123    }
124    #[inline(always)]
125    fn y(&self) -> Self::PScalar {
126        self.y
127    }
128    #[inline(always)]
129    fn set_x(&mut self, x: Self::PScalar) {
130        self.x = x;
131    }
132    #[inline(always)]
133    fn set_y(&mut self, y: Self::PScalar) {
134        self.y = y;
135    }
136    #[inline(always)]
137    fn at(&self, index: u8) -> Self::PScalar {
138        match index {
139            0 => self.x,
140            1 => self.y,
141            _ => unreachable!(),
142        }
143    }
144    #[inline(always)]
145    fn at_mut(&mut self, index: u8) -> &mut Self::PScalar {
146        match index {
147            0 => &mut self.x,
148            1 => &mut self.y,
149            _ => unreachable!(),
150        }
151    }
152
153    const DIMENSION: u8 = 2;
154}
155
156#[cfg(feature = "cgmath")]
157#[cfg(feature = "vector-traits")]
158impl PointTrait for cgmath::Vector2<f64> {
159    type PScalar = f64;
160    #[inline(always)]
161    fn x(&self) -> Self::PScalar {
162        self.x
163    }
164    #[inline(always)]
165    fn y(&self) -> Self::PScalar {
166        self.y
167    }
168    #[inline(always)]
169    fn set_x(&mut self, x: Self::PScalar) {
170        self.x = x;
171    }
172    #[inline(always)]
173    fn set_y(&mut self, y: Self::PScalar) {
174        self.y = y;
175    }
176    #[inline(always)]
177    fn at(&self, index: u8) -> Self::PScalar {
178        match index {
179            0 => self.x,
180            1 => self.y,
181            _ => unreachable!(),
182        }
183    }
184    #[inline(always)]
185    fn at_mut(&mut self, index: u8) -> &mut Self::PScalar {
186        match index {
187            0 => &mut self.x,
188            1 => &mut self.y,
189            _ => unreachable!(),
190        }
191    }
192
193    const DIMENSION: u8 = 2;
194}
195
196#[cfg(feature = "glam")]
197#[cfg(feature = "vector-traits")]
198impl PointTrait for DVec2 {
199    type PScalar = f64;
200    #[inline(always)]
201    fn x(&self) -> Self::PScalar {
202        self.x
203    }
204    #[inline(always)]
205    fn y(&self) -> Self::PScalar {
206        self.y
207    }
208    #[inline(always)]
209    fn set_x(&mut self, x: Self::PScalar) {
210        self.x = x;
211    }
212    #[inline(always)]
213    fn set_y(&mut self, y: Self::PScalar) {
214        self.y = y;
215    }
216
217    #[inline(always)]
218    fn at(&self, index: u8) -> Self::PScalar {
219        match index {
220            0 => self.x,
221            1 => self.y,
222            _ => unreachable!(),
223        }
224    }
225    #[inline(always)]
226    fn at_mut(&mut self, index: u8) -> &mut Self::PScalar {
227        match index {
228            0 => &mut self.x,
229            1 => &mut self.y,
230            _ => unreachable!(),
231        }
232    }
233
234    const DIMENSION: u8 = 2;
235}
236
237impl<P: PointTrait> Default for KDTree<P> {
238    fn default() -> Self {
239        Self {
240            root: None,
241            rect: None,
242        }
243    }
244}
245
246impl<P: PointTrait> Debug for KDTree<P> {
247    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
248        if let Some(ref root_node) = self.root {
249            writeln!(f, "KDTree(")?;
250            root_node.format_node(f, 0)?;
251            writeln!(f, ")")
252        } else {
253            write!(f, "KDTree()")
254        }
255    }
256}