forked from libgeos/geos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrientationIndexStressTest.cpp
More file actions
221 lines (187 loc) · 6.39 KB
/
Copy pathOrientationIndexStressTest.cpp
File metadata and controls
221 lines (187 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2025 Martin Davis
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************/
/*--------------------------------------------------------------
Stress test for the Orientation Index implementation.
Usage: stress_orientation [ -v ] [ -d ]
-d - run diagonal line segment tests
-v - displays the input and results from each test
A robust orientation index implementation should be internally consistent
- i.e. it should produce the same result for the 3 possible
permutations of the input coordinates which have the same orientation:
p0-p1 / p2 p1-p2 / p0 p2-p0 / p1
Also, the reverse orientations should themselves be consistent,
and be opposite in sign to the forward orientation.
The robust implementation uses DoubleDouble arithmetic and a filter to improve computation time.
It is compared to the simple Floating-Point orientation computation, which is not robust.
Two kinds of test generators are provided:
- random line segments with midpoints
- points at increasing decimal intervals along a diagonal line segment LINESTRING(2 0, 0 2)
--------------------------------------------------------------*/
#include <geos/algorithm/Orientation.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/LineSegment.h>
#include <geos/io/WKTWriter.h>
#include <iomanip>
using namespace geos::algorithm;
using namespace geos::geom;
using namespace geos::io;
int
orientationIndexFP(const Coordinate& p1, const Coordinate& p2,const Coordinate& q){
double dx1 = p2.x-p1.x;
double dy1 = p2.y-p1.y;
double dx2 = q.x-p2.x;
double dy2 = q.y-p2.y;
double det = dx1 * dy2 - dx2 * dy1;
if (det > 0.0) return 1;
if (det < 0.0) return-1;
return 0;
}
bool isVerbose = false;
bool isDiagonalTest = false;
std::size_t count;
std::size_t failDD;
std::size_t failFP;
void parseFlag(char* arg) {
char flag = arg[1];
switch (flag) {
case 'v':
isVerbose = true; break;
case 'd':
isDiagonalTest = true; break;
}
}
void parseArgs(int argc, char** argv) {
if (argc <= 1) return;
int i = 1;
//-- parse flags
while (i < argc && argv[i][0] == '-' && isalpha(argv[i][1])) {
parseFlag(argv[i]);
i++;
}
}
char orientSym(int orientationIndex) {
if (orientationIndex < 0) return '-';
if (orientationIndex > 0) return '+';
return '0';
}
bool isConsistent(std::string tag, Coordinate p0, Coordinate p1, Coordinate p2,
std::function<int(Coordinate p0, Coordinate p1, Coordinate p2)> orientFunc )
{
int orient0 = orientFunc(p0, p1, p2);
int orient1 = orientFunc(p1, p2, p0);
int orient2 = orientFunc(p2, p0, p1);
bool isConsistentForward = orient0 == orient1 && orient0 == orient2;
int orientRev0 = orientFunc(p1, p0, p2);
int orientRev1 = orientFunc(p0, p2, p1);
int orientRev2 = orientFunc(p2, p1, p0);
bool isConsistentRev = orientRev0 == orientRev1 && orientRev0 == orientRev2;
bool isConsistent = isConsistentForward && isConsistentRev;
if (isConsistent) {
bool isOpposite = orient0 == -orientRev0;
isConsistent &= isOpposite;
}
if (isVerbose) {
std::string consistentInd = isConsistent ? " " : "<!";
std::cout << tag << ": "
<< orientSym(orient0) << orientSym(orient1) << orientSym(orient2) << " "
<< orientSym(orientRev0) << orientSym(orientRev1) << orientSym(orientRev2) << " "
<< " " << consistentInd << " ";
}
return isConsistent;
}
bool isConsistentDD(Coordinate p0, Coordinate p1, Coordinate p2)
{
return isConsistent("DD", p0, p1, p2,
[](Coordinate p0, Coordinate p1, Coordinate p2) -> int {
return Orientation::index(p0, p1, p2);
});
}
bool isConsistentFP(Coordinate p0, Coordinate p1, Coordinate p2)
{
return isConsistent("FP", p0, p1, p2,
[](Coordinate p0, Coordinate p1, Coordinate p2) -> int {
return orientationIndexFP(p0, p1, p2);
});
}
Coordinate randomCoord() {
double x = (10.0 * random()) / RAND_MAX;
double y = (10.0 * random()) / RAND_MAX;
return Coordinate(x, y);
}
void checkTest(Coordinate p0, Coordinate p1, Coordinate p2)
{
bool isCorrectDD = isConsistentDD(p0, p1, p2);
bool isCorrectFP = isConsistentFP(p0, p1, p2);
if (isVerbose) {
std::cout << std::setprecision(20) << " "
<< "LINESTRING ( " << p0.x << " " << p0.y << ", " << p1.x << " " << p1.y << " )"
<< " - " << "POINT ( " << p2.x << " " << p2.y << " )"
<< std::endl;
}
count++;
if (! isCorrectDD) failDD++;
if (! isCorrectFP) failFP++;
}
void reportStats(std::string tag = "") {
std::cout << tag << "Num tests: " << count
<< " DD fail = " << failDD << " (" << round((100.0 * failDD / (double) count)) << "%)"
<< " FP fail = " << failFP << " (" << round((100.0 * failFP / (double) count)) << "%)"
<< std::endl;
}
void runDiagonalTests()
{
const int DIAG_SIZE = 2;
Coordinate p0 = Coordinate(DIAG_SIZE, 0);
Coordinate p1 = Coordinate(0, DIAG_SIZE);
const int MAX_PRECISION = 3;
int d = 10;
for (int i = 0; i < MAX_PRECISION; i++) {
int num_points = DIAG_SIZE * d;
for (int ix = 0; ix <= num_points; ix++) {
int iy = num_points - ix;
double x = ix / (double) d;
double y = iy / (double) d;
Coordinate p2 = Coordinate(x, y);
checkTest(p0, p1, p2);
}
d *= 10;
reportStats();
}
}
void runRandomTests()
{
srand (static_cast <unsigned> (time(0)));
const size_t MAX_ITER = 10'000'000;
for (size_t i = 1 ; i <= MAX_ITER; i++) {
Coordinate p0 = randomCoord();
Coordinate p1 = randomCoord();
Coordinate p2 = Coordinate(LineSegment::midPoint(p0, p1));
checkTest(p0, p1, p2);
if (i % 10'000 == 0) {
reportStats();
}
}
}
int main(int argc, char** argv) {
if (argc > 1) {
parseArgs(argc, argv);
}
if (isDiagonalTest) {
runDiagonalTests();
}
else {
runRandomTests();
}
reportStats("Final: ");
}