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
use crate::delaunay::{DelaunayTriangle, DelaunayTriangleHandle, TriangleId};
use crate::ocl::prm::Float2;

use std::fmt;
use std::hash;

use arena_system::{Arena, Handle, Index, RawHandle};
use smallvec::SmallVec;

pub type PointId = i64;

/// A (x; y) coordinate with additional information.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Point {
    coords: Float2,

    is_bounding: bool,
    previous_in_outline: PointId,

    triangle_fan: SmallVec<[TriangleId; 6]>,
}

impl Point {
    /// Creates a new [`Point`].
    pub fn new(x: f32, y: f32) -> Self {
        Self {
            coords: Float2::new(x, y),

            is_bounding: false,
            previous_in_outline: -1,

            triangle_fan: SmallVec::new(),
        }
    }

    /// Creates a new [`Point`] with the information if it is bounding or not.
    pub fn with_is_bounding(x: f32, y: f32, is_bounding: bool) -> Self {
        Self {
            coords: Float2::new(x, y),

            is_bounding,
            previous_in_outline: -1,

            triangle_fan: SmallVec::new(),
        }
    }

    /// Creates a new [`Point`] with the information about the previous one in the outline.
    pub fn with_previous(x: f32, y: f32, previous_in_outline: PointId) -> Self {
        Self {
            coords: Float2::new(x, y),

            is_bounding: false,
            previous_in_outline,

            triangle_fan: SmallVec::new(),
        }
    }

    /// Creates a new [`Point`] with the information about the previous one in the outline
    /// and if it is bounding or not.
    pub fn with_is_bounding_and_previous(
        x: f32,
        y: f32,
        is_bounding: bool,
        previous_in_outline: PointId,
    ) -> Self {
        Self {
            coords: Float2::new(x, y),

            is_bounding,
            previous_in_outline,

            triangle_fan: SmallVec::new(),
        }
    }

    /// Returns the `x` coordinate of the point.
    pub fn x(&self) -> f32 {
        self.coords[0]
    }

    /// Returns the `y` coordinate of the point.
    pub fn y(&self) -> f32 {
        self.coords[1]
    }

    pub fn coords(&self) -> Float2 {
        self.coords
    }

    /// Returns coordinates of the point.
    pub fn set_coords(&mut self, coords: Float2) {
        self.coords = coords;
    }

    /// Checks if the point is bounding.
    pub fn is_bounding(&self) -> bool {
        self.is_bounding
    }

    /// Returns the previous point in the outline.
    pub fn previous_in_outline(&self) -> PointId {
        self.previous_in_outline
    }

    /// Sets the previous point in the outline.
    pub fn set_previous_in_outline(&mut self, previous_in_outline: PointId) {
        self.previous_in_outline = previous_in_outline;
    }

    /// Returns the triangle fan of the point.
    pub fn triangle_fan(&self) -> &SmallVec<[TriangleId; 6]> {
        &self.triangle_fan
    }

    /// Sets the triangle fan of the point.
    pub fn set_triangle_fan(&mut self, triangle_fan: SmallVec<[TriangleId; 6]>) {
        self.triangle_fan = triangle_fan;
    }

    /// Returns a point between two points.
    pub fn midpoint(&self, other: &Point) -> Point {
        Point::new((self.x() + other.x()) / 2.0, (self.y() + other.y()) / 2.0)
    }

    /// Returns the squared distance between two points.
    pub fn distance_squared(&self, other: &Point) -> f32 {
        let p = Point::new(self.x() - other.x(), self.y() - other.y());

        p.x().powi(2) + p.y().powi(2)
    }

    /// Returns the distance between two points.
    pub fn distance(&self, other: &Point) -> f32 {
        self.distance_squared(other).sqrt()
    }
}

/// A handle of the [`Point`] which is used by [`Arena`].
#[derive(Clone, Copy)]
pub struct PointHandle<'arena> {
    raw: RawHandle<'arena, Point>,
    triangles: Option<&'arena Arena<DelaunayTriangle>>,
}

impl<'arena> PointHandle<'arena> {
    /// Returns the `x` coordinate of the point.
    pub fn x(&self) -> f32 {
        self.get().unwrap().coords[0]
    }

    /// Returns the `y` coordinate of the point.
    pub fn y(&self) -> f32 {
        self.get().unwrap().coords[1]
    }

    /// Returns coordinates of the point.
    pub fn coords(&self) -> Float2 {
        self.get().unwrap().coords
    }

    /// Sets the coordinates of the point.
    pub fn set_coords(&mut self, coords: Float2) {
        self.get_mut().unwrap().coords = coords;
    }

    /// Checks if the point is bounding.
    pub fn is_bounding(&self) -> bool {
        self.get().unwrap().is_bounding
    }

    /// Returns the previous point in the outline.
    pub fn previous_in_outline(&self) -> PointHandle<'arena> {
        let this = self.get().expect("Can't get the point");

        self.arena().handle(this.previous_in_outline().into(), self.triangles)
    }

    /// Returns the triangle fan of the point.
    pub fn triangle_fan(&self) -> SmallVec<[DelaunayTriangleHandle<'arena>; 6]> {
        self.get()
            .unwrap()
            .triangle_fan
            .iter()
            .copied()
            .map(|i| i.into())
            .map(|i| self.triangles.unwrap().handle(i, self.arena()))
            .collect()
    }

    /// Sets the triangle fan of the point.
    pub fn set_triangle_fan(
        &mut self,
        triangle_fan: SmallVec<[DelaunayTriangleHandle<'arena>; 6]>,
    ) {
        self.get_mut().unwrap().triangle_fan =
            triangle_fan.into_iter().map(|h| h.index().into()).collect();
    }

    /// Adds `triangle` to the triangle fan of the point.
    pub fn add_triangle_to_fan(&self, triangle: DelaunayTriangleHandle) {
        let triangle_fan = &mut self.get_mut().unwrap().triangle_fan;

        triangle_fan.push(triangle.index().into());
    }

    /// Removes `triangle` from the triangle fan of the point.
    pub fn remove_triangle_from_fan(&self, triangle_index: Index) {
        let triangle_fan = &mut self.get_mut().unwrap().triangle_fan;
        let position = triangle_fan.iter().position(|t| *t == triangle_index.into());

        if let Some(position) = position {
            triangle_fan.remove(position);
        }
    }

    /// Checks if the point is connected to the other one.
    pub fn is_connected_to(&self, other: PointHandle<'arena>) -> bool {
        self.triangle_fan()
            .into_iter()
            .any(|t| t.points().contains(&other))
            && *self != other
    }

    /// Calculates skew product of `self` and `other` points
    /// with the origin of coordinates at `origin`
    pub fn skew_product(&self, origin: &Self, other: &Self) -> f32 {
        let a = Point::new(self.x() - origin.x(), self.y() - origin.y());
        let b = Point::new(other.x() - origin.x(), other.y() - origin.y());

        a.x() * b.y() - a.y() * b.x()
    }

    /// Returns the distance between two points.
    pub fn distance(&self, other: &PointHandle) -> f32 {
        let this = self.get().expect("Can't get the point");
        let other = other.get().expect("Can't get the other point");

        this.distance(&other)
    }
}

impl<'arena> Handle<'arena> for PointHandle<'arena> {
    type Type = Point;
    type Userdata = Option<&'arena Arena<DelaunayTriangle>>;

    fn from_raw(raw: RawHandle<'arena, Self::Type>, userdata: Self::Userdata) -> Self {
        Self { raw, triangles: userdata }
    }

    fn to_raw(&self) -> RawHandle<'arena, Self::Type> {
        self.raw
    }
}

impl fmt::Debug for PointHandle<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_fmt(format_args!("PointHandle({:?})", self.to_raw()))
    }
}

impl PartialEq for PointHandle<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.to_raw() == other.to_raw()
    }
}
impl Eq for PointHandle<'_> {}

impl PartialOrd for PointHandle<'_> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.to_raw().partial_cmp(&other.to_raw())
    }
}

impl Ord for PointHandle<'_> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.to_raw().cmp(&other.to_raw())
    }
}

impl hash::Hash for PointHandle<'_> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        let index: i64 = self.index().into();
        index.hash(state);
    }
}