Spline.h

Go to the documentation of this file.
00001 #ifndef SimTK_SIMMATH_SPLINE_H_
00002 #define SimTK_SIMMATH_SPLINE_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                      SimTK Core: SimTK Simmath(tm)                         *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK Core biosimulation toolkit originating from      *
00008  * Simbios, the NIH National Center for Physics-Based Simulation of           *
00009  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
00010  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
00011  *                                                                            *
00012  * Portions copyright (c) 2008-2009 Stanford University and the Authors.      *
00013  * Authors: Peter Eastman                                                     *
00014  * Contributors:                                                              *
00015  *                                                                            *
00016  * Permission is hereby granted, free of charge, to any person obtaining a    *
00017  * copy of this software and associated documentation files (the "Software"), *
00018  * to deal in the Software without restriction, including without limitation  *
00019  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
00020  * and/or sell copies of the Software, and to permit persons to whom the      *
00021  * Software is furnished to do so, subject to the following conditions:       *
00022  *                                                                            *
00023  * The above copyright notice and this permission notice shall be included in *
00024  * all copies or substantial portions of the Software.                        *
00025  *                                                                            *
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
00029  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
00030  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
00031  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
00032  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
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 } // namespace SimTK
00165 
00166 #endif // SimTK_SIMMATH_SPLINE_H_
00167 
00168 

Generated on Thu Aug 12 16:37:26 2010 for SimTKcore by  doxygen 1.6.1