GEOS  3.9.1dev
CentralEndpointIntersector.h
Go to the documentation of this file.
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&
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&
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 
103 
104  void
106  {
107  geom::Coordinate centroid = average(_pts);
108  _intPt = findNearestPoint(centroid, _pts);
109  }
110 
111  static geom::Coordinate
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 
141  const std::vector<geom::Coordinate>& pts) const
142  {
143  double minDistSq = std::numeric_limits<double>::max();
145  for(std::size_t i = 0, n = pts.size(); i < n; ++i) {
146  double distSq = p.distanceSquared(pts[i]);
147  if(distSq < minDistSq) {
148  minDistSq = distSq;
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 distanceSquared(const Coordinate &p) const
#define GEOS_DLL
Definition: export.h:28
static Coordinate & getNull()
double y
y-coordinate
Definition: Coordinate.h:83
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
const geom::Coordinate & getIntersection() const
geom::Coordinate findNearestPoint(const geom::Coordinate &p, const std::vector< geom::Coordinate > &pts) const
static const geom::Coordinate & getIntersection(const geom::Coordinate &p00, const geom::Coordinate &p01, const geom::Coordinate &p10, const geom::Coordinate &p11)
Basic namespace for all GEOS functionalities.
Computes an approximate intersection of two line segments by taking the most central of the endpoints...
CentralEndpointIntersector(const geom::Coordinate &p00, const geom::Coordinate &p01, const geom::Coordinate &p10, const geom::Coordinate &p11)
double x
x-coordinate
Definition: Coordinate.h:80
static geom::Coordinate average(const std::vector< geom::Coordinate > &pts)