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