GEOS  3.14.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 #pragma once
20 
21 #include <geos/export.h>
22 #include <geos/geom/Coordinate.h>
23 
24 #include <string>
25 #include <limits>
26 
27 #ifdef _MSC_VER
28 #pragma warning(push)
29 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
30 #endif
31 
32 // Forward declarations
33 namespace geos {
34 namespace geom {
35 //class PrecisionModel;
36 }
37 }
38 
39 namespace geos {
40 namespace algorithm { // geos::algorithm
41 
62 
63 public:
64 
65  static const geom::Coordinate&
66  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&
89  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
104  compute()
105  {
106  geom::Coordinate centroid = average(_pts);
107  _intPt = findNearestPoint(centroid, _pts);
108  }
109 
110  static geom::Coordinate
111  average(
112  const std::vector<geom::Coordinate>& pts)
113  {
114  geom::Coordinate avg(0, 0);
115  std::size_t n = pts.size();
116  if(! n) {
117  return avg;
118  }
119  for(std::size_t i = 0; i < n; ++i) {
120  avg.x += pts[i].x;
121  avg.y += pts[i].y;
122  }
123  avg.x /= n;
124  avg.y /= n;
125  return avg;
126  }
127 
139  findNearestPoint(const geom::Coordinate& p,
140  const std::vector<geom::Coordinate>& pts) const
141  {
142  double minDistSq = DoubleInfinity;
143  geom::Coordinate result = geom::Coordinate::getNull();
144  for(std::size_t i = 0, n = pts.size(); i < n; ++i) {
145  double distSq = p.distanceSquared(pts[i]);
146  if(distSq < minDistSq) {
147  minDistSq = distSq;
148  result = pts[i];
149  }
150  }
151  return result;
152  }
153 };
154 
155 } // namespace geos::algorithm
156 } // namespace geos
157 
158 #ifdef _MSC_VER
159 #pragma warning(pop)
160 #endif
161 
Computes an approximate intersection of two line segments by taking the most central of the endpoints...
Definition: CentralEndpointIntersector.h:61
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:217
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25