SplineFitter.h

Go to the documentation of this file.
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 "simmath/internal/Spline.h"
00036 #include "simmath/internal/GCVSPLUtil.h"
00037 
00038 namespace SimTK {
00039 
00072 template <class T>
00073 class SplineFitter {
00074 public:
00075     SplineFitter(const SplineFitter& copy) : impl(copy.impl) {
00076         impl->referenceCount++;
00077     }
00078     SplineFitter operator=(const SplineFitter& copy) {
00079         impl = copy.impl;
00080         impl->referenceCount++;
00081         return *this;
00082     }
00083     ~SplineFitter() {
00084         impl->referenceCount--;
00085         if (impl->referenceCount == 0)
00086             delete impl;
00087     }
00095     static SplineFitter fitFromGCV(int degree, const Vector& x, const Vector_<T>& y) {
00096         Vector_<T> coeff;
00097         Vector wk;
00098         int ier;
00099         GCVSPLUtil::gcvspl(x, y, Vector(x.size(), 1.0), static_cast<T>(1), degree, 2, 0, coeff, wk, ier);
00100         return SplineFitter<T>(new SplineFitterImpl(degree, Spline_<T>(degree, x, coeff), wk[3], wk[4], wk[2]));
00101     }
00110     static SplineFitter fitFromErrorVariance(int degree, const Vector& x, const Vector_<T>& y, Real error) {
00111         Vector_<T> coeff;
00112         Vector wk;
00113         int ier;
00114         GCVSPLUtil::gcvspl(x, y, Vector(x.size(), 1.0), 1, degree, 3, error, coeff, wk, ier);
00115         return SplineFitter<T>(new SplineFitterImpl(degree, Spline_<T>(degree, x, coeff), wk[3], wk[4], wk[2]));
00116     }
00126     static SplineFitter fitFromDOF(int degree, const Vector& x, const Vector_<T>& y, Real dof) {
00127         Vector_<T> coeff;
00128         Vector wk;
00129         int ier;
00130         GCVSPLUtil::gcvspl(x, y, Vector(x.size(), 1.0), static_cast<T>(1), degree, 4, dof, coeff, wk, ier);
00131         return SplineFitter<T>(new SplineFitterImpl(degree, Spline_<T>(degree, x, coeff), wk[3], wk[4], wk[2]));
00132     }
00141     static SplineFitter fitForSmoothingParameter(int degree, const Vector& x, const Vector_<T>& y, Real p) {
00142         Vector_<T> coeff;
00143         Vector wk;
00144         int ier;
00145         GCVSPLUtil::gcvspl(x, y, Vector(x.size(), 1.0), static_cast<T>(1), degree, 1, p, coeff, wk, ier);
00146         return SplineFitter<T>(new SplineFitterImpl(degree, Spline_<T>(degree, x, coeff), wk[3], wk[4], wk[2]));
00147     }
00151     const Spline_<T>& getSpline() {
00152         return impl->spline;
00153     }
00157     Real getSmoothingParameter() {
00158         return impl->p;
00159     }
00163     Real getMeanSquaredError() {
00164         return impl->error;
00165     }
00169     Real getDegreesOfFreedom() {
00170         return impl->dof;
00171     }
00172 private:
00173     class SplineFitterImpl;
00174     SplineFitter(SplineFitterImpl *impl) : impl(impl) {
00175     }
00176     SplineFitterImpl* impl;
00177 };
00178 
00179 template <class T>
00180 class SplineFitter<T>::SplineFitterImpl {
00181 public:
00182     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) {
00183     }
00184     ~SplineFitterImpl() {
00185         assert(referenceCount == 0);
00186     }
00187     int referenceCount;
00188     int degree;
00189     Spline_<T> spline;
00190     Real p, error, dof;
00191 };
00192 
00193 } // namespace SimTK
00194 
00195 #endif // SimTK_SIMMATH_SPLINE_FITTER_H_
00196 
00197 

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