earclip 1.8.0

Triangle mesh designed to be fast, efficient, and sphere capable.
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
#![no_std]
#![forbid(unsafe_code)]
#![deny(missing_docs)]
//! # earclip
//!
//! ## Description
//!
//! The `earclip` module is a tool to convert 2D and 3D polygons into a triangle mesh designed to be
//! fast, efficient, and sphere capable.
//!
//! ## Install
//!
//! ```bash
//! cargo add earclip
//! ```
//!
//! ## Usage
//!
//! This crate takes advantage of the [`s2json::GetXY`] and [`s2json::GetZ`] traits for 2D and 3D
//! polygon triangulation. If you need to use your own 2D and 3D polygon types, just implement
//! those traits for them.
//!
//! - [`crate::earclip`]: for 2D and 3D polygon triangulation
//! - [`crate::earclip_float`]: for 2D and 3D polygon triangulation on flattened f64 data
//! - [`crate::tesselate`]: for tesselating a flattened polygon
//! - [`crate::flatten`]: for flattening a 2D or 3D polygon
//! - [`crate::flatten_float`]: for flattening a 2D or 3D polygon whose points are f64 vectors
//!
//! ## Example
//!
//! ```rust
//! use earclip::earclip_float;
//!
//! let polygon = vec![vec![vec![0.0, 0.0, 0.0], vec![1.0, 0.0, 0.0], vec![0.0, 1.0, 0.0]]];
//! let (vertices, indices) = earclip_float(&polygon, None, None);
//! assert_eq!(vertices, vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0]);
//! assert_eq!(indices, vec![1, 2, 0]);
//! ```

extern crate alloc;

/// The `earcut` module
pub mod earcut;

use alloc::vec::Vec;
use core::f64;
pub use earcut::{earcut, signed_area};
use libm::fabs;
pub use s2json::{GetXY, GetZ};

/// An earcut polygon generator with tesselation support
pub fn earclip<T: GetXY + GetZ>(
    polygon: &[Vec<T>],
    modulo: Option<f64>,
    offset: Option<usize>,
) -> (Vec<f64>, Vec<usize>) {
    let modulo = modulo.unwrap_or(f64::INFINITY);
    let offset = offset.unwrap_or(0);

    // Use earcut to build standard triangle set
    let (mut vertices, hole_indices, dim) = flatten(polygon); // dim => dimensions
    let mut indices = earcut(&vertices, &hole_indices, dim);

    // tesselate if necessary
    if modulo != f64::INFINITY {
        tesselate(&mut vertices, &mut indices, modulo, dim);
    }

    // update offsets
    for index in indices.iter_mut() {
        *index += offset;
    }

    (vertices, indices)
}

/// An earcut polygon generator with tesselation support
pub fn earclip_float(
    polygon: &[Vec<Vec<f64>>],
    modulo: Option<f64>,
    offset: Option<usize>,
) -> (Vec<f64>, Vec<usize>) {
    let modulo = modulo.unwrap_or(f64::INFINITY);
    let offset = offset.unwrap_or(0);

    // Use earcut to build standard triangle set
    let (mut vertices, hole_indices, dim) = flatten_float(polygon); // dim => dimensions
    let mut indices = earcut(&vertices, &hole_indices, dim);

    // tesselate if necessary
    if modulo != f64::INFINITY {
        tesselate(&mut vertices, &mut indices, modulo, 2);
    }

    // update offsets
    indices = indices.into_iter().map(|index| index + offset).collect();

    (vertices, indices)
}

/// Tesselates the flattened polygon
pub fn tesselate(vertices: &mut Vec<f64>, indices: &mut Vec<usize>, modulo: f64, dim: usize) {
    for axis in 0..dim {
        let mut i: isize = 0;
        while i < indices.len() as isize {
            let i_index = i as usize;
            let a = indices[i_index];
            let b = indices[i_index + 1];
            let c = indices[i_index + 2];

            if let Some(new_triangle) =
                split_if_necessary(a, b, c, vertices, indices, dim, axis, modulo)
            {
                indices[i_index] = new_triangle[0];
                indices[i_index + 1] = new_triangle[1];
                indices[i_index + 2] = new_triangle[2];
                i -= 3;
            }

            i += 3;
        }
    }
}

/// given vertices, and an axis of said vertices:
/// find a number "x" that is x % modulo == 0 and between v1 and v2
#[allow(clippy::too_many_arguments)]
fn split_if_necessary(
    i1: usize,
    i2: usize,
    i3: usize,
    vertices: &mut Vec<f64>,
    indices: &mut Vec<usize>,
    dim: usize,
    axis: usize,
    modulo: f64,
) -> Option<[usize; 3]> {
    let v1 = vertices[i1 * dim + axis];
    let v2 = vertices[i2 * dim + axis];
    let v3 = vertices[i3 * dim + axis];
    // 1 is corner
    if v1 < v2 && v1 < v3 {
        let mod_point = v1 + modulo - mod2(v1, modulo);
        if mod_point > v1 && mod_point <= v2 && mod_point <= v3 && v2 != mod_point {
            return Some(split_right(
                mod_point, i1, i2, i3, v1, v2, v3, vertices, indices, dim, axis, modulo,
            ));
        }
    } else if v1 > v2 && v1 > v3 {
        let mut m2 = mod2(v1, modulo);
        if m2 == 0. {
            m2 = modulo;
        }
        let mod_point = v1 - m2;
        if mod_point < v1 && mod_point >= v2 && mod_point >= v3 && v2 != mod_point {
            return Some(split_left(
                mod_point, i1, i2, i3, v1, v2, v3, vertices, indices, dim, axis, modulo,
            ));
        }
    }
    // 2 is corner
    if v2 < v1 && v2 < v3 {
        let mod_point = v2 + modulo - mod2(v2, modulo);
        if mod_point > v2
            && mod_point <= v3
            && mod_point <= v1
            && (v1 != mod_point || v3 != mod_point)
        {
            return Some(split_right(
                mod_point, i2, i3, i1, v2, v3, v1, vertices, indices, dim, axis, modulo,
            ));
        }
    } else if v2 > v1 && v2 > v3 {
        let mut m2 = mod2(v2, modulo);
        if m2 == 0. {
            m2 = modulo;
        }
        let mod_point = v2 - m2;
        if mod_point < v2
            && mod_point >= v3
            && mod_point >= v1
            && (v1 != mod_point || v3 != mod_point)
        {
            return Some(split_left(
                mod_point, i2, i3, i1, v2, v3, v1, vertices, indices, dim, axis, modulo,
            ));
        }
    }
    // 3 is corner
    if v3 < v1 && v3 < v2 {
        let mod_point = v3 + modulo - mod2(v3, modulo);
        if mod_point > v3
            && mod_point <= v1
            && mod_point <= v2
            && (v1 != mod_point || v2 != mod_point)
        {
            return Some(split_right(
                mod_point, i3, i1, i2, v3, v1, v2, vertices, indices, dim, axis, modulo,
            ));
        }
    } else if v3 > v1 && v3 > v2 {
        let mut m2 = mod2(v3, modulo);
        if m2 == 0. {
            m2 = modulo;
        }
        let mod_point = v3 - m2;
        if mod_point < v3
            && mod_point >= v1
            && mod_point >= v2
            && (v1 != mod_point || v2 != mod_point)
        {
            return Some(split_left(
                mod_point, i3, i1, i2, v3, v1, v2, vertices, indices, dim, axis, modulo,
            ));
        }
    }

    None
}

/// Creates a vertex at the specified split point, only manipulating the specified axis
#[allow(clippy::too_many_arguments)]
fn create_vertex(
    split_point: f64,
    i1: usize,
    i2: usize,
    v1: f64,
    v2: f64,
    vertices: &mut Vec<f64>,
    dim: usize,
    axis: usize,
) -> usize {
    let index = vertices.len() / dim;
    let travel_divisor = (v2 - v1) / (split_point - v1);
    for i in 0..2 {
        let va1 = vertices[i1 * dim + i];
        let va2 = vertices[i2 * dim + i];
        if i != axis {
            vertices.push(va1 + (va2 - va1) / travel_divisor);
        } else {
            vertices.push(split_point);
        }
    }
    index
}

/// Splits triangles based on a modulo value and adjusts vertices
#[allow(clippy::too_many_arguments)]
fn split_right(
    mod_point: f64,
    i1: usize,
    i2: usize,
    i3: usize,
    v1: f64,
    v2: f64,
    v3: f64,
    vertices: &mut Vec<f64>,
    indices: &mut Vec<usize>,
    dim: usize,
    axis: usize,
    modulo: f64,
) -> [usize; 3] {
    let mut mod_point = mod_point;
    // Creating the first set of split vertices
    let mut i12 = create_vertex(mod_point, i1, i2, v1, v2, vertices, dim, axis);
    let mut i13 = create_vertex(mod_point, i1, i3, v1, v3, vertices, dim, axis);
    indices.extend([i1, i12, i13]);

    mod_point += modulo;

    if v2 < v3 {
        while mod_point < v2 {
            indices.extend([i13, i12]);
            i13 = create_vertex(mod_point, i1, i3, v1, v3, vertices, dim, axis);
            indices.extend([i13, i13, i12]);
            i12 = create_vertex(mod_point, i1, i2, v1, v2, vertices, dim, axis);
            indices.push(i12);
            mod_point += modulo;
        }
        indices.extend([i13, i12, i2]);
        [i13, i2, i3]
    } else {
        while mod_point < v3 {
            indices.extend([i13, i12]);
            i13 = create_vertex(mod_point, i1, i3, v1, v3, vertices, dim, axis);
            indices.extend([i13, i13, i12]);
            i12 = create_vertex(mod_point, i1, i2, v1, v2, vertices, dim, axis);
            indices.push(i12);
            mod_point += modulo;
        }
        indices.extend([i13, i12, i3]);
        [i3, i12, i2]
    }
}

/// Splits triangles based on a modulo value and adjusts vertices
#[allow(clippy::too_many_arguments)]
fn split_left(
    mod_point: f64,
    i1: usize,
    i2: usize,
    i3: usize,
    v1: f64,
    v2: f64,
    v3: f64,
    vertices: &mut Vec<f64>,
    indices: &mut Vec<usize>,
    dim: usize,
    axis: usize,
    modulo: f64,
) -> [usize; 3] {
    let mut mod_point = mod_point;
    // first case is a standalone triangle
    let mut i12 = create_vertex(mod_point, i1, i2, v1, v2, vertices, dim, axis);
    let mut i13 = create_vertex(mod_point, i1, i3, v1, v3, vertices, dim, axis);
    indices.extend([i1, i12, i13]);
    mod_point -= modulo;
    if v2 > v3 {
        // create lines up to i2
        while mod_point > v2 {
            // next triangles are i13->i12->nexti13 and nexti13->i12->nexti12 so store in necessary order
            indices.extend([i13, i12]);
            i13 = create_vertex(mod_point, i1, i3, v1, v3, vertices, dim, axis);
            indices.extend([i13, i13, i12]);
            i12 = create_vertex(mod_point, i1, i2, v1, v2, vertices, dim, axis);
            indices.push(i12);
            // increment
            mod_point -= modulo;
        }
        // add v2 triangle if necessary
        indices.extend([i13, i12, i2]);
        // return the remaining triangle
        [i13, i2, i3]
    } else {
        // create lines up to i2
        while mod_point > v3 {
            // next triangles are i13->i12->nexti13 and nexti13->i12->nexti12 so store in necessary order
            indices.extend([i13, i12]);
            i13 = create_vertex(mod_point, i1, i3, v1, v3, vertices, dim, axis);
            indices.extend([i13, i13, i12]);
            i12 = create_vertex(mod_point, i1, i2, v1, v2, vertices, dim, axis);
            indices.push(i12);
            // increment
            mod_point -= modulo;
        }
        // add v3 triangle if necessary
        indices.extend([i13, i12, i3]);
        // return the remaining triangle
        [i3, i12, i2]
    }
}

/// Returns x modulo n (supports negative numbers)
fn mod2(x: f64, n: f64) -> f64 {
    ((x % n) + n) % n
}

/// Flattens a 2D or 3D array whether its a flat point ([x, y, z]) or object ({ x, y, z })
pub fn flatten<T: GetXY + GetZ>(data: &[Vec<T>]) -> (Vec<f64>, Vec<usize>, usize) {
    let mut vertices = Vec::new();
    let mut hole_indices = Vec::new();
    let mut hole_index = 0;
    let mut dim = 2; // Assume 2D unless a 3D point is found
    if !data.is_empty() && !data[0].is_empty() && data[0][0].z().is_some() {
        dim = 3;
    }

    for (i, line) in data.iter().enumerate() {
        for point in line {
            vertices.push(point.x());
            vertices.push(point.y());
            if dim == 3 {
                vertices.push(point.z().unwrap_or_default());
            }
        }
        if i > 0 {
            hole_index += data[i - 1].len();
            hole_indices.push(hole_index);
        }
    }

    (vertices, hole_indices, dim)
}

/// Flattens a 2D or 3D array whether its a flat point ([x, y, z]) or object ({ x, y, z })
pub fn flatten_float(data: &[Vec<Vec<f64>>]) -> (Vec<f64>, Vec<usize>, usize) {
    let mut vertices = Vec::new();
    let mut hole_indices = Vec::new();
    let mut hole_index = 0;
    let mut dim = 2; // Assume 2D unless a 3D point is found

    for (i, line) in data.iter().enumerate() {
        for point in line {
            vertices.push(point[0]);
            vertices.push(point[1]);
            // Check if it's a 3D point
            if point.len() > 2 {
                vertices.push(point[2]);
                dim = 3; // Update dimensionality to 3D if any point is 3D
            }
        }
        if i > 0 {
            hole_index += data[i - 1].len();
            hole_indices.push(hole_index);
        }
    }

    (vertices, hole_indices, dim)
}

// /// Convert a structure into a 2d vector array
// pub fn convert_2d<P: GetXY>(data: &[Vec<P>]) -> Vec<Vec<Vec<f64>>> {
//     data.iter()
//         .map(|line| line.iter().map(|point| Vec::from([point.x(), point.y()])).collect())
//         .collect()
// }

// /// Convert a structure into a 3d vector array
// pub fn convert_3d<P: GetXY + GetZ>(data: &[Vec<P>]) -> Vec<Vec<Vec<f64>>> {
//     data.iter()
//         .map(|line| {
//             line.iter()
//                 .map(|point| Vec::from([point.x(), point.y(), point.z().unwrap_or_default()]))
//                 .collect()
//         })
//         .collect()
// }

/// Returns a percentage difference between the polygon area and its triangulation area;
/// used to verify correctness of triangulation
pub fn deviation(data: &[f64], hole_indices: &[usize], triangles: &[usize], dim: usize) -> f64 {
    let has_holes = !hole_indices.is_empty();
    let outer_len = if has_holes { hole_indices[0] * dim } else { data.len() };
    let mut polygon_area = fabs(signed_area(data, 0, outer_len, dim));

    if has_holes {
        for i in 0..hole_indices.len() {
            let start = hole_indices[i] * dim;
            let end =
                if i < hole_indices.len() - 1 { hole_indices[i + 1] * dim } else { data.len() };
            polygon_area -= fabs(signed_area(data, start, end, dim));
        }
    }

    let mut triangles_area = 0.;
    let mut i = 0;
    while i < triangles.len() {
        let a = triangles[i] * dim;
        let b = triangles[i + 1] * dim;
        let c = triangles[i + 2] * dim;
        triangles_area += fabs(
            (data[a] - data[c]) * (data[b + 1] - data[a + 1])
                - (data[a] - data[b]) * (data[c + 1] - data[a + 1]),
        );
        i += 3;
    }

    let zero = 0.;
    if polygon_area == zero && triangles_area == zero {
        zero
    } else {
        fabs((triangles_area - polygon_area) / polygon_area)
    }
}