Interpolator.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/raster/Interpolator.h
22 
23  \brief It interpolates one pixel based on a selected algorithm.
24 */
25 
26 #ifndef __TERRALIB_RASTER_INTERNAL_INTERPOLATOR_H
27 #define __TERRALIB_RASTER_INTERNAL_INTERPOLATOR_H
28 
29 // TerraLib
30 #include "Raster.h"
31 #include "Enums.h"
32 
33 // STL
34 #include <complex>
35 #include <vector>
36 
37 // Boost
38 #include <boost/noncopyable.hpp>
39 
40 namespace te
41 {
42  namespace rst
43  {
44 // Forward declaration
45  class Raster;
46 
47  /*!
48  \class Interpolator
49 
50  \brief It interpolates one pixel based on a selected algorithm.
51  Methods currently available are Nearest Neighbor, Bilinear, and Bicubic.
52 
53  \ingroup rst
54  */
55  class TERASTEREXPORT Interpolator : public boost::noncopyable
56  {
57  public:
58 
59  /*!
60  \brief Allowed interpolation methods.
61  */
63 
64  /*!
65  \brief Constructor.
66 
67  \param r The raster where to resample.
68  \param m The method of interpolation to apply.
69  */
70  Interpolator(Raster const* r, int m);
71 
72  /*!
73  \brief Constructor.
74 
75  \param r The raster where to resample.
76  \param m The method of interpolation to apply.
77  \param noDataValues A vector with no-data values (will overwride the input raster no-data values) or an empty vectory if the input raster no-data values must be used.
78  */
79  Interpolator(Raster const* r, int m, const std::vector< std::complex<double> >& noDataValues);
80 
81  /*!
82  \brief Constructor.
83 
84  \param r The raster where to resample.
85  \param m The method of interpolation to apply.
86  \param noDataValues A vector with no-data values (will overwride the input raster no-data values) or an empty vectory if the input raster no-data values must be used.
87  \param windowRadius Interpolator windows radius around the target pixel (when applicable).
88  */
89  Interpolator(Raster const* r, int m, const std::vector< std::complex<double> >& noDataValues,
90  const unsigned int windowRadius );
91 
92  /*! \brief Destructor. */
93  virtual ~Interpolator();
94 
95  /*!
96  \brief Get the interpolated value at specific band.
97 
98  \param c The column position (double).
99  \param r The row position (double).
100  \param v The output value or the current input raster no-data value if the requested coordinates are outside the valid image bounds.
101  \param b The band to obtain the value.
102  \note The caller of this method must be aware that the returned value may be outside the original input rasters valid values range.
103  */
104  inline void getValue(const double& c, const double& r, std::complex<double>& v, const std::size_t& b)
105  {
106  assert(b < m_raster->getNumberOfBands());
107  (this->*(m_function))(c, r, v, b);
108  };
109 
110  /*!
111  \brief Get the interpolated value for all bands.
112 
113  \param c The column position (double).
114  \param r The row position (double).
115  \param v A vector of values, for all bands, or the current input raster no-data values if the requested coordinates are outside the valid image bounds..
116  \note The caller of this method must be aware that the returned values may be outside the original input rasters valid values range.
117  */
118  void getValues(const double& c, const double& r, std::vector<std::complex<double> >& values);
119 
120  /*!
121  \brief Create a clone copy of this instance.
122 
123  \return A clone copy of this instance (the caller of this method must take the ownership of the returned object).
124  */
126 
127  /*!
128  \brief Get the interpolator windows radius around the target pixel (when applicable).
129 
130  \return Get the interpolator windows radius around the target pixel (when applicable).
131  */
132  unsigned int getWindowRadius() const;
133 
134  static void getInterpMethodsInfo(
135  std::vector< std::pair< te::rst::InterpolationMethod, std::string > >& info );
136 
137  protected:
138 
139  /*!
140  \brief Type definition for the interpolation function.
141 
142  \param c The column position (double).
143  \param r The row position (double).
144  \param v The output value.
145  \param b The band to obtain the value.
146  */
147  typedef void (Interpolator::*InterpolationFunction)(const double& c, const double& r, std::complex<double>& v, const std::size_t& b);
148 
149  /*!
150  \brief Nearest neighbor interpolation method.
151 
152  \param c The column position (double).
153  \param r The row position (double).
154  \param v The output value.
155  \param b The band to obtain the value.
156  */
157  void nearestNeighborGetValue(const double& c, const double& r, std::complex<double>& v, const std::size_t& b);
158 
159  /*!
160  \brief Bilinear interpolation method.
161 
162  \param c The column position (double).
163  \param r The row position (double).
164  \param v The output value.
165  \param b The band to obtain the value.
166  */
167  void bilinearGetValue(const double& c, const double& r, std::complex<double>& v, const std::size_t& b);
168 
169  /*!
170  \brief Bicubic interpolation method.
171 
172  \param c The column position (double).
173  \param r The row position (double).
174  \param v The output value.
175  \param b The band to obtain the value.
176  */
177  void bicubicGetValue(const double& c, const double& r, std::complex<double>& v, const std::size_t& b);
178 
179  /*!
180  \brief Mean interpolation method.
181 
182  \param c The column position (double).
183  \param r The row position (double).
184  \param v The output value.
185  \param b The band to obtain the value.
186  */
187  void meanGetValue(const double& c, const double& r, std::complex<double>& v, const std::size_t& b);
188 
189  /*!
190  \brief Min interpolation method.
191 
192  \param c The column position (double).
193  \param r The row position (double).
194  \param v The output value.
195  \param b The band to obtain the value.
196  */
197  void minGetValue(const double& c, const double& r, std::complex<double>& v, const std::size_t& b);
198 
199  /*!
200  \brief Max interpolation method.
201 
202  \param c The column position (double).
203  \param r The row position (double).
204  \param v The output value.
205  \param b The band to obtain the value.
206  */
207  void maxGetValue(const double& c, const double& r, std::complex<double>& v, const std::size_t& b);
208 
209  /*!
210  \brief Mode interpolation method.
211 
212  \param c The column position (double).
213  \param r The row position (double).
214  \param v The output value.
215  \param b The band to obtain the value.
216  */
217  void modeGetValue(const double& c, const double& r, std::complex<double>& v, const std::size_t& b);
218 
219  /*!
220  \brief Median interpolation method.
221 
222  \param c The column position (double).
223  \param r The row position (double).
224  \param v The output value.
225  \param b The band to obtain the value.
226  */
227  void medianGetValue(const double& c, const double& r, std::complex<double>& v, const std::size_t& b);
228 
229  /*!
230  \brief Initialize this instance..
231 
232  \param r The raster where to resample.
233  \param method The method of interpolation to apply.
234  \param noDataValues A vector with no-data values (will overwride the input raster no-data values) or an empty vectory if the input raster no-data values must be used.
235  \param windowRadius Interpolator windows radius around the target pixel (when applicable).
236  \return true if OK, false on errors.
237  */
238  bool initialize(Raster const * const rasterPointer, int method,
239  const std::vector< std::complex<double> >& noDataValues,
240  const unsigned int windowRadius );
241 
242  protected:
243 
244  Raster const* m_raster; //!< My input raster.
245  int m_method; //!< The interpolation method.
246  InterpolationFunction m_function; //!< The current interpolation function pointer.
247  std::vector< std::complex<double> > m_noDataValues; //!< Raster no-data values (for each band);
248  unsigned int m_windowRadius; //!< Interpolator windows radius around the target pixel (when applicable).
249  unsigned int m_rasterNRows; //!< Last raster row index.
250  unsigned int m_rasterNCols; //!< Last raster column index.
251 
252  // nearest Neighbor interpolation variables
253 
254  unsigned int m_nnCR;
255  unsigned int m_nnRR;
256  double m_nnLastRow; //!< Last row available for nearest Neighbor interpolation.
257  double m_nnLastCol; //!< Last column available for nearest Neighbor interpolation.
258 
259 
260  /* Bilinear interapolation variables. */
261  double m_bilRowMin; //!< Minimum row for bilinear interpolation.
262  double m_bilRowMax; //!< Maximum row for bilinear interpolation.
263  double m_bilColMin; //!< Minimum column for bilinear interpolation.
264  double m_bilColMax; //!< Maximum column for bilinear interpolation.
265  double m_bilRowDifMin; //!< Minimum difference between rows (min/max).
266  double m_bilRowDifMax; //!< Maximum difference between rows (min/max).
267  double m_bilColDifMin; //!< Minimum difference between columns (min/max).
268  double m_bilColDifMax; //!< Maximum difference between columns (min/max).
269  double m_bilDistances[4]; //!< Bilinear distances.
270  double m_bilWeights[4]; //!< Bilinear weights;
271  std::vector<std::complex<double> > m_bilValues; //!< Bilinear values;
272  double m_bilLastRow; //!< Last row available for bilinear interpolation.
273  double m_bilLastCol; //!< Last column available for bilinear interpolation.
274 
275  /* Bicubic interapolation variables. */
276  unsigned m_bicGridRow;
277  unsigned m_bicGridCol;
278  unsigned m_bicBufRow;
279  unsigned m_bicBufCol;
282  double m_bicBbufferReal[4][4];
283  double m_bicBbufferImag[4][4];
284  double m_bicOffsetX;
285  double m_bicOffsetY;
286  double m_bicKernel;
287  double m_bicHWeights[4];
288  double m_bicVWeights[4];
289  double m_bicHSum;
290  double m_bicVSum;
293  double m_bicRowsValuesReal[4];
294  double m_bicRowsValuesImag[4];
295  double m_bicRowBound; //!< Last row available for bicubic interpolation.
296  double m_bicColBound; //!< Last column available for bicubic interpolation.
297 
298  // window based interpolators variables
309  std::complex<double> m_wInterpAuxCValue;
310  std::vector< std::complex<double> > m_wInterpAuxCValues;
311  std::vector< unsigned int > m_wInterpAuxCValuesFreq;
312  };
313 
314  } // end namespace rst
315 } // end namespace te
316 
317 #endif // __TERRALIB_RASTER_INTERNAL_INTERPOLATOR_H
It interpolates one pixel based on a selected algorithm. Methods currently available are Nearest Neig...
Definition: Interpolator.h:56
std::vector< std::complex< double > > m_bilValues
Bilinear values;.
Definition: Interpolator.h:271
te::rst::Interpolator * clone() const
Create a clone copy of this instance.
double m_bilLastCol
Last column available for bilinear interpolation.
Definition: Interpolator.h:273
std::complex< double > m_wInterpAuxCValue
Definition: Interpolator.h:309
void maxGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Max interpolation method.
void minGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Min interpolation method.
double m_nnLastCol
Last column available for nearest Neighbor interpolation.
Definition: Interpolator.h:257
long int m_wInterpWindowFirstCol
Definition: Interpolator.h:300
double m_bilColDifMax
Maximum difference between columns (min/max).
Definition: Interpolator.h:268
void getValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Get the interpolated value at specific band.
Definition: Interpolator.h:104
unsigned int m_wInterpWindowValidElementsCount
Definition: Interpolator.h:305
double m_bilColMax
Maximum column for bilinear interpolation.
Definition: Interpolator.h:264
long int m_wInterpWindowFirstRow
Definition: Interpolator.h:299
int m_method
The interpolation method.
Definition: Interpolator.h:245
void modeGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Mode interpolation method.
std::vector< std::complex< double > > m_noDataValues
Raster no-data values (for each band);.
Definition: Interpolator.h:247
Interpolator(Raster const *r, int m, const std::vector< std::complex< double > > &noDataValues, const unsigned int windowRadius)
Constructor.
unsigned int m_rasterNRows
Last raster row index.
Definition: Interpolator.h:249
void medianGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Median interpolation method.
double m_nnLastRow
Last row available for nearest Neighbor interpolation.
Definition: Interpolator.h:256
InterpolationFunction m_function
The current interpolation function pointer.
Definition: Interpolator.h:246
Interpolator(Raster const *r, int m)
Constructor.
virtual ~Interpolator()
Destructor.
double m_bicRowBound
Last row available for bicubic interpolation.
Definition: Interpolator.h:295
void meanGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Mean interpolation method.
double m_bilRowDifMin
Minimum difference between rows (min/max).
Definition: Interpolator.h:265
void nearestNeighborGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Nearest neighbor interpolation method.
void getValues(const double &c, const double &r, std::vector< std::complex< double > > &values)
Get the interpolated value for all bands.
static void getInterpMethodsInfo(std::vector< std::pair< te::rst::InterpolationMethod, std::string > > &info)
Raster const * m_raster
My input raster.
Definition: Interpolator.h:244
unsigned int m_rasterNCols
Last raster column index.
Definition: Interpolator.h:250
unsigned int getWindowRadius() const
Get the interpolator windows radius around the target pixel (when applicable).
double m_bilRowMin
Minimum row for bilinear interpolation.
Definition: Interpolator.h:261
std::vector< std::complex< double > > m_wInterpAuxCValues
Definition: Interpolator.h:310
unsigned int m_windowRadius
Interpolator windows radius around the target pixel (when applicable).
Definition: Interpolator.h:248
InterpolationMethod Method
Allowed interpolation methods.
Definition: Interpolator.h:62
double m_bilColDifMin
Minimum difference between columns (min/max).
Definition: Interpolator.h:267
void bicubicGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Bicubic interpolation method.
bool initialize(Raster const *const rasterPointer, int method, const std::vector< std::complex< double > > &noDataValues, const unsigned int windowRadius)
Initialize this instance..
std::vector< unsigned int > m_wInterpAuxCValuesFreq
Definition: Interpolator.h:311
long int m_wInterpWindowRowsBound
Definition: Interpolator.h:301
double m_bilRowDifMax
Maximum difference between rows (min/max).
Definition: Interpolator.h:266
double m_bicColBound
Last column available for bicubic interpolation.
Definition: Interpolator.h:296
void bilinearGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Bilinear interpolation method.
double m_bilLastRow
Last row available for bilinear interpolation.
Definition: Interpolator.h:272
long int m_wInterpWindowColsBound
Definition: Interpolator.h:302
double m_bilColMin
Minimum column for bilinear interpolation.
Definition: Interpolator.h:263
std::size_t m_wInterpAuxCValuesSize
Definition: Interpolator.h:307
double m_bilRowMax
Maximum row for bilinear interpolation.
Definition: Interpolator.h:262
Interpolator(Raster const *r, int m, const std::vector< std::complex< double > > &noDataValues)
Constructor.
std::size_t m_wInterpAuxCValuesIdx
Definition: Interpolator.h:308
An abstract class for raster data strucutures.
Definition: Raster.h:72
InterpolationMethod
Allowed interpolation methods.
Definition: Enums.h:93
TerraLib.
Raster implementaton for TerraLib 4.x.
#define TERASTEREXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:63
Enumerations of XML module.