| 1 |
#include "bltInt.h" |
| 2 |
|
| 3 |
typedef double TriDiagonalMatrix[3]; |
| 4 |
typedef struct { |
| 5 |
double b, c, d; |
| 6 |
} Cubic2D; |
| 7 |
|
| 8 |
typedef struct { |
| 9 |
double b, c, d, e, f; |
| 10 |
} Quint2D; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#define E1 param[0] |
| 16 |
#define E2 param[1] |
| 17 |
#define V1 param[2] |
| 18 |
#define V2 param[3] |
| 19 |
#define W1 param[4] |
| 20 |
#define W2 param[5] |
| 21 |
#define Z1 param[6] |
| 22 |
#define Z2 param[7] |
| 23 |
#define Y1 param[8] |
| 24 |
#define Y2 param[9] |
| 25 |
|
| 26 |
static Tcl_CmdProc SplineCmd; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
static int |
| 43 |
Search(points, nPoints, key, foundPtr) |
| 44 |
Point2D points[]; |
| 45 |
|
| 46 |
int nPoints; |
| 47 |
double key; |
| 48 |
|
| 49 |
int *foundPtr; |
| 50 |
|
| 51 |
{ |
| 52 |
int high, low, mid; |
| 53 |
|
| 54 |
low = 0; |
| 55 |
high = nPoints - 1; |
| 56 |
|
| 57 |
while (high >= low) { |
| 58 |
mid = (high + low) / 2; |
| 59 |
if (key > points[mid].x) { |
| 60 |
low = mid + 1; |
| 61 |
} else if (key < points[mid].x) { |
| 62 |
high = mid - 1; |
| 63 |
} else { |
| 64 |
*foundPtr = 1; |
| 65 |
return mid; |
| 66 |
} |
| 67 |
} |
| 68 |
*foundPtr = 0; |
| 69 |
return low; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static int |
| 87 |
QuadChoose(p, q, m1, m2, epsilon) |
| 88 |
Point2D *p; |
| 89 |
|
| 90 |
Point2D *q; |
| 91 |
|
| 92 |
double m1; |
| 93 |
double m2; |
| 94 |
double epsilon; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
{ |
| 103 |
double slope; |
| 104 |
|
| 105 |
|
| 106 |
slope = (q->y - p->y) / (q->x - p->x); |
| 107 |
|
| 108 |
if (slope != 0.0) { |
| 109 |
double relerr; |
| 110 |
double mref, mref1, mref2, prod1, prod2; |
| 111 |
|
| 112 |
prod1 = slope * m1; |
| 113 |
prod2 = slope * m2; |
| 114 |
|
| 115 |
|
| 116 |
mref = FABS(slope); |
| 117 |
mref1 = FABS(m1); |
| 118 |
mref2 = FABS(m2); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
relerr = epsilon * mref; |
| 125 |
if ((FABS(slope - m1) > relerr) && (FABS(slope - m2) > relerr) && |
| 126 |
(prod1 >= 0.0) && (prod2 >= 0.0)) { |
| 127 |
double prod; |
| 128 |
|
| 129 |
prod = (mref - mref1) * (mref - mref2); |
| 130 |
if (prod < 0.0) { |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
return 1; |
| 138 |
} |
| 139 |
if (mref1 > (mref * 2.0)) { |
| 140 |
if (mref2 <= ((2.0 - epsilon) * mref)) { |
| 141 |
return 3; |
| 142 |
} |
| 143 |
} else if (mref2 <= (mref * 2.0)) { |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
return 2; |
| 155 |
} else if (mref1 <= ((2.0 - epsilon) * mref)) { |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
return 3; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
return 4; |
| 169 |
} else { |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
if ((prod1 < 0.0) && (prod2 < 0.0)) { |
| 175 |
return 2; |
| 176 |
} else if (prod1 < 0.0) { |
| 177 |
if (mref2 > ((epsilon + 1.0) * mref)) { |
| 178 |
return 1; |
| 179 |
} else { |
| 180 |
return 2; |
| 181 |
} |
| 182 |
} else if (mref1 > ((epsilon + 1.0) * mref)) { |
| 183 |
return 1; |
| 184 |
} else { |
| 185 |
return 2; |
| 186 |
} |
| 187 |
} |
| 188 |
} else if ((m1 * m2) >= 0.0) { |
| 189 |
return 2; |
| 190 |
} else { |
| 191 |
return 1; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static void |
| 224 |
QuadCases(p, q, m1, m2, param, which) |
| 225 |
Point2D *p, *q; |
| 226 |
double m1, m2; |
| 227 |
double param[]; |
| 228 |
int which; |
| 229 |
{ |
| 230 |
if ((which == 3) || (which == 4)) { |
| 231 |
double mbar1, mbar2, mbar3, c1, d1, h1, j1a, k1; |
| 232 |
|
| 233 |
c1 = p->x + (q->y - p->y) / m1; |
| 234 |
d1 = q->x + (p->y - q->y) / m2; |
| 235 |
h1 = c1 * 2.0 - p->x; |
| 236 |
j1a = d1 * 2.0 - q->x; |
| 237 |
mbar1 = (q->y - p->y) / (h1 - p->x); |
| 238 |
mbar2 = (p->y - q->y) / (j1a - q->x); |
| 239 |
|
| 240 |
if (which == 4) { |
| 241 |
Y1 = (p->x + c1) / 2.0; |
| 242 |
V1 = (p->x + Y1) / 2.0; |
| 243 |
V2 = m1 * (V1 - p->x) + p->y; |
| 244 |
Z1 = (d1 + q->x) / 2.0; |
| 245 |
W1 = (q->x + Z1) / 2.0; |
| 246 |
W2 = m2 * (W1 - q->x) + q->y; |
| 247 |
mbar3 = (W2 - V2) / (W1 - V1); |
| 248 |
Y2 = mbar3 * (Y1 - V1) + V2; |
| 249 |
Z2 = mbar3 * (Z1 - V1) + V2; |
| 250 |
E1 = (Y1 + Z1) / 2.0; |
| 251 |
E2 = mbar3 * (E1 - V1) + V2; |
| 252 |
} else { |
| 253 |
k1 = (p->y - q->y + q->x * mbar2 - p->x * mbar1) / (mbar2 - mbar1); |
| 254 |
if (FABS(m1) > FABS(m2)) { |
| 255 |
Z1 = (k1 + p->x) / 2.0; |
| 256 |
} else { |
| 257 |
Z1 = (k1 + q->x) / 2.0; |
| 258 |
} |
| 259 |
V1 = (p->x + Z1) / 2.0; |
| 260 |
V2 = p->y + m1 * (V1 - p->x); |
| 261 |
W1 = (q->x + Z1) / 2.0; |
| 262 |
W2 = q->y + m2 * (W1 - q->x); |
| 263 |
Z2 = V2 + (W2 - V2) / (W1 - V1) * (Z1 - V1); |
| 264 |
} |
| 265 |
} else if (which == 2) { |
| 266 |
Z1 = (p->x + q->x) / 2.0; |
| 267 |
V1 = (p->x + Z1) / 2.0; |
| 268 |
V2 = p->y + m1 * (V1 - p->x); |
| 269 |
W1 = (Z1 + q->x) / 2.0; |
| 270 |
W2 = q->y + m2 * (W1 - q->x); |
| 271 |
Z2 = (V2 + W2) / 2.0; |
| 272 |
} else { |
| 273 |
double ztwo; |
| 274 |
|
| 275 |
Z1 = (p->y - q->y + m2 * q->x - m1 * p->x) / (m2 - m1); |
| 276 |
ztwo = p->y + m1 * (Z1 - p->x); |
| 277 |
V1 = (p->x + Z1) / 2.0; |
| 278 |
V2 = (p->y + ztwo) / 2.0; |
| 279 |
W1 = (Z1 + q->x) / 2.0; |
| 280 |
W2 = (ztwo + q->y) / 2.0; |
| 281 |
Z2 = V2 + (W2 - V2) / (W1 - V1) * (Z1 - V1); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
static int |
| 286 |
QuadSelect(p, q, m1, m2, epsilon, param) |
| 287 |
Point2D *p, *q; |
| 288 |
double m1, m2; |
| 289 |
double epsilon; |
| 290 |
double param[]; |
| 291 |
{ |
| 292 |
int ncase; |
| 293 |
|
| 294 |
ncase = QuadChoose(p, q, m1, m2, epsilon); |
| 295 |
QuadCases(p, q, m1, m2, param, ncase); |
| 296 |
return ncase; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
INLINE static double |
| 307 |
QuadGetImage(p1, p2, p3, x1, x2, x3) |
| 308 |
double p1, p2, p3; |
| 309 |
double x1, x2, x3; |
| 310 |
{ |
| 311 |
double A, B, C; |
| 312 |
double y; |
| 313 |
|
| 314 |
A = x1 - x2; |
| 315 |
B = x2 - x3; |
| 316 |
C = x1 - x3; |
| 317 |
|
| 318 |
y = (p1 * (A * A) + p2 * 2.0 * B * A + p3 * (B * B)) / (C * C); |
| 319 |
return y; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
static void |
| 350 |
QuadSpline(intp, left, right, param, ncase) |
| 351 |
Point2D *intp; |
| 352 |
Point2D *left; |
| 353 |
|
| 354 |
Point2D *right; |
| 355 |
|
| 356 |
double param[]; |
| 357 |
int ncase; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
{ |
| 362 |
double y; |
| 363 |
|
| 364 |
if (ncase == 4) { |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
if (Y1 > intp->x) { |
| 373 |
y = QuadGetImage(left->y, V2, Y2, Y1, intp->x, left->x); |
| 374 |
} else if (Y1 < intp->x) { |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
if (Z1 > intp->x) { |
| 380 |
y = QuadGetImage(Y2, E2, Z2, Z1, intp->x, Y1); |
| 381 |
} else if (Z1 < intp->x) { |
| 382 |
y = QuadGetImage(Z2, W2, right->y, right->x, intp->x, Z1); |
| 383 |
} else { |
| 384 |
y = Z2; |
| 385 |
} |
| 386 |
} else { |
| 387 |
y = Y2; |
| 388 |
} |
| 389 |
} else { |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
if (Z1 < intp->x) { |
| 398 |
y = QuadGetImage(Z2, W2, right->y, right->x, intp->x, Z1); |
| 399 |
} else if (Z1 > intp->x) { |
| 400 |
y = QuadGetImage(left->y, V2, Z2, Z1, intp->x, left->x); |
| 401 |
} else { |
| 402 |
y = Z2; |
| 403 |
} |
| 404 |
} |
| 405 |
intp->y = y; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
static void |
| 427 |
QuadSlopes(points, m, nPoints) |
| 428 |
Point2D points[]; |
| 429 |
double *m; |
| 430 |
|
| 431 |
int nPoints; |
| 432 |
|
| 433 |
{ |
| 434 |
double xbar, xmid, xhat, ydif1, ydif2; |
| 435 |
double yxmid; |
| 436 |
double m1, m2; |
| 437 |
double m1s, m2s; |
| 438 |
register int i, n, l; |
| 439 |
|
| 440 |
m1s = m2s = m1 = m2 = 0; |
| 441 |
for (l = 0, i = 1, n = 2; i < (nPoints - 1); l++, i++, n++) { |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
ydif1 = points[i].y - points[l].y; |
| 447 |
ydif2 = points[n].y - points[i].y; |
| 448 |
m1 = ydif1 / (points[i].x - points[l].x); |
| 449 |
m2 = ydif2 / (points[n].x - points[i].x); |
| 450 |
if (i == 1) { |
| 451 |
m1s = m1, m2s = m2; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
if ((m1 == 0.0) || (m2 == 0.0) || ((m1 * m2) <= 0.0)) { |
| 459 |
m[i] = 0.0; |
| 460 |
} else if (FABS(m1) > FABS(m2)) { |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
xbar = ydif2 / m1 + points[i].x; |
| 465 |
xhat = (xbar + points[n].x) / 2.0; |
| 466 |
m[i] = ydif2 / (xhat - points[i].x); |
| 467 |
} else { |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
xbar = -ydif1 / m2 + points[i].x; |
| 472 |
xhat = (points[l].x + xbar) / 2.0; |
| 473 |
m[i] = ydif1 / (points[i].x - xhat); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
i = nPoints - 2; |
| 479 |
n = nPoints - 1; |
| 480 |
if ((m1 * m2) < 0.0) { |
| 481 |
m[n] = m2 * 2.0; |
| 482 |
} else { |
| 483 |
xmid = (points[i].x + points[n].x) / 2.0; |
| 484 |
yxmid = m[i] * (xmid - points[i].x) + points[i].y; |
| 485 |
m[n] = (points[n].y - yxmid) / (points[n].x - xmid); |
| 486 |
if ((m[n] * m2) < 0.0) { |
| 487 |
m[n] = 0.0; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
if ((m1s * m2s) < 0.0) { |
| 493 |
m[0] = m1s * 2.0; |
| 494 |
} else { |
| 495 |
xmid = (points[0].x + points[1].x) / 2.0; |
| 496 |
yxmid = m[1] * (xmid - points[1].x) + points[1].y; |
| 497 |
m[0] = (yxmid - points[0].y) / (xmid - points[0].x); |
| 498 |
if ((m[0] * m1s) < 0.0) { |
| 499 |
m[0] = 0.0; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
static int |
| 556 |
QuadEval(origPts, nOrigPts, intpPts, nIntpPts, m, epsilon) |
| 557 |
Point2D origPts[]; |
| 558 |
int nOrigPts; |
| 559 |
Point2D intpPts[]; |
| 560 |
int nIntpPts; |
| 561 |
double *m; |
| 562 |
|
| 563 |
double epsilon; |
| 564 |
{ |
| 565 |
int error; |
| 566 |
register int i, j; |
| 567 |
double param[10]; |
| 568 |
int ncase; |
| 569 |
int start, end; |
| 570 |
int l, p; |
| 571 |
register int n; |
| 572 |
int found; |
| 573 |
|
| 574 |
|
| 575 |
error = 0; |
| 576 |
l = nOrigPts - 1; |
| 577 |
p = l - 1; |
| 578 |
ncase = 1; |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
for (j = 1; j < nIntpPts; j++) { |
| 584 |
if (intpPts[j].x < intpPts[j - 1].x) { |
| 585 |
return 2; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
for (start = 0; start < nIntpPts; start++) { |
| 593 |
if (intpPts[start].x >= origPts[0].x) { |
| 594 |
break; |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
for (end = nIntpPts - 1; end >= 0; end--) { |
| 602 |
if (intpPts[end].x <= origPts[l].x) { |
| 603 |
break; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
if (start > 0) { |
| 608 |
error = 1; |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
ncase = QuadSelect(origPts, origPts + 1, m[0], m[1], epsilon, param); |
| 615 |
for (j = 0; j < (start - 1); j++) { |
| 616 |
QuadSpline(intpPts + j, origPts, origPts + 1, param, ncase); |
| 617 |
} |
| 618 |
if (nIntpPts == 1) { |
| 619 |
return error; |
| 620 |
} |
| 621 |
} |
| 622 |
if ((nIntpPts == 1) && (end != (nIntpPts - 1))) { |
| 623 |
goto noExtrapolation; |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
i = Search(origPts, nOrigPts, intpPts[start].x, &found); |
| 632 |
|
| 633 |
n = i + 1; |
| 634 |
if (n >= nOrigPts) { |
| 635 |
n = nOrigPts - 1; |
| 636 |
i = nOrigPts - 2; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
if (found) { |
| 645 |
do { |
| 646 |
intpPts[start].y = origPts[i].y; |
| 647 |
start++; |
| 648 |
if (start >= nIntpPts) { |
| 649 |
return error; |
| 650 |
} |
| 651 |
} while (intpPts[start - 1].x == intpPts[start].x); |
| 652 |
|
| 653 |
for (;;) { |
| 654 |
if (intpPts[start].x < origPts[n].x) { |
| 655 |
break; |
| 656 |
} |
| 657 |
if (intpPts[start].x == origPts[n].x) { |
| 658 |
do { |
| 659 |
intpPts[start].y = origPts[n].y; |
| 660 |
start++; |
| 661 |
if (start >= nIntpPts) { |
| 662 |
return error; |
| 663 |
} |
| 664 |
} while (intpPts[start].x == intpPts[start - 1].x); |
| 665 |
} |
| 666 |
i++; |
| 667 |
n++; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
if ((i > 0) || (error != 1)) { |
| 675 |
ncase = QuadSelect(origPts + i, origPts + n, m[i], m[n], |
| 676 |
epsilon, param); |
| 677 |
} |
| 678 |
for (j = start; j <= end; j++) { |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
if (intpPts[j].x == origPts[n].x) { |
| 685 |
intpPts[j].y = origPts[n].y; |
| 686 |
continue; |
| 687 |
} else if (intpPts[j].x > origPts[n].x) { |
| 688 |
double delta; |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
do { |
| 693 |
i++, n++; |
| 694 |
delta = intpPts[j].x - origPts[n].x; |
| 695 |
} while (delta > 0.0); |
| 696 |
|
| 697 |
if (delta < 0.0) { |
| 698 |
ncase = QuadSelect(origPts + i, origPts + n, m[i], |
| 699 |
m[n], epsilon, param); |
| 700 |
} else if (delta == 0.0) { |
| 701 |
intpPts[j].y = origPts[n].y; |
| 702 |
continue; |
| 703 |
} |
| 704 |
} |
| 705 |
QuadSpline(intpPts + j, origPts + i, origPts + n, param, ncase); |
| 706 |
} |
| 707 |
|
| 708 |
if (end == (nIntpPts - 1)) { |
| 709 |
return error; |
| 710 |
} |
| 711 |
if ((n == l) && (intpPts[end].x != origPts[l].x)) { |
| 712 |
goto noExtrapolation; |
| 713 |
} |
| 714 |
|
| 715 |
error = 1; |
| 716 |
|
| 717 |
ncase = QuadSelect(origPts + p, origPts + l, m[p], m[l], epsilon, param); |
| 718 |
|
| 719 |
noExtrapolation: |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
for (j = (end + 1); j < nIntpPts; j++) { |
| 725 |
QuadSpline(intpPts + j, origPts + p, origPts + l, param, ncase); |
| 726 |
} |
| 727 |
return error; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
int |
| 753 |
Blt_QuadraticSpline(origPts, nOrigPts, intpPts, nIntpPts) |
| 754 |
Point2D origPts[]; |
| 755 |
int nOrigPts; |
| 756 |
Point2D intpPts[]; |
| 757 |
int nIntpPts; |
| 758 |
{ |
| 759 |
double epsilon; |
| 760 |
double *work; |
| 761 |
int result; |
| 762 |
|
| 763 |
work = Blt_Malloc(nOrigPts * sizeof(double)); |
| 764 |
assert(work); |
| 765 |
|
| 766 |
epsilon = 0.0; |
| 767 |
|
| 768 |
QuadSlopes(origPts, work, nOrigPts); |
| 769 |
result = QuadEval(origPts, nOrigPts, intpPts, nIntpPts, work, epsilon); |
| 770 |
Blt_Free(work); |
| 771 |
if (result > 1) { |
| 772 |
return FALSE; |
| 773 |
} |
| 774 |
return TRUE; |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
int |
| 791 |
Blt_NaturalSpline(origPts, nOrigPts, intpPts, nIntpPts) |
| 792 |
Point2D origPts[]; |
| 793 |
int nOrigPts; |
| 794 |
Point2D intpPts[]; |
| 795 |
int nIntpPts; |
| 796 |
{ |
| 797 |
Cubic2D *eq; |
| 798 |
Point2D *iPtr, *endPtr; |
| 799 |
TriDiagonalMatrix *A; |
| 800 |
double *dx; |
| 801 |
double x, dy, alpha; |
| 802 |
int isKnot; |
| 803 |
register int i, j, n; |
| 804 |
|
| 805 |
dx = Blt_Malloc(sizeof(double) * nOrigPts); |
| 806 |
|
| 807 |
for (i = 0, j = 1; j < nOrigPts; i++, j++) { |
| 808 |
dx[i] = origPts[j].x - origPts[i].x; |
| 809 |
if (dx[i] < 0.0) { |
| 810 |
return 0; |
| 811 |
} |
| 812 |
} |
| 813 |
n = nOrigPts - 1; |
| 814 |
A = Blt_Malloc(sizeof(TriDiagonalMatrix) * nOrigPts); |
| 815 |
if (A == NULL) { |
| 816 |
Blt_Free(dx); |
| 817 |
return 0; |
| 818 |
} |
| 819 |
|
| 820 |
A[0][0] = A[n][0] = 1.0; |
| 821 |
A[0][1] = A[n][1] = 0.0; |
| 822 |
A[0][2] = A[n][2] = 0.0; |
| 823 |
|
| 824 |
|
| 825 |
for (i = 0, j = 1; j < n; j++, i++) { |
| 826 |
alpha = 3.0 * ((origPts[j + 1].y / dx[j]) - (origPts[j].y / dx[i]) - |
| 827 |
(origPts[j].y / dx[j]) + (origPts[i].y / dx[i])); |
| 828 |
A[j][0] = 2 * (dx[j] + dx[i]) - dx[i] * A[i][1]; |
| 829 |
A[j][1] = dx[j] / A[j][0]; |
| 830 |
A[j][2] = (alpha - dx[i] * A[i][2]) / A[j][0]; |
| 831 |
} |
| 832 |
eq = Blt_Malloc(sizeof(Cubic2D) * nOrigPts); |
| 833 |
|
| 834 |
if (eq == NULL) { |
| 835 |
Blt_Free(A); |
| 836 |
Blt_Free(dx); |
| 837 |
return FALSE; |
| 838 |
} |
| 839 |
eq[0].c = eq[n].c = 0.0; |
| 840 |
for (j = n, i = n - 1; i >= 0; i--, j--) { |
| 841 |
eq[i].c = A[i][2] - A[i][1] * eq[j].c; |
| 842 |
dy = origPts[i+1].y - origPts[i].y; |
| 843 |
eq[i].b = (dy) / dx[i] - dx[i] * (eq[j].c + 2.0 * eq[i].c) / 3.0; |
| 844 |
eq[i].d = (eq[j].c - eq[i].c) / (3.0 * dx[i]); |
| 845 |
} |
| 846 |
Blt_Free(A); |
| 847 |
Blt_Free(dx); |
| 848 |
|
| 849 |
endPtr = intpPts + nIntpPts; |
| 850 |
|
| 851 |
for (iPtr = intpPts; iPtr < endPtr; iPtr++) { |
| 852 |
iPtr->y = 0.0; |
| 853 |
x = iPtr->x; |
| 854 |
|
| 855 |
|
| 856 |
if ((x < origPts[0].x) || (x > origPts[n].x)) { |
| 857 |
continue; |
| 858 |
} |
| 859 |
|
| 860 |
i = Search(origPts, nOrigPts, x, &isKnot); |
| 861 |
if (isKnot) { |
| 862 |
iPtr->y = origPts[i].y; |
| 863 |
} else { |
| 864 |
i--; |
| 865 |
x -= origPts[i].x; |
| 866 |
iPtr->y = origPts[i].y + |
| 867 |
x * (eq[i].b + x * (eq[i].c + x * eq[i].d)); |
| 868 |
} |
| 869 |
} |
| 870 |
Blt_Free(eq); |
| 871 |
return TRUE; |
| 872 |
} |
| 873 |
|
| 874 |
static Blt_OpSpec splineOps[] = |
| 875 |
{ |
| 876 |
{"natural", 1, (Blt_Op)Blt_NaturalSpline, 6, 6, "x y splx sply",}, |
| 877 |
{"quadratic", 1, (Blt_Op)Blt_QuadraticSpline, 6, 6, "x y splx sply",}, |
| 878 |
}; |
| 879 |
static int nSplineOps = sizeof(splineOps) / sizeof(Blt_OpSpec); |
| 880 |
|
| 881 |
|
| 882 |
static int |
| 883 |
SplineCmd(clientData, interp, argc, argv) |
| 884 |
ClientData clientData; |
| 885 |
Tcl_Interp *interp; |
| 886 |
int argc; |
| 887 |
char **argv; |
| 888 |
{ |
| 889 |
Blt_Op proc; |
| 890 |
Blt_Vector *x, *y, *splX, *splY; |
| 891 |
double *xArr, *yArr; |
| 892 |
register int i; |
| 893 |
Point2D *origPts, *intpPts; |
| 894 |
int nOrigPts, nIntpPts; |
| 895 |
|
| 896 |
proc = Blt_GetOp(interp, nSplineOps, splineOps, BLT_OP_ARG1, argc, argv,0); |
| 897 |
if (proc == NULL) { |
| 898 |
return TCL_ERROR; |
| 899 |
} |
| 900 |
if ((Blt_GetVector(interp, argv[2], &x) != TCL_OK) || |
| 901 |
(Blt_GetVector(interp, argv[3], &y) != TCL_OK) || |
| 902 |
(Blt_GetVector(interp, argv[4], &splX) != TCL_OK)) { |
| 903 |
return TCL_ERROR; |
| 904 |
} |
| 905 |
nOrigPts = Blt_VecLength(x); |
| 906 |
if (nOrigPts < 3) { |
| 907 |
Tcl_AppendResult(interp, "length of vector \"", argv[2], "\" is < 3", |
| 908 |
(char *)NULL); |
| 909 |
return TCL_ERROR; |
| 910 |
} |
| 911 |
for (i = 1; i < nOrigPts; i++) { |
| 912 |
if (Blt_VecData(x)[i] < Blt_VecData(x)[i - 1]) { |
| 913 |
Tcl_AppendResult(interp, "x vector \"", argv[2], |
| 914 |
"\" must be monotonically increasing", (char *)NULL); |
| 915 |
return TCL_ERROR; |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
if (Blt_VecData(x)[i - 1] <= Blt_VecData(x)[0]) { |
| 920 |
Tcl_AppendResult(interp, "x vector \"", argv[2], |
| 921 |
"\" must be monotonically increasing", (char *)NULL); |
| 922 |
return TCL_ERROR; |
| 923 |
} |
| 924 |
if (nOrigPts != Blt_VecLength(y)) { |
| 925 |
Tcl_AppendResult(interp, "vectors \"", argv[2], "\" and \"", argv[3], |
| 926 |
" have different lengths", (char *)NULL); |
| 927 |
return TCL_ERROR; |
| 928 |
} |
| 929 |
nIntpPts = Blt_VecLength(splX); |
| 930 |
if (Blt_GetVector(interp, argv[5], &splY) != TCL_OK) { |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
if (Blt_CreateVector(interp, argv[5], nIntpPts, &splY) != TCL_OK) { |
| 937 |
return TCL_ERROR; |
| 938 |
} |
| 939 |
} else if (nIntpPts != Blt_VecLength(splY)) { |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
if (Blt_ResizeVector(splY, nIntpPts) != TCL_OK) { |
| 945 |
return TCL_ERROR; |
| 946 |
} |
| 947 |
} |
| 948 |
origPts = Blt_Malloc(sizeof(Point2D) * nOrigPts); |
| 949 |
if (origPts == NULL) { |
| 950 |
Tcl_AppendResult(interp, "can't allocate \"", Blt_Itoa(nOrigPts), |
| 951 |
"\" points", (char *)NULL); |
| 952 |
return TCL_ERROR; |
| 953 |
} |
| 954 |
intpPts = Blt_Malloc(sizeof(Point2D) * nIntpPts); |
| 955 |
if (intpPts == NULL) { |
| 956 |
Tcl_AppendResult(interp, "can't allocate \"", Blt_Itoa(nIntpPts), |
| 957 |
"\" points", (char *)NULL); |
| 958 |
Blt_Free(origPts); |
| 959 |
return TCL_ERROR; |
| 960 |
} |
| 961 |
xArr = Blt_VecData(x); |
| 962 |
yArr = Blt_VecData(y); |
| 963 |
for (i = 0; i < nOrigPts; i++) { |
| 964 |
origPts[i].x = xArr[i]; |
| 965 |
origPts[i].y = yArr[i]; |
| 966 |
} |
| 967 |
xArr = Blt_VecData(splX); |
| 968 |
yArr = Blt_VecData(splY); |
| 969 |
for (i = 0; i < nIntpPts; i++) { |
| 970 |
intpPts[i].x = xArr[i]; |
| 971 |
intpPts[i].y = yArr[i]; |
| 972 |
} |
| 973 |
if (!(*proc) (origPts, nOrigPts, intpPts, nIntpPts)) { |
| 974 |
Tcl_AppendResult(interp, "error generating spline for \"", |
| 975 |
Blt_NameOfVector(splY), "\"", (char *)NULL); |
| 976 |
Blt_Free(origPts); |
| 977 |
Blt_Free(intpPts); |
| 978 |
return TCL_ERROR; |
| 979 |
} |
| 980 |
yArr = Blt_VecData(splY); |
| 981 |
for (i = 0; i < nIntpPts; i++) { |
| 982 |
yArr[i] = intpPts[i].y; |
| 983 |
} |
| 984 |
Blt_Free(origPts); |
| 985 |
Blt_Free(intpPts); |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
if (Blt_ResetVector(splY, Blt_VecData(splY), Blt_VecLength(splY), |
| 991 |
Blt_VecSize(splY), TCL_STATIC) != TCL_OK) { |
| 992 |
return TCL_ERROR; |
| 993 |
} |
| 994 |
return TCL_OK; |
| 995 |
} |
| 996 |
|
| 997 |
int |
| 998 |
Blt_SplineInit(interp) |
| 999 |
Tcl_Interp *interp; |
| 1000 |
{ |
| 1001 |
static Blt_CmdSpec cmdSpec = |
| 1002 |
{"spline", SplineCmd,}; |
| 1003 |
|
| 1004 |
if (Blt_InitCmd(interp, "blt", &cmdSpec) == NULL) { |
| 1005 |
return TCL_ERROR; |
| 1006 |
} |
| 1007 |
return TCL_OK; |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
#define SQR(x) ((x)*(x)) |
| 1012 |
|
| 1013 |
typedef struct { |
| 1014 |
double t; |
| 1015 |
double x; |
| 1016 |
double y; |
| 1017 |
} CubicSpline; |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
static int |
| 1038 |
SolveCubic1(A, n) |
| 1039 |
TriDiagonalMatrix A[]; |
| 1040 |
int n; |
| 1041 |
{ |
| 1042 |
int i; |
| 1043 |
double m_ij, m_n, m_nn, d; |
| 1044 |
|
| 1045 |
if (n < 1) { |
| 1046 |
return FALSE; |
| 1047 |
} |
| 1048 |
d = A[0][1]; |
| 1049 |
if (d <= 0.0) { |
| 1050 |
return FALSE; |
| 1051 |
} |
| 1052 |
m_n = A[0][0]; |
| 1053 |
m_nn = A[n - 1][1]; |
| 1054 |
for (i = 0; i < n - 2; i++) { |
| 1055 |
m_ij = A[i][2]; |
| 1056 |
A[i][2] = m_ij / d; |
| 1057 |
A[i][0] = m_n / d; |
| 1058 |
m_nn -= A[i][0] * m_n; |
| 1059 |
m_n = -A[i][2] * m_n; |
| 1060 |
d = A[i + 1][1] - A[i][2] * m_ij; |
| 1061 |
if (d <= 0.0) { |
| 1062 |
return FALSE; |
| 1063 |
} |
| 1064 |
A[i + 1][1] = d; |
| 1065 |
} |
| 1066 |
if (n >= 2) { |
| 1067 |
m_n += A[n - 2][2]; |
| 1068 |
A[n - 2][0] = m_n / d; |
| 1069 |
A[n - 1][1] = d = m_nn - A[n - 2][0] * m_n; |
| 1070 |
if (d <= 0.0) { |
| 1071 |
return FALSE; |
| 1072 |
} |
| 1073 |
} |
| 1074 |
return TRUE; |
| 1075 |
} |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
static void |
| 1083 |
SolveCubic2(A, spline, nIntervals) |
| 1084 |
TriDiagonalMatrix A[]; |
| 1085 |
CubicSpline spline[]; |
| 1086 |
int nIntervals; |
| 1087 |
{ |
| 1088 |
int i; |
| 1089 |
double x, y; |
| 1090 |
int n, m; |
| 1091 |
|
| 1092 |
n = nIntervals - 2; |
| 1093 |
m = nIntervals - 1; |
| 1094 |
|
| 1095 |
|
| 1096 |
x = spline[m].x; |
| 1097 |
y = spline[m].y; |
| 1098 |
for (i = 0; i < n; i++) { |
| 1099 |
spline[i + 1].x -= A[i][2] * spline[i].x; |
| 1100 |
spline[i + 1].y -= A[i][2] * spline[i].y; |
| 1101 |
x -= A[i][0] * spline[i].x; |
| 1102 |
y -= A[i][0] * spline[i].y; |
| 1103 |
} |
| 1104 |
if (n >= 0) { |
| 1105 |
|
| 1106 |
spline[m].x = x - A[n][0] * spline[n].x; |
| 1107 |
spline[m].y = y - A[n][0] * spline[n].y; |
| 1108 |
} |
| 1109 |
|
| 1110 |
for (i = 0; i < nIntervals; i++) { |
| 1111 |
spline[i].x /= A[i][1]; |
| 1112 |
spline[i].y /= A[i][1]; |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
x = spline[m].x; |
| 1117 |
y = spline[m].y; |
| 1118 |
if (n >= 0) { |
| 1119 |
|
| 1120 |
spline[n].x -= A[n][0] * x; |
| 1121 |
spline[n].y -= A[n][0] * y; |
| 1122 |
} |
| 1123 |
for (i = (n - 1); i >= 0; i--) { |
| 1124 |
|
| 1125 |
spline[i].x -= A[i][2] * spline[i + 1].x + A[i][0] * x; |
| 1126 |
spline[i].y -= A[i][2] * spline[i + 1].y + A[i][0] * y; |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
static CubicSpline * |
| 1137 |
CubicSlopes(points, nPoints, isClosed, unitX, unitY) |
| 1138 |
Point2D points[]; |
| 1139 |
int nPoints; |
| 1140 |
int isClosed; |
| 1141 |
double unitX, unitY; |
| 1142 |
{ |
| 1143 |
CubicSpline *spline; |
| 1144 |
register CubicSpline *s1, *s2; |
| 1145 |
int n, i; |
| 1146 |
double norm, dx, dy; |
| 1147 |
TriDiagonalMatrix *A; |
| 1148 |
|
| 1149 |
spline = Blt_Malloc(sizeof(CubicSpline) * nPoints); |
| 1150 |
if (spline == NULL) { |
| 1151 |
return NULL; |
| 1152 |
} |
| 1153 |
A = Blt_Malloc(sizeof(TriDiagonalMatrix) * nPoints); |
| 1154 |
if (A == NULL) { |
| 1155 |
Blt_Free(spline); |
| 1156 |
return NULL; |
| 1157 |
} |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
s1 = spline; |
| 1163 |
for (i = 0; i < nPoints - 1; i++) { |
| 1164 |
s1->x = points[i+1].x - points[i].x; |
| 1165 |
s1->y = points[i+1].y - points[i].y; |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
dx = s1->x / unitX; |
| 1172 |
dy = s1->y / unitY; |
| 1173 |
s1->t = sqrt(dx * dx + dy * dy); |
| 1174 |
|
| 1175 |
s1->x /= s1->t; |
| 1176 |
s1->y /= s1->t; |
| 1177 |
s1++; |
| 1178 |
} |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
n = nPoints - 2; |
| 1184 |
if (isClosed) { |
| 1185 |
|
| 1186 |
spline[nPoints - 1].t = spline[0].t; |
| 1187 |
spline[nPoints - 1].x = spline[0].x; |
| 1188 |
spline[nPoints - 1].y = spline[0].y; |
| 1189 |
n++; |
| 1190 |
} |
| 1191 |
s1 = spline, s2 = s1 + 1; |
| 1192 |
for (i = 0; i < n; i++) { |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
A[i][0] = s1->t; |
| 1197 |
A[i][1] = 2.0 * (s1->t + s2->t); |
| 1198 |
A[i][2] = s2->t; |
| 1199 |
|
| 1200 |
|
| 1201 |
s1->x = (s2->x - s1->x) * 6.0; |
| 1202 |
s1->y = (s2->y - s1->y) * 6.0; |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
dx = s1->x / unitX; |
| 1214 |
dy = s1->y / unitY; |
| 1215 |
norm = sqrt(dx * dx + dy * dy) / 8.5; |
| 1216 |
if (norm > 1.0) { |
| 1217 |
|
| 1218 |
s1->x /= norm; |
| 1219 |
s1->y /= norm; |
| 1220 |
} |
| 1221 |
s1++, s2++; |
| 1222 |
} |
| 1223 |
|
| 1224 |
if (!isClosed) { |
| 1225 |
|
| 1226 |
A[0][1] += A[0][0]; |
| 1227 |
A[0][0] = 0.0; |
| 1228 |
A[n-1][1] += A[n-1][2]; |
| 1229 |
A[n-1][2] = 0.0; |
| 1230 |
} |
| 1231 |
|
| 1232 |
|
| 1233 |
if (SolveCubic1(A, n)) { |
| 1234 |
SolveCubic2(A, spline, n); |
| 1235 |
} else { |
| 1236 |
Blt_Free(A); |
| 1237 |
Blt_Free(spline); |
| 1238 |
return NULL; |
| 1239 |
} |
| 1240 |
|
| 1241 |
s2 = spline + n, s1 = s2 - 1; |
| 1242 |
for (; s2 > spline; s2--, s1--) { |
| 1243 |
s2->x = s1->x; |
| 1244 |
s2->y = s1->y; |
| 1245 |
} |
| 1246 |
if (isClosed) { |
| 1247 |
spline[0].x = spline[n].x; |
| 1248 |
spline[0].y = spline[n].y; |
| 1249 |
} else { |
| 1250 |
|
| 1251 |
spline[0].x = spline[1].x; |
| 1252 |
spline[0].y = spline[1].y; |
| 1253 |
spline[n + 1].x = spline[n].x; |
| 1254 |
spline[n + 1].y = spline[n].y; |
| 1255 |
} |
| 1256 |
Blt_Free( A); |
| 1257 |
return spline; |
| 1258 |
} |
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
static int |
| 1267 |
CubicEval(origPts, nOrigPts, intpPts, nIntpPts, spline) |
| 1268 |
Point2D origPts[]; |
| 1269 |
int nOrigPts; |
| 1270 |
Point2D intpPts[]; |
| 1271 |
int nIntpPts; |
| 1272 |
CubicSpline spline[]; |
| 1273 |
{ |
| 1274 |
double t, tSkip, tMax; |
| 1275 |
Point2D p, q; |
| 1276 |
double d, hx, dx0, dx01, hy, dy0, dy01; |
| 1277 |
register int i, j, count; |
| 1278 |
|
| 1279 |
|
| 1280 |
tMax = 0.0; |
| 1281 |
for (i = 0; i < nOrigPts - 1; i++) { |
| 1282 |
tMax += spline[i].t; |
| 1283 |
} |
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
tSkip = (1. - 1e-7) * tMax / (nIntpPts - 1); |
| 1289 |
|
| 1290 |
t = 0.0; |
| 1291 |
q = origPts[0]; |
| 1292 |
count = 0; |
| 1293 |
|
| 1294 |
intpPts[count++] = q; |
| 1295 |
t += tSkip; |
| 1296 |
|
| 1297 |
for (i = 0, j = 1; j < nOrigPts; i++, j++) { |
| 1298 |
d = spline[i].t; |
| 1299 |
p = q; |
| 1300 |
q = origPts[i+1]; |
| 1301 |
hx = (q.x - p.x) / d; |
| 1302 |
hy = (q.y - p.y) / d; |
| 1303 |
dx0 = (spline[j].x + 2 * spline[i].x) / 6.0; |
| 1304 |
dy0 = (spline[j].y + 2 * spline[i].y) / 6.0; |
| 1305 |
dx01 = (spline[j].x - spline[i].x) / (6.0 * d); |
| 1306 |
dy01 = (spline[j].y - spline[i].y) / (6.0 * d); |
| 1307 |
while (t <= spline[i].t) { |
| 1308 |
p.x += t * (hx + (t - d) * (dx0 + t * dx01)); |
| 1309 |
p.y += t * (hy + (t - d) * (dy0 + t * dy01)); |
| 1310 |
intpPts[count++] = p; |
| 1311 |
t += tSkip; |
| 1312 |
} |
| 1313 |
|
| 1314 |
t -= spline[i].t; |
| 1315 |
} |
| 1316 |
return count; |
| 1317 |
} |
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
int |
| 1326 |
Blt_NaturalParametricSpline(origPts, nOrigPts, extsPtr, isClosed, |
| 1327 |
intpPts, nIntpPts) |
| 1328 |
Point2D origPts[]; |
| 1329 |
int nOrigPts; |
| 1330 |
Extents2D *extsPtr; |
| 1331 |
int isClosed; |
| 1332 |
Point2D *intpPts; |
| 1333 |
int nIntpPts; |
| 1334 |
{ |
| 1335 |
double unitX, unitY; |
| 1336 |
CubicSpline *spline; |
| 1337 |
int result; |
| 1338 |
|
| 1339 |
if (nOrigPts < 3) { |
| 1340 |
return 0; |
| 1341 |
} |
| 1342 |
if (isClosed) { |
| 1343 |
origPts[nOrigPts].x = origPts[0].x; |
| 1344 |
origPts[nOrigPts].y = origPts[0].y; |
| 1345 |
nOrigPts++; |
| 1346 |
} |
| 1347 |
|
| 1348 |
unitX = extsPtr->right - extsPtr->left; |
| 1349 |
unitY = extsPtr->bottom - extsPtr->top; |
| 1350 |
|
| 1351 |
if (unitX < FLT_EPSILON) { |
| 1352 |
unitX = FLT_EPSILON; |
| 1353 |
} |
| 1354 |
if (unitY < FLT_EPSILON) { |
| 1355 |
unitY = FLT_EPSILON; |
| 1356 |
} |
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
spline = CubicSlopes(origPts, nOrigPts, isClosed, unitX, unitY); |
| 1363 |
if (spline == NULL) { |
| 1364 |
return 0; |
| 1365 |
} |
| 1366 |
result= CubicEval(origPts, nOrigPts, intpPts, nIntpPts, spline); |
| 1367 |
Blt_Free(spline); |
| 1368 |
return result; |
| 1369 |
} |
| 1370 |
|
| 1371 |
static void |
| 1372 |
CatromCoeffs(p, a, b, c, d) |
| 1373 |
Point2D *p; |
| 1374 |
Point2D *a, *b, *c, *d; |
| 1375 |
{ |
| 1376 |
a->x = -p[0].x + 3.0 * p[1].x - 3.0 * p[2].x + p[3].x; |
| 1377 |
b->x = 2.0 * p[0].x - 5.0 * p[1].x + 4.0 * p[2].x - p[3].x; |
| 1378 |
c->x = -p[0].x + p[2].x; |
| 1379 |
d->x = 2.0 * p[1].x; |
| 1380 |
a->y = -p[0].y + 3.0 * p[1].y - 3.0 * p[2].y + p[3].y; |
| 1381 |
b->y = 2.0 * p[0].y - 5.0 * p[1].y + 4.0 * p[2].y - p[3].y; |
| 1382 |
c->y = -p[0].y + p[2].y; |
| 1383 |
d->y = 2.0 * p[1].y; |
| 1384 |
} |
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
int |
| 1400 |
Blt_CatromParametricSpline(points, nPoints, intpPts, nIntpPts) |
| 1401 |
Point2D *points; |
| 1402 |
int nPoints; |
| 1403 |
Point2D *intpPts; |
| 1404 |
int nIntpPts; |
| 1405 |
{ |
| 1406 |
register int i; |
| 1407 |
Point2D *origPts; |
| 1408 |
double t; |
| 1409 |
int interval; |
| 1410 |
Point2D a, b, c, d; |
| 1411 |
|
| 1412 |
assert(nPoints > 0); |
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
origPts = Blt_Malloc((nPoints + 4) * sizeof(Point2D)); |
| 1419 |
memcpy(origPts + 1, points, sizeof(Point2D) * nPoints); |
| 1420 |
|
| 1421 |
origPts[0] = origPts[1]; |
| 1422 |
origPts[nPoints + 2] = origPts[nPoints + 1] = origPts[nPoints]; |
| 1423 |
|
| 1424 |
for (i = 0; i < nIntpPts; i++) { |
| 1425 |
interval = (int)intpPts[i].x; |
| 1426 |
t = intpPts[i].y; |
| 1427 |
assert(interval < nPoints); |
| 1428 |
CatromCoeffs(origPts + interval, &a, &b, &c, &d); |
| 1429 |
intpPts[i].x = (d.x + t * (c.x + t * (b.x + t * a.x))) / 2.0; |
| 1430 |
intpPts[i].y = (d.y + t * (c.y + t * (b.y + t * a.y))) / 2.0; |
| 1431 |
} |
| 1432 |
Blt_Free(origPts); |
| 1433 |
return 1; |
| 1434 |
} |