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