/usr/include/terralib/kernel/TeGridIndex.h is in libterralib-dev 4.0.0-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | /************************************************************************************
TerraLib - a library for developing GIS applications.
Copyright © 2001-2007 INPE and Tecgraf/PUC-Rio.
This code is part of the TerraLib library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
You should have received a copy of the GNU Lesser General Public
License along with this library.
The authors reassure the license terms regarding the warranties.
They specifically disclaim any warranties, including, but not limited to,
the implied warranties of merchantability and fitness for a particular purpose.
The library provided hereunder is on an "as is" basis, and the authors have no
obligation to provide maintenance, support, updates, enhancements, or modifications.
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
indirect, special, incidental, or consequential damages arising out of the use
of this library and its documentation.
*************************************************************************************/
/*! \file TeGridIndex.h
\brief This file contains an implementation of a fixed grid data structure.
*/
#ifndef __TERRALIB_INTERNAL_GRIDINDEX_H
#define __TERRALIB_INTERNAL_GRIDINDEX_H
#include "TeGeometry.h"
#include "TeIntersector.h"
#include <map>
/*
* WARNING:
*
* 1. This module IS UNDER DEVELOPMENT!
*
* 2. These data structures means to be used only in TerraLib kernel and should not be used by anyone because
* the support and interfaces can be changed in future.
*
*/
namespace TeSAM
{
/** @defgroup GridStructure Fixed Grid Data Structure
* TerraLib Fixed Grid Data Structure.
* @{
*/
//! A class that represents a fixed grid
/*!
.
WARNING:
1. XXXX
2. XXXX.
3.
*/
class TL_DLL TeGridIndex
{
public:
//! grid column/line cell index
typedef pair<int, int> gridKey;
//! grid structure
typedef multimap<gridKey, unsigned int> gridType; // em cada linha e coluna podemos associar uma lista de outros objetos
double dx_; //!< grid resolution in x axis.
double dy_; //!< grid resolution in y axis.
int npartsX_; //!< Number of columns
int npartsY_; //!< Number of lines
double xi_; //!< Lower left x.
double yi_; //!< Lower left y.
double xf_; //!< Upper right x.
double yf_; //!< Upper right y.
gridType grid_; //!< the grid.
unsigned int size_; //!< Number of elements in the grid (if one element intersects more than one grid cell, they will count many times).
public:
//! Empty constructor
TeGridIndex()
: dx_(0.0), dy_(0.0), npartsX_(0), npartsY_(0), xi_(0.0), yi_(0.0), xf_(0.0), yf_(0.0), size_(0u)
{
}
//! Constructor of the grid
TeGridIndex(const TeBox& b, const double& resX, const double& resY)
: size_(0u)
{
setBox(b);
setResolution(resX, resY);
}
//! Adjusts grid bounding box
void setBox(const TeBox& b)
{
xi_ = b.x1_;
yi_ = b.y1_;
xf_ = b.x2_;
yf_ = b.y2_;
}
//! Returns the number of associatade cells
unsigned int size() const
{
return size_;
}
//! Sets grid resolution factor
void setResolution(const double& resX, const double& resY)
{
dx_ = resX;
dy_ = resY;
if((dx_ <= 0.0) || (dy_ <= 0.0))
{
npartsX_ = 0;
npartsY_ = 0;
}
else
{
npartsX_ = TeRound((xf_ - xi_) / dx_) + 1;
npartsY_ = TeRound((yf_ - yi_) / dy_) + 1;
}
}
//! Return number of columns
int getNumCols() const
{
return npartsX_;
}
//! Return number of lines
int getNumLines() const
{
return npartsY_;
}
//! Tells if the grid definition is correct
bool isValid() const
{
return (npartsX_ != 0 && npartsY_ != 0);
}
//! Inserts an item with one coordinate
void insert(const TeCoord2D& c, const unsigned int& itemId)
{
int col = int((c.x_ - xi_) / dx_);
int line = int((c.y_ - yi_) / dy_);
gridKey key(col, line);
grid_.insert(gridType::value_type(key, itemId));
++size_;
}
//! Returns lower and upper grid positions
void getIndex(const TeCoord2D& c1, const TeCoord2D& c2, int& col1, int& line1, int& col2, int& line2)
{
double lowerX = ((c1.x_ < c2.x_) ? c1.x_ : c2.x_);
double lowerY = ((c1.y_ < c2.y_) ? c1.y_ : c2.y_);
double upperX = ((c1.x_ > c2.x_) ? c1.x_ : c2.x_);
double upperY = ((c1.y_ > c2.y_) ? c1.y_ : c2.y_);
col1 = int((lowerX - xi_) / dx_);
line1 = int((lowerY - yi_) / dy_);
col2 = int((upperX - xi_) / dx_) + 1;
line2 = int((upperY - yi_) / dy_) + 1;
}
//! Inserts intem into the grid
void insert(const TeCoord2D& c1, const TeCoord2D& c2, const unsigned int& itemId)
{
gridKey key(0, 0);
int col1;
int line1;
int col2;
int line2;
getIndex(c1, c2, col1, line1, col2, line2);
for(int i = col1; i <= col2; ++i)
{
key.first = i;
for(int j = line1; j <= line2; ++j)
{
key.second = j;
grid_.insert(gridType::value_type(key, itemId));
++size_;
}
}
}
//! Put line segments over the grid cells
void indexLine(const TeLine2D& l)
{
unsigned int nstep = l.size() - 1;
for(unsigned int i = 0; i < nstep; ++i)
insert(l[i], l[i+1], i);
}
//! Return a reference for internal grid representation
gridType& getGrid()
{
return grid_;
}
//! Only for debug purpose
void getLines(TeLineSet& lset)
{
for(int i = 0; i <= npartsX_; ++i)
{
TeCoord2D c1, c2;
c1.x_ = xi_ + dx_ * i;
c1.y_ = yi_;
c2.x_ = xi_ + dx_ * i;
c2.y_ = yi_ + npartsY_ * dy_;
TeLine2D l;
l.add(c1); l.add(c2);
lset.add(l);
}
for(int j = 0; j <= npartsY_; ++j)
{
TeCoord2D c1, c2;
c1.x_ = xi_;
c1.y_ = yi_ + dy_ * j;
c2.x_ = xi_ + npartsX_ * dx_;
c2.y_ = yi_ + dy_ * j;
TeLine2D l;
l.add(c1); l.add(c2);
lset.add(l);
}
}
private:
//! No copy allowed
TeGridIndex(const TeGridIndex& rhs);
//! No copy allowed
TeGridIndex& operator=(const TeGridIndex& rhs);
}; // end of class TeGridIndex
//! Get average length os segments in each direction
TL_DLL inline void getAverage(const TeLine2D& l, double& dx, double& dy)
{
unsigned int nstep = l.size() - 1;
double sumX = 0.0;
double sumY = 0.0;
for(unsigned int i = 0; i < nstep; ++i)
{
sumX += fabs(l[i + 1].x_ - l[i].x_);
sumY += fabs(l[i + 1].y_ - l[i].y_);
}
dx = (sumX / double(nstep)) * 8.0;
dy = (sumY / double(nstep)) * 8.0;
return;
}
//! Computes intersections
TL_DLL inline bool TeSafeIntersectionsGrid(const TeLine2D& redLine, const TeLine2D& blueLine, TeINTERSECTOR2::TeVectorBoundaryIP& report, const unsigned int& redObjId = 0, const unsigned int& blueObjId = 0)
{
double dx = 0.0;
double dy = 0.0;
getAverage(redLine, dx, dy);
TeGridIndex redGrid(redLine.box(), dx, dy);
redGrid.indexLine(redLine);
TeGridIndex::gridKey key(0, 0);
int colmax = redGrid.getNumCols();
int colmin = 0;
int linemax = redGrid.getNumLines();
int linemin = 0;
int col1 = 0;
int col2 = 0;
int line1 = 0;
int line2 = 0;
bool hasIntersections = false;
TeSegmentIntersectionType t = TeImproperIntersection;
// Para cada segmento azul, verificar os quadrantes interceptados
unsigned int nStep = blueLine.size() - 1;
for(unsigned int k = 0; k < nStep; ++k)
{
redGrid.getIndex(blueLine[k], blueLine[k+1], col1, line1, col2, line2);
if((col2 < colmin) || (line2 < linemin) || (col1 > colmax) || (line1 > linemax))
continue;
//if(((col1 > colmax) && (col2 > colmax)) || ((line1 > linemax) && (line2 > linemax)))
// continue;
if(col1 < colmin)
col1 = colmin;
if(col2 > colmax)
col2 = colmax;
if(line1 < linemin)
line1 = linemin;
if(line2 > linemax)
line2 = linemax;
for(int col = col1; col <= col2; ++col)
for(int lin = line1; lin <= line2; ++lin)
{
key.first = col;
key.second = lin;
pair<TeGridIndex::gridType::iterator, TeGridIndex::gridType::iterator> its = redGrid.getGrid().equal_range(key);
while(its.first != its.second)
{
TeINTERSECTOR2::TeBoundaryIP ip;
if(TeINTERSECTOR2::TeIntersection(redLine[its.first->second], redLine[its.first->second + 1], blueLine[k], blueLine[k+1], ip, t))
{
hasIntersections = true;
ip.redPartNum_ = redObjId;
ip.redSegNum_ = its.first->second;
ip.bluePartNum_ = blueObjId;
ip.blueSegNum_ = k;
if(ip.coords_.size() == 2) //overlap
{
// Verificar se os pontos estão em ordem crescente
if(ip.coords_[0].x_ < ip.coords_[1].x_)
{
report.push_back(ip);
}
else if(ip.coords_[0].x_ > ip.coords_[1].x_)
{
swap(ip.coords_[0], ip.coords_[1]);
report.push_back(ip);
}
else if(ip.coords_[0].y_ < ip.coords_[1].y_)
{
report.push_back(ip);
}
else
{
swap(ip.coords_[0], ip.coords_[1]);
report.push_back(ip);
}
}
else
{
report.push_back(ip);
}
}
++(its.first);
}
}
}
return hasIntersections;
}
/** @} */
}; // end namespace TeSAM
#endif // __TERRALIB_INTERNAL_GRIDINDEX_H
|