GEOS  3.8.0dev
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&
67  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 
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&
90  getIntersection() const
91  {
92  return _intPt;
93  }
94 
95 
96 private:
97 
98  // This is likely overkill.. we'll be allocating heap
99  // memory at every call !
100  std::vector<geom::Coordinate> _pts;
101 
102  geom::Coordinate _intPt;
103 
104  void
105  compute()
106  {
107  geom::Coordinate centroid = average(_pts);
108  _intPt = findNearestPoint(centroid, _pts);
109  }
110 
111  static geom::Coordinate
112  average(
113  const std::vector<geom::Coordinate>& pts)
114  {
115  geom::Coordinate avg(0, 0);
116  size_t n = pts.size();
117  if(! n) {
118  return avg;
119  }
120  for(std::size_t i = 0; i < n; ++i) {
121  avg.x += pts[i].x;
122  avg.y += pts[i].y;
123  }
124  avg.x /= n;
125  avg.y /= n;
126  return avg;
127  }
128 
140  findNearestPoint(const geom::Coordinate& p,
141  const std::vector<geom::Coordinate>& pts) const
142  {
143  double minDist = std::numeric_limits<double>::max();
144  geom::Coordinate result = geom::Coordinate::getNull();
145  for(std::size_t i = 0, n = pts.size(); i < n; ++i) {
146  double dist = p.distance(pts[i]);
147  if(dist < minDist) {
148  minDist = dist;
149  result = pts[i];
150  }
151  }
152  return result;
153  }
154 };
155 
156 } // namespace geos::algorithm
157 } // namespace geos
158 
159 #ifdef _MSC_VER
160 #pragma warning(pop)
161 #endif
162 
163 #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