/src/geos/src/noding/OrientedCoordinateArray.cpp
Line | Count | Source |
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: noding/OrientedCoordinateArray.java rev. 1.1 (JTS-1.9) |
16 | | * |
17 | | **********************************************************************/ |
18 | | |
19 | | //#include <cmath> |
20 | | //#include <sstream> |
21 | | |
22 | | #include <geos/noding/OrientedCoordinateArray.h> |
23 | | |
24 | | #include <geos/geom/CoordinateSequence.h> |
25 | | |
26 | | using namespace geos::geom; |
27 | | |
28 | | #ifdef _MSC_VER |
29 | | #pragma warning(disable : 4127) |
30 | | #endif |
31 | | |
32 | | namespace geos { |
33 | | namespace noding { // geos.noding |
34 | | |
35 | | /* private static */ |
36 | | bool |
37 | | OrientedCoordinateArray::orientation(const CoordinateSequence& pts) |
38 | 0 | { |
39 | 0 | return CoordinateSequence::increasingDirection(pts) == 1; |
40 | 0 | } |
41 | | |
42 | | int |
43 | | OrientedCoordinateArray::compareTo(const OrientedCoordinateArray& oca) const |
44 | 0 | { |
45 | 0 | int comp = compareOriented(*pts, orientationVar, |
46 | 0 | *oca.pts, oca.orientationVar); |
47 | 0 | return comp; |
48 | 0 | } |
49 | | |
50 | | |
51 | | /* private static */ |
52 | | int |
53 | | OrientedCoordinateArray::compareOriented(const geom::CoordinateSequence& pts1, |
54 | | bool orientation1, |
55 | | const geom::CoordinateSequence& pts2, |
56 | | bool orientation2) |
57 | 0 | { |
58 | 0 | int dir1 = orientation1 ? 1 : -1; |
59 | 0 | int dir2 = orientation2 ? 1 : -1; |
60 | 0 | int limit1 = orientation1 ? static_cast<int>(pts1.size()) : -1; |
61 | 0 | int limit2 = orientation2 ? static_cast<int>(pts2.size()) : -1; |
62 | |
|
63 | 0 | int i1 = orientation1 ? 0 : static_cast<int>(pts1.size() - 1); |
64 | 0 | int i2 = orientation2 ? 0 : static_cast<int>(pts2.size() - 1); |
65 | | //int comp = 0; // unused, but is in JTS ... |
66 | 0 | while(true) { |
67 | 0 | int compPt = pts1[static_cast<std::size_t>(i1)].compareTo(pts2[static_cast<std::size_t>(i2)]); |
68 | 0 | if(compPt != 0) { |
69 | 0 | return compPt; |
70 | 0 | } |
71 | 0 | i1 += dir1; |
72 | 0 | i2 += dir2; |
73 | 0 | bool done1 = i1 == limit1; |
74 | 0 | bool done2 = i2 == limit2; |
75 | 0 | if(done1 && ! done2) { |
76 | 0 | return -1; |
77 | 0 | } |
78 | 0 | if(! done1 && done2) { |
79 | 0 | return 1; |
80 | 0 | } |
81 | 0 | if(done1 && done2) { |
82 | 0 | return 0; |
83 | 0 | } |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | | bool |
88 | 0 | OrientedCoordinateArray::operator==(const OrientedCoordinateArray& other) const { |
89 | 0 | auto sz1 = pts->size(); |
90 | 0 | auto sz2 = other.pts->size(); |
91 | |
|
92 | 0 | if (sz1 != sz2) { |
93 | 0 | return false; |
94 | 0 | } |
95 | | |
96 | 0 | if (orientationVar == other.orientationVar) { |
97 | 0 | for (std::size_t i = 0; i < sz1; i++) { |
98 | 0 | if (pts->getAt(i) != other.pts->getAt(i)) { |
99 | 0 | return false; |
100 | 0 | } |
101 | 0 | } |
102 | 0 | } else { |
103 | 0 | for (std::size_t i = 0; i < sz1; i++) { |
104 | 0 | if (pts->getAt(i) != other.pts->getAt(sz2 - i - 1)) { |
105 | 0 | return false; |
106 | 0 | } |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | 0 | return true; |
111 | 0 | } |
112 | | |
113 | | size_t |
114 | 0 | OrientedCoordinateArray::HashCode::operator()(const geos::noding::OrientedCoordinateArray &oca) const { |
115 | 0 | Coordinate::HashCode coordHash; |
116 | |
|
117 | 0 | auto sz = oca.pts->getSize(); |
118 | |
|
119 | 0 | std::size_t result = std::hash<size_t>{}(sz); |
120 | |
|
121 | 0 | if (oca.orientationVar) { |
122 | 0 | for (std::size_t i = 0; i < sz; i++) { |
123 | 0 | result ^= coordHash(oca.pts->getAt(i)); |
124 | 0 | } |
125 | 0 | } else { |
126 | 0 | for (std::size_t i = sz; i > 0; i--) { |
127 | 0 | result ^= coordHash(oca.pts->getAt(i-1)); |
128 | 0 | } |
129 | 0 | } |
130 | |
|
131 | 0 | return result; |
132 | 0 | } |
133 | | |
134 | | } // namespace geos.noding |
135 | | } // namespace geos |
136 | | |