ArithmeticOperations.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/rp/ArithmeticOperations.h
22  \brief Performs arithmetic operation over raster data.
23  */
24 
25 #ifndef __TERRALIB_RP_INTERNAL_ARITHMETICOPERATIONS_H
26 #define __TERRALIB_RP_INTERNAL_ARITHMETICOPERATIONS_H
27 
28 #include "Algorithm.h"
29 #include "../raster/Grid.h"
30 #include "../raster/Interpolator.h"
31 #include "../common/progress/TaskProgress.h"
32 
33 #include <map>
34 #include <memory>
35 #include <stack>
36 #include <string>
37 #include <vector>
38 #include <cmath>
39 
40 namespace te
41 {
42  namespace rst
43  {
44  class Raster;
45  }
46 
47  namespace rp
48  {
49  /*!
50  \class ArithmeticOperations
51 
52  \brief Performs arithmetic operation over raster data.
53 
54  \details The expression (arithmetic stringIt) must be formed by a combination of the following elements,
55  separated by spaces:
56 
57  Operators:
58  +(Sum of Rasters or a raster and a real number),
59  -(Subtraction of Rasters or a raster and a real number),
60  *(Multiplication of Rasters or a raster and a real number),
61  /(Division of Rasters or a raster and a real number)
62  ^(Exponential of a raster and a real number)
63  sqrt() (Square root of a raster)
64  sin() (Sine of a raster)
65  asin() (Arc Sine of a raster)
66  cos() (Cosine of a raster)
67  acos() (Arc Cosine of a raster)
68  log() (Common (base-10) logarithm of a raster)
69  tan() (Tangent of a raster)
70  atan() (Arc tangent of a raster)
71  ln() (Natural (base e) logarithm of a raster)
72 
73  Real Numbers (negative numbers must follow the form "-1.0")
74 
75  Raster bands: R0:1, R0:1, R1:0, .... (R0:1 is a reference to the first raster - with index 0 - from feeder, second band - with index 1).
76 
77  \ingroup rp_gen
78  */
80  {
81  public:
82 
83  /*!
84  \class InputParameters
85  \brief ArithmeticOperations input parameters
86  */
88  {
89  public:
90 
91  std::vector< te::rst::Raster* > m_inputRasters; //!< Input rasters vector.
92 
93  std::string m_arithmeticString; //!< Arithmetic string.
94 
95  bool m_normalize; //!< Output values normalization will be performed to fit the original input raster values range (default:true).
96 
97  bool m_enableProgress; //!< Enable/Disable the progress interface (default:false).
98 
99  te::rst::Interpolator::Method m_interpMethod; //!< The raster interpolator method (default:NearestNeighbor).
100 
101  InputParameters();
102 
104 
105  ~InputParameters();
106 
107  //overload
108  void reset() _NOEXCEPT_OP(false);
109 
110  //overload
111  const InputParameters& operator=( const InputParameters& params );
112 
113  //overload
114  AbstractParameters* clone() const;
115  };
116 
117  /*!
118  \class OutputParameters
119  \brief ArithmeticOperations output parameters
120  */
122  {
123  public:
124 
125  std::string m_rType; //!< Output raster data source type (as described in te::raster::RasterFactory ).
126 
127  std::map< std::string, std::string > m_rInfo; //!< The necessary information to create the output rasters (as described in te::raster::RasterFactory).
128 
129  std::unique_ptr< te::rst::Raster > m_outputRasterPtr; //!< The generated output registered raster.
130 
132 
134 
135  ~OutputParameters();
136 
137  //overload
138  void reset() _NOEXCEPT_OP(false);
139 
140  //overload
141  const OutputParameters& operator=( const OutputParameters& params );
142 
143  //overload
144  AbstractParameters* clone() const;
145  };
146 
148 
150 
151  //overload
152  bool execute( AlgorithmOutputParameters& outputParams ) _NOEXCEPT_OP(false);
153 
154  //overload
155  void reset() _NOEXCEPT_OP(false);
156 
157  //overload
158  bool initialize( const AlgorithmInputParameters& inputParams ) _NOEXCEPT_OP(false);
159 
160  bool isInitialized() const;
161 
162  protected:
163 
164  /*!
165  \class ExecStackElement
166  \brief Execution stack element.
167  */
169  {
170  public :
171 
172  bool m_isRaster; //!< true if this is a raster pointer element.
173 
174  bool m_isRealNumber; //!< true if this is a real number element.
175 
176  bool m_isBinaryOp; //!< true if this is a binary operator pointer element.
177 
178  bool m_isUnaryOp; //!< true if this is a unary operator pointer element.
179 
180  double m_realNumberValue; //!< Real number value.
181 
182  unsigned int m_rasterBand; //!< Raster band index.
183 
184  te::rst::Raster* m_rasterNPtr; //!< Raster pointer.
185 
186  std::string m_binaryOp; //!< Binary operator.
187 
188  std::string m_unaryOp; //!< Unary operator.
189 
190  mutable std::unique_ptr< te::rst::Raster > m_rasterHandler; //!< Raster handler.
191 
193  : m_isRaster(false), m_isRealNumber(false), m_isBinaryOp(false), m_isUnaryOp(false),
194  m_realNumberValue(0), m_rasterBand(0), m_rasterNPtr(0), m_binaryOp(""), m_unaryOp("")
195  {
196  operator=( rhs );
197  }
198 
200  : m_isRaster(false), m_isRealNumber(false), m_isBinaryOp(false), m_isUnaryOp(false),
201  m_realNumberValue(0), m_rasterBand(0), m_rasterNPtr(0), m_binaryOp(""), m_unaryOp("")
202  {}
203 
205  {
206  m_isRaster = rhs.m_isRaster;
207  m_isRealNumber = rhs.m_isRealNumber;
208  m_isBinaryOp = rhs.m_isBinaryOp;
209  m_isUnaryOp = rhs.m_isUnaryOp;
210  m_realNumberValue = rhs.m_realNumberValue;
211  m_rasterBand = rhs.m_rasterBand;
212  m_rasterNPtr = rhs.m_rasterNPtr;
213  m_binaryOp = rhs.m_binaryOp;
214  m_unaryOp = rhs.m_unaryOp;
215  m_rasterHandler.reset( rhs.m_rasterHandler.release() );
216 
217  return *this;
218  }
219 
221  };
222 
223  typedef std::stack< ExecStackElement > ExecStackT; //!< Execution stack type definition.
224 
225  /*!
226  \brief Type definition for a operation function pointer.
227  */
228  typedef void (ArithmeticOperations::*BinOpFuncPtrT)( const double& inputValue1, const double& inputValue2,
229  double& outputValue ) const;
230 
231  typedef void (ArithmeticOperations::*UnaryOpFuncPtrT)(const double& inputValue1, double& outputValue) const;
232 
234 
235  bool m_isInitialized; //!< Tells if this instance is initialized.
236 
237  /*!
238  \brief Addition binary operator function.
239  */
240  inline void additionBinOp( const double& inputValue1, const double& inputValue2,
241  double& outputValue ) const { outputValue = inputValue1 + inputValue2; }
242 
243  /*!
244  \brief Subtraction binary operator function.
245  */
246  inline void subtractionBinOp( const double& inputValue1, const double& inputValue2,
247  double& outputValue ) const { outputValue = inputValue1 - inputValue2; }
248 
249  /*!
250  \brief Multiplication binary operator function.
251  */
252  inline void multiplicationBinOp( const double& inputValue1, const double& inputValue2,
253  double& outputValue ) const { outputValue = inputValue1 * inputValue2; }
254 
255  /*!
256  \brief Division binary operator function.
257  */
258  inline void divisionBinOp( const double& inputValue1, const double& inputValue2,
259  double& outputValue ) const {
260  ( inputValue2 == 0.0 ) ? ( outputValue = 0 )
261  : ( outputValue = inputValue1 / inputValue2 );
262  }
263 
264  /*!
265  \brief Exponencial binary operator function.
266  */
267  inline void exponencialBinOp( const double& inputValue1, const double& inputValue2,
268  double& outputValue ) const { outputValue = pow(inputValue1, inputValue2); }
269 
270  /*!
271  \brief Square root unary operator function.
272  */
273  inline void sqrtUnaryOp(const double& inputValue,
274  double& outputValue) const { outputValue = sqrt(inputValue); }
275 
276  /*!
277  \brief Sine unary operator function.
278  */
279  inline void sinUnaryOp(const double& inputValue,
280  double& outputValue) const { outputValue = sin(inputValue); }
281 
282  /*!
283  \brief Arc sine unary operator function.
284  */
285  inline void asinUnaryOp(const double& inputValue,
286  double& outputValue) const { outputValue = asin(inputValue); }
287 
288  /*!
289  \brief Cosine unary operator function.
290  */
291  inline void cosUnaryOp(const double& inputValue,
292  double& outputValue) const { outputValue = cos(inputValue); }
293 
294  /*!
295  \brief Arc cosine unary operator function.
296  */
297  inline void acosUnaryOp(const double& inputValue,
298  double& outputValue) const { outputValue = acos(inputValue); }
299 
300  /*!
301  \brief Common logarithm unary operator function.
302  */
303  inline void logUnaryOp(const double& inputValue,
304  double& outputValue) const { outputValue = log10(inputValue); }
305 
306  /*!
307  \brief Tangent unary operator function.
308  */
309  inline void tanUnaryOp(const double& inputValue,
310  double& outputValue) const { outputValue = tan(inputValue); }
311 
312  /*!
313  \brief Arc Tangent unary operator function.
314  */
315  inline void atanUnaryOp(const double& inputValue,
316  double& outputValue) const { outputValue = atan(inputValue); }
317 
318  /*!
319  \brief unary operator function.
320  */
321  inline void lnUnaryOp(const double& inputValue,
322  double& outputValue) const { outputValue = log(inputValue); }
323 
324  /*!
325  \brief Execute the automata parsing the given input string.
326  \param aStr The input arithmetic expression string.
327  \param inRasters Input rasters pointers.
328  \param outRaster Output raster pointer (pre-initiated).
329  \param generateOutput If true, the output raster data will be generated, if false only the automata execution will be performed..
330  \param progressPtr A pointer to a progress interface to be pulsed on each operation or a null pointer.
331  */
332  bool executeString( const std::string& aStr,
333  const std::vector< te::rst::Raster* >& inRasters,
334  std::unique_ptr<te::rst::Raster>& outRaster,
335  bool generateOutput,
336  te::common::TaskProgress* const progressPtr ) const;
337 
338  /*!
339  \brief Convert the input tokens vector from the infix notation to postfix notation.
340  \param input The input tokens vector.
341  \param output The output tokens vector.
342  */
343  void inFix2PostFix( const std::vector< std::string >& input,
344  std::vector< std::string >& output ) const;
345 
346  /*!
347  \brief Print tokens to stout.
348  \param input The input tokens vector.
349  */
350  void printTokens( const std::vector< std::string >& input ) const;
351 
352  /*!
353  \brief Returns true if the given token is an operator.
354  \param inputToken Input token.
355  \return Returns true if the given token is an operator.
356  */
357  bool isOperator( const std::string& inputToken ) const;
358 
359  /*!
360  \brief Returns true if the given token is a binary operator.
361  \param inputToken Input token.
362  \return Returns true if the given token is a binary operator.
363  */
364  bool isBinaryOperator( const std::string& inputToken ) const;
365 
366  /*!
367  \brief Returns true if the given token is a unary operator.
368  \param inputToken Input token.
369  \return Returns true if the given token is a unary operator.
370  */
371  bool isUnaryOperator( const std::string& inputToken ) const;
372 
373  /*!
374  \brief Returns true if operator1 has greater of equal precedence over operator2.
375  \param operator1 Operator1 input token.
376  \param operator2 Operator2 input token.
377  \return Returns true if operator1 has greater of equal precedence over operator2.
378  */
379  bool op1HasGreaterOrEqualPrecedence( const std::string& operator1,
380  const std::string& operator2 ) const;
381 
382  /*!
383  \brief Returns true if the given token is a raster data token.
384  \param token Input token.
385  \param rasterIdx The output converted raster index value.
386  \param bandIdx The output converted band index value.
387  \return Returns true if the given token is a raster data token.
388  */
389  bool isRasterBandToken( const std::string& token, unsigned int& rasterIdx,
390  unsigned int& bandIdx ) const;
391 
392  /*!
393  \brief Execute the given binary operator using the current given execution stack.
394  \param token Operator token.
395  \param execStack Execution stack.
396  \param generateOutput if true the execution will generate valid output data, if false only dummy stack elements will be generated.
397  \return true if OK, false on errors..
398  */
399  bool execBinaryOperator( const std::string& token, ExecStackT&
400  execStack, bool generateOutput ) const;
401 
402  /*!
403  \brief Execute the given binary operator using the given input rasters
404  \param inRaster1 Input raster 1.
405  \param band1 Input raster 1 band.
406  \param inRaster2 Input raster 2.
407  \param band2 Input raster 2 band.
408  \param binOptFunctPtr The binary operation function pointer.
409  \param outRasterPtr The generated output raster.
410  \return true if OK, false on errors..
411  */
412  bool execBinaryOperatorRasterXRaster( const te::rst::Raster& inRaster1,
413  const unsigned int band1, const te::rst::Raster& inRaster2,
414  const unsigned int band2,
415  const BinOpFuncPtrT binOptFunctPtr,
416  std::unique_ptr<te::rst::Raster>& outRasterPtr ) const;
417 
418  /*!
419  \brief Execute the given binary operator using the given input raster and a real number
420  \param inRaster Input raster.
421  \param bandIdx Input raster band.
422  \param binOptFunctPtr The binary operation function pointer.
423  \param outRasterPtr The generated output raster.
424  \param realNumberIsRigthtTerm true if the real number is the right term.
425  \return true if OK, false on errors..
426  */
427  bool execBinaryOperatorRasterXReal( const te::rst::Raster& inRaster,
428  const unsigned int bandIdx, const double value,
429  const BinOpFuncPtrT binOptFunctPtr,
430  std::unique_ptr<te::rst::Raster>& outRasterPtr,
431  const bool realNumberIsRigthtTerm ) const;
432 
433  /*!
434  \brief Execute the given unary operator using the current given execution stack.
435  \param token Operator token.
436  \param execStack Execution stack.
437  \param generateOutput if true the execution will generate valid output data, if false only dummy stack elements will be generated.
438  \return true if OK, false on errors..
439  */
440  bool execUnaryOperator( const std::string& token, ExecStackT&
441  execStack, bool generateOutput ) const;
442 
443  /*!
444  \brief Execute the given unary operator using the given input raster
445  \param inRaster Input raster.
446  \param band1 Input raster band.
447  \param unaryOptFunctPtr The unary operation function pointer.
448  \param outRasterPtr The generated output raster.
449  \return true if OK, false on errors..
450  */
451  bool execUnaryOperatorRaster(const te::rst::Raster& inRaster,
452  const unsigned int band, const UnaryOpFuncPtrT unaryOptFunctPtr,
453  std::unique_ptr<te::rst::Raster>& outRasterPtr) const;
454 
455  /*!
456  \brief Execute the given unary operator using the given a real number
457  \param unaryOptFunctPtr The unary operation function pointer.
458  \param outRasterPtr The generated output raster.
459  \param realNumberIsRigthtTerm true if the real number is the right term.
460  \return true if OK, false on errors..
461  */
462  bool execUnaryOperatorReal(const double value,
463  const UnaryOpFuncPtrT unaryOptFunctPtr,
464  std::unique_ptr<te::rst::Raster>& outRasterPtr,
465  const bool realNumberIsRigthtTerm) const;
466 
467  /*!
468  \brief Returns true if the given token is a real number.
469  \param token Input token.
470  \param realValue The output converted value.
471  \return Returns true if the given token is a real number.
472  */
473  bool isRealNumberToken( const std::string& token, double& realValue ) const;
474 
475  /*!
476  \brief Allocate a new RAM memory raster.
477  \param grid The output raster grid.
478  \param rasterPtr The output raster pointer.
479  \return Returns true if OK, false on errors.
480  */
481  bool allocResultRaster( const te::rst::Grid& grid,
482  std::unique_ptr<te::rst::Raster>& rasterPtr ) const;
483 
484  /*!
485  \brief Split the input string into a vector of token strings
486  \param inputStr The input string.
487  \param outTokens The generated output tokens vector.
488  */
489  void getTokensStrs( const std::string& inputStr,
490  std::vector< std::string >& outTokens ) const;
491  };
492  } // end namespace rp
493 } // end namespace te
494 
495 #endif // __TERRALIB_RP_INTERNAL_ARITHMETICOPERATIONS_H
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
void divisionBinOp(const double &inputValue1, const double &inputValue2, double &outputValue) const
Division binary operator function.
void exponencialBinOp(const double &inputValue1, const double &inputValue2, double &outputValue) const
Exponencial binary operator function.
void cosUnaryOp(const double &inputValue, double &outputValue) const
Cosine unary operator function.
bool m_isBinaryOp
true if this is a binary operator pointer element.
bool m_isInitialized
Tells if this instance is initialized.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
Raster Processing algorithm output parameters base interface.
bool m_normalize
Output values normalization will be performed to fit the original input raster values range (default:...
ArithmeticOperations::InputParameters m_inputParameters
Input execution parameters.
std::map< std::string, std::string > m_rInfo
The necessary information to create the output rasters (as described in te::raster::RasterFactory).
#define _NOEXCEPT_OP(x)
Raster Processing algorithm base interface class.
InterpolationMethod
Allowed interpolation methods.
Definition: Enums.h:92
bool m_enableProgress
Enable/Disable the progress interface (default:false).
bool m_isRaster
true if this is a raster pointer element.
void additionBinOp(const double &inputValue1, const double &inputValue2, double &outputValue) const
Addition binary operator function.
void subtractionBinOp(const double &inputValue1, const double &inputValue2, double &outputValue) const
Subtraction binary operator function.
std::stack< ExecStackElement > ExecStackT
Execution stack type definition.
void acosUnaryOp(const double &inputValue, double &outputValue) const
Arc cosine unary operator function.
void tanUnaryOp(const double &inputValue, double &outputValue) const
Tangent unary operator function.
Raster Processing algorithm base interface.
Definition: Algorithm.h:41
An abstract class for raster data strucutures.
Definition: Raster.h:71
bool m_isUnaryOp
true if this is a unary operator pointer element.
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
The generated output registered raster.
TerraLib.
ArithmeticOperations output parameters.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
void asinUnaryOp(const double &inputValue, double &outputValue) const
Arc sine unary operator function.
ExecStackElement & operator=(const ExecStackElement &rhs)
void multiplicationBinOp(const double &inputValue1, const double &inputValue2, double &outputValue) const
Multiplication binary operator function.
void atanUnaryOp(const double &inputValue, double &outputValue) const
Arc Tangent unary operator function.
void logUnaryOp(const double &inputValue, double &outputValue) const
Common logarithm unary operator function.
te::rst::Raster * m_rasterNPtr
Raster pointer.
std::vector< te::rst::Raster *> m_inputRasters
Input rasters vector.
void sinUnaryOp(const double &inputValue, double &outputValue) const
Sine unary operator function.
Raster Processing algorithm input parameters base interface.
std::unique_ptr< te::rst::Raster > m_rasterHandler
Raster handler.
void lnUnaryOp(const double &inputValue, double &outputValue) const
unary operator function.
void sqrtUnaryOp(const double &inputValue, double &outputValue) const
Square root unary operator function.
Performs arithmetic operation over raster data.
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
bool m_isRealNumber
true if this is a real number element.
te::rst::Interpolator::Method m_interpMethod
The raster interpolator method (default:NearestNeighbor).
ArithmeticOperations input parameters.
std::string m_arithmeticString
Arithmetic string.