| [10302] | 1 | /*========================================================================= | 
|---|
 | 2 |  | 
|---|
 | 3 |   Program:   Visualization Toolkit | 
|---|
 | 4 |   Module:    $RCSfile: Spline.cxx,v $ | 
|---|
 | 5 |  | 
|---|
 | 6 |   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen | 
|---|
 | 7 |   All rights reserved. | 
|---|
 | 8 |   See Copyright.txt or http://www.kitware.com/Copyright.htm for details. | 
|---|
 | 9 |  | 
|---|
 | 10 |      This software is distributed WITHOUT ANY WARRANTY; without even | 
|---|
 | 11 |      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | 
|---|
 | 12 |      PURPOSE.  See the above copyright notice for more information. | 
|---|
 | 13 |  | 
|---|
 | 14 | =========================================================================*/ | 
|---|
 | 15 | #include "spline.h" | 
|---|
 | 16 |  | 
|---|
 | 17 |  | 
|---|
 | 18 | //---------------------------------------------------------------------------- | 
|---|
 | 19 | // Construct a spline wth the folloing defaults: | 
|---|
 | 20 | // ClampValueOff | 
|---|
 | 21 | Spline::Spline () | 
|---|
 | 22 | { | 
|---|
 | 23 |   this->ComputeTime = 0; | 
|---|
 | 24 |   this->ClampValue = 0; | 
|---|
 | 25 |   this->PiecewiseFunction = vtkPiecewiseFunction::New(); | 
|---|
 | 26 |   this->Intervals = NULL; | 
|---|
 | 27 |   this->Coefficients = NULL; | 
|---|
 | 28 |   this->LeftConstraint = 1; | 
|---|
 | 29 |   this->LeftValue = 0.0; | 
|---|
 | 30 |   this->RightConstraint = 1; | 
|---|
 | 31 |   this->RightValue = 0.0; | 
|---|
 | 32 |   this->Closed = 0; | 
|---|
 | 33 |  | 
|---|
 | 34 |   this->ParametricRange[0] = -1; | 
|---|
 | 35 |   this->ParametricRange[1] = -1; | 
|---|
 | 36 | } | 
|---|
 | 37 |  | 
|---|
 | 38 | //---------------------------------------------------------------------------- | 
|---|
 | 39 | Spline::~Spline () | 
|---|
 | 40 | { | 
|---|
 | 41 |   this->PiecewiseFunction->Delete(); | 
|---|
 | 42 |   if (this->Coefficients) | 
|---|
 | 43 |   { | 
|---|
 | 44 |     delete [] this->Coefficients; | 
|---|
 | 45 |   } | 
|---|
 | 46 |   if (this->Intervals) | 
|---|
 | 47 |   { | 
|---|
 | 48 |     delete [] this->Intervals; | 
|---|
 | 49 |   } | 
|---|
 | 50 | } | 
|---|
 | 51 |  | 
|---|
 | 52 | //---------------------------------------------------------------------------- | 
|---|
 | 53 | void Spline::SetParametricRange(double tMin, double tMax) | 
|---|
 | 54 | { | 
|---|
 | 55 |   if ( tMin != this->ParametricRange[0] || tMax != this->ParametricRange[1] ) | 
|---|
 | 56 |   { | 
|---|
 | 57 |     if ( tMin >= tMax ) | 
|---|
 | 58 |     { | 
|---|
 | 59 |       tMax = tMin + 1; | 
|---|
 | 60 |     } | 
|---|
 | 61 |  | 
|---|
 | 62 |     this->ParametricRange[0] = tMin; | 
|---|
 | 63 |     this->ParametricRange[1] = tMax; | 
|---|
 | 64 |  | 
|---|
 | 65 |     this->Modified(); | 
|---|
 | 66 |   } | 
|---|
 | 67 | } | 
|---|
 | 68 |  | 
|---|
 | 69 | //---------------------------------------------------------------------------- | 
|---|
 | 70 | void Spline::GetParametricRange(double tRange[2]) const | 
|---|
 | 71 | { | 
|---|
 | 72 |   if ( this->ParametricRange[0] != this->ParametricRange[1] ) | 
|---|
 | 73 |   { | 
|---|
 | 74 |     tRange[0] = this->ParametricRange[0]; | 
|---|
 | 75 |     tRange[1] = this->ParametricRange[1]; | 
|---|
 | 76 |   } | 
|---|
 | 77 |   else | 
|---|
 | 78 |   { | 
|---|
 | 79 |     tRange[0] = this->PiecewiseFunction->GetRange()[0]; | 
|---|
 | 80 |     tRange[1] = this->PiecewiseFunction->GetRange()[1]; | 
|---|
 | 81 |   } | 
|---|
 | 82 | } | 
|---|
 | 83 |  | 
|---|
 | 84 | //---------------------------------------------------------------------------- | 
|---|
 | 85 | double Spline::ComputeLeftDerivative() | 
|---|
 | 86 | { | 
|---|
 | 87 |   double *dptr = this->PiecewiseFunction->GetDataPointer(); | 
|---|
 | 88 |   int size = this->PiecewiseFunction->GetSize(); | 
|---|
 | 89 |   if ( dptr == NULL || size < 2 ) | 
|---|
 | 90 |   { | 
|---|
 | 91 |     return 0.0; | 
|---|
 | 92 |   } | 
|---|
 | 93 |   else | 
|---|
 | 94 |   { | 
|---|
 | 95 |     return (dptr[2]-dptr[0]); | 
|---|
 | 96 |   } | 
|---|
 | 97 | } | 
|---|
 | 98 |  | 
|---|
 | 99 | //---------------------------------------------------------------------------- | 
|---|
 | 100 | double Spline::ComputeRightDerivative() | 
|---|
 | 101 | { | 
|---|
 | 102 |   double *dptr = this->PiecewiseFunction->GetDataPointer(); | 
|---|
 | 103 |   int size = this->PiecewiseFunction->GetSize(); | 
|---|
 | 104 |   if ( dptr == NULL || size < 2 ) | 
|---|
 | 105 |   { | 
|---|
 | 106 |     return 0.0; | 
|---|
 | 107 |   } | 
|---|
 | 108 |   else | 
|---|
 | 109 |   { | 
|---|
 | 110 |     return (dptr[(size-1)*2]-dptr[(size-2)*2]); | 
|---|
 | 111 |   } | 
|---|
 | 112 | } | 
|---|
 | 113 |  | 
|---|
 | 114 | //---------------------------------------------------------------------------- | 
|---|
 | 115 | int Spline::GetNumberOfPoints() | 
|---|
 | 116 | { | 
|---|
 | 117 |   return this->PiecewiseFunction->GetSize(); | 
|---|
 | 118 | } | 
|---|
 | 119 |  | 
|---|
 | 120 |  | 
|---|
 | 121 | //---------------------------------------------------------------------------- | 
|---|
 | 122 | // Add a point to the Piecewise Functions containing the data | 
|---|
 | 123 | void Spline::AddPoint (double t, double x) | 
|---|
 | 124 | { | 
|---|
 | 125 |   if ( this->ParametricRange[0] != this->ParametricRange[1] ) | 
|---|
 | 126 |   { | 
|---|
 | 127 |     t = (t < this->ParametricRange[0] ? this->ParametricRange[0] : | 
|---|
 | 128 |          (t > this->ParametricRange[1] ? this->ParametricRange[1] : t)); | 
|---|
 | 129 |   } | 
|---|
 | 130 |   this->PiecewiseFunction->AddPoint (t, x); | 
|---|
 | 131 | } | 
|---|
 | 132 |  | 
|---|
 | 133 | //---------------------------------------------------------------------------- | 
|---|
 | 134 | // Remove a point from the Piecewise Functions. | 
|---|
 | 135 | void Spline::RemovePoint (double t) | 
|---|
 | 136 | { | 
|---|
 | 137 |   if ( this->ParametricRange[0] != this->ParametricRange[1] ) | 
|---|
 | 138 |   { | 
|---|
 | 139 |     t = (t < this->ParametricRange[0] ? this->ParametricRange[0] : | 
|---|
 | 140 |          (t > this->ParametricRange[1] ? this->ParametricRange[1] : t)); | 
|---|
 | 141 |   } | 
|---|
 | 142 |   this->PiecewiseFunction->RemovePoint (t); | 
|---|
 | 143 | } | 
|---|
 | 144 |  | 
|---|
 | 145 | //---------------------------------------------------------------------------- | 
|---|
 | 146 | // Remove all points from the Piecewise Functions. | 
|---|
 | 147 | void Spline::RemoveAllPoints () | 
|---|
 | 148 | { | 
|---|
 | 149 |   this->PiecewiseFunction->RemoveAllPoints (); | 
|---|
 | 150 | } | 
|---|
 | 151 |  | 
|---|
 | 152 |  | 
|---|
 | 153 |  | 
|---|
 | 154 |  | 
|---|
 | 155 |  | 
|---|
 | 156 | //---------------------------------------------------------------------------- | 
|---|
 | 157 | int Spline::FindIndex(int size, double t) | 
|---|
 | 158 | { | 
|---|
 | 159 |   int index=0; | 
|---|
 | 160 |   if ( size > 2 ) //bisection method for speed | 
|---|
 | 161 |   { | 
|---|
 | 162 |     int rightIdx = size - 1; | 
|---|
 | 163 |     int centerIdx = rightIdx - size/2; | 
|---|
 | 164 |     for (int converged=0; !converged; ) | 
|---|
 | 165 |     { | 
|---|
 | 166 |       if ( this->Intervals[index] <= t && t <= this->Intervals[centerIdx] ) | 
|---|
 | 167 |       { | 
|---|
 | 168 |         rightIdx = centerIdx; | 
|---|
 | 169 |       } | 
|---|
 | 170 |       else //if ( this->Intervals[centerIdx] < t && t <= this->Intervals[rightIdx] ) | 
|---|
 | 171 |       { | 
|---|
 | 172 |         index = centerIdx; | 
|---|
 | 173 |       } | 
|---|
 | 174 |       if ( (index + 1) == rightIdx ) | 
|---|
 | 175 |       { | 
|---|
 | 176 |         converged = 1; | 
|---|
 | 177 |       } | 
|---|
 | 178 |       else | 
|---|
 | 179 |       { | 
|---|
 | 180 |         centerIdx = index + (rightIdx-index)/2; | 
|---|
 | 181 |       } | 
|---|
 | 182 |     }//while not converged | 
|---|
 | 183 |   } | 
|---|
 | 184 |   return index; | 
|---|
 | 185 | } | 
|---|
 | 186 |  | 
|---|