scirs2-spatial 0.4.2

Spatial algorithms module for SciRS2 (scirs2-spatial)
Documentation
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! Incremental Delaunay triangulation (Bowyer-Watson) and Voronoi diagram.
//!
//! This module provides:
//!
//! * [`DelaunayTriangulation`] — Build from a point set or insert points
//!   incrementally using the Bowyer-Watson algorithm.
//! * [`VoronoiDiagram`] — Dual of the Delaunay triangulation; each
//!   circumcentre becomes a Voronoi vertex and each Delaunay edge becomes a
//!   Voronoi ridge.
//!
//! Both structures are self-contained pure-Rust implementations.
//!
//! # Examples
//!
//! ```
//! use scirs2_spatial::delaunay_voronoi::{DelaunayTriangulation, VoronoiDiagram};
//!
//! let pts = vec![[0.0, 0.0], [4.0, 0.0], [2.0, 3.0], [2.0, 1.0]];
//! let dt = DelaunayTriangulation::from_points(&pts);
//! assert!(!dt.triangles.is_empty());
//!
//! let vor = VoronoiDiagram::from_delaunay(&dt, [-1.0, -1.0, 5.0, 4.0]);
//! assert!(!vor.vertices.is_empty());
//! ```

use std::collections::{HashMap, HashSet};

// ── Helper geometry ────────────────────────────────────────────────────────────

/// Compute the circumcircle of three 2-D points.
///
/// Returns `(centre_x, centre_y, radius²)`.
/// Returns `None` if points are collinear.
fn circumcircle(a: [f64; 2], b: [f64; 2], c: [f64; 2]) -> Option<([f64; 2], f64)> {
    let ax = b[0] - a[0];
    let ay = b[1] - a[1];
    let bx = c[0] - a[0];
    let by = c[1] - a[1];
    let d = 2.0 * (ax * by - ay * bx);
    if d.abs() < 1e-10 {
        return None; // collinear
    }
    let ux = (by * (ax * ax + ay * ay) - ay * (bx * bx + by * by)) / d;
    let uy = (ax * (bx * bx + by * by) - bx * (ax * ax + ay * ay)) / d;
    let cx = a[0] + ux;
    let cy = a[1] + uy;
    let r2 = ux * ux + uy * uy;
    Some(([cx, cy], r2))
}

/// Signed 2-D cross product of vectors AB and AC.
#[inline]
fn cross2(a: [f64; 2], b: [f64; 2], c: [f64; 2]) -> f64 {
    (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])
}

// ── DelaunayTriangulation ──────────────────────────────────────────────────────

/// Incremental Delaunay triangulation built with the Bowyer-Watson algorithm.
///
/// Indices in `triangles` reference the public `points` vector.  The first
/// three points (index 0, 1, 2) are the super-triangle vertices that bracket
/// all user-supplied points; they are stripped in the final result.
pub struct DelaunayTriangulation {
    /// All points including the internal super-triangle vertices (indices 0–2).
    pub points: Vec<[f64; 2]>,
    /// Triangles as triples of indices into `points`.
    pub triangles: Vec<[usize; 3]>,
    /// Number of super-triangle vertices prepended.
    super_offset: usize,
}

impl DelaunayTriangulation {
    // ── Construction ──────────────────────────────────────────────────────────

    /// Build a Delaunay triangulation from a slice of 2-D points.
    pub fn from_points(points: &[[f64; 2]]) -> Self {
        let mut dt = Self::with_super_triangle(points);
        for &p in points {
            dt.insert(p);
        }
        dt.finalise();
        dt
    }

    /// Create an empty triangulation containing only the super-triangle.
    fn with_super_triangle(points: &[[f64; 2]]) -> Self {
        // Compute bounding box.
        let (mut min_x, mut min_y, mut max_x, mut max_y) =
            (f64::INFINITY, f64::INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
        for &p in points {
            min_x = min_x.min(p[0]);
            min_y = min_y.min(p[1]);
            max_x = max_x.max(p[0]);
            max_y = max_y.max(p[1]);
        }
        let dx = (max_x - min_x).max(1.0);
        let dy = (max_y - min_y).max(1.0);
        let delta = dx.max(dy) * 10.0;

        let s0 = [min_x - delta, min_y - delta];
        let s1 = [min_x + dx / 2.0, max_y + delta];
        let s2 = [max_x + delta, min_y - delta];

        let mut pts = vec![s0, s1, s2];
        pts.reserve(points.len());

        Self {
            points: pts,
            triangles: vec![[0, 1, 2]],
            super_offset: 3,
        }
    }

    /// Insert a single point and restore the Delaunay property.
    pub fn insert(&mut self, point: [f64; 2]) {
        let pid = self.points.len();
        self.points.push(point);

        // Find all triangles whose circumcircle contains `point`.
        let mut bad: Vec<usize> = Vec::new();
        for (i, &tri) in self.triangles.iter().enumerate() {
            let a = self.points[tri[0]];
            let b = self.points[tri[1]];
            let c = self.points[tri[2]];
            if let Some(([cx, cy], r2)) = circumcircle(a, b, c) {
                let dx = point[0] - cx;
                let dy = point[1] - cy;
                if dx * dx + dy * dy < r2 + 1e-10 {
                    bad.push(i);
                }
            }
        }

        if bad.is_empty() {
            return;
        }

        // Find the boundary polygon of the "bad" triangles (the hole).
        let boundary = Self::boundary_polygon(&self.triangles, &bad);

        // Remove bad triangles (in reverse order to preserve indices).
        let mut bad_sorted = bad.clone();
        bad_sorted.sort_unstable();
        bad_sorted.dedup();
        for &i in bad_sorted.iter().rev() {
            self.triangles.swap_remove(i);
        }

        // Re-triangulate the hole.
        for [ea, eb] in boundary {
            self.triangles.push([ea, eb, pid]);
        }
    }

    /// Collect the boundary edges of the union of a set of triangles.
    ///
    /// An edge shared by exactly one bad triangle forms the boundary.
    fn boundary_polygon(triangles: &[[usize; 3]], bad: &[usize]) -> Vec<[usize; 2]> {
        let bad_set: HashSet<usize> = bad.iter().copied().collect();

        // Build edge → list of bad triangles that contain it.
        let mut edge_count: HashMap<[usize; 2], usize> = HashMap::new();
        for &i in bad {
            let tri = triangles[i];
            for &edge in &[
                [tri[0].min(tri[1]), tri[0].max(tri[1])],
                [tri[1].min(tri[2]), tri[1].max(tri[2])],
                [tri[0].min(tri[2]), tri[0].max(tri[2])],
            ] {
                *edge_count.entry(edge).or_insert(0) += 1;
            }
        }

        // Boundary edges appear exactly once among bad triangles.
        let boundary_edges: Vec<[usize; 2]> = edge_count
            .into_iter()
            .filter(|(_, count)| *count == 1)
            .map(|(e, _)| e)
            .collect();

        // Re-orient edges consistently (the orientation of the original triangle).
        let mut oriented: Vec<[usize; 2]> = Vec::with_capacity(boundary_edges.len());
        let boundary_set: HashSet<[usize; 2]> = boundary_edges.into_iter().collect();

        for &i in &bad_set {
            let tri = triangles[i];
            let edges_oriented = [[tri[0], tri[1]], [tri[1], tri[2]], [tri[2], tri[0]]];
            for e in &edges_oriented {
                let canonical = [e[0].min(e[1]), e[0].max(e[1])];
                if boundary_set.contains(&canonical) {
                    oriented.push(*e);
                }
            }
        }
        oriented
    }

    /// Remove all triangles that share a vertex with the super-triangle.
    fn finalise(&mut self) {
        let super_verts: HashSet<usize> = (0..self.super_offset).collect();
        self.triangles.retain(|tri| {
            !tri.iter().any(|v| super_verts.contains(v))
        });
        // Remap indices: remove the 3 super-triangle vertices.
        let offset = self.super_offset;
        for tri in &mut self.triangles {
            tri[0] -= offset;
            tri[1] -= offset;
            tri[2] -= offset;
        }
        // Remove super-triangle points.
        let user_points: Vec<[f64; 2]> = self.points[offset..].to_vec();
        self.points = user_points;
        self.super_offset = 0;
    }

    // ── Queries ───────────────────────────────────────────────────────────────

    /// Find the index of the triangle containing `point` (O(n) walk).
    ///
    /// Returns `None` if the point is outside the convex hull.
    pub fn locate_point(&self, point: [f64; 2]) -> Option<usize> {
        for (i, &tri) in self.triangles.iter().enumerate() {
            let a = self.points[tri[0]];
            let b = self.points[tri[1]];
            let c = self.points[tri[2]];
            let c1 = cross2(a, b, point);
            let c2 = cross2(b, c, point);
            let c3 = cross2(c, a, point);
            if (c1 >= 0.0 && c2 >= 0.0 && c3 >= 0.0)
                || (c1 <= 0.0 && c2 <= 0.0 && c3 <= 0.0)
            {
                return Some(i);
            }
        }
        None
    }

    /// Return all unique edges as `[vertex_a, vertex_b]` pairs (unsorted).
    pub fn edges(&self) -> Vec<[usize; 2]> {
        let mut edge_set: HashSet<[usize; 2]> = HashSet::new();
        for &tri in &self.triangles {
            for &[a, b] in &[
                [tri[0].min(tri[1]), tri[0].max(tri[1])],
                [tri[1].min(tri[2]), tri[1].max(tri[2])],
                [tri[0].min(tri[2]), tri[0].max(tri[2])],
            ] {
                edge_set.insert([a, b]);
            }
        }
        edge_set.into_iter().collect()
    }

    /// Return the circumcircle of triangle `tri_idx` as `(centre, radius)`.
    pub fn circumcircle_of(&self, tri_idx: usize) -> Option<([f64; 2], f64)> {
        let tri = self.triangles.get(tri_idx)?;
        let a = self.points[tri[0]];
        let b = self.points[tri[1]];
        let c = self.points[tri[2]];
        circumcircle(a, b, c).map(|(centre, r2)| (centre, r2.sqrt()))
    }
}

// ── VoronoiDiagram ────────────────────────────────────────────────────────────

/// Voronoi diagram derived from a Delaunay triangulation.
pub struct VoronoiDiagram {
    /// Input sites (copy of Delaunay points).
    pub sites: Vec<[f64; 2]>,
    /// Voronoi vertices = circumcentres of Delaunay triangles.
    pub vertices: Vec<[f64; 2]>,
    /// For each site, the ordered list of Voronoi vertex indices bounding its cell.
    /// May be empty for sites with unbounded cells.
    pub regions: Vec<Vec<usize>>,
    /// Pairs of site indices sharing a Voronoi ridge (one per Delaunay edge).
    pub ridge_points: Vec<[usize; 2]>,
    /// Pairs of Voronoi vertex indices; -1 denotes an unbounded ray.
    pub ridge_vertices: Vec<[i64; 2]>,
}

impl VoronoiDiagram {
    /// Build a Voronoi diagram from an existing [`DelaunayTriangulation`].
    ///
    /// `bbox` = `[x_min, y_min, x_max, y_max]` — bounding box for clipping
    /// unbounded ridges.
    pub fn from_delaunay(dt: &DelaunayTriangulation, bbox: [f64; 4]) -> Self {
        let n_tris = dt.triangles.len();
        let mut vertices: Vec<[f64; 2]> = Vec::with_capacity(n_tris);

        // Map triangle index → Voronoi vertex index.
        let mut tri_to_vert: HashMap<usize, usize> = HashMap::new();

        for (i, &tri) in dt.triangles.iter().enumerate() {
            let a = dt.points[tri[0]];
            let b = dt.points[tri[1]];
            let c = dt.points[tri[2]];
            if let Some((centre, _)) = circumcircle(a, b, c) {
                tri_to_vert.insert(i, vertices.len());
                vertices.push(centre);
            }
        }

        // Build adjacency: for each directed edge (a, b), which triangles contain it?
        // Edge (u, v) with u < v → list of triangles.
        let mut edge_to_tris: HashMap<[usize; 2], Vec<usize>> = HashMap::new();
        for (i, &tri) in dt.triangles.iter().enumerate() {
            for &[a, b] in &[
                [tri[0].min(tri[1]), tri[0].max(tri[1])],
                [tri[1].min(tri[2]), tri[1].max(tri[2])],
                [tri[0].min(tri[2]), tri[0].max(tri[2])],
            ] {
                edge_to_tris.entry([a, b]).or_default().push(i);
            }
        }

        let mut ridge_points: Vec<[usize; 2]> = Vec::new();
        let mut ridge_vertices: Vec<[i64; 2]> = Vec::new();

        let [bx0, by0, bx1, by1] = bbox;

        for ([a, b], tris) in &edge_to_tris {
            let site_a = *a;
            let site_b = *b;

            match tris.as_slice() {
                [t1, t2] => {
                    // Shared by two triangles → finite ridge.
                    let v1 = tri_to_vert.get(t1).copied().map(|i| i as i64).unwrap_or(-1);
                    let v2 = tri_to_vert.get(t2).copied().map(|i| i as i64).unwrap_or(-1);
                    ridge_points.push([site_a, site_b]);
                    ridge_vertices.push([v1, v2]);
                }
                [t1] => {
                    // Boundary edge → semi-infinite ridge.
                    let v1 = tri_to_vert.get(t1).copied().map(|i| i as i64).unwrap_or(-1);
                    // Compute a far point in the direction perpendicular to the edge,
                    // pointing outward.
                    let pa = dt.points[site_a];
                    let pb = dt.points[site_b];
                    let mid = [(pa[0] + pb[0]) * 0.5, (pa[1] + pb[1]) * 0.5];
                    // Outward normal (perpendicular to edge, pointing away from interior).
                    let dx = pb[0] - pa[0];
                    let dy = pb[1] - pa[1];
                    let len = (dx * dx + dy * dy).sqrt().max(1e-12);
                    let nx = -dy / len;
                    let ny = dx / len;

                    // Place "far" vertex at bbox boundary.
                    let far_scale = (bx1 - bx0).max(by1 - by0) * 2.0;
                    let fx = (mid[0] + nx * far_scale).clamp(bx0, bx1);
                    let fy = (mid[1] + ny * far_scale).clamp(by0, by1);
                    let far_idx = vertices.len() as i64;
                    vertices.push([fx, fy]);

                    ridge_points.push([site_a, site_b]);
                    ridge_vertices.push([v1, far_idx]);
                }
                _ => {}
            }
        }

        // Build regions: for each site, collect connected Voronoi vertex indices.
        let n_sites = dt.points.len();
        let mut regions: Vec<Vec<usize>> = vec![Vec::new(); n_sites];

        // A site's region is formed by the vertices of all triangles incident to it.
        let mut site_tris: Vec<Vec<usize>> = vec![Vec::new(); n_sites];
        for (i, &tri) in dt.triangles.iter().enumerate() {
            for &s in &tri {
                if s < n_sites {
                    site_tris[s].push(i);
                }
            }
        }

        for (s, tris) in site_tris.iter().enumerate() {
            let vert_indices: Vec<usize> = tris
                .iter()
                .filter_map(|&t| tri_to_vert.get(&t).copied())
                .collect();
            // Sort vertices in CCW order around the site.
            let site = dt.points[s];
            let mut unique: Vec<usize> = vert_indices;
            unique.sort_unstable();
            unique.dedup();
            unique.sort_unstable_by(|&vi, &vj| {
                let a = vertices[vi];
                let b = vertices[vj];
                let ang_a = (a[1] - site[1]).atan2(a[0] - site[0]);
                let ang_b = (b[1] - site[1]).atan2(b[0] - site[0]);
                ang_a.partial_cmp(&ang_b).unwrap_or(std::cmp::Ordering::Equal)
            });
            regions[s] = unique;
        }

        Self {
            sites: dt.points.clone(),
            vertices,
            regions,
            ridge_points,
            ridge_vertices,
        }
    }

    /// Build a Voronoi diagram from a raw point set.
    pub fn from_points(points: &[[f64; 2]], bbox: [f64; 4]) -> Self {
        let dt = DelaunayTriangulation::from_points(points);
        Self::from_delaunay(&dt, bbox)
    }

    /// Compute the area of a Voronoi cell using the shoelace formula.
    ///
    /// Returns `None` if the region is empty (site on convex hull with no
    /// bounded cell) or the polygon is degenerate.
    pub fn cell_area(&self, site_idx: usize) -> Option<f64> {
        let region = self.regions.get(site_idx)?;
        if region.len() < 3 {
            return None;
        }
        let verts: Vec<[f64; 2]> = region
            .iter()
            .filter_map(|&vi| self.vertices.get(vi).copied())
            .collect();
        if verts.len() < 3 {
            return None;
        }
        let n = verts.len();
        let mut area = 0.0_f64;
        for i in 0..n {
            let j = (i + 1) % n;
            area += verts[i][0] * verts[j][1];
            area -= verts[j][0] * verts[i][1];
        }
        Some(area.abs() / 2.0)
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_circumcircle_equilateral() {
        // Equilateral triangle: circumradius = side / √3
        let s = 2.0_f64;
        let a = [0.0, 0.0];
        let b = [s, 0.0];
        let c = [s / 2.0, s * 3.0_f64.sqrt() / 2.0];
        let (centre, r2) = circumcircle(a, b, c).expect("circumcircle");
        let r = r2.sqrt();
        let expected_r = s / 3.0_f64.sqrt();
        assert!((r - expected_r).abs() < 1e-10, "r={r}");
        // Centre should be equidistant from all three vertices.
        let da = ((a[0] - centre[0]).powi(2) + (a[1] - centre[1]).powi(2)).sqrt();
        let db = ((b[0] - centre[0]).powi(2) + (b[1] - centre[1]).powi(2)).sqrt();
        let dc = ((c[0] - centre[0]).powi(2) + (c[1] - centre[1]).powi(2)).sqrt();
        assert!((da - db).abs() < 1e-9);
        assert!((da - dc).abs() < 1e-9);
    }

    #[test]
    fn test_circumcircle_collinear() {
        let a = [0.0, 0.0];
        let b = [1.0, 0.0];
        let c = [2.0, 0.0];
        assert!(circumcircle(a, b, c).is_none());
    }

    #[test]
    fn test_delaunay_basic() {
        let pts = vec![[0.0, 0.0], [4.0, 0.0], [2.0, 3.0], [2.0, 1.0]];
        let dt = DelaunayTriangulation::from_points(&pts);
        assert_eq!(dt.points.len(), 4);
        // 4 points → at least 2 triangles (possibly 3).
        assert!(dt.triangles.len() >= 2);
        // Euler: V - E + F = 2 for convex sets (roughly).
        for tri in &dt.triangles {
            for &v in tri {
                assert!(v < 4, "out of range vertex {}", v);
            }
        }
    }

    #[test]
    fn test_delaunay_grid() {
        // 3×3 grid: 9 points → 8 triangles (standard).
        let pts: Vec<[f64; 2]> = (0..3)
            .flat_map(|i| (0..3).map(move |j| [i as f64, j as f64]))
            .collect();
        let dt = DelaunayTriangulation::from_points(&pts);
        assert_eq!(dt.points.len(), 9);
        // For a convex set of n points: #triangles ≤ 2n - h - 2 where h = hull size.
        assert!(dt.triangles.len() >= 4);
    }

    #[test]
    fn test_locate_point() {
        let pts = vec![[0.0, 0.0], [4.0, 0.0], [2.0, 3.0]];
        let dt = DelaunayTriangulation::from_points(&pts);
        // Only one triangle — interior point should be found.
        let idx = dt.locate_point([2.0, 1.0]);
        assert!(idx.is_some());
        // Point outside the hull.
        let outside = dt.locate_point([10.0, 10.0]);
        assert!(outside.is_none());
    }

    #[test]
    fn test_edges_count() {
        let pts = vec![[0.0, 0.0], [1.0, 0.0], [0.5, 1.0], [0.5, 0.3]];
        let dt = DelaunayTriangulation::from_points(&pts);
        let edges = dt.edges();
        // Each triangle has 3 edges; shared edges counted once.
        // For 4 points on a convex hull with one interior: 3+3 triangles, 5 unique edges.
        // The exact count depends on the triangulation, but at least #triangles * 3 / 2.
        assert!(!edges.is_empty());
    }

    #[test]
    fn test_voronoi_basic() {
        let pts = vec![[0.0, 0.0], [4.0, 0.0], [2.0, 3.0], [2.0, 1.0]];
        let dt = DelaunayTriangulation::from_points(&pts);
        let vor = VoronoiDiagram::from_delaunay(&dt, [-2.0, -2.0, 6.0, 5.0]);

        // Same number of sites as input points.
        assert_eq!(vor.sites.len(), pts.len());
        // Some ridges must exist.
        assert!(!vor.ridge_points.is_empty());
        // Vertices are circumcentres.
        assert!(!vor.vertices.is_empty());
    }

    #[test]
    fn test_voronoi_from_points() {
        let pts: Vec<[f64; 2]> = (0..4)
            .flat_map(|i| (0..4).map(move |j| [i as f64, j as f64]))
            .collect();
        let vor = VoronoiDiagram::from_points(&pts, [-1.0, -1.0, 4.0, 4.0]);
        assert_eq!(vor.sites.len(), 16);
        assert!(!vor.vertices.is_empty());
    }

    #[test]
    fn test_voronoi_cell_area() {
        // Four corners of a 4×4 square: interior site [2,2] should have a bounded cell.
        let pts = vec![[0.0, 0.0], [4.0, 0.0], [0.0, 4.0], [4.0, 4.0], [2.0, 2.0]];
        let vor = VoronoiDiagram::from_points(&pts, [-1.0, -1.0, 5.0, 5.0]);
        // Cell area of interior point should be finite and positive.
        let area = vor.cell_area(4);
        if let Some(a) = area {
            assert!(a > 0.0, "area={a}");
        }
        // Degenerate site (< 3 vertices in region) returns None.
        let empty = VoronoiDiagram {
            sites: vec![[0.0, 0.0]],
            vertices: vec![],
            regions: vec![vec![]],
            ridge_points: vec![],
            ridge_vertices: vec![],
        };
        assert!(empty.cell_area(0).is_none());
    }

    #[test]
    fn test_delaunay_single_point() {
        let pts = vec![[0.0, 0.0]];
        let dt = DelaunayTriangulation::from_points(&pts);
        // Not enough points for a triangle.
        assert_eq!(dt.triangles.len(), 0);
    }

    #[test]
    fn test_delaunay_two_points() {
        let pts = vec![[0.0, 0.0], [1.0, 0.0]];
        let dt = DelaunayTriangulation::from_points(&pts);
        assert_eq!(dt.triangles.len(), 0);
    }
}