poly2tri-rs 0.1.0

An idiomatic and fast Constrained Delaunay Triangulation library
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
/// This example is like a visual debugger, it can draw each step
use clap::Parser;
use poly2tri_rs::{
    loader::{Loader, PlainFileLoader},
    Context, Edge, Observer, Point, Sweeper, TriangleId,
};
use utils::draw_svg;
mod utils;

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
    /// Name of the person to greet
    #[arg(short, long)]
    path: Option<std::path::PathBuf>,

    #[arg(short, long)]
    output: Option<std::path::PathBuf>,

    #[arg(long)]
    point: bool,

    #[arg(long)]
    edge: bool,

    #[arg(long, default_value = "false")]
    test: bool,

    #[arg(long, default_value = "false")]
    debug: bool,

    #[arg(long, default_value = "1")]
    bench_count: usize,

    #[arg(long, default_value = "1000")]
    frame_count: usize,
}

fn main() {
    let args = Args::parse();

    let sweeper_builder = {
        let mut file_loader = PlainFileLoader::default();
        file_loader
            .load(args.path.as_ref().unwrap().as_os_str().to_str().unwrap())
            .unwrap()
    };

    if let Some(output_path) = args.output {
        // draw result instead of debug

        draw_svg(sweeper_builder.build().triangulate(), output_path);

        return;
    }

    if args.bench_count == 1 {
        let mut observer = DrawObserver::new(&args);
        let _result = sweeper_builder
            .clone()
            .build()
            .triangulate_with_observer(&mut observer);
        observer.save();

        // measure time with dummy observer
        let start = std::time::Instant::now();
        let count = 1000;
        for _ in 0..count {
            let _ = sweeper_builder.clone().build().triangulate();
        }
        let end = std::time::Instant::now();
        let duration = end.duration_since(start) / count;
        println!("{:?} per draw", duration);
    } else {
        for _ in 0..args.bench_count {
            let _ = sweeper_builder.clone().build().triangulate();
        }
    }
}

struct DrawObserver {
    messages: Vec<String>,
    debug: bool,
    // whether show debug info, like point_id, locations, triangle, messages
    point: bool,
    edge: bool,

    frame_count: usize,

    legalizing: Option<TriangleId>,

    // whether all process done
    finalized: bool,

    /// how many legalize steps triggered
    legalized_step_count: u64,
    point_count: u64,
    edge_count: u64,
    rotate_count: u64,

    /// svgs
    frames: Vec<String>,
    frame_messages: Vec<Vec<String>>,
}

impl DrawObserver {
    fn new(args: &Args) -> Self {
        Self {
            debug: args.debug,
            point: args.point,
            edge: args.edge,
            messages: Default::default(),
            legalizing: Default::default(),
            finalized: false,
            frames: vec![],
            frame_messages: vec![],
            legalized_step_count: 0,
            point_count: 0,
            edge_count: 0,
            rotate_count: 0,
            frame_count: args.frame_count,
        }
    }

    fn save(&self) {
        use askama::Template;

        #[derive(Template)] // this will generate the code...
        #[template(path = "draw_template.html")] // using the template in this path, relative
                                                 // to the `templates` dir in the crate root

        struct DrawTemplate<'a> {
            // the name of the struct can be anything
            frames: &'a [String],
            frame_messages: &'a [Vec<String>],
            legalized_count: u64,
            point_count: u64,
            edge_count: u64,
            rotate_count: u64,
        }

        let html_content = DrawTemplate {
            frames: self.frames.as_slice(),
            frame_messages: &self.frame_messages,
            legalized_count: self.legalized_step_count,
            point_count: self.point_count,
            edge_count: self.edge_count,
            rotate_count: self.rotate_count,
        }
        .render()
        .unwrap();

        std::fs::write("draw.html", html_content).unwrap();
    }
}

impl Observer for DrawObserver {
    fn point_event(&mut self, point_id: poly2tri_rs::PointId, context: &Context) {
        self.point_count += 1;
        if !self.point {
            return;
        }
        let point = context.points.get_point(point_id).unwrap();
        self.messages
            .push(format!("point event: {point_id:?} {point:?}"));
        self.draw(context);
    }

    fn edge_event(&mut self, edge: Edge, context: &Context) {
        self.edge_count += 1;
        if !self.edge {
            return;
        }
        self.messages.push(format!(
            "edge_event: p:{} q:{}",
            edge.p.as_usize(),
            edge.q.as_usize(),
        ));

        self.draw(context);
    }

    fn will_legalize(&mut self, triangle_id: TriangleId, _context: &Context) {
        self.legalizing = Some(triangle_id);
    }

    fn legalize_step(&mut self, _triangle_id: TriangleId, _context: &Context) {
        // do nothing for now
        self.legalized_step_count += 1;
    }

    fn triangle_rotated(
        &mut self,
        _triangle_id: TriangleId,
        _opposite_triangle_id: TriangleId,
        _context: &Context,
    ) {
        self.rotate_count += 1;
    }

    fn legalized(&mut self, _triangle_id: TriangleId, _context: &Context) {
        self.legalizing = None;
    }

    fn sweep_done(&mut self, context: &Context) {
        self.messages.push("sweep done".into());
        self.draw(context);
    }

    fn finalized(&mut self, context: &Context) {
        self.messages.push("finalized".into());
        self.finalized = true;
        self.draw(context);
    }
}

impl DrawObserver {
    fn draw(&mut self, context: &Context) {
        if self.frames.len() >= self.frame_count {
            return;
        }

        use svg::Document;
        use svg::Node;

        #[derive(Debug, Clone, Copy)]
        struct MapRect {
            x: f64,
            y: f64,
            w: f64,
            h: f64,
        }

        // map rect with y flipped, svg's coordinate with origin at left-top
        #[derive(Debug)]
        struct Map {
            from: MapRect,
            to: MapRect,
        }

        impl Map {
            fn map_point(&self, x: f64, y: f64) -> (f64, f64) {
                let x = (x - self.from.x) / self.from.w * self.to.w + self.to.x;
                let y = self.to.h - (y - self.from.y) / self.from.h * self.to.h + self.to.y;
                (x, y)
            }
        }

        let mut min_x = f64::MAX;
        let mut max_x = f64::MIN;
        let mut min_y = f64::MAX;
        let mut max_y = f64::MIN;
        for p in context.points.iter().map(|(_, p, _)| p) {
            min_x = min_x.min(p.x);
            max_x = max_x.max(p.x);
            min_y = min_y.min(p.y);
            max_y = max_y.max(p.y);
        }

        let w = max_x - min_x;
        let space = w * 0.05; // give some space

        let from = MapRect {
            x: min_x - space,
            y: min_y - space,
            w: max_x - min_x + 2. * space,
            h: max_y - min_y + 2. * space,
        };

        let to = if from.w <= 100. {
            MapRect {
                x: 0.,
                y: 0.,
                w: 800.,
                h: 800.,
            }
        } else {
            from
        };
        let map = Map { from, to };

        let mut doc = Document::new()
            .set("viewBox", (to.x, to.y, to.w, to.h))
            .set("style", "background-color: #F5F5F5");

        if !self.finalized {
            let point_r = from.w / 200.;

            for (id, point, edges) in context.points.iter() {
                let (x, y) = map.map_point(point.x, point.y);

                if self.debug {
                    doc.append(text(
                        format!("({}) ({:.2}, {:.2})", id.as_usize(), point.x, point.y),
                        (x, y),
                    ));
                }

                if self.point {
                    doc.append(circle((x, y), point_r, "red", "clear"));
                }

                for p_id in edges {
                    let p_point = context.points.get_point(p_id).unwrap();
                    let p = map.map_point(p_point.x, p_point.y);
                    let q = map.map_point(point.x, point.y);

                    doc.append(line(p, q, "black"));
                }
            }

            for (id, t) in context.triangles.iter() {
                let p0 = context.points.get_point(t.points[0]).unwrap();
                let p1 = context.points.get_point(t.points[1]).unwrap();
                let p2 = context.points.get_point(t.points[2]).unwrap();

                let p0 = map.map_point(p0.x, p0.y);
                let p1 = map.map_point(p1.x, p1.y);
                let p2 = map.map_point(p2.x, p2.y);

                doc.append(triangle(p0, p1, p2, "blue", "clear"));

                let center = ((p0.0 + p1.0 + p2.0) / 3., (p0.1 + p1.1 + p2.1) / 3.);

                let point_percent = 0.5;
                let center_percent = 1. - point_percent;

                if self.debug {
                    let p0_drifted = (
                        center.0 * center_percent + p0.0 * point_percent,
                        center.1 * center_percent + p0.1 * point_percent,
                    );
                    let p1_drifted = (
                        center.0 * center_percent + p1.0 * point_percent,
                        center.1 * center_percent + p1.1 * point_percent,
                    );
                    let p2_drifted = (
                        center.0 * center_percent + p2.0 * point_percent,
                        center.1 * center_percent + p2.1 * point_percent,
                    );

                    let color_for_idx = |idx: usize| {
                        let color = if t.is_constrained(idx) {
                            "yellow"
                        } else {
                            "gray"
                        };
                        let color = if t.neighbors[idx].invalid() {
                            "red"
                        } else {
                            color
                        };
                        let color = if t.is_delaunay(idx) { "black" } else { color };
                        color
                    };

                    doc.append(line(p0_drifted, p1_drifted, color_for_idx(2)));
                    doc.append(line(p1_drifted, p2_drifted, color_for_idx(0)));
                    doc.append(line(p2_drifted, p0_drifted, color_for_idx(1)));

                    doc.append(text(
                        format!("{}", id.as_usize()),
                        ((p0.0 + p1.0 + p2.0) / 3., (p0.1 + p1.1 + p2.1) / 3.),
                    ));
                }
            }

            if self.debug {
                for node in context.advancing_front.iter() {
                    if let Some(t) = node.triangle {
                        let t = context.triangles.get(t).unwrap();

                        let p0 = context.points.get_point(t.points[0]).unwrap();
                        let p1 = context.points.get_point(t.points[1]).unwrap();
                        let p2 = context.points.get_point(t.points[2]).unwrap();

                        let p0 = map.map_point(p0.x, p0.y);
                        let p1 = map.map_point(p1.x, p1.y);
                        let p2 = map.map_point(p2.x, p2.y);

                        doc.append(line(p0, p1, "red"));
                        doc.append(line(p1, p2, "red"));
                        doc.append(line(p2, p0, "red"));
                    }
                }
            }
        }

        let border_color = if context.result.iter().any(|t| {
            let t = context.triangles.get(*t).unwrap();

            let p0 = context.points.get_point(t.points[0]).unwrap();
            let p1 = context.points.get_point(t.points[1]).unwrap();
            let p2 = context.points.get_point(t.points[2]).unwrap();

            let p0 = map.map_point(p0.x, p0.y);
            let p1 = map.map_point(p1.x, p1.y);
            let p2 = map.map_point(p2.x, p2.y);

            distance(p0, p1) < 1. || distance(p0, p2) < 1. || distance(p1, p2) < 1.
        }) {
            "clear"
        } else {
            "white"
        };

        for t in &context.result {
            let t = context.triangles.get(*t).unwrap();

            let p0 = context.points.get_point(t.points[0]).unwrap();
            let p1 = context.points.get_point(t.points[1]).unwrap();
            let p2 = context.points.get_point(t.points[2]).unwrap();

            let p0 = map.map_point(p0.x, p0.y);
            let p1 = map.map_point(p1.x, p1.y);
            let p2 = map.map_point(p2.x, p2.y);

            doc.append(triangle(p0, p1, p2, border_color, "blue"));
        }

        let mut draw_illegal_triangle = |tid: TriangleId, fill_color: &str, border_color: &str| {
            let t = tid.get(&context.triangles);
            let p0 = context.points.get_point(t.points[0]).unwrap();
            let p1 = context.points.get_point(t.points[1]).unwrap();
            let p2 = context.points.get_point(t.points[2]).unwrap();

            {
                let p0 = map.map_point(p0.x, p0.y);
                let p1 = map.map_point(p1.x, p1.y);
                let p2 = map.map_point(p2.x, p2.y);

                doc.append(triangle(p0, p1, p2, fill_color, border_color));
            }
        };

        let illegal_pairs = Sweeper::illegal_triangles(context);
        for (from_tid, to_tid) in illegal_pairs {
            let from_t = from_tid.get(&context.triangles);
            let to_t = to_tid.get(&context.triangles);

            let (from_i, to_i) = from_t.common_edge_index(to_t).unwrap();

            let p = from_t.points[from_i];
            let (illegal, det) = in_circle(
                context.points.get_point(p).unwrap(),
                context.points.get_point(from_t.point_ccw(p)).unwrap(),
                context.points.get_point(from_t.point_cw(p)).unwrap(),
                context.points.get_point(to_t.points[to_i]).unwrap(),
            );

            self.messages.push(format!("illegal: {illegal} det: {det}"));

            draw_illegal_triangle(from_tid, "red", "red");
            draw_illegal_triangle(to_tid, "yellow", "red");
        }

        self.frames.push(doc.to_string());
        self.frame_messages.push(std::mem::take(&mut self.messages));
    }
}

fn line(p: (f64, f64), q: (f64, f64), color: &str) -> svg::node::element::Line {
    svg::node::element::Line::new()
        .set("class", "edge")
        .set("stroke", to_color(color))
        .set("x1", p.0)
        .set("y1", p.1)
        .set("x2", q.0)
        .set("y2", q.1)
}

fn text(content: impl Into<String>, p: (f64, f64)) -> svg::node::element::Text {
    svg::node::element::Text::new()
        .add(svg::node::Text::new(content))
        .set("x", p.0)
        .set("y", p.1)
}

fn triangle(
    p0: (f64, f64),
    p1: (f64, f64),
    p2: (f64, f64),
    border_color: &str,
    fill_color: &str,
) -> svg::node::element::Path {
    let data = svg::node::element::path::Data::new()
        .move_to(p0)
        .line_to(p1)
        .line_to(p2)
        .close();

    svg::node::element::Path::new()
        .set("d", data)
        .set("stroke", to_color(border_color))
        .set("fill", to_color(fill_color))
}

fn distance(l: (f64, f64), r: (f64, f64)) -> f64 {
    ((r.0 - l.0) * (r.0 - l.0) + (r.1 - l.1) * (r.1 - l.1)).sqrt()
}

fn circle(
    c: (f64, f64),
    r: f64,
    stroke_color: &str,
    fill_color: &str,
) -> svg::node::element::Circle {
    svg::node::element::Circle::new()
        .set("cx", c.0)
        .set("cy", c.1)
        .set("r", r)
        .set("stroke-color", to_color(stroke_color))
        .set("stroke-width", 1)
        .set("fill-color", to_color(fill_color))
}

fn to_color(name: &str) -> String {
    match name {
        "blue" => "#29B6F6",
        "yellow" => "#FFA726",
        "red" => "#EF5350",
        "black" => "#3E2723",
        "gray" => "#616161",
        "clear" => "#00000000",
        _ => name,
    }
    .into()
}

/// both check and returns the det value
fn in_circle(pa: Point, pb: Point, pc: Point, pd: Point) -> (bool, f64) {
    let adx = pa.x - pd.x;
    let ady = pa.y - pd.y;
    let bdx = pb.x - pd.x;
    let bdy = pb.y - pd.y;

    let adxbdy = adx * bdy;
    let bdxady = bdx * ady;
    let oabd = adxbdy - bdxady;

    if oabd <= 0. {
        return (false, 0.);
    }

    let cdx = pc.x - pd.x;
    let cdy = pc.y - pd.y;

    let cdxady = cdx * ady;
    let adxcdy = adx * cdy;
    let ocad = cdxady - adxcdy;

    if ocad <= 0. {
        return (false, 0.);
    }

    let bdxcdy = bdx * cdy;
    let cdxbdy = cdx * bdy;

    let alift = adx * adx + ady * ady;
    let blift = bdx * bdx + bdy * bdy;
    let clift = cdx * cdx + cdy * cdy;

    let det = alift * (bdxcdy - cdxbdy) + blift * ocad + clift * oabd;

    (det > 0., det)
}