GEOS  3.8.0
PointPairDistance.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2009 Sandro Santilli <strk@kbt.io>
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/distance/PointPairDistance.java 1.1 (JTS-1.9)
16  *
17  **********************************************************************/
18 
19 #ifndef GEOS_ALGORITHM_DISTANCE_POINTPAIRDISTANCE_H
20 #define GEOS_ALGORITHM_DISTANCE_POINTPAIRDISTANCE_H
21 
22 #include <geos/constants.h> // for DoubleNotANumber
23 #include <geos/geom/Coordinate.h> // for inlines
24 
25 #include <array>
26 #include <cassert>
27 
28 namespace geos {
29 namespace algorithm { // geos::algorithm
30 namespace distance { // geos::algorithm::distance
31 
38 public:
39 
41  :
42  distanceSquared(DoubleNotANumber),
43  isNull(true)
44  {}
45 
46  void
47  initialize()
48  {
49  isNull = true;
50  }
51 
52  void
53  initialize(const geom::Coordinate& p0, const geom::Coordinate& p1)
54  {
55  pt[0] = p0;
56  pt[1] = p1;
57  distanceSquared = p0.distanceSquared(p1);
58  isNull = false;
59  }
60 
61  double
62  getDistance() const
63  {
64  return std::sqrt(distanceSquared);
65  }
66 
67  const std::array<geom::Coordinate, 2>&
68  getCoordinates() const
69  {
70  return pt;
71  }
72 
73  const geom::Coordinate&
74  getCoordinate(size_t i) const
75  {
76  assert(i < pt.size());
77  return pt[i];
78  }
79 
80  void
81  setMaximum(const PointPairDistance& ptDist)
82  {
83  setMaximum(ptDist.pt[0], ptDist.pt[1]);
84  }
85 
86  void
87  setMaximum(const geom::Coordinate& p0, const geom::Coordinate& p1)
88  {
89  if(isNull) {
90  initialize(p0, p1);
91  return;
92  }
93  double distSq = p0.distanceSquared(p1);
94  if(distSq > distanceSquared) {
95  initialize(p0, p1, distSq);
96  }
97  }
98 
99  void
100  setMinimum(const PointPairDistance& ptDist)
101  {
102  setMinimum(ptDist.pt[0], ptDist.pt[1]);
103  }
104 
105  void
106  setMinimum(const geom::Coordinate& p0, const geom::Coordinate& p1)
107  {
108  if(isNull) {
109  initialize(p0, p1);
110  return;
111  }
112  double distSq = p0.distanceSquared(p1);
113  if(distSq < distanceSquared) {
114  initialize(p0, p1, distSq);
115  }
116  }
117 
118  bool
119  getIsNull()
120  {
121  return isNull;
122  }
123 
124 private:
125 
132  void
133  initialize(const geom::Coordinate& p0, const geom::Coordinate& p1,
134  double distSquared)
135  {
136  pt[0] = p0;
137  pt[1] = p1;
138  distanceSquared = distSquared;
139  isNull = false;
140  }
141 
142  std::array<geom::Coordinate, 2> pt;
143 
144  double distanceSquared;
145 
146  bool isNull;
147 };
148 
149 } // geos::algorithm::distance
150 } // geos::algorithm
151 } // geos
152 
153 #endif // GEOS_ALGORITHM_DISTANCE_POINTPAIRDISTANCE_H
154 
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
Definition: PointPairDistance.h:37
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25