Simbody  3.5
Spline.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMMATH_SPLINE_H_
2 #define SimTK_SIMMATH_SPLINE_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKmath *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2008-13 Stanford University and the Authors. *
13  * Authors: Peter Eastman *
14  * Contributors: Michael Sherman *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
27 #include "SimTKcommon.h"
30 
31 #include <limits>
32 
33 namespace SimTK {
34 
51 template <class T>
52 class Spline_ : public Function_<T> {
53 public:
64  Spline_(int degree, const Vector& x, const Vector_<T>& y)
65  : impl(new SplineImpl(degree, x, y)) {}
66 
69  Spline_() : impl(NULL) {}
70 
73  Spline_(const Spline_& source) : impl(source.impl)
74  { if (impl) impl->referenceCount++; }
75 
78  Spline_& operator=(const Spline_& source) {
79  if (impl) {
80  impl->referenceCount--;
81  if (impl->referenceCount == 0)
82  delete impl;
83  }
84  impl = source.impl;
85  if (impl) impl->referenceCount++;
86  return *this;
87  }
88 
92  if (impl) {
93  impl->referenceCount--;
94  if (impl->referenceCount == 0)
95  delete impl;
96  }
97  }
98 
103  T calcValue(Real x) const {
104  assert(impl);
105  return impl->getValue(x);
106  }
107 
118  T calcDerivative(int order, Real x) const {
119  assert(impl);
120  assert(order > 0);
121  return impl->getDerivative(order, x);
122  }
123 
127  assert(impl);
128  return impl->x;
129  }
133  assert(impl);
134  return impl->y;
135  }
136 
138  int getSplineDegree() const {
139  assert(impl);
140  return impl->degree;
141  }
142 
145  T calcValue(const Vector& x) const OVERRIDE_11 {
146  assert(x.size() == 1);
147  return calcValue(x[0]);
148  }
155  T calcDerivative(const Array_<int>& derivComponents, const Vector& x) const
157  { assert(x.size() == 1);
158  return calcDerivative((int)derivComponents.size(), x[0]); }
161  T calcDerivative(const std::vector<int>& derivComponents,
162  const Vector& x) const
163  { assert(x.size() == 1);
164  return calcDerivative((int)derivComponents.size(), x[0]); }
165 
167  int getArgumentSize() const OVERRIDE_11 {return 1;}
170  { return std::numeric_limits<int>::max(); }
171 
172 private:
173  class SplineImpl;
174  SplineImpl* impl;
175 };
176 
179 
181 template <class T>
182 class Spline_<T>::SplineImpl {
183 public:
184  SplineImpl(int degree, const Vector& x, const Vector_<T>& y)
185  : referenceCount(1), degree(degree), x(x), y(y) {}
187  assert(referenceCount == 0);
188  }
189  T getValue(Real t) const {
190  return GCVSPLUtil::splder(0, degree, t, x, y);
191  }
192  T getDerivative(int derivOrder, Real t) const {
193  return GCVSPLUtil::splder(derivOrder, degree, t, x, y);
194  }
196  int degree;
199 };
200 
201 } // namespace SimTK
202 
203 #endif // SimTK_SIMMATH_SPLINE_H_
204 
205 
T calcValue(const Vector &x) const override
Alternate signature provided to implement the generic Function_ interface expects a one-element Vecto...
Definition: Spline.h:145
Spline_()
Default constructor creates an empty Spline_ handle; not very useful.
Definition: Spline.h:69
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
Spline_(const Spline_ &source)
Copy constructor is shallow and reference-counted; that is, the new Spline_ refers to the same object...
Definition: Spline.h:73
int getMaxDerivativeOrder() const override
Required by the Function_ interface.
Definition: Spline.h:169
int size() const
Definition: VectorBase.h:396
~SplineImpl()
Definition: Spline.h:186
int getSplineDegree() const
Get the degree of the spline.
Definition: Spline.h:138
SimTK_Real Real
This is the default compiled-in floating point type for SimTK, either float or double.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:593
~Spline_()
Destructor decrements the reference count and frees the heap space if this is the last reference...
Definition: Spline.h:91
Spline_< Real > Spline
Provide a convenient name for a scalar-valued Spline_.
Definition: Spline.h:178
T getValue(Real t) const
Definition: Spline.h:189
int getArgumentSize() const override
Required by the Function_ interface.
Definition: Spline.h:167
SplineImpl(int degree, const Vector &x, const Vector_< T > &y)
Definition: Spline.h:184
Spline_(int degree, const Vector &x, const Vector_< T > &y)
Create a Spline_ object based on a set of control points. See SplineFitter for a nicer way to create ...
Definition: Spline.h:64
int degree
Definition: Spline.h:196
const Vector & getControlPointLocations() const
Get the locations (that is, the values of the independent variable) for each of the Bezier control po...
Definition: Spline.h:126
static Real splder(int derivOrder, int degree, Real t, const Vector &x, const Vector &coeff)
Includes internal headers providing declarations for the basic SimTK Core classes, including Simmatrix.
#define OVERRIDE_11
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:267
Vector_< T > y
Definition: Spline.h:198
T calcDerivative(int order, Real x) const
Calculate a derivative of the spline function with respect to its independent variable, at the given value.
Definition: Spline.h:118
Vector x
Definition: Spline.h:197
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
Spline_ & operator=(const Spline_ &source)
Copy assignment is shallow and reference-counted; that is, after the assignment this Spline_ refers t...
Definition: Spline.h:78
T getDerivative(int derivOrder, Real t) const
Definition: Spline.h:192
int referenceCount
Definition: Spline.h:195
This abstract class represents a mathematical function that calculates a value of arbitrary type base...
Definition: Function.h:51
T calcValue(Real x) const
Calculate the values of the dependent variables at a particular value of the independent variable...
Definition: Spline.h:103
This is the header file that every Simmath compilation unit should include first. ...
This is the implementation class that supports the Spline_ interface.
Definition: Spline.h:182
This class implements a non-uniform Bezier curve.
Definition: Spline.h:52
const Vector_< T > & getControlPointValues() const
Get the values of the dependent variables at each of the Bezier control points.
Definition: Spline.h:132
T calcDerivative(const Array_< int > &derivComponents, const Vector &x) const override
Alternate signature provided to implement the generic Function_ interface expects an awkward derivCom...
Definition: Spline.h:155
T calcDerivative(const std::vector< int > &derivComponents, const Vector &x) const
For the Function_ style interface, this provides compatibility with std::vector.
Definition: Spline.h:161