| TerraLib 4.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/image_processing/TePDITileIndexer.hpp
Go to the documentation of this file.

    00001 /*
    00002 TerraLib - a library for developing GIS applications.
    00003 Copyright  2001, 2002, 2003 INPE and Tecgraf/PUC-Rio.
    00004
    00005 This code is part of the TerraLib library.
    00006 This library is free software; you can redistribute it and/or
    00007 modify it under the terms of the GNU Lesser General Public
    00008 License as published by the Free Software Foundation; either
    00009 version 2.1 of the License, or (at your option) any later version.
    00010
    00011 You should have received a copy of the GNU Lesser General Public
    00012 License along with this library.
    00013
    00014 The authors reassure the license terms regarding the warranties.
    00015 They specifically disclaim any warranties, including, but not limited to,
    00016 the implied warranties of merchantability and fitness for a particular
    00017 purpose. The library provided hereunder is on an "as is" basis, and the
    00018 authors have no obligation to provide maintenance, support, updates,
    00019 enhancements, or modifications.
    00020 In no event shall INPE be held liable to any party
    00021 for direct, indirect, special, incidental, or consequential damages arising
    00022 out of the use of this library and its documentation.
    00023 */
    00024
    00025 #ifndef TEPDITILEINDEXER_HPP
    00026 #define TEPDITILEINDEXER_HPP
    00027
    00028 #include "../kernel/TeGeometry.h"
    00029 #include "../kernel/TeAgnostic.h"
    00030
    00031 #include <algorithm>
    00032
    00033 #include <math.h>
    00034
    00041 class PDI_DLL TePDITileIndexer
    00042 {
    00043   public :
    00044
    00048     typedef vector< pair< unsigned int, unsigned int > > TeTileSegIndex;
    00049
    00050   protected:
    00051
    00053     const TePolygon& refPol_;
    00054
    00056     double dy_;
    00057
    00059     std::vector<TeTileSegIndex*> tileIndex_;
    00060
    00061     TePDITileIndexer( const TePDITileIndexer& );
    00062
    00064     const TePDITileIndexer& operator=( const TePDITileIndexer& );
    00065
    00073     void getTileIndex(const TeCoord2D& c1, const TeCoord2D& c2,
    00074       unsigned int& firstTile, unsigned int& lastTile) const
    00075     {
    00076       TEAGN_DEBUG_CONDITION( ( dy_ > 0 ),
    00077         "Invalid tile indexer Y resolution" );
    00078
    00079       double lowerY = ((c1.y_ < c2.y_) ? c1.y_ : c2.y_);
    00080       double upperY = ((c1.y_ > c2.y_) ? c1.y_ : c2.y_);
    00081
    00082       firstTile = int((lowerY - refPol_.box().y1()) / dy_);
    00083       lastTile  = int((upperY - refPol_.box().y1()) / dy_);
    00084     }
    00085
    00087
    00091     void getTileIndex(const double& y, unsigned int& tileIndex) const
    00092     {
    00093       TEAGN_DEBUG_CONDITION( ( dy_ > 0 ),
    00094         "Invalid tile indexer Y resolution" );
    00095
    00096       tileIndex = int( (y - refPol_.box().y1() ) / dy_ );
    00097     }
    00098
    00102     void init()
    00103     {
    00104       dy_ = 0;
    00105     }
    00106
    00107   public:
    00108
    00110
    00114     TePDITileIndexer( const TePolygon& pol, const double& dy )
    00115       : refPol_( pol )
    00116     {
    00117       TEAGN_DEBUG_CONDITION( ( dy > 0 ),
    00118         "Invalid tile index dy" );
    00119
    00120       init();
    00121
    00122       dy_ = dy;
    00123
    00124       /* Building new index */
    00125
    00126       if( refPol_.size() > 0 ) {
    00127         TEAGN_DEBUG_CONDITION( ( dy_ > 0 ),
    00128           "Invalid tile indexer Y resolution" );
    00129
    00130         unsigned int total_tiles_number = 1 +
    00131           ( (unsigned int)floor( refPol_.box().height() / dy_ ) );
    00132
    00133         for(unsigned int i = 0; i < total_tiles_number; ++i)
    00134         {
    00135           tileIndex_.push_back( new TeTileSegIndex );
    00136         }
    00137
    00138         const unsigned int rings_number = refPol_.size();
    00139
    00140         // for each polygon ring, we need to index its segments
    00141         for(unsigned int i = 0; i < rings_number; ++i)
    00142         {
    00143           addRing( i );
    00144         }
    00145       }
    00146     }
    00147
    00151     void clear()
    00152     {
    00153       vector<TeTileSegIndex*>::iterator it = tileIndex_.begin();
    00154       vector<TeTileSegIndex*>::iterator it_end = tileIndex_.end();
    00155
    00156       while( it != it_end ) {
    00157         delete ( *it );
    00158
    00159         ++it;
    00160       }
    00161
    00162       tileIndex_.clear();
    00163
    00164       init();
    00165     }
    00166
    00167     ~TePDITileIndexer()
    00168     {
    00169       clear();
    00170     }
    00171
    00173     void addRing( const unsigned int& ring_index )
    00174     {
    00175       TEAGN_DEBUG_CONDITION( ( ring_index < refPol_.size() ),
    00176         "Invalid ring_index" );
    00177
    00178       unsigned int num_points = refPol_[ring_index].size();
    00179
    00180       if( num_points > 0 ) {
    00181         unsigned int numSegments = num_points - 1;
    00182
    00183         for(unsigned int j = 0; j < numSegments; ++j)
    00184         {
    00185           // creates a pointer to the segment
    00186           pair<unsigned int, unsigned int> segPointer(
    00187             ring_index, j);
    00188
    00189           // finds the tiles that this segments intersects
    00190           unsigned int firstTileIndex = 0;
    00191           unsigned int lastTileIndex = 0;
    00192
    00193           getTileIndex( refPol_[ring_index][j],
    00194             refPol_[ring_index][j + 1],
    00195             firstTileIndex, lastTileIndex);
    00196
    00197           TEAGN_DEBUG_CONDITION(
    00198             ( firstTileIndex < tileIndex_.size() ),
    00199             "Invalid firstTileIndex" );
    00200           TEAGN_DEBUG_CONDITION(
    00201             ( lastTileIndex < tileIndex_.size() ),
    00202             "Invalid lastTileIndex" );
    00203
    00204           // associates the pointer segment to the tiles
    00205           for(unsigned int k = firstTileIndex; k <= lastTileIndex; ++k)
    00206           {
    00207             tileIndex_[k]->push_back(segPointer);
    00208           }
    00209         }
    00210       }
    00211     }
    00212
    00217     void getTile( const double& y, TeTileSegIndex** index ) const
    00218     {
    00219       unsigned int tidx;
    00220       getTileIndex( y, tidx );
    00221
    00222       if( tidx < tileIndex_.size() ) {
    00223         ( *index ) = tileIndex_[ tidx ];
    00224       } else {
    00225         ( *index ) = 0;
    00226       }
    00227     }
    00228
    00229     const TePolygon& getPol() const
    00230     {
    00231       return refPol_;
    00232     }
    00233
    00234     unsigned int getTilesNumber()
    00235     {
    00236       return (unsigned int)tileIndex_.size();
    00237     }
    00238
    00239 };
    00240
    00241
    00242 #endif

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines