aboutsummaryrefslogtreecommitdiff
path: root/src/critical_points.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 22:28:13 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 22:47:34 +0000
commitcb070b2d3f2b6e241e3be1b7e0eeb90fc755255f (patch)
treec7d90c0abe9533c87d51b5196871846855efa1ba /src/critical_points.c
downloadthesis-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/critical_points.c')
-rw-r--r--src/critical_points.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/critical_points.c b/src/critical_points.c
new file mode 100644
index 0000000..f11dfe7
--- /dev/null
+++ b/src/critical_points.c
@@ -0,0 +1,126 @@
1#include "critical_points.h"
2#include <math.h>
3#include "da.h"
4
5static double
6angle_sweep (double a0, double a1, int ccw)
7{
8 double sweep = a1 - a0;
9
10 if (ccw) {
11 while (sweep <= 0)
12 sweep += 2*M_PI;
13 } else {
14 while (sweep >= 0)
15 sweep -= 2*M_PI;
16 }
17
18 // NOTE(cmmm): might need some eps
19
20 return sweep;
21}
22
23
24static struct vec2
25tangent_beg (struct vec2 p0, struct path_element *pe)
26{
27 ASSERT (pe);
28
29 struct vec2 p1 = { pe->x, pe->y };
30 if (~pe->flags & PEF_ARC)
31 return vec2_unit (vec2_sub (p1, p0));
32
33 struct vec2 c = { pe->i, pe->j };
34 double a0 = vec2_angle0 (vec2_sub (p0, c));
35 double a1 = vec2_angle0 (vec2_sub (p1, c));
36 double sweep = angle_sweep (a0, a1, pe->flags & PEF_CCW);
37 double sgn = sweep < 0 ? -1 : 1;
38 double a = a0;
39 return (struct vec2) { -sgn*sin (a), sgn*cos (a) };
40}
41
42static struct vec2
43tangent_end (struct vec2 p0, struct path_element *pe)
44{
45 ASSERT (pe);
46
47 struct vec2 p1 = { pe->x, pe->y };
48 if (~pe->flags & PEF_ARC)
49 return vec2_unit (vec2_sub (p1, p0));
50
51 struct vec2 c = { pe->i, pe->j };
52 double a0 = vec2_angle0 (vec2_sub (p0, c));
53 double a1 = vec2_angle0 (vec2_sub (p1, c));
54 double sweep = angle_sweep (a0, a1, pe->flags & PEF_CCW);
55 double sgn = sweep < 0 ? -1 : 1;
56 double a = a0 + sweep;
57 return (struct vec2) { -sgn*sin (a), sgn*cos (a) };
58}
59
60struct critical_points
61find_critical_points (const struct path *path)
62{
63 struct critical_points cps = {0};
64
65 if (path->count == 0)
66 return cps;
67
68 struct critical_point cp = {
69 .right = &path->items[0],
70 .r = {
71 .x = path->initial_x,
72 .y = path->initial_y,
73 }
74 };
75 if (~cp.right->flags & PEF_TRAVERSING) {
76 cp.type = CPT_BEG;
77 cp.t_right = tangent_beg (cp.r, cp.right);
78 DA_APPEND (cps, cp);
79 }
80
81 struct vec2 beg = cp.r;
82 for (uint32_t i = 1; i < path->count; ++i) {
83 cp.left = cp.right;
84 cp.right = &path->items[i];
85
86 cp.s = beg;
87 cp.r.x = cp.left->x;
88 cp.r.y = cp.left->y;
89
90 if (cp.left ->flags & PEF_TRAVERSING &&
91 cp.right->flags & PEF_TRAVERSING) {
92 goto next;
93 }
94
95 struct vec2 end = {
96 .x = cp.right->x,
97 .y = cp.right->y,
98 };
99
100 cp.t_left = tangent_end (beg, cp.left );
101 cp.t_right = tangent_beg (cp.r, cp.right);
102
103 double min_turn = M_PI/16;
104 double turn = vec2_angle (cp.t_left, cp.t_right);
105
106 if (cp.left ->flags & PEF_TRAVERSING) cp.type = CPT_IN;
107 else if (cp.right->flags & PEF_TRAVERSING) cp.type = CPT_OUT;
108 else if (turn < min_turn) goto next;
109 else cp.type = CPT_LOOP;
110
111 DA_APPEND (cps, cp);
112 next:
113 beg = cp.r;
114 }
115
116 cp.left = &DA_LAST (*path);
117 if (~cp.left->flags & PEF_TRAVERSING) {
118 cp.type = CPT_END;
119 cp.r.x = cp.left->x;
120 cp.r.y = cp.left->y;
121 cp.t_left = tangent_end (beg, cp.left);
122 DA_APPEND (cps, cp);
123 }
124
125 return cps;
126}