/src/geos/include/geos/operation/overlayng/EdgeKey.h
Line | Count | Source |
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 | | /** |
30 | | * A key for sorting and comparing edges in a noded arrangement. |
31 | | * Relies on the fact that in a correctly noded arrangement |
32 | | * edges are identical (up to direction) |
33 | | * iff they have their first segment in common. |
34 | | * |
35 | | * @author mdavis |
36 | | * |
37 | | */ |
38 | | class GEOS_DLL EdgeKey { |
39 | | using Coordinate = geos::geom::Coordinate; |
40 | | |
41 | | private: |
42 | | |
43 | | // Members |
44 | | double p0x; |
45 | | double p0y; |
46 | | double p1x; |
47 | | double p1y; |
48 | | |
49 | | // Methods |
50 | | void initPoints(const Edge* edge) |
51 | 20.7M | { |
52 | 20.7M | bool direction = edge->direction(); |
53 | 20.7M | if (direction) { |
54 | 10.4M | init(edge->getCoordinate(0), |
55 | 10.4M | edge->getCoordinate(1)); |
56 | 10.4M | } |
57 | 10.2M | else { |
58 | 10.2M | std::size_t len = edge->size(); |
59 | 10.2M | init(edge->getCoordinate(len - 1), |
60 | 10.2M | edge->getCoordinate(len - 2)); |
61 | 10.2M | } |
62 | 20.7M | } |
63 | | |
64 | | void init(const geom::Coordinate& p0, const geom::Coordinate& p1) |
65 | 20.7M | { |
66 | 20.7M | p0x = p0.x; |
67 | 20.7M | p0y = p0.y; |
68 | 20.7M | p1x = p1.x; |
69 | 20.7M | p1y = p1.y; |
70 | 20.7M | } |
71 | | |
72 | | |
73 | | public: |
74 | | |
75 | | EdgeKey(const Edge* edge) |
76 | 20.7M | { |
77 | 20.7M | initPoints(edge); |
78 | 20.7M | } |
79 | | |
80 | | int compareTo(const EdgeKey* ek) const |
81 | 365M | { |
82 | 365M | if (p0x < ek->p0x) return -1; |
83 | 189M | if (p0x > ek->p0x) return 1; |
84 | 73.7M | if (p0y < ek->p0y) return -1; |
85 | 56.5M | if (p0y > ek->p0y) return 1; |
86 | | // first points are equal, compare second |
87 | 45.6M | if (p1x < ek->p1x) return -1; |
88 | 27.7M | if (p1x > ek->p1x) return 1; |
89 | 18.1M | if (p1y < ek->p1y) return -1; |
90 | 17.4M | if (p1y > ek->p1y) return 1; |
91 | 17.1M | return 0; |
92 | 17.4M | } |
93 | | |
94 | | bool equals(const EdgeKey* ek) const |
95 | 0 | { |
96 | 0 | return p0x == ek->p0x |
97 | 0 | && p0y == ek->p0y |
98 | 0 | && p1x == ek->p1x |
99 | 0 | && p1y == ek->p1y; |
100 | 0 | } |
101 | | |
102 | | friend bool operator<(const EdgeKey& ek1, const EdgeKey& ek2) |
103 | 365M | { |
104 | 365M | return ek1.compareTo(&ek2) < 0; |
105 | 365M | }; |
106 | | |
107 | | friend bool operator==(const EdgeKey& ek1, const EdgeKey& ek2) |
108 | 0 | { |
109 | 0 | return ek1.equals(&ek2); |
110 | 0 | }; |
111 | | |
112 | | }; |
113 | | |
114 | | |
115 | | } // namespace geos.operation.overlayng |
116 | | } // namespace geos.operation |
117 | | } // namespace geos |
118 | | |