Commit 058bd493 authored by Bill Lorensen's avatar Bill Lorensen
Browse files

ENH: new spline classes.

parent b95d87b7
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCardinalSpline.cxx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 1993-1996 Ken Martin, Will Schroeder, Bill Lorensen.
This software is copyrighted by Ken Martin, Will Schroeder and Bill Lorensen.
The following terms apply to all files associated with the software unless
explicitly disclaimed in individual files. This copyright specifically does
not apply to the related textbook "The Visualization Toolkit" ISBN
013199837-4 published by Prentice Hall which is covered by its own copyright.
The authors hereby grant permission to use, copy, and distribute this
software and its documentation for any purpose, provided that existing
copyright notices are retained in all copies and that this notice is included
verbatim in any distributions. Additionally, the authors grant permission to
modify this software and its documentation for any purpose, provided that
such modifications are not distributed without the explicit consent of the
authors and that existing copyright notices are retained in all copies. Some
of the algorithms implemented by this software are patented, observe all
applicable patent law.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
=========================================================================*/
#include "vtkCardinalSpline.h"
// Description
// Construct a CardinalSpline wth the folloing defaults:
// ClampingOff
vtkCardinalSpline::vtkCardinalSpline ()
{
}
// Description
// Compute Cardinal Splines for each dependent variable
void vtkCardinalSpline::Compute ()
{
float *ts, *xs;
float *work;
float *coefficients;
float *dependent;
int size;
int i;
// get the size of the independent variables
size = this->PiecewiseFunction->GetSize ();
// copy the independent variables
if (this->Intervals) delete this->Intervals;
this->Intervals = new float[size];
ts = this->PiecewiseFunction->GetDataPointer ();
for (i = 0; i < size; i++)
{
this->Intervals[i] = *(ts + 2 * i);
}
// allocate memory for work arrays
work = new float[size];
// allocate memory for coefficients
if (this->Coefficients) delete this->Coefficients;
this->Coefficients = new float [4 * size];
// allocate memory for dependent variables
dependent = new float [size];
// get start of coefficients for this dependent variable
coefficients = this->Coefficients;
// get the dependent variable values
xs = this->PiecewiseFunction->GetDataPointer () + 1;
for (int j = 0; j < size; j++)
{
*(dependent + j) = *(xs + 2*j);
}
this->Fit1D (size, this->Intervals, dependent,
work, (float (*)[4])coefficients,
this->LeftConstraint, this->LeftValue,
this->RightConstraint, this->RightValue);
// free the work array and dependent variable storage
delete work;
delete dependent;
}
// Description
// Compute the coefficients for a 1D spline
void vtkCardinalSpline::Fit1D (int size, float *x, float *y,
float *work, float coefficients[][4],
int leftConstraint, float leftValue,
int rightConstraint, float rightValue)
{
float b;
float xlk;
float xlkp;
int k;
// develop constraint at leftmost point.
switch (leftConstraint) {
case 1:
// desired slope at leftmost point is leftValue.
coefficients[0][1] = 1.0;
coefficients[0][2] = 0.0;
work[0] = leftValue;
break;
case 2:
// desired second derivative at leftmost point is leftValue.
coefficients[0][1] = 2.0;
coefficients[0][2] = 1.0;
work[0]= 3.0 * ((y[1] - y[0]) / (x[1] - x[0])) -
0.5 * (x[1] -x[0]) * leftValue;
break;
case 3:
// desired second derivative at leftmost point is
// leftValue times second derivative at first interior point.
coefficients[0][1] = 2.0;
coefficients[0][2] = 4.0 * ((0.5 + leftValue) /
(2.0 + leftValue));
work[0]= 6.0 * ((1.0 + leftValue) / (2.0 + leftValue)) *
((y[1] - y[0]) / (x[1]-x[0]));
break;
}
// develop body of band matrix.
for (k = 1; k < size - 1; k++)
{
xlk = x[k] - x[k-1];
xlkp = x[k+1] - x[k];
coefficients[k][0] = xlkp;
coefficients[k][1] = 2.0 * (xlkp + xlk);
coefficients[k][2] = xlk;
work[k] = 3.0 * (((xlkp * (y[k] - y[k-1])) / xlk) +
((xlk * (y[k+1] - y[k])) / xlkp));
}
// develop constraint at rightmost point.
switch (rightConstraint) {
case 1:
// desired slope at rightmost point is rightValue
coefficients[size - 1][0] = 0.0;
coefficients[size - 1][1] = 1.0;
work[size - 1] = rightValue;
break;
case 2:
// desired second derivative at rightmost point is rightValue.
coefficients[size-1][0] = 1.0;
coefficients[size-1][1] = 2.0;
work[size-1] = 3.0 * ((y[size-1] - y[size-2]) /
(x[size-1] - x[size-2])) +
0.5 * (x[size-1]-x[size-2]) * rightValue;
break;
case 3:
// desired second derivative at rightmost point is
// rightValue times second derivative at last interior point.
coefficients[size-1][0] = 4.0 * ((0.5 + rightValue) /
(2.0 + rightValue));
coefficients[size-1][1] = 2.0;
work[size-1] = 6.0 * ((1.0 + rightValue) / (2.0 + rightValue)) *
((y[size-1] - y[size-2]) /
(x[size-1] - x[size-2]));
break;
}
// solve resulting set of equations.
coefficients[0][2] = coefficients[0][2] / coefficients[0][1];
work[0] = work[0] / coefficients[0][1];
coefficients[size-1][2] = 0.0;
for (k = 1; k < size; k++)
{
coefficients[k][1] = coefficients[k][1] - (coefficients[k][0] *
coefficients[k-1][2]);
coefficients[k][2] = coefficients[k][2] / coefficients[k][1];
work[k] = (work[k] - (coefficients[k][0] * work[k-1]))
/ coefficients[k][1];
}
for (k = size - 2; k >= 0; k--)
{
work[k] = work[k] - (coefficients[k][2] * work[k+1]);
}
// the column vector work now contains the first
// derivative of the spline function at each joint.
// compute the coefficients of the cubic between
// each pair of joints.
for (k = 0; k < size - 1; k++)
{
b = x[k+1] - x[k];
coefficients[k][0] = y[k];
coefficients[k][1] = work[k];
coefficients[k][2] = (3.0 * (y[k+1] - y[k])) / (b * b) -
(work[k+1] + 2.0 * work[k]) / b;
coefficients[k][3] = (2.0 * (y[k] - y[k+1])) / (b * b * b) +
(work[k+1] + work[k]) / (b * b);
}
// the coefficients of a fictitious nth cubic
// are evaluated. this may simplify
// algorithms which include both end points.
coefficients[size-1][0] = y[size-1];
coefficients[size-1][1] = work[size-1];
coefficients[size-1][2] = coefficients[size-2][2] +
3.0 * coefficients[size-2][3] * b;
coefficients[size-1][3] = coefficients[size-2][3];
}
void vtkCardinalSpline::PrintSelf(ostream& os, vtkIndent indent)
{
vtkSpline::PrintSelf(os,indent);
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCardinalSpline.h
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 1993-1996 Ken Martin, Will Schroeder, Bill Lorensen.
This software is copyrighted by Ken Martin, Will Schroeder and Bill Lorensen.
The following terms apply to all files associated with the software unless
explicitly disclaimed in individual files. This copyright specifically does
not apply to the related textbook "The Visualization Toolkit" ISBN
013199837-4 published by Prentice Hall which is covered by its own copyright.
The authors hereby grant permission to use, copy, and distribute this
software and its documentation for any purpose, provided that existing
copyright notices are retained in all copies and that this notice is included
verbatim in any distributions. Additionally, the authors grant permission to
modify this software and its documentation for any purpose, provided that
such modifications are not distributed without the explicit consent of the
authors and that existing copyright notices are retained in all copies. Some
of the algorithms implemented by this software are patented, observe all
applicable patent law.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
=========================================================================*/
// .NAME vtkCardinalSpline - computes an interpolating spline using a
// a Cardinal basis.
// .SECTION Description
// .SECTION See Also
// vtkSpline
#ifndef __vtkCardinalSpline_h
#define __vtkCardinalSpline_h
#include <stdio.h>
#include "vtkSpline.h"
class VTK_EXPORT vtkCardinalSpline : public vtkSpline
{
public:
vtkCardinalSpline();
static vtkCardinalSpline *New() {return new vtkCardinalSpline;};
char *GetClassName() {return "vtkCardinalSpline";};
void PrintSelf(ostream& os, vtkIndent indent);
void Compute ();
protected:
void Fit1D (int n, float *x, float *y, float *w, float coefficients[][4],
int leftConstraint, float leftValue, int rightConstraint, float rightValue);
};
#endif
/*=========================================================================
Program: Visualization Toolkit
Module: vtkKochanekSpline.cxx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 1993-1996 Ken Martin, Will Schroeder, Bill Lorensen.
This software is copyrighted by Ken Martin, Will Schroeder and Bill Lorensen.
The following terms apply to all files associated with the software unless
explicitly disclaimed in individual files. This copyright specifically does
not apply to the related textbook "The Visualization Toolkit" ISBN
013199837-4 published by Prentice Hall which is covered by its own copyright.
The authors hereby grant permission to use, copy, and distribute this
software and its documentation for any purpose, provided that existing
copyright notices are retained in all copies and that this notice is included
verbatim in any distributions. Additionally, the authors grant permission to
modify this software and its documentation for any purpose, provided that
such modifications are not distributed without the explicit consent of the
authors and that existing copyright notices are retained in all copies. Some
of the algorithms implemented by this software are patented, observe all
applicable patent law.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
=========================================================================*/
#include "vtkKochanekSpline.h"
// Description
// Construct a KochanekSpline wth the following defaults:
// DefaultBias = 0
// DefaultTension = 0
// DefaultContinuity = 0
vtkKochanekSpline::vtkKochanekSpline ()
{
this->DefaultBias = 0.0;
this->DefaultTension = 0.0;
this->DefaultContinuity = 0.0;
}
// Description
// Compute Kochanek Splines for each dependent variable
void vtkKochanekSpline::Compute ()
{
float *ts, *xs;
float *coefficients;
float *dependent;
int size;
int i;
// get the size of the independent variables
size = this->PiecewiseFunction->GetSize ();
// copy the independent variables
if (this->Intervals) delete this->Intervals;
this->Intervals = new float[size];
ts = this->PiecewiseFunction->GetDataPointer ();
for (i = 0; i < size; i++)
{
this->Intervals[i] = *(ts + 2 * i);
}
// allocate memory for coefficients
if (this->Coefficients) delete this->Coefficients;
this->Coefficients = new float [4 * size];
// allocate memory for dependent variables
dependent = new float [size];
// get start of coefficients for this dependent variable
coefficients = this->Coefficients;
// get the dependent variable values
xs = this->PiecewiseFunction->GetDataPointer () + 1;
for (int j = 0; j < size; j++)
{
*(dependent + j) = *(xs + 2*j);
}
this->Fit1D (size, this->Intervals, dependent,
this->DefaultTension,
this->DefaultBias,
this->DefaultContinuity,
(float (*)[4])coefficients,
this->LeftConstraint, this->LeftValue,
this->RightConstraint, this->RightValue);
// free the dependent variable storage
delete dependent;
// update compute time
this->ComputeTime = this->GetMTime();
}
#define EPSILON .0001
// Description
// Compute the coefficients for a 1D spline
void vtkKochanekSpline::Fit1D (int size, float *x, float *y,
float tension, float bias, float continuity,
float coefficients[][4],
int leftConstraint, float leftValue,
int rightConstraint, float rightValue)
{
float cs; /* source chord */
float cd; /* destination chord */
float ds; /* source deriviative */
float dd; /* destination deriviative */
float n0, n1; /* number of frames btwn nodes */
int N; /* top point number */
int i;
if (size == 2)
{
// two points, set coeffieints for a straight line
coefficients[0][3] = 0.0;
coefficients[1][3] = 0.0;
coefficients[0][2] = 0.0;
coefficients[1][2] = 0.0;
coefficients[0][1] = (y[1] - y[0]) / (x[1] - x[0]);
coefficients[1][1] = coefficients[0][1];
coefficients[0][0] = y[0];
coefficients[1][0] = y[1];
}
N = size - 1;
for (i=1; i < N; i++)
{
cs = y[i] - y[i-1];
cd = y[i+1] - y[i];
ds = cs*((1 - tension)*(1 - continuity)*(1 + bias)) / 2.0
+ cd*((1 - tension)*(1 + continuity)*(1 - bias)) / 2.0;
dd = cs*((1 - tension)*(1 + continuity)*(1 + bias)) / 2.0
+ cd*((1 - tension)*(1 - continuity)*(1 - bias)) / 2.0;
// adjust deriviatives for non uniform spacing between nodes
n1 = x[i+1] - x[i];
n0 = x[i] - x[i-1];
ds *= (2 * n1 / (n0 + n1));
dd *= (2 * n0 / (n0 + n1));
coefficients[i][0] = y[i];
coefficients[i][1] = dd;
coefficients[i][2] = ds;
}
// Calculate the deriviatives at the end points
coefficients[0][0] = y[0];
coefficients[N][0] = y[N];
switch (leftConstraint) {
case 1:
// desired slope at leftmost point is leftValue
coefficients[0][1] = leftValue;
break;
case 2:
// desired secord derivative at leftmost point is leftValue
coefficients[0][1] = (6*(y[1] - y[0]) - 2*coefficients[1][2] - leftValue)
/ 4.0;
break;
case 3:
// desired secord derivative at leftmost point is leftValue
// times secod derivative at first interior point
if ((leftValue > (-2.0 + EPSILON)) ||
(leftValue < (-2.0 - EPSILON)))
{
coefficients[0][1] = (3*(1 + leftValue)*(y[1] - y[0]) -
(1 + 2*leftValue)*coefficients[1][2])
/ (2 + leftValue);
}
else
{
coefficients[0][1] = 0.0;
}
break;
}
switch (rightConstraint)
{
case 1:
// desired slope at rightmost point is rightValue
coefficients[N][2] = rightValue;
break;
case 2:
// desired second derivative at rightmost point is rightValue
coefficients[N][2] = (6*(y[N] - y[N-1]) - 2*coefficients[N-1][1] +
rightValue) / 4.0;
break;
case 3:
// desired secord derivative at rightmost point is rightValue
// times secord derivative at last interior point
if ((rightValue > (-2.0 + EPSILON)) ||
(rightValue < (-2.0 - EPSILON)))
{
coefficients[N][2] = (3*(1 + rightValue)*(y[N] - y[N-1]) -
(1 + 2*rightValue)*coefficients[N-1][1])
/ (2 + rightValue);
}
else
{
coefficients[N][2] = 0.0;
}
break;
}
// Compute the Coefficients
for (i=0; i < N; i++) {
//
// c0 = P ; c1 = DD ;
// i i i i
//
// c1 = P ; c2 = DS ;
// i+1 i+1 i+1 i+1
//
// c2 = -3P + 3P - 2DD - DS ;
// i i i+1 i i+1
//
// c3 = 2P - 2P + DD + DS ;
// i i i+1 i i+1
//
coefficients[i][2] = (-3 * y[i]) + ( 3 * y[i+1])
+ (-2 * coefficients[i][1]) + (-1 * coefficients[i+1][2]);
coefficients[i][3] = ( 2 * y[i]) + (-2 * y[i+1])
+ ( 1 * coefficients[i][1]) + ( 1 * coefficients[i+1][2]);
}
}
void vtkKochanekSpline::PrintSelf(ostream& os, vtkIndent indent)
{
vtkSpline::PrintSelf(os,indent);
os << indent << "DefaultBias: " << this->DefaultBias << "\n";
os << indent << "DefaultTension: " << this->DefaultTension << "\n";
os << indent << "DefaultContinuity: " << this->DefaultContinuity << "\n";
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkKochanekSpline.h
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 1993-1996 Ken Martin, Will Schroeder, Bill Lorensen.
This software is copyrighted by Ken Martin, Will Schroeder and Bill Lorensen.
The following terms apply to all files associated with the software unless
explicitly disclaimed in individual files. This copyright specifically does
not apply to the related textbook "The Visualization Toolkit" ISBN
013199837-4 published by Prentice Hall which is covered by its own copyright.
The authors hereby grant permission to use, copy, and distribute this
software and its documentation for any purpose, provided that existing
copyright notices are retained in all copies and that this notice is included
verbatim in any distributions. Additionally, the authors grant permission to
modify this software and its documentation for any purpose, provided that
such modifications are not distributed without the explicit consent of the
authors and that existing copyright notices are retained in all copies. Some
of the algorithms implemented by this software are patented, observe all
applicable patent law.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
=========================================================================*/
// .NAME vtkKochanekSpline - computes an interpolating spline using a
// a Kochanek basis.