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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSpline.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSpline.h"
#include "vtkPiecewiseFunction.h"
//----------------------------------------------------------------------------
// Construct a spline wth the folloing defaults:
// ClampValueOff
vtkSpline::vtkSpline ()
{
this->ComputeTime = 0;
this->ClampValue = 0;
this->PiecewiseFunction = vtkPiecewiseFunction::New();
this->Intervals = NULL;
this->Coefficients = NULL;
this->LeftConstraint = 1;
this->LeftValue = 0.0;
this->RightConstraint = 1;
this->RightValue = 0.0;
this->Closed = 0;
this->ParametricRange[0] = -1;
this->ParametricRange[1] = -1;
}
//----------------------------------------------------------------------------
vtkSpline::~vtkSpline ()
{
this->PiecewiseFunction->Delete();
delete [] this->Coefficients;
delete [] this->Intervals;
}
//----------------------------------------------------------------------------
void vtkSpline::SetParametricRange(double tMin, double tMax)
{
if ( tMin != this->ParametricRange[0] || tMax != this->ParametricRange[1] )
{
if ( tMin >= tMax )
{
tMax = tMin + 1;
}
this->ParametricRange[0] = tMin;
this->ParametricRange[1] = tMax;
this->Modified();
}
}
//----------------------------------------------------------------------------
void vtkSpline::GetParametricRange(double tRange[2]) const
{
if ( this->ParametricRange[0] != this->ParametricRange[1] )
{
tRange[0] = this->ParametricRange[0];
tRange[1] = this->ParametricRange[1];
}
else
{
tRange[0] = this->PiecewiseFunction->GetRange()[0];
tRange[1] = this->PiecewiseFunction->GetRange()[1];
}
}
//----------------------------------------------------------------------------
double vtkSpline::ComputeLeftDerivative()
{
double *dptr = this->PiecewiseFunction->GetDataPointer();
int size = this->PiecewiseFunction->GetSize();
if ( dptr == NULL || size < 2 )
{
return 0.0;
}
else
{
return (dptr[2]-dptr[0]);
}
}
//----------------------------------------------------------------------------
double vtkSpline::ComputeRightDerivative()
{
double *dptr = this->PiecewiseFunction->GetDataPointer();
int size = this->PiecewiseFunction->GetSize();
if ( dptr == NULL || size < 2 )
{
return 0.0;
}
else
{
return (dptr[(size-1)*2]-dptr[(size-2)*2]);
}
}
//----------------------------------------------------------------------------
int vtkSpline::GetNumberOfPoints()
{
return this->PiecewiseFunction->GetSize();
}
//----------------------------------------------------------------------------
// Add a point to the Piecewise Functions containing the data
void vtkSpline::AddPoint (double t, double x)
{
if ( this->ParametricRange[0] != this->ParametricRange[1] )
{
t = (t < this->ParametricRange[0] ? this->ParametricRange[0] :
(t > this->ParametricRange[1] ? this->ParametricRange[1] : t));
}
this->PiecewiseFunction->AddPoint (t, x);
}
//----------------------------------------------------------------------------
// Remove a point from the Piecewise Functions.
void vtkSpline::RemovePoint (double t)
{
if ( this->ParametricRange[0] != this->ParametricRange[1] )
{
t = (t < this->ParametricRange[0] ? this->ParametricRange[0] :
(t > this->ParametricRange[1] ? this->ParametricRange[1] : t));
}
this->PiecewiseFunction->RemovePoint (t);
}
//----------------------------------------------------------------------------
// Remove all points from the Piecewise Functions.
void vtkSpline::RemoveAllPoints ()
{
this->PiecewiseFunction->RemoveAllPoints ();
}
//----------------------------------------------------------------------------
void vtkSpline::DeepCopy(vtkSpline *s)
{
vtkSpline *spline = vtkSpline::SafeDownCast(s);
if ( spline != NULL )
{
this->ClampValue = s->ClampValue;
this->LeftConstraint = s->LeftConstraint;
this->LeftValue = s->LeftValue;
this->RightConstraint = s->RightConstraint;
this->RightValue = s->RightValue;
this->Closed = s->Closed;
this->PiecewiseFunction->DeepCopy(s->PiecewiseFunction);
}
}
//----------------------------------------------------------------------------
// Overload standard modified time function. If data is modified,
// then this object is modified as well.
unsigned long vtkSpline::GetMTime()
{
unsigned long mTime=this->vtkObject::GetMTime();
unsigned long DataMTime;
if ( this->PiecewiseFunction != NULL )
{
DataMTime = this->PiecewiseFunction->GetMTime();
mTime = ( DataMTime > mTime ? DataMTime : mTime );
}
return mTime;
}
//----------------------------------------------------------------------------
int vtkSpline::FindIndex(int size, double t)
{
int index=0;
if ( size > 2 ) //bisection method for speed
{
int rightIdx = size - 1;
int centerIdx = rightIdx - size/2;
for (int converged=0; !converged; )
{
if ( this->Intervals[index] <= t && t <= this->Intervals[centerIdx] )
{
rightIdx = centerIdx;
}
else //if ( this->Intervals[centerIdx] < t && t <= this->Intervals[rightIdx] )
{
index = centerIdx;
}
if ( (index + 1) == rightIdx )
{
converged = 1;
}
else
{
centerIdx = index + (rightIdx-index)/2;
}
}//while not converged
}
return index;
}
//----------------------------------------------------------------------------
void vtkSpline::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Clamp Value: " << (this->ClampValue ? "On\n" : "Off\n");
os << indent << "Left Constraint: " << this->LeftConstraint << "\n";
os << indent << "Right Constraint: " << this->RightConstraint << "\n";
os << indent << "Left Value: " << this->LeftValue << "\n";
os << indent << "Right Value: " << this->RightValue << "\n";
os << indent << "Closed: " << (this->Closed ? "On\n" : "Off\n");
os << indent << "Piecewise Function:\n";
this->PiecewiseFunction->PrintSelf(os,indent.GetNextIndent());
os << indent << "Closed: " << (this->Closed ? "On\n" : "Off\n");
}
|