Add to win32 instructions
[geos.git] / source / algorithm / InteriorPointLine.cpp
blobd382d3c00f2421df34b323df0c7ec181a04af797
1 /**********************************************************************
2 * $Id$
4 * GEOS - Geometry Engine Open Source
5 * http://geos.refractions.net
7 * Copyright (C) 2005-2006 Refractions Research Inc.
8 * Copyright (C) 2001-2002 Vivid Solutions Inc.
10 * This is free software; you can redistribute and/or modify it under
11 * the terms of the GNU Lesser General Public Licence as published
12 * by the Free Software Foundation.
13 * See the COPYING file for more information.
15 **********************************************************************
17 **********************************************************************/
19 #include <geos/algorithm/InteriorPointLine.h>
20 #include <geos/geom/Coordinate.h>
21 #include <geos/geom/Geometry.h>
22 #include <geos/geom/GeometryCollection.h>
23 #include <geos/geom/LineString.h>
24 #include <geos/geom/CoordinateSequence.h>
26 #ifndef GEOS_DEBUG
27 #define GEOS_DEBUG 0
28 #endif
30 #ifdef GEOS_DEBUG
31 #include <iostream>
32 #endif
34 using namespace geos::geom;
36 namespace geos {
37 namespace algorithm { // geos.algorithm
39 InteriorPointLine::InteriorPointLine(const Geometry *g)
41 minDistance=DoubleInfinity;
42 hasInterior=false;
43 if ( g->getCentroid(centroid) )
45 #if GEOS_DEBUG
46 std::cerr << "Centroid: " << centroid << std::endl;
47 #endif
48 addInterior(g);
49 if (!hasInterior) addEndpoints(g);
53 InteriorPointLine::~InteriorPointLine()
57 /* private
59 * Tests the interior vertices (if any)
60 * defined by a linear Geometry for the best inside point.
61 * If a Geometry is not of dimension 1 it is not tested.
62 * @param geom the geometry to add
64 void
65 InteriorPointLine::addInterior(const Geometry *geom)
67 const LineString *ls = dynamic_cast<const LineString*>(geom);
68 if ( ls ) {
69 addInterior(ls->getCoordinatesRO());
70 return;
73 const GeometryCollection *gc = dynamic_cast<const GeometryCollection*>(geom);
74 if ( gc )
76 for(std::size_t i=0, n=gc->getNumGeometries(); i<n; i++) {
77 addInterior(gc->getGeometryN(i));
82 void
83 InteriorPointLine::addInterior(const CoordinateSequence *pts)
85 const std::size_t n=pts->getSize()-1;
86 for(std::size_t i=1; i<n; ++i)
88 add(pts->getAt(i));
92 /* private
94 * Tests the endpoint vertices
95 * defined by a linear Geometry for the best inside point.
96 * If a Geometry is not of dimension 1 it is not tested.
97 * @param geom the geometry to add
99 void
100 InteriorPointLine::addEndpoints(const Geometry *geom)
102 const LineString *ls = dynamic_cast<const LineString*>(geom);
103 if ( ls ) {
104 addEndpoints(ls->getCoordinatesRO());
105 return;
108 const GeometryCollection *gc = dynamic_cast<const GeometryCollection*>(geom);
109 if ( gc )
111 for(std::size_t i=0, n=gc->getNumGeometries(); i<n; i++) {
112 addEndpoints(gc->getGeometryN(i));
117 void
118 InteriorPointLine::addEndpoints(const CoordinateSequence *pts)
120 add(pts->getAt(0));
121 add(pts->getAt(pts->getSize()-1));
124 /*private*/
125 void
126 InteriorPointLine::add(const Coordinate& point)
129 double dist=point.distance(centroid);
130 #if GEOS_DEBUG
131 std::cerr << "point " << point << " dist " << dist << ", minDistance " << minDistance << std::endl;
132 #endif
133 if (!hasInterior || dist<minDistance) {
134 interiorPoint=point;
135 #if GEOS_DEBUG
136 std::cerr << " is new InteriorPoint" << std::endl;
137 #endif
138 minDistance=dist;
139 hasInterior=true;
143 bool
144 InteriorPointLine::getInteriorPoint(Coordinate& ret) const
146 if ( ! hasInterior ) return false;
147 ret=interiorPoint;
148 return true;
151 } // namespace geos.algorithm
152 } // namespace geos
154 /**********************************************************************
155 * $Log$
156 * Revision 1.18 2006/03/21 11:12:23 strk
157 * Cleanups: headers inclusion and Log section
159 * Revision 1.17 2006/03/09 16:46:45 strk
160 * geos::geom namespace definition, first pass at headers split
161 **********************************************************************/