i_triangle 0.45.0

Polygon Triangulation Library: Efficient Delaunay Triangulation for Complex Shapes.
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
use crate::advanced::delaunay::IntDelaunay;
use crate::geom::triangle::IntTriangle;
use alloc::vec::Vec;
use core::iter::FusedIterator;
use i_overlay::i_float::int::number::int::IntNumber;
use i_overlay::i_float::int::number::wide_int::WideIntNumber;
use i_overlay::i_float::int::point::IntPoint;
use i_overlay::i_float::triangle::Triangle;
use i_overlay::i_shape::util::reserve::Reserve;

pub trait IndexType: Copy + Clone + TryFrom<usize> + Default {
    const MAX: usize;
    const ZERO: Self;
    fn add(self, other: Self) -> Self;
    fn into_usize(self) -> usize;
}

impl IndexType for u8 {
    const MAX: usize = u8::MAX as usize;
    const ZERO: Self = 0;
    #[inline]
    fn add(self, other: Self) -> Self {
        self + other
    }
    #[inline]
    fn into_usize(self) -> usize {
        self as usize
    }
}
impl IndexType for u16 {
    const MAX: usize = u16::MAX as usize;
    const ZERO: Self = 0;
    #[inline]
    fn add(self, other: Self) -> Self {
        self + other
    }
    #[inline]
    fn into_usize(self) -> usize {
        self as usize
    }
}
impl IndexType for u32 {
    const MAX: usize = u32::MAX as usize;
    const ZERO: Self = 0;
    #[inline]
    fn add(self, other: Self) -> Self {
        self + other
    }
    #[inline]
    fn into_usize(self) -> usize {
        self as usize
    }
}
impl IndexType for u64 {
    const MAX: usize = u64::MAX as usize;
    const ZERO: Self = 0;
    #[inline]
    fn add(self, other: Self) -> Self {
        self + other
    }
    #[inline]
    fn into_usize(self) -> usize {
        self as usize
    }
}
impl IndexType for usize {
    const MAX: usize = usize::MAX;
    const ZERO: Self = 0;
    #[inline]
    fn add(self, other: Self) -> Self {
        self + other
    }
    #[inline]
    fn into_usize(self) -> usize {
        self
    }
}

#[derive(Debug, Clone)]
pub struct IntTriangulation<I: IntNumber, N = u16> {
    pub points: Vec<IntPoint<I>>,
    pub indices: Vec<N>,
}

impl<I: IntNumber, N> Default for IntTriangulation<I, N> {
    #[inline]
    fn default() -> Self {
        Self {
            points: Vec::new(),
            indices: Vec::new(),
        }
    }
}

/// Iterator over resolved triangles in a flat [`IntTriangulation`].
///
/// Each item contains the three triangle points addressed by one consecutive
/// triple in the triangulation index buffer.
#[derive(Clone)]
pub struct IntTriangleIterator<'a, I: IntNumber, N> {
    points: &'a [IntPoint<I>],
    indices: core::slice::ChunksExact<'a, N>,
}

impl<I: IntNumber, N: IndexType> Iterator for IntTriangleIterator<'_, I, N> {
    type Item = [IntPoint<I>; 3];

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let indices = self.indices.next()?;
        let a = self.points[indices[0].into_usize()];
        let b = self.points[indices[1].into_usize()];
        let c = self.points[indices[2].into_usize()];
        Some([a, b, c])
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.indices.size_hint()
    }
}

impl<I: IntNumber, N: IndexType> ExactSizeIterator for IntTriangleIterator<'_, I, N> {
    #[inline]
    fn len(&self) -> usize {
        self.indices.len()
    }
}

impl<I: IntNumber, N: IndexType> FusedIterator for IntTriangleIterator<'_, I, N> {}

/// A int triangle mesh produced by the triangulation process.
///
/// This is the low-level output containing full triangle and vertex data,
/// including adjacency and vertex indices. It can be converted into a higher-level
/// `Triangulation` (index buffer + point list) using [`into_triangulation`].
///
/// Use this when you need detailed control over topology, neighbor tracking, or
/// advanced mesh manipulation.
#[derive(Debug)]
pub struct RawIntTriangulation<I: IntNumber> {
    pub(crate) triangles: Vec<IntTriangle<I>>,
    pub(crate) points: Vec<IntPoint<I>>,
}

impl<I: IntNumber> Default for RawIntTriangulation<I> {
    #[inline]
    fn default() -> Self {
        Self {
            triangles: Vec::new(),
            points: Vec::new(),
        }
    }
}

impl<I: IntNumber> RawIntTriangulation<I> {
    #[inline]
    pub(super) fn new(triangles: Vec<IntTriangle<I>>, points: Vec<IntPoint<I>>) -> Self {
        Self { triangles, points }
    }

    /// Returns true if the triangulation contains no triangles.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.triangles.is_empty()
    }

    /// Returns a reference to the list of points used in the triangulation.
    ///
    /// Each point corresponds to a coordinate used by one or more triangles.
    #[inline]
    pub fn points(&self) -> &Vec<IntPoint<I>> {
        &self.points
    }

    /// Returns a flat list of triangle vertex indices (ABC ordering).
    ///
    /// Each triangle contributes 3 indices into the `points` buffer.
    #[inline]
    pub fn triangle_indices<N: IndexType>(&self) -> Vec<N> {
        let mut indices = Vec::new();
        self.triangles.feed_indices(self.points.len(), &mut indices);
        indices
    }

    /// Returns the indices of each triangle's neighboring triangles.
    #[inline]
    pub fn triangle_neighbors(&self) -> Vec<[usize; 3]> {
        self.triangles
            .iter()
            .map(|triangle| triangle.neighbors)
            .collect()
    }

    /// Converts the int triangulation into a simpler index-based mesh.
    ///
    /// Returns a [`IntTriangulation`] with separate index buffer and point list.
    #[inline]
    pub fn into_triangulation<N: IndexType>(self) -> IntTriangulation<I, N> {
        IntTriangulation {
            indices: self.triangle_indices(),
            points: self.points,
        }
    }

    /// Converts the int triangulation into a simpler index-based mesh.
    ///
    /// Returns a [`IntTriangulation`] with separate index buffer and point list.
    #[inline]
    pub fn to_triangulation<N: IndexType>(&self) -> IntTriangulation<I, N> {
        IntTriangulation {
            indices: self.triangle_indices(),
            points: self.points.as_slice().to_vec(),
        }
    }

    #[inline]
    pub(crate) fn shift(&mut self, points_offset: usize, triangle_offset: usize) {
        for t in self.triangles.iter_mut() {
            t.vertices[0].index += points_offset;
            t.vertices[1].index += points_offset;
            t.vertices[2].index += points_offset;
            t.neighbors[0] = t.neighbors[0].saturating_add(triangle_offset);
            t.neighbors[1] = t.neighbors[1].saturating_add(triangle_offset);
            t.neighbors[2] = t.neighbors[2].saturating_add(triangle_offset);
        }
    }
}
impl<I: IntNumber, N: IndexType> IntTriangulation<I, N> {
    #[inline]
    pub fn empty() -> Self {
        Self {
            points: Vec::new(),
            indices: Vec::new(),
        }
    }

    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            points: Vec::with_capacity(capacity),
            indices: Vec::with_capacity(3 * capacity),
        }
    }

    #[inline]
    pub fn join(&mut self, other: &Self) {
        let points_offset = N::try_from(self.points.len()).unwrap_or(N::ZERO);
        for &i in other.indices.iter() {
            self.indices.push(i.add(points_offset));
        }
        self.points.extend_from_slice(&other.points)
    }

    /// Iterates over resolved triangle points.
    ///
    /// The iterator walks `indices` in exact triples and yields the matching
    /// `[IntPoint; 3]` for each triangle.
    #[inline]
    pub fn triangles(&self) -> IntTriangleIterator<'_, I, N> {
        IntTriangleIterator {
            points: &self.points,
            indices: self.indices.chunks_exact(3),
        }
    }

    #[inline]
    pub fn reserve_and_clear(&mut self, new_len: usize) {
        self.points.reserve_capacity(new_len);
        self.points.clear();
        self.indices.reserve_capacity(3 * new_len);
        self.indices.clear();
    }

    #[inline]
    pub fn fill_with_raw(&mut self, triangulation: &RawIntTriangulation<I>) {
        self.points.clear();
        self.points.extend_from_slice(&triangulation.points);

        triangulation
            .triangles
            .feed_indices(triangulation.points.len(), &mut self.indices);
    }

    #[inline]
    pub fn fill_with_delaunay(&mut self, delaunay: &IntDelaunay<I>) {
        self.points.clear();
        self.points.extend_from_slice(&delaunay.points);

        delaunay
            .triangles
            .feed_indices(delaunay.points.len(), &mut self.indices);
    }
}

#[cfg(test)]
mod tests {
    use super::IntTriangulation;
    use alloc::{vec, vec::Vec};
    use i_overlay::i_float::int::point::IntPoint;

    #[test]
    fn triangles_iterates_resolved_points() {
        let triangulation = IntTriangulation {
            points: vec![
                IntPoint::new(0, 0),
                IntPoint::new(10, 0),
                IntPoint::new(10, 10),
                IntPoint::new(0, 10),
            ],
            indices: vec![0_u16, 1, 2, 0, 2, 3],
        };

        let triangles: Vec<_> = triangulation.triangles().collect();

        assert_eq!(
            triangles,
            vec![
                [
                    IntPoint::new(0, 0),
                    IntPoint::new(10, 0),
                    IntPoint::new(10, 10),
                ],
                [
                    IntPoint::new(0, 0),
                    IntPoint::new(10, 10),
                    IntPoint::new(0, 10),
                ],
            ]
        );
    }
}

pub(crate) trait IndicesBuilder {
    fn feed_indices<N: IndexType>(&self, max_count: usize, indices: &mut Vec<N>);
}

impl<I: IntNumber> IndicesBuilder for [IntTriangle<I>] {
    #[inline]
    fn feed_indices<N: IndexType>(&self, max_count: usize, indices: &mut Vec<N>) {
        if max_count > N::MAX {
            panic!(
                "Index type `{}` cannot hold {} points",
                core::any::type_name::<N>(),
                max_count
            );
        }

        let count = 3 * self.len();
        indices.reserve_capacity(count);
        indices.clear();

        for t in self.iter() {
            let i0 = unsafe { N::try_from(t.vertices[0].index).unwrap_unchecked() };
            let i1 = unsafe { N::try_from(t.vertices[1].index).unwrap_unchecked() };
            let i2 = unsafe { N::try_from(t.vertices[2].index).unwrap_unchecked() };
            indices.push(i0);
            indices.push(i1);
            indices.push(i2);
        }
    }
}

impl<I: IntNumber> RawIntTriangulation<I> {
    pub fn validate(&self) {
        for (i, t) in self.triangles.iter().enumerate() {
            let a = t.vertices[0].point;
            let b = t.vertices[1].point;
            let c = t.vertices[2].point;
            let area = Triangle::area_two(a, b, c);
            assert!(area >= I::Wide::ZERO);

            let n0 = t.neighbors[0];
            let n1 = t.neighbors[1];
            let n2 = t.neighbors[2];

            if n0 != usize::MAX {
                assert!(self.triangles[n0].neighbors.contains(&i));
            }
            if n1 != usize::MAX {
                assert!(self.triangles[n1].neighbors.contains(&i));
            }
            if n2 != usize::MAX {
                assert!(self.triangles[n2].neighbors.contains(&i));
            }
        }
    }

    pub fn area_two(&self) -> I::Wide {
        let mut s = I::Wide::ZERO;
        for t in self.triangles.iter() {
            let a = t.vertices[0].point;
            let b = t.vertices[1].point;
            let c = t.vertices[2].point;

            s = s + Triangle::area_two(a, b, c);
        }
        s
    }
}

#[cfg(test)]
impl<I: IntNumber, N: IndexType> IntTriangulation<I, N> {
    pub fn validate(&self, shape_x2_area: I::Wide) {
        let mut s = I::Wide::ZERO;
        let mut i = 0;
        while i < self.indices.len() {
            let ai = self.indices[i];
            i += 1;
            let bi = self.indices[i];
            i += 1;
            let ci = self.indices[i];
            i += 1;

            let a = self.points[ai.into_usize()];
            let b = self.points[bi.into_usize()];
            let c = self.points[ci.into_usize()];

            let abc = Triangle::area_two(a, b, c);

            assert!(abc > I::Wide::ZERO);

            s = s + abc;
        }

        assert!(s == shape_x2_area);
    }
}