Skip to content

Commit acb01df

Browse files
committed
Edge coloring by edge to edge distance - experimental version
1 parent dc36f71 commit acb01df

File tree

3 files changed

+289
-1
lines changed

3 files changed

+289
-1
lines changed

core/edge-coloring.cpp

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11

22
#include "edge-coloring.h"
33

4+
#include <cstdlib>
5+
#include <cmath>
6+
#include <cstring>
7+
#include <queue>
8+
#include "arithmetics.hpp"
9+
410
namespace msdfgen {
511

612
static bool isCorner(const Vector2 &aDir, const Vector2 &bDir, double crossThreshold) {
@@ -215,4 +221,279 @@ void edgeColoringInkTrap(Shape &shape, double angleThreshold, unsigned long long
215221
}
216222
}
217223

224+
// EDGE COLORING BY DISTANCE - EXPERIMENTAL IMPLEMENTATION - WORK IN PROGRESS
225+
#define MAX_RECOLOR_STEPS 16
226+
#define EDGE_DISTANCE_PRECISION 16
227+
228+
static double edgeToEdgeDistance(const EdgeSegment &a, const EdgeSegment &b, int precision) {
229+
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))
230+
return 0;
231+
double iFac = 1./precision;
232+
double minDistance = (b.point(0)-a.point(0)).length();
233+
for (int i = 0; i <= precision; ++i) {
234+
double t = iFac*i;
235+
double d = fabs(a.signedDistance(b.point(t), t).distance);
236+
minDistance = min(minDistance, d);
237+
}
238+
for (int i = 0; i <= precision; ++i) {
239+
double t = iFac*i;
240+
double d = fabs(b.signedDistance(a.point(t), t).distance);
241+
minDistance = min(minDistance, d);
242+
}
243+
return minDistance;
244+
}
245+
246+
static double splineToSplineDistance(EdgeSegment * const *edgeSegments, int aStart, int aEnd, int bStart, int bEnd, int precision) {
247+
double minDistance = fabs(SignedDistance::INFINITE.distance);
248+
for (int ai = aStart; ai < aEnd; ++ai)
249+
for (int bi = bStart; bi < bEnd && minDistance; ++bi) {
250+
double d = edgeToEdgeDistance(*edgeSegments[ai], *edgeSegments[bi], precision);
251+
minDistance = min(minDistance, d);
252+
}
253+
return minDistance;
254+
}
255+
256+
static void colorSecondDegreeGraph(int *coloring, const int * const *edgeMatrix, int vertexCount, unsigned long long seed) {
257+
for (int i = 0; i < vertexCount; ++i) {
258+
int possibleColors = 7;
259+
for (int j = 0; j < i; ++j) {
260+
if (edgeMatrix[i][j])
261+
possibleColors &= ~(1<<coloring[j]);
262+
}
263+
int color = 0;
264+
switch (possibleColors) {
265+
case 1:
266+
color = 0;
267+
break;
268+
case 2:
269+
color = 1;
270+
break;
271+
case 3:
272+
color = (int) seed&1;
273+
seed >>= 1;
274+
break;
275+
case 4:
276+
color = 2;
277+
break;
278+
case 5:
279+
color = ((int) seed+1&1)<<1;
280+
seed >>= 1;
281+
break;
282+
case 6:
283+
color = ((int) seed&1)+1;
284+
seed >>= 1;
285+
break;
286+
case 7:
287+
color = int((seed+i)%3);
288+
seed /= 3;
289+
break;
290+
}
291+
coloring[i] = color;
292+
}
293+
}
294+
295+
static int vertexPossibleColors(const int *coloring, const int *edgeVector, int vertexCount) {
296+
int usedColors = 0;
297+
for (int i = 0; i < vertexCount; ++i)
298+
if (edgeVector[i])
299+
usedColors |= 1<<coloring[i];
300+
return 7&~usedColors;
301+
}
302+
303+
static void uncolorSameNeighbors(std::queue<int> &uncolored, int *coloring, const int * const *edgeMatrix, int vertex, int vertexCount) {
304+
for (int i = vertex+1; i < vertexCount; ++i) {
305+
if (edgeMatrix[vertex][i] && coloring[i] == coloring[vertex]) {
306+
coloring[i] = -1;
307+
uncolored.push(i);
308+
}
309+
}
310+
for (int i = 0; i < vertex; ++i) {
311+
if (edgeMatrix[vertex][i] && coloring[i] == coloring[vertex]) {
312+
coloring[i] = -1;
313+
uncolored.push(i);
314+
}
315+
}
316+
}
317+
318+
static bool tryAddEdge(int *coloring, int * const *edgeMatrix, int vertexCount, int vertexA, int vertexB, int *coloringBuffer) {
319+
static const int FIRST_POSSIBLE_COLOR[8] = { -1, 0, 1, 0, 2, 2, 1, 0 };
320+
edgeMatrix[vertexA][vertexB] = 1;
321+
edgeMatrix[vertexB][vertexA] = 1;
322+
if (coloring[vertexA] != coloring[vertexB])
323+
return true;
324+
int bPossibleColors = vertexPossibleColors(coloring, edgeMatrix[vertexB], vertexCount);
325+
if (bPossibleColors) {
326+
coloring[vertexB] = FIRST_POSSIBLE_COLOR[bPossibleColors];
327+
return true;
328+
}
329+
memcpy(coloringBuffer, coloring, sizeof(int)*vertexCount);
330+
std::queue<int> uncolored;
331+
{
332+
int *coloring = coloringBuffer;
333+
coloring[vertexB] = FIRST_POSSIBLE_COLOR[7&~(1<<coloring[vertexA])];
334+
uncolorSameNeighbors(uncolored, coloring, edgeMatrix, vertexB, vertexCount);
335+
int step = 0;
336+
while (!uncolored.empty() && step < MAX_RECOLOR_STEPS) {
337+
int i = uncolored.front();
338+
uncolored.pop();
339+
int possibleColors = vertexPossibleColors(coloring, edgeMatrix[i], vertexCount);
340+
if (possibleColors) {
341+
coloring[i] = FIRST_POSSIBLE_COLOR[possibleColors];
342+
continue;
343+
}
344+
do {
345+
coloring[i] = step++%3;
346+
} while (edgeMatrix[i][vertexA] && coloring[i] == coloring[vertexA]);
347+
uncolorSameNeighbors(uncolored, coloring, edgeMatrix, i, vertexCount);
348+
}
349+
}
350+
if (!uncolored.empty()) {
351+
edgeMatrix[vertexA][vertexB] = 0;
352+
edgeMatrix[vertexB][vertexA] = 0;
353+
return false;
354+
}
355+
memcpy(coloring, coloringBuffer, sizeof(int)*vertexCount);
356+
return true;
357+
}
358+
359+
static int cmpDoublePtr(const void *a, const void *b) {
360+
return sign(**reinterpret_cast<const double * const *>(a)-**reinterpret_cast<const double * const *>(b));
361+
}
362+
363+
void edgeColoringByDistance(Shape &shape, double angleThreshold, unsigned long long seed) {
364+
365+
std::vector<EdgeSegment *> edgeSegments;
366+
std::vector<int> splineStarts;
367+
368+
double crossThreshold = sin(angleThreshold);
369+
std::vector<int> corners;
370+
for (std::vector<Contour>::iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
371+
if (!contour->edges.empty()) {
372+
// Identify corners
373+
corners.clear();
374+
Vector2 prevDirection = contour->edges.back()->direction(1);
375+
int index = 0;
376+
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge, ++index) {
377+
if (isCorner(prevDirection.normalize(), (*edge)->direction(0).normalize(), crossThreshold))
378+
corners.push_back(index);
379+
prevDirection = (*edge)->direction(1);
380+
}
381+
382+
splineStarts.push_back((int) edgeSegments.size());
383+
// Smooth contour
384+
if (corners.empty())
385+
for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge)
386+
edgeSegments.push_back(&**edge);
387+
// "Teardrop" case
388+
else if (corners.size() == 1) {
389+
int corner = corners[0];
390+
if (contour->edges.size() >= 3) {
391+
int m = (int) contour->edges.size();
392+
for (int i = 0; i < m; ++i) {
393+
if (i == m/2)
394+
splineStarts.push_back((int) edgeSegments.size());
395+
if (int(3+2.875*i/(m-1)-1.4375+.5)-3)
396+
edgeSegments.push_back(&*contour->edges[(corner+i)%m]);
397+
else
398+
contour->edges[(corner+i)%m]->color = WHITE;
399+
}
400+
} else if (contour->edges.size() >= 1) {
401+
// Less than three edge segments for three colors => edges must be split
402+
EdgeSegment *parts[7] = { };
403+
contour->edges[0]->splitInThirds(parts[0+3*corner], parts[1+3*corner], parts[2+3*corner]);
404+
if (contour->edges.size() >= 2) {
405+
contour->edges[1]->splitInThirds(parts[3-3*corner], parts[4-3*corner], parts[5-3*corner]);
406+
edgeSegments.push_back(parts[0]);
407+
edgeSegments.push_back(parts[1]);
408+
parts[2]->color = parts[3]->color = WHITE;
409+
splineStarts.push_back((int) edgeSegments.size());
410+
edgeSegments.push_back(parts[4]);
411+
edgeSegments.push_back(parts[5]);
412+
} else {
413+
edgeSegments.push_back(parts[0]);
414+
parts[1]->color = WHITE;
415+
splineStarts.push_back((int) edgeSegments.size());
416+
edgeSegments.push_back(parts[2]);
417+
}
418+
contour->edges.clear();
419+
for (int i = 0; parts[i]; ++i)
420+
contour->edges.push_back(EdgeHolder(parts[i]));
421+
}
422+
}
423+
// Multiple corners
424+
else {
425+
int cornerCount = (int) corners.size();
426+
int spline = 0;
427+
int start = corners[0];
428+
int m = (int) contour->edges.size();
429+
for (int i = 0; i < m; ++i) {
430+
int index = (start+i)%m;
431+
if (spline+1 < cornerCount && corners[spline+1] == index) {
432+
splineStarts.push_back((int) edgeSegments.size());
433+
++spline;
434+
}
435+
edgeSegments.push_back(&*contour->edges[index]);
436+
}
437+
}
438+
}
439+
splineStarts.push_back((int) edgeSegments.size());
440+
441+
int segmentCount = (int) edgeSegments.size();
442+
int splineCount = (int) splineStarts.size()-1;
443+
if (!splineCount)
444+
return;
445+
446+
std::vector<double> distanceMatrixStorage(splineCount*splineCount);
447+
std::vector<double *> distanceMatrix(splineCount);
448+
for (int i = 0; i < splineCount; ++i)
449+
distanceMatrix[i] = &distanceMatrixStorage[i*splineCount];
450+
const double *distanceMatrixBase = &distanceMatrixStorage[0];
451+
452+
for (int i = 0; i < splineCount; ++i) {
453+
distanceMatrix[i][i] = -1;
454+
for (int j = i+1; j < splineCount; ++j) {
455+
double dist = splineToSplineDistance(&edgeSegments[0], splineStarts[i], splineStarts[i+1], splineStarts[j], splineStarts[j+1], EDGE_DISTANCE_PRECISION);
456+
distanceMatrix[i][j] = dist;
457+
distanceMatrix[j][i] = dist;
458+
}
459+
}
460+
461+
std::vector<const double *> graphEdgeDistances;
462+
graphEdgeDistances.reserve(splineCount*(splineCount-1)/2);
463+
for (int i = 0; i < splineCount; ++i)
464+
for (int j = i+1; j < splineCount; ++j)
465+
graphEdgeDistances.push_back(&distanceMatrix[i][j]);
466+
int graphEdgeCount = (int) graphEdgeDistances.size();
467+
if (!graphEdgeDistances.empty())
468+
qsort(&graphEdgeDistances[0], graphEdgeDistances.size(), sizeof(const double *), &cmpDoublePtr);
469+
470+
std::vector<int> edgeMatrixStorage(splineCount*splineCount);
471+
std::vector<int *> edgeMatrix(splineCount);
472+
for (int i = 0; i < splineCount; ++i)
473+
edgeMatrix[i] = &edgeMatrixStorage[i*splineCount];
474+
int nextEdge = 0;
475+
for (; nextEdge < graphEdgeCount && !*graphEdgeDistances[nextEdge]; ++nextEdge) {
476+
int elem = graphEdgeDistances[nextEdge]-distanceMatrixBase;
477+
int row = elem/splineCount;
478+
int col = elem%splineCount;
479+
edgeMatrix[row][col] = 1;
480+
edgeMatrix[col][row] = 1;
481+
}
482+
483+
std::vector<int> coloring(2*splineCount);
484+
colorSecondDegreeGraph(&coloring[0], &edgeMatrix[0], splineCount, seed);
485+
for (; nextEdge < graphEdgeCount; ++nextEdge) {
486+
int elem = graphEdgeDistances[nextEdge]-distanceMatrixBase;
487+
tryAddEdge(&coloring[0], &edgeMatrix[0], splineCount, elem/splineCount, elem%splineCount, &coloring[splineCount]);
488+
}
489+
490+
const EdgeColor colors[3] = { YELLOW, CYAN, MAGENTA };
491+
int spline = -1;
492+
for (int i = 0; i < segmentCount; ++i) {
493+
if (splineStarts[spline+1] == i)
494+
++spline;
495+
edgeSegments[i]->color = colors[coloring[spline]];
496+
}
497+
}
498+
218499
}

core/edge-coloring.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ void edgeColoringSimple(Shape &shape, double angleThreshold, unsigned long long
2020
*/
2121
void edgeColoringInkTrap(Shape &shape, double angleThreshold, unsigned long long seed = 0);
2222

23+
/** The alternative coloring by distance tries to use different colors for edges that are close together.
24+
* This should theoretically be the best strategy on average. However, since it needs to compute the distance
25+
* between all pairs of edges, and perform a graph optimization task, it is much slower than the rest.
26+
*/
27+
void edgeColoringByDistance(Shape &shape, double angleThreshold, unsigned long long seed = 0);
28+
2329
}

main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ static const char *helpText =
304304
"\tSets the scale used to convert shape units to pixels asymmetrically.\n"
305305
" -autoframe\n"
306306
"\tAutomatically scales (unless specified) and translates the shape to fit.\n"
307-
" -coloringstrategy <simple / inktrap>\n"
307+
" -coloringstrategy <simple / inktrap / distance>\n"
308308
"\tSelects the strategy of the edge coloring heuristic.\n"
309309
" -distanceshift <shift>\n"
310310
"\tShifts all normalized distances in the output distance field by this value.\n"
@@ -715,6 +715,7 @@ int main(int argc, const char * const *argv) {
715715
ARG_CASE("-coloringstrategy", 1) {
716716
if (!strcmp(argv[argPos+1], "simple")) edgeColoring = edgeColoringSimple;
717717
else if (!strcmp(argv[argPos+1], "inktrap")) edgeColoring = edgeColoringInkTrap;
718+
else if (!strcmp(argv[argPos+1], "distance")) edgeColoring = edgeColoringByDistance;
718719
else
719720
puts("Unknown coloring strategy specified.");
720721
argPos += 2;

0 commit comments

Comments
 (0)