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
#[cfg(feature = "rayon")]
use rayon::prelude::*;

pub mod dcel;
pub mod geom;

pub use dcel::TrianglesDCEL;
pub use geom::{Point, Triangle};

const STACK_CAPACITY: usize = 512;

/// Option<usize>, where None is represented by -1
///
/// Takes 8 bytes instead of 16.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct OptionIndex(usize);

impl OptionIndex {
    /// Returns `Some(idx)` value
    #[inline]
    pub fn some(idx: usize) -> OptionIndex {
        debug_assert!(idx < std::usize::MAX);
        OptionIndex(idx)
    }

    /// Returns None value
    #[inline]
    pub fn none() -> OptionIndex {
        OptionIndex(std::usize::MAX)
    }

    /// Returns true if it is a `Some` value
    #[inline]
    pub fn is_some(self) -> bool {
        self != OptionIndex::none()
    }

    /// Returns true if it is a `None` value
    #[inline]
    pub fn is_none(self) -> bool {
        self == OptionIndex::none()
    }

    /// Returns the associated `Option` value
    #[inline]
    pub fn get(self) -> Option<usize> {
        if self.is_some() {
            Some(self.0)
        } else {
            None
        }
    }
}

/// Maps angle between `point` and `center` to index in the hash table
fn angular_hash(point: Point, center: Point, size: usize) -> usize {
    let angle = geom::pseudo_angle(point.x - center.x, point.y - center.y);
    (angle * size as f32) as usize % size
}

/// Counter-clockwise convex hull
struct Hull {
    /// Maps point index to next point index
    next: Vec<usize>,

    /// Maps point index to previous point index
    prev: Vec<usize>,

    /// Radial hash table
    hash_table: Vec<OptionIndex>,

    /// Boundary triangles
    triangles: Vec<OptionIndex>,

    /// Center point for calculating radial hash
    center: Point,

    /// Starting point index
    start: usize,
}

impl Hull {
    fn new(seed: [usize; 3], points: &[Point]) -> Hull {
        let capacity = points.len();
        let table_size = (capacity as f32).sqrt().ceil() as usize;

        let center = Triangle(points[seed[0]], points[seed[1]], points[seed[2]]).circumcenter();

        let mut hull = Hull {
            next: vec![0; capacity],
            prev: vec![0; capacity],
            hash_table: vec![OptionIndex::none(); table_size],
            triangles: vec![OptionIndex::none(); capacity],
            start: seed[0],
            center,
        };

        hull.next[seed[0]] = seed[1];
        hull.next[seed[1]] = seed[2];
        hull.next[seed[2]] = seed[0];

        hull.prev[seed[0]] = seed[2];
        hull.prev[seed[1]] = seed[0];
        hull.prev[seed[2]] = seed[1];

        hull.triangles[seed[0]] = OptionIndex::some(0);
        hull.triangles[seed[1]] = OptionIndex::some(1);
        hull.triangles[seed[2]] = OptionIndex::some(2);

        hull.add_hash(seed[0], points[seed[0]]);
        hull.add_hash(seed[1], points[seed[1]]);
        hull.add_hash(seed[2], points[seed[2]]);

        hull
    }

    /// Adds a new point in the hash table
    fn add_hash(&mut self, index: usize, point: Point) {
        let table_size = self.hash_table.len();
        self.hash_table[angular_hash(point, self.center, table_size)] = OptionIndex::some(index);
    }

    /// Returns the first convex hull edge visible from the point and a boolean
    /// indicating whether the previous edge may be visible too
    fn find_visible_edge(&self, point: Point, points: &[Point]) -> Option<(usize, bool)> {
        let table_size = self.hash_table.len();
        let hash = angular_hash(point, self.center, table_size);

        let mut start = OptionIndex::none();

        // basically linear probing hash table
        for i in 0..table_size {
            start = self.hash_table[(hash + i) % table_size];

            // if e == self.next[e] then it is an empty hash table entry; skip it
            if start.get().filter(|&e| e != self.next[e]).is_some() {
                break;
            }
        }

        // now `start` is a point near enough to the target
        // let's go forward to find a visible edge

        let start = self.prev[start.get()?];
        let mut edge = start;

        loop {
            let next = self.next[edge];
            let tri = Triangle(point, points[edge], points[next]);

            if tri.is_left_handed() {
                // edge is visible, breakin' outta hell
                break;
            }

            edge = next;
            if edge == start {
                // avoiding the endless loop
                return None;
            }
        }

        // if edge == start then we made 0 iterations, so we can't say for sure
        // that there are no visible edges preceding the start one

        Some((edge, edge == start))
    }
}

/// Calculates the median point (arithmetic mean of the coordinates)
fn find_center(points: &[Point]) -> Point {
    let (x_sum, y_sum) = points
        .iter()
        .fold((0.0, 0.0), |(x, y), point| (x + point.x, y + point.y));

    Point::new(x_sum / points.len() as f32, y_sum / points.len() as f32)
}

fn find_seed_triangle(points: &[Point]) -> Option<(Triangle, [usize; 3])> {
    let center = find_center(&points);

    #[cfg(feature = "rayon")]
    let iter = points.par_iter();

    #[cfg(not(feature = "rayon"))]
    let iter = points.iter();

    let (seed_idx, seed) = iter.clone().cloned().enumerate().min_by(|(_, a), (_, b)| {
        a.distance_sq(center)
            .partial_cmp(&b.distance_sq(center))
            .unwrap()
    })?;

    let (nearest_idx, nearest, _) = iter
        .clone()
        .cloned()
        .enumerate()
        .filter(|&(i, _)| i != seed_idx)
        .map(|(i, p)| (i, p, p.distance_sq(seed)))
        .filter(|(_, _, d)| d.abs() > std::f32::EPSILON)
        .min_by(|(_, _, a), (_, _, b)| a.partial_cmp(&b).unwrap())?;

    let (third_idx, third) = iter
        .cloned()
        .enumerate()
        .filter(|&(i, _)| i != seed_idx && i != nearest_idx)
        .min_by(|&(_, a), &(_, b)| {
            let t0 = Triangle(seed, nearest, a);
            let t1 = Triangle(seed, nearest, b);

            t0.circumradius_sq()
                .partial_cmp(&t1.circumradius_sq())
                .unwrap()
        })?;

    let tri = Triangle(seed, nearest, third);

    if tri.is_right_handed() {
        Some((tri, [seed_idx, nearest_idx, third_idx]))
    } else {
        let tri = Triangle(seed, third, nearest);
        Some((tri, [seed_idx, third_idx, nearest_idx]))
    }
}

/// Delaunay triangulation
pub struct Delaunay {
    pub dcel: TrianglesDCEL,
    hull: Hull,
    stack: Vec<usize>,
}

impl Delaunay {
    /// Triangulates a set of given points, if it is possible.
    pub fn new(points: &[Point]) -> Option<Delaunay> {
        let (seed, seed_indices) = find_seed_triangle(points)?;
        let seed_circumcenter = seed.circumcenter();

        let mut indices = (0..points.len())
            .filter(|&i| i != seed_indices[0] && i != seed_indices[1] && i != seed_indices[2])
            .collect::<Vec<_>>();

        let cmp = |&a: &usize, &b: &usize| {
            points[a]
                .distance_sq(seed_circumcenter)
                .partial_cmp(&points[b].distance_sq(seed_circumcenter))
                .unwrap()
        };

        #[cfg(feature = "rayon")]
        indices.par_sort_by(cmp);

        #[cfg(not(feature = "rayon"))]
        indices.sort_by(cmp);

        let max_triangles = 2 * points.len() - 3 - 2;

        let mut delaunay = Delaunay {
            dcel: TrianglesDCEL::with_capacity(max_triangles),
            hull: Hull::new(seed_indices, points),
            stack: Vec::with_capacity(STACK_CAPACITY),
        };

        delaunay.dcel.add_triangle(seed_indices);

        let mut prev_point: Option<Point> = None;

        for &i in &indices {
            let point = points[i];

            if let Some(p) = prev_point {
                if p.approx_eq(point) {
                    continue;
                }
            }

            delaunay.add_point(i, points);
            prev_point = Some(point);
        }

        Some(delaunay)
    }

    fn add_point(&mut self, index: usize, points: &[Point]) {
        let point = points[index];

        let (mut start, should_walk_back) = match self.hull.find_visible_edge(point, points) {
            Some(v) => v,
            None => return,
        };

        let mut end = self.hull.next[start];

        let t = self.add_triangle(
            [start, index, end],
            [
                OptionIndex::none(),
                OptionIndex::none(),
                self.hull.triangles[start],
            ],
        );

        self.hull.triangles[index] = OptionIndex::some(self.legalize(t + 2, points));
        self.hull.triangles[start] = OptionIndex::some(t);

        loop {
            let next = self.hull.next[end];
            let tri = Triangle(point, points[next], points[end]);
            if !tri.is_right_handed() {
                break;
            }

            let t = self.add_triangle(
                [end, index, next],
                [
                    self.hull.triangles[index],
                    OptionIndex::none(),
                    self.hull.triangles[end],
                ],
            );

            self.hull.triangles[index] = OptionIndex::some(self.legalize(t + 2, points));
            self.hull.next[end] = end;
            end = next;
        }

        if should_walk_back {
            loop {
                let prev = self.hull.prev[start];
                let tri = Triangle(point, points[start], points[prev]);
                if !tri.is_right_handed() {
                    break;
                }

                let t = self.add_triangle(
                    [prev, index, start],
                    [
                        OptionIndex::none(),
                        self.hull.triangles[start],
                        self.hull.triangles[prev],
                    ],
                );

                self.legalize(t + 2, points);

                self.hull.triangles[prev] = OptionIndex::some(t);
                self.hull.next[start] = start;
                start = prev;
            }
        }

        self.hull.start = start;
        self.hull.next[start] = index;
        self.hull.next[index] = end;

        self.hull.prev[end] = index;
        self.hull.prev[index] = start;

        self.hull.add_hash(index, point);
        self.hull.add_hash(start, points[start]);
    }

    fn add_triangle(&mut self, vertices: [usize; 3], halfedges: [OptionIndex; 3]) -> usize {
        let t = self.dcel.add_triangle(vertices);

        for (i, &halfedge) in halfedges.iter().enumerate() {
            if let Some(e) = halfedge.get() {
                self.dcel.link(t + i, e);
            }
        }

        t
    }

    fn legalize(&mut self, index: usize, points: &[Point]) -> usize {
        self.stack.push(index);

        let mut output = 0;

        while let Some(a) = self.stack.pop() {
            let ar = self.dcel.prev_edge(a);
            output = ar;

            let b = match self.dcel.twin(a) {
                Some(v) => v,
                None => continue,
            };

            let br = self.dcel.next_edge(b);
            let bl = self.dcel.prev_edge(b);

            /* if the pair of triangles doesn't satisfy the Delaunay condition
             * (p1 is inside the circumcircle of [p0, pl, pr]), flip them,
             * then do the same check/flip recursively for the new pair of triangles
             *
             *           pl                    pl
             *          /||\                  /  \
             *       al/ || \bl            al/    \a
             *        /  ||  \              /      \
             *       /  a||b  \    flip    /___ar___\
             *     p0\   ||   /p1   =>   p0\---bl---/p1
             *        \  ||  /              \      /
             *       ar\ || /br             b\    /br
             *          \||/                  \  /
             *           pr                    pr
             */

            let [p0, pr, pl] = self.dcel.triangle_points(ar);
            let p1 = self.dcel.triangle_points(bl)[0];

            let illegal = Triangle(points[p0], points[pr], points[pl]).in_circumcircle(points[p1]);

            if !illegal {
                continue;
            }

            self.dcel.vertices[a] = p1;
            self.dcel.vertices[b] = p0;

            let hbl = self.dcel.twin(bl);

            self.dcel.link_option(a, hbl);
            self.dcel.link_option(b, self.dcel.twin(ar));
            self.dcel.link(ar, bl);

            if hbl.is_none() {
                let mut edge = self.hull.start;

                loop {
                    if self.hull.triangles[edge] == OptionIndex::some(bl) {
                        self.hull.triangles[edge] = OptionIndex::some(a);
                        break;
                    }

                    edge = self.hull.next[edge];

                    if edge == self.hull.start || edge == self.hull.next[edge] {
                        break;
                    }
                }
            }

            if self.stack.len() >= STACK_CAPACITY - 1 {
                continue;
            }

            self.stack.push(br);
            self.stack.push(a);
        }

        output
    }
}