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