1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 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 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
|
/*******************************************************************************
* splines.cpp
*
* This module implements splines.
*
* ---------------------------------------------------------------------------
* Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
* Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
*
* POV-Ray is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* POV-Ray is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------------
* POV-Ray is based on the popular DKB raytracer version 2.12.
* DKBTrace was originally written by David K. Buck.
* DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
* ---------------------------------------------------------------------------
* $File: //depot/public/povray/3.x/source/backend/math/splines.cpp $
* $Revision: #1 $
* $Change: 6069 $
* $DateTime: 2013/11/06 11:59:40 $
* $Author: chrisc $
*******************************************************************************/
// frame.h must always be the first POV file included (pulls in platform config)
#include "backend/frame.h"
#include "backend/math/splines.h"
#include "base/pov_err.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
// this must be the last file included
#include "base/povdebug.h"
namespace pov
{
/*****************************************************************************
* Global Variables
******************************************************************************/
/*****************************************************************************
* Local preprocessor defines
******************************************************************************/
/*****************************************************************************
* Local typedefs
******************************************************************************/
/*****************************************************************************
* Local variables
******************************************************************************/
/*****************************************************************************
* Local functions
******************************************************************************/
DBL linear_interpolate(const SPLINE_ENTRY * se, int i, int k, DBL p);
DBL quadratic_interpolate(const SPLINE_ENTRY * se, int i, int k, DBL p);
DBL natural_interpolate(const SPLINE_ENTRY * se, int i, int k, DBL p);
DBL catmull_rom_interpolate(const SPLINE_ENTRY * se, int i, int k, DBL p);
int findt(const SPLINE * sp, DBL Time);
void mkfree(SPLINE * sp, int i);
void Precompute_Cubic_Coeffs(SPLINE *sp);
/*****************************************************************************
*
* FUNCTION
*
* Precompute_Cubic_Coeffs
*
* INPUT
*
* sp : a pointer to the spline to compute interpolation coefficients for
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* Mark Wagner
*
* DESCRIPTION
*
* Computes the coefficients used in cubic_interpolate.
*
* CHANGES
*
******************************************************************************/
void Precompute_Cubic_Coeffs(SPLINE *sp)
{
int i, k;
DBL *h;
DBL *b;
DBL *u;
DBL *v;
h = (DBL *)POV_MALLOC(sp->Number_Of_Entries*sizeof(DBL), "Spline coefficient storage");
b = (DBL *)POV_MALLOC(sp->Number_Of_Entries*sizeof(DBL), "Spline coefficient storage");
u = (DBL *)POV_MALLOC(sp->Number_Of_Entries*sizeof(DBL), "Spline coefficient storage");
v = (DBL *)POV_MALLOC(sp->Number_Of_Entries*sizeof(DBL), "Spline coefficient storage");
for(k = 0; k < 5; k++)
{
for(i = 0; i <= sp->Number_Of_Entries - 2; i++)
{
h[i] = sp->SplineEntries[i+1].par - sp->SplineEntries[i].par;
b[i] = (sp->SplineEntries[i+1].vec[k] - sp->SplineEntries[i].vec[k])/h[i];
}
u[1] = 2*(h[0]+h[1]);
v[1] = 6*(b[1]-b[0]);
for(i = 2; i <= sp->Number_Of_Entries - 2; i++)
{
u[i] = 2*(h[i]+h[i-1]) - (h[i-1]*h[i-1])/u[i-1];
v[i] = 6*(b[i]-b[i-1]) - (h[i-1]*v[i-1])/u[i-1];
}
sp->SplineEntries[sp->Number_Of_Entries-1].coeff[k] = 0;
for(i = sp->Number_Of_Entries-2; i > 0; i--)
{
sp->SplineEntries[i].coeff[k] = (v[i] - h[i]*sp->SplineEntries[i+1].coeff[k])/u[i];
}
sp->SplineEntries[0].coeff[k] = 0;
}
sp->Coeffs_Computed = true;
POV_FREE(h);
POV_FREE(b);
POV_FREE(u);
POV_FREE(v);
}
/*****************************************************************************
*
* FUNCTION
*
* linear_interpolate
*
* INPUT
*
* se : a pointer to the entries in the spline
* i : the first point to interpolate between
* k : which dimension of the 5D vector to interpolate in
* p : the parameter to interpolate the value for
*
* OUTPUT
*
* RETURNS
*
* The value of the kth dimension of the vector linearly interpolated at p
*
* AUTHOR
*
* Wolfgang Ortmann
*
* DESCRIPTION
*
* CHANGES
*
******************************************************************************/
DBL linear_interpolate(const SPLINE_ENTRY * se, int i, int k, DBL p)
{
DBL p1, p2, v1, v2;
p1 = se[i].par;
p2 = se[i+1].par;
v1 = se[i].vec[k];
v2 = se[i+1].vec[k];
return (p-p1)*(v2-v1)/(p2-p1)+v1;
}
/*****************************************************************************
*
* FUNCTION
*
* quadratic_interpolate
*
* INPUT
*
* se : a pointer to the entries in the spline
* i : the second point of three to interpolate between
* k : which dimension of the 5D vector to interpolate in
* p : the parameter to interpolate the value for
*
* OUTPUT
*
* RETURNS
*
* The value of the kth dimension of the quadratic interpolation of the
* vector at p
*
* AUTHOR
*
* Wolfgang Ortmann
*
* DESCRIPTION
*
* CHANGES
*
******************************************************************************/
DBL quadratic_interpolate(const SPLINE_ENTRY * se, int i, int k, DBL p)
{
/* fit quadratic function to three point*/
DBL n;
DBL p1, p2, p3,
v1, v2, v3,
a, b, c;
/* assignments to make life easier */
p1=se[i-1].par; p2=se[i].par; p3=se[i+1].par;
v1=se[i-1].vec[k]; v2=se[i].vec[k]; v3=se[i+1].vec[k];
n=(p2-p1)*(p3-p1)*(p3-p2);
/* MWW NOTE: I'm assuming these are correct. I didn't write them */
a=(-p2*v1+p3*v1
+p1*v2-p3*v2
-p1*v3+p2*v3) /n;
b=( p2*p2*v1 - p3*p3*v1
-p1*p1*v2 + p3*p3*v2
+p1*p1*v3 - p2*p2*v3) /n;
c=(-p2*p2*p3*v1+p2*p3*p3*v1
+p1*p1*p3*v2-p1*p3*p3*v2
-p1*p1*p2*v3+p1*p2*p2*v3) /n;
return (a*p+b)*p+c; /* Fast way of doing ap^2+bp+c */
}
/*****************************************************************************
*
* FUNCTION
*
* natural_interpolate
*
* INPUT
*
* se : a pointer to the entries in the spline
* i : the first point in the interpolation interval
* k : which dimension of the 5D vector to interpolate in
* p : the parameter to interpolate the value for
*
* OUTPUT
*
* RETURNS
*
* The value of the kth dimension of the natural cubic interpolation
* of the vector at p
*
* AUTHOR
*
* Mark Wagner
*
* DESCRIPTION
*
* CHANGES
*
******************************************************************************/
DBL natural_interpolate(const SPLINE_ENTRY * se, int i, int k, DBL p)
{
DBL h, tmp;
h = se[i+1].par - se[i].par;
tmp = se[i].coeff[k]/2.0 + ((p - se[i].par)*(se[i+1].coeff[k] - se[i].coeff[k]))/(6.0*h);
tmp = -(h/6.0)*(se[i+1].coeff[k] + 2.0*se[i].coeff[k]) + (se[i+1].vec[k] - se[i].vec[k])/h + (p - se[i].par)*tmp;
return se[i].vec[k] + (p - se[i].par)*tmp;
}
/*****************************************************************************
*
* FUNCTION
*
* catmull_rom_interpolate
*
* INPUT
*
* se : a pointer to the entries in the spline
* i : the first point in the interpolation interval
* k : which dimension of the 5D vector to interpolate in
* p : the parameter to interpolate the value for
*
* OUTPUT
*
* RETURNS
*
* The value of the kth dimension of the Catmull-Rom interpolation of the
* vector at p
*
* AUTHOR
*
* Mark Wagner
*
* DESCRIPTION
*
* CHANGES
*
******************************************************************************/
DBL catmull_rom_interpolate(const SPLINE_ENTRY * se, int i, int k, DBL p)
{
DBL dt = (se[i+1].par - se[i].par); /* Time between se[i] and se[i+1] */
DBL u = (p - se[i].par)/dt; /* Fractional time from se[i] to p */
DBL dp0 = ((se[i].vec[k] - se[i-1].vec[k])/(se[i].par - se[i-1].par) + (se[i+1].vec[k] - se[i].vec[k])/(se[i+1].par - se[i].par))/2.0 * dt;
DBL dp1 = ((se[i+2].vec[k] - se[i+1].vec[k])/(se[i+2].par - se[i+1].par) + (se[i+1].vec[k] - se[i].vec[k])/(se[i+1].par - se[i].par))/2.0 * dt;
return (se[i].vec[k] * (2*u*u*u - 3*u*u + 1) +
se[i+1].vec[k] * (3*u*u - 2*u*u*u) +
dp0 * (u*u*u - 2*u*u + u) +
dp1 * (u*u*u - u*u) );
}
/*****************************************************************************
*
* FUNCTION
*
* findt
*
* INPUT
*
* sp : a pointer to a spline
* Time : The parameter to search for
*
* OUTPUT
*
* RETURNS
*
* The first spline entry with a parameter greater than Time
*
* AUTHOR
*
* Wolfgang Ortmann
*
* DESCRIPTION
*
* CHANGES
*
* Mark Wagner 6 Nov 2000 : Changed from linear search to binary search
*
******************************************************************************/
int findt(const SPLINE * sp, DBL Time)
{
int i, i2;
SPLINE_ENTRY * se;
se = sp->SplineEntries;
if(sp->Number_Of_Entries == 0) return 0;
if(Time <= se[0].par) return 0;
if(Time >= se[sp->Number_Of_Entries-1].par) return sp->Number_Of_Entries;
i = sp->Number_Of_Entries / 2;
/* Bracket the proper entry */
if( Time > se[i].par ) /* i is lower, i2 is upper */
{
i2 = sp->Number_Of_Entries-1;
while(i2 - i > 1)
{
if(Time > se[i+(i2-i)/2].par)
i = i+(i2-i)/2;
else
i2 = i+(i2-i)/2;
}
return i2;
}
else /* i is upper, i2 is lower */
{
i2 = 0;
while(i - i2 > 1)
{
if(Time > se[i2+(i-i2)/2].par)
i2 = i2+(i-i2)/2;
else
i = i2+(i-i2)/2;
}
return i;
}
}
/*****************************************************************************
*
* FUNCTION
*
* mkfree
*
* INPUT
*
* sp : a pointer to the entries in the spline
* i : the index of the entry to make available
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* Wolfgang Ortmann
*
* DESCRIPTION
*
* Makes the spline entry at index i available for inserting a new entry
* by moving all entries starting with that index down by one slot in the
* array.
*
* CHANGES
*
******************************************************************************/
void mkfree(SPLINE * sp, int i)
{
int j;
SPLINE_ENTRY * se;
se = sp->SplineEntries;
for (j=sp->Number_Of_Entries; j>i; j--)
se[j] = se[j-1];
}
/*****************************************************************************
*
* FUNCTION
*
* Create_Spline
*
* INPUT
*
* Type : the type of the new spline
*
* OUTPUT
*
* RETURNS
*
* A pointer to the newly-created spline
*
* AUTHOR
*
* Wolfgang Ortmann
*
* DESCRIPTION
*
* CHANGES
*
* Mark Wagner 6 Nov 2000 : Added support for dynamic resizing of the
* SplineEntry array.
* Mark Wagner 25 Aug 2001 : Added support for pre-computing coefficients
* of cubic splines
*
******************************************************************************/
SPLINE * Create_Spline(int Type)
{
SPLINE * New;
New = (SPLINE *)POV_MALLOC(sizeof(SPLINE), "spline");
New->SplineEntries = (SPLINE_ENTRY *)POV_MALLOC(INIT_SPLINE_SIZE*sizeof(SPLINE_ENTRY), "spline entry");
New->Max_Entries = INIT_SPLINE_SIZE;
New->Number_Of_Entries = 0;
New->Type = Type;
New->Coeffs_Computed = false;
New->Terms = 2;
// New->Cache_Valid = false; [JG] flyspray #294, cache is not thread-safe
New->ref_count = 1;
int i;
for (i=0; i< New->Max_Entries; i++)
{
New->SplineEntries[i].par=-1e6; //this should be a negative large number
}
return New;
}
/*****************************************************************************
*
* FUNCTION
*
* Copy_Spline
*
* INPUT
*
* Old : A pointer to the old spline
*
* OUTPUT
*
* RETURNS
*
* A pointer to the new spline
*
* AUTHOR
*
* Wolfgang Ortmann
*
* DESCRIPTION
*
* CHANGES
*
* Mark Wagner 6 Nov 2000 : Added support for dynamic resizing of the
* SplineEntry array
* Mark Wagner 25 Aug 2001 : Added support for pre-computing coefficients
* of cubic splines
*
******************************************************************************/
SPLINE * Copy_Spline(const SPLINE * Old)
{
SPLINE * New;
New = (SPLINE *)POV_MALLOC(sizeof(SPLINE), "spline");
New->SplineEntries = (SPLINE_ENTRY *)POV_MALLOC(Old->Number_Of_Entries*sizeof(SPLINE_ENTRY), "spline entry");
POV_MEMCPY(New->SplineEntries, Old->SplineEntries, Old->Number_Of_Entries*sizeof(SPLINE_ENTRY));
New->Max_Entries = Old->Number_Of_Entries;
New->Number_Of_Entries = Old->Number_Of_Entries;
New->Type = Old->Type;
New->Coeffs_Computed = Old->Coeffs_Computed;
New->Terms = Old->Terms;
//[JG] flyspray #294, cache is not thread-safe
// New->Cache_Valid = false; // we don't copy the cache so mark it as invalid
New->ref_count = 1;
return New;
}
void Acquire_Spline_Reference(SPLINE * Spline)
{
if (Spline)
{
if (Spline->ref_count >= INT_MAX)
throw POV_EXCEPTION_STRING("Too many unresolved references to single spline\n");
Spline->ref_count ++;
}
}
void Release_Spline_Reference(SPLINE * Spline)
{
if (Spline)
Destroy_Spline(Spline);
}
/*****************************************************************************
*
* FUNCTION
*
* Destroy_Spline
*
* INPUT
*
* Spline : A pointer to the spline to delete
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* Wolfgang Ortmann
*
* DESCRIPTION
*
* Deletes the SplineEntry array
*
* CHANGES
*
******************************************************************************/
void Destroy_Spline(SPLINE * Spline)
{
if (Spline->ref_count <= 0)
throw POV_EXCEPTION_STRING("Internal error: Invalid spline reference count\n");
Spline->ref_count --;
if (Spline->ref_count == 0)
{
POV_FREE(Spline->SplineEntries);
POV_FREE(Spline);
}
}
/*****************************************************************************
*
* FUNCTION
*
* Insert_Spline_Entry
*
* INPUT
*
* sp : a pointer to the spline to insert the new value in
* p : the value of the parameter at that point
* v : a 5D vector that is the value of the spline at that parameter
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* Wolfgang Ortmann
*
* DESCRIPTION
*
* Inserts a value into the given spline, sorting the entries in order by
* increasing p
* If a value of the parameter already exists, that value is overwritten
*
* CHANGES
*
* Mark Wagner 8 Nov 2000 : Added dynamic sizing of the SplineEntry array
* If a value of the parameter already exists, that value is overwritten
* Mark Wagner 25 Aug 2001 : Modified for compatibility with new method of
* computing cubic splines.
* Mark Wagner 30 Aug 2001 : Fixed a bug with over-writing of parameter
* values.
*
******************************************************************************/
void Insert_Spline_Entry(SPLINE * sp, DBL p, const EXPRESS v)
{
int i, k;
/* Reset the Coeffs_Computed flag. Inserting a new point invalidates
* pre-computed coefficients */
sp->Coeffs_Computed = false;
// sp->Cache_Valid = false;[JG] flyspray #294, cache is not thread-safe
/* If all space is used, reallocate */
if(sp->Number_Of_Entries >= sp->Max_Entries)
{
sp->Max_Entries += INIT_SPLINE_SIZE;
sp->SplineEntries = (SPLINE_ENTRY *)POV_REALLOC(sp->SplineEntries, sp->Max_Entries * sizeof(SPLINE_ENTRY), "Temporary Spline Entries");
for (i = sp->Number_Of_Entries; i < sp->Max_Entries; i++)
{
sp->SplineEntries[i].par=-1e6;
}
}
i = findt(sp, p);
/* If p is already in spline, replace */
/* The clause after the || is needed because findt returns sp->Number_Of_Entries
* if p is greater than OR EQUAL TO the highest par in the spline */
if(sp->Number_Of_Entries != 0 && ((sp->SplineEntries[i].par == p) || (i == sp->Number_Of_Entries && sp->SplineEntries[i-1].par == p)))
{
for(k=0; k<5; k++)
sp->SplineEntries[i].vec[k] = v[k];
}
else
{
mkfree(sp, i);
sp->SplineEntries[i].par = p;
for(k=0; k<5; k++)
sp->SplineEntries[i].vec[k] = v[k];
sp->Number_Of_Entries += 1;
}
}
/*****************************************************************************
*
* FUNCTION
*
* Get_Spline_Value
*
* INPUT
*
* sp : a pointer to the spline to interpolate
* p : the parameter to interpolate the value for
* v : a pointer to a 5D vector to store the interpolated values in
*
* OUTPUT
*
* v : a pointer to a 5D vector to store the interpolated values in
*
* RETURNS
*
* The value of the first element of the interpolated vector
*
* AUTHOR
*
* Wolfgang Ortmann
*
* DESCRIPTION
*
* Interpolates the spline at the given point. If the point is outside the
* range of parameters of the spline, returns the value at the
* appropriate endpoint (ie. no extrapolation). If there are not
* enough points in the spline to do the desired type of
* interpolation, does the next best type (cubic->quadratic->linear).
*
* CHANGES
*
* Mark Wagner 8 Nov 2000 : Complete overhaul. I am apalled at the
* problems I found in the original implementation
* Mark Wagner 25 Aug 2001 : Changed interpolation method to pre-compute
* coefficients for cubic splines.
* Mark Wagner 24 Feb 2002 : Added support for Catmull-Rom interpolation
* Re-arranged the code to make future additions cleaner by moving
* more of the code into the "switch" statement
*
******************************************************************************/
DBL Get_Spline_Val(SPLINE *sp, DBL p, EXPRESS v, int *Terms)
{
int i, k;
int last;
SPLINE_ENTRY * se;
*Terms = sp->Terms;
if(!sp->Coeffs_Computed)
{
switch(sp->Type)
{
case NATURAL_SPLINE:
Precompute_Cubic_Coeffs(sp);
break;
default:
break;
}
}
/* [JG] flyspray #294, cache is not thread-safe
// check if the value is in the cache
if((sp->Cache_Point == p) && (sp->Cache_Type == sp->Type))
{
if(sp->Cache_Valid == true) // doing this here is more efficient as it is rarely false [trf]
{
Assign_Express(v, sp->Cache_Data);
return sp->Cache_Data[0];
}
}
// init some cache data
sp->Cache_Valid = false;
sp->Cache_Type = sp->Type;
sp->Cache_Point = p;
*/
last = sp->Number_Of_Entries-1;
se = sp->SplineEntries;
if(last == 0)
{/* if only one entry then return this */
for(k=0; k<5; k++)
v[k] = se[0].vec[k];
return se[0].vec[0];
}
/* Find which spline segment we're in. i is the control point at the end of the segment */
i = findt(sp, p);
switch(sp->Type)
{
case LINEAR_SPLINE:
for(k=0; k<5; k++)
{
/* If outside spline range, return first or last point */
if(i == 0)
v[k] = se[0].vec[k];
else if(i > last)
v[k] = se[last].vec[k];
/* Else, normal case */
else
v[k] = linear_interpolate(se, i-1, k, p);
}
break;
case QUADRATIC_SPLINE:
for(k=0; k<5; k++)
{
/* If outside the spline range, return the first or last point */
if(i == 0)
v[k] = se[0].vec[k];
else if(i > last)
v[k] = se[last].vec[k];
/* If not enough points, reduce order */
else if(last == 1)
v[k] = linear_interpolate(se, i-1, k, p);
/* Normal case: between the second and last points */
else if(i > 1)
{
v[k] = quadratic_interpolate(se, i-1, k, p);
}
else /* Special case: between first and second points */
{
v[k] = quadratic_interpolate(se, i, k, p);
}
}
break;
case NATURAL_SPLINE:
for(k=0; k<5; k++)
{
/* If outside the spline range, return the first or last point */
if(i == 0)
v[k] = se[0].vec[k];
else if(i > last)
v[k] = se[last].vec[k];
/* Else, normal case. cubic_interpolate can handle the case of not enough points */
else
v[k] = natural_interpolate(se, i-1, k, p);
}
break;
case CATMULL_ROM_SPLINE:
for(k=0; k<5; k++)
{
/* If only two points, return their average */
if(last == 1)
v[k] = (se[0].vec[k] + se[1].vec[k])/2.0;
/* Catmull-Rom: If only three points, return the second one */
/* Catmull-Rom: Can't interpolate before second point or after next-to-last */
else if(i < 2)
v[k] = se[1].vec[k];
else if(i >= last)
v[k] = se[last-1].vec[k];
/* Else, normal case */
else
v[k] = catmull_rom_interpolate(se, i-1, k, p);
}
break;
default:
throw POV_EXCEPTION_STRING("Unknown spline type found.\n");
}
/* [JG] flyspray #294, cache is not thread-safe
// put data in cache
Assign_Express(sp->Cache_Data, v);
sp->Cache_Valid = true;
*/
return v[0];
}
}
|