GEOS
3.4.2
|
00001 /********************************************************************** 00002 * 00003 * GEOS - Geometry Engine Open Source 00004 * http://geos.osgeo.org 00005 * 00006 * Copyright (C) 2006 Refractions Research Inc. 00007 * 00008 * This is free software; you can redistribute and/or modify it under 00009 * the terms of the GNU Lesser General Public Licence as published 00010 * by the Free Software Foundation. 00011 * See the COPYING file for more information. 00012 * 00013 ********************************************************************** 00014 * 00015 * Last port: algorithm/CentralEndpointIntersector.java rev. 1.1 00016 * 00017 **********************************************************************/ 00018 00019 #ifndef GEOS_ALGORITHM_CENTRALENDPOINTINTERSECTOR_H 00020 #define GEOS_ALGORITHM_CENTRALENDPOINTINTERSECTOR_H 00021 00022 #include <geos/export.h> 00023 #include <geos/geom/Coordinate.h> 00024 00025 #include <string> 00026 #include <limits> 00027 00028 #ifdef _MSC_VER 00029 #pragma warning(push) 00030 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class 00031 #endif 00032 00033 // Forward declarations 00034 namespace geos { 00035 namespace geom { 00036 //class PrecisionModel; 00037 } 00038 } 00039 00040 namespace geos { 00041 namespace algorithm { // geos::algorithm 00042 00062 class GEOS_DLL CentralEndpointIntersector { 00063 00064 public: 00065 00066 static const geom::Coordinate& getIntersection(const geom::Coordinate& p00, 00067 const geom::Coordinate& p01, const geom::Coordinate& p10, 00068 const geom::Coordinate& p11) 00069 { 00070 CentralEndpointIntersector intor(p00, p01, p10, p11); 00071 return intor.getIntersection(); 00072 } 00073 00074 CentralEndpointIntersector(const geom::Coordinate& p00, 00075 const geom::Coordinate& p01, 00076 const geom::Coordinate& p10, 00077 const geom::Coordinate& p11) 00078 : 00079 _pts(4) 00080 { 00081 _pts[0]=p00; 00082 _pts[1]=p01; 00083 _pts[2]=p10; 00084 _pts[3]=p11; 00085 compute(); 00086 } 00087 00088 const geom::Coordinate& getIntersection() const 00089 { 00090 return _intPt; 00091 } 00092 00093 00094 private: 00095 00096 // This is likely overkill.. we'll be allocating heap 00097 // memory at every call ! 00098 std::vector<geom::Coordinate> _pts; 00099 00100 geom::Coordinate _intPt; 00101 00102 void compute() 00103 { 00104 geom::Coordinate centroid = average(_pts); 00105 _intPt = findNearestPoint(centroid, _pts); 00106 } 00107 00108 static geom::Coordinate average( 00109 const std::vector<geom::Coordinate>& pts) 00110 { 00111 geom::Coordinate avg(0, 0); 00112 size_t n = pts.size(); 00113 if ( ! n ) return avg; 00114 for (std::size_t i=0; i<n; ++i) 00115 { 00116 avg.x += pts[i].x; 00117 avg.y += pts[i].y; 00118 } 00119 avg.x /= n; 00120 avg.y /= n; 00121 return avg; 00122 } 00123 00134 geom::Coordinate findNearestPoint(const geom::Coordinate& p, 00135 const std::vector<geom::Coordinate>& pts) const 00136 { 00137 double minDist = std::numeric_limits<double>::max(); 00138 geom::Coordinate result = geom::Coordinate::getNull(); 00139 for (std::size_t i = 0, n=pts.size(); i < n; ++i) { 00140 double dist = p.distance(pts[i]); 00141 if (dist < minDist) { 00142 minDist = dist; 00143 result = pts[i]; 00144 } 00145 } 00146 return result; 00147 } 00148 }; 00149 00150 } // namespace geos::algorithm 00151 } // namespace geos 00152 00153 #ifdef _MSC_VER 00154 #pragma warning(pop) 00155 #endif 00156 00157 #endif // GEOS_ALGORITHM_CENTRALENDPOINTINTERSECTOR_H