| 1 | |
| 2 | #include "edge-coloring.h" |
| 3 | |
| 4 | #include <cstdlib> |
| 5 | #include <cmath> |
| 6 | #include <cstring> |
| 7 | #include <cfloat> |
| 8 | #include <vector> |
| 9 | #include <queue> |
| 10 | #include "arithmetics.hpp" |
| 11 | |
| 12 | namespace msdfgen { |
| 13 | |
| 14 | /** |
| 15 | * For each position < n, this function will return -1, 0, or 1, |
| 16 | * depending on whether the position is closer to the beginning, middle, or end, respectively. |
| 17 | * It is guaranteed that the output will be balanced in that the total for positions 0 through n-1 will be zero. |
| 18 | */ |
| 19 | static int symmetricalTrichotomy(int position, int n) { |
| 20 | return int(3+2.875*position/(n-1)-1.4375+.5)-3; |
| 21 | } |
| 22 | |
| 23 | static bool isCorner(const Vector2 &aDir, const Vector2 &bDir, double crossThreshold) { |
| 24 | return dotProduct(aDir, bDir) <= 0 || fabs(crossProduct(aDir, bDir)) > crossThreshold; |
| 25 | } |
| 26 | |
| 27 | static double estimateEdgeLength(const EdgeSegment *edge) { |
| 28 | double len = 0; |
| 29 | Point2 prev = edge->point(0); |
| 30 | for (int i = 1; i <= MSDFGEN_EDGE_LENGTH_PRECISION; ++i) { |
| 31 | Point2 cur = edge->point(1./MSDFGEN_EDGE_LENGTH_PRECISION*i); |
| 32 | len += (cur-prev).length(); |
| 33 | prev = cur; |
| 34 | } |
| 35 | return len; |
| 36 | } |
| 37 | |
| 38 | static int seedExtract2(unsigned long long &seed) { |
| 39 | int v = int(seed)&1; |
| 40 | seed >>= 1; |
| 41 | return v; |
| 42 | } |
| 43 | |
| 44 | static int seedExtract3(unsigned long long &seed) { |
| 45 | int v = int(seed%3); |
| 46 | seed /= 3; |
| 47 | return v; |
| 48 | } |
| 49 | |
| 50 | static EdgeColor initColor(unsigned long long &seed) { |
| 51 | static const EdgeColor colors[3] = { CYAN, MAGENTA, YELLOW }; |
| 52 | return colors[seedExtract3(seed)]; |
| 53 | } |
| 54 | |
| 55 | static void switchColor(EdgeColor &color, unsigned long long &seed) { |
| 56 | int shifted = color<<(1+seedExtract2(seed)); |
| 57 | color = EdgeColor((shifted|shifted>>3)&WHITE); |
| 58 | } |
| 59 | |
| 60 | static void switchColor(EdgeColor &color, unsigned long long &seed, EdgeColor banned) { |
| 61 | EdgeColor combined = EdgeColor(color&banned); |
| 62 | if (combined == RED || combined == GREEN || combined == BLUE) |
| 63 | color = EdgeColor(combined^WHITE); |
| 64 | else |
| 65 | switchColor(color, seed); |
| 66 | } |
| 67 | |
| 68 | void edgeColoringSimple(Shape &shape, double angleThreshold, unsigned long long seed) { |
| 69 | double crossThreshold = sin(angleThreshold); |
| 70 | EdgeColor color = initColor(seed); |
| 71 | std::vector<int> corners; |
| 72 | for (std::vector<Contour>::iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) { |
| 73 | if (contour->edges.empty()) |
| 74 | continue; |
| 75 | { // Identify corners |
| 76 | corners.clear(); |
| 77 | Vector2 prevDirection = contour->edges.back()->direction(1); |
| 78 | int index = 0; |
| 79 | for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge, ++index) { |
| 80 | if (isCorner(prevDirection.normalize(), (*edge)->direction(0).normalize(), crossThreshold)) |
| 81 | corners.push_back(index); |
| 82 | prevDirection = (*edge)->direction(1); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Smooth contour |
| 87 | if (corners.empty()) { |
| 88 | switchColor(color, seed); |
| 89 | for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) |
| 90 | (*edge)->color = color; |
| 91 | } |
| 92 | // "Teardrop" case |
| 93 | else if (corners.size() == 1) { |
| 94 | EdgeColor colors[3]; |
| 95 | switchColor(color, seed); |
| 96 | colors[0] = color; |
| 97 | colors[1] = WHITE; |
| 98 | switchColor(color, seed); |
| 99 | colors[2] = color; |
| 100 | int corner = corners[0]; |
| 101 | if (contour->edges.size() >= 3) { |
| 102 | int m = (int) contour->edges.size(); |
| 103 | for (int i = 0; i < m; ++i) |
| 104 | contour->edges[(corner+i)%m]->color = colors[1+symmetricalTrichotomy(i, m)]; |
| 105 | } else if (contour->edges.size() >= 1) { |
| 106 | // Less than three edge segments for three colors => edges must be split |
| 107 | EdgeSegment *parts[7] = { }; |
| 108 | contour->edges[0]->splitInThirds(parts[0+3*corner], parts[1+3*corner], parts[2+3*corner]); |
| 109 | if (contour->edges.size() >= 2) { |
| 110 | contour->edges[1]->splitInThirds(parts[3-3*corner], parts[4-3*corner], parts[5-3*corner]); |
| 111 | parts[0]->color = parts[1]->color = colors[0]; |
| 112 | parts[2]->color = parts[3]->color = colors[1]; |
| 113 | parts[4]->color = parts[5]->color = colors[2]; |
| 114 | } else { |
| 115 | parts[0]->color = colors[0]; |
| 116 | parts[1]->color = colors[1]; |
| 117 | parts[2]->color = colors[2]; |
| 118 | } |
| 119 | contour->edges.clear(); |
| 120 | for (int i = 0; parts[i]; ++i) |
| 121 | contour->edges.push_back(EdgeHolder(parts[i])); |
| 122 | } |
| 123 | } |
| 124 | // Multiple corners |
| 125 | else { |
| 126 | int cornerCount = (int) corners.size(); |
| 127 | int spline = 0; |
| 128 | int start = corners[0]; |
| 129 | int m = (int) contour->edges.size(); |
| 130 | switchColor(color, seed); |
| 131 | EdgeColor initialColor = color; |
| 132 | for (int i = 0; i < m; ++i) { |
| 133 | int index = (start+i)%m; |
| 134 | if (spline+1 < cornerCount && corners[spline+1] == index) { |
| 135 | ++spline; |
| 136 | switchColor(color, seed, EdgeColor((spline == cornerCount-1)*initialColor)); |
| 137 | } |
| 138 | contour->edges[index]->color = color; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | struct EdgeColoringInkTrapCorner { |
| 145 | int index; |
| 146 | double prevEdgeLengthEstimate; |
| 147 | bool minor; |
| 148 | EdgeColor color; |
| 149 | }; |
| 150 | |
| 151 | void edgeColoringInkTrap(Shape &shape, double angleThreshold, unsigned long long seed) { |
| 152 | typedef EdgeColoringInkTrapCorner Corner; |
| 153 | double crossThreshold = sin(angleThreshold); |
| 154 | EdgeColor color = initColor(seed); |
| 155 | std::vector<Corner> corners; |
| 156 | for (std::vector<Contour>::iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) { |
| 157 | if (contour->edges.empty()) |
| 158 | continue; |
| 159 | double splineLength = 0; |
| 160 | { // Identify corners |
| 161 | corners.clear(); |
| 162 | Vector2 prevDirection = contour->edges.back()->direction(1); |
| 163 | int index = 0; |
| 164 | for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge, ++index) { |
| 165 | if (isCorner(prevDirection.normalize(), (*edge)->direction(0).normalize(), crossThreshold)) { |
| 166 | Corner corner = { index, splineLength }; |
| 167 | corners.push_back(corner); |
| 168 | splineLength = 0; |
| 169 | } |
| 170 | splineLength += estimateEdgeLength(*edge); |
| 171 | prevDirection = (*edge)->direction(1); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Smooth contour |
| 176 | if (corners.empty()) { |
| 177 | switchColor(color, seed); |
| 178 | for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) |
| 179 | (*edge)->color = color; |
| 180 | } |
| 181 | // "Teardrop" case |
| 182 | else if (corners.size() == 1) { |
| 183 | EdgeColor colors[3]; |
| 184 | switchColor(color, seed); |
| 185 | colors[0] = color; |
| 186 | colors[1] = WHITE; |
| 187 | switchColor(color, seed); |
| 188 | colors[2] = color; |
| 189 | int corner = corners[0].index; |
| 190 | if (contour->edges.size() >= 3) { |
| 191 | int m = (int) contour->edges.size(); |
| 192 | for (int i = 0; i < m; ++i) |
| 193 | contour->edges[(corner+i)%m]->color = colors[1+symmetricalTrichotomy(i, m)]; |
| 194 | } else if (contour->edges.size() >= 1) { |
| 195 | // Less than three edge segments for three colors => edges must be split |
| 196 | EdgeSegment *parts[7] = { }; |
| 197 | contour->edges[0]->splitInThirds(parts[0+3*corner], parts[1+3*corner], parts[2+3*corner]); |
| 198 | if (contour->edges.size() >= 2) { |
| 199 | contour->edges[1]->splitInThirds(parts[3-3*corner], parts[4-3*corner], parts[5-3*corner]); |
| 200 | parts[0]->color = parts[1]->color = colors[0]; |
| 201 | parts[2]->color = parts[3]->color = colors[1]; |
| 202 | parts[4]->color = parts[5]->color = colors[2]; |
| 203 | } else { |
| 204 | parts[0]->color = colors[0]; |
| 205 | parts[1]->color = colors[1]; |
| 206 | parts[2]->color = colors[2]; |
| 207 | } |
| 208 | contour->edges.clear(); |
| 209 | for (int i = 0; parts[i]; ++i) |
| 210 | contour->edges.push_back(EdgeHolder(parts[i])); |
| 211 | } |
| 212 | } |
| 213 | // Multiple corners |
| 214 | else { |
| 215 | int cornerCount = (int) corners.size(); |
| 216 | int majorCornerCount = cornerCount; |
| 217 | if (cornerCount > 3) { |
| 218 | corners.begin()->prevEdgeLengthEstimate += splineLength; |
| 219 | for (int i = 0; i < cornerCount; ++i) { |
| 220 | if ( |
| 221 | corners[i].prevEdgeLengthEstimate > corners[(i+1)%cornerCount].prevEdgeLengthEstimate && |
| 222 | corners[(i+1)%cornerCount].prevEdgeLengthEstimate < corners[(i+2)%cornerCount].prevEdgeLengthEstimate |
| 223 | ) { |
| 224 | corners[i].minor = true; |
| 225 | --majorCornerCount; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | EdgeColor initialColor = BLACK; |
| 230 | for (int i = 0; i < cornerCount; ++i) { |
| 231 | if (!corners[i].minor) { |
| 232 | --majorCornerCount; |
| 233 | switchColor(color, seed, EdgeColor(!majorCornerCount*initialColor)); |
| 234 | corners[i].color = color; |
| 235 | if (!initialColor) |
| 236 | initialColor = color; |
| 237 | } |
| 238 | } |
| 239 | for (int i = 0; i < cornerCount; ++i) { |
| 240 | if (corners[i].minor) { |
| 241 | EdgeColor nextColor = corners[(i+1)%cornerCount].color; |
| 242 | corners[i].color = EdgeColor((color&nextColor)^WHITE); |
| 243 | } else |
| 244 | color = corners[i].color; |
| 245 | } |
| 246 | int spline = 0; |
| 247 | int start = corners[0].index; |
| 248 | color = corners[0].color; |
| 249 | int m = (int) contour->edges.size(); |
| 250 | for (int i = 0; i < m; ++i) { |
| 251 | int index = (start+i)%m; |
| 252 | if (spline+1 < cornerCount && corners[spline+1].index == index) |
| 253 | color = corners[++spline].color; |
| 254 | contour->edges[index]->color = color; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // EDGE COLORING BY DISTANCE - EXPERIMENTAL IMPLEMENTATION - WORK IN PROGRESS |
| 261 | #define MAX_RECOLOR_STEPS 16 |
| 262 | #define EDGE_DISTANCE_PRECISION 16 |
| 263 | |
| 264 | static double edgeToEdgeDistance(const EdgeSegment &a, const EdgeSegment &b, int precision) { |
| 265 | 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)) |
| 266 | return 0; |
| 267 | double iFac = 1./precision; |
| 268 | double minDistance = (b.point(0)-a.point(0)).length(); |
| 269 | for (int i = 0; i <= precision; ++i) { |
| 270 | double t = iFac*i; |
| 271 | double d = fabs(a.signedDistance(b.point(t), t).distance); |
| 272 | minDistance = min(minDistance, d); |
| 273 | } |
| 274 | for (int i = 0; i <= precision; ++i) { |
| 275 | double t = iFac*i; |
| 276 | double d = fabs(b.signedDistance(a.point(t), t).distance); |
| 277 | minDistance = min(minDistance, d); |
| 278 | } |
| 279 | return minDistance; |
| 280 | } |
| 281 | |
| 282 | static double splineToSplineDistance(EdgeSegment *const *edgeSegments, int aStart, int aEnd, int bStart, int bEnd, int precision) { |
| 283 | double minDistance = DBL_MAX; |
| 284 | for (int ai = aStart; ai < aEnd; ++ai) |
| 285 | for (int bi = bStart; bi < bEnd && minDistance; ++bi) { |
| 286 | double d = edgeToEdgeDistance(*edgeSegments[ai], *edgeSegments[bi], precision); |
| 287 | minDistance = min(minDistance, d); |
| 288 | } |
| 289 | return minDistance; |
| 290 | } |
| 291 | |
| 292 | static void colorSecondDegreeGraph(int *coloring, const int *const *edgeMatrix, int vertexCount, unsigned long long seed) { |
| 293 | for (int i = 0; i < vertexCount; ++i) { |
| 294 | int possibleColors = 7; |
| 295 | for (int j = 0; j < i; ++j) { |
| 296 | if (edgeMatrix[i][j]) |
| 297 | possibleColors &= ~(1<<coloring[j]); |
| 298 | } |
| 299 | int color = 0; |
| 300 | switch (possibleColors) { |
| 301 | case 1: |
| 302 | color = 0; |
| 303 | break; |
| 304 | case 2: |
| 305 | color = 1; |
| 306 | break; |
| 307 | case 3: |
| 308 | color = seedExtract2(seed); // 0 or 1 |
| 309 | break; |
| 310 | case 4: |
| 311 | color = 2; |
| 312 | break; |
| 313 | case 5: |
| 314 | color = (int) !seedExtract2(seed)<<1; // 2 or 0 |
| 315 | break; |
| 316 | case 6: |
| 317 | color = seedExtract2(seed)+1; // 1 or 2 |
| 318 | break; |
| 319 | case 7: |
| 320 | color = (seedExtract3(seed)+i)%3; // 0 or 1 or 2 |
| 321 | break; |
| 322 | } |
| 323 | coloring[i] = color; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | static int vertexPossibleColors(const int *coloring, const int *edgeVector, int vertexCount) { |
| 328 | int usedColors = 0; |
| 329 | for (int i = 0; i < vertexCount; ++i) |
| 330 | if (edgeVector[i]) |
| 331 | usedColors |= 1<<coloring[i]; |
| 332 | return 7&~usedColors; |
| 333 | } |
| 334 | |
| 335 | static void uncolorSameNeighbors(std::queue<int> &uncolored, int *coloring, const int *const *edgeMatrix, int vertex, int vertexCount) { |
| 336 | for (int i = vertex+1; i < vertexCount; ++i) { |
| 337 | if (edgeMatrix[vertex][i] && coloring[i] == coloring[vertex]) { |
| 338 | coloring[i] = -1; |
| 339 | uncolored.push(i); |
| 340 | } |
| 341 | } |
| 342 | for (int i = 0; i < vertex; ++i) { |
| 343 | if (edgeMatrix[vertex][i] && coloring[i] == coloring[vertex]) { |
| 344 | coloring[i] = -1; |
| 345 | uncolored.push(i); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | static bool tryAddEdge(int *coloring, int *const *edgeMatrix, int vertexCount, int vertexA, int vertexB, int *coloringBuffer) { |
| 351 | static const int FIRST_POSSIBLE_COLOR[8] = { -1, 0, 1, 0, 2, 2, 1, 0 }; |
| 352 | edgeMatrix[vertexA][vertexB] = 1; |
| 353 | edgeMatrix[vertexB][vertexA] = 1; |
| 354 | if (coloring[vertexA] != coloring[vertexB]) |
| 355 | return true; |
| 356 | int bPossibleColors = vertexPossibleColors(coloring, edgeMatrix[vertexB], vertexCount); |
| 357 | if (bPossibleColors) { |
| 358 | coloring[vertexB] = FIRST_POSSIBLE_COLOR[bPossibleColors]; |
| 359 | return true; |
| 360 | } |
| 361 | memcpy(coloringBuffer, coloring, sizeof(int)*vertexCount); |
| 362 | std::queue<int> uncolored; |
| 363 | { |
| 364 | int *coloring = coloringBuffer; |
| 365 | coloring[vertexB] = FIRST_POSSIBLE_COLOR[7&~(1<<coloring[vertexA])]; |
| 366 | uncolorSameNeighbors(uncolored, coloring, edgeMatrix, vertexB, vertexCount); |
| 367 | int step = 0; |
| 368 | while (!uncolored.empty() && step < MAX_RECOLOR_STEPS) { |
| 369 | int i = uncolored.front(); |
| 370 | uncolored.pop(); |
| 371 | int possibleColors = vertexPossibleColors(coloring, edgeMatrix[i], vertexCount); |
| 372 | if (possibleColors) { |
| 373 | coloring[i] = FIRST_POSSIBLE_COLOR[possibleColors]; |
| 374 | continue; |
| 375 | } |
| 376 | do { |
| 377 | coloring[i] = step++%3; |
| 378 | } while (edgeMatrix[i][vertexA] && coloring[i] == coloring[vertexA]); |
| 379 | uncolorSameNeighbors(uncolored, coloring, edgeMatrix, i, vertexCount); |
| 380 | } |
| 381 | } |
| 382 | if (!uncolored.empty()) { |
| 383 | edgeMatrix[vertexA][vertexB] = 0; |
| 384 | edgeMatrix[vertexB][vertexA] = 0; |
| 385 | return false; |
| 386 | } |
| 387 | memcpy(coloring, coloringBuffer, sizeof(int)*vertexCount); |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | static int cmpDoublePtr(const void *a, const void *b) { |
| 392 | return sign(**reinterpret_cast<const double *const *>(a)-**reinterpret_cast<const double *const *>(b)); |
| 393 | } |
| 394 | |
| 395 | void edgeColoringByDistance(Shape &shape, double angleThreshold, unsigned long long seed) { |
| 396 | |
| 397 | std::vector<EdgeSegment *> edgeSegments; |
| 398 | std::vector<int> splineStarts; |
| 399 | |
| 400 | double crossThreshold = sin(angleThreshold); |
| 401 | std::vector<int> corners; |
| 402 | for (std::vector<Contour>::iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) |
| 403 | if (!contour->edges.empty()) { |
| 404 | // Identify corners |
| 405 | corners.clear(); |
| 406 | Vector2 prevDirection = contour->edges.back()->direction(1); |
| 407 | int index = 0; |
| 408 | for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge, ++index) { |
| 409 | if (isCorner(prevDirection.normalize(), (*edge)->direction(0).normalize(), crossThreshold)) |
| 410 | corners.push_back(index); |
| 411 | prevDirection = (*edge)->direction(1); |
| 412 | } |
| 413 | |
| 414 | splineStarts.push_back((int) edgeSegments.size()); |
| 415 | // Smooth contour |
| 416 | if (corners.empty()) |
| 417 | for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) |
| 418 | edgeSegments.push_back(&**edge); |
| 419 | // "Teardrop" case |
| 420 | else if (corners.size() == 1) { |
| 421 | int corner = corners[0]; |
| 422 | if (contour->edges.size() >= 3) { |
| 423 | int m = (int) contour->edges.size(); |
| 424 | for (int i = 0; i < m; ++i) { |
| 425 | if (i == m/2) |
| 426 | splineStarts.push_back((int) edgeSegments.size()); |
| 427 | if (symmetricalTrichotomy(i, m)) |
| 428 | edgeSegments.push_back(&*contour->edges[(corner+i)%m]); |
| 429 | else |
| 430 | contour->edges[(corner+i)%m]->color = WHITE; |
| 431 | } |
| 432 | } else if (contour->edges.size() >= 1) { |
| 433 | // Less than three edge segments for three colors => edges must be split |
| 434 | EdgeSegment *parts[7] = { }; |
| 435 | contour->edges[0]->splitInThirds(parts[0+3*corner], parts[1+3*corner], parts[2+3*corner]); |
| 436 | if (contour->edges.size() >= 2) { |
| 437 | contour->edges[1]->splitInThirds(parts[3-3*corner], parts[4-3*corner], parts[5-3*corner]); |
| 438 | edgeSegments.push_back(parts[0]); |
| 439 | edgeSegments.push_back(parts[1]); |
| 440 | parts[2]->color = parts[3]->color = WHITE; |
| 441 | splineStarts.push_back((int) edgeSegments.size()); |
| 442 | edgeSegments.push_back(parts[4]); |
| 443 | edgeSegments.push_back(parts[5]); |
| 444 | } else { |
| 445 | edgeSegments.push_back(parts[0]); |
| 446 | parts[1]->color = WHITE; |
| 447 | splineStarts.push_back((int) edgeSegments.size()); |
| 448 | edgeSegments.push_back(parts[2]); |
| 449 | } |
| 450 | contour->edges.clear(); |
| 451 | for (int i = 0; parts[i]; ++i) |
| 452 | contour->edges.push_back(EdgeHolder(parts[i])); |
| 453 | } |
| 454 | } |
| 455 | // Multiple corners |
| 456 | else { |
| 457 | int cornerCount = (int) corners.size(); |
| 458 | int spline = 0; |
| 459 | int start = corners[0]; |
| 460 | int m = (int) contour->edges.size(); |
| 461 | for (int i = 0; i < m; ++i) { |
| 462 | int index = (start+i)%m; |
| 463 | if (spline+1 < cornerCount && corners[spline+1] == index) { |
| 464 | splineStarts.push_back((int) edgeSegments.size()); |
| 465 | ++spline; |
| 466 | } |
| 467 | edgeSegments.push_back(&*contour->edges[index]); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | splineStarts.push_back((int) edgeSegments.size()); |
| 472 | |
| 473 | int segmentCount = (int) edgeSegments.size(); |
| 474 | int splineCount = (int) splineStarts.size()-1; |
| 475 | if (!splineCount) |
| 476 | return; |
| 477 | |
| 478 | std::vector<double> distanceMatrixStorage(splineCount*splineCount); |
| 479 | std::vector<double *> distanceMatrix(splineCount); |
| 480 | for (int i = 0; i < splineCount; ++i) |
| 481 | distanceMatrix[i] = &distanceMatrixStorage[i*splineCount]; |
| 482 | const double *distanceMatrixBase = &distanceMatrixStorage[0]; |
| 483 | |
| 484 | for (int i = 0; i < splineCount; ++i) { |
| 485 | distanceMatrix[i][i] = -1; |
| 486 | for (int j = i+1; j < splineCount; ++j) { |
| 487 | double dist = splineToSplineDistance(&edgeSegments[0], splineStarts[i], splineStarts[i+1], splineStarts[j], splineStarts[j+1], EDGE_DISTANCE_PRECISION); |
| 488 | distanceMatrix[i][j] = dist; |
| 489 | distanceMatrix[j][i] = dist; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | std::vector<const double *> graphEdgeDistances; |
| 494 | graphEdgeDistances.reserve(splineCount*(splineCount-1)/2); |
| 495 | for (int i = 0; i < splineCount; ++i) |
| 496 | for (int j = i+1; j < splineCount; ++j) |
| 497 | graphEdgeDistances.push_back(&distanceMatrix[i][j]); |
| 498 | int graphEdgeCount = (int) graphEdgeDistances.size(); |
| 499 | if (!graphEdgeDistances.empty()) |
| 500 | qsort(&graphEdgeDistances[0], graphEdgeDistances.size(), sizeof(const double *), &cmpDoublePtr); |
| 501 | |
| 502 | std::vector<int> edgeMatrixStorage(splineCount*splineCount); |
| 503 | std::vector<int *> edgeMatrix(splineCount); |
| 504 | for (int i = 0; i < splineCount; ++i) |
| 505 | edgeMatrix[i] = &edgeMatrixStorage[i*splineCount]; |
| 506 | int nextEdge = 0; |
| 507 | for (; nextEdge < graphEdgeCount && !*graphEdgeDistances[nextEdge]; ++nextEdge) { |
| 508 | int elem = (int) (graphEdgeDistances[nextEdge]-distanceMatrixBase); |
| 509 | int row = elem/splineCount; |
| 510 | int col = elem%splineCount; |
| 511 | edgeMatrix[row][col] = 1; |
| 512 | edgeMatrix[col][row] = 1; |
| 513 | } |
| 514 | |
| 515 | std::vector<int> coloring(2*splineCount); |
| 516 | colorSecondDegreeGraph(&coloring[0], &edgeMatrix[0], splineCount, seed); |
| 517 | for (; nextEdge < graphEdgeCount; ++nextEdge) { |
| 518 | int elem = (int) (graphEdgeDistances[nextEdge]-distanceMatrixBase); |
| 519 | tryAddEdge(&coloring[0], &edgeMatrix[0], splineCount, elem/splineCount, elem%splineCount, &coloring[splineCount]); |
| 520 | } |
| 521 | |
| 522 | const EdgeColor colors[3] = { YELLOW, CYAN, MAGENTA }; |
| 523 | int spline = -1; |
| 524 | for (int i = 0; i < segmentCount; ++i) { |
| 525 | if (splineStarts[spline+1] == i) |
| 526 | ++spline; |
| 527 | edgeSegments[i]->color = colors[coloring[spline]]; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | } |