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
|
/**********************************************************************
* $Id: InteriorPointLine.cpp 1986 2007-06-08 15:27:42Z 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.
*
**********************************************************************
*
**********************************************************************/
#include <geos/algorithm/InteriorPointLine.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/GeometryCollection.h>
#include <geos/geom/LineString.h>
#include <geos/geom/CoordinateSequence.h>
#ifndef GEOS_DEBUG
#define GEOS_DEBUG 0
#endif
#ifdef GEOS_DEBUG
#include <iostream>
#endif
using namespace geos::geom;
namespace geos {
namespace algorithm { // geos.algorithm
InteriorPointLine::InteriorPointLine(const Geometry *g)
{
minDistance=DoubleInfinity;
hasInterior=false;
if ( g->getCentroid(centroid) )
{
#if GEOS_DEBUG
std::cerr << "Centroid: " << centroid << std::endl;
#endif
addInterior(g);
if (!hasInterior) addEndpoints(g);
}
}
InteriorPointLine::~InteriorPointLine()
{
}
/* private
*
* Tests the interior vertices (if any)
* defined by a linear Geometry for the best inside point.
* If a Geometry is not of dimension 1 it is not tested.
* @param geom the geometry to add
*/
void
InteriorPointLine::addInterior(const Geometry *geom)
{
const LineString *ls = dynamic_cast<const LineString*>(geom);
if ( ls ) {
addInterior(ls->getCoordinatesRO());
return;
}
const GeometryCollection *gc = dynamic_cast<const GeometryCollection*>(geom);
if ( gc )
{
for(std::size_t i=0, n=gc->getNumGeometries(); i<n; i++) {
addInterior(gc->getGeometryN(i));
}
}
}
void
InteriorPointLine::addInterior(const CoordinateSequence *pts)
{
const std::size_t n=pts->getSize()-1;
for(std::size_t i=1; i<n; ++i)
{
add(pts->getAt(i));
}
}
/* private
*
* Tests the endpoint vertices
* defined by a linear Geometry for the best inside point.
* If a Geometry is not of dimension 1 it is not tested.
* @param geom the geometry to add
*/
void
InteriorPointLine::addEndpoints(const Geometry *geom)
{
const LineString *ls = dynamic_cast<const LineString*>(geom);
if ( ls ) {
addEndpoints(ls->getCoordinatesRO());
return;
}
const GeometryCollection *gc = dynamic_cast<const GeometryCollection*>(geom);
if ( gc )
{
for(std::size_t i=0, n=gc->getNumGeometries(); i<n; i++) {
addEndpoints(gc->getGeometryN(i));
}
}
}
void
InteriorPointLine::addEndpoints(const CoordinateSequence *pts)
{
add(pts->getAt(0));
add(pts->getAt(pts->getSize()-1));
}
/*private*/
void
InteriorPointLine::add(const Coordinate& point)
{
double dist=point.distance(centroid);
#if GEOS_DEBUG
std::cerr << "point " << point << " dist " << dist << ", minDistance " << minDistance << std::endl;
#endif
if (!hasInterior || dist<minDistance) {
interiorPoint=point;
#if GEOS_DEBUG
std::cerr << " is new InteriorPoint" << std::endl;
#endif
minDistance=dist;
hasInterior=true;
}
}
bool
InteriorPointLine::getInteriorPoint(Coordinate& ret) const
{
if ( ! hasInterior ) return false;
ret=interiorPoint;
return true;
}
} // namespace geos.algorithm
} // namespace geos
/**********************************************************************
* $Log$
* Revision 1.18 2006/03/21 11:12:23 strk
* Cleanups: headers inclusion and Log section
*
* Revision 1.17 2006/03/09 16:46:45 strk
* geos::geom namespace definition, first pass at headers split
**********************************************************************/
|