Simbody
|
00001 #ifndef SimTK_SIMMATH_SPLINE_FITTER_H_ 00002 #define SimTK_SIMMATH_SPLINE_FITTER_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 "SimTKcommon.h" 00036 #include "simmath/internal/common.h" 00037 #include "simmath/internal/Spline.h" 00038 #include "simmath/internal/GCVSPLUtil.h" 00039 00040 namespace SimTK { 00041 00074 template <class T> 00075 class SplineFitter { 00076 public: 00077 SplineFitter(const SplineFitter& copy) : impl(copy.impl) { 00078 impl->referenceCount++; 00079 } 00080 SplineFitter operator=(const SplineFitter& copy) { 00081 impl = copy.impl; 00082 impl->referenceCount++; 00083 return *this; 00084 } 00085 ~SplineFitter() { 00086 impl->referenceCount--; 00087 if (impl->referenceCount == 0) 00088 delete impl; 00089 } 00097 static SplineFitter fitFromGCV(int degree, const Vector& x, const Vector_<T>& y) { 00098 Vector_<T> coeff; 00099 Vector wk; 00100 int ier; 00101 GCVSPLUtil::gcvspl(x, y, Vector(x.size(), 1.0), static_cast<T>(1), degree, 2, 0, coeff, wk, ier); 00102 return SplineFitter<T>(new SplineFitterImpl(degree, Spline_<T>(degree, x, coeff), wk[3], wk[4], wk[2])); 00103 } 00112 static SplineFitter fitFromErrorVariance(int degree, const Vector& x, const Vector_<T>& y, Real error) { 00113 Vector_<T> coeff; 00114 Vector wk; 00115 int ier; 00116 GCVSPLUtil::gcvspl(x, y, Vector(x.size(), 1.0), 1, degree, 3, error, coeff, wk, ier); 00117 return SplineFitter<T>(new SplineFitterImpl(degree, Spline_<T>(degree, x, coeff), wk[3], wk[4], wk[2])); 00118 } 00128 static SplineFitter fitFromDOF(int degree, const Vector& x, const Vector_<T>& y, Real dof) { 00129 Vector_<T> coeff; 00130 Vector wk; 00131 int ier; 00132 GCVSPLUtil::gcvspl(x, y, Vector(x.size(), 1.0), static_cast<T>(1), degree, 4, dof, coeff, wk, ier); 00133 return SplineFitter<T>(new SplineFitterImpl(degree, Spline_<T>(degree, x, coeff), wk[3], wk[4], wk[2])); 00134 } 00143 static SplineFitter fitForSmoothingParameter(int degree, const Vector& x, const Vector_<T>& y, Real p) { 00144 Vector_<T> coeff; 00145 Vector wk; 00146 int ier; 00147 GCVSPLUtil::gcvspl(x, y, Vector(x.size(), 1.0), static_cast<T>(1), degree, 1, p, coeff, wk, ier); 00148 return SplineFitter<T>(new SplineFitterImpl(degree, Spline_<T>(degree, x, coeff), wk[3], wk[4], wk[2])); 00149 } 00153 const Spline_<T>& getSpline() { 00154 return impl->spline; 00155 } 00159 Real getSmoothingParameter() { 00160 return impl->p; 00161 } 00165 Real getMeanSquaredError() { 00166 return impl->error; 00167 } 00171 Real getDegreesOfFreedom() { 00172 return impl->dof; 00173 } 00174 private: 00175 class SplineFitterImpl; 00176 SplineFitter(SplineFitterImpl *impl) : impl(impl) { 00177 } 00178 SplineFitterImpl* impl; 00179 }; 00180 00181 template <class T> 00182 class SplineFitter<T>::SplineFitterImpl { 00183 public: 00184 SplineFitterImpl(int degree, const Spline_<T>& spline, Real p, Real error, Real dof) : referenceCount(1), degree(degree), spline(spline), p(p), error(error), dof(dof) { 00185 } 00186 ~SplineFitterImpl() { 00187 assert(referenceCount == 0); 00188 } 00189 int referenceCount; 00190 int degree; 00191 Spline_<T> spline; 00192 Real p, error, dof; 00193 }; 00194 00195 } // namespace SimTK 00196 00197 #endif // SimTK_SIMMATH_SPLINE_FITTER_H_ 00198 00199