-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGeometryUtils.cpp
More file actions
268 lines (205 loc) · 7.93 KB
/
GeometryUtils.cpp
File metadata and controls
268 lines (205 loc) · 7.93 KB
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
#include "GeometryUtils.h"
#include "LinearAlgebra.h"
#include "SegmentedPath.h"
#include <iostream>
using namespace dawn;
using namespace std;
bool GeometryUtils::point_in_triangle(vec2f P, vec2f A, vec2f B, vec2f C) {
bool b0 = vec2f(P[0] - A[0], P[1] - A[1]).dot(vec2f(A[1] - B[1], B[0] - A[0])) > 0;
bool b1 = vec2f(P[0] - B[0], P[1] - B[1]).dot(vec2f(B[1] - C[1], C[0] - B[0])) > 0;
bool b2 = vec2f(P[0] - C[0], P[1] - C[1]).dot(vec2f(C[1] - A[1], A[0] - C[0])) > 0;
return (b0 == b1 && b1 == b2);
}
bool GeometryUtils::is_ear(int ai, int bi, int ci, const vec2farray &vertices) {
vec2f a = vertices[ai];
vec2f b = vertices[bi];
vec2f c = vertices[ci];
for (unsigned int i = 0; i < vertices.size(); i++) {
if (i != ai && i != bi && i != ci) {
vec2f p = vertices[i];
if (point_in_triangle(p, a, b, c)) {
return false;
}
}
}
return true;
}
void GeometryUtils::triangulate_ec(const vec2farray &vertices, std::vector<uint8_t> &indices) {
size_t triangles_length = 0;
size_t vertices_length = vertices.size();
if (vertices_length == 3) {
indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
} else if (vertices_length > 3) {
int l[vertices_length], r[vertices_length];
int i = 0;
for (i = 0; i < vertices_length; i++) {
l[i] = ((i - 1) + vertices_length) % vertices_length;
r[i] = ((i + 1) + vertices_length) % vertices_length;
}
int loops = 0;
i = 0;
while (triangles_length < (vertices_length - 2)) {
if (is_ear(l[i], i, r[i], vertices)) {
// Add triangle
indices.push_back(l[i]);
indices.push_back(i);
indices.push_back(r[i]);
triangles_length += 1;
l[ r[i] ] = l[i];
r[ l[i] ] = r[i];
}
i = r[i];
loops++;
if (loops > 1000) {
cerr << "Aborting triangulation!" << endl;
return;
}
}
}
}
void GeometryUtils::create_uvs(const vec2farray &vertices, vec2farray &uvs, vec4f uv) {
float x0, x1, y0, y1;
x0 = x1 = vertices[0][0];
y0 = y1 = vertices[0][1];
float u0 = uv[0];
float u1 = uv[1];
float v0 = uv[2];
float v1 = uv[3];
for (vec2farray::const_iterator itr = vertices.begin(); itr != vertices.end(); itr++) {
x0 = std::min((*itr)[0], x0);
x1 = std::max((*itr)[0], x1);
y0 = std::max((*itr)[1], y0);
y1 = std::min((*itr)[1], y1);
}
for (vec2farray::const_iterator itr = vertices.begin(); itr != vertices.end(); itr++) {
float u = lerp(u0, u1, ((*itr)[0] - x0) / (x1 - x0));
float v = lerp(v0, v1, ((*itr)[1] - y0) / (y1 - y0));
uvs.push_back(vec2f(u, v));
}
}
void GeometryUtils::arc(vec2farray &positions, float cx, float cy, float r, float start, float sweep)
{
float segments = 12; // TODO Calculate this based on r and sweep angle etc.
float degrees = (sweep) / (float)segments;
// TODO Should it start at 0 or should it skip first if positions.size() > 0 (as in cairo)
for (int i = 0; i < segments; i++) {
float x = cx + cos(degrees * (float)i + start) * r;
float y = cy + sin(degrees * (float)i + start) * r;
positions.push_back(vec2f(x, y));
}
}
vec2f getBezierPoint(vec2farray points, float t) {
size_t numPoints = points.size();
vec2f* tmp = new vec2f[numPoints];
for (unsigned int i = 0; i < numPoints; i++) {
tmp[i] = points[i];
}
int i = numPoints - 1;
while (i > 0) {
for (int k = 0; k < i; k++)
tmp[k] = tmp[k] + t * ( tmp[k+1] - tmp[k] );
i--;
}
vec2f answer = tmp[0];
delete[] tmp;
return answer;
}
void GeometryUtils::fill(const Path *path, vec2farray &positions, std::vector<uint8_t> &indices)
{
// TODO Clean up Bezier part
if (path->type() == CONSTANTS::SegmentedPath) {
const SegmentedPath *spath = static_cast<const SegmentedPath *>(path);
SegmentList segments = spath->segments();
positions.push_back(spath->start());
for (SegmentList::iterator itr = segments.begin(); itr != segments.end(); itr++) {
std::cout << "Adding BezierCurveToSegment" << std::endl;
vec2farray points = (*itr)->points();
points.insert(points.begin(), positions.back());
if (points.size() > 2) {
int interpolations = (points.size() - 1) * 20;
for (unsigned int i = 0; i < interpolations; i++) {
float t = (float)i / (float)interpolations;
positions.push_back(getBezierPoint(points, t));
}
}
positions.push_back(points.back());
}
}
cout << "triangulate" << endl;
triangulate_ec(positions, indices);
}
void GeometryUtils::stroke(const Path *path, float strokewidth, vec2farray &positions, std::vector<uint8_t> &indices)
{
float w2 = strokewidth * 0.5f;
if (path->type() == CONSTANTS::SegmentedPath) {
const SegmentedPath *spath = static_cast<const SegmentedPath *>(path);
vec2f start = spath->start();
SegmentList segments = spath->segments();
bool finish = spath->finish();
BezierCurveToSegment *last = NULL;
vec2f p0 = start;
for (SegmentList::iterator itr = segments.begin(); itr != segments.end(); itr++) {
std::cout << "Adding BezierCurveToSegment" << std::endl;
vec2farray points = (*itr)->points();
vec2f p1 = points.back();
vec2f delta = p1 - p0;
delta.normalize();
vec2f l = vec2f(-delta[1] * w2, delta[0] * w2);
vec2f r = vec2f( delta[1] * w2, -delta[0] * w2);
cout << "delta=" << delta[0] << ", " << delta[1] <<endl;
cout << "l=" << l[0] << ", " << l[1] <<endl;
cout << "r=" << r[0] << ", " << r[1] <<endl;
vec2f a = p0 + l;
vec2f b = p1 + l;
vec2f c = p1 + r;
vec2f d = p0 + r;
cout << "p0=" << p0[0] << ", " << p0[1] <<endl;
cout << "p1=" << p1[0] << ", " << p1[1] <<endl;
cout << "a=" << a[0] << ", " << a[1] <<endl;
cout << "b=" << b[0] << ", " << b[1] <<endl;
cout << "c=" << c[0] << ", " << c[1] <<endl;
cout << "d=" << d[0] << ", " << d[1] <<endl;
if (!finish && !last) {
cout << "Could add start cap" << p0[0] << ", " << p0[1] <<endl;
positions.push_back(a);
positions.push_back(p0 - delta * w2);
positions.push_back(d);
indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
}
uint8_t index = positions.size();
positions.push_back(a);
positions.push_back(b);
positions.push_back(c);
positions.push_back(d);
indices.push_back(index);
indices.push_back(index + 1);
indices.push_back(index + 2);
indices.push_back(index);
indices.push_back(index + 2);
indices.push_back(index + 3);
if (!finish && (itr + 1) == segments.end()) {
cout << "Could add end cap" << p1[0] << ", " << p1[1] <<endl;
positions.push_back(b);
positions.push_back(p1 + delta * w2);
positions.push_back(c);
indices.push_back(index + 4);
indices.push_back(index + 5);
indices.push_back(index + 6);
}
p0 = p1;
}
}
// fill(path, positions, indices);
/*
positions.push_back(vec2f(-1, 0));
positions.push_back(vec2f( 0, -1));
positions.push_back(vec2f( 1, 0));
indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
*/
}