| 1 | /**********************************************************************
|
|---|
| 2 | * $Id: ConvexHull.h 3255 2011-03-01 17:56:10Z mloskot $
|
|---|
| 3 | *
|
|---|
| 4 | * GEOS - Geometry Engine Open Source
|
|---|
| 5 | * http://geos.refractions.net
|
|---|
| 6 | *
|
|---|
| 7 | * Copyright (C) 2005-2006 Refractions Research Inc.
|
|---|
| 8 | * Copyright (C) 2001-2002 Vivid Solutions Inc.
|
|---|
| 9 | *
|
|---|
| 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.
|
|---|
| 14 | *
|
|---|
| 15 | **********************************************************************/
|
|---|
| 16 |
|
|---|
| 17 | #ifndef GEOS_ALGORITHM_CONVEXHULL_H
|
|---|
| 18 | #define GEOS_ALGORITHM_CONVEXHULL_H
|
|---|
| 19 |
|
|---|
| 20 | #include <geos/export.h>
|
|---|
| 21 | #include <vector>
|
|---|
| 22 |
|
|---|
| 23 | // FIXME: avoid using Cordinate:: typedefs to avoid full include
|
|---|
| 24 | #include <geos/geom/Coordinate.h>
|
|---|
| 25 |
|
|---|
| 26 | #ifdef _MSC_VER
|
|---|
| 27 | #pragma warning(push)
|
|---|
| 28 | #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
|
|---|
| 29 | #endif
|
|---|
| 30 |
|
|---|
| 31 | // Forward declarations
|
|---|
| 32 | namespace geos {
|
|---|
| 33 | namespace geom {
|
|---|
| 34 | class Geometry;
|
|---|
| 35 | class GeometryFactory;
|
|---|
| 36 | class CoordinateSequence;
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | namespace geos {
|
|---|
| 41 | namespace algorithm { // geos::algorithm
|
|---|
| 42 |
|
|---|
| 43 | /**
|
|---|
| 44 | * Computes the convex hull of a Geometry.
|
|---|
| 45 | *
|
|---|
| 46 | * The convex hull is the smallest convex Geometry that contains all the
|
|---|
| 47 | * points in the input Geometry.
|
|---|
| 48 | *
|
|---|
| 49 | * Uses the Graham Scan algorithm.
|
|---|
| 50 | *
|
|---|
| 51 | * Last port: algorithm/ConvexHull.java rev. 1.26 (JTS-1.7)
|
|---|
| 52 | *
|
|---|
| 53 | */
|
|---|
| 54 | class GEOS_DLL ConvexHull {
|
|---|
| 55 | private:
|
|---|
| 56 | const geom::GeometryFactory *geomFactory;
|
|---|
| 57 | geom::Coordinate::ConstVect inputPts;
|
|---|
| 58 |
|
|---|
| 59 | void extractCoordinates(const geom::Geometry *geom);
|
|---|
| 60 |
|
|---|
| 61 | /// Create a CoordinateSequence from the Coordinate::ConstVect
|
|---|
| 62 | /// This is needed to construct the geometries.
|
|---|
| 63 | /// Here coordinate copies happen
|
|---|
| 64 | /// The returned object is newly allocated !NO EXCEPTION SAFE!
|
|---|
| 65 | geom::CoordinateSequence *toCoordinateSequence(geom::Coordinate::ConstVect &cv);
|
|---|
| 66 |
|
|---|
| 67 | void computeOctPts(const geom::Coordinate::ConstVect &src,
|
|---|
| 68 | geom::Coordinate::ConstVect &tgt);
|
|---|
| 69 |
|
|---|
| 70 | bool computeOctRing(const geom::Coordinate::ConstVect &src,
|
|---|
| 71 | geom::Coordinate::ConstVect &tgt);
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Uses a heuristic to reduce the number of points scanned
|
|---|
| 75 | * to compute the hull.
|
|---|
| 76 | * The heuristic is to find a polygon guaranteed to
|
|---|
| 77 | * be in (or on) the hull, and eliminate all points inside it.
|
|---|
| 78 | * A quadrilateral defined by the extremal points
|
|---|
| 79 | * in the four orthogonal directions
|
|---|
| 80 | * can be used, but even more inclusive is
|
|---|
| 81 | * to use an octilateral defined by the points in the
|
|---|
| 82 | * 8 cardinal directions.
|
|---|
| 83 | *
|
|---|
| 84 | * Note that even if the method used to determine the polygon
|
|---|
| 85 | * vertices is not 100% robust, this does not affect the
|
|---|
| 86 | * robustness of the convex hull.
|
|---|
| 87 | *
|
|---|
| 88 | * @param pts The vector of const Coordinate pointers
|
|---|
| 89 | * to be reduced
|
|---|
| 90 | *
|
|---|
| 91 | *
|
|---|
| 92 | * WARNING: the parameter will be modified
|
|---|
| 93 | *
|
|---|
| 94 | */
|
|---|
| 95 | void reduce(geom::Coordinate::ConstVect &pts);
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 | /// parameter will be modified
|
|---|
| 99 | void preSort(geom::Coordinate::ConstVect &pts);
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * Given two points p and q compare them with respect to their radial
|
|---|
| 103 | * ordering about point o. First checks radial ordering.
|
|---|
| 104 | * If points are collinear, the comparison is based
|
|---|
| 105 | * on their distance to the origin.
|
|---|
| 106 | *
|
|---|
| 107 | * p < q iff
|
|---|
| 108 | *
|
|---|
| 109 | * - ang(o-p) < ang(o-q) (e.g. o-p-q is CCW)
|
|---|
| 110 | * - or ang(o-p) == ang(o-q) && dist(o,p) < dist(o,q)
|
|---|
| 111 | *
|
|---|
| 112 | * @param o the origin
|
|---|
| 113 | * @param p a point
|
|---|
| 114 | * @param q another point
|
|---|
| 115 | * @return -1, 0 or 1 depending on whether p is less than,
|
|---|
| 116 | * equal to or greater than q
|
|---|
| 117 | */
|
|---|
| 118 | int polarCompare(const geom::Coordinate &o,
|
|---|
| 119 | const geom::Coordinate &p, const geom::Coordinate &q);
|
|---|
| 120 |
|
|---|
| 121 | void grahamScan(const geom::Coordinate::ConstVect &c,
|
|---|
| 122 | geom::Coordinate::ConstVect &ps);
|
|---|
| 123 |
|
|---|
| 124 | /**
|
|---|
| 125 | * @param vertices the vertices of a linear ring,
|
|---|
| 126 | * which may or may not be
|
|---|
| 127 | * flattened (i.e. vertices collinear)
|
|---|
| 128 | *
|
|---|
| 129 | * @return a 2-vertex LineString if the vertices are
|
|---|
| 130 | * collinear; otherwise, a Polygon with unnecessary
|
|---|
| 131 | * (collinear) vertices removed
|
|---|
| 132 | */
|
|---|
| 133 | geom::Geometry* lineOrPolygon(const geom::Coordinate::ConstVect &vertices);
|
|---|
| 134 |
|
|---|
| 135 | /**
|
|---|
| 136 | * Write in 'cleaned' a version of 'input' with collinear
|
|---|
| 137 | * vertexes removed.
|
|---|
| 138 | */
|
|---|
| 139 | void cleanRing(const geom::Coordinate::ConstVect &input,
|
|---|
| 140 | geom::Coordinate::ConstVect &cleaned);
|
|---|
| 141 |
|
|---|
| 142 | /**
|
|---|
| 143 | * @return whether the three coordinates are collinear
|
|---|
| 144 | * and c2 lies between c1 and c3 inclusive
|
|---|
| 145 | */
|
|---|
| 146 | bool isBetween(const geom::Coordinate& c1, const geom::Coordinate& c2, const geom::Coordinate& c3);
|
|---|
| 147 |
|
|---|
| 148 | public:
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * Create a new convex hull construction for the input Geometry.
|
|---|
| 152 | */
|
|---|
| 153 | ConvexHull(const geom::Geometry *newGeometry);
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 | ~ConvexHull();
|
|---|
| 157 |
|
|---|
| 158 | /**
|
|---|
| 159 | * Returns a Geometry that represents the convex hull of
|
|---|
| 160 | * the input geometry.
|
|---|
| 161 | * The returned geometry contains the minimal number of points
|
|---|
| 162 | * needed to represent the convex hull.
|
|---|
| 163 | * In particular, no more than two consecutive points
|
|---|
| 164 | * will be collinear.
|
|---|
| 165 | *
|
|---|
| 166 | * @return if the convex hull contains 3 or more points,
|
|---|
| 167 | * a Polygon; 2 points, a LineString;
|
|---|
| 168 | * 1 point, a Point; 0 points, an empty GeometryCollection.
|
|---|
| 169 | */
|
|---|
| 170 | geom::Geometry* getConvexHull();
|
|---|
| 171 | };
|
|---|
| 172 |
|
|---|
| 173 | } // namespace geos::algorithm
|
|---|
| 174 | } // namespace geos
|
|---|
| 175 |
|
|---|
| 176 | #ifdef _MSC_VER
|
|---|
| 177 | #pragma warning(pop)
|
|---|
| 178 | #endif
|
|---|
| 179 |
|
|---|
| 180 | #ifdef GEOS_INLINE
|
|---|
| 181 | # include "geos/algorithm/ConvexHull.inl"
|
|---|
| 182 | #endif
|
|---|
| 183 |
|
|---|
| 184 | #endif // GEOS_ALGORITHM_CONVEXHULL_H
|
|---|
| 185 |
|
|---|
| 186 | /**********************************************************************
|
|---|
| 187 | * $Log$
|
|---|
| 188 | * Revision 1.2 2006/03/24 09:52:41 strk
|
|---|
| 189 | * USE_INLINE => GEOS_INLINE
|
|---|
| 190 | *
|
|---|
| 191 | * Revision 1.1 2006/03/09 16:46:48 strk
|
|---|
| 192 | * geos::geom namespace definition, first pass at headers split
|
|---|
| 193 | *
|
|---|
| 194 | **********************************************************************/
|
|---|
| 195 |
|
|---|