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