All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TileIndexer.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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/raster/TileIndexer.cpp
22 
23  \brief Polygon tile indexing class for optmized geometrical relational tests.
24 */
25 
26 // TerraLib
27 #include "../common/STLUtils.h"
28 #include "../geometry.h"
29 #include "TileIndexer.h"
30 
31 // STL
32 #include <iostream>
33 
35  unsigned int& firstTile, unsigned int& lastTile) const
36 {
37 // Invalid tile indexer Y resolution
38  assert(m_dy > 0);
39 
40  double lowerY = ((p1.getY() < p2.getY())? p1.getY(): p2.getY());
41  double upperY = ((p1.getY() > p2.getY())? p1.getY(): p2.getY());
42 
43  firstTile = int((lowerY - m_referencePolygon.getMBR()->getLowerLeftY()) / m_dy);
44  lastTile = int((upperY - m_referencePolygon.getMBR()->getLowerLeftY()) / m_dy);
45 }
46 
47 void te::rst::TileIndexer::getTileIndex(const double& y, unsigned int& tileIndex) const
48 {
49  assert(m_dy > 0);
50 
51  tileIndex = int( (y - m_referencePolygon.getMBR()->getLowerLeftY() ) / m_dy );
52 }
53 
55 {
56  m_dy = 0;
57 }
58 
60  : m_referencePolygon(pol)
61 {
62  assert(dy > 0);
63 
64  init();
65  m_dy = dy;
66 
67 // Building new index
69  {
70  unsigned int total_tiles_number = 1 +
71  ((unsigned int) floor(m_referencePolygon.getMBR()->getHeight() / m_dy));
72 
73  for(unsigned int i = 0; i < total_tiles_number; i++)
74  m_tileIndex.push_back(new TileSegIndex);
75 
76 // for each polygon ring, we need to index its segments
77  for(unsigned int i = 0; i < m_referencePolygon.getNumRings(); i++)
78  addRing(i);
79  }
80 }
81 
83 {
84  te::common::FreeContents(m_tileIndex);
85  m_tileIndex.clear();
86  init();
87 }
88 
90 {
91  clear();
92 }
93 
94 void te::rst::TileIndexer::addRing(const unsigned int& ri)
95 {
96  assert(ri < m_referencePolygon.getNumRings());
97 
98  unsigned int num_points = m_referencePolygon[ri]->getNPoints();
99 
100  if (num_points > 0)
101  {
102  unsigned int numSegments = num_points - 1;
103 
104  for(unsigned int j = 0; j < numSegments; j++)
105  {
106 // creates a pointer to the segment
107  std::pair<unsigned int, unsigned int> segPointer(ri, j);
108 
109 // finds the tiles that this segment intersects
110  unsigned int firstTileIndex = 0;
111  unsigned int lastTileIndex = 0;
112 
113  te::gm::Point* cj = (static_cast<te::gm::LinearRing*> (m_referencePolygon[ri]))->getPointN(j);
114  te::gm::Point* cjp1 = (static_cast<te::gm::LinearRing*> (m_referencePolygon[ri]))->getPointN(j + 1);
115  getTileIndex(*cj, *cjp1, firstTileIndex, lastTileIndex);
116 
117  assert(firstTileIndex < m_tileIndex.size());
118  assert(lastTileIndex < m_tileIndex.size());
119 
120 // associates the pointer segment to the tiles
121  for(unsigned int k = firstTileIndex; k <= lastTileIndex; k++)
122  m_tileIndex[k]->push_back(segPointer);
123  }
124  }
125 }
126 
127 void te::rst::TileIndexer::getTile(const double& y, TileSegIndex** index) const
128 {
129  unsigned int tidx;
130  getTileIndex(y, tidx);
131 
132  if (tidx < m_tileIndex.size())
133  (*index) = m_tileIndex[tidx];
134  else
135  (*index) = 0;
136 }
137 
139 {
140  return m_referencePolygon;
141 }
std::size_t getNumRings() const
It returns the number of rings in this CurvePolygon.
Definition: CurvePolygon.h:153
~TileIndexer()
Destructor.
Definition: TileIndexer.cpp:89
const te::gm::Polygon & m_referencePolygon
Reference polygon.
Definition: TileIndexer.h:128
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
Definition: Envelope.h:400
void getTileIndex(const te::gm::Point &p1, const te::gm::Point &p2, unsigned int &firstTile, unsigned int &lastTile) const
Gets tile index intervals in y direction for a given segment.
Definition: TileIndexer.cpp:34
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:150
A point with x and y coordinate values.
Definition: Point.h:50
Polygon tile indexing class for optmized geometrical relational tests.
double m_dy
Tile resolution along "y" axis.
Definition: TileIndexer.h:127
void getTile(const double &y, TileSegIndex **index) const
Gets tile index.
void addRing(const unsigned int &ri)
Update the tile index with the information of the supplied ring.
Definition: TileIndexer.cpp:94
void clear()
Clear all internal resources.
Definition: TileIndexer.cpp:82
TileIndexer(const TileIndexer &)
Constructor.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
std::vector< TileSegIndex * > m_tileIndex
Each tile segments index vector.
Definition: TileIndexer.h:129
std::vector< std::pair< unsigned int, unsigned int > > TileSegIndex
Definition: TileIndexer.h:59
const te::gm::Polygon & getPolygon() const
Returns the polygon.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
double getHeight() const
It returns the envelope height.
Definition: Envelope.h:448
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
Definition: Geometry.cpp:103
void init()
Init internal variables.
Definition: TileIndexer.cpp:54