Simbody  3.8
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 #include <memory>
33 
34 namespace SimTK {
35 
52 template <class T>
53 class Spline_ : public Function_<T> {
54 public:
65  Spline_(int degree, const Vector& x, const Vector_<T>& y)
66  : impl(std::make_shared<SplineImpl>(degree, x, y)) {}
67 
70  Spline_() = default;
71 
74  Spline_(const Spline_& source) = default;
75 
78  Spline_& operator=(const Spline_& source) = default;
79 
82  ~Spline_() = default;
83 
88  T calcValue(Real x) const {
89  assert(impl);
90  return impl->getValue(x);
91  }
92 
103  T calcDerivative(int order, Real x) const {
104  assert(impl);
105  assert(order > 0);
106  return impl->getDerivative(order, x);
107  }
108 
112  assert(impl);
113  return impl->x;
114  }
118  assert(impl);
119  return impl->y;
120  }
121 
123  int getSplineDegree() const {
124  assert(impl);
125  return impl->degree;
126  }
127 
130  T calcValue(const Vector& x) const override {
131  assert(x.size() == 1);
132  return calcValue(x[0]);
133  }
140  T calcDerivative(const Array_<int>& derivComponents, const Vector& x) const
141  override
142  { assert(x.size() == 1);
143  return calcDerivative((int)derivComponents.size(), x[0]); }
146  T calcDerivative(const std::vector<int>& derivComponents,
147  const Vector& x) const
148  { assert(x.size() == 1);
149  return calcDerivative((int)derivComponents.size(), x[0]); }
150 
152  int getArgumentSize() const override {return 1;}
154  int getMaxDerivativeOrder() const override
155  { return std::numeric_limits<int>::max(); }
157  Spline_* clone() const override {return new Spline_(*this);}
158 
159 private:
160  class SplineImpl;
161  std::shared_ptr<SplineImpl> impl;
162 };
163 
166 
168 template <class T>
169 class Spline_<T>::SplineImpl {
170 public:
171  SplineImpl(int degree, const Vector& x, const Vector_<T>& y)
172  : degree(degree), x(x), y(y) {}
173 
174  T getValue(Real t) const {
175  return GCVSPLUtil::splder(0, degree, t, x, y);
176  }
177  T getDerivative(int derivOrder, Real t) const {
178  return GCVSPLUtil::splder(derivOrder, degree, t, x, y);
179  }
180 
181  int degree;
184 };
185 
186 } // namespace SimTK
187 
188 #endif // SimTK_SIMMATH_SPLINE_H_
189 
190 
Includes internal headers providing declarations for the basic SimTK Core classes,...
This is the header file that every Simmath compilation unit should include first.
size_type size() const
Return the current number of elements stored in this array.
Definition: Array.h:2075
This abstract class represents a mathematical function that calculates a value of arbitrary type base...
Definition: Function.h:51
static Real splder(int derivOrder, int degree, Real t, const Vector &x, const Vector &coeff)
This is the implementation class that supports the Spline_ interface.
Definition: Spline.h:169
Vector x
Definition: Spline.h:182
SplineImpl(int degree, const Vector &x, const Vector_< T > &y)
Definition: Spline.h:171
T getDerivative(int derivOrder, Real t) const
Definition: Spline.h:177
int degree
Definition: Spline.h:181
Vector_< T > y
Definition: Spline.h:183
T getValue(Real t) const
Definition: Spline.h:174
This class implements a non-uniform Bezier curve.
Definition: Spline.h:53
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:111
int getArgumentSize() const override
Required by the Function_ interface.
Definition: Spline.h:152
const Vector_< T > & getControlPointValues() const
Get the values of the dependent variables at each of the Bezier control points.
Definition: Spline.h:117
int getSplineDegree() const
Get the degree of the spline.
Definition: Spline.h:123
~Spline_()=default
Destructor decrements the reference count and frees the heap space if this is the last reference.
T calcValue(const Vector &x) const override
Alternate signature provided to implement the generic Function_ interface expects a one-element Vecto...
Definition: Spline.h:130
int getMaxDerivativeOrder() const override
Required by the Function_ interface.
Definition: Spline.h:154
Spline_ * clone() const override
Required by the Function_ interface.
Definition: Spline.h:157
Spline_(const Spline_ &source)=default
Copy constructor is shallow and reference-counted; that is, the new Spline_ refers to the same object...
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:65
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:146
Spline_ & operator=(const Spline_ &source)=default
Copy assignment is shallow and reference-counted; that is, after the assignment this Spline_ refers t...
T calcValue(Real x) const
Calculate the values of the dependent variables at a particular value of the independent variable.
Definition: Spline.h:88
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:140
Spline_()=default
Default constructor creates an empty Spline_ handle; not very useful.
T calcDerivative(int order, Real x) const
Calculate a derivative of the spline function with respect to its independent variable,...
Definition: Spline.h:103
int size() const
Definition: VectorBase.h:396
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
Spline_< Real > Spline
Provide a convenient name for a scalar-valued Spline_.
Definition: Spline.h:165
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:607