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
|
/**********************************************************************
* $Id: Edge.cpp,v 1.10 2004/12/08 13:54:43 strk Exp $
*
* GEOS - Geometry Engine Open Source
* http://geos.refractions.net
*
* 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.
*
**********************************************************************/
#include <geos/geomgraph.h>
//#define DEBUG_INTERSECT 0
#ifndef DEBUG
#define DEBUG 0
#endif
namespace geos {
/**
* Updates an IM from the label for an edge.
* Handles edges from both L and A geometrys.
*/
void Edge::updateIM(Label *lbl,IntersectionMatrix *im){
im->setAtLeastIfValid(lbl->getLocation(0,Position::ON),lbl->getLocation(1,Position::ON),1);
if (lbl->isArea()) {
im->setAtLeastIfValid(lbl->getLocation(0,Position::LEFT),lbl->getLocation(1,Position::LEFT),2);
im->setAtLeastIfValid(lbl->getLocation(0,Position::RIGHT),lbl->getLocation(1,Position::RIGHT),2);
}
}
Edge::Edge(){
//cerr<<"["<<this<<"] Edge()"<<endl;
eiList=NULL;
isIsolatedVar=true;
depth=NULL;
depthDelta=0;
mce=NULL;
pts=NULL;
env=NULL;
}
Edge::~Edge(){
//cerr<<"["<<this<<"] ~Edge()"<<endl;
delete eiList;
delete depth;
delete mce;
delete pts;
delete env;
}
Edge::Edge(CoordinateSequence* newPts, Label *newLabel):GraphComponent(newLabel){
//cerr<<"["<<this<<"] Edge(CoordinateSequence *, Label *)"<<endl;
eiList=new EdgeIntersectionList(this);
isIsolatedVar=true;
depth=new Depth();
depthDelta=0;
// label=newLabel;
mce=NULL;
pts=newPts;
env=NULL;
}
Edge::Edge(CoordinateSequence* newPts){
//cerr<<"["<<this<<"] Edge(CoordinateSequence *)"<<endl;
eiList=new EdgeIntersectionList(this);
isIsolatedVar=true;
depth=new Depth();
depthDelta=0;
label=NULL;
mce=NULL;
pts=newPts;
env=NULL;
}
int Edge::getNumPoints() {
return pts->getSize();
}
void Edge::setName(string newName) {
name=newName;
}
//CoordinateSequence Edge::getCoordinates(){
// return pts;
//}
const CoordinateSequence* Edge::getCoordinates() const {
return pts;
}
const
Coordinate& Edge::getCoordinate(int i)
{
return pts->getAt(i);
}
// const or new return value ??
const
Coordinate& Edge::getCoordinate()
{
if (pts->getSize()>0) return pts->getAt(0);
auto_ptr<const Coordinate> ret(new Coordinate(DoubleNotANumber,DoubleNotANumber,DoubleNotANumber));
return *ret;
}
Depth*
Edge::getDepth()
{
return depth;
}
/*
* The depthDelta is the change in depth as an edge is crossed from R to L
* @return the change in depth as the edge is crossed from R to L
*/
int
Edge::getDepthDelta()
{
return depthDelta;
}
void
Edge::setDepthDelta(int newDepthDelta)
{
depthDelta=newDepthDelta;
}
int
Edge::getMaximumSegmentIndex()
{
return pts->getSize()-1;
}
EdgeIntersectionList*
Edge::getEdgeIntersectionList()
{
return eiList;
}
MonotoneChainEdge*
Edge::getMonotoneChainEdge()
{
if (mce==NULL) mce=new MonotoneChainEdge(this);
return mce;
}
bool
Edge::isClosed()
{
return pts->getAt(0)==pts->getAt(pts->getSize()-1);
}
/*
* An Edge is collapsed if it is an Area edge and it consists of
* two segments which are equal and opposite (eg a zero-width V).
*/
bool
Edge::isCollapsed()
{
if (!label->isArea()) return false;
if (pts->getSize()!= 3) return false;
if (pts->getAt(0)==pts->getAt(2) ) return true;
return false;
}
Edge*
Edge::getCollapsedEdge()
{
CoordinateSequence *newPts = new DefaultCoordinateSequence(2);
newPts->setAt(pts->getAt(0),0);
newPts->setAt(pts->getAt(1),1);
return new Edge(newPts, Label::toLineLabel(label));
}
void
Edge::setIsolated(bool newIsIsolated)
{
isIsolatedVar=newIsIsolated;
}
bool
Edge::isIsolated()
{
return isIsolatedVar;
}
/*
* Adds EdgeIntersections for one or both
* intersections found for a segment of an edge to the edge intersection list.
*/
void
Edge::addIntersections(LineIntersector *li, int segmentIndex, int geomIndex)
{
#if DEBUG
cerr<<"["<<this<<"] Edge::addIntersections("<<li->toString()<<", "<<segmentIndex<<", "<<geomIndex<<") called"<<endl;
#endif
for (int i=0; i<li->getIntersectionNum();i++) {
addIntersection(li,segmentIndex,geomIndex,i);
}
}
/*
* Add an EdgeIntersection for intersection intIndex.
* An intersection that falls exactly on a vertex of the edge is normalized
* to use the higher of the two possible segmentIndexes
*/
void
Edge::addIntersection(LineIntersector *li,int segmentIndex,int geomIndex,int intIndex)
{
#if DEBUG
cerr<<"["<<this<<"] Edge::addIntersection("<<li->toString()<<", "<<segmentIndex<<", "<<geomIndex<<", "<<intIndex<<") called"<<endl;
#endif
const Coordinate& intPt=li->getIntersection(intIndex);
int normalizedSegmentIndex=segmentIndex;
double dist=li->getEdgeDistance(geomIndex,intIndex);
// normalize the intersection point location
int nextSegIndex=normalizedSegmentIndex+1;
if (nextSegIndex<pts->getSize()) {
const Coordinate& nextPt=pts->getAt(nextSegIndex);
// Normalize segment index if intPt falls on vertex
// The check for point equality is 2D only - Z values are ignored
if (intPt.equals2D(nextPt)) {
normalizedSegmentIndex=nextSegIndex;
dist=0.0;
}
}
/*
* Add the intersection point to edge intersection list.
*/
#if DEBUG
cerr<<"Edge::addIntersection adding to edge intersection list point "<<intPt.toString()<<endl;
#endif
//EdgeIntersection *ei=eiList->add(intPt,normalizedSegmentIndex,dist);
eiList->add(intPt,normalizedSegmentIndex,dist);
}
/*
* Update the IM with the contribution for this component.
* A component only contributes if it has a labelling for both parent geometries
*/
void
Edge::computeIM(IntersectionMatrix *im)
{
updateIM(label,im);
}
/*
* equals is defined to be:
*
* e1 equals e2
* <b>iff</b>
* the coordinates of e1 are the same or the reverse of the coordinates in e2
*/
bool operator==(Edge e1, Edge e2)
{
if (e1.pts->getSize()!=e2.pts->getSize()) return false;
bool isEqualForward=true;
bool isEqualReverse=true;
int iRev=e1.pts->getSize();
for (int i=0; i<e1.pts->getSize();i++) {
if (!e1.pts->getAt(i).equals2D(e2.pts->getAt(i))) {
isEqualForward=false;
}
if (!e1.pts->getAt(i).equals2D(e2.pts->getAt(--iRev))) {
isEqualReverse=false;
}
if (!isEqualForward && !isEqualReverse) return false;
}
return true;
}
/*
* equals is defined to be:
* <p>
* e1 equals e2
* <b>iff</b>
* the coordinates of e1 are the same or the reverse of the coordinates in e2
*/
bool Edge::equals(Edge *e){
if (pts->getSize()!=e->pts->getSize()) return false;
bool isEqualForward=true;
bool isEqualReverse=true;
int iRev=pts->getSize();
for (int i=0; i<pts->getSize();i++) {
if (!pts->getAt(i).equals2D(e->pts->getAt(i))) {
isEqualForward=false;
}
if (!pts->getAt(i).equals2D(e->pts->getAt(--iRev))) {
isEqualReverse=false;
}
if (!isEqualForward && !isEqualReverse) return false;
}
return true;
}
/*
* @return true if the coordinate sequences of the Edges are identical
*/
bool
Edge::isPointwiseEqual(Edge *e)
{
#if DEBUG > 2
cerr<<"Edge::isPointwiseEqual call"<<endl;
#endif
if (pts->getSize()!=e->pts->getSize()) return false;
#if DEBUG
cerr<<"Edge::isPointwiseEqual scanning "<<e->pts->getSize()<<"x"<<pts->getSize()<<" points"<<endl;
#endif
for (int i=0;i<pts->getSize();i++) {
if (!pts->getAt(i).equals2D(e->pts->getAt(i))) {
return false;
}
}
return true;
}
string Edge::print(){
string out="edge " + name + ": ";
out+="LINESTRING (";
for(int i=0; i<pts->getSize();i++) {
if (i>0) out+=",";
out+=pts->getAt(i).toString();
}
out+=") ";
out+=label->toString();
out+=" ";
out+=depthDelta;
return out;
}
string Edge::printReverse(){
string out="edge " + name + ": ";
for(int i=pts->getSize()-1;i>=0;i--) {
out+=pts->getAt(i).toString() + " ";
}
out+="\n";
return out;
}
Envelope* Edge::getEnvelope(){
// compute envelope lazily
if (env==NULL) {
env=new Envelope();
for (int i = 0; i<pts->getSize(); i++) {
env->expandToInclude(pts->getAt(i));
}
}
return env;
}
}
/**********************************************************************
* $Log: Edge.cpp,v $
* Revision 1.10 2004/12/08 13:54:43 strk
* gcc warnings checked and fixed, general cleanups.
*
* Revision 1.9 2004/11/23 19:53:06 strk
* Had LineIntersector compute Z by interpolation.
*
* Revision 1.8 2004/11/22 12:59:25 strk
* Added debugging lines
*
* Revision 1.7 2004/11/01 16:43:04 strk
* Added Profiler code.
* Temporarly patched a bug in DoubleBits (must check drawbacks).
* Various cleanups and speedups.
*
* Revision 1.6 2004/10/20 17:32:14 strk
* Initial approach to 2.5d intersection()
*
* Revision 1.5 2004/07/21 09:55:24 strk
* CoordinateSequence::atLeastNCoordinatesOrNothing definition fix.
* Documentation fixes.
*
* Revision 1.4 2004/07/08 19:34:49 strk
* Mirrored JTS interface of CoordinateSequence, factory and
* default implementations.
* Added DefaultCoordinateSequenceFactory::instance() function.
*
* Revision 1.3 2004/07/02 13:28:26 strk
* Fixed all #include lines to reflect headers layout change.
* Added client application build tips in README.
*
* Revision 1.2 2004/06/16 13:13:25 strk
* Changed interface of SegmentString, now copying CoordinateSequence argument.
* Fixed memory leaks associated with this and MultiGeometry constructors.
* Other associated fixes.
*
* Revision 1.1 2004/03/19 09:48:45 ybychkov
* "geomgraph" and "geomgraph/indexl" upgraded to JTS 1.4
*
* Revision 1.14 2003/11/07 01:23:42 pramsey
* Add standard CVS headers licence notices and copyrights to all cpp and h
* files.
*
* Revision 1.13 2003/10/15 16:39:03 strk
* Made Edge::getCoordinates() return a 'const' value. Adapted code set.
*
**********************************************************************/
|