GEOS  3.8.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.
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 <vector> // for composition
26 #include <cassert>
27
28 namespace geos {
29 namespace algorithm { // geos::algorithm
30 namespace distance { // geos::algorithm::distance
31
38 public:
39
41  :
42  pt(2),
43  distance(DoubleNotANumber),
44  isNull(true)
45  {
46  assert(pt.size() == 2);
47  }
48
49  void
50  initialize()
51  {
52  isNull = true;
53  }
54
55  void
56  initialize(const geom::Coordinate& p0, const geom::Coordinate& p1)
57  {
58  pt[0] = p0;
59  pt[1] = p1;
60  distance = p0.distance(p1);
61  isNull = false;
62  }
63
64  double
65  getDistance() const
66  {
67  return distance;
68  }
69
70  const std::vector<geom::Coordinate>&
71  getCoordinates() const
72  {
73  return pt;
74  }
75
76  const geom::Coordinate&
77  getCoordinate(size_t i) const
78  {
79  assert(i < pt.size());
80  return pt[i];
81  }
82
83  void
84  setMaximum(const PointPairDistance& ptDist)
85  {
86  setMaximum(ptDist.pt[0], ptDist.pt[1]);
87  }
88
89  void
90  setMaximum(const geom::Coordinate& p0, const geom::Coordinate& p1)
91  {
92  if(isNull) {
93  initialize(p0, p1);
94  return;
95  }
96  double dist = p0.distance(p1);
97  if(dist > distance) {
98  initialize(p0, p1, dist);
99  }
100  }
101
102  void
103  setMinimum(const PointPairDistance& ptDist)
104  {
105  setMinimum(ptDist.pt[0], ptDist.pt[1]);
106  }
107
108  void
109  setMinimum(const geom::Coordinate& p0, const geom::Coordinate& p1)
110  {
111  if(isNull) {
112  initialize(p0, p1);
113  return;
114  }
115  double dist = p0.distance(p1);
116  if(dist < distance) {
117  initialize(p0, p1, dist);
118  }
119  }
120
121  bool
122  getIsNull()
123  {
124  return isNull;
125  }
126
127 private:
128
135  void
136  initialize(const geom::Coordinate& p0, const geom::Coordinate& p1,
137  double dist)
138  {
139  pt[0] = p0;
140  pt[1] = p1;
141  distance = dist;
142  isNull = false;
143  }
144
145  std::vector<geom::Coordinate> pt;
146
147  double distance;
148
149  bool isNull;
150 };
151
152 } // geos::algorithm::distance
153 } // geos::algorithm
154 } // geos
155
156 #endif // GEOS_ALGORITHM_DISTANCE_POINTPAIRDISTANCE_H
157
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
double distance(const Coordinate &p) const