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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
/**********************************************************************
* $Id: EdgeRing.cpp 1820 2006-09-06 16:54:23Z mloskot $
*
* GEOS - Geometry Engine Open Source
* http://geos.refractions.net
*
* Copyright (C) 2005-2006 Refractions Research Inc.
* Copyright (C) 2001-2002 Vivid Solutions Inc.
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************
*
* Last port: geomgraph/EdgeRing.java rev. 1.9
*
**********************************************************************/
#include <geos/util/Assert.h>
#include <geos/util/TopologyException.h>
#include <geos/algorithm/CGAlgorithms.h>
#include <geos/geomgraph/EdgeRing.h>
#include <geos/geomgraph/DirectedEdge.h>
#include <geos/geomgraph/DirectedEdgeStar.h>
#include <geos/geomgraph/Edge.h>
#include <geos/geomgraph/Node.h>
#include <geos/geomgraph/Label.h>
#include <geos/geomgraph/Position.h>
#include <geos/geom/CoordinateSequenceFactory.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/LinearRing.h>
#include <geos/geom/Location.h>
#include <geos/geom/Envelope.h>
#include <vector>
#include <cassert>
#include <iostream> // for operator<<
#ifndef GEOS_DEBUG
#define GEOS_DEBUG 0
#endif
#if GEOS_DEBUG
#include <iostream>
#endif
using namespace std;
using namespace geos::algorithm;
using namespace geos::geom;
namespace geos {
namespace geomgraph { // geos.geomgraph
EdgeRing::EdgeRing(DirectedEdge *newStart,
const GeometryFactory *newGeometryFactory)
:
startDe(newStart),
geometryFactory(newGeometryFactory),
holes(),
maxNodeDegree(-1),
edges(),
pts(newGeometryFactory->getCoordinateSequenceFactory()->create(NULL)),
label(Location::UNDEF), // new Label(Location::UNDEF)),
ring(NULL),
isHoleVar(false),
shell(NULL)
{
/*
* Commented out to fix different polymorphism in C++ (from Java)
* Make sure these calls are made by derived classes !
*/
//computePoints(start);
//computeRing();
#if GEOS_DEBUG
cerr << "EdgeRing[" << this << "] ctor" << endl;
#endif
testInvariant();
}
EdgeRing::~EdgeRing()
{
testInvariant();
/*
* If we constructed a ring, we did by transferring
* ownership of the CoordinateSequence, so it will be
* destroyed by `ring' dtor and we must not destroy
* it twice.
*/
if ( ring == NULL )
{
delete pts;
}
else
{
delete ring;
}
for(size_t i=0, n=holes.size(); i<n; ++i)
{
delete holes[i];
}
#if GEOS_DEBUG
cerr << "EdgeRing[" << this << "] dtor" << endl;
#endif
}
bool
EdgeRing::isIsolated()
{
testInvariant();
return (label.getGeometryCount()==1);
}
bool
EdgeRing::isHole()
{
testInvariant();
// We can't tell if this is an hole
// unless we computed the ring
// see computeRing()
assert(ring);
return isHoleVar;
}
LinearRing*
EdgeRing::getLinearRing()
{
testInvariant();
// return new LinearRing(*ring);
return ring;
}
Label&
EdgeRing::getLabel()
{
testInvariant();
return label;
}
bool
EdgeRing::isShell()
{
testInvariant();
return (shell==NULL);
}
EdgeRing*
EdgeRing::getShell()
{
testInvariant();
return shell;
}
void
EdgeRing::setShell(EdgeRing *newShell)
{
shell=newShell;
if (shell!=NULL) shell->addHole(this);
testInvariant();
}
void
EdgeRing::addHole(EdgeRing *edgeRing)
{
holes.push_back(edgeRing);
testInvariant();
}
/*public*/
Polygon*
EdgeRing::toPolygon(const GeometryFactory* geometryFactory)
{
testInvariant();
size_t nholes=holes.size();
vector<Geometry *> *holeLR=new vector<Geometry *>(nholes);
for (size_t i=0; i<nholes; ++i)
{
Geometry *hole=holes[i]->getLinearRing()->clone();
(*holeLR)[i]=hole;
}
// We don't use "clone" here because
// GeometryFactory::createPolygon really
// wants a LinearRing
//
LinearRing *shellLR=new LinearRing(*(getLinearRing()));
return geometryFactory->createPolygon(shellLR, holeLR);
}
/*public*/
void
EdgeRing::computeRing()
{
testInvariant();
if (ring!=NULL) return; // don't compute more than once
ring=geometryFactory->createLinearRing(pts);
isHoleVar=CGAlgorithms::isCCW(pts);
testInvariant();
}
/*public*/
vector<DirectedEdge*>&
EdgeRing::getEdges()
{
testInvariant();
return edges;
}
/*protected*/
void
EdgeRing::computePoints(DirectedEdge *newStart)
{
startDe=newStart;
DirectedEdge *de=newStart;
bool isFirstEdge=true;
do {
//util::Assert::isTrue(de!=NULL,"EdgeRing::computePoints: found null Directed Edge");
//assert(de!=NULL); // EdgeRing::computePoints: found null Directed Edge
if(de==NULL)
throw util::TopologyException(
"EdgeRing::computePoints: found null Directed Edge");
if (de->getEdgeRing()==this)
throw util::TopologyException(
"Directed Edge visited twice during ring-building",
de->getCoordinate());
edges.push_back(de);
Label *deLabel=de->getLabel();
assert(deLabel);
assert(deLabel->isArea());
mergeLabel(*deLabel);
addPoints(de->getEdge(),de->isForward(),isFirstEdge);
isFirstEdge=false;
setEdgeRing(de,this);
de=getNext(de);
} while (de!=startDe);
testInvariant();
}
/*public*/
int
EdgeRing::getMaxNodeDegree()
{
testInvariant();
if (maxNodeDegree<0) computeMaxNodeDegree();
return maxNodeDegree;
}
/*private*/
void
EdgeRing::computeMaxNodeDegree()
{
maxNodeDegree=0;
DirectedEdge *de=startDe;
do {
Node *node=de->getNode();
EdgeEndStar* ees = node->getEdges();
assert(dynamic_cast<DirectedEdgeStar*>(ees));
DirectedEdgeStar* des = static_cast<DirectedEdgeStar*>(ees);
int degree=des->getOutgoingDegree(this);
if (degree>maxNodeDegree) maxNodeDegree=degree;
de=getNext(de);
} while (de!=startDe);
maxNodeDegree *= 2;
testInvariant();
}
/*public*/
void
EdgeRing::setInResult()
{
DirectedEdge *de=startDe;
do {
de->getEdge()->setInResult(true);
de=de->getNext();
} while (de!=startDe);
testInvariant();
}
/*protected*/
void
EdgeRing::mergeLabel(Label& deLabel)
{
mergeLabel(deLabel, 0);
mergeLabel(deLabel, 1);
testInvariant();
}
/*protected*/
void
EdgeRing::mergeLabel(Label& deLabel, int geomIndex)
{
testInvariant();
int loc=deLabel.getLocation(geomIndex, Position::RIGHT);
// no information to be had from this label
if (loc==Location::UNDEF) return;
// if there is no current RHS value, set it
if (label.getLocation(geomIndex)==Location::UNDEF) {
label.setLocation(geomIndex,loc);
return;
}
}
/*protected*/
void
EdgeRing::addPoints(Edge *edge, bool isForward, bool isFirstEdge)
{
// EdgeRing::addPoints: can't add points after LinearRing construction
assert(ring==NULL);
assert(edge);
const CoordinateSequence* edgePts=edge->getCoordinates();
assert(edgePts);
size_t numEdgePts=edgePts->getSize();
assert(pts);
if (isForward) {
size_t startIndex=1;
if (isFirstEdge) startIndex=0;
for (size_t i=startIndex; i<numEdgePts; ++i)
{
pts->add(edgePts->getAt(i));
}
}
else { // is backward
size_t startIndex=numEdgePts-1;
if (isFirstEdge) startIndex=numEdgePts;
//for (int i=startIndex;i>=0;i--)
for (size_t i=startIndex; i>0; --i)
{
pts->add(edgePts->getAt(i-1));
}
}
testInvariant();
}
/*public*/
bool
EdgeRing::containsPoint(const Coordinate& p)
{
testInvariant();
assert(ring);
const Envelope* env=ring->getEnvelopeInternal();
assert(env);
if ( ! env->contains(p) ) return false;
if ( ! CGAlgorithms::isPointInRing(p, ring->getCoordinatesRO()) )
return false;
for (vector<EdgeRing*>::iterator i=holes.begin(); i<holes.end(); ++i)
{
EdgeRing *hole=*i;
assert(hole);
if (hole->containsPoint(p))
{
return false;
}
}
return true;
}
std::ostream& operator<< (std::ostream& os, const EdgeRing& er)
{
os << "EdgeRing[" << &er << "]: "
<< std::endl
<< "Points: " << er.pts
<< std::endl;
return os;
}
} // namespace geos.geomgraph
} // namespace geos
/**********************************************************************
* $Log$
* Revision 1.25 2006/07/08 00:33:54 strk
* * configure.in: incremented CAPI minor version, to avoid falling behind any future version from the 2.2. branch.
* * source/geom/Geometry.cpp, source/geom/GeometryFactory.cpp,
* source/geomgraph/EdgeRing.cpp,
* source/headers/geos/geom/Geometry.h,
* source/headers/geos/geom/GeometryFactory.h,
* source/headers/geos/geom/GeometryFactory.inl,
* source/headers/geos/geomgraph/EdgeRing.h:
* updated doxygen comments (sync with JTS head).
* * source/headers/geos/platform.h.in: include <inttypes.h>
* rather then <stdint.h>
*
* Revision 1.24 2006/06/12 11:29:23 strk
* unsigned int => size_t
*
* Revision 1.23 2006/04/06 09:41:55 strk
* Added operator<<, added pts!=NULL assertion in testInvariant() function
*
* Revision 1.22 2006/03/29 13:53:56 strk
* EdgeRing equipped with Invariant testing function and lots of exceptional assertions. Removed useless heap allocations, and pointers usages.
*
* Revision 1.21 2006/03/27 16:02:33 strk
* Added INL file for MinimalEdgeRing, added many debugging blocks,
* fixed memory leak in ConnectedInteriorTester (bug #59)
*
* Revision 1.20 2006/03/23 15:10:29 strk
* Dropped by-pointer TopologyException constructor, various small cleanups
*
**********************************************************************/
|