Spline.h
Go to the documentation of this file.00001 #ifndef SimTK_SIMMATH_SPLINE_H_
00002 #define SimTK_SIMMATH_SPLINE_H_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "SimTKcommon.h"
00036 #include "simmath/internal/GCVSPLUtil.h"
00037
00038 #include <limits>
00039
00040 namespace SimTK {
00041
00050 template <class T>
00051 class Spline_ : public Function_<T> {
00052 public:
00060 Spline_(int degree, const Vector& x, const Vector_<T>& y) : impl(new SplineImpl(degree, x, y)) {
00061 }
00062 Spline_(const Spline_& copy) : impl(copy.impl) {
00063 impl->referenceCount++;
00064 }
00065 Spline_() : impl(NULL) {
00066 }
00067 Spline_& operator=(const Spline_& copy) {
00068 if (impl) {
00069 impl->referenceCount--;
00070 if (impl->referenceCount == 0)
00071 delete impl;
00072 }
00073 impl = copy.impl;
00074 assert(impl);
00075 impl->referenceCount++;
00076 return *this;
00077 }
00078 ~Spline_() {
00079 if (impl) {
00080 impl->referenceCount--;
00081 if (impl->referenceCount == 0)
00082 delete impl;
00083 }
00084 }
00090 T calcValue(const Vector& x) const {
00091 assert(impl);
00092 assert(x.size() == 1);
00093 return impl->getValue(x[0]);
00094 }
00100 T calcDerivative(const Array_<int>& derivComponents, const Vector& x) const {
00101 assert(impl);
00102 assert(x.size() == 1);
00103 assert(derivComponents.size() > 0);
00104 return impl->getDerivative(derivComponents.size(), x[0]);
00105 }
00106 int getArgumentSize() const {
00107 return 1;
00108 }
00109 int getMaxDerivativeOrder() const {
00110 return std::numeric_limits<int>::max();
00111 }
00115 const Vector& getControlPointLocations() const {
00116 assert(impl);
00117 return impl->x;
00118 }
00122 const Vector_<T>& getControlPointValues() const {
00123 assert(impl);
00124 return impl->y;
00125 }
00129 int getSplineDegree() const {
00130 assert(impl);
00131 return impl->degree;
00132 }
00133
00135 T calcDerivative(const std::vector<int>& derivComponents, const Vector& x) const
00136 { return calcDerivative(ArrayViewConst_<int>(derivComponents),x); }
00137 private:
00138 class SplineImpl;
00139 SplineImpl* impl;
00140 };
00141
00142 typedef Spline_<Real> Spline;
00143
00144 template <class T>
00145 class Spline_<T>::SplineImpl {
00146 public:
00147 SplineImpl(int degree, const Vector& x, const Vector_<T>& y) : referenceCount(1), degree(degree), x(x), y(y) {
00148 }
00149 ~SplineImpl() {
00150 assert(referenceCount == 0);
00151 }
00152 T getValue(Real t) const {
00153 return GCVSPLUtil::splder(0, degree, t, x, y);
00154 }
00155 T getDerivative(int derivOrder, Real t) const {
00156 return GCVSPLUtil::splder(derivOrder, degree, t, x, y);
00157 }
00158 int referenceCount;
00159 int degree;
00160 Vector x;
00161 Vector_<T> y;
00162 };
00163
00164 }
00165
00166 #endif // SimTK_SIMMATH_SPLINE_H_
00167
00168