Simbody
|
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/common.h" 00037 #include "simmath/internal/GCVSPLUtil.h" 00038 00039 #include <limits> 00040 00041 namespace SimTK { 00042 00051 template <class T> 00052 class Spline_ : public Function_<T> { 00053 public: 00061 Spline_(int degree, const Vector& x, const Vector_<T>& y) : impl(new SplineImpl(degree, x, y)) { 00062 } 00063 Spline_(const Spline_& copy) : impl(copy.impl) { 00064 impl->referenceCount++; 00065 } 00066 Spline_() : impl(NULL) { 00067 } 00068 Spline_& operator=(const Spline_& copy) { 00069 if (impl) { 00070 impl->referenceCount--; 00071 if (impl->referenceCount == 0) 00072 delete impl; 00073 } 00074 impl = copy.impl; 00075 assert(impl); 00076 impl->referenceCount++; 00077 return *this; 00078 } 00079 ~Spline_() { 00080 if (impl) { 00081 impl->referenceCount--; 00082 if (impl->referenceCount == 0) 00083 delete impl; 00084 } 00085 } 00091 T calcValue(const Vector& x) const { 00092 assert(impl); 00093 assert(x.size() == 1); 00094 return impl->getValue(x[0]); 00095 } 00101 T calcDerivative(const Array_<int>& derivComponents, const Vector& x) const { 00102 assert(impl); 00103 assert(x.size() == 1); 00104 assert(derivComponents.size() > 0); 00105 return impl->getDerivative(derivComponents.size(), x[0]); 00106 } 00107 int getArgumentSize() const { 00108 return 1; 00109 } 00110 int getMaxDerivativeOrder() const { 00111 return std::numeric_limits<int>::max(); 00112 } 00116 const Vector& getControlPointLocations() const { 00117 assert(impl); 00118 return impl->x; 00119 } 00123 const Vector_<T>& getControlPointValues() const { 00124 assert(impl); 00125 return impl->y; 00126 } 00130 int getSplineDegree() const { 00131 assert(impl); 00132 return impl->degree; 00133 } 00134 00136 T calcDerivative(const std::vector<int>& derivComponents, const Vector& x) const 00137 { return calcDerivative(ArrayViewConst_<int>(derivComponents),x); } 00138 private: 00139 class SplineImpl; 00140 SplineImpl* impl; 00141 }; 00142 00143 typedef Spline_<Real> Spline; 00144 00145 template <class T> 00146 class Spline_<T>::SplineImpl { 00147 public: 00148 SplineImpl(int degree, const Vector& x, const Vector_<T>& y) : referenceCount(1), degree(degree), x(x), y(y) { 00149 } 00150 ~SplineImpl() { 00151 assert(referenceCount == 0); 00152 } 00153 T getValue(Real t) const { 00154 return GCVSPLUtil::splder(0, degree, t, x, y); 00155 } 00156 T getDerivative(int derivOrder, Real t) const { 00157 return GCVSPLUtil::splder(derivOrder, degree, t, x, y); 00158 } 00159 int referenceCount; 00160 int degree; 00161 Vector x; 00162 Vector_<T> y; 00163 }; 00164 00165 } // namespace SimTK 00166 00167 #endif // SimTK_SIMMATH_SPLINE_H_ 00168 00169