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
|
/**********************************************************************
* $Id: FuzzyPointLocator.cpp 1941 2006-12-13 10:55:55Z strk $
*
* GEOS - Geometry Engine Open Source
* http://geos.refractions.net
*
* Copyright (C) 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: operation/overlay/validate/FuzzyPointLocator.java rev. 1.1
* (we should move in GEOS too, probably)
*
**********************************************************************/
#include <geos/operation/overlay/FuzzyPointLocator.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/Point.h> // for Point upcast
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/Location.h> // for Location::Value enum
#include <cassert>
#include <functional>
#include <vector>
#include <sstream>
#include <memory> // for auto_ptr
#ifndef GEOS_DEBUG
#define GEOS_DEBUG 0
#endif
#define COMPUTE_Z 1
#define USE_ELEVATION_MATRIX 1
#define USE_INPUT_AVGZ 0
using namespace std;
using namespace geos::geom;
using namespace geos::algorithm;
namespace geos {
namespace operation { // geos.operation
namespace overlay { // geos.operation.overlay
FuzzyPointLocator::FuzzyPointLocator(const geom::Geometry& geom,
double nTolerance)
:
g(geom),
tolerance(nTolerance),
ptLocator(),
linework(extractLineWork(g))
{
}
/*private*/
std::auto_ptr<Geometry>
FuzzyPointLocator::extractLineWork(const geom::Geometry& geom)
{
vector<Geometry*>* lineGeoms = new vector<Geometry*>();
try { // geoms array will leak if an exception is thrown
for (size_t i=0, n=g.getNumGeometries(); i<n; ++i)
{
const Geometry* gComp = g.getGeometryN(i);
Geometry* lineGeom = NULL;
// only get linework for polygonal components
if (gComp->getDimension() == 2) {
lineGeom = gComp->getBoundary();
lineGeoms->push_back(lineGeom);
}
}
return std::auto_ptr<Geometry>(g.getFactory()->buildGeometry(lineGeoms));
} catch (...) { // avoid leaks
for (size_t i=0, n=lineGeoms->size(); i<n; ++i)
{
delete (*lineGeoms)[i];
}
delete lineGeoms;
throw;
}
}
/*private*/
std::auto_ptr<Geometry>
FuzzyPointLocator::getLineWork(const geom::Geometry& geom)
{
vector<Geometry*>* lineGeoms = new vector<Geometry*>();
try { // geoms array will leak if an exception is thrown
for (size_t i=0, n=g.getNumGeometries(); i<n; ++i)
{
const Geometry* gComp = g.getGeometryN(i);
Geometry* lineGeom;
if (gComp->getDimension() == 2) {
lineGeom = gComp->getBoundary();
}
else {
lineGeom = gComp->clone();
}
lineGeoms->push_back(lineGeom);
}
return std::auto_ptr<Geometry>(g.getFactory()->buildGeometry(lineGeoms));
} catch (...) { // avoid leaks
for (size_t i=0, n=lineGeoms->size(); i<n; ++i)
{
delete (*lineGeoms)[i];
}
delete lineGeoms;
throw;
}
}
/* public */
Location::Value
FuzzyPointLocator::getLocation(const Coordinate& pt)
{
auto_ptr<Geometry> point(g.getFactory()->createPoint(pt));
double dist = linework->distance(point.get());
// if point is close to boundary, it is considered
// to be on the boundary
if (dist < tolerance)
return Location::BOUNDARY;
// now we know point must be clearly inside or outside geometry,
// so return actual location value
// (the static_cast is needed because PointLocator doesn't cleanly
// return a Location::Value - it should !!)
return static_cast<Location::Value>(ptLocator.locate(pt, &g));
}
} // namespace geos.operation.overlay
} // namespace geos.operation
} // namespace geos
/**********************************************************************
* $Log$
**********************************************************************/
|