GEOS  3.11.0rc0
Coordinate.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 #pragma once
16 
17 #include <geos/export.h>
18 #include <geos/constants.h> // for DoubleNotANumber
19 #include <set>
20 #include <stack>
21 #include <vector> // for typedefs
22 #include <string>
23 #include <limits>
24 
25 #ifdef _MSC_VER
26 #pragma warning(push)
27 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
28 #endif
29 
30 namespace geos {
31 namespace geom { // geos.geom
32 
33 struct CoordinateLessThen;
34 
55 // Define the following to make assignments and copy constructions
56 // NON-(will let profilers report usages)
57 //#define PROFILE_COORDINATE_COPIES 1
58 class GEOS_DLL Coordinate {
59 
60 private:
61 
62  static Coordinate _nullCoord;
63 
64 public:
66  typedef std::set<const Coordinate*, CoordinateLessThen> ConstSet;
67 
69  typedef std::vector<const Coordinate*> ConstVect;
70 
72  typedef std::stack<const Coordinate*> ConstStack;
73 
75  typedef std::vector<Coordinate> Vect;
76 
78  double x;
79 
81  double y;
82 
84  double z;
85 
87  GEOS_DLL friend std::ostream& operator<< (std::ostream& os, const Coordinate& c);
88 
90  GEOS_DLL friend bool operator==(const Coordinate& a, const Coordinate& b)
91  {
92  return a.equals2D(b);
93  };
94 
96  GEOS_DLL friend bool operator!=(const Coordinate& a, const Coordinate& b)
97  {
98  return ! a.equals2D(b);
99  };
100 
101  Coordinate()
102  : x(0.0)
103  , y(0.0)
104  , z(DoubleNotANumber)
105  {};
106 
107  Coordinate(double xNew, double yNew, double zNew = DoubleNotANumber)
108  : x(xNew)
109  , y(yNew)
110  , z(zNew)
111  {};
112 
113  void setNull()
114  {
115  x = DoubleNotANumber;
116  y = DoubleNotANumber;
117  z = DoubleNotANumber;
118  };
119 
120  static Coordinate& getNull();
121 
122  bool isNull() const
123  {
124  return (std::isnan(x) && std::isnan(y) && std::isnan(z));
125  };
126 
127  bool isValid() const
128  {
129  return std::isfinite(x) && std::isfinite(y);
130  };
131 
132  bool equals2D(const Coordinate& other) const
133  {
134  if(x != other.x) {
135  return false;
136  }
137  if(y != other.y) {
138  return false;
139  }
140  return true;
141  };
142 
143  bool equals2D(const Coordinate& other, double tolerance) const
144  {
145  if (std::abs(x - other.x) > tolerance) {
146  return false;
147  }
148  if (std::abs(y - other.y) > tolerance) {
149  return false;
150  }
151  return true;
152  };
153 
155  bool equals(const Coordinate& other) const
156  {
157  return equals2D(other);
158  };
159 
161  int compareTo(const Coordinate& other) const
162  {
163  if(x < other.x) {
164  return -1;
165  }
166  if(x > other.x) {
167  return 1;
168  }
169  if(y < other.y) {
170  return -1;
171  }
172  if(y > other.y) {
173  return 1;
174  }
175  return 0;
176  };
177 
179  bool equals3D(const Coordinate& other) const
180  {
181  return (x == other.x) && (y == other.y) &&
182  ((z == other.z) || (std::isnan(z) && std::isnan(other.z)));
183  };
184 
186  std::string toString() const;
187 
190  //void makePrecise(const PrecisionModel *pm);
191  double distance(const Coordinate& p) const
192  {
193  double dx = x - p.x;
194  double dy = y - p.y;
195  return std::sqrt(dx * dx + dy * dy);
196  };
197 
198  double distanceSquared(const Coordinate& p) const
199  {
200  double dx = x - p.x;
201  double dy = y - p.y;
202  return dx * dx + dy * dy;
203  };
204 
205  struct GEOS_DLL HashCode
206  {
207  std::size_t operator()(const Coordinate & c) const
208  {
209  size_t h = std::hash<double>{}(c.x);
210  h ^= std::hash<double>{}(c.y) << 1;
211  // z ordinate ignored for consistency with operator==
212  return h;
213  };
214  };
215 
216 };
217 
218 
220 struct GEOS_DLL CoordinateLessThen {
221 
222  bool operator()(const Coordinate* a, const Coordinate* b) const
223  {
224  if(a->compareTo(*b) < 0) {
225  return true;
226  }
227  else {
228  return false;
229  }
230  };
231 
232  bool operator()(const Coordinate& a, const Coordinate& b) const
233  {
234  if(a.compareTo(b) < 0) {
235  return true;
236  }
237  else {
238  return false;
239  }
240  };
241 
242 };
243 
245 inline bool operator<(const Coordinate& a, const Coordinate& b)
246 {
247  return CoordinateLessThen()(a, b);
248 }
249 
250 } // namespace geos.geom
251 } // namespace geos
252 
253 #ifdef _MSC_VER
254 #pragma warning(pop)
255 #endif
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269 
270 
271 
272 
273 
274 
275 
276 
277 
278 
279 
280 
281 
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:58
bool equals(const Coordinate &other) const
2D only
Definition: Coordinate.h:155
friend bool operator==(const Coordinate &a, const Coordinate &b)
Equality operator for Coordinate. 2D only.
Definition: Coordinate.h:90
std::string toString() const
Returns a string of the form (x,y,z) .
friend bool operator!=(const Coordinate &a, const Coordinate &b)
Inequality operator for Coordinate. 2D only.
Definition: Coordinate.h:96
std::vector< Coordinate > Vect
A vector of Coordinate objects (real object, not pointers)
Definition: Coordinate.h:75
std::vector< const Coordinate * > ConstVect
A vector of const Coordinate pointers.
Definition: Coordinate.h:69
std::set< const Coordinate *, CoordinateLessThen > ConstSet
A set of const Coordinate pointers.
Definition: Coordinate.h:66
std::stack< const Coordinate * > ConstStack
A stack of const Coordinate pointers.
Definition: Coordinate.h:72
double distance(const Coordinate &p) const
Definition: Coordinate.h:191
double y
y-coordinate
Definition: Coordinate.h:81
double x
x-coordinate
Definition: Coordinate.h:78
int compareTo(const Coordinate &other) const
TODO: deprecate this, move logic to CoordinateLessThen instead.
Definition: Coordinate.h:161
bool equals3D(const Coordinate &other) const
3D comparison
Definition: Coordinate.h:179
double z
z-coordinate
Definition: Coordinate.h:84
bool operator<(const Coordinate &a, const Coordinate &b)
Strict weak ordering operator for Coordinate.
Definition: Coordinate.h:245
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25
Strict weak ordering Functor for Coordinate.
Definition: Coordinate.h:220