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
te::rst::Interpolator::m_bicVSum
double m_bicVSum
Definition: Interpolator.h:290
te::rst::Interpolator::medianGetValue
void medianGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Median interpolation method.
Raster.h
Raster implementaton for TerraLib 4.x.
te::rst::Interpolator::m_bicRowAccumReal
double m_bicRowAccumReal
Definition: Interpolator.h:291
te::rst::Interpolator::getValue
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
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::rst::Interpolator::bilinearGetValue
void bilinearGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Bilinear interpolation method.
te::rst::Interpolator::minGetValue
void minGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Min interpolation method.
te::rst::Interpolator::m_wInterpAuxCValues
std::vector< std::complex< double > > m_wInterpAuxCValues
Definition: Interpolator.h:310
te::rst::Interpolator::nearestNeighborGetValue
void nearestNeighborGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Nearest neighbor interpolation method.
te::rst::Interpolator::clone
te::rst::Interpolator * clone() const
Create a clone copy of this instance.
te::rst::Interpolator::m_function
InterpolationFunction m_function
The current interpolation function pointer.
Definition: Interpolator.h:246
te::rst::Raster
An abstract class for raster data strucutures.
Definition: Raster.h:72
te::rst::Interpolator::m_bilLastCol
double m_bilLastCol
Last column available for bilinear interpolation.
Definition: Interpolator.h:273
te::rst::Interpolator::maxGetValue
void maxGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Max interpolation method.
te::rst::Interpolator::getInterpMethodsInfo
static void getInterpMethodsInfo(std::vector< std::pair< te::rst::InterpolationMethod, std::string > > &info)
te::rst::Interpolator::m_wInterpAuxCValue
std::complex< double > m_wInterpAuxCValue
Definition: Interpolator.h:309
te::rst::Interpolator::m_rasterNRows
unsigned int m_rasterNRows
Last raster row index.
Definition: Interpolator.h:249
te::rst::Interpolator::m_wInterpWindowCol
long int m_wInterpWindowCol
Definition: Interpolator.h:304
te::rst::Interpolator::modeGetValue
void modeGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Mode interpolation method.
te::rst::Interpolator::m_bicOffsetY
double m_bicOffsetY
Definition: Interpolator.h:285
te::rst::Interpolator::Interpolator
Interpolator(Raster const *r, int m, const std::vector< std::complex< double > > &noDataValues, const unsigned int windowRadius)
Constructor.
te::rst::Interpolator::m_bilColDifMin
double m_bilColDifMin
Minimum difference between columns (min/max).
Definition: Interpolator.h:267
te::rst::Interpolator::m_wInterpWindowRow
long int m_wInterpWindowRow
Definition: Interpolator.h:303
te::rst::Interpolator::m_bicGridRow
unsigned m_bicGridRow
Definition: Interpolator.h:276
te::rst::Interpolator::m_bicKernel
double m_bicKernel
Definition: Interpolator.h:286
te::rst::Interpolator::m_bilLastRow
double m_bilLastRow
Last row available for bilinear interpolation.
Definition: Interpolator.h:272
te::rst::Interpolator::Method
InterpolationMethod Method
Allowed interpolation methods.
Definition: Interpolator.h:62
te::rst::Interpolator::m_bicBufRow
unsigned m_bicBufRow
Definition: Interpolator.h:278
te::rst::Interpolator::m_bilColMin
double m_bilColMin
Minimum column for bilinear interpolation.
Definition: Interpolator.h:263
te::rst::Interpolator::m_windowRadius
unsigned int m_windowRadius
Interpolator windows radius around the target pixel (when applicable).
Definition: Interpolator.h:248
TERASTEREXPORT
#define TERASTEREXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:63
te::rst::Interpolator::m_bicBufCol
unsigned m_bicBufCol
Definition: Interpolator.h:279
te::rst::Interpolator::getWindowRadius
unsigned int getWindowRadius() const
Get the interpolator windows radius around the target pixel (when applicable).
te::rst::Interpolator::m_nnLastCol
double m_nnLastCol
Last column available for nearest Neighbor interpolation.
Definition: Interpolator.h:257
te::rst::Interpolator::m_bicColBound
double m_bicColBound
Last column available for bicubic interpolation.
Definition: Interpolator.h:296
Enums.h
Enumerations of XML module.
te::rst::InterpolationMethod
InterpolationMethod
Allowed interpolation methods.
Definition: Enums.h:93
te::rst::Interpolator::m_nnCR
unsigned int m_nnCR
Definition: Interpolator.h:254
te::rst::Interpolator::m_bicHSum
double m_bicHSum
Definition: Interpolator.h:289
te::rst::Interpolator::m_bilValues
std::vector< std::complex< double > > m_bilValues
Bilinear values;.
Definition: Interpolator.h:271
te::rst::Interpolator::m_bilRowMin
double m_bilRowMin
Minimum row for bilinear interpolation.
Definition: Interpolator.h:261
te::rst::Interpolator::m_wInterpAuxCValuesFreq
std::vector< unsigned int > m_wInterpAuxCValuesFreq
Definition: Interpolator.h:311
te::rst::Interpolator::m_bicReadRealValue
double m_bicReadRealValue
Definition: Interpolator.h:280
te::rst::Interpolator::m_wInterpWindowRowsBound
long int m_wInterpWindowRowsBound
Definition: Interpolator.h:301
te::rst::Interpolator::m_nnLastRow
double m_nnLastRow
Last row available for nearest Neighbor interpolation.
Definition: Interpolator.h:256
te::rst::Interpolator::m_wInterpAuxCValuesIdx
std::size_t m_wInterpAuxCValuesIdx
Definition: Interpolator.h:308
te::rst::Interpolator::~Interpolator
virtual ~Interpolator()
Destructor.
te::rst::Interpolator::Interpolator
Interpolator(Raster const *r, int m)
Constructor.
te::rst::Interpolator::m_wInterpValueFound
bool m_wInterpValueFound
Definition: Interpolator.h:306
te::rst::Interpolator::m_bilRowMax
double m_bilRowMax
Maximum row for bilinear interpolation.
Definition: Interpolator.h:262
te::rst::Interpolator::m_bicRowBound
double m_bicRowBound
Last row available for bicubic interpolation.
Definition: Interpolator.h:295
te::rst::Interpolator::Interpolator
Interpolator(Raster const *r, int m, const std::vector< std::complex< double > > &noDataValues)
Constructor.
te::rst::Interpolator::initialize
bool initialize(Raster const *const rasterPointer, int method, const std::vector< std::complex< double > > &noDataValues, const unsigned int windowRadius)
Initialize this instance..
te::rst::Interpolator::m_bicOffsetX
double m_bicOffsetX
Definition: Interpolator.h:284
te::rst::Interpolator
It interpolates one pixel based on a selected algorithm. Methods currently available are Nearest Neig...
Definition: Interpolator.h:56
te::rst::Interpolator::m_bilColDifMax
double m_bilColDifMax
Maximum difference between columns (min/max).
Definition: Interpolator.h:268
te::rst::Interpolator::m_bicGridCol
unsigned m_bicGridCol
Definition: Interpolator.h:277
te::rst::Interpolator::m_raster
Raster const * m_raster
My input raster.
Definition: Interpolator.h:244
te::rst::Interpolator::meanGetValue
void meanGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Mean interpolation method.
te::rst::Interpolator::m_bicRowAccumImag
double m_bicRowAccumImag
Definition: Interpolator.h:292
te::rst::Interpolator::m_bicReadImagValue
double m_bicReadImagValue
Definition: Interpolator.h:281
te::rst::Interpolator::m_rasterNCols
unsigned int m_rasterNCols
Last raster column index.
Definition: Interpolator.h:250
te::rst::Interpolator::m_bilColMax
double m_bilColMax
Maximum column for bilinear interpolation.
Definition: Interpolator.h:264
te::rst::Interpolator::m_noDataValues
std::vector< std::complex< double > > m_noDataValues
Raster no-data values (for each band);.
Definition: Interpolator.h:247
te::rst::Interpolator::m_wInterpWindowColsBound
long int m_wInterpWindowColsBound
Definition: Interpolator.h:302
te::rst::Interpolator::m_wInterpWindowFirstRow
long int m_wInterpWindowFirstRow
Definition: Interpolator.h:299
te::rst::Interpolator::bicubicGetValue
void bicubicGetValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Bicubic interpolation method.
te::rst::Interpolator::m_wInterpWindowFirstCol
long int m_wInterpWindowFirstCol
Definition: Interpolator.h:300
te::rst::Interpolator::m_wInterpAuxCValuesSize
std::size_t m_wInterpAuxCValuesSize
Definition: Interpolator.h:307
te::rst::Interpolator::m_nnRR
unsigned int m_nnRR
Definition: Interpolator.h:255
te::rst::Interpolator::m_bilRowDifMax
double m_bilRowDifMax
Maximum difference between rows (min/max).
Definition: Interpolator.h:266
te::rst::Interpolator::m_wInterpWindowValidElementsCount
unsigned int m_wInterpWindowValidElementsCount
Definition: Interpolator.h:305
te::rst::Interpolator::m_method
int m_method
The interpolation method.
Definition: Interpolator.h:245
te::rst::Interpolator::m_bilRowDifMin
double m_bilRowDifMin
Minimum difference between rows (min/max).
Definition: Interpolator.h:265
te::rst::Interpolator::getValues
void getValues(const double &c, const double &r, std::vector< std::complex< double > > &values)
Get the interpolated value for all bands.