GEOS 3.3.8

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List

CentralEndpointIntersector.h
1 /**********************************************************************
2 * $Id: CentralEndpointIntersector.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) 2006 Refractions Research Inc.
8 *
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU Lesser General Public Licence as published
11 * by the Free Software Foundation.
12 * See the COPYING file for more information.
13 *
14 **********************************************************************
15 *
16 * Last port: algorithm/CentralEndpointIntersector.java rev. 1.1
17 *
18 **********************************************************************/
19
20 #ifndef GEOS_ALGORITHM_CENTRALENDPOINTINTERSECTOR_H
21 #define GEOS_ALGORITHM_CENTRALENDPOINTINTERSECTOR_H
22
23 #include <geos/export.h>
24 #include <geos/geom/Coordinate.h>
25
26 #include <string>
27 #include <limits>
28
29 #ifdef _MSC_VER
30 #pragma warning(push)
31 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
32 #endif
33
34 // Forward declarations
35 namespace geos {
36 namespace geom {
37 //class PrecisionModel;
38 }
39 }
40
41 namespace geos {
42 namespace algorithm { // geos::algorithm
43
63 class GEOS_DLL CentralEndpointIntersector {
64
65 public:
66
67 static const geom::Coordinate& getIntersection(const geom::Coordinate& p00,
68 const geom::Coordinate& p01, const geom::Coordinate& p10,
69 const geom::Coordinate& p11)
70 {
71 CentralEndpointIntersector intor(p00, p01, p10, p11);
72 return intor.getIntersection();
73 }
74
75 CentralEndpointIntersector(const geom::Coordinate& p00,
76 const geom::Coordinate& p01,
77 const geom::Coordinate& p10,
78 const geom::Coordinate& p11)
79 :
80 _pts(4)
81 {
82 _pts[0]=p00;
83 _pts[1]=p01;
84 _pts[2]=p10;
85 _pts[3]=p11;
86 compute();
87 }
88
89 const geom::Coordinate& getIntersection() const
90 {
91 return _intPt;
92 }
93
94
95 private:
96
97 // This is likely overkill.. we'll be allocating heap
98 // memory at every call !
99 std::vector<geom::Coordinate> _pts;
100
101 geom::Coordinate _intPt;
102
103 void compute()
104 {
105 geom::Coordinate centroid = average(_pts);
106 _intPt = findNearestPoint(centroid, _pts);
107 }
108
109 static geom::Coordinate average(
110 const std::vector<geom::Coordinate>& pts)
111 {
112 geom::Coordinate avg(0, 0);
113 size_t n = pts.size();
114 if ( ! n ) return avg;
115 for (std::size_t i=0; i<n; ++i)
116 {
117 avg.x += pts[i].x;
118 avg.y += pts[i].y;
119 }
120 avg.x /= n;
121 avg.y /= n;
122 return avg;
123 }
124
135 geom::Coordinate findNearestPoint(const geom::Coordinate& p,
136 const std::vector<geom::Coordinate>& pts) const
137 {
138 double minDist = std::numeric_limits<double>::max();
139 geom::Coordinate result = geom::Coordinate::getNull();
140 for (std::size_t i = 0, n=pts.size(); i < n; ++i) {
141 double dist = p.distance(pts[i]);
142 if (dist < minDist) {
143 minDist = dist;
144 result = pts[i];
145 }
146 }
147 return result;
148 }
149 };
150
151 } // namespace geos::algorithm
152 } // namespace geos
153
154 #ifdef _MSC_VER
155 #pragma warning(pop)
156 #endif
157
158 #endif // GEOS_ALGORITHM_CENTRALENDPOINTINTERSECTOR_H
159
160 /**********************************************************************
161 * $Log$
162 **********************************************************************/
163