GEOS  3.13.0dev
EdgeKey.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca>
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/operation/overlayng/OverlayLabel.h>
18 #include <geos/operation/overlayng/EdgeKey.h>
19 #include <geos/operation/overlayng/Edge.h>
20 #include <geos/geom/Coordinate.h>
21 #include <geos/export.h>
22 
23 
24 
25 namespace geos { // geos.
26 namespace operation { // geos.operation
27 namespace overlayng { // geos.operation.overlayng
28 
29 
31 
41 class GEOS_DLL EdgeKey {
42 
43 private:
44 
45  // Members
46  double p0x;
47  double p0y;
48  double p1x;
49  double p1y;
50 
51  // Methods
52  void initPoints(const Edge* edge)
53  {
54  bool direction = edge->direction();
55  if (direction) {
56  init(edge->getCoordinate(0),
57  edge->getCoordinate(1));
58  }
59  else {
60  std::size_t len = edge->size();
61  init(edge->getCoordinate(len - 1),
62  edge->getCoordinate(len - 2));
63  }
64  }
65 
66  void init(const geom::Coordinate& p0, const geom::Coordinate& p1)
67  {
68  p0x = p0.x;
69  p0y = p0.y;
70  p1x = p1.x;
71  p1y = p1.y;
72  }
73 
74 
75 public:
76 
77  EdgeKey(const Edge* edge)
78  {
79  initPoints(edge);
80  }
81 
82  int compareTo(const EdgeKey* ek) const
83  {
84  if (p0x < ek->p0x) return -1;
85  if (p0x > ek->p0x) return 1;
86  if (p0y < ek->p0y) return -1;
87  if (p0y > ek->p0y) return 1;
88  // first points are equal, compare second
89  if (p1x < ek->p1x) return -1;
90  if (p1x > ek->p1x) return 1;
91  if (p1y < ek->p1y) return -1;
92  if (p1y > ek->p1y) return 1;
93  return 0;
94  }
95 
96  bool equals(const EdgeKey* ek) const
97  {
98  return p0x == ek->p0x
99  && p0y == ek->p0y
100  && p1x == ek->p1x
101  && p1y == ek->p1y;
102  }
103 
104  friend bool operator<(const EdgeKey& ek1, const EdgeKey& ek2)
105  {
106  return ek1.compareTo(&ek2) < 0;
107  };
108 
109  friend bool operator==(const EdgeKey& ek1, const EdgeKey& ek2)
110  {
111  return ek1.equals(&ek2);
112  };
113 
114 };
115 
116 
117 } // namespace geos.operation.overlayng
118 } // namespace geos.operation
119 } // namespace geos
120 
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:216
Definition: EdgeKey.h:41
Definition: operation/overlayng/Edge.h:55
bool operator<(const CoordinateXY &a, const CoordinateXY &b)
Strict weak ordering operator for Coordinate.
Definition: Coordinate.h:453
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25