Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
CatmullRomInterpolator.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef CatmullRomInterpolator_INCLUDE_ONCE
33 #define CatmullRomInterpolator_INCLUDE_ONCE
34 
35 #include <vlCore/Interpolator.hpp>
36 
37 namespace vl
38 {
45  template<typename T>
47  {
49 
50  public:
52  {
53  VL_DEBUG_SET_OBJECT_NAME()
54  }
55 
56  CatmullRomInterpolator(const std::vector<T>& path): mPath(path) {}
57 
59  void setupEndPoints(bool is_loop)
60  {
61  VL_CHECK(mPath.size()>=2)
62  if (mPath.size()<2)
63  return;
64 
66 
67  /*
68  D-------C
69  . |
70  . |
71  . |
72  A-------B
73  */
74 
75  if (is_loop)
76  {
77  T a = mCatmullRomSpline[0];
78  T b = mCatmullRomSpline[1];
79  T d = mCatmullRomSpline[mCatmullRomSpline.size()-1];
80 
81  mCatmullRomSpline.insert(mCatmullRomSpline.begin(),d);
82  mCatmullRomSpline.push_back(a);
83  mCatmullRomSpline.push_back(b);
84  }
85  else
86  {
87  T a = mCatmullRomSpline[0] + (mCatmullRomSpline[0] - mCatmullRomSpline[1]);
88  T b = mCatmullRomSpline[mCatmullRomSpline.size()-1] + (mCatmullRomSpline[mCatmullRomSpline.size()-1] - mCatmullRomSpline[mCatmullRomSpline.size()-2]);
89  mCatmullRomSpline.insert(mCatmullRomSpline.begin(),a);
90  mCatmullRomSpline.push_back(b);
91  }
92 
93  }
94 
96  T computePoint(float t) const
97  {
98  VL_CHECK(mCatmullRomSpline.size() >= 4)
99  size_t size = mCatmullRomSpline.size()-2;
100  t = clamp(t, 0.0f, 1.0f);
101  if (t == 0.0f)
102  return mCatmullRomSpline[1];
103  else
104  if (t == 1.0f)
105  return mCatmullRomSpline[ mCatmullRomSpline.size()-1-1 ];
106  else
107  {
108  int i = 1 + (int)((size-1)*t);
109  int i0 = i-1;
110  int i1 = i;
111  int i2 = i+1;
112  int i3 = i+2;
113  VL_CHECK(i3<(int)mCatmullRomSpline.size())
114  float tt = (size-1)*t - int((size-1)*t); // frac generates rounding errors
115  T p0 = mCatmullRomSpline[i0];
116  T p1 = mCatmullRomSpline[i1];
117  T p2 = mCatmullRomSpline[i2];
118  T p3 = mCatmullRomSpline[i3];
119  T p = ( (p1 * 2.0f) + (-p0 + p2) * tt +
120  ( p0*2.0f - p1*5.0f + p2*4.0f - p3) * tt*tt +
121  ( p0*-1 + p1*3.0f - p2*3.0f + p3) * tt*tt*tt ) * 0.5f;
122  return p;
123  }
124  }
125 
130  void setPath(const std::vector<T>& path) { mPath = path; }
131 
133  const std::vector<T>& path() const { return mPath; }
134 
136  std::vector<T>& path() { return mPath; }
137 
138  protected:
139  std::vector<T> mPath;
140  std::vector<T> mCatmullRomSpline;
141  };
142 
151 
154  {
156  public:
157  CatmullRomInterpolatorFVec4(): mInterpolator( new CatmullRomInterpolatorFVec4_T ) {}
158  CatmullRomInterpolatorFVec4(const std::vector<fvec4>& path): mInterpolator( new CatmullRomInterpolatorFVec4_T(path) ) {}
159  fvec4 computePoint(float t) const { return interpolator()->computePoint(t); }
160  CatmullRomInterpolatorFVec4_T* interpolator() { return mInterpolator.get(); }
161  const CatmullRomInterpolatorFVec4_T* interpolator() const { return mInterpolator.get(); }
162  void setInterpolator(CatmullRomInterpolatorFVec4_T* interpolator) { mInterpolator = interpolator; }
163  protected:
165  };
168  {
170  public:
171  CatmullRomInterpolatorFVec3(): mInterpolator( new CatmullRomInterpolatorFVec3_T ) {}
172  CatmullRomInterpolatorFVec3(const std::vector<fvec3>& path): mInterpolator( new CatmullRomInterpolatorFVec3_T(path) ) {}
173  fvec3 computePoint(float t) const { return interpolator()->computePoint(t); }
174  CatmullRomInterpolatorFVec3_T* interpolator() { return mInterpolator.get(); }
175  const CatmullRomInterpolatorFVec3_T* interpolator() const { return mInterpolator.get(); }
176  void setInterpolator(CatmullRomInterpolatorFVec3_T* interpolator) { mInterpolator = interpolator; }
177  protected:
179  };
182  {
184  public:
185  CatmullRomInterpolatorFVec2(): mInterpolator( new CatmullRomInterpolatorFVec2_T ) {}
186  CatmullRomInterpolatorFVec2(const std::vector<fvec2>& path): mInterpolator( new CatmullRomInterpolatorFVec2_T(path) ) {}
187  fvec2 computePoint(float t) const { return interpolator()->computePoint(t); }
188  CatmullRomInterpolatorFVec2_T* interpolator() { return mInterpolator.get(); }
189  const CatmullRomInterpolatorFVec2_T* interpolator() const { return mInterpolator.get(); }
190  void setInterpolator(CatmullRomInterpolatorFVec2_T* interpolator) { mInterpolator = interpolator; }
191  protected:
193  };
196  {
198  public:
199  CatmullRomInterpolatorFloat(): mInterpolator( new CatmullRomInterpolatorFloat_T ) {}
200  CatmullRomInterpolatorFloat(const std::vector<float>& path): mInterpolator( new CatmullRomInterpolatorFloat_T(path) ) {}
201  float computePoint(float t) const { return interpolator()->computePoint(t); }
202  CatmullRomInterpolatorFloat_T* interpolator() { return mInterpolator.get(); }
203  const CatmullRomInterpolatorFloat_T* interpolator() const { return mInterpolator.get(); }
204  void setInterpolator(CatmullRomInterpolatorFloat_T* interpolator) { mInterpolator = interpolator; }
205  protected:
207  };
210  {
212  public:
213  CatmullRomInterpolatorDVec4(): mInterpolator( new CatmullRomInterpolatorDVec4_T ) {}
214  CatmullRomInterpolatorDVec4(const std::vector<dvec4>& path): mInterpolator( new CatmullRomInterpolatorDVec4_T(path) ) {}
215  dvec4 computePoint(float t) const { return interpolator()->computePoint(t); }
216  CatmullRomInterpolatorDVec4_T* interpolator() { return mInterpolator.get(); }
217  const CatmullRomInterpolatorDVec4_T* interpolator() const { return mInterpolator.get(); }
218  void setInterpolator(CatmullRomInterpolatorDVec4_T* interpolator) { mInterpolator = interpolator; }
219  protected:
221  };
224  {
226  public:
227  CatmullRomInterpolatorDVec3(): mInterpolator( new CatmullRomInterpolatorDVec3_T ) {}
228  CatmullRomInterpolatorDVec3(const std::vector<dvec3>& path): mInterpolator( new CatmullRomInterpolatorDVec3_T(path) ) {}
229  dvec3 computePoint(float t) const { return interpolator()->computePoint(t); }
230  CatmullRomInterpolatorDVec3_T* interpolator() { return mInterpolator.get(); }
231  const CatmullRomInterpolatorDVec3_T* interpolator() const { return mInterpolator.get(); }
232  void setInterpolator(CatmullRomInterpolatorDVec3_T* interpolator) { mInterpolator = interpolator; }
233  protected:
235  };
238  {
240  public:
241  CatmullRomInterpolatorDVec2(): mInterpolator( new CatmullRomInterpolatorDVec2_T ) {}
242  CatmullRomInterpolatorDVec2(const std::vector<dvec2>& path): mInterpolator( new CatmullRomInterpolatorDVec2_T(path) ) {}
243  dvec2 computePoint(float t) const { return interpolator()->computePoint(t); }
244  CatmullRomInterpolatorDVec2_T* interpolator() { return mInterpolator.get(); }
245  const CatmullRomInterpolatorDVec2_T* interpolator() const { return mInterpolator.get(); }
246  void setInterpolator(CatmullRomInterpolatorDVec2_T* interpolator) { mInterpolator = interpolator; }
247  protected:
249  };
252  {
254  public:
255  CatmullRomInterpolatorDouble(): mInterpolator( new CatmullRomInterpolatorDouble_T ) {}
256  CatmullRomInterpolatorDouble(const std::vector<double>& path): mInterpolator( new CatmullRomInterpolatorDouble_T(path) ) {}
257  double computePoint(float t) const { return interpolator()->computePoint(t); }
258  CatmullRomInterpolatorDouble_T* interpolator() { return mInterpolator.get(); }
259  const CatmullRomInterpolatorDouble_T* interpolator() const { return mInterpolator.get(); }
260  void setInterpolator(CatmullRomInterpolatorDouble_T* interpolator) { mInterpolator = interpolator; }
261  protected:
263  };
264 }
265 
266 #endif
const CatmullRomInterpolatorFloat_T * interpolator() const
CatmullRomInterpolator< float > CatmullRomInterpolatorFloat_T
Abstract class that interpolates vl::fvec2 values.
ref< CatmullRomInterpolatorDVec4_T > mInterpolator
float clamp(float x, float minval, float maxval)
Definition: Vector2.hpp:315
CatmullRomInterpolatorDVec3_T * interpolator()
CatmullRomInterpolatorDVec3(const std::vector< dvec3 > &path)
const CatmullRomInterpolatorFVec2_T * interpolator() const
CatmullRomInterpolatorDVec4_T * interpolator()
void setInterpolator(CatmullRomInterpolatorFloat_T *interpolator)
CatmullRomInterpolatorFVec3(const std::vector< fvec3 > &path)
dvec4 computePoint(float t) const
Samples the interpolator at the given point.
Abstract class that interpolates vl::dvec4 values.
void setInterpolator(CatmullRomInterpolatorFVec4_T *interpolator)
CatmullRomInterpolator< dvec4 > CatmullRomInterpolatorDVec4_T
Abstract class that interpolates vl::dvec2 values.
Interpolates float values using a CatmullRomInterpolator.
void setInterpolator(CatmullRomInterpolatorDouble_T *interpolator)
T computePoint(float t) const
Samples the Catmull-Rom spline at the given point. The t parameter must be in the range 0...
CatmullRomInterpolatorDouble_T * interpolator()
void setInterpolator(CatmullRomInterpolatorFVec3_T *interpolator)
The LinearInterpolator class is a template class that implements Catmull-Rom spline interpolation...
Abstract class that interpolates double values.
Abstract class that interpolates float values.
CatmullRomInterpolatorFloat_T * interpolator()
CatmullRomInterpolatorFVec4_T * interpolator()
fvec4 computePoint(float t) const
Samples the interpolator at the given point.
CatmullRomInterpolator< fvec4 > CatmullRomInterpolatorFVec4_T
const CatmullRomInterpolatorDouble_T * interpolator() const
const CatmullRomInterpolatorDVec4_T * interpolator() const
ref< CatmullRomInterpolatorFVec4_T > mInterpolator
CatmullRomInterpolator< fvec3 > CatmullRomInterpolatorFVec3_T
void setPath(const std::vector< T > &path)
The control points defining the Catmull-Rom spline.
Interpolates double values using a CatmullRomInterpolator.
Interpolates dvec2 values using a CatmullRomInterpolator.
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
CatmullRomInterpolatorDVec2_T * interpolator()
ref< CatmullRomInterpolatorFVec2_T > mInterpolator
ref< CatmullRomInterpolatorFloat_T > mInterpolator
Visualization Library main namespace.
CatmullRomInterpolatorFVec2(const std::vector< fvec2 > &path)
CatmullRomInterpolator< double > CatmullRomInterpolatorDouble_T
ref< CatmullRomInterpolatorDVec3_T > mInterpolator
Interpolates fvec2 values using a CatmullRomInterpolator.
fvec3 computePoint(float t) const
Samples the interpolator at the given point.
double computePoint(float t) const
Samples the interpolator at the given point.
CatmullRomInterpolatorDVec2(const std::vector< dvec2 > &path)
fvec2 computePoint(float t) const
Samples the interpolator at the given point.
CatmullRomInterpolatorFVec4(const std::vector< fvec4 > &path)
Interpolates dvec4 values using a CatmullRomInterpolator.
ref< CatmullRomInterpolatorDouble_T > mInterpolator
CatmullRomInterpolator< dvec3 > CatmullRomInterpolatorDVec3_T
The base class for all the reference counted objects.
Definition: Object.hpp:158
const CatmullRomInterpolatorDVec2_T * interpolator() const
ref< CatmullRomInterpolatorFVec3_T > mInterpolator
CatmullRomInterpolator< dvec2 > CatmullRomInterpolatorDVec2_T
float computePoint(float t) const
Samples the interpolator at the given point.
CatmullRomInterpolatorDVec4(const std::vector< dvec4 > &path)
Interpolates fvec4 values using a CatmullRomInterpolator.
void setInterpolator(CatmullRomInterpolatorDVec3_T *interpolator)
CatmullRomInterpolatorFloat(const std::vector< float > &path)
void setupEndPoints(bool is_loop)
Call this function after having specified the control points if you want to automatically generate th...
CatmullRomInterpolatorFVec2_T * interpolator()
Interpolates dvec3 values using a CatmullRomInterpolator.
void setInterpolator(CatmullRomInterpolatorFVec2_T *interpolator)
std::vector< T > & path()
The control points defining the Catmull-Rom spline.
dvec2 computePoint(float t) const
Samples the interpolator at the given point.
CatmullRomInterpolator(const std::vector< T > &path)
Abstract class that interpolates vl::fvec3 values.
const std::vector< T > & path() const
The control points defining the Catmull-Rom spline.
const CatmullRomInterpolatorDVec3_T * interpolator() const
Interpolates fvec3 values using a CatmullRomInterpolator.
dvec3 computePoint(float t) const
Samples the interpolator at the given point.
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
const CatmullRomInterpolatorFVec4_T * interpolator() const
CatmullRomInterpolatorDouble(const std::vector< double > &path)
Abstract class that interpolates vl::dvec3 values.
ref< CatmullRomInterpolatorDVec2_T > mInterpolator
const CatmullRomInterpolatorFVec3_T * interpolator() const
void setInterpolator(CatmullRomInterpolatorDVec2_T *interpolator)
CatmullRomInterpolator< fvec2 > CatmullRomInterpolatorFVec2_T
#define VL_CHECK(expr)
Definition: checks.hpp:73
void setInterpolator(CatmullRomInterpolatorDVec4_T *interpolator)
CatmullRomInterpolatorFVec3_T * interpolator()
Abstract class that interpolates vl::fvec4 values.