Debian Codegraph @

lib/ortho/trap.h

raw
1 /**
2 * @file
3 * @brief trapezoid elements and utilities for partition.c
4 *
5 * See [Fast polygon triangulation based on Seidel's algorithm](http://gamma.cs.unc.edu/SEIDEL/)
6 *
7 */
8
9 /*************************************************************************
10 * Copyright (c) 2011 AT&T Intellectual Property
11 * All rights reserved. This program and the accompanying materials
12 * are made available under the terms of the Eclipse Public License v1.0
13 * which accompanies this distribution, and is available at
14 * https://www.eclipse.org/legal/epl-v10.html
15 *
16 * Contributors: Details at https://graphviz.org
17 *************************************************************************/
18
19 #pragma once
20
21 #include <math.h>
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <util/list.h>
26
27 /* Segment attributes */
28
29 typedef struct {
30 pointf v0, v1; /* two endpoints */
31 bool is_inserted; /* inserted in trapezoidation yet ? */
32 size_t root0, root1; ///< root nodes in Q
33 int next; /* Next logical segment */
34 int prev; /* Previous segment */
35 } segment_t;
36
37
38 /* Trapezoid attributes */
39
40 typedef struct {
41 int lseg, rseg; /* two adjoining segments */
42 pointf hi, lo; /* max/min y-values */
43 size_t u0, u1;
44 size_t d0, d1;
45 size_t sink; ///< pointer to corresponding in Q
46 size_t usave; ///< I forgot what this means
47 int uside; ///< I forgot what this means
48 bool is_valid;
49 } trap_t;
50
51 /// is the given index a reference to an existing trapezoid?
52 ///
53 /// The indices stored in `trap_t.{u0|u1|d0|d1|usave}` can contain sentinel
54 /// values that represent an unset or invalid trapezoid. This function can be
55 /// used to determine the validity of these fields.
56 static inline bool is_valid_trap(size_t index) {
57 return index != 0 && index != SIZE_MAX;
58 }
59
60 /// an array of trapezoids
61 typedef LIST(trap_t) traps_t;
62
63 #define C_EPS 1.0e-7 /* tolerance value: Used for making */
64 /* all decisions about collinearity or */
65 /* left/right of segment. Decrease */
66 /* this value if the input points are */
67 /* spaced very close together */
68
69 static inline bool fp_equal(double s, double t) { return fabs(s - t) <= C_EPS; }
70
71 /**
72 * @brief double floating point three-way comparison
73 *
74 * Returns -1, 0, or 1 if f1 respectively, less than,
75 * almost equal with tolerance C_EPS, or greater than f2.
76 * The purpose of the function is workaround precision issues.
77 */
78
79 static inline int dfp_cmp(double f1, double f2) {
80 double d = f1 - f2;
81 if (d < -C_EPS)
82 return -1;
83 if (d > C_EPS)
84 return 1;
85 return 0;
86 }
87
88 static inline bool equal_to(pointf v0, pointf v1) {
89 return fp_equal(v0.y, v1.y) && fp_equal(v0.x, v1.x);
90 }
91
92 static inline bool greater_than(pointf v0, pointf v1) {
93 return v0.y > v1.y + C_EPS ? true
94 : (v0.y < v1.y - C_EPS ? false : v0.x > v1.x);
95 }
96
97 extern traps_t construct_trapezoids(int, segment_t*, int*);