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 
39 namespace te
40 {
41  namespace rst
42  {
43  class Raster;
44  }
45 
46  namespace rp
47  {
48  /*!
49  \class ArithmeticOperations
50 
51  \brief Performs arithmetic operation over raster data.
52 
53  \details The expression (arithmetic stringIt) must be formed by a combination of the following elements,
54  separated by spaces:
55 
56  Operators: +, -, *, /
57 
58  Real Numbers (negative numbers must follow the form "-1.0")
59 
60  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).
61 
62  \note Reference: TerraLib 4 - Image Processing Module
63 
64  \ingroup rp_gen
65  */
67  {
68  public:
69 
70  /*!
71  \class InputParameters
72  \brief ArithmeticOperations input parameters
73  */
75  {
76  public:
77 
78  std::vector< te::rst::Raster* > m_inputRasters; //!< Input rasters vector.
79 
80  std::string m_arithmeticString; //!< Arithmetic string.
81 
82  bool m_normalize; //!< Output values normalization will be performed to fit the allowed values range (default:false).
83 
84  bool m_enableProgress; //!< Enable/Disable the progress interface (default:false).
85 
86  te::rst::Interpolator::Method m_interpMethod; //!< The raster interpolator method (default:NearestNeighbor).
87 
89 
91 
92  ~InputParameters();
93 
94  //overload
95  void reset() throw( te::rp::Exception );
96 
97  //overload
98  const InputParameters& operator=( const InputParameters& params );
99 
100  //overload
101  AbstractParameters* clone() const;
102  };
103 
104  /*!
105  \class OutputParameters
106  \brief ArithmeticOperations output parameters
107  */
109  {
110  public:
111 
112  std::string m_rType; //!< Output raster data source type (as described in te::raster::RasterFactory ).
113 
114  std::map< std::string, std::string > m_rInfo; //!< The necessary information to create the output rasters (as described in te::raster::RasterFactory).
115 
116  std::auto_ptr< te::rst::Raster > m_outputRasterPtr; //!< The generated output registered raster.
117 
119 
121 
122  ~OutputParameters();
123 
124  //overload
125  void reset() throw( te::rp::Exception );
126 
127  //overload
128  const OutputParameters& operator=( const OutputParameters& params );
129 
130  //overload
131  AbstractParameters* clone() const;
132  };
133 
135 
137 
138  //overload
139  bool execute( AlgorithmOutputParameters& outputParams ) throw( te::rp::Exception );
140 
141  //overload
142  void reset() throw( te::rp::Exception );
143 
144  //overload
145  bool initialize( const AlgorithmInputParameters& inputParams ) throw( te::rp::Exception );
146 
147  bool isInitialized() const;
148 
149  protected:
150 
151  /*!
152  \class ExecStackElement
153  \brief Execution stack element.
154  */
156  {
157  public :
158 
159  bool m_isRaster; //!< true if this is a raster pointer element.
160 
161  bool m_isRealNumber; //!< true if this is a real number element.
162 
163  double m_realNumberValue; //!< Real number value.
164 
165  unsigned int m_rasterBand; //!< Raster band index.
166 
167  te::rst::Raster* m_rasterNPtr; //!< Raster pointer.
168 
169  mutable std::auto_ptr< te::rst::Raster > m_rasterHandler; //!< Raster handler.
170 
171  ExecStackElement( const ExecStackElement& rhs )
172  : m_isRaster( false ), m_isRealNumber( false ), m_realNumberValue( 0 ),
173  m_rasterBand( 0 ), m_rasterNPtr( 0 )
174  {
175  operator==( rhs );
176  };
177 
179  : m_isRaster( false ), m_isRealNumber( false ), m_realNumberValue( 0 ),
180  m_rasterBand( 0 ), m_rasterNPtr( 0 )
181  {};
182 
183  ExecStackElement& operator==( const ExecStackElement& rhs )
184  {
185  m_isRaster = rhs.m_isRaster;
186  m_isRealNumber = rhs.m_isRealNumber;
187  m_realNumberValue = rhs.m_realNumberValue;
188  m_rasterBand = rhs.m_rasterBand;
189  m_rasterNPtr = rhs.m_rasterNPtr;
190  m_rasterHandler.reset( rhs.m_rasterHandler.release() );
191 
192  return *this;
193  };
194 
196  };
197 
198  typedef std::stack< ExecStackElement > ExecStackT; //!< Execution stack type definition.
199 
200  /*!
201  \brief Type definition for a operation function pointer.
202  */
203  typedef void (ArithmeticOperations::*BinOpFuncPtrT)( const double& inputValue1, const double& inputValue2,
204  double& outputValue ) const;
205 
207 
208  bool m_isInitialized; //!< Tells if this instance is initialized.
209 
210  /*!
211  \brief Addition binary operator function.
212  */
213  inline void additionBinOp( const double& inputValue1, const double& inputValue2,
214  double& outputValue ) const { outputValue = inputValue1 + inputValue2; };
215 
216  /*!
217  \brief Subtraction binary operator function.
218  */
219  inline void subtractionBinOp( const double& inputValue1, const double& inputValue2,
220  double& outputValue ) const { outputValue = inputValue1 - inputValue2; };
221 
222  /*!
223  \brief Multiplication binary operator function.
224  */
225  inline void multiplicationBinOp( const double& inputValue1, const double& inputValue2,
226  double& outputValue ) const { outputValue = inputValue1 * inputValue2; };
227 
228  /*!
229  \brief Division binary operator function.
230  */
231  inline void divisionBinOp( const double& inputValue1, const double& inputValue2,
232  double& outputValue ) const { ( inputValue2 == 0.0 ) ? ( outputValue = 0 )
233  : ( outputValue = inputValue1 / inputValue2 ); };
234 
235  /*!
236  \brief Execute the automata parsing the given input string.
237  \param aStr The input arithmetic expression string.
238  \param inRasters Input rasters pointers.
239  \param outRaster Output raster pointer (pre-initiated).
240  \param generateOutput If true, the output raster data will be generated, if false only the automata execution will be performed..
241  \param progressPtr A pointer to a progress interface to be pulsed on each operation or a null pointer.
242  */
243  bool executeString( const std::string& aStr,
244  const std::vector< te::rst::Raster* >& inRasters,
245  std::auto_ptr<te::rst::Raster>& outRaster,
246  bool generateOutput,
247  te::common::TaskProgress* const progressPtr ) const;
248 
249  /*!
250  \brief Convert the input tokens vector from the infix notation to postfix notation.
251  \param input The input tokens vector.
252  \param output The output tokens vector.
253  */
254  void inFix2PostFix( const std::vector< std::string >& input,
255  std::vector< std::string >& output ) const;
256 
257  /*!
258  \brief Print tokens to stout.
259  \param input The input tokens vector.
260  */
261  void printTokens( const std::vector< std::string >& input ) const;
262 
263  /*!
264  \brief Returns true if the given token is an operator.
265  \param inputToken Input token.
266  \return Returns true if the given token is an operator.
267  */
268  bool isOperator( const std::string& inputToken ) const;
269 
270  /*!
271  \brief Returns true if the given token is a binary operator.
272  \param inputToken Input token.
273  \return Returns true if the given token is a binary operator.
274  */
275  bool isBinaryOperator( const std::string& inputToken ) const;
276 
277  /*!
278  \brief Returns true if the given token is a unary operator.
279  \param inputToken Input token.
280  \return Returns true if the given token is a unary operator.
281  */
282  bool isUnaryOperator( const std::string& inputToken ) const;
283 
284  /*!
285  \brief Returns true if operator1 has greater of equal precedence over operator2.
286  \param operator1 Operator1 input token.
287  \param operator2 Operator2 input token.
288  \return Returns true if operator1 has greater of equal precedence over operator2.
289  */
290  bool op1HasGreaterOrEqualPrecedence( const std::string& operator1,
291  const std::string& operator2 ) const;
292 
293  /*!
294  \brief Returns true if the given token is a raster data token.
295  \param token Input token.
296  \param rasterIdx The output converted raster index value.
297  \param bandIdx The output converted band index value.
298  \return Returns true if the given token is a raster data token.
299  */
300  bool isRasterBandToken( const std::string& token, unsigned int& rasterIdx,
301  unsigned int& bandIdx ) const;
302 
303  /*!
304  \brief Execute the given binary operator using the current given execution stack.
305  \param token Operator token.
306  \param execStack Execution stack.
307  \param generateOutput if true the execution will generate valid output data, if false only dummy stack elements will be generated.
308  \return true if OK, false on errors..
309  */
310  bool execBinaryOperator( const std::string& token, ExecStackT&
311  execStack, bool generateOutput ) const;
312 
313  /*!
314  \brief Execute the given binary operator using the given input rasters
315  \param inRaster1 Input raster 1.
316  \param band1 Input raster 1 band.
317  \param inRaster2 Input raster 2.
318  \param band2 Input raster 2 band.
319  \param binOptFunctPtr The binary operation function pointer.
320  \param outRasterPtr The generated output raster.
321  \return true if OK, false on errors..
322  */
323  bool execBinaryOperatorRasterXRaster( const te::rst::Raster& inRaster1,
324  const unsigned int band1, const te::rst::Raster& inRaster2,
325  const unsigned int band2,
326  const BinOpFuncPtrT binOptFunctPtr,
327  std::auto_ptr<te::rst::Raster>& outRasterPtr ) const;
328 
329  /*!
330  \brief Execute the given binary operator using the given input raster and a real number
331  \param inRaster Input raster.
332  \param bandIdx Input raster band.
333  \param binOptFunctPtr The binary operation function pointer.
334  \param outRasterPtr The generated output raster.
335  \param realNumberIsRigthtTerm true if the real number is the right term.
336  \return true if OK, false on errors..
337  */
338  bool execBinaryOperatorRasterXReal( const te::rst::Raster& inRaster,
339  const unsigned int bandIdx, const double value,
340  const BinOpFuncPtrT binOptFunctPtr,
341  std::auto_ptr<te::rst::Raster>& outRasterPtr,
342  const bool realNumberIsRigthtTerm ) const;
343 
344  /*!
345  \brief Execute the given unary operator using the current given execution stack.
346  \param token Operator token.
347  \param execStack Execution stack.
348  \param generateOutput if true the execution will generate valid output data, if false only dummy stack elements will be generated.
349  \return true if OK, false on errors..
350  */
351  bool execUnaryOperator( const std::string& token, ExecStackT&
352  execStack, bool generateOutput ) const;
353 
354  /*!
355  \brief Returns true if the given token is a real number.
356  \param token Input token.
357  \param realValue The output converted value.
358  \return Returns true if the given token is a real number.
359  */
360  bool isRealNumberToken( const std::string& token, double& realValue ) const;
361 
362  /*!
363  \brief Allocate a new RAM memory raster.
364  \param grid The output raster grid.
365  \param rasterPtr The output raster pointer.
366  \return Returns true if OK, false on errors.
367  */
368  bool allocResultRaster( const te::rst::Grid& grid,
369  std::auto_ptr<te::rst::Raster>& rasterPtr ) const;
370 
371  /*!
372  \brief Split the input string into a vector of token strings
373  \param inputStr The input string.
374  \param outTokens The generated output tokens vector.
375  */
376  void getTokensStrs( const std::string& inputStr,
377  std::vector< std::string >& outTokens ) const;
378  };
379  } // end namespace rp
380 } // end namespace te
381 
382 #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.
Base exception class for plugin module.
Definition: Exception.h:42
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 allowed values range (default:false).
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).
TEDATAACCESSEXPORT te::da::Expression * operator==(const te::da::Expression &e1, const te::da::Expression &e2)
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.
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
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
te::rst::Raster * m_rasterNPtr
Raster pointer.
std::vector< te::rst::Raster * > m_inputRasters
Input rasters vector.
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
The generated output registered raster.
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.
ExecStackElement & operator==(const ExecStackElement &rhs)
void additionBinOp(const double &inputValue1, const double &inputValue2, double &outputValue) const
Addition binary operator function.
std::string m_arithmeticString
Arithmetic string.