MIRA
LinearInterpolator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_LINEARINTERPOLATOR_H_
48 #define _MIRA_LINEARINTERPOLATOR_H_
49 
50 #include <assert.h>
51 
52 #include <limits>
53 
54 #include <math/IsApprox.h>
55 #include <math/Lerp.h>
56 #include <math/IntervalFilter.h>
57 
58 namespace mira {
59 
61 
71 {
72 public:
73  bool canExtrapolate() const { return true; }
74  int samples() const { return 2; }
75  int samplesBefore() const { return 1; }
76  int samplesAfter() const { return 1; }
77 
78  template <typename Tx, typename Ty, typename ContainerTx, typename ContainerTy>
79  Ty apply(const ContainerTx& x, const ContainerTy& y, const Tx& xx)
80  {
81  assert(x.size()==y.size());
82  assert(x.size()==2);
83 
84  double dx = (double)x.back() - (double)x.front();
85  // check if dx is close to 0, then return the first
86  if(isApprox(dx,0.0, std::numeric_limits<double>::epsilon()))
87  return y.front();
88 
89  return lerp(y.front(), y.back(), ((double)xx-(double)x.front())/dx);
90  }
91 };
92 
94 
104 {
105 public:
106  bool canExtrapolate() const { return false; }
107  int samples() const { return 2; }
108  int samplesBefore() const { return 1; }
109  int samplesAfter() const { return 1; }
110 
111  template <typename Tx, typename Ty, typename ContainerTx, typename ContainerTy>
112  Ty apply(const ContainerTx& x, const ContainerTy& y, const Tx& xx)
113  {
114  assert(x.size()==y.size());
115  assert(x.size()==2);
116 
117  double dx = (double)x.back() - (double)x.front();
118  // check if dx is close to 0, then return the first
119  if(isApprox(dx,0.0, std::numeric_limits<double>::epsilon()))
120  return y.front();
121 
122  return lerp(y.front(), y.back(), ((double)xx-(double)x.front())/dx);
123  }
124 };
125 
127 
138 {
139 public:
140 
141  bool canExtrapolate() const { return true; }
142  int samples() const { return 2; }
143  int samplesBefore() const { return 1; }
144  int samplesAfter() const { return 1; }
145 
146  template <typename Tx, typename Ty, typename ContainerTx, typename ContainerTy>
147  Ty apply(const ContainerTx& x, const ContainerTy& y, const Tx& xx)
148  {
149  assert(x.size()==y.size());
150  assert(x.size()==2);
151 
152  // handle extrapolation case
153  if (xx < x.front())
154  return y.front();
155  if (xx > x.back())
156  return y.back();
157 
158  double dx = (double)x.back() - (double)x.front();
159  // check if dx is close to 0, then return the first
160  if(isApprox(dx,0.0, std::numeric_limits<double>::epsilon()))
161  return y.front();
162 
163  return lerp(y.front(), y.back(), ((double)xx-(double)x.front())/dx);
164  }
165 };
166 
168 
182 {
183 public:
185  mLimit(limit) {}
186 
187  bool canExtrapolate() const { return true; }
188  int samples() const { return 2; }
189  int samplesBefore() const { return 1; }
190  int samplesAfter() const { return 1; }
191 
192  template <typename Tx, typename Ty, typename ContainerTx, typename ContainerTy>
193  Ty apply(const ContainerTx& x, const ContainerTy& y, const Tx& xx)
194  {
195  assert(x.size()==y.size());
196  assert(x.size()==2);
197 
198  double dx = (double)x.back() - (double)x.front();
199  // check if dx is close to 0, then return the first
200  if(isApprox(dx,0.0, std::numeric_limits<double>::epsilon()))
201  return y.front();
202 
203  // check extrapolation case
204  double alpha = ((double)xx-(double)x.front())/dx;
205  if (alpha < -mLimit || alpha > (1 + mLimit))
206  MIRA_THROW(XRuntime,
207  "LinearInterpolatorExtrapolationLimit: Lerp alpha " <<
208  "(" << alpha << ") out of specified limits " <<
209  "(" << -mLimit << ", " << 1 + mLimit << ")");
210 
211  return lerp(y.front(), y.back(), alpha);
212  }
213 
214 private:
215  double mLimit;
216 };
217 
219 
220 } // namespace
221 
222 #endif /* _MIRA_LINEARINTERPOLATOR_H_ */
bool canExtrapolate() const
Definition: LinearInterpolator.h:73
bool canExtrapolate() const
Definition: LinearInterpolator.h:141
LinearInterpolatorExtrapolationLimit(double limit=1.0)
Definition: LinearInterpolator.h:184
Ty apply(const ContainerTx &x, const ContainerTy &y, const Tx &xx)
Definition: LinearInterpolator.h:112
int samples() const
Definition: LinearInterpolator.h:74
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
int samplesBefore() const
Definition: LinearInterpolator.h:75
1D linear interpolator.
Definition: LinearInterpolator.h:103
int samplesAfter() const
Definition: LinearInterpolator.h:144
1D linear interpolator.
Definition: LinearInterpolator.h:181
T lerp(const T &a, const T &b, S alpha)
Linear interpolation of different types like scalars, angles and rotations, vectors, etc.
Definition: Lerp.h:76
int samples() const
Definition: LinearInterpolator.h:188
Functions for linear interpolation of different types like scalars, angles and rotations.
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
Contains helper to check if two values are approximately the same.
bool canExtrapolate() const
Definition: LinearInterpolator.h:106
int samplesBefore() const
Definition: LinearInterpolator.h:143
1D linear interpolator.
Definition: LinearInterpolator.h:70
Ty apply(const ContainerTx &x, const ContainerTy &y, const Tx &xx)
Definition: LinearInterpolator.h:193
Ty apply(const ContainerTx &x, const ContainerTy &y, const Tx &xx)
Definition: LinearInterpolator.h:79
Ty apply(const ContainerTx &x, const ContainerTy &y, const Tx &xx)
Definition: LinearInterpolator.h:147
int samplesBefore() const
Definition: LinearInterpolator.h:108
int samples() const
Definition: LinearInterpolator.h:142
int samplesAfter() const
Definition: LinearInterpolator.h:190
1D linear interpolator.
Definition: LinearInterpolator.h:137
int samples() const
Definition: LinearInterpolator.h:107
int samplesAfter() const
Definition: LinearInterpolator.h:76
bool canExtrapolate() const
Definition: LinearInterpolator.h:187
IntervalFilter base class/concept.
Concept and base class for all Interpolators and Filters.
Definition: IntervalFilter.h:61
int samplesBefore() const
Definition: LinearInterpolator.h:189
int samplesAfter() const
Definition: LinearInterpolator.h:109
bool isApprox(const T &a, const T &b, const U &tol)
Returns true, if the value a has approximately the same value as b.
Definition: IsApprox.h:61