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 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 "simmath/internal/Function.h"
00036 #include "simmath/internal/GCVSPLUtil.h"
00037 
00038 #include <limits>
00039 
00040 namespace SimTK {
00041 
00049 template <int N>
00050 class Spline : public Function<N> {
00051 public:
00059     Spline(int degree, const Vector& x, const Vector_<Vec<N> >& y) : impl(new SplineImpl(degree, x, y)) {
00060     }
00061     Spline(const Spline& copy) : impl(copy.impl) {
00062         impl->referenceCount++;
00063     }
00064     Spline() : impl(NULL) {
00065     }
00066     Spline& operator=(const Spline& copy) {
00067         if (impl) {
00068             impl->referenceCount--;
00069             if (impl->referenceCount == 0)
00070                 delete impl;
00071         }
00072         impl = copy.impl;
00073         assert(impl);
00074         impl->referenceCount++;
00075         return *this;
00076     }
00077     ~Spline() {
00078         if (impl) {
00079             impl->referenceCount--;
00080             if (impl->referenceCount == 0)
00081                 delete impl;
00082         }
00083     }
00089     Vec<N> calcValue(const Vector& x) const {
00090         assert(impl);
00091         assert(x.size() == 1);
00092         return impl->getValue(x[0]);
00093     }
00099     Vec<N> calcDerivative(const std::vector<int>& derivComponents, const Vector& x) const {
00100         assert(impl);
00101         assert(x.size() == 1);
00102         assert(derivComponents.size() > 0);
00103         return impl->getDerivative(derivComponents.size(), x[0]);
00104     }
00105     int getArgumentSize() const {
00106         return 1;
00107     }
00108     int getMaxDerivativeOrder() const {
00109         return std::numeric_limits<int>::max();
00110     }
00114     const Vector& getControlPointLocations() const {
00115         assert(impl);
00116         return impl->x;
00117     }
00121     const Vector_<Vec<N> >& getControlPointValues() const {
00122         assert(impl);
00123         return impl->y;
00124     }
00128     int getSplineDegree() const {
00129         assert(impl);
00130         return impl->degree;
00131     }
00132 private:
00133     class SplineImpl;
00134     SplineImpl* impl;
00135 };
00136 
00137 template <int N>
00138 class Spline<N>::SplineImpl {
00139 public:
00140     SplineImpl(int degree, const Vector& x, const Vector_<Vec<N> >& y) : degree(degree), x(x), y(y), referenceCount(1) {
00141     }
00142     ~SplineImpl() {
00143         assert(referenceCount == 0);
00144     }
00145     Vec<N> getValue(Real t) const {
00146         return GCVSPLUtil::splder(0, degree, t, x, y);
00147     }
00148     Vec<N> getDerivative(int derivOrder, Real t) const {
00149         return GCVSPLUtil::splder(derivOrder, degree, t, x, y);
00150     }
00151     int referenceCount;
00152     int degree;
00153     Vector x;
00154     Vector_<Vec<N> > y;
00155 };
00156 
00157 } // namespace SimTK
00158 
00159 #endif // SimTK_SIMMATH_SPLINE_H_
00160 
00161 

Generated on Fri Sep 26 07:44:17 2008 for SimTKcore by  doxygen 1.5.6