Composer.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 #ifndef __TERRALIB_RP_INTERNAL_COMPOSER_H
21 #define __TERRALIB_RP_INTERNAL_COMPOSER_H
22 
23 #include "Config.h"
24 #include "Macros.h"
25 #include "ComposerRule.h"
26 #include "../raster/Grid.h"
27 #include "../raster/Raster.h"
28 #include "../raster/Interpolator.h"
29 #include "../raster/RasterSynchronizer.h"
30 #include "../raster/TileIndexer.h"
31 #include "../geometry/Polygon.h"
32 #include "../geometry/MultiPolygon.h"
33 #include "../geometry/Point.h"
34 #include "../geometry/GeometricTransformation.h"
35 
36 #include <boost/ptr_container/ptr_vector.hpp>
37 
38 #include <vector>
39 #include <complex>
40 #include <memory>
41 #include <thread>
42 
43 namespace te
44 {
45  namespace rp
46  {
47  /*!
48  \class Composer
49  \brief Composed pixel value calculation for two overlaped rasters.
50  \details The overlap between the two images is modeled by the given geometric transformation direct mapping input raster 1 indexed coords (line, column) to input raster 2 indexed coords.
51  \note Optimized for rasters where all bands have the same blocking scheme.
52  \ingroup rp_aux
53  */
55  {
56  public:
57 
58  /*! Default constructor. */
60 
61  /*! Default destructor. */
63 
64  /*!
65  \brief Inititate the composer instance.
66  \param raster1 Raster 1.
67  \param raster1Bands Raster 1 band indexes to use.
68  \param raster2 Raster 2.
69  \param raster2Bands Raster 2 band indexes to use (this vector has the same size as raster1Bands).
70  \param raster3 Raster 3.
71  \param raster3Bands Raster 3 band indexes to use (this vector has the same size as raster1Bands).
72  \param interpMethod1 The interpolation method to use when reading raster 1 data.
73  \param interpMethod2 The interpolation method to use when reading raster 2 data.
74  \param noDataValue The value returned where there is no pixel data bo compose.
75  \param forceRaster1NoDataValue Use noDataValue as the input raster 1 no-data value (The original rasters no-data values will be ignored)
76  \param forceRaster2NoDataValue Use noDataValue as the input raster 1 no-data value (The original rasters no-data values will be ignored)
77  \param pixelOffsets1 The values offset to be applied to raster 1 pixel values before the composeed value calcule (one element for each used raster channel/band).
78  \param pixelScales1 The values scale to be applied to raster 1 pixel values before the composeed value calcule (one element for each used raster channel/band).
79  \param pixelOffsets2 The values offset to be applied to raster 2 pixel values before the composeed value calcule (one element for each used raster channel/band).
80  \param pixelScales2 The values scale to be applied to raster 2 pixel values before the composeed value calcule (one element for each used raster channel/band).
81  \param threadsNumber Enable/disable the use of threads when applicable (0:automatic , 1:disabled, any other integer dictates the number of threads).
82  \param enableProgressInterface Enable/disable the use of a progress interface when applicable.
83  \param rulePtr A pointer to a valid composer rule.
84  \param enableRasterCache Enable/disable the use of raster cache.
85  \return true if ok, false on errors
86  \note Aboute scale and offset parametrs: outValue = ( inputValue * scale ) + offset
87  \note All raster 3 bands must have the same blocking scheme.
88  */
89  bool initialize(
90  te::rst::Raster& raster1,
91  const std::vector< unsigned int >& raster1Bands,
92  const te::rst::Raster& raster2,
93  const std::vector< unsigned int >& raster2Bands,
94  te::rst::Raster& raster3,
95  const std::vector< unsigned int >& raster3Bands,
96  const te::rst::Interpolator::Method& interpMethod1,
97  const te::rst::Interpolator::Method& interpMethod2,
98  const double& noDataValue,
99  const bool forceRaster1NoDataValue,
100  const bool forceRaster2NoDataValue,
101  const std::vector< double >& pixelOffsets1,
102  const std::vector< double >& pixelScales1,
103  const std::vector< double >& pixelOffsets2,
104  const std::vector< double >& pixelScales2,
105  const unsigned int threadsNumber,
106  const bool enableProgressInterface,
107  ComposerRule const * const rulePtr,
108  const bool enableRasterCache );
109 
110 
111  /*!
112  \brief Compose a pixel value using the current parameters.
113  \param line Line (raster 3 reference).
114  \param col Column (raster 3 reference).
115  \param values A pointer to a pre-allocated vector where the composeed values will be stored.
116  \note The caller of this method must be aware that the returned composeed value may be outside the original input rasters valid values range.
117  \note Raster 1 values have precedence over raster 2 values (when applicable).
118  */
119  inline void getComposedValues( const double& line, const double& col,
120  double* const values )
121  {
122  TERP_DEBUG_TRUE_OR_THROW( m_rulePtr.get() != nullptr, "Invalid compose rule" );
123  m_rulePtr->getComposedValues( line, col, values );
124  };
125 
126  /*!
127  \brief Execute a composition of the given input rasters and write the result into raster 3.
128  \note Raster 1 values have precedence over raster 2 values (when applicable).
129  */
131 
132  /*!
133  \brief Return the current error message if there is any.
134 
135  \return Return the current error message if there is any.
136  */
137  const std::string& getErrorMessage() const;
138 
139  protected:
140 
141  /*!
142  \brief Raster block info
143  */
145  {
147  unsigned int m_blkX;
148  unsigned int m_blkY;
154  };
155 
156  /*!
157  \brief The parameters passed to composeIntoRaster1Thread method.
158  */
160  {
161  public :
162 
163  bool* m_returnValuePtr; //!< A pointer to the threadreturn value.
164  bool* m_abortValuePtr; //!< A pointer to the abort execution value.
165  te::rst::RasterSynchronizer* m_sync1Ptr; //!< Raster 1 syncronizer pointer.
166  te::rst::RasterSynchronizer* m_sync2Ptr; //!< Raster 2 syncronizer pointer.
167  te::rst::RasterSynchronizer* m_sync3Ptr; //!< Raster 3 syncronizer pointer.
168  std::vector< RasterBlockInfo >* m_raster3BlocksInfosPtr; //!< blocks to process.
169  std::mutex* m_mutexPtr; //!< mutex pointer.
170  std::mutex* m_blockProcessedSignalMutexPtr; //!< Mutex used to update the main process progress update.
171  std::condition_variable* m_blockProcessedSignalPtr; //!< Signal used to update the main process progress update.
172  unsigned int* m_runningThreadsCounterPtr; //!< a pointer to the running threads counter.
173  std::vector< unsigned int > const* m_raster1BandsPtr; //!< Input raster 1 band indexes to use.
174  std::vector< unsigned int > const* m_raster2BandsPtr; //!< Input raster 2 band indexes to use (this vector has the same size as raster1Bands).
175  std::vector< unsigned int > const* m_raster3BandsPtr; //!< Input raster 3 band indexes to use (this vector has the same size as raster1Bands).
176  te::rst::Interpolator::Method m_interpMethod1; //!< The interpolation method to use when reading raster 2 data.
177  te::rst::Interpolator::Method m_interpMethod2; //!< The interpolation method to use when reading raster 2 data.
178  double m_noDataValue; //!< The value returned where there is no pixel data bo compose.
179  bool m_forceRaster1NoDataValue; //!< Use noDataValue as the input rasters no-data value (The original rasters no-data values will be ignored)
180  bool m_forceRaster2NoDataValue; //!< Use noDataValue as the input rasters no-data value (The original rasters no-data values will be ignored)
181  std::vector< double > const* m_pixelOffsets1Ptr; //!< The values offset to be applied to raster 1 pixel values before the composeed value calcule (one element for each used raster channel/band).
182  std::vector< double > const* m_pixelScales1Ptr; //!< The values scale to be applied to raster 1 pixel values before the composeed value calcule (one element for each used raster channel/band).
183  std::vector< double > const* m_pixelOffsets2Ptr; //!< The values offset to be applied to raster 2 pixel values before the composeed value calcule (one element for each used raster channel/band).
184  std::vector< double > const* m_pixelScales2Ptr; //!< The values scale to be applied to raster 2 pixel values before the composeed value calcule (one element for each used raster channel/band).
185  unsigned long int m_maxRasterCachedBlocks; //!< The maximum number of raster cache blocks.
186  bool m_useProgress; //!< If enabled each thread will use its own progress interface, if false only a signal will be emitted on each processed block.
187  ComposerRule const * m_rulePtr; // a pointer to a rula to be used or a null pointer.
188  unsigned int* m_processedBlocksNmbPtr;
189 
191 
193 
194  protected :
195 
197 
199  };
200 
201  bool m_enableProgressInterface; //!< Enable progress interface.
202  bool m_forceRaster1NoDataValue; //!< Use noDataValue as the input raster 1 no-data value (The original rasters no-data values will be ignored)
203  bool m_forceRaster2NoDataValue; //!< Use noDataValue as the input raster 2 no-data value (The original rasters no-data values will be ignored)
204  unsigned int m_threadsNumber; //!< The number of threads to use (0:automatic , 1:disabled, any other integer dictates the number of threads)..
205  te::rst::Raster const * m_raster1Ptr; //!< Raster 1.
206  te::rst::Raster const * m_raster2Ptr; //!< Raster 2.
207  te::rst::Raster * m_raster3Ptr; //!< Raster 3.
208  te::rst::Interpolator::Method m_interpMethod1; //!< The interpolation method to use when reading raster 1 data.
209  te::rst::Interpolator::Method m_interpMethod2; //!< The interpolation method to use when reading raster 2 data.
210  double m_outputNoDataValue; //!< The output raster no-data value.
211  std::vector< unsigned int > m_raster1Bands; //!< Input raster 1 band indexes to use.
212  std::vector< unsigned int > m_raster2Bands; //!< Input raster 2 band indexes to use.
213  std::vector< unsigned int > m_raster3Bands; //!< Input raster 3 band indexes to use.
214  std::vector< double > m_pixelOffsets1; //!< The values offset to be applied to raster 1 pixel values before the composeed value calcule (one element for each used raster channel/band).
215  std::vector< double > m_pixelScales1; //!< The values scale to be applied to raster 1 pixel values before the composeed value calcule (one element for each used raster channel/band).
216  std::vector< double > m_pixelOffsets2; //!< The values offset to be applied to raster 2 pixel values before the composeed value calcule (one element for each used raster channel/band).
217  std::vector< double > m_pixelScales2; //!< The values scale to be applied to raster 2 pixel values before the composeed value calcule (one element for each used raster channel/band).
218  std::unique_ptr< ComposerRule > m_rulePtr; //!< Composer rule pointer.
219  std::string m_errorMessage; //!< Current error message.
220  bool m_enableRasterCache; //!< enable/disable the use of raster cache.
221 
222  /*! \brief Reset the instance to its initial default state. */
223  void initState();
224 
225  /*! \brief Clear all internal allocated resources. */
226  void clear();
227 
228  /*!
229  \brief Thread entry for the method composeIntoRaster1.
230  */
232 
233  /*!
234  \brief Set the current error message.
235 
236  \param newErrorMessage New error message;
237  */
238  void setErrorMessage( const std::string& newErrorMessage );
239 
240  private :
241 
242  Composer( const Composer& other );
243 
244  Composer& operator=( const Composer& other );
245  };
246 
247  } // end namespace rp
248 } // end namespace te
249 
250 #endif
251 
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Definition: Macros.h:445
The parameters passed to composeIntoRaster1Thread method.
Definition: Composer.h:160
std::vector< unsigned int > const * m_raster3BandsPtr
Input raster 3 band indexes to use (this vector has the same size as raster1Bands).
Definition: Composer.h:175
std::vector< double > const * m_pixelScales2Ptr
The values scale to be applied to raster 2 pixel values before the composeed value calcule (one eleme...
Definition: Composer.h:184
te::rst::RasterSynchronizer * m_sync1Ptr
Raster 1 syncronizer pointer.
Definition: Composer.h:165
std::vector< double > const * m_pixelScales1Ptr
The values scale to be applied to raster 1 pixel values before the composeed value calcule (one eleme...
Definition: Composer.h:182
std::vector< unsigned int > const * m_raster2BandsPtr
Input raster 2 band indexes to use (this vector has the same size as raster1Bands).
Definition: Composer.h:174
ComposeIntoRaster3ThreadParams(const ComposeIntoRaster3ThreadParams &)
Definition: Composer.h:196
te::rst::Interpolator::Method m_interpMethod2
The interpolation method to use when reading raster 2 data.
Definition: Composer.h:177
bool m_forceRaster1NoDataValue
Use noDataValue as the input rasters no-data value (The original rasters no-data values will be ignor...
Definition: Composer.h:179
std::condition_variable * m_blockProcessedSignalPtr
Signal used to update the main process progress update.
Definition: Composer.h:171
unsigned long int m_maxRasterCachedBlocks
The maximum number of raster cache blocks.
Definition: Composer.h:185
std::vector< double > const * m_pixelOffsets1Ptr
The values offset to be applied to raster 1 pixel values before the composeed value calcule (one elem...
Definition: Composer.h:181
ComposeIntoRaster3ThreadParams & operator=(const ComposeIntoRaster3ThreadParams &)
Definition: Composer.h:198
std::mutex * m_blockProcessedSignalMutexPtr
Mutex used to update the main process progress update.
Definition: Composer.h:170
std::vector< unsigned int > const * m_raster1BandsPtr
Input raster 1 band indexes to use.
Definition: Composer.h:173
std::vector< double > const * m_pixelOffsets2Ptr
The values offset to be applied to raster 2 pixel values before the composeed value calcule (one elem...
Definition: Composer.h:183
unsigned int * m_runningThreadsCounterPtr
a pointer to the running threads counter.
Definition: Composer.h:172
te::rst::RasterSynchronizer * m_sync2Ptr
Raster 2 syncronizer pointer.
Definition: Composer.h:166
bool m_forceRaster2NoDataValue
Use noDataValue as the input rasters no-data value (The original rasters no-data values will be ignor...
Definition: Composer.h:180
bool m_useProgress
If enabled each thread will use its own progress interface, if false only a signal will be emitted on...
Definition: Composer.h:186
double m_noDataValue
The value returned where there is no pixel data bo compose.
Definition: Composer.h:178
te::rst::RasterSynchronizer * m_sync3Ptr
Raster 3 syncronizer pointer.
Definition: Composer.h:167
bool * m_returnValuePtr
A pointer to the threadreturn value.
Definition: Composer.h:163
bool * m_abortValuePtr
A pointer to the abort execution value.
Definition: Composer.h:164
std::vector< RasterBlockInfo > * m_raster3BlocksInfosPtr
blocks to process.
Definition: Composer.h:168
te::rst::Interpolator::Method m_interpMethod1
The interpolation method to use when reading raster 2 data.
Definition: Composer.h:176
Composed pixel value calculation for two overlaped rasters.
Definition: Composer.h:55
te::rst::Interpolator::Method m_interpMethod1
The interpolation method to use when reading raster 1 data.
Definition: Composer.h:208
te::rst::Raster * m_raster3Ptr
Raster 3.
Definition: Composer.h:207
te::rst::Interpolator::Method m_interpMethod2
The interpolation method to use when reading raster 2 data.
Definition: Composer.h:209
void clear()
Clear all internal allocated resources.
std::vector< unsigned int > m_raster2Bands
Input raster 2 band indexes to use.
Definition: Composer.h:212
te::rst::Raster const * m_raster1Ptr
Raster 1.
Definition: Composer.h:205
void initState()
Reset the instance to its initial default state.
bool composeIntoRaster3()
Execute a composition of the given input rasters and write the result into raster 3.
bool m_forceRaster2NoDataValue
Use noDataValue as the input raster 2 no-data value (The original rasters no-data values will be igno...
Definition: Composer.h:203
unsigned int m_threadsNumber
The number of threads to use (0:automatic , 1:disabled, any other integer dictates the number of thre...
Definition: Composer.h:204
Composer & operator=(const Composer &other)
std::vector< double > m_pixelScales2
The values scale to be applied to raster 2 pixel values before the composeed value calcule (one eleme...
Definition: Composer.h:217
std::vector< double > m_pixelScales1
The values scale to be applied to raster 1 pixel values before the composeed value calcule (one eleme...
Definition: Composer.h:215
std::string m_errorMessage
Current error message.
Definition: Composer.h:219
std::vector< double > m_pixelOffsets2
The values offset to be applied to raster 2 pixel values before the composeed value calcule (one elem...
Definition: Composer.h:216
bool m_enableRasterCache
enable/disable the use of raster cache.
Definition: Composer.h:220
const std::string & getErrorMessage() const
Return the current error message if there is any.
Composer(const Composer &other)
bool m_enableProgressInterface
Enable progress interface.
Definition: Composer.h:201
void getComposedValues(const double &line, const double &col, double *const values)
Compose a pixel value using the current parameters.
Definition: Composer.h:119
std::vector< unsigned int > m_raster1Bands
Input raster 1 band indexes to use.
Definition: Composer.h:211
std::vector< double > m_pixelOffsets1
The values offset to be applied to raster 1 pixel values before the composeed value calcule (one elem...
Definition: Composer.h:214
void setErrorMessage(const std::string &newErrorMessage)
Set the current error message.
bool initialize(te::rst::Raster &raster1, const std::vector< unsigned int > &raster1Bands, const te::rst::Raster &raster2, const std::vector< unsigned int > &raster2Bands, te::rst::Raster &raster3, const std::vector< unsigned int > &raster3Bands, const te::rst::Interpolator::Method &interpMethod1, const te::rst::Interpolator::Method &interpMethod2, const double &noDataValue, const bool forceRaster1NoDataValue, const bool forceRaster2NoDataValue, const std::vector< double > &pixelOffsets1, const std::vector< double > &pixelScales1, const std::vector< double > &pixelOffsets2, const std::vector< double > &pixelScales2, const unsigned int threadsNumber, const bool enableProgressInterface, ComposerRule const *const rulePtr, const bool enableRasterCache)
Inititate the composer instance.
std::vector< unsigned int > m_raster3Bands
Input raster 3 band indexes to use.
Definition: Composer.h:213
std::unique_ptr< ComposerRule > m_rulePtr
Composer rule pointer.
Definition: Composer.h:218
bool m_forceRaster1NoDataValue
Use noDataValue as the input raster 1 no-data value (The original rasters no-data values will be igno...
Definition: Composer.h:202
double m_outputNoDataValue
The output raster no-data value.
Definition: Composer.h:210
te::rst::Raster const * m_raster2Ptr
Raster 2.
Definition: Composer.h:206
static void composeIntoRaster3Thread(ComposeIntoRaster3ThreadParams *paramsPtr)
Thread entry for the method composeIntoRaster1.
An access synchronizer to be used in SynchronizedRaster raster instances.
An abstract class for raster data strucutures.
Definition: Raster.h:72
InterpolationMethod
Allowed interpolation methods.
Definition: Enums.h:93
TerraLib.
unsigned int m_rasterRows2ProcessBound
Definition: Composer.h:151
unsigned int m_firstRasterRow2Process
Definition: Composer.h:150
unsigned int m_firstRasterCol2Process
Definition: Composer.h:152
unsigned int m_rasterCols2ProcessBound
Definition: Composer.h:153
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
Proxy configuration file for TerraView (see terraview_config.h).