| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include <math.h> |
| 24 |
#include <limits.h> |
| 25 |
#include <float.h> |
| 26 |
#include <string.h> |
| 27 |
#include <assert.h> |
| 28 |
|
| 29 |
#include "pm_c_util.h" |
| 30 |
#include "mallocvar.h" |
| 31 |
|
| 32 |
#include "autotrace.h" |
| 33 |
#include "fit.h" |
| 34 |
#include "message.h" |
| 35 |
#include "logreport.h" |
| 36 |
#include "spline.h" |
| 37 |
#include "point.h" |
| 38 |
#include "vector.h" |
| 39 |
#include "curve.h" |
| 40 |
#include "pxl-outline.h" |
| 41 |
#include "epsilon.h" |
| 42 |
|
| 43 |
#define CUBE(x) ((x) * (x) * (x)) |
| 44 |
|
| 45 |
typedef enum {LINEEND_INIT, LINEEND_TERM} LineEnd; |
| 46 |
|
| 47 |
static LineEnd |
| 48 |
otherEnd(LineEnd const thisEnd) { |
| 49 |
|
| 50 |
switch (thisEnd) { |
| 51 |
case LINEEND_INIT: return LINEEND_TERM; |
| 52 |
case LINEEND_TERM: return LINEEND_INIT; |
| 53 |
} |
| 54 |
assert(false); |
| 55 |
return LINEEND_INIT; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
typedef struct IndexList { |
| 63 |
unsigned int * data; |
| 64 |
unsigned int length; |
| 65 |
} IndexList; |
| 66 |
|
| 67 |
|
| 68 |
#define GET_INDEX(i_l, n) ((i_l).data[n]) |
| 69 |
#define INDEX_LIST_LENGTH(iL) ((iL).length) |
| 70 |
#define GET_LAST_INDEX(iL) ((iL).data[INDEX_LIST_LENGTH(iL) - 1]) |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
static pm_pixelcoord |
| 76 |
intCoordFmReal(Point const realCoord) { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
pm_pixelcoord intCoord; |
| 82 |
|
| 83 |
intCoord.col = ROUND(realCoord.x); |
| 84 |
intCoord.row = ROUND(realCoord.y); |
| 85 |
|
| 86 |
return intCoord; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
static IndexList |
| 94 |
indexList_new(void) { |
| 95 |
|
| 96 |
IndexList indexList; |
| 97 |
|
| 98 |
indexList.data = NULL; |
| 99 |
INDEX_LIST_LENGTH(indexList) = 0; |
| 100 |
|
| 101 |
return indexList; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
static void |
| 107 |
indexList_free(IndexList * const indexListP) { |
| 108 |
|
| 109 |
if (INDEX_LIST_LENGTH(*indexListP) > 0) { |
| 110 |
free(indexListP->data); |
| 111 |
indexListP->data = NULL; |
| 112 |
INDEX_LIST_LENGTH(*indexListP) = 0; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static void |
| 119 |
indexList_append(IndexList * const listP, |
| 120 |
unsigned int const newIndex) { |
| 121 |
|
| 122 |
INDEX_LIST_LENGTH(*listP)++; |
| 123 |
REALLOCARRAY_NOFAIL(listP->data, INDEX_LIST_LENGTH(*listP)); |
| 124 |
listP->data[INDEX_LIST_LENGTH(*listP) - 1] = newIndex; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
static void |
| 130 |
appendCorner(IndexList * const cornerListP, |
| 131 |
unsigned int const pixelSeq, |
| 132 |
pixel_outline_type const outline, |
| 133 |
float const angle, |
| 134 |
char const logType) { |
| 135 |
|
| 136 |
pm_pixelcoord const coord = O_COORDINATE(outline, pixelSeq); |
| 137 |
|
| 138 |
indexList_append(cornerListP, pixelSeq); |
| 139 |
LOG4(" (%d,%d)%c%.3f", coord.col, coord.row, logType, angle); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
static void |
| 145 |
findVectors(unsigned int const testIndex, |
| 146 |
pixel_outline_type const outline, |
| 147 |
Vector * const inP, |
| 148 |
Vector * const outP, |
| 149 |
unsigned int const cornerSurround) { |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
pm_pixelcoord const candidate = O_COORDINATE(outline, testIndex); |
| 160 |
|
| 161 |
unsigned int i; |
| 162 |
unsigned int doneCt; |
| 163 |
|
| 164 |
inP->dx = inP->dy = inP->dz = 0.0; |
| 165 |
outP->dx = outP->dy = outP->dz = 0.0; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
for (i = O_PREV(outline, testIndex), doneCt = 0; |
| 170 |
doneCt < cornerSurround; |
| 171 |
i = O_PREV(outline, i), ++doneCt) |
| 172 |
*inP = vector_sum(*inP, vector_IPointDiff(O_COORDINATE(outline, i), |
| 173 |
candidate)); |
| 174 |
|
| 175 |
|
| 176 |
for (i = O_NEXT(outline, testIndex), doneCt = 0; |
| 177 |
doneCt < cornerSurround; |
| 178 |
i = O_NEXT(outline, i), ++doneCt) |
| 179 |
*outP = vector_sum(*outP, vector_IPointDiff(O_COORDINATE(outline, i), |
| 180 |
candidate)); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
static void |
| 186 |
lookAheadForBetterCorner(pixel_outline_type const outline, |
| 187 |
unsigned int const basePixelSeq, |
| 188 |
float const baseCornerAngle, |
| 189 |
unsigned int const cornerSurround, |
| 190 |
unsigned int const cornerAlwaysThreshold, |
| 191 |
unsigned int * const highestExaminedP, |
| 192 |
float * const bestCornerAngleP, |
| 193 |
unsigned int * const bestCornerIndexP, |
| 194 |
IndexList * const equallyGoodListP, |
| 195 |
IndexList * const cornerListP, |
| 196 |
at_exception_type * const exceptionP) { |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
float bestCornerAngle; |
| 215 |
unsigned bestCornerIndex; |
| 216 |
IndexList equallyGoodList; |
| 217 |
unsigned int q; |
| 218 |
unsigned int i; |
| 219 |
|
| 220 |
bestCornerIndex = basePixelSeq; |
| 221 |
bestCornerAngle = baseCornerAngle; |
| 222 |
|
| 223 |
equallyGoodList = indexList_new(); |
| 224 |
|
| 225 |
q = basePixelSeq; |
| 226 |
i = basePixelSeq + 1; |
| 227 |
|
| 228 |
while (i < bestCornerIndex + cornerSurround && |
| 229 |
i < O_LENGTH(outline) && |
| 230 |
!at_exception_got_fatal(exceptionP)) { |
| 231 |
|
| 232 |
Vector inVector, outVector; |
| 233 |
float cornerAngle; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
q = i % O_LENGTH(outline); |
| 238 |
findVectors(q, outline, &inVector, &outVector, cornerSurround); |
| 239 |
cornerAngle = vector_angle(inVector, outVector, exceptionP); |
| 240 |
if (!at_exception_got_fatal(exceptionP)) { |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if (cornerAngle <= cornerAlwaysThreshold && q >= basePixelSeq) |
| 248 |
appendCorner(cornerListP, q, outline, cornerAngle, '\\'); |
| 249 |
|
| 250 |
if (epsilon_equal(cornerAngle, bestCornerAngle)) |
| 251 |
indexList_append(&equallyGoodList, q); |
| 252 |
else if (cornerAngle < bestCornerAngle) { |
| 253 |
bestCornerAngle = cornerAngle; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
i = bestCornerIndex = q; |
| 258 |
indexList_free(&equallyGoodList); |
| 259 |
equallyGoodList = indexList_new(); |
| 260 |
} |
| 261 |
++i; |
| 262 |
} |
| 263 |
} |
| 264 |
*bestCornerAngleP = bestCornerAngle; |
| 265 |
*bestCornerIndexP = bestCornerIndex; |
| 266 |
*equallyGoodListP = equallyGoodList; |
| 267 |
*highestExaminedP = q; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
static void |
| 273 |
establishCornerSearchLimits(pixel_outline_type const outline, |
| 274 |
fitting_opts_type * const fittingOptsP, |
| 275 |
unsigned int * const firstP, |
| 276 |
unsigned int * const lastP) { |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
assert(O_LENGTH(outline) >= 1); |
| 281 |
assert(O_LENGTH(outline) - 1 >= fittingOptsP->corner_surround); |
| 282 |
|
| 283 |
*firstP = 0; |
| 284 |
*lastP = O_LENGTH(outline) - 1; |
| 285 |
if (outline.open) { |
| 286 |
*firstP += fittingOptsP->corner_surround; |
| 287 |
*lastP -= fittingOptsP->corner_surround; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
static void |
| 294 |
removeAdjacentCorners(IndexList * const listP, |
| 295 |
unsigned int const lastIndex, |
| 296 |
bool const mustRemoveAdjCorners, |
| 297 |
at_exception_type * const exception) { |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
unsigned int j; |
| 310 |
unsigned int last; |
| 311 |
IndexList newList; |
| 312 |
|
| 313 |
newList = indexList_new(); |
| 314 |
|
| 315 |
for (j = INDEX_LIST_LENGTH (*listP) - 1; j > 0; --j) { |
| 316 |
unsigned int search; |
| 317 |
unsigned int maxIndex; |
| 318 |
|
| 319 |
|
| 320 |
for (search = 0, maxIndex = j; search < j; ++search) |
| 321 |
if (GET_INDEX (*listP, search) > GET_INDEX (*listP, maxIndex)) |
| 322 |
maxIndex = search; |
| 323 |
|
| 324 |
if (maxIndex != j) { |
| 325 |
unsigned int const temp = GET_INDEX (*listP, j); |
| 326 |
GET_INDEX (*listP, j) = GET_INDEX (*listP, maxIndex); |
| 327 |
GET_INDEX (*listP, maxIndex) = temp; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
for (j = 0; j < INDEX_LIST_LENGTH(*listP) - 1; ++j) { |
| 336 |
unsigned int const current = GET_INDEX(*listP, j); |
| 337 |
unsigned int const next = GET_INDEX(*listP, j + 1); |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
if ((mustRemoveAdjCorners) && ((next == current + 1) || |
| 343 |
(next == current))) |
| 344 |
++j; |
| 345 |
|
| 346 |
indexList_append(&newList, current); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
last = GET_LAST_INDEX(*listP); |
| 353 |
if (INDEX_LIST_LENGTH(newList) == 0 |
| 354 |
|| !(last == GET_LAST_INDEX(newList) + 1 |
| 355 |
|| (last == lastIndex && GET_INDEX(*listP, 0) == 0))) |
| 356 |
indexList_append(&newList, last); |
| 357 |
|
| 358 |
indexList_free(listP); |
| 359 |
*listP = newList; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
#define ONLY_ONE_ZERO(v) \ |
| 381 |
(((v).dx == 0.0 && (v).dy != 0.0) || ((v).dy == 0.0 && (v).dx != 0.0)) |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
#define CLOCKWISE_KNEE(prev_delta, next_delta) \ |
| 390 |
((prev_delta.dx == -1.0 && next_delta.dy == 1.0) \ |
| 391 |
|| (prev_delta.dy == 1.0 && next_delta.dx == 1.0) \ |
| 392 |
|| (prev_delta.dx == 1.0 && next_delta.dy == -1.0) \ |
| 393 |
|| (prev_delta.dy == -1.0 && next_delta.dx == -1.0)) |
| 394 |
|
| 395 |
#define COUNTERCLOCKWISE_KNEE(prev_delta, next_delta) \ |
| 396 |
((prev_delta.dy == 1.0 && next_delta.dx == -1.0) \ |
| 397 |
|| (prev_delta.dx == 1.0 && next_delta.dy == 1.0) \ |
| 398 |
|| (prev_delta.dy == -1.0 && next_delta.dx == 1.0) \ |
| 399 |
|| (prev_delta.dx == -1.0 && next_delta.dy == -1.0)) |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
static void |
| 404 |
remove_knee_points(Curve * const curveP, |
| 405 |
bool const clockwise) { |
| 406 |
|
| 407 |
unsigned int const offset = CURVE_CYCLIC(curveP) ? 0 : 1; |
| 408 |
Curve * const trimmedCurveP = curve_copyMost(curveP); |
| 409 |
|
| 410 |
pm_pixelcoord previous; |
| 411 |
unsigned int i; |
| 412 |
|
| 413 |
if (!CURVE_CYCLIC(curveP)) |
| 414 |
curve_appendPixel(trimmedCurveP, |
| 415 |
intCoordFmReal(CURVE_POINT(curveP, 0))); |
| 416 |
|
| 417 |
previous = intCoordFmReal(CURVE_POINT(curveP, |
| 418 |
CURVE_PREV(curveP, offset))); |
| 419 |
|
| 420 |
for (i = offset; i < CURVE_LENGTH(curveP) - offset; ++i) { |
| 421 |
pm_pixelcoord const current = |
| 422 |
intCoordFmReal(CURVE_POINT(curveP, i)); |
| 423 |
pm_pixelcoord const next = |
| 424 |
intCoordFmReal(CURVE_POINT(curveP, CURVE_NEXT(curveP, i))); |
| 425 |
Vector const prev_delta = vector_IPointDiff(previous, current); |
| 426 |
Vector const next_delta = vector_IPointDiff(next, current); |
| 427 |
|
| 428 |
if (ONLY_ONE_ZERO(prev_delta) && ONLY_ONE_ZERO(next_delta) |
| 429 |
&& ((clockwise && CLOCKWISE_KNEE(prev_delta, next_delta)) |
| 430 |
|| (!clockwise |
| 431 |
&& COUNTERCLOCKWISE_KNEE(prev_delta, next_delta)))) |
| 432 |
LOG2(" (%d,%d)", current.col, current.row); |
| 433 |
else { |
| 434 |
previous = current; |
| 435 |
curve_appendPixel(trimmedCurveP, current); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
if (!CURVE_CYCLIC(curveP)) |
| 440 |
curve_appendPixel(trimmedCurveP, |
| 441 |
intCoordFmReal(LAST_CURVE_POINT(curveP))); |
| 442 |
|
| 443 |
if (CURVE_LENGTH(trimmedCurveP) == CURVE_LENGTH(curveP)) |
| 444 |
LOG(" (none)"); |
| 445 |
|
| 446 |
LOG(".\n"); |
| 447 |
|
| 448 |
curve_move(curveP, trimmedCurveP); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
static void |
| 454 |
filter(Curve * const curveP, |
| 455 |
fitting_opts_type * const fittingOptsP) { |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
unsigned int const offset = CURVE_CYCLIC(curveP) ? 0 : 1; |
| 461 |
|
| 462 |
unsigned int iteration, thisPoint; |
| 463 |
Point prevNewPoint; |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
if (CURVE_LENGTH(curveP) < 5) { |
| 471 |
LOG1("Length is %u, not enough to filter.\n", CURVE_LENGTH(curveP)); |
| 472 |
return; |
| 473 |
} |
| 474 |
|
| 475 |
prevNewPoint.x = FLT_MAX; |
| 476 |
prevNewPoint.y = FLT_MAX; |
| 477 |
prevNewPoint.z = FLT_MAX; |
| 478 |
|
| 479 |
for (iteration = 0; |
| 480 |
iteration < fittingOptsP->filter_iterations; |
| 481 |
++iteration) { |
| 482 |
Curve * const newcurveP = curve_copyMost(curveP); |
| 483 |
|
| 484 |
bool collapsed; |
| 485 |
|
| 486 |
collapsed = false; |
| 487 |
|
| 488 |
|
| 489 |
if (offset) |
| 490 |
curve_appendPoint(newcurveP, CURVE_POINT(curveP, 0)); |
| 491 |
|
| 492 |
for (thisPoint = offset; |
| 493 |
thisPoint < CURVE_LENGTH(curveP) - offset; |
| 494 |
++thisPoint) { |
| 495 |
Vector in, out, sum; |
| 496 |
Point newPoint; |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
signed int prev, prevprev; |
| 504 |
unsigned int next, nextnext; |
| 505 |
Point candidate = CURVE_POINT(curveP, thisPoint); |
| 506 |
|
| 507 |
prev = CURVE_PREV(curveP, thisPoint); |
| 508 |
prevprev = CURVE_PREV(curveP, prev); |
| 509 |
next = CURVE_NEXT(curveP, thisPoint); |
| 510 |
nextnext = CURVE_NEXT(curveP, next); |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
in.dx = in.dy = in.dz = 0.0; |
| 516 |
|
| 517 |
in = vector_sum(in, |
| 518 |
vector_fromTwoPoints(CURVE_POINT(curveP, prev), |
| 519 |
candidate)); |
| 520 |
if (prevprev >= 0) |
| 521 |
in = vector_sum( |
| 522 |
in, |
| 523 |
vector_fromTwoPoints(CURVE_POINT(curveP, prevprev), |
| 524 |
candidate)); |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
out.dx = out.dy = out.dz = 0.0; |
| 530 |
|
| 531 |
out = vector_sum( |
| 532 |
out, |
| 533 |
vector_fromTwoPoints(CURVE_POINT(curveP, next), candidate)); |
| 534 |
if (nextnext < CURVE_LENGTH(curveP)) |
| 535 |
out = vector_sum( |
| 536 |
out, |
| 537 |
vector_fromTwoPoints(CURVE_POINT(curveP, nextnext), |
| 538 |
candidate)); |
| 539 |
|
| 540 |
|
| 541 |
newPoint = candidate; |
| 542 |
sum = vector_sum(in, out); |
| 543 |
|
| 544 |
newPoint.x += sum.dx / 6; |
| 545 |
newPoint.y += sum.dy / 6; |
| 546 |
newPoint.z += sum.dz / 6; |
| 547 |
if (fabs(prevNewPoint.x - newPoint.x) < 0.3 |
| 548 |
&& fabs (prevNewPoint.y - newPoint.y) < 0.3 |
| 549 |
&& fabs (prevNewPoint.z - newPoint.z) < 0.3) { |
| 550 |
collapsed = true; |
| 551 |
break; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
curve_appendPoint(newcurveP, prevNewPoint = newPoint); |
| 558 |
} |
| 559 |
|
| 560 |
if (collapsed) |
| 561 |
curve_free(newcurveP); |
| 562 |
else { |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
if (offset) |
| 567 |
curve_appendPoint(newcurveP, LAST_CURVE_POINT(curveP)); |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
curve_move(curveP, newcurveP); |
| 573 |
} |
| 574 |
} |
| 575 |
curve_log(curveP, false); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
static void |
| 581 |
removeAdjacent(IndexList * const cornerListP, |
| 582 |
pixel_outline_type const outline, |
| 583 |
fitting_opts_type * const fittingOptsP, |
| 584 |
at_exception_type * const exception) { |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
if (INDEX_LIST_LENGTH(*cornerListP) > 0) |
| 592 |
removeAdjacentCorners( |
| 593 |
cornerListP, |
| 594 |
O_LENGTH(outline) - (outline.open ? 2 : 1), |
| 595 |
fittingOptsP->remove_adjacent_corners, |
| 596 |
exception); |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
static IndexList |
| 602 |
findCorners(pixel_outline_type const outline, |
| 603 |
fitting_opts_type * const fittingOptsP, |
| 604 |
at_exception_type * const exceptionP) { |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
unsigned int p; |
| 613 |
unsigned int firstPixelSeq, lastPixelSeq; |
| 614 |
IndexList cornerList; |
| 615 |
|
| 616 |
cornerList = indexList_new(); |
| 617 |
|
| 618 |
if (O_LENGTH(outline) <= fittingOptsP->corner_surround * 2 + 1) |
| 619 |
return cornerList; |
| 620 |
|
| 621 |
establishCornerSearchLimits(outline, fittingOptsP, |
| 622 |
&firstPixelSeq, &lastPixelSeq); |
| 623 |
|
| 624 |
|
| 625 |
for (p = firstPixelSeq; p <= lastPixelSeq;) { |
| 626 |
Vector inVector, outVector; |
| 627 |
float cornerAngle; |
| 628 |
|
| 629 |
|
| 630 |
findVectors(p, outline, &inVector, &outVector, |
| 631 |
fittingOptsP->corner_surround); |
| 632 |
cornerAngle = vector_angle(inVector, outVector, exceptionP); |
| 633 |
if (at_exception_got_fatal(exceptionP)) |
| 634 |
goto cleanup; |
| 635 |
|
| 636 |
if (fabs(cornerAngle) <= fittingOptsP->corner_threshold) { |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
float bestCornerAngle; |
| 648 |
unsigned bestCornerIndex; |
| 649 |
IndexList equallyGoodList; |
| 650 |
unsigned int q; |
| 651 |
|
| 652 |
if (cornerAngle <= fittingOptsP->corner_always_threshold) |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
appendCorner(&cornerList, p, outline, cornerAngle, '\\'); |
| 657 |
|
| 658 |
lookAheadForBetterCorner(outline, p, cornerAngle, |
| 659 |
fittingOptsP->corner_surround, |
| 660 |
fittingOptsP->corner_always_threshold, |
| 661 |
&q, |
| 662 |
&bestCornerAngle, &bestCornerIndex, |
| 663 |
&equallyGoodList, |
| 664 |
&cornerList, |
| 665 |
exceptionP); |
| 666 |
|
| 667 |
if (at_exception_got_fatal(exceptionP)) |
| 668 |
goto cleanup; |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
if (bestCornerAngle > fittingOptsP->corner_always_threshold |
| 677 |
&& bestCornerIndex >= p) { |
| 678 |
|
| 679 |
unsigned int j; |
| 680 |
|
| 681 |
appendCorner(&cornerList, bestCornerIndex, |
| 682 |
outline, bestCornerAngle, '/'); |
| 683 |
|
| 684 |
for (j = 0; j < INDEX_LIST_LENGTH (equallyGoodList); ++j) |
| 685 |
appendCorner(&cornerList, GET_INDEX(equallyGoodList, j), |
| 686 |
outline, bestCornerAngle, '@'); |
| 687 |
} |
| 688 |
indexList_free(&equallyGoodList); |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
p = (q < p) ? O_LENGTH(outline) : q + 1; |
| 695 |
} else |
| 696 |
++p; |
| 697 |
} |
| 698 |
removeAdjacent(&cornerList, outline, fittingOptsP, exceptionP); |
| 699 |
|
| 700 |
cleanup: |
| 701 |
return cornerList; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
static void |
| 707 |
makeOutlineOneCurve(pixel_outline_type const outline, |
| 708 |
CurveList * const curveListP) { |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
Curve * curveP; |
| 715 |
unsigned int pixelSeq; |
| 716 |
|
| 717 |
curveP = curve_new(); |
| 718 |
|
| 719 |
for (pixelSeq = 0; pixelSeq < O_LENGTH(outline); ++pixelSeq) |
| 720 |
curve_appendPixel(curveP, O_COORDINATE(outline, pixelSeq)); |
| 721 |
|
| 722 |
if (outline.open) |
| 723 |
CURVE_CYCLIC(curveP) = false; |
| 724 |
else |
| 725 |
CURVE_CYCLIC(curveP) = true; |
| 726 |
|
| 727 |
|
| 728 |
NEXT_CURVE(curveP) = curveP; |
| 729 |
PREVIOUS_CURVE(curveP) = curveP; |
| 730 |
|
| 731 |
curve_appendList(curveListP, curveP); |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
static void |
| 737 |
addCurveStartingAtCorner(pixel_outline_type const outline, |
| 738 |
IndexList const cornerList, |
| 739 |
unsigned int const cornerSeq, |
| 740 |
CurveList * const curveListP, |
| 741 |
Curve ** const curCurvePP) { |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
unsigned int const cornerPixelSeq = GET_INDEX(cornerList, cornerSeq); |
| 753 |
|
| 754 |
unsigned int lastPixelSeq; |
| 755 |
Curve * curveP; |
| 756 |
unsigned int pixelSeq; |
| 757 |
|
| 758 |
if (cornerSeq + 1 >= cornerList.length) |
| 759 |
|
| 760 |
lastPixelSeq = O_LENGTH(outline) - 1; |
| 761 |
else |
| 762 |
|
| 763 |
lastPixelSeq = GET_INDEX(cornerList, cornerSeq + 1); |
| 764 |
|
| 765 |
curveP = curve_new(); |
| 766 |
|
| 767 |
for (pixelSeq = cornerPixelSeq; pixelSeq <= lastPixelSeq; ++pixelSeq) |
| 768 |
curve_appendPixel(curveP, O_COORDINATE(outline, pixelSeq)); |
| 769 |
|
| 770 |
curve_appendList(curveListP, curveP); |
| 771 |
{ |
| 772 |
|
| 773 |
|
| 774 |
Curve * const oldCurCurveP = *curCurvePP; |
| 775 |
|
| 776 |
if (oldCurCurveP) { |
| 777 |
NEXT_CURVE(oldCurCurveP) = curveP; |
| 778 |
PREVIOUS_CURVE(curveP) = oldCurCurveP; |
| 779 |
} |
| 780 |
*curCurvePP = curveP; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
static void |
| 787 |
divideOutlineWithCorners(pixel_outline_type const outline, |
| 788 |
IndexList const cornerList, |
| 789 |
CurveList * const curveListP) { |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
unsigned int const firstCurveSeq = CURVE_LIST_LENGTH(*curveListP); |
| 811 |
|
| 812 |
unsigned int cornerSeq; |
| 813 |
Curve * curCurveP; |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
assert(cornerList.length > 0); |
| 819 |
|
| 820 |
curCurveP = NULL; |
| 821 |
|
| 822 |
if (outline.open) { |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
Curve * curveP; |
| 827 |
unsigned int pixelSeq; |
| 828 |
|
| 829 |
curveP = curve_new(); |
| 830 |
|
| 831 |
for (pixelSeq = 0; pixelSeq <= GET_INDEX(cornerList, 0); ++pixelSeq) |
| 832 |
curve_appendPixel(curveP, O_COORDINATE(outline, pixelSeq)); |
| 833 |
|
| 834 |
curve_appendList(curveListP, curveP); |
| 835 |
curCurveP = curveP; |
| 836 |
} else { |
| 837 |
|
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
for (cornerSeq = 0; cornerSeq < cornerList.length; ++cornerSeq) |
| 844 |
addCurveStartingAtCorner(outline, cornerList, cornerSeq, curveListP, |
| 845 |
&curCurveP); |
| 846 |
|
| 847 |
if (!outline.open) { |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
Curve * const firstCurveP = CURVE_LIST_ELT(*curveListP, firstCurveSeq); |
| 853 |
|
| 854 |
unsigned int pixelSeq; |
| 855 |
|
| 856 |
for (pixelSeq = 0; pixelSeq <= GET_INDEX(cornerList, 0); ++pixelSeq) |
| 857 |
curve_appendPixel(curCurveP, O_COORDINATE(outline, pixelSeq)); |
| 858 |
|
| 859 |
NEXT_CURVE(curCurveP) = firstCurveP; |
| 860 |
PREVIOUS_CURVE(firstCurveP) = curCurveP; |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
static CurveListArray |
| 867 |
split_at_corners(pixel_outline_list_type const pixelList, |
| 868 |
fitting_opts_type * const fittingOptsP, |
| 869 |
at_exception_type * const exception) { |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
unsigned outlineSeq; |
| 899 |
CurveListArray curveArray; |
| 900 |
|
| 901 |
curveArray = curve_newListArray(); |
| 902 |
|
| 903 |
LOG("\nFinding corners:\n"); |
| 904 |
|
| 905 |
for (outlineSeq = 0; |
| 906 |
outlineSeq < O_LIST_LENGTH(pixelList); |
| 907 |
++outlineSeq) { |
| 908 |
|
| 909 |
pixel_outline_type const outline = |
| 910 |
O_LIST_OUTLINE(pixelList, outlineSeq); |
| 911 |
|
| 912 |
IndexList cornerList; |
| 913 |
CurveList curveList; |
| 914 |
|
| 915 |
curveList = curve_newList(); |
| 916 |
|
| 917 |
CURVE_LIST_CLOCKWISE(curveList) = O_CLOCKWISE(outline); |
| 918 |
curveList.color = outline.color; |
| 919 |
curveList.open = outline.open; |
| 920 |
|
| 921 |
LOG1("#%u:", outlineSeq); |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
if (O_LENGTH(outline) > fittingOptsP->corner_surround * 2 + 2) |
| 930 |
cornerList = findCorners(outline, fittingOptsP, exception); |
| 931 |
|
| 932 |
else { |
| 933 |
int const surround = (O_LENGTH(outline) - 3) / 2; |
| 934 |
if (surround >= 2) { |
| 935 |
unsigned int const oldCornerSurround = |
| 936 |
fittingOptsP->corner_surround; |
| 937 |
fittingOptsP->corner_surround = surround; |
| 938 |
cornerList = findCorners(outline, fittingOptsP, exception); |
| 939 |
fittingOptsP->corner_surround = oldCornerSurround; |
| 940 |
} else { |
| 941 |
cornerList.length = 0; |
| 942 |
cornerList.data = NULL; |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
if (cornerList.length == 0) |
| 947 |
|
| 948 |
makeOutlineOneCurve(outline, &curveList); |
| 949 |
else |
| 950 |
divideOutlineWithCorners(outline, cornerList, &curveList); |
| 951 |
|
| 952 |
LOG1(" [%u].\n", cornerList.length); |
| 953 |
indexList_free(&cornerList); |
| 954 |
|
| 955 |
|
| 956 |
curve_appendArray(&curveArray, curveList); |
| 957 |
} |
| 958 |
|
| 959 |
return curveArray; |
| 960 |
} |
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
static void |
| 965 |
removeKnees(CurveList const curveList) { |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
unsigned int curveSeq; |
| 972 |
|
| 973 |
LOG("\nRemoving knees:\n"); |
| 974 |
|
| 975 |
for (curveSeq = 0; curveSeq < curveList.length; ++curveSeq) { |
| 976 |
LOG1("#%u:", curveSeq); |
| 977 |
remove_knee_points(CURVE_LIST_ELT(curveList, curveSeq), |
| 978 |
CURVE_LIST_CLOCKWISE(curveList)); |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
static void |
| 985 |
computePointWeights(CurveList const curveList, |
| 986 |
fitting_opts_type * const fittingOptsP, |
| 987 |
distance_map_type * const distP) { |
| 988 |
|
| 989 |
unsigned int const height = distP->height; |
| 990 |
|
| 991 |
unsigned int curveSeq; |
| 992 |
|
| 993 |
for (curveSeq = 0; curveSeq < curveList.length; ++curveSeq) { |
| 994 |
Curve * const curveP = CURVE_LIST_ELT(curveList, curveSeq); |
| 995 |
|
| 996 |
unsigned pointSeq; |
| 997 |
|
| 998 |
for (pointSeq = 0; pointSeq < CURVE_LENGTH(curveP); ++pointSeq) { |
| 999 |
Point * const coordP = &CURVE_POINT(curveP, pointSeq); |
| 1000 |
unsigned int const x = coordP->x; |
| 1001 |
unsigned int const y = height - (unsigned int)coordP->y - 1; |
| 1002 |
|
| 1003 |
float width, w; |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
width = distP->d[y][x]; |
| 1011 |
if (y - 1 >= 0) { |
| 1012 |
if ((w = distP->d[y-1][x]) > width) |
| 1013 |
width = w; |
| 1014 |
if (x - 1 >= 0) { |
| 1015 |
if ((w = distP->d[y][x-1]) > width) |
| 1016 |
width = w; |
| 1017 |
if ((w = distP->d[y-1][x-1]) > width) |
| 1018 |
width = w; |
| 1019 |
} |
| 1020 |
if (x + 1 < distP->width) { |
| 1021 |
if ((w = distP->d[y][x+1]) > width) |
| 1022 |
width = w; |
| 1023 |
if ((w = distP->d[y-1][x+1]) > width) |
| 1024 |
width = w; |
| 1025 |
} |
| 1026 |
} |
| 1027 |
if (y + 1 < height) { |
| 1028 |
if ((w = distP->d[y+1][x]) > width) |
| 1029 |
width = w; |
| 1030 |
if (x - 1 >= 0 && (w = distP->d[y+1][x-1]) > width) |
| 1031 |
width = w; |
| 1032 |
if (x + 1 < distP->width && (w = distP->d[y+1][x+1]) > width) |
| 1033 |
width = w; |
| 1034 |
} |
| 1035 |
coordP->z = width * (fittingOptsP->width_weight_factor); |
| 1036 |
} |
| 1037 |
} |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
static void |
| 1043 |
filterCurves(CurveList const curveList, |
| 1044 |
fitting_opts_type * const fittingOptsP) { |
| 1045 |
|
| 1046 |
unsigned int curveSeq; |
| 1047 |
|
| 1048 |
LOG("\nFiltering curves:\n"); |
| 1049 |
|
| 1050 |
for (curveSeq = 0; curveSeq < curveList.length; ++curveSeq) { |
| 1051 |
LOG1("#%u: ", curveSeq); |
| 1052 |
filter(CURVE_LIST_ELT(curveList, curveSeq), fittingOptsP); |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
static void |
| 1059 |
logSplinesForCurve(unsigned int const curveSeq, |
| 1060 |
spline_list_type const curveSplines) { |
| 1061 |
|
| 1062 |
unsigned int splineSeq; |
| 1063 |
|
| 1064 |
LOG1("Fitted splines for curve #%u:\n", curveSeq); |
| 1065 |
for (splineSeq = 0; |
| 1066 |
splineSeq < SPLINE_LIST_LENGTH(curveSplines); |
| 1067 |
++splineSeq) { |
| 1068 |
LOG1(" %u: ", splineSeq); |
| 1069 |
if (log_file) |
| 1070 |
print_spline(log_file, SPLINE_LIST_ELT(curveSplines, splineSeq)); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
static void |
| 1077 |
changeBadLines(spline_list_type * const splineListP, |
| 1078 |
const fitting_opts_type * const fittingOptsP) { |
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
unsigned int const length = SPLINE_LIST_LENGTH(*splineListP); |
| 1090 |
|
| 1091 |
unsigned int thisSpline; |
| 1092 |
bool foundCubic; |
| 1093 |
|
| 1094 |
LOG1("\nChecking for bad lines (length %u):\n", length); |
| 1095 |
|
| 1096 |
|
| 1097 |
for (thisSpline = 0, foundCubic = false; |
| 1098 |
thisSpline < length; |
| 1099 |
++thisSpline) { |
| 1100 |
if (SPLINE_DEGREE(SPLINE_LIST_ELT(*splineListP, thisSpline)) == |
| 1101 |
CUBICTYPE) { |
| 1102 |
foundCubic = true; |
| 1103 |
break; |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
if (foundCubic) { |
| 1112 |
unsigned int thisSpline; |
| 1113 |
|
| 1114 |
for (thisSpline = 0; thisSpline < length; ++thisSpline) { |
| 1115 |
spline_type const s = SPLINE_LIST_ELT(*splineListP, thisSpline); |
| 1116 |
|
| 1117 |
if (SPLINE_DEGREE(s) == LINEARTYPE) { |
| 1118 |
LOG1(" #%u: ", thisSpline); |
| 1119 |
if (SPLINE_LINEARITY(s) > |
| 1120 |
fittingOptsP->line_reversion_threshold) { |
| 1121 |
LOG("reverted, "); |
| 1122 |
SPLINE_DEGREE(SPLINE_LIST_ELT(*splineListP, thisSpline)) |
| 1123 |
= CUBICTYPE; |
| 1124 |
} |
| 1125 |
LOG1("linearity %.3f.\n", SPLINE_LINEARITY(s)); |
| 1126 |
} |
| 1127 |
} |
| 1128 |
} else |
| 1129 |
LOG(" No lines.\n"); |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
static bool |
| 1135 |
splineLinearEnough(spline_type * const splineP, |
| 1136 |
Curve * const curve, |
| 1137 |
const fitting_opts_type * const fittingOptsP) { |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
float A, B, C; |
| 1144 |
unsigned int thisPoint; |
| 1145 |
float dist; |
| 1146 |
float startEndDist; |
| 1147 |
float threshold; |
| 1148 |
|
| 1149 |
LOG ("Checking linearity:\n"); |
| 1150 |
|
| 1151 |
A = END_POINT(*splineP).x - BEG_POINT(*splineP).x; |
| 1152 |
B = END_POINT(*splineP).y - BEG_POINT(*splineP).y; |
| 1153 |
C = END_POINT(*splineP).z - BEG_POINT(*splineP).z; |
| 1154 |
|
| 1155 |
startEndDist = (float) (SQR(A) + SQR(B) + SQR(C)); |
| 1156 |
LOG1("start_end_distance is %.3f.\n", sqrt(startEndDist)); |
| 1157 |
|
| 1158 |
LOG3(" Line endpoints are (%.3f, %.3f, %.3f) and ", |
| 1159 |
BEG_POINT(*splineP).x, |
| 1160 |
BEG_POINT(*splineP).y, |
| 1161 |
BEG_POINT(*splineP).z); |
| 1162 |
LOG3("(%.3f, %.3f, %.3f)\n", |
| 1163 |
END_POINT(*splineP).x, END_POINT(*splineP).y, END_POINT(*splineP).z); |
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
for (thisPoint = 0, dist = 0.0; |
| 1168 |
thisPoint < CURVE_LENGTH(curve); |
| 1169 |
++thisPoint) { |
| 1170 |
|
| 1171 |
float const t = CURVE_DIST(curve, thisPoint); |
| 1172 |
Point const splinePoint = evaluate_spline(*splineP, t); |
| 1173 |
|
| 1174 |
float const a = splinePoint.x - BEG_POINT(*splineP).x; |
| 1175 |
float const b = splinePoint.y - BEG_POINT(*splineP).y; |
| 1176 |
float const c = splinePoint.z - BEG_POINT(*splineP).z; |
| 1177 |
|
| 1178 |
float const w = (A*a + B*b + C*c) / startEndDist; |
| 1179 |
|
| 1180 |
dist += (float)sqrt(SQR(a-A*w) + SQR(b-B*w) + SQR(c-C*w)); |
| 1181 |
} |
| 1182 |
LOG1(" Total distance is %.3f, ", dist); |
| 1183 |
|
| 1184 |
dist /= (CURVE_LENGTH (curve) - 1); |
| 1185 |
|
| 1186 |
LOG1 ("which is %.3f normalized.\n", dist); |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
SPLINE_LINEARITY(*splineP) = dist; |
| 1193 |
LOG1(" Final linearity: %.3f.\n", SPLINE_LINEARITY (*splineP)); |
| 1194 |
|
| 1195 |
if (startEndDist * (float) 0.5 > fittingOptsP->line_threshold) |
| 1196 |
threshold = fittingOptsP->line_threshold; |
| 1197 |
else |
| 1198 |
threshold = startEndDist * (float) 0.5; |
| 1199 |
LOG1("threshold is %.3f .\n", threshold); |
| 1200 |
|
| 1201 |
if (dist < threshold) |
| 1202 |
return true; |
| 1203 |
else |
| 1204 |
return false; |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
static spline_list_type * |
| 1212 |
fitCurve(Curve * const curveP, |
| 1213 |
Vector const begSlope, |
| 1214 |
Vector const endSlope, |
| 1215 |
const fitting_opts_type * const fittingOptsP, |
| 1216 |
at_exception_type * const exceptionP); |
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
static spline_list_type * |
| 1221 |
fitWithLine(Curve * const curveP) { |
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
spline_type line; |
| 1230 |
|
| 1231 |
LOG("Fitting with straight line:\n"); |
| 1232 |
|
| 1233 |
SPLINE_DEGREE(line) = LINEARTYPE; |
| 1234 |
BEG_POINT(line) = CONTROL1(line) = CURVE_POINT(curveP, 0); |
| 1235 |
END_POINT(line) = CONTROL2(line) = LAST_CURVE_POINT(curveP); |
| 1236 |
|
| 1237 |
|
| 1238 |
SPLINE_LINEARITY(line) = 0; |
| 1239 |
|
| 1240 |
if (log_file) { |
| 1241 |
LOG(" "); |
| 1242 |
print_spline(log_file, line); |
| 1243 |
} |
| 1244 |
|
| 1245 |
return new_spline_list_with_spline(line); |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
static float |
| 1251 |
b2(float const fracCurveDist) { |
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
return 3.0 * SQR(fracCurveDist) * (1.0 - fracCurveDist); |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
struct Mat22 { |
| 1264 |
struct { float beg; float end; } beg; |
| 1265 |
struct { float beg; float end; } end; |
| 1266 |
}; |
| 1267 |
|
| 1268 |
struct Mat2 { float beg; float end; }; |
| 1269 |
|
| 1270 |
struct VectorBegEndPair { |
| 1271 |
Vector beg; |
| 1272 |
Vector end; |
| 1273 |
}; |
| 1274 |
|
| 1275 |
|
| 1276 |
static void |
| 1277 |
computeCX(Curve * const curveP, |
| 1278 |
struct VectorBegEndPair const tang, |
| 1279 |
struct Mat22 * const cP, |
| 1280 |
struct Mat2 * const xP) { |
| 1281 |
|
| 1282 |
Vector const begVector = vector_fromPoint(CURVE_POINT(curveP, 0)); |
| 1283 |
Vector const endVector = vector_fromPoint(LAST_CURVE_POINT(curveP)); |
| 1284 |
|
| 1285 |
unsigned int pointSeq; |
| 1286 |
|
| 1287 |
cP->beg.beg = 0.0; cP->beg.end = 0.0; cP->end.beg = 0.0; cP->end.end = 0.0; |
| 1288 |
|
| 1289 |
|
| 1290 |
xP->beg = 0.0; xP->end = 0.0; |
| 1291 |
|
| 1292 |
for (pointSeq = 0; pointSeq < CURVE_LENGTH(curveP); ++pointSeq) { |
| 1293 |
float const curveDistFmBeg = CURVE_DIST(curveP, pointSeq); |
| 1294 |
float const curveDistToEnd = 1.0 - curveDistFmBeg; |
| 1295 |
struct VectorBegEndPair a; |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
Vector temp, temp0, temp1; |
| 1306 |
|
| 1307 |
a.beg = vector_scaled(tang.beg, b2(curveDistToEnd)); |
| 1308 |
a.end = vector_scaled(tang.end, b2(curveDistFmBeg)); |
| 1309 |
|
| 1310 |
cP->beg.beg += vector_dotProduct(a.beg, a.beg); |
| 1311 |
cP->beg.end += vector_dotProduct(a.beg, a.end); |
| 1312 |
cP->end.beg += vector_dotProduct(a.end, a.beg); |
| 1313 |
cP->end.end += vector_dotProduct(a.end, a.end); |
| 1314 |
|
| 1315 |
|
| 1316 |
temp0 = vector_scaled(begVector, |
| 1317 |
CUBE(curveDistToEnd) + b2(curveDistToEnd)); |
| 1318 |
temp1 = vector_scaled(endVector, |
| 1319 |
CUBE(curveDistFmBeg) + b2(curveDistFmBeg)); |
| 1320 |
|
| 1321 |
temp = vector_fromPoint( |
| 1322 |
vector_diffPoint( |
| 1323 |
CURVE_POINT(curveP, pointSeq), vector_sum(temp0, temp1))); |
| 1324 |
|
| 1325 |
xP->beg += vector_dotProduct(temp, a.beg); |
| 1326 |
xP->end += vector_dotProduct(temp, a.end); |
| 1327 |
} |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
static spline_type |
| 1333 |
fitOneSpline(Curve * const curveP, |
| 1334 |
Vector const begSlope, |
| 1335 |
Vector const endSlope, |
| 1336 |
at_exception_type * const exceptionP) { |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
struct VectorBegEndPair tang; |
| 1370 |
spline_type spline; |
| 1371 |
struct Mat22 C; |
| 1372 |
struct Mat2 X; |
| 1373 |
|
| 1374 |
tang.beg = begSlope; tang.end = endSlope; |
| 1375 |
|
| 1376 |
computeCX(curveP, tang, &C, &X); |
| 1377 |
|
| 1378 |
{ |
| 1379 |
float const XCendDet = X.beg * C.end.end - X.end * C.beg.end; |
| 1380 |
float const CbegXDet = C.beg.beg * X.end - C.beg.end * X.beg; |
| 1381 |
float const CDet = C.beg.beg * C.end.end - C.end.beg * C.beg.end; |
| 1382 |
|
| 1383 |
if (CDet == 0.0) { |
| 1384 |
LOG("zero determinant of C matrix"); |
| 1385 |
at_exception_fatal(exceptionP, "zero determinant of C matrix"); |
| 1386 |
} else { |
| 1387 |
|
| 1388 |
float const alphaBeg = XCendDet / CDet; |
| 1389 |
float const alphaEnd = CbegXDet / CDet; |
| 1390 |
|
| 1391 |
BEG_POINT(spline) = CURVE_POINT(curveP, 0); |
| 1392 |
END_POINT(spline) = LAST_CURVE_POINT(curveP); |
| 1393 |
CONTROL1(spline) = vector_sumPoint( |
| 1394 |
BEG_POINT(spline), vector_scaled(tang.beg, alphaBeg)); |
| 1395 |
CONTROL2(spline) = vector_sumPoint( |
| 1396 |
END_POINT(spline), vector_scaled(tang.end, alphaEnd)); |
| 1397 |
SPLINE_DEGREE(spline) = CUBICTYPE; |
| 1398 |
} |
| 1399 |
} |
| 1400 |
return spline; |
| 1401 |
} |
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
static void |
| 1406 |
logSplineFit(spline_type const spline) { |
| 1407 |
|
| 1408 |
if (SPLINE_DEGREE(spline) == LINEARTYPE) |
| 1409 |
LOG(" fitted to line:\n"); |
| 1410 |
else |
| 1411 |
LOG(" fitted to spline:\n"); |
| 1412 |
|
| 1413 |
if (log_file) { |
| 1414 |
LOG (" "); |
| 1415 |
print_spline(log_file, spline); |
| 1416 |
} |
| 1417 |
} |
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
static Vector |
| 1422 |
findHalfTangent(LineEnd const toWhichEnd, |
| 1423 |
Curve * const curveP, |
| 1424 |
unsigned int const tangentSurround) { |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
Point const tangentPoint = |
| 1451 |
CURVE_POINT(curveP, |
| 1452 |
toWhichEnd == LINEEND_INIT ? 0 : CURVE_LENGTH(curveP) - 1); |
| 1453 |
Vector const zeroZero = { 0.0, 0.0 }; |
| 1454 |
|
| 1455 |
unsigned int surroundCt; |
| 1456 |
bool gotNonzero; |
| 1457 |
Vector mean; |
| 1458 |
|
| 1459 |
for (surroundCt = MIN(CURVE_LENGTH(curveP) / 2, tangentSurround), |
| 1460 |
gotNonzero = false; |
| 1461 |
!gotNonzero; |
| 1462 |
--surroundCt) { |
| 1463 |
|
| 1464 |
unsigned int i; |
| 1465 |
Vector sum; |
| 1466 |
unsigned int n; |
| 1467 |
|
| 1468 |
for (i = 0, n = 0, sum = zeroZero; i < surroundCt; ++i) { |
| 1469 |
unsigned int const thisIndex = |
| 1470 |
toWhichEnd == LINEEND_INIT ? |
| 1471 |
i + 1 : CURVE_LENGTH(curveP) - 1 - i; |
| 1472 |
Point const thisPoint = CURVE_POINT(curveP, thisIndex); |
| 1473 |
|
| 1474 |
if (!point_equal(thisPoint, tangentPoint)) { |
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
sum = vector_sum(sum, vector_pointDirection(thisPoint, |
| 1480 |
tangentPoint)); |
| 1481 |
++n; |
| 1482 |
} |
| 1483 |
} |
| 1484 |
mean = n > 0 ? vector_scaled(sum, 1.0 / n) : vector_horizontal(); |
| 1485 |
|
| 1486 |
if (vector_equal(mean, vector_zero())) { |
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
assert(surroundCt > 0); |
| 1491 |
} else |
| 1492 |
gotNonzero = true; |
| 1493 |
} |
| 1494 |
|
| 1495 |
return mean; |
| 1496 |
} |
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
static void |
| 1501 |
findTangent(Curve * const curveP, |
| 1502 |
LineEnd const toWhichEnd, |
| 1503 |
Curve * const adjacentCurveP, |
| 1504 |
unsigned int const tangentSurround, |
| 1505 |
Vector * const tangentP) { |
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
Vector const slopeThisCurve = |
| 1531 |
findHalfTangent(toWhichEnd, curveP, tangentSurround); |
| 1532 |
|
| 1533 |
LOG2(" tangent to %s of curve %lx: ", |
| 1534 |
toWhichEnd == LINEEND_INIT ? "start" : "end", (unsigned long)curveP); |
| 1535 |
|
| 1536 |
LOG3("(this curve half tangent (%.3f,%.3f,%.3f)) ", |
| 1537 |
slopeThisCurve.dx, slopeThisCurve.dy, slopeThisCurve.dz); |
| 1538 |
|
| 1539 |
if (adjacentCurveP) { |
| 1540 |
Vector const slopeAdjCurve = |
| 1541 |
findHalfTangent(otherEnd(toWhichEnd), |
| 1542 |
adjacentCurveP, |
| 1543 |
tangentSurround); |
| 1544 |
|
| 1545 |
LOG3("(adjacent curve half tangent (%.3f,%.3f,%.3f)) ", |
| 1546 |
slopeAdjCurve.dx, slopeAdjCurve.dy, slopeAdjCurve.dz); |
| 1547 |
*tangentP = vector_scaled(vector_sum(slopeThisCurve, slopeAdjCurve), |
| 1548 |
0.5); |
| 1549 |
} else |
| 1550 |
*tangentP = slopeThisCurve; |
| 1551 |
|
| 1552 |
LOG3("(%.3f,%.3f,%.3f).\n", tangentP->dx, tangentP->dy, tangentP->dz); |
| 1553 |
} |
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
static void |
| 1558 |
findError(Curve * const curveP, |
| 1559 |
spline_type const spline, |
| 1560 |
float * const errorP, |
| 1561 |
unsigned int * const worstPointP, |
| 1562 |
at_exception_type * const exceptionP) { |
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
unsigned int thisPoint; |
| 1575 |
float totalError; |
| 1576 |
float worstError; |
| 1577 |
unsigned int worstPoint; |
| 1578 |
|
| 1579 |
assert(CURVE_LENGTH(curveP) > 0); |
| 1580 |
|
| 1581 |
totalError = 0.0; |
| 1582 |
worstError = FLT_MIN; |
| 1583 |
worstPoint = 0; |
| 1584 |
|
| 1585 |
for (thisPoint = 0; thisPoint < CURVE_LENGTH(curveP); ++thisPoint) { |
| 1586 |
Point const curvePoint = CURVE_POINT(curveP, thisPoint); |
| 1587 |
float const t = CURVE_DIST(curveP, thisPoint); |
| 1588 |
Point const splinePoint = evaluate_spline(spline, t); |
| 1589 |
float const thisError = point_distance(curvePoint, splinePoint); |
| 1590 |
if (thisError >= worstError) { |
| 1591 |
worstPoint = thisPoint; |
| 1592 |
worstError = thisError; |
| 1593 |
} |
| 1594 |
totalError += thisError; |
| 1595 |
} |
| 1596 |
|
| 1597 |
if (epsilon_equal(totalError, 0.0)) |
| 1598 |
LOG(" Every point fits perfectly.\n"); |
| 1599 |
else { |
| 1600 |
LOG5(" Worst error (at (%.3f,%.3f,%.3f), point #%u) was %.3f.\n", |
| 1601 |
CURVE_POINT(curveP, worstPoint).x, |
| 1602 |
CURVE_POINT(curveP, worstPoint).y, |
| 1603 |
CURVE_POINT(curveP, worstPoint).z, |
| 1604 |
worstPoint, worstError); |
| 1605 |
LOG1(" Total error was %.3f.\n", totalError); |
| 1606 |
LOG2(" Average error (over %u points) was %.3f.\n", |
| 1607 |
CURVE_LENGTH(curveP), totalError / CURVE_LENGTH(curveP)); |
| 1608 |
} |
| 1609 |
assert(worstPoint < CURVE_LENGTH(curveP)); |
| 1610 |
*errorP = worstError; |
| 1611 |
*worstPointP = worstPoint; |
| 1612 |
} |
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
static void |
| 1617 |
subdivideCurve(Curve * const curveP, |
| 1618 |
unsigned int const subdivisionIndex, |
| 1619 |
const fitting_opts_type * const fittingOptsP, |
| 1620 |
Curve ** const leftCurvePP, |
| 1621 |
Curve ** const rghtCurvePP, |
| 1622 |
Vector * const joinSlopeP) { |
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
Curve * leftCurveP; |
| 1636 |
Curve * rghtCurveP; |
| 1637 |
|
| 1638 |
leftCurveP = curve_new(); |
| 1639 |
rghtCurveP = curve_new(); |
| 1640 |
|
| 1641 |
LOG4(" Subdividing curve %lx into %lx and %lx at point #%u\n", |
| 1642 |
(unsigned long)curveP, |
| 1643 |
(unsigned long)leftCurveP, (unsigned long)rghtCurveP, |
| 1644 |
subdivisionIndex); |
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
assert(subdivisionIndex < CURVE_LENGTH(curveP)); |
| 1650 |
CURVE_LENGTH(leftCurveP) = subdivisionIndex + 1; |
| 1651 |
CURVE_LENGTH(rghtCurveP) = CURVE_LENGTH(curveP) - subdivisionIndex; |
| 1652 |
|
| 1653 |
MALLOCARRAY_NOFAIL(leftCurveP->pointList, CURVE_LENGTH(leftCurveP)); |
| 1654 |
memcpy(leftCurveP->pointList, &curveP->pointList[0], |
| 1655 |
CURVE_LENGTH(leftCurveP) * sizeof(curveP->pointList[0])); |
| 1656 |
|
| 1657 |
MALLOCARRAY_NOFAIL(rghtCurveP->pointList, CURVE_LENGTH(rghtCurveP)); |
| 1658 |
memcpy(rghtCurveP->pointList, &curveP->pointList[subdivisionIndex], |
| 1659 |
CURVE_LENGTH(rghtCurveP) * sizeof(curveP->pointList[0])); |
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
findTangent(leftCurveP, |
| 1668 |
LINEEND_TERM, |
| 1669 |
rghtCurveP, |
| 1670 |
fittingOptsP->tangent_surround, joinSlopeP); |
| 1671 |
|
| 1672 |
*leftCurvePP = leftCurveP; |
| 1673 |
*rghtCurvePP = rghtCurveP; |
| 1674 |
} |
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
static spline_list_type * |
| 1679 |
leftRightConcat(const spline_list_type * const leftSplineListP, |
| 1680 |
const spline_list_type * const rghtSplineListP, |
| 1681 |
at_exception_type * const exceptionP) { |
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
spline_list_type * retval; |
| 1689 |
|
| 1690 |
retval = new_spline_list(); |
| 1691 |
|
| 1692 |
if (leftSplineListP == NULL) { |
| 1693 |
LOG("Could not fit spline to left curve.\n"); |
| 1694 |
at_exception_warning(exceptionP, "Could not fit left spline list"); |
| 1695 |
} else |
| 1696 |
concat_spline_lists(retval, *leftSplineListP); |
| 1697 |
|
| 1698 |
if (rghtSplineListP == NULL) { |
| 1699 |
LOG("Could not fit spline to right curve.\n"); |
| 1700 |
at_exception_warning(exceptionP, "Could not fit right spline list"); |
| 1701 |
} else |
| 1702 |
concat_spline_lists(retval, *rghtSplineListP); |
| 1703 |
|
| 1704 |
return retval; |
| 1705 |
} |
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
static unsigned int |
| 1710 |
divisionPoint(Curve * const curveP, |
| 1711 |
unsigned int const worstFitPoint) { |
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
assert(CURVE_LENGTH(curveP) >= 4); |
| 1727 |
|
| 1728 |
return MAX(2, MIN(worstFitPoint, CURVE_LENGTH(curveP) - 2)); |
| 1729 |
} |
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
static spline_list_type * |
| 1734 |
divideAndFit(Curve * const curveP, |
| 1735 |
Vector const begSlope, |
| 1736 |
Vector const endSlope, |
| 1737 |
unsigned int const subdivisionIndex, |
| 1738 |
const fitting_opts_type * const fittingOptsP, |
| 1739 |
at_exception_type * const exceptionP) { |
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
spline_list_type * retval; |
| 1752 |
Curve * leftCurveP; |
| 1753 |
|
| 1754 |
Curve * rghtCurveP; |
| 1755 |
|
| 1756 |
Vector joinSlope; |
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
spline_list_type * leftSplineListP; |
| 1761 |
|
| 1762 |
assert(subdivisionIndex > 1); |
| 1763 |
assert(subdivisionIndex < CURVE_LENGTH(curveP)-1); |
| 1764 |
subdivideCurve(curveP, subdivisionIndex, fittingOptsP, |
| 1765 |
&leftCurveP, &rghtCurveP, &joinSlope); |
| 1766 |
|
| 1767 |
leftSplineListP = fitCurve(leftCurveP, begSlope, joinSlope, |
| 1768 |
fittingOptsP, exceptionP); |
| 1769 |
|
| 1770 |
if (!at_exception_got_fatal(exceptionP)) { |
| 1771 |
spline_list_type * rghtSplineListP; |
| 1772 |
|
| 1773 |
rghtSplineListP = fitCurve(rghtCurveP, joinSlope, endSlope, |
| 1774 |
fittingOptsP, exceptionP); |
| 1775 |
|
| 1776 |
if (!at_exception_got_fatal(exceptionP)) { |
| 1777 |
if (leftSplineListP == NULL && rghtSplineListP == NULL) |
| 1778 |
retval = NULL; |
| 1779 |
else |
| 1780 |
retval = leftRightConcat(leftSplineListP, rghtSplineListP, |
| 1781 |
exceptionP); |
| 1782 |
|
| 1783 |
if (rghtSplineListP) { |
| 1784 |
free_spline_list(*rghtSplineListP); |
| 1785 |
free(rghtSplineListP); |
| 1786 |
} |
| 1787 |
} |
| 1788 |
if (leftSplineListP) { |
| 1789 |
free_spline_list(*leftSplineListP); |
| 1790 |
free(leftSplineListP); |
| 1791 |
} |
| 1792 |
} |
| 1793 |
|
| 1794 |
curve_free(leftCurveP); |
| 1795 |
curve_free(rghtCurveP); |
| 1796 |
|
| 1797 |
return retval; |
| 1798 |
} |
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
static spline_list_type * |
| 1803 |
fitWithLeastSquares(Curve * const curveP, |
| 1804 |
Vector const begSlope, |
| 1805 |
Vector const endSlope, |
| 1806 |
const fitting_opts_type * const fittingOptsP, |
| 1807 |
at_exception_type * const exceptionP) { |
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
spline_list_type * retval; |
| 1814 |
spline_type spline; |
| 1815 |
|
| 1816 |
LOG("\nFitting with least squares:\n"); |
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
curve_setDistance(curveP); |
| 1824 |
|
| 1825 |
if (CURVE_CYCLIC(curveP) && CURVE_LENGTH(curveP) < 4) { |
| 1826 |
unsigned i; |
| 1827 |
for (i = 0; i < CURVE_LENGTH(curveP); ++i) { |
| 1828 |
Point const point = CURVE_POINT(curveP, i); |
| 1829 |
fprintf(stderr, "point %u = (%f, %f)\n", i, point.x, point.y); |
| 1830 |
} |
| 1831 |
} |
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
spline = fitOneSpline(curveP, begSlope, endSlope, exceptionP); |
| 1836 |
if (!at_exception_got_fatal(exceptionP)) { |
| 1837 |
float error; |
| 1838 |
unsigned int worstPoint; |
| 1839 |
|
| 1840 |
logSplineFit(spline); |
| 1841 |
|
| 1842 |
findError(curveP, spline, &error, &worstPoint, exceptionP); |
| 1843 |
assert(worstPoint < CURVE_LENGTH(curveP)); |
| 1844 |
|
| 1845 |
if (error < fittingOptsP->error_threshold && !CURVE_CYCLIC(curveP)) { |
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
if (splineLinearEnough(&spline, curveP, fittingOptsP)) { |
| 1851 |
SPLINE_DEGREE(spline) = LINEARTYPE; |
| 1852 |
LOG("Changed to line.\n"); |
| 1853 |
} |
| 1854 |
retval = new_spline_list_with_spline(spline); |
| 1855 |
LOG1("Accepted error of %.3f.\n", error); |
| 1856 |
} else { |
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
unsigned int const divIndex = divisionPoint(curveP, worstPoint); |
| 1861 |
LOG1("\nSubdividing at point #%u\n", divIndex); |
| 1862 |
LOG4(" Worst match point: (%.3f,%.3f), #%u. Error %.3f\n", |
| 1863 |
CURVE_POINT(curveP, worstPoint).x, |
| 1864 |
CURVE_POINT(curveP, worstPoint).y, worstPoint, error); |
| 1865 |
|
| 1866 |
retval = divideAndFit(curveP, begSlope, endSlope, divIndex, |
| 1867 |
fittingOptsP, exceptionP); |
| 1868 |
} |
| 1869 |
} else |
| 1870 |
retval = NULL; |
| 1871 |
|
| 1872 |
return retval; |
| 1873 |
} |
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
static spline_list_type * |
| 1878 |
fitCurve(Curve * const curveP, |
| 1879 |
Vector const begSlope, |
| 1880 |
Vector const endSlope, |
| 1881 |
const fitting_opts_type * const fittingOptsP, |
| 1882 |
at_exception_type * const exceptionP) { |
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
spline_list_type * fittedSplinesP; |
| 1889 |
|
| 1890 |
if (CURVE_LENGTH(curveP) < 2) { |
| 1891 |
LOG("Tried to fit curve with fewer than two points"); |
| 1892 |
at_exception_warning(exceptionP, |
| 1893 |
"Tried to fit curve with less than two points"); |
| 1894 |
fittedSplinesP = NULL; |
| 1895 |
} else if (CURVE_LENGTH(curveP) < 4) |
| 1896 |
fittedSplinesP = fitWithLine(curveP); |
| 1897 |
else |
| 1898 |
fittedSplinesP = |
| 1899 |
fitWithLeastSquares(curveP, begSlope, endSlope, fittingOptsP, |
| 1900 |
exceptionP); |
| 1901 |
|
| 1902 |
return fittedSplinesP; |
| 1903 |
} |
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
static void |
| 1908 |
fitCurves(CurveList const curveList, |
| 1909 |
pixel const color, |
| 1910 |
const fitting_opts_type * const fittingOptsP, |
| 1911 |
spline_list_type * const splinesP, |
| 1912 |
at_exception_type * const exceptionP) { |
| 1913 |
|
| 1914 |
spline_list_type curveListSplines; |
| 1915 |
unsigned int curveSeq; |
| 1916 |
|
| 1917 |
curveListSplines = empty_spline_list(); |
| 1918 |
|
| 1919 |
curveListSplines.open = curveList.open; |
| 1920 |
curveListSplines.clockwise = curveList.clockwise; |
| 1921 |
curveListSplines.color = color; |
| 1922 |
|
| 1923 |
for (curveSeq = 0; |
| 1924 |
curveSeq < curveList.length && !at_exception_got_fatal(exceptionP); |
| 1925 |
++curveSeq) { |
| 1926 |
|
| 1927 |
Curve * const curveP = CURVE_LIST_ELT(curveList, curveSeq); |
| 1928 |
|
| 1929 |
Vector begSlope, endSlope; |
| 1930 |
spline_list_type * curveSplinesP; |
| 1931 |
|
| 1932 |
LOG2("\nFitting curve #%u (%lx):\n", curveSeq, (unsigned long)curveP); |
| 1933 |
|
| 1934 |
LOG("Finding tangents:\n"); |
| 1935 |
findTangent(curveP, LINEEND_INIT, |
| 1936 |
CURVE_CYCLIC(curveP) ? curveP : NULL, |
| 1937 |
fittingOptsP->tangent_surround, |
| 1938 |
&begSlope); |
| 1939 |
findTangent(curveP, LINEEND_TERM, |
| 1940 |
CURVE_CYCLIC(curveP) ? curveP : NULL, |
| 1941 |
fittingOptsP->tangent_surround, &endSlope); |
| 1942 |
|
| 1943 |
curveSplinesP = fitCurve(curveP, begSlope, endSlope, fittingOptsP, |
| 1944 |
exceptionP); |
| 1945 |
if (!at_exception_got_fatal(exceptionP)) { |
| 1946 |
if (curveSplinesP == NULL) { |
| 1947 |
LOG1("Could not fit curve #%u", curveSeq); |
| 1948 |
at_exception_warning(exceptionP, "Could not fit curve"); |
| 1949 |
} else { |
| 1950 |
logSplinesForCurve(curveSeq, *curveSplinesP); |
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
changeBadLines(curveSplinesP, fittingOptsP); |
| 1957 |
|
| 1958 |
concat_spline_lists(&curveListSplines, *curveSplinesP); |
| 1959 |
free_spline_list(*curveSplinesP); |
| 1960 |
free(curveSplinesP); |
| 1961 |
} |
| 1962 |
} |
| 1963 |
} |
| 1964 |
if (at_exception_got_fatal(exceptionP)) |
| 1965 |
free_spline_list(curveListSplines); |
| 1966 |
else |
| 1967 |
*splinesP = curveListSplines; |
| 1968 |
} |
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
static void |
| 1973 |
logFittedSplines(spline_list_type const curve_list_splines) { |
| 1974 |
|
| 1975 |
unsigned int splineSeq; |
| 1976 |
|
| 1977 |
LOG("\nFitted splines are:\n"); |
| 1978 |
for (splineSeq = 0; |
| 1979 |
splineSeq < SPLINE_LIST_LENGTH(curve_list_splines); |
| 1980 |
++splineSeq) { |
| 1981 |
LOG1(" %u: ", splineSeq); |
| 1982 |
print_spline(log_file, |
| 1983 |
SPLINE_LIST_ELT(curve_list_splines, splineSeq)); |
| 1984 |
} |
| 1985 |
} |
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
static void |
| 1990 |
fitCurveList(CurveList const curveList, |
| 1991 |
fitting_opts_type * const fittingOptsP, |
| 1992 |
distance_map_type * const dist, |
| 1993 |
pixel const color, |
| 1994 |
spline_list_type * const splineListP, |
| 1995 |
at_exception_type * const exception) { |
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
Curve * curveP; |
| 2002 |
spline_list_type curveListSplines; |
| 2003 |
|
| 2004 |
removeKnees(curveList); |
| 2005 |
|
| 2006 |
if (dist != NULL) |
| 2007 |
computePointWeights(curveList, fittingOptsP, dist); |
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
filterCurves(curveList, fittingOptsP); |
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
curveP = CURVE_LIST_ELT(curveList, 0); |
| 2027 |
if (CURVE_CYCLIC(curveP)) |
| 2028 |
curve_appendPoint(curveP, CURVE_POINT(curveP, 0)); |
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
fitCurves(curveList, color, fittingOptsP, &curveListSplines, exception); |
| 2033 |
if (!at_exception_got_fatal(exception)) { |
| 2034 |
if (log_file) |
| 2035 |
logFittedSplines(curveListSplines); |
| 2036 |
*splineListP = curveListSplines; |
| 2037 |
} |
| 2038 |
} |
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
static void |
| 2043 |
fitCurvesToSplines(CurveListArray const curveArray, |
| 2044 |
fitting_opts_type * const fittingOptsP, |
| 2045 |
distance_map_type * const dist, |
| 2046 |
unsigned short const width, |
| 2047 |
unsigned short const height, |
| 2048 |
at_exception_type * const exception, |
| 2049 |
at_progress_func notifyProgress, |
| 2050 |
void * const progressData, |
| 2051 |
at_testcancel_func testCancel, |
| 2052 |
void * const testcancelData, |
| 2053 |
spline_list_array_type * const splineListArrayP) { |
| 2054 |
|
| 2055 |
unsigned splineListSeq; |
| 2056 |
bool cancelled; |
| 2057 |
spline_list_array_type splineListArray; |
| 2058 |
|
| 2059 |
splineListArray = new_spline_list_array(); |
| 2060 |
splineListArray.centerline = fittingOptsP->centerline; |
| 2061 |
splineListArray.preserve_width = fittingOptsP->preserve_width; |
| 2062 |
splineListArray.width_weight_factor = fittingOptsP->width_weight_factor; |
| 2063 |
splineListArray.backgroundSpec = fittingOptsP->backgroundSpec; |
| 2064 |
splineListArray.background_color = fittingOptsP->background_color; |
| 2065 |
|
| 2066 |
splineListArray.width = width; |
| 2067 |
splineListArray.height = height; |
| 2068 |
|
| 2069 |
for (splineListSeq = 0, cancelled = false; |
| 2070 |
splineListSeq < CURVE_LIST_ARRAY_LENGTH(curveArray) && |
| 2071 |
!at_exception_got_fatal(exception) && !cancelled; |
| 2072 |
++splineListSeq) { |
| 2073 |
|
| 2074 |
CurveList const curveList = |
| 2075 |
CURVE_LIST_ARRAY_ELT(curveArray, splineListSeq); |
| 2076 |
|
| 2077 |
spline_list_type curveSplineList; |
| 2078 |
|
| 2079 |
if (notifyProgress) |
| 2080 |
notifyProgress((((float)splineListSeq)/ |
| 2081 |
((float)CURVE_LIST_ARRAY_LENGTH(curveArray) * |
| 2082 |
(float)3.0) + (float)0.333), |
| 2083 |
progressData); |
| 2084 |
if (testCancel && testCancel(testcancelData)) |
| 2085 |
cancelled = true; |
| 2086 |
|
| 2087 |
LOG1("\nFitting curve list #%u:\n", splineListSeq); |
| 2088 |
|
| 2089 |
fitCurveList(curveList, fittingOptsP, dist, curveList.color, |
| 2090 |
&curveSplineList, exception); |
| 2091 |
if (!at_exception_got_fatal(exception)) |
| 2092 |
append_spline_list(&splineListArray, curveSplineList); |
| 2093 |
} |
| 2094 |
*splineListArrayP = splineListArray; |
| 2095 |
} |
| 2096 |
|
| 2097 |
|
| 2098 |
|
| 2099 |
void |
| 2100 |
fit_outlines_to_splines(pixel_outline_list_type const pixelOutlineList, |
| 2101 |
fitting_opts_type * const fittingOptsP, |
| 2102 |
distance_map_type * const dist, |
| 2103 |
unsigned short const width, |
| 2104 |
unsigned short const height, |
| 2105 |
at_exception_type * const exception, |
| 2106 |
at_progress_func notifyProgress, |
| 2107 |
void * const progressData, |
| 2108 |
at_testcancel_func testCancel, |
| 2109 |
void * const testcancelData, |
| 2110 |
spline_list_array_type * const splineListArrayP) { |
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
CurveListArray const curveListArray = |
| 2116 |
split_at_corners(pixelOutlineList, fittingOptsP, exception); |
| 2117 |
|
| 2118 |
fitCurvesToSplines(curveListArray, fittingOptsP, dist, width, height, |
| 2119 |
exception, notifyProgress, progressData, |
| 2120 |
testCancel, testcancelData, splineListArrayP); |
| 2121 |
|
| 2122 |
curve_freeListArray(&curveListArray, notifyProgress, progressData); |
| 2123 |
|
| 2124 |
flush_log_output(); |
| 2125 |
} |
| 2126 |
|
| 2127 |
|
| 2128 |
|