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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
|
/**********************************************************************
* $Id: ConvexHull.cpp 1820 2006-09-06 16:54:23Z mloskot $
*
* GEOS - Geometry Engine Open Source
* http://geos.refractions.net
*
* Copyright (C) 2001-2002 Vivid Solutions Inc.
* Copyright (C) 2005 2006 Refractions Research 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: algorithm/ConvexHull.java rev. 1.26 (JTS-1.7)
*
**********************************************************************/
#include <geos/algorithm/ConvexHull.h>
#include <geos/algorithm/CGAlgorithms.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/Point.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/LineString.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/CoordinateSequenceFactory.h>
#include <typeinfo>
#include <algorithm>
#ifndef GEOS_INLINE
# include "geos/algorithm/ConvexHull.inl"
#endif
using namespace geos::geom;
namespace geos {
namespace algorithm { // geos.algorithm
namespace {
/**
* This is the class used to sort in radial order.
* It's defined here as it's a static one.
*/
class RadiallyLessThen {
private:
const Coordinate *origin;
int
polarCompare(const Coordinate *o, const Coordinate *p,
const Coordinate *q)
{
double dxp=p->x-o->x;
double dyp=p->y-o->y;
double dxq=q->x-o->x;
double dyq=q->y-o->y;
int orient = CGAlgorithms::computeOrientation(*o, *p, *q);
if (orient == CGAlgorithms::COUNTERCLOCKWISE) return 1;
if (orient == CGAlgorithms::CLOCKWISE) return -1;
// points are collinear - check distance
double op = dxp * dxp + dyp * dyp;
double oq = dxq * dxq + dyq * dyq;
if (op < oq) {
return -1;
}
if (op > oq) {
return 1;
}
return 0;
}
public:
RadiallyLessThen(const Coordinate *c): origin(c) {}
bool operator() (const Coordinate *p1, const Coordinate *p2)
{
return ( polarCompare(origin, p1, p2) == -1 );
}
};
} // unnamed namespace
/* private */
CoordinateSequence *
ConvexHull::toCoordinateSequence(Coordinate::ConstVect &cv)
{
const CoordinateSequenceFactory *csf =
geomFactory->getCoordinateSequenceFactory();
// Create a new Coordinate::Vect for feeding it to
// the CoordinateSequenceFactory
Coordinate::Vect *vect=new Coordinate::Vect();
size_t n=cv.size();
vect->reserve(n); // avoid multiple reallocs
for (size_t i=0; i<n; ++i)
{
vect->push_back(*(cv[i])); // Coordinate copy
}
return csf->create(vect); // takes ownership of the vector
}
/* private */
void
ConvexHull::computeOctPts(const Coordinate::ConstVect &inputPts,
Coordinate::ConstVect &pts)
{
// Initialize all slots with first input coordinate
pts = Coordinate::ConstVect(8, inputPts[0]);
for (size_t i=1, n=inputPts.size(); i<n; ++i)
{
if (inputPts[i]->x < pts[0]->x) {
pts[0] = inputPts[i];
}
if (inputPts[i]->x - inputPts[i]->y < pts[1]->x - pts[1]->y) {
pts[1] = inputPts[i];
}
if (inputPts[i]->y > pts[2]->y) {
pts[2] = inputPts[i];
}
if (inputPts[i]->x + inputPts[i]->y > pts[3]->x + pts[3]->y) {
pts[3] = inputPts[i];
}
if (inputPts[i]->x > pts[4]->x) {
pts[4] = inputPts[i];
}
if (inputPts[i]->x - inputPts[i]->y > pts[5]->x - pts[5]->y) {
pts[5] = inputPts[i];
}
if (inputPts[i]->y < pts[6]->y) {
pts[6] = inputPts[i];
}
if (inputPts[i]->x + inputPts[i]->y < pts[7]->x + pts[7]->y) {
pts[7] = inputPts[i];
}
}
}
/* private */
bool
ConvexHull::computeOctRing(const Coordinate::ConstVect &inputPts,
Coordinate::ConstVect &dest)
{
computeOctPts(inputPts, dest);
// Remove consecutive equal Coordinates
// unique() returns an iterator to the end of the resulting
// sequence, we erase from there to the end.
dest.erase( unique(dest.begin(),dest.end()), dest.end() );
// points must all lie in a line
if ( dest.size() < 3 ) return false;
// close ring
dest.push_back(dest[0]);
return true;
}
/* private */
void
ConvexHull::reduce(Coordinate::ConstVect &pts)
{
Coordinate::ConstVect polyPts;
if ( ! computeOctRing(pts, polyPts) ) {
// unable to compute interior polygon for some reason
return;
}
// add points defining polygon
Coordinate::ConstSet reducedSet;
reducedSet.insert(polyPts.begin(), polyPts.end());
/**
* Add all unique points not in the interior poly.
* CGAlgorithms.isPointInRing is not defined for points
* actually on the ring, but this doesn't matter since
* the points of the interior polygon are forced to be
* in the reduced set.
*
* @@TIP: there should be a std::algo for this
*/
for (size_t i=0, n=pts.size(); i<n; ++i)
{
if ( !CGAlgorithms::isPointInRing(*(pts[i]), polyPts) )
{
reducedSet.insert(pts[i]);
}
}
inputPts.assign(reducedSet.begin(), reducedSet.end());
}
Geometry*
ConvexHull::getConvexHull()
{
size_t nInputPts=inputPts.size();
if (nInputPts==0) // Return an empty geometry
return geomFactory->createEmptyGeometry();
if (nInputPts==1) // Return a Point
{
// Copy the Coordinate from the ConstVect
return geomFactory->createPoint(*(inputPts[0]));
}
if (nInputPts==2) // Return a LineString
{
// Copy all Coordinates from the ConstVect
CoordinateSequence *cs = toCoordinateSequence(inputPts);
return geomFactory->createLineString(cs);
}
// use heuristic to reduce points, if large
if (nInputPts > 50 )
{
reduce(inputPts);
}
// sort points for Graham scan.
preSort(inputPts);
// Use Graham scan to find convex hull.
Coordinate::ConstVect cHS;
grahamScan(inputPts, cHS);
return lineOrPolygon(cHS);
}
/* private */
void
ConvexHull::preSort(Coordinate::ConstVect &pts)
{
// find the lowest point in the set. If two or more points have
// the same minimum y coordinate choose the one with the minimum x.
// This focal point is put in array location pts[0].
for(size_t i=1, n=pts.size(); i<n; ++i)
{
const Coordinate *p0=pts[0]; // this will change
const Coordinate *pi=pts[i];
if ( (pi->y<p0->y) || ((pi->y==p0->y) && (pi->x<p0->x)) )
{
const Coordinate *t=p0;
pts[0]=pi;
pts[i]=t;
}
}
// sort the points radially around the focal point.
sort(pts.begin(), pts.end(), RadiallyLessThen(pts[0]));
}
/*private*/
void
ConvexHull::grahamScan(const Coordinate::ConstVect &c,
Coordinate::ConstVect &ps)
{
ps.push_back(c[0]);
ps.push_back(c[1]);
ps.push_back(c[2]);
for(size_t i=3, n=c.size(); i<n; ++i)
{
const Coordinate *p = ps.back(); ps.pop_back();
while (CGAlgorithms::computeOrientation(*(ps.back()),
*p, *(c[i])) > 0)
{
p = ps.back(); ps.pop_back();
}
ps.push_back(p);
ps.push_back(c[i]);
}
ps.push_back(c[0]);
}
///*
// * Returns a pointer to input, modifying input
// */
//CoordinateSequence*
//ConvexHull::preSort(CoordinateSequence *pts)
//{
// Coordinate t;
//
// // find the lowest point in the set. If two or more points have
// // the same minimum y coordinate choose the one with the minimu x.
// // This focal point is put in array location pts[0].
// size_t npts=pts->getSize();
// for(size_t i=1; i<npts; ++i)
// {
// const Coordinate &p0=pts->getAt(0); // this will change
// const Coordinate &pi=pts->getAt(i);
// if ( (pi.y<p0.y) || ((pi.y==p0.y) && (pi.x<p0.x)) )
// {
// t=p0;
// pts->setAt(pi, 0);
// pts->setAt( t, i);
// }
// }
// // sort the points radially around the focal point.
// radialSort(pts);
// return pts;
//}
///*
// * Returns a newly allocated CoordinateSequence object
// */
//CoordinateSequence*
//ConvexHull::grahamScan(const CoordinateSequence *c)
//{
// const Coordinate *p;
//
// vector<Coordinate> *ps=new vector<Coordinate>();
// ps->push_back(c->getAt(0));
// ps->push_back(c->getAt(1));
// ps->push_back(c->getAt(2));
//
// p=&(c->getAt(2));
// size_t npts=c->getSize();
// for(size_t i=3; i<npts; ++i)
// {
// p=&(ps->back());
// ps->pop_back();
// while (CGAlgorithms::computeOrientation(ps->back(), *p, c->getAt(i)) > 0)
// {
// p=&(ps->back());
// ps->pop_back();
// }
// ps->push_back(*p);
// ps->push_back(c->getAt(i));
// }
// ps->push_back(c->getAt(0));
//
// return geomFactory->getCoordinateSequenceFactory()->create(ps);
//}
//void
//ConvexHull::radialSort(CoordinateSequence *p)
//{
// // A selection sort routine, assumes the pivot point is
// // the first point (i.e., p[0]).
//
// const Coordinate &p0=p->getAt(0); // the pivot point
//
// Coordinate t;
// size_t npts=p->getSize();
// for(size_t i=1; i<npts-1; ++i)
// {
// size_t min=i;
//
// for(size_t j=i+1; j<npts; ++j)
// {
// const Coordinate &pj=p->getAt(j);
//
// if ( polarCompare(p0, pj, p->getAt(min)) < 0 )
// {
// min=j;
// }
// }
//
// /*
// * Swap point[i] and point[min]
// * We can skip this if they have
// * the same value
// */
// if ( i != min )
// {
// t=p->getAt(i);
// p->setAt(p->getAt(min), i);
// p->setAt(t, min);
// }
// }
//}
/*private*/
bool
ConvexHull::isBetween(const Coordinate &c1, const Coordinate &c2, const Coordinate &c3)
{
if (CGAlgorithms::computeOrientation(c1, c2, c3)!=0) {
return false;
}
if (c1.x!=c3.x) {
if (c1.x<=c2.x && c2.x<=c3.x) {
return true;
}
if (c3.x<=c2.x && c2.x<=c1.x) {
return true;
}
}
if (c1.y!=c3.y) {
if (c1.y<=c2.y && c2.y<=c3.y) {
return true;
}
if (c3.y<=c2.y && c2.y<=c1.y) {
return true;
}
}
return false;
}
//void
//ConvexHull::makeBigQuad(const CoordinateSequence *pts, BigQuad &bigQuad)
//{
// bigQuad.northmost=bigQuad.southmost=
// bigQuad.westmost=bigQuad.eastmost=pts->getAt(0);
//
// size_t npts=pts->getSize();
// for (size_t i=1; i<npts; ++i)
// {
// const Coordinate &pi=pts->getAt(i);
//
// if (pi.x<bigQuad.westmost.x)
// bigQuad.westmost=pi;
//
// if (pi.x>bigQuad.eastmost.x)
// bigQuad.eastmost=pi;
//
// if (pi.y<bigQuad.southmost.y)
// bigQuad.southmost=pi;
//
// if (pi.y>bigQuad.northmost.y)
// bigQuad.northmost=pi;
// }
//}
/* private */
Geometry*
ConvexHull::lineOrPolygon(const Coordinate::ConstVect &input)
{
Coordinate::ConstVect cleaned;
cleanRing(input, cleaned);
if (cleaned.size()==3) { // shouldn't this be 2 ??
cleaned.resize(2);
CoordinateSequence *cl1=toCoordinateSequence(cleaned);
LineString *ret = geomFactory->createLineString(cl1);
return ret;
}
CoordinateSequence *cl2=toCoordinateSequence(cleaned);
LinearRing *linearRing=geomFactory->createLinearRing(cl2);
return geomFactory->createPolygon(linearRing,NULL);
}
/*private*/
void
ConvexHull::cleanRing(const Coordinate::ConstVect &original,
Coordinate::ConstVect &cleaned)
{
size_t npts=original.size();
const Coordinate *last = original[npts-1];
//util::Assert::equals(*(original[0]), *last);
assert(last);
assert(original[0]->equals2D(*last));
const Coordinate *prev = NULL;
for (size_t i=0; i<npts-1; ++i)
{
const Coordinate *curr = original[i];
const Coordinate *next = original[i+1];
// skip consecutive equal coordinates
if (curr->equals2D(*next)) continue;
if ( prev != NULL && isBetween(*prev, *curr, *next) )
{
continue;
}
cleaned.push_back(curr);
prev=curr;
}
cleaned.push_back(last);
}
///**
// * @param vertices the vertices of a linear ring, which may or may not be
// * flattened (i.e. vertices collinear)
// * @return a newly allocated CoordinateSequence
// */
//CoordinateSequence*
//ConvexHull::cleanRing(const CoordinateSequence *original)
//{
// Assert::equals(original->getAt(0),original->getAt(original->getSize()-1));
//
// size_t npts=original->getSize();
//
// vector<Coordinate> *newPts=new vector<Coordinate>;
// newPts->reserve(npts);
//
// const Coordinate *previousDistinctCoordinate=NULL;
// for(size_t i=0; i<npts-1; ++i)
// {
// const Coordinate ¤tCoordinate=original->getAt(i);
// const Coordinate &nextCoordinate=original->getAt(i+1);
//
// // skip repeated points (shouldn't this have been already done elsewhere?)
// if (currentCoordinate==nextCoordinate) continue;
//
// // skip collinear point
// if (previousDistinctCoordinate!=NULL &&
// isBetween(*previousDistinctCoordinate, currentCoordinate, nextCoordinate))
// {
// continue;
// }
//
// newPts->push_back(currentCoordinate);
//
// previousDistinctCoordinate=¤tCoordinate;
// }
//
// newPts->push_back(original->getAt(npts-1));
//
// CoordinateSequence *cleanedRing=geomFactory->getCoordinateSequenceFactory()->create(newPts);
// return cleanedRing;
//}
} // namespace geos.algorithm
} // namespace geos
/**********************************************************************
* $Log$
* Revision 1.22 2006/06/12 10:49:43 strk
* unsigned int => size_t
*
* Revision 1.21 2006/03/24 09:52:41 strk
* USE_INLINE => GEOS_INLINE
*
* Revision 1.20 2006/03/21 11:12:23 strk
* Cleanups: headers inclusion and Log section
*
* Revision 1.19 2006/03/09 16:46:45 strk
* geos::geom namespace definition, first pass at headers split
**********************************************************************/
|