forked from Earthmark/msdfgen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
edge-coloring.cpp
499 lines (468 loc) · 20.6 KB
/
edge-coloring.cpp
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
#include "edge-coloring.h"
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <queue>
#include "arithmetics.hpp"
namespace msdfgen {
static bool isCorner(const Vector2 &aDir, const Vector2 &bDir, double crossThreshold) {
return dotProduct(aDir, bDir) <= 0 || fabs(crossProduct(aDir, bDir)) > crossThreshold;
}
static double estimateEdgeLength(const EdgeSegment *edge) {
double len = 0;
Point2 prev = edge->point(0);
for (int i = 1; i <= MSDFGEN_EDGE_LENGTH_PRECISION; ++i) {
Point2 cur = edge->point(1./MSDFGEN_EDGE_LENGTH_PRECISION*i);
len += (cur-prev).length();
prev = cur;
}
return len;
}
static void switchColor(EdgeColor &color, unsigned long long &seed, EdgeColor banned = BLACK) {
EdgeColor combined = EdgeColor(color&banned);
if (combined == RED || combined == GREEN || combined == BLUE) {
color = EdgeColor(combined^WHITE);
return;
}
if (color == BLACK || color == WHITE) {
static const EdgeColor start[3] = { CYAN, MAGENTA, YELLOW };
color = start[seed%3];
seed /= 3;
return;
}
int shifted = color<<(1+(seed&1));
color = EdgeColor((shifted|shifted>>3)&WHITE);
seed >>= 1;
}
void edgeColoringSimple(Shape &shape, double angleThreshold, unsigned long long seed) {
double crossThreshold = sin(angleThreshold);
std::vector<int> corners;
for (std::vector<Contour>::iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
// Identify corners
corners.clear();
if (!contour->edges.empty()) {
Vector2 prevDirection = contour->edges.back()->direction(1);
int index = 0;
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge, ++index) {
if (isCorner(prevDirection.normalize(), (*edge)->direction(0).normalize(), crossThreshold))
corners.push_back(index);
prevDirection = (*edge)->direction(1);
}
}
// Smooth contour
if (corners.empty())
for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge)
(*edge)->color = WHITE;
// "Teardrop" case
else if (corners.size() == 1) {
EdgeColor colors[3] = { WHITE, WHITE };
switchColor(colors[0], seed);
switchColor(colors[2] = colors[0], seed);
int corner = corners[0];
if (contour->edges.size() >= 3) {
int m = (int) contour->edges.size();
for (int i = 0; i < m; ++i)
contour->edges[(corner+i)%m]->color = (colors+1)[int(3+2.875*i/(m-1)-1.4375+.5)-3];
} else if (contour->edges.size() >= 1) {
// Less than three edge segments for three colors => edges must be split
EdgeSegment *parts[7] = { };
contour->edges[0]->splitInThirds(parts[0+3*corner], parts[1+3*corner], parts[2+3*corner]);
if (contour->edges.size() >= 2) {
contour->edges[1]->splitInThirds(parts[3-3*corner], parts[4-3*corner], parts[5-3*corner]);
parts[0]->color = parts[1]->color = colors[0];
parts[2]->color = parts[3]->color = colors[1];
parts[4]->color = parts[5]->color = colors[2];
} else {
parts[0]->color = colors[0];
parts[1]->color = colors[1];
parts[2]->color = colors[2];
}
contour->edges.clear();
for (int i = 0; parts[i]; ++i)
contour->edges.push_back(EdgeHolder(parts[i]));
}
}
// Multiple corners
else {
int cornerCount = (int) corners.size();
int spline = 0;
int start = corners[0];
int m = (int) contour->edges.size();
EdgeColor color = WHITE;
switchColor(color, seed);
EdgeColor initialColor = color;
for (int i = 0; i < m; ++i) {
int index = (start+i)%m;
if (spline+1 < cornerCount && corners[spline+1] == index) {
++spline;
switchColor(color, seed, EdgeColor((spline == cornerCount-1)*initialColor));
}
contour->edges[index]->color = color;
}
}
}
}
struct EdgeColoringInkTrapCorner {
int index;
double prevEdgeLengthEstimate;
bool minor;
EdgeColor color;
};
void edgeColoringInkTrap(Shape &shape, double angleThreshold, unsigned long long seed) {
typedef EdgeColoringInkTrapCorner Corner;
double crossThreshold = sin(angleThreshold);
std::vector<Corner> corners;
for (std::vector<Contour>::iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
// Identify corners
double splineLength = 0;
corners.clear();
if (!contour->edges.empty()) {
Vector2 prevDirection = contour->edges.back()->direction(1);
int index = 0;
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge, ++index) {
if (isCorner(prevDirection.normalize(), (*edge)->direction(0).normalize(), crossThreshold)) {
Corner corner = { index, splineLength };
corners.push_back(corner);
splineLength = 0;
}
splineLength += estimateEdgeLength(*edge);
prevDirection = (*edge)->direction(1);
}
}
// Smooth contour
if (corners.empty())
for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge)
(*edge)->color = WHITE;
// "Teardrop" case
else if (corners.size() == 1) {
EdgeColor colors[3] = { WHITE, WHITE };
switchColor(colors[0], seed);
switchColor(colors[2] = colors[0], seed);
int corner = corners[0].index;
if (contour->edges.size() >= 3) {
int m = (int) contour->edges.size();
for (int i = 0; i < m; ++i)
contour->edges[(corner+i)%m]->color = (colors+1)[int(3+2.875*i/(m-1)-1.4375+.5)-3];
} else if (contour->edges.size() >= 1) {
// Less than three edge segments for three colors => edges must be split
EdgeSegment *parts[7] = { };
contour->edges[0]->splitInThirds(parts[0+3*corner], parts[1+3*corner], parts[2+3*corner]);
if (contour->edges.size() >= 2) {
contour->edges[1]->splitInThirds(parts[3-3*corner], parts[4-3*corner], parts[5-3*corner]);
parts[0]->color = parts[1]->color = colors[0];
parts[2]->color = parts[3]->color = colors[1];
parts[4]->color = parts[5]->color = colors[2];
} else {
parts[0]->color = colors[0];
parts[1]->color = colors[1];
parts[2]->color = colors[2];
}
contour->edges.clear();
for (int i = 0; parts[i]; ++i)
contour->edges.push_back(EdgeHolder(parts[i]));
}
}
// Multiple corners
else {
int cornerCount = (int) corners.size();
int majorCornerCount = cornerCount;
if (cornerCount > 3) {
corners.begin()->prevEdgeLengthEstimate += splineLength;
for (int i = 0; i < cornerCount; ++i) {
if (
corners[i].prevEdgeLengthEstimate > corners[(i+1)%cornerCount].prevEdgeLengthEstimate &&
corners[(i+1)%cornerCount].prevEdgeLengthEstimate < corners[(i+2)%cornerCount].prevEdgeLengthEstimate
) {
corners[i].minor = true;
--majorCornerCount;
}
}
}
EdgeColor color = WHITE;
EdgeColor initialColor = BLACK;
for (int i = 0; i < cornerCount; ++i) {
if (!corners[i].minor) {
--majorCornerCount;
switchColor(color, seed, EdgeColor(!majorCornerCount*initialColor));
corners[i].color = color;
if (!initialColor)
initialColor = color;
}
}
for (int i = 0; i < cornerCount; ++i) {
if (corners[i].minor) {
EdgeColor nextColor = corners[(i+1)%cornerCount].color;
corners[i].color = EdgeColor((color&nextColor)^WHITE);
} else
color = corners[i].color;
}
int spline = 0;
int start = corners[0].index;
color = corners[0].color;
int m = (int) contour->edges.size();
for (int i = 0; i < m; ++i) {
int index = (start+i)%m;
if (spline+1 < cornerCount && corners[spline+1].index == index)
color = corners[++spline].color;
contour->edges[index]->color = color;
}
}
}
}
// EDGE COLORING BY DISTANCE - EXPERIMENTAL IMPLEMENTATION - WORK IN PROGRESS
#define MAX_RECOLOR_STEPS 16
#define EDGE_DISTANCE_PRECISION 16
static double edgeToEdgeDistance(const EdgeSegment &a, const EdgeSegment &b, int precision) {
if (a.point(0) == b.point(0) || a.point(0) == b.point(1) || a.point(1) == b.point(0) || a.point(1) == b.point(1))
return 0;
double iFac = 1./precision;
double minDistance = (b.point(0)-a.point(0)).length();
for (int i = 0; i <= precision; ++i) {
double t = iFac*i;
double d = fabs(a.signedDistance(b.point(t), t).distance);
minDistance = min(minDistance, d);
}
for (int i = 0; i <= precision; ++i) {
double t = iFac*i;
double d = fabs(b.signedDistance(a.point(t), t).distance);
minDistance = min(minDistance, d);
}
return minDistance;
}
static double splineToSplineDistance(EdgeSegment * const *edgeSegments, int aStart, int aEnd, int bStart, int bEnd, int precision) {
double minDistance = fabs(SignedDistance::INFINITE.distance);
for (int ai = aStart; ai < aEnd; ++ai)
for (int bi = bStart; bi < bEnd && minDistance; ++bi) {
double d = edgeToEdgeDistance(*edgeSegments[ai], *edgeSegments[bi], precision);
minDistance = min(minDistance, d);
}
return minDistance;
}
static void colorSecondDegreeGraph(int *coloring, const int * const *edgeMatrix, int vertexCount, unsigned long long seed) {
for (int i = 0; i < vertexCount; ++i) {
int possibleColors = 7;
for (int j = 0; j < i; ++j) {
if (edgeMatrix[i][j])
possibleColors &= ~(1<<coloring[j]);
}
int color = 0;
switch (possibleColors) {
case 1:
color = 0;
break;
case 2:
color = 1;
break;
case 3:
color = (int) seed&1;
seed >>= 1;
break;
case 4:
color = 2;
break;
case 5:
color = ((int) seed+1&1)<<1;
seed >>= 1;
break;
case 6:
color = ((int) seed&1)+1;
seed >>= 1;
break;
case 7:
color = int((seed+i)%3);
seed /= 3;
break;
}
coloring[i] = color;
}
}
static int vertexPossibleColors(const int *coloring, const int *edgeVector, int vertexCount) {
int usedColors = 0;
for (int i = 0; i < vertexCount; ++i)
if (edgeVector[i])
usedColors |= 1<<coloring[i];
return 7&~usedColors;
}
static void uncolorSameNeighbors(std::queue<int> &uncolored, int *coloring, const int * const *edgeMatrix, int vertex, int vertexCount) {
for (int i = vertex+1; i < vertexCount; ++i) {
if (edgeMatrix[vertex][i] && coloring[i] == coloring[vertex]) {
coloring[i] = -1;
uncolored.push(i);
}
}
for (int i = 0; i < vertex; ++i) {
if (edgeMatrix[vertex][i] && coloring[i] == coloring[vertex]) {
coloring[i] = -1;
uncolored.push(i);
}
}
}
static bool tryAddEdge(int *coloring, int * const *edgeMatrix, int vertexCount, int vertexA, int vertexB, int *coloringBuffer) {
static const int FIRST_POSSIBLE_COLOR[8] = { -1, 0, 1, 0, 2, 2, 1, 0 };
edgeMatrix[vertexA][vertexB] = 1;
edgeMatrix[vertexB][vertexA] = 1;
if (coloring[vertexA] != coloring[vertexB])
return true;
int bPossibleColors = vertexPossibleColors(coloring, edgeMatrix[vertexB], vertexCount);
if (bPossibleColors) {
coloring[vertexB] = FIRST_POSSIBLE_COLOR[bPossibleColors];
return true;
}
memcpy(coloringBuffer, coloring, sizeof(int)*vertexCount);
std::queue<int> uncolored;
{
int *coloring = coloringBuffer;
coloring[vertexB] = FIRST_POSSIBLE_COLOR[7&~(1<<coloring[vertexA])];
uncolorSameNeighbors(uncolored, coloring, edgeMatrix, vertexB, vertexCount);
int step = 0;
while (!uncolored.empty() && step < MAX_RECOLOR_STEPS) {
int i = uncolored.front();
uncolored.pop();
int possibleColors = vertexPossibleColors(coloring, edgeMatrix[i], vertexCount);
if (possibleColors) {
coloring[i] = FIRST_POSSIBLE_COLOR[possibleColors];
continue;
}
do {
coloring[i] = step++%3;
} while (edgeMatrix[i][vertexA] && coloring[i] == coloring[vertexA]);
uncolorSameNeighbors(uncolored, coloring, edgeMatrix, i, vertexCount);
}
}
if (!uncolored.empty()) {
edgeMatrix[vertexA][vertexB] = 0;
edgeMatrix[vertexB][vertexA] = 0;
return false;
}
memcpy(coloring, coloringBuffer, sizeof(int)*vertexCount);
return true;
}
static int cmpDoublePtr(const void *a, const void *b) {
return sign(**reinterpret_cast<const double * const *>(a)-**reinterpret_cast<const double * const *>(b));
}
void edgeColoringByDistance(Shape &shape, double angleThreshold, unsigned long long seed) {
std::vector<EdgeSegment *> edgeSegments;
std::vector<int> splineStarts;
double crossThreshold = sin(angleThreshold);
std::vector<int> corners;
for (std::vector<Contour>::iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
if (!contour->edges.empty()) {
// Identify corners
corners.clear();
Vector2 prevDirection = contour->edges.back()->direction(1);
int index = 0;
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge, ++index) {
if (isCorner(prevDirection.normalize(), (*edge)->direction(0).normalize(), crossThreshold))
corners.push_back(index);
prevDirection = (*edge)->direction(1);
}
splineStarts.push_back((int) edgeSegments.size());
// Smooth contour
if (corners.empty())
for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge)
edgeSegments.push_back(&**edge);
// "Teardrop" case
else if (corners.size() == 1) {
int corner = corners[0];
if (contour->edges.size() >= 3) {
int m = (int) contour->edges.size();
for (int i = 0; i < m; ++i) {
if (i == m/2)
splineStarts.push_back((int) edgeSegments.size());
if (int(3+2.875*i/(m-1)-1.4375+.5)-3)
edgeSegments.push_back(&*contour->edges[(corner+i)%m]);
else
contour->edges[(corner+i)%m]->color = WHITE;
}
} else if (contour->edges.size() >= 1) {
// Less than three edge segments for three colors => edges must be split
EdgeSegment *parts[7] = { };
contour->edges[0]->splitInThirds(parts[0+3*corner], parts[1+3*corner], parts[2+3*corner]);
if (contour->edges.size() >= 2) {
contour->edges[1]->splitInThirds(parts[3-3*corner], parts[4-3*corner], parts[5-3*corner]);
edgeSegments.push_back(parts[0]);
edgeSegments.push_back(parts[1]);
parts[2]->color = parts[3]->color = WHITE;
splineStarts.push_back((int) edgeSegments.size());
edgeSegments.push_back(parts[4]);
edgeSegments.push_back(parts[5]);
} else {
edgeSegments.push_back(parts[0]);
parts[1]->color = WHITE;
splineStarts.push_back((int) edgeSegments.size());
edgeSegments.push_back(parts[2]);
}
contour->edges.clear();
for (int i = 0; parts[i]; ++i)
contour->edges.push_back(EdgeHolder(parts[i]));
}
}
// Multiple corners
else {
int cornerCount = (int) corners.size();
int spline = 0;
int start = corners[0];
int m = (int) contour->edges.size();
for (int i = 0; i < m; ++i) {
int index = (start+i)%m;
if (spline+1 < cornerCount && corners[spline+1] == index) {
splineStarts.push_back((int) edgeSegments.size());
++spline;
}
edgeSegments.push_back(&*contour->edges[index]);
}
}
}
splineStarts.push_back((int) edgeSegments.size());
int segmentCount = (int) edgeSegments.size();
int splineCount = (int) splineStarts.size()-1;
if (!splineCount)
return;
std::vector<double> distanceMatrixStorage(splineCount*splineCount);
std::vector<double *> distanceMatrix(splineCount);
for (int i = 0; i < splineCount; ++i)
distanceMatrix[i] = &distanceMatrixStorage[i*splineCount];
const double *distanceMatrixBase = &distanceMatrixStorage[0];
for (int i = 0; i < splineCount; ++i) {
distanceMatrix[i][i] = -1;
for (int j = i+1; j < splineCount; ++j) {
double dist = splineToSplineDistance(&edgeSegments[0], splineStarts[i], splineStarts[i+1], splineStarts[j], splineStarts[j+1], EDGE_DISTANCE_PRECISION);
distanceMatrix[i][j] = dist;
distanceMatrix[j][i] = dist;
}
}
std::vector<const double *> graphEdgeDistances;
graphEdgeDistances.reserve(splineCount*(splineCount-1)/2);
for (int i = 0; i < splineCount; ++i)
for (int j = i+1; j < splineCount; ++j)
graphEdgeDistances.push_back(&distanceMatrix[i][j]);
int graphEdgeCount = (int) graphEdgeDistances.size();
if (!graphEdgeDistances.empty())
qsort(&graphEdgeDistances[0], graphEdgeDistances.size(), sizeof(const double *), &cmpDoublePtr);
std::vector<int> edgeMatrixStorage(splineCount*splineCount);
std::vector<int *> edgeMatrix(splineCount);
for (int i = 0; i < splineCount; ++i)
edgeMatrix[i] = &edgeMatrixStorage[i*splineCount];
int nextEdge = 0;
for (; nextEdge < graphEdgeCount && !*graphEdgeDistances[nextEdge]; ++nextEdge) {
int elem = (int) (graphEdgeDistances[nextEdge]-distanceMatrixBase);
int row = elem/splineCount;
int col = elem%splineCount;
edgeMatrix[row][col] = 1;
edgeMatrix[col][row] = 1;
}
std::vector<int> coloring(2*splineCount);
colorSecondDegreeGraph(&coloring[0], &edgeMatrix[0], splineCount, seed);
for (; nextEdge < graphEdgeCount; ++nextEdge) {
int elem = (int) (graphEdgeDistances[nextEdge]-distanceMatrixBase);
tryAddEdge(&coloring[0], &edgeMatrix[0], splineCount, elem/splineCount, elem%splineCount, &coloring[splineCount]);
}
const EdgeColor colors[3] = { YELLOW, CYAN, MAGENTA };
int spline = -1;
for (int i = 0; i < segmentCount; ++i) {
if (splineStarts[spline+1] == i)
++spline;
edgeSegments[i]->color = colors[coloring[spline]];
}
}
}