GEOS  3.14.0dev
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 #pragma once
20 
21 #include <geos/constants.h> // for DoubleNotANumber
22 #include <geos/geom/Coordinate.h> // for inlines
23 
24 #include <array>
25 #include <cassert>
26 
27 namespace geos {
28 namespace algorithm { // geos::algorithm
29 namespace distance { // geos::algorithm::distance
30 
37 public:
38 
40  :
41  distanceSquared(DoubleNotANumber),
42  isNull(true)
43  {}
44 
45  void
46  initialize()
47  {
48  isNull = true;
49  }
50 
51  void
52  initialize(const geom::CoordinateXY& p0, const geom::CoordinateXY& p1)
53  {
54  pt[0] = p0;
55  pt[1] = p1;
56  distanceSquared = p0.distanceSquared(p1);
57  isNull = false;
58  }
59 
60  double
61  getDistance() const
62  {
63  return std::sqrt(distanceSquared);
64  }
65 
66  const std::array<geom::CoordinateXY, 2>&
67  getCoordinates() const
68  {
69  return pt;
70  }
71 
72  const geom::CoordinateXY&
73  getCoordinate(std::size_t i) const
74  {
75  assert(i < pt.size());
76  return pt[i];
77  }
78 
79  void
80  setMaximum(const PointPairDistance& ptDist)
81  {
82  setMaximum(ptDist.pt[0], ptDist.pt[1]);
83  }
84 
85  void
86  setMaximum(const geom::CoordinateXY& p0, const geom::CoordinateXY& p1)
87  {
88  if(isNull) {
89  initialize(p0, p1);
90  return;
91  }
92  double distSq = p0.distanceSquared(p1);
93  if(distSq > distanceSquared) {
94  initialize(p0, p1, distSq);
95  }
96  }
97 
98  void
99  setMinimum(const PointPairDistance& ptDist)
100  {
101  setMinimum(ptDist.pt[0], ptDist.pt[1]);
102  }
103 
104  void
105  setMinimum(const geom::CoordinateXY& p0, const geom::CoordinateXY& p1)
106  {
107  if(isNull) {
108  initialize(p0, p1);
109  return;
110  }
111  double distSq = p0.distanceSquared(p1);
112  if(distSq < distanceSquared) {
113  initialize(p0, p1, distSq);
114  }
115  }
116 
117  bool
118  getIsNull()
119  {
120  return isNull;
121  }
122 
123 private:
124 
131  void
132  initialize(const geom::CoordinateXY& p0, const geom::CoordinateXY& p1,
133  double distSquared)
134  {
135  pt[0] = p0;
136  pt[1] = p1;
137  distanceSquared = distSquared;
138  isNull = false;
139  }
140 
141  std::array<geom::CoordinateXY, 2> pt;
142 
143  double distanceSquared;
144 
145  bool isNull;
146 };
147 
148 } // geos::algorithm::distance
149 } // geos::algorithm
150 } // geos
151 
Definition: PointPairDistance.h:36
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25