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