Simbody
3.4 (development)
|
00001 #ifndef SimTK_SIMMATH_GCVSPL_UTIL_H_ 00002 #define SimTK_SIMMATH_GCVSPL_UTIL_H_ 00003 00004 /* -------------------------------------------------------------------------- * 00005 * Simbody(tm): SimTKmath * 00006 * -------------------------------------------------------------------------- * 00007 * This is part of the SimTK 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/home/simbody. * 00011 * * 00012 * Portions copyright (c) 2008-12 Stanford University and the Authors. * 00013 * Authors: Peter Eastman * 00014 * Contributors: * 00015 * * 00016 * Licensed under the Apache License, Version 2.0 (the "License"); you may * 00017 * not use this file except in compliance with the License. You may obtain a * 00018 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. * 00019 * * 00020 * Unless required by applicable law or agreed to in writing, software * 00021 * distributed under the License is distributed on an "AS IS" BASIS, * 00022 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 00023 * See the License for the specific language governing permissions and * 00024 * limitations under the License. * 00025 * -------------------------------------------------------------------------- */ 00026 00027 #include "SimTKcommon.h" 00028 #include "simmath/internal/common.h" 00029 00030 // These are global functions. 00031 int SimTK_gcvspl_(const SimTK::Real *, const SimTK::Real *, int *, const SimTK::Real *, const SimTK::Real *, int *, int *, 00032 int *, int *, SimTK::Real *, SimTK::Real *, int *, SimTK::Real *, int *); 00033 SimTK::Real SimTK_splder_(int *, int *, int *, SimTK::Real *, const SimTK::Real *, const SimTK::Real *, int *, SimTK::Real *, int); 00034 00035 namespace SimTK { 00036 00044 class SimTK_SIMMATH_EXPORT GCVSPLUtil { 00045 public: 00046 static void gcvspl(const Vector& x, const Vector& y, const Vector& wx, Real wy, int m, int md, Real val, Vector& c, Vector& wk, int& ier); 00047 template <int K> 00048 static void gcvspl(const Vector& x, const Vector_<Vec<K> >& y, const Vector& wx, Vec<K> wy, int m, int md, Real val, Vector_<Vec<K> >& c, Vector& wk, int& ier); 00049 static Real splder(int derivOrder, int degree, Real t, const Vector& x, const Vector& coeff); 00050 template <int K> 00051 static Vec<K> splder(int derivOrder, int degree, Real t, const Vector& x, const Vector_<Vec<K> >& coeff); 00052 }; 00053 00054 template <int K> 00055 void GCVSPLUtil::gcvspl(const Vector& x, const Vector_<Vec<K> >& y, const Vector& wx, Vec<K> wy, int degree, int md, Real val, Vector_<Vec<K> >& c, Vector& wk, int& ier) { 00056 SimTK_APIARGCHECK_ALWAYS(degree > 0 && degree%2==1, "GCVSPLUtil", "gcvspl", "degree must be positive and odd"); 00057 SimTK_APIARGCHECK_ALWAYS(y.size() >= x.size(), "GCVSPLUtil", "gcvspl", "y is shorter than x"); 00058 SimTK_APIARGCHECK_ALWAYS(wx.size() >= x.size(), "GCVSPLUtil", "gcvspl", "wx and x must be the same size"); 00059 SimTK_APIARGCHECK_ALWAYS(x.hasContiguousData(), "GCVSPLUtil", "gcvspl", "x must have contiguous storage (i.e. not be a view)"); 00060 SimTK_APIARGCHECK_ALWAYS(wk.hasContiguousData(), "GCVSPLUtil", "gcvspl", "wk must have contiguous storage (i.e. not be a view)"); 00061 00062 // Create various temporary variables. 00063 00064 int m = (degree+1)/2; 00065 int n = x.size(); 00066 int ny = y.size(); 00067 Vector yvec(ny*K); 00068 int index = 0; 00069 for (int j = 0; j < K; ++j) 00070 for (int i = 0; i < ny; ++i) 00071 yvec[index++] = y[i][j]; 00072 int nc = n*K; 00073 Vector cvec(nc); 00074 wk.resize(6*(m*n+1)+n); 00075 int k = K; 00076 00077 // Invoke GCV. 00078 00079 SimTK_gcvspl_(&x[0], &yvec[0], &ny, &wx[0], &wy[0], &m, &n, &k, &md, &val, &cvec[0], &n, &wk[0], &ier); 00080 if (ier != 0) { 00081 SimTK_APIARGCHECK_ALWAYS(n >= 2*m, "GCVSPLUtil", "gcvspl", "Too few data points"); 00082 SimTK_APIARGCHECK_ALWAYS(ier != 2, "GCVSPLUtil", "gcvspl", "The values in x must be strictly increasing"); 00083 SimTK_APIARGCHECK_ALWAYS(ier == 0, "GCVSPLUtil", "gcvspl", "GCVSPL returned an error code"); 00084 } 00085 c.resize(n); 00086 index = 0; 00087 for (int j = 0; j < K; ++j) 00088 for (int i = 0; i < n; ++i) 00089 c[i][j] = cvec[index++]; 00090 } 00091 00092 template <int K> 00093 Vec<K> GCVSPLUtil::splder(int derivOrder, int degree, Real t, const Vector& x, const Vector_<Vec<K> >& coeff) { 00094 assert(derivOrder >= 0); 00095 assert(t >= x[0] && t <= x[x.size()-1]); 00096 assert(x.size() == coeff.size()); 00097 assert(degree > 0 && degree%2==1); 00098 assert(x.hasContiguousData()); 00099 00100 // Create various temporary variables. 00101 00102 00103 Vec<K> result; 00104 int m = (degree+1)/2; 00105 int n = x.size(); 00106 int interval = (int) ceil(n*(t-x[0])/(x[n-1]-x[0])); 00107 00108 const int MaxCheapM = 32; 00109 Real qbuf[2*MaxCheapM]; 00110 00111 Real *q = qbuf; // tentatively 00112 if (m > MaxCheapM) 00113 q = new Real[2*m]; // use heap instead; don't forget to delete 00114 00115 int offset = (int) (&coeff[1][0]-&coeff[0][0]); 00116 00117 // Evaluate the spline one component at a time. 00118 00119 for (int i = 0; i < K; ++i) 00120 result[i] = SimTK_splder_(&derivOrder, &m, &n, &t, &x[0], &coeff[0][i], &interval, q, offset); 00121 00122 if (m > MaxCheapM) 00123 delete[] q; 00124 00125 return result; 00126 } 00127 00128 } // namespace SimTK 00129 00130 #endif // SimTK_SIMMATH_GCVSPL_UTIL_H_ 00131 00132