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]
LinearInterpolator.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 LinearInterpolator_INCLUDE_ONCE
33 #define LinearInterpolator_INCLUDE_ONCE
34 
35 #include <vlCore/Interpolator.hpp>
36 
37 namespace vl
38 {
43  template<typename T>
44  class LinearInterpolator: public Object
45  {
47 
48  public:
50  {
51  VL_DEBUG_SET_OBJECT_NAME()
52  }
53 
54  LinearInterpolator(const std::vector<T>& path): mPath(path) {}
55 
57  T computePoint(float t) const
58  {
59  t = clamp(t,0.0f,1.0f);
60  if (t == 0.0f)
61  return mPath[0];
62  if (t == 1.0f)
63  return mPath.back();
64  int p0 = int((mPath.size()-1)*t);
65  int p1 = p0+1;
66  if (p1 > (int)mPath.size()-1)
67  p1 = (int)mPath.size()-1;
68  float tt = (mPath.size()-1)*t - p0/*int((mPath.size()-1)*t)*/;
69  return mPath[p0]*(1.0f-tt) + mPath[p1]*tt;
70  }
71 
75  void setPath(const std::vector<T>& path) { mPath = path; }
77  const std::vector<T>& path() const { return mPath; }
79  std::vector<T>& path() { return mPath; }
80 
81  protected:
82  std::vector<T> mPath;
83  std::vector<T> mLinearSpline;
84  };
85 
94 
97  {
99  public:
100  LinearInterpolatorFVec4(): mInterpolator( new LinearInterpolatorFVec4_T ) {}
101  LinearInterpolatorFVec4(const std::vector<fvec4>& path): mInterpolator( new LinearInterpolatorFVec4_T(path) ) {}
102  fvec4 computePoint(float t) const { return interpolator()->computePoint(t); }
103  LinearInterpolatorFVec4_T* interpolator() { return mInterpolator.get(); }
104  const LinearInterpolatorFVec4_T* interpolator() const { return mInterpolator.get(); }
105  void setInterpolator(LinearInterpolatorFVec4_T* interpolator) { mInterpolator = interpolator; }
106  protected:
108  };
111  {
113  public:
114  LinearInterpolatorFVec3(): mInterpolator( new LinearInterpolatorFVec3_T ) {}
115  LinearInterpolatorFVec3(const std::vector<fvec3>& path): mInterpolator( new LinearInterpolatorFVec3_T(path) ) {}
116  fvec3 computePoint(float t) const { return interpolator()->computePoint(t); }
117  LinearInterpolatorFVec3_T* interpolator() { return mInterpolator.get(); }
118  const LinearInterpolatorFVec3_T* interpolator() const { return mInterpolator.get(); }
119  void setInterpolator(LinearInterpolatorFVec3_T* interpolator) { mInterpolator = interpolator; }
120  protected:
122  };
125  {
127  public:
128  LinearInterpolatorFVec2(): mInterpolator( new LinearInterpolatorFVec2_T ) {}
129  LinearInterpolatorFVec2(const std::vector<fvec2>& path): mInterpolator( new LinearInterpolatorFVec2_T(path) ) {}
130  fvec2 computePoint(float t) const { return interpolator()->computePoint(t); }
131  LinearInterpolatorFVec2_T* interpolator() { return mInterpolator.get(); }
132  const LinearInterpolatorFVec2_T* interpolator() const { return mInterpolator.get(); }
133  void setInterpolator(LinearInterpolatorFVec2_T* interpolator) { mInterpolator = interpolator; }
134  protected:
136  };
139  {
141  public:
142  LinearInterpolatorFloat(): mInterpolator( new LinearInterpolatorFloat_T ) {}
143  LinearInterpolatorFloat(const std::vector<float>& path): mInterpolator( new LinearInterpolatorFloat_T(path) ) {}
144  float computePoint(float t) const { return interpolator()->computePoint(t); }
145  LinearInterpolatorFloat_T* interpolator() { return mInterpolator.get(); }
146  const LinearInterpolatorFloat_T* interpolator() const { return mInterpolator.get(); }
147  void setInterpolator(LinearInterpolatorFloat_T* interpolator) { mInterpolator = interpolator; }
148  protected:
150  };
153  {
155  public:
156  LinearInterpolatorDVec4(): mInterpolator( new LinearInterpolatorDVec4_T ) {}
157  LinearInterpolatorDVec4(const std::vector<dvec4>& path): mInterpolator( new LinearInterpolatorDVec4_T(path) ) {}
158  dvec4 computePoint(float t) const { return interpolator()->computePoint(t); }
159  LinearInterpolatorDVec4_T* interpolator() { return mInterpolator.get(); }
160  const LinearInterpolatorDVec4_T* interpolator() const { return mInterpolator.get(); }
161  void setInterpolator(LinearInterpolatorDVec4_T* interpolator) { mInterpolator = interpolator; }
162  protected:
164  };
167  {
169  public:
170  LinearInterpolatorDVec3(): mInterpolator( new LinearInterpolatorDVec3_T ) {}
171  LinearInterpolatorDVec3(const std::vector<dvec3>& path): mInterpolator( new LinearInterpolatorDVec3_T(path) ) {}
172  dvec3 computePoint(float t) const { return interpolator()->computePoint(t); }
173  LinearInterpolatorDVec3_T* interpolator() { return mInterpolator.get(); }
174  const LinearInterpolatorDVec3_T* interpolator() const { return mInterpolator.get(); }
175  void setInterpolator(LinearInterpolatorDVec3_T* interpolator) { mInterpolator = interpolator; }
176  protected:
178  };
181  {
183  public:
184  LinearInterpolatorDVec2(): mInterpolator( new LinearInterpolatorDVec2_T ) {}
185  LinearInterpolatorDVec2(const std::vector<dvec2>& path): mInterpolator( new LinearInterpolatorDVec2_T(path) ) {}
186  dvec2 computePoint(float t) const { return interpolator()->computePoint(t); }
187  LinearInterpolatorDVec2_T* interpolator() { return mInterpolator.get(); }
188  const LinearInterpolatorDVec2_T* interpolator() const { return mInterpolator.get(); }
189  void setInterpolator(LinearInterpolatorDVec2_T* interpolator) { mInterpolator = interpolator; }
190  protected:
192  };
195  {
197  public:
198  LinearInterpolatorDouble(): mInterpolator( new LinearInterpolatorDouble_T ) {}
199  LinearInterpolatorDouble(const std::vector<double>& path): mInterpolator( new LinearInterpolatorDouble_T(path) ) {}
200  double computePoint(float t) const { return interpolator()->computePoint(t); }
201  LinearInterpolatorDouble_T* interpolator() { return mInterpolator.get(); }
202  const LinearInterpolatorDouble_T* interpolator() const { return mInterpolator.get(); }
203  void setInterpolator(LinearInterpolatorDouble_T* interpolator) { mInterpolator = interpolator; }
204  protected:
206  };
207 }
208 
209 #endif
LinearInterpolatorDVec3(const std::vector< dvec3 > &path)
LinearInterpolatorFVec4_T * interpolator()
Interpolates dvec3 values using a LinearInterpolator.
Interpolates fvec2 values using a LinearInterpolator.
ref< LinearInterpolatorFVec4_T > mInterpolator
Abstract class that interpolates vl::fvec2 values.
float clamp(float x, float minval, float maxval)
Definition: Vector2.hpp:315
ref< LinearInterpolatorDVec2_T > mInterpolator
LinearInterpolator< float > LinearInterpolatorFloat_T
LinearInterpolatorDVec2(const std::vector< dvec2 > &path)
const LinearInterpolatorDouble_T * interpolator() const
const LinearInterpolatorDVec4_T * interpolator() const
void setInterpolator(LinearInterpolatorFloat_T *interpolator)
LinearInterpolator< fvec3 > LinearInterpolatorFVec3_T
ref< LinearInterpolatorFloat_T > mInterpolator
Abstract class that interpolates vl::dvec4 values.
Abstract class that interpolates vl::dvec2 values.
LinearInterpolatorDouble(const std::vector< double > &path)
const LinearInterpolatorFloat_T * interpolator() const
LinearInterpolator< dvec2 > LinearInterpolatorDVec2_T
Abstract class that interpolates double values.
Abstract class that interpolates float values.
ref< LinearInterpolatorFVec2_T > mInterpolator
void setPath(const std::vector< T > &path)
The control points defining the Catmull-Rom spline.
Interpolates float values using a LinearInterpolator.
fvec3 computePoint(float t) const
Samples the interpolator at the given point.
Interpolates dvec4 values using a LinearInterpolator.
std::vector< T > mLinearSpline
dvec2 computePoint(float t) const
Samples the interpolator at the given point.
dvec3 computePoint(float t) const
Samples the interpolator at the given point.
LinearInterpolatorFVec3_T * interpolator()
LinearInterpolator(const std::vector< T > &path)
LinearInterpolator< dvec3 > LinearInterpolatorDVec3_T
LinearInterpolatorDVec4_T * interpolator()
The LinearInterpolator class is a template class that implements linear interpolation.
LinearInterpolatorFloat(const std::vector< float > &path)
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
const std::vector< T > & path() const
The control points defining the Catmull-Rom spline.
std::vector< T > & path()
The control points defining the Catmull-Rom spline.
T computePoint(float t) const
Samples the path at the given point. The t parameter must be in the range 0.0 ... 1...
Visualization Library main namespace.
LinearInterpolatorFVec3(const std::vector< fvec3 > &path)
double computePoint(float t) const
Samples the interpolator at the given point.
const LinearInterpolatorFVec4_T * interpolator() const
LinearInterpolatorDouble_T * interpolator()
ref< LinearInterpolatorDVec4_T > mInterpolator
Interpolates fvec3 values using a LinearInterpolator.
void setInterpolator(LinearInterpolatorFVec3_T *interpolator)
void setInterpolator(LinearInterpolatorFVec4_T *interpolator)
Interpolates dvec2 values using a LinearInterpolator.
The base class for all the reference counted objects.
Definition: Object.hpp:158
LinearInterpolator< fvec2 > LinearInterpolatorFVec2_T
LinearInterpolator< fvec4 > LinearInterpolatorFVec4_T
LinearInterpolator< dvec4 > LinearInterpolatorDVec4_T
const LinearInterpolatorDVec3_T * interpolator() const
fvec2 computePoint(float t) const
Samples the interpolator at the given point.
void setInterpolator(LinearInterpolatorFVec2_T *interpolator)
dvec4 computePoint(float t) const
Samples the interpolator at the given point.
ref< LinearInterpolatorDouble_T > mInterpolator
const LinearInterpolatorDVec2_T * interpolator() const
LinearInterpolatorFloat_T * interpolator()
fvec4 computePoint(float t) const
Samples the interpolator at the given point.
Interpolates fvec4 values using a LinearInterpolator.
void setInterpolator(LinearInterpolatorDVec4_T *interpolator)
LinearInterpolatorFVec2_T * interpolator()
ref< LinearInterpolatorFVec3_T > mInterpolator
Interpolates double values using a LinearInterpolator.
LinearInterpolatorDVec2_T * interpolator()
Abstract class that interpolates vl::fvec3 values.
void setInterpolator(LinearInterpolatorDVec3_T *interpolator)
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
ref< LinearInterpolatorDVec3_T > mInterpolator
const LinearInterpolatorFVec2_T * interpolator() const
LinearInterpolatorFVec4(const std::vector< fvec4 > &path)
LinearInterpolator< double > LinearInterpolatorDouble_T
Abstract class that interpolates vl::dvec3 values.
float computePoint(float t) const
Samples the interpolator at the given point.
LinearInterpolatorFVec2(const std::vector< fvec2 > &path)
const LinearInterpolatorFVec3_T * interpolator() const
void setInterpolator(LinearInterpolatorDouble_T *interpolator)
LinearInterpolatorDVec3_T * interpolator()
void setInterpolator(LinearInterpolatorDVec2_T *interpolator)
LinearInterpolatorDVec4(const std::vector< dvec4 > &path)
Abstract class that interpolates vl::fvec4 values.