rgeometry 0.12.0

High-Level Computational Geometry
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
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet, VecDeque};

use crate::algorithms::triangulation::earclip;
use crate::data::{IndexEdge, Point, PointId, Polygon};
use crate::{Orientation, PolygonScalar};

/// Computes a constrained Delaunay triangulation for a simple polygon.
///
/// The implementation first ear-clips the polygon to obtain an initial
/// triangulation and then applies Lawson edge flips to restore the constrained
/// Delaunay condition while keeping boundary edges fixed. The worst-case time
/// complexity is $O(n^2)$.
///
/// # Panics
///
/// Panics if the polygon contains holes. Support for polygons with holes is
/// not yet implemented.
pub fn constrained_delaunay<T>(poly: &Polygon<T>) -> Vec<(PointId, PointId, PointId)>
where
  T: PolygonScalar,
{
  assert!(
    poly.rings.len() == 1,
    "constrained Delaunay currently only supports simple polygons without holes"
  );

  let mut triangles: Vec<[PointId; 3]> =
    earclip::earclip(poly).map(|(a, b, c)| [a, b, c]).collect();

  if triangles.is_empty() {
    return Vec::new();
  }

  let constraint_edges = collect_constraint_edges(poly);
  let mut edge_map = build_edge_map(&triangles, &constraint_edges);
  let mut queue = VecDeque::new();
  for (&edge, info) in edge_map.iter() {
    if !info.constraint && info.triangles.len() == 2 {
      queue.push_back(edge);
    }
  }

  while let Some(edge) = queue.pop_front() {
    let Some(info) = edge_map.get(&edge) else {
      continue;
    };
    if info.constraint || info.triangles.len() != 2 {
      continue;
    }
    let [t1_idx, t2_idx] = [info.triangles[0], info.triangles[1]];
    let tri1 = triangles[t1_idx];
    let tri2 = triangles[t2_idx];

    let Some(opp1) = tri1.iter().copied().find(|pid| !tri2.contains(pid)) else {
      continue;
    };
    let tri1_rot = rotate_triangle(&tri1, opp1);
    let shared0 = tri1_rot[1];
    let shared1 = tri1_rot[2];
    if !tri2.contains(&shared0) || !tri2.contains(&shared1) {
      continue;
    }
    let opp2 = tri2
      .iter()
      .copied()
      .find(|pid| *pid != shared0 && *pid != shared1)
      .unwrap();

    // Skip non-convex quadrilaterals.
    let o1 = Point::orient(
      &poly.points[opp1.usize()],
      &poly.points[opp2.usize()],
      &poly.points[shared0.usize()],
    );
    let o2 = Point::orient(
      &poly.points[opp1.usize()],
      &poly.points[opp2.usize()],
      &poly.points[shared1.usize()],
    );
    if matches!(
      (o1, o2),
      (Orientation::CounterClockWise, Orientation::CounterClockWise)
        | (Orientation::ClockWise, Orientation::ClockWise)
    ) {
      continue;
    }

    let incircle = incircle_at(&poly.points, opp1, shared0, shared1, opp2);
    if incircle != Ordering::Greater {
      continue;
    }

    let tri1_old_edges = triangle_edges(&triangles[t1_idx]);
    let tri2_old_edges = triangle_edges(&triangles[t2_idx]);
    remove_triangle_edges(&mut edge_map, &tri1_old_edges, t1_idx);
    remove_triangle_edges(&mut edge_map, &tri2_old_edges, t2_idx);

    triangles[t1_idx] = [opp1, shared1, opp2];
    triangles[t2_idx] = [opp2, shared0, opp1];

    ensure_ccw(&mut triangles[t1_idx], &poly.points);
    ensure_ccw(&mut triangles[t2_idx], &poly.points);

    let tri1_new_edges = triangle_edges(&triangles[t1_idx]);
    let tri2_new_edges = triangle_edges(&triangles[t2_idx]);
    add_triangle_edges(&mut edge_map, &constraint_edges, &tri1_new_edges, t1_idx);
    add_triangle_edges(&mut edge_map, &constraint_edges, &tri2_new_edges, t2_idx);

    for edge in tri1_new_edges.into_iter().chain(tri2_new_edges.into_iter()) {
      if let Some(info) = edge_map.get(&edge)
        && !info.constraint
        && info.triangles.len() == 2
      {
        queue.push_back(edge);
      }
    }
  }

  triangles
    .into_iter()
    .map(|tri| (tri[0], tri[1], tri[2]))
    .collect()
}

fn collect_constraint_edges<T>(poly: &Polygon<T>) -> HashSet<IndexEdge> {
  let mut set = HashSet::new();
  for ring in &poly.rings {
    for window in ring.windows(2) {
      let edge = IndexEdge::new(window[0], window[1]);
      set.insert(edge);
    }
    if let (Some(first), Some(last)) = (ring.first(), ring.last()) {
      set.insert(IndexEdge::new(*last, *first));
    }
  }
  set
}

fn build_edge_map(
  triangles: &[[PointId; 3]],
  constraints: &HashSet<IndexEdge>,
) -> HashMap<IndexEdge, EdgeInfo> {
  let mut map = HashMap::new();

  for &edge in constraints {
    map.insert(
      edge,
      EdgeInfo {
        constraint: true,
        triangles: Vec::new(),
      },
    );
  }

  for (idx, tri) in triangles.iter().enumerate() {
    for edge in triangle_edges(tri) {
      map
        .entry(edge)
        .or_insert_with(|| EdgeInfo {
          constraint: constraints.contains(&edge),
          triangles: Vec::new(),
        })
        .add_triangle(idx);
    }
  }

  map
}

fn rotate_triangle(tri: &[PointId; 3], first: PointId) -> [PointId; 3] {
  if tri[0] == first {
    [tri[0], tri[1], tri[2]]
  } else if tri[1] == first {
    [tri[1], tri[2], tri[0]]
  } else {
    [tri[2], tri[0], tri[1]]
  }
}

#[derive(Clone, Debug)]
struct EdgeInfo {
  constraint: bool,
  triangles: Vec<usize>,
}

impl EdgeInfo {
  fn add_triangle(&mut self, idx: usize) {
    if !self.triangles.contains(&idx) {
      self.triangles.push(idx);
    }
  }

  fn remove_triangle(&mut self, idx: usize) {
    if let Some(pos) = self.triangles.iter().position(|&other| other == idx) {
      self.triangles.swap_remove(pos);
    }
  }
}

fn triangle_edges(tri: &[PointId; 3]) -> [IndexEdge; 3] {
  [
    IndexEdge::new(tri[0], tri[1]),
    IndexEdge::new(tri[1], tri[2]),
    IndexEdge::new(tri[2], tri[0]),
  ]
}

fn remove_triangle_edges(
  edge_map: &mut HashMap<IndexEdge, EdgeInfo>,
  edges: &[IndexEdge; 3],
  tri_idx: usize,
) {
  for edge in edges {
    if let Some(info) = edge_map.get_mut(edge) {
      info.remove_triangle(tri_idx);
      if info.triangles.is_empty() && !info.constraint {
        edge_map.remove(edge);
      }
    }
  }
}

fn add_triangle_edges(
  edge_map: &mut HashMap<IndexEdge, EdgeInfo>,
  constraints: &HashSet<IndexEdge>,
  edges: &[IndexEdge; 3],
  tri_idx: usize,
) {
  for edge in edges {
    edge_map
      .entry(*edge)
      .or_insert_with(|| EdgeInfo {
        constraint: constraints.contains(edge),
        triangles: Vec::new(),
      })
      .add_triangle(tri_idx);
  }
}

fn ensure_ccw<T>(tri: &mut [PointId; 3], points: &[Point<T>])
where
  T: PolygonScalar,
{
  if Point::orient(
    &points[tri[0].usize()],
    &points[tri[1].usize()],
    &points[tri[2].usize()],
  )
  .is_cw()
  {
    tri.swap(1, 2);
  }
}

fn incircle_at<T>(points: &[Point<T>], a: PointId, b: PointId, c: PointId, d: PointId) -> Ordering
where
  T: PolygonScalar,
{
  let pa = point_coords(&points[a.usize()]);
  let pb = point_coords(&points[b.usize()]);
  let pc = point_coords(&points[c.usize()]);
  let pd = point_coords(&points[d.usize()]);
  T::incircle(&pa, &pb, &pc, &pd)
}

fn point_coords<T>(point: &Point<T>) -> [T; 2]
where
  T: PolygonScalar,
{
  [point.x_coord().clone(), point.y_coord().clone()]
}

#[cfg(test)]
mod tests {
  use super::*;
  use num::BigInt;
  use num_rational::BigRational;
  use proptest::collection::vec;
  use proptest::prelude::*;
  use std::collections::{BTreeSet, HashSet};

  fn br(val: i64) -> BigRational {
    BigRational::from_integer(BigInt::from(val))
  }

  #[test]
  fn square_prefers_longer_diagonal() {
    let poly = Polygon::new_unchecked(vec![
      Point::new([br(0), br(0)]),
      Point::new([br(1), br(0)]),
      Point::new([br(1), br(1)]),
      Point::new([br(0), br(1)]),
    ]);

    let triangles = constrained_delaunay(&poly);
    let edges: HashSet<IndexEdge> = triangles
      .iter()
      .flat_map(|tri| {
        let arr = [tri.0, tri.1, tri.2];
        triangle_edges(&arr).into_iter()
      })
      .collect();
    let diag = IndexEdge::new(poly.rings[0][1], poly.rings[0][3]);
    assert!(edges.contains(&diag));
  }

  #[test]
  fn concave_polygon_respects_constraints() {
    let poly = Polygon::new_unchecked(vec![
      Point::new([br(0), br(0)]),
      Point::new([br(4), br(0)]),
      Point::new([br(4), br(4)]),
      Point::new([br(2), br(2)]),
      Point::new([br(0), br(4)]),
    ]);
    let triangles = constrained_delaunay(&poly);
    assert_eq!(triangles.len(), 3);

    let boundary: HashSet<IndexEdge> = collect_constraint_edges(&poly);
    let triset: HashSet<IndexEdge> = triangles
      .iter()
      .flat_map(|tri| {
        let arr = [tri.0, tri.1, tri.2];
        triangle_edges(&arr).into_iter()
      })
      .collect();
    for edge in boundary {
      assert!(triset.contains(&edge), "missing boundary edge {:?}", edge);
    }
  }

  fn small_polygons() -> impl Strategy<Value = Polygon<BigRational>> {
    vec((-10..=10, -10..=10), 3..8).prop_filter_map("simple polygon", |pts| {
      let mut set = BTreeSet::new();
      let mut points = Vec::new();
      for (x, y) in pts {
        if !set.insert((x, y)) {
          return None;
        }
        points.push(Point::new([
          BigRational::from_integer(BigInt::from(x)),
          BigRational::from_integer(BigInt::from(y)),
        ]));
      }
      Polygon::new(points).ok()
    })
  }

  proptest! {
    #![proptest_config(ProptestConfig::with_cases(12))]
    #[test]
    fn constrained_triangulation_is_delaunay(
      poly in small_polygons()
    ) {
      prop_assume!(poly.rings.len() == 1);

      let triangles = constrained_delaunay(&poly);
      let n = poly.rings[0].len();
      prop_assert_eq!(triangles.len(), n.saturating_sub(2));

      for tri in &triangles {
        let orientation = Point::orient(
          &poly.points[tri.0.usize()],
          &poly.points[tri.1.usize()],
          &poly.points[tri.2.usize()]
        );
        prop_assert!(orientation.is_ccw());
      }

      let constraints = collect_constraint_edges(&poly);
      let triangle_arrays: Vec<[PointId; 3]> = triangles
        .iter()
        .map(|tri| [tri.0, tri.1, tri.2])
        .collect();
      let adjacency = build_edge_map(&triangle_arrays, &constraints);

      for (&edge, info) in adjacency.iter() {
        if constraints.contains(&edge) {
          continue;
        }
        if info.triangles.len() != 2 {
          continue;
        }
        let t1_idx = info.triangles[0];
        let t2_idx = info.triangles[1];
        let tri1 = triangle_arrays[t1_idx];
        let tri2 = triangle_arrays[t2_idx];
        let Some(opp1) = tri1
          .iter()
          .copied()
          .find(|pid| !tri2.contains(pid))
        else {
          continue;
        };
        let tri1_rot = rotate_triangle(&tri1, opp1);
        let shared0 = tri1_rot[1];
        let shared1 = tri1_rot[2];
        prop_assert!(tri2.contains(&shared0));
        prop_assert!(tri2.contains(&shared1));
        let opp2 = tri2
          .iter()
          .copied()
          .find(|pid| *pid != shared0 && *pid != shared1)
          .unwrap();

        let sign = incircle_at(&poly.points, opp1, shared0, shared1, opp2);
        prop_assert_ne!(sign, Ordering::Greater);
      }
    }
  }
}