Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Interpolator.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef _BIAS_INTERPOLATOR_
27 #define _BIAS_INTERPOLATOR_
28 
29 #include <bias_config.h>
30 
31 #include <math.h>
32 
33 #include <Base/Common/BIASpragmaStart.hh>
34 
35 #include <vector>
36 #include "Base/Debug/Debug.hh"
37 #include "Base/Math/Matrix.hh"
38 #include "Base/Math/Vector.hh"
39 #include "Base/Math/Vector2.hh"
40 #include "Base/Math/Vector3.hh"
41 #include "Base/Image/Image.hh"
42 
43 
44 namespace BIAS {
45  /** @class Interpolator
46  @ingroup g_mathalgo
47  @brief this class interpolates a function y=f(t)
48  between given control points (the y-values)
49 
50  this class interpolates a function y=f(t)
51  with different algorithms between given control points (the y-values),
52  which can be one, two or three dimensional.
53  Interpolation means, the curve reaches all control points.
54 
55  usage:
56  -# SetControlPoints()
57 
58  -# optional: SetKnotPoints(), if not set the t values range form 0 to 1
59  such that the controlpoints are uniformly spaced
60 
61  -# call a Interpolation function, like Spline() or Bezier3()
62 
63  see also 'Computer Graphics & Geometric Modeling' from David Salomon
64  @author Daniel Grest, Juli 2002
65  */
66 #define D_INTERPOL_SPLINE3 1<<0
67 #define D_INTERPOL_SPLINE2 1<<1
68 #define D_INTERPOL_COMMON 1<<2
69 #define D_INTERPOL_BEZIER 1<<3
70 
71  class BIASMathAlgo_EXPORT Interpolator : public Debug
72  {
73 
74  public:
75  Interpolator();
77  { operator=(ip); }
78  ~Interpolator();
79 
80  /** resets everything.
81  clears all lists and calls constructor
82  */
83  void Clear();
84 
85  /** set the control points, which control the interpolating curve
86  */
87  void SetControlPoints(const std::vector<double> &cPnt1){
88  listPntDim1_=cPnt1;};
89  void SetControlPoints(const std::vector<BIAS::Vector2<double> > &cPnt);
90  void SetControlPoints(const std::vector<BIAS::Vector3<double> > &cPnt);
91  void GetControlPoints(std::vector<double> &cPnt) const {
92  cPnt=listPntDim1_;};
93  void GetControlPoints(std::vector<BIAS::Vector2<double> > &cPnt) const;
94  void GetControlPoints(std::vector<BIAS::Vector3<double> > &cPnt) const;
95 
96  /** set the additional knot points,
97  if you want nonuniform interpolation.
98  Otherwise they will be uniformly spaced.
99  The values in kPnt must be increasing,
100  such that kPnt[i]<kPnt[i+1]
101  */
102  void SetKnotPoints(const std::vector<double> &kPnt);
103  void GetKnotPoints(std::vector<double> &kPnt) const{
104  kPnt=KPts_;};
105 
106 
107  inline void SetStartTangent(double startTangent){
108  startTangentX_=startTangent;
109  };
110  inline void SetStartTangent(BIAS::Vector2<double> startTangent){
111  startTangentX_=startTangent[0];startTangentY_=startTangent[1];
112  };
113  inline void SetStartTangent(BIAS::Vector3<double> startTangent){
114  startTangentX_=startTangent[0]; startTangentY_=startTangent[1];
115  startTangentZ_=startTangent[2];
116  };
117  inline void ClearStartTangent(){
118  startTangentX_=startTangentY_=startTangentZ_=-10000;
119  };
120 
121  inline void SetEndTangent(double endTangent){endTangentX_=endTangent;};
122  inline void SetEndTangent(BIAS::Vector2<double> endTangent){
123  endTangentX_=endTangent[0];endTangentY_=endTangent[1];};
124  inline void SetEndTangent(BIAS::Vector3<double> endTangent){
125  endTangentX_=endTangent[0]; endTangentY_=endTangent[1];
126  endTangentZ_=endTangent[2];
127  };
128  inline void ClearEndTangent(){
129  endTangentX_=endTangentY_=endTangentZ_=-10000;
130  };
131 
132  /** for hermite splines, a tangent can be given for each control point.
133  */
134  void SetTangentsHermite(const std::vector<double> &cTan1){
135  listTangDim1_=cTan1;};
136 
137  /** call this for restart at t= first knot point
138  initiates recalculation of all polynom coefficients
139  at first call of Spline()
140  */
141  void InitSpline();
142 
143 
144  /** these functions do the Spline interpolation
145  which reaches each control point.
146  k is the degree of the interpolation polynom.
147  implemented for k=1,2,3,4 (linear, quadratic splines,
148  cubic splines, akima cubic splines)
149  If no start or end tangents are set, the end condition is
150  relaxed for cubic and akima splines.
151  for k=5 and a 1D function hermite splines are implemented (todo for vector2/3).
152  */
153  int Spline(double &res, double t, unsigned int k=3);
154  int Spline(BIAS::Vector2<double> &res, double t, unsigned int k=3);
155  int Spline(BIAS::Vector3<double> &res, double t, unsigned int k=3);
156 
157 
158  /** here the calculation of all coefficients for the bezier interpolation
159  is done and the nmatrix is set
160  is called by Bezier3() if not called manually
161  @author Ingo Schiller
162  @param dimension of Control Points
163  */
164  void InitBezier(int dim_of_CP);
165 
166 
167  /** these functions do the bezier interpolation as described
168  in David Salomon 4.14.19 which reaches each control point
169  /////////////////////////////////////////////
170  IMPORTANT NOTICE:
171  if the knotpoints are not set, the value of t must be between 0 and 1!
172  if t > 1 the value of the last controlpoint is returned!
173  /////////////////////////////////////////////
174  calculates P(t)= tvector(t)*nmatrix*cpoints
175  @author Ingo Schiller
176  @param reference to the result(return value)
177  @param time t of which value is to be calculated
178  */
179 
180  int Bezier3(double &res, double t);
181  int Bezier3(BIAS::Vector2<double> &res, double t);
182  int Bezier3(BIAS::Vector3<double> &res, double t);
183 
184  void DrawBezierCoords2(Image<unsigned char> &img);
185 
186  /**
187  @brief Prepare Look-up-Table in range [min,max] with N samples
188 
189  Prepare a Look-up-Table for spline interpolation in the range
190  [min,max] with N samples. Use SplineFromLuT() afterwards;
191  @author evers
192  @date 2005-04-15
193  */
194  int PrepareLuTSpline(double min, double max, unsigned int N,
195  unsigned int k=3);
196 
197  /**
198  @brief Get spline interpolation using Look-up-Table.
199  return code is negativ if argument is out of bound os LuT
200  @author evers
201  @date 2005-04-15
202  */
203  int SplineFromLuT(double &res, double t) const;
204 
205  /**
206  * Compare LuT data structures if matching perfectly.
207  * Used for ProjectionParametersSpherical compare.
208  * @author bartczak
209  * @date 06/2006
210  */
211  const bool DoLuTSplinesDiffer(const Interpolator& I) const;
212 
213  protected:
214  void InitSpline(const unsigned int k, const unsigned int dim);
215  void InitLinear(std::vector<BIAS::Vector<double> > &listP,
216  const std::vector<double> &listPnt);
217  // precalculates all coefficients for the segments
218  // for polynoms of degree 3
219  void InitSpline3(std::vector<BIAS::Vector<double> > &listP,
220  const std::vector<double>& listPnt,
221  const double startTangent=0, const double endTangent=0);
222 
223  // precalculates all coefficients for the segments
224  // for polynoms of degree 2
225  void InitSpline2(std::vector<BIAS::Vector<double> > &listP,
226  const std::vector<double>& listPnt,
227  double startTangent=0);
228 
229  // precalculates all coefficients for an akima spline
230  // of degree 3
231  void InitSplineAkima(std::vector<BIAS::Vector<double> > &listP,
232  const std::vector<double>& listPnt,
233  const double startTangent,const double endTangent);
234 
235  void InitSplineHermite(std::vector<double> &tangents,
236  const std::vector<double>& listPnt,
237  const double startTangent, const double endTangent);
238 
239  // validates important conditions
240  bool validate(double &t, bool debug=false);
241 
242  //subroutine for akima spline
243  void fakima(BIAS::Vector<double> &a,
245  int from, int to, int intervall);
246 
247  std::vector<double> KPts_; // knot values for control points
248  std::vector<double> T_; // knot value intervals for control points
249  //double tOffset_; // T_[0] has to be 0, for coefficient calculation
250 
251  std::vector<double> listPntDim1_; // control points dimension 1
252  std::vector<double> listPntDim2_; // control points dimension 2
253  std::vector<double> listPntDim3_; // control points dimension 3
254 
255  std::vector<double> listTangDim1_; // hermite tangents dimension 1;
256 
257  // list of polynom coefficients for dimension 1,2,3
258  std::vector<BIAS::Vector<double> > listPolynoms1_;
259  std::vector<BIAS::Vector<double> > listPolynoms2_;
260  std::vector<BIAS::Vector<double> > listPolynoms3_;
261 
262  // lists of bezier coefficients for all dimensions
263  std::vector<double> bXK_1_;
264  std::vector<double> bXK_2_;
265  std::vector<double> bXK_3_;
266 
267  std::vector<double> bYK_1_;
268  std::vector<double> bYK_2_;
269  std::vector<double> bYK_3_;
270  //nmatrix for the calcutaion of the bezier interpolation
272 
276  double endTangentX_;
277  double endTangentY_;
278  double endTangentZ_;
279 
280  int actIndex_; // the index of the actual intervall for t
281  // the degree of the polynom for which was initialized
282  unsigned int initializedPoly_;
283  //indicates if InitBezier has been called
285 
286  std::vector<double> SplineLuT1_;
287  double LuTMin1_,LuTMax1_, LuTSampleDist1_;
288 
289  };
290 
291 }
292 
293 #include <Base/Common/BIASpragmaEnd.hh>
294 
295 #endif
296 
std::vector< double > listPntDim3_
void SetTangentsHermite(const std::vector< double > &cTan1)
for hermite splines, a tangent can be given for each control point.
std::vector< double > SplineLuT1_
std::vector< BIAS::Vector< double > > listPolynoms2_
std::vector< BIAS::Vector< double > > listPolynoms1_
BIAS::Matrix< double > nmatrix_
void GetKnotPoints(std::vector< double > &kPnt) const
std::vector< double > listTangDim1_
std::vector< double > bXK_1_
void SetStartTangent(double startTangent)
void SetEndTangent(BIAS::Vector2< double > endTangent)
std::vector< BIAS::Vector< double > > listPolynoms3_
void SetEndTangent(BIAS::Vector3< double > endTangent)
this class interpolates a function y=f(t) between given control points (the y-values) ...
Definition: Interpolator.hh:71
std::vector< double > bXK_3_
std::vector< double > bYK_3_
void SetStartTangent(BIAS::Vector3< double > startTangent)
Interpolator(const Interpolator &ip)
Definition: Interpolator.hh:76
std::vector< double > bYK_1_
void SetControlPoints(const std::vector< double > &cPnt1)
set the control points, which control the interpolating curve
Definition: Interpolator.hh:87
std::vector< double > KPts_
unsigned int initializedPoly_
std::vector< double > bYK_2_
std::vector< double > bXK_2_
void GetControlPoints(std::vector< double > &cPnt) const
Definition: Interpolator.hh:91
std::vector< double > T_
void SetStartTangent(BIAS::Vector2< double > startTangent)
std::vector< double > listPntDim2_
std::vector< double > listPntDim1_
void SetEndTangent(double endTangent)