File: OrientationIsCCWTest.cpp

package info (click to toggle)
geos 3.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,212 kB
  • sloc: cpp: 199,103; xml: 56,065; ansic: 6,162; sh: 287; makefile: 26
file content (254 lines) | stat: -rw-r--r-- 5,927 bytes parent folder | download
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// Test Suite for Orientation::isCCW() function
// Ported from JTS junit/algorithm/IsCCWTest.java

// tut
#include <tut/tut.hpp>
// geos
#include <geos/algorithm/Orientation.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/Coordinate.h>
#include <geos/io/WKTReader.h>
#include <geos/io/WKBReader.h>
// std
#include <string>
#include <memory>
#include <sstream>

using namespace geos::algorithm;

namespace tut {
//
// Test Group
//

struct test_isccw_data {
    typedef std::unique_ptr<geos::geom::Geometry> GeometryPtr;

    geos::io::WKTReader reader_;
    geos::io::WKBReader breader_;

    test_isccw_data()
    {
    }

    ~test_isccw_data()
    {
    }

    void
    checkCCW(bool expectedCCW, const std::string& wkt)
    {
        auto poly = reader_.read<geos::geom::Polygon>(wkt);
        ensure("WKT must be POLYGON)", poly != nullptr);
        const geos::geom::CoordinateSequence* cs = poly->getExteriorRing()->getCoordinatesRO();
        bool actualCCW = Orientation::isCCW(cs);
        ensure_equals("CoordinateSequence isCCW", expectedCCW, actualCCW);
    }

    void
    checkCCWArea(bool expectedCCWArea, const std::string& wkt)
    {
        auto poly = reader_.read<geos::geom::Polygon>(wkt);
        ensure("WKT must be POLYGON)", poly != nullptr);
        const geos::geom::CoordinateSequence* cs = poly->getExteriorRing()->getCoordinatesRO();
        bool actualCCWArea = Orientation::isCCWArea(cs);
        ensure_equals("CoordinateSequence isCCWArea", expectedCCWArea, actualCCWArea);
    }

    void
    checkHexOrientationCCW(bool expectedCCW, std::istringstream& wkt)
    {
        GeometryPtr geom(breader_.readHEX(wkt));
        auto cs = geom->getCoordinates();
        bool actualCCW = Orientation::isCCW(cs.get());
        ensure_equals("CoordinateSequence isCCW", expectedCCW, actualCCW);
    }

};

typedef test_group<test_isccw_data> group;
typedef group::object object;

group test_isccw_group("geos::algorithm::CGAlgorithms::OrientationIsCCW");

//
// Test Cases
//

// 1 - Test if coordinates of polygon are counter-clockwise oriented
template<>
template<>
void object::test<1>
()
{
    const std::string wkt("POLYGON ((60 180, 140 240, 140 240, 140 240, 200 180, 120 120, 60 180))");
    checkCCW(false, wkt);
}

// 2 - Test if coordinates of polygon are counter-clockwise oriented
template<>
template<>
void object::test<2>
()
{
    const std::string wkt("POLYGON ((60 180, 140 120, 100 180, 140 240, 60 180))");
    checkCCW(true, wkt);
}

// 3 - Test the same polygon as in test No 2 but with duplicated top point
template<>
template<>
void object::test<3>
()
{
    const std::string wkt("POLYGON ((60 180, 140 120, 100 180, 140 240, 140 240, 60 180))");
    checkCCW(true, wkt);
}

// 4 - Test orientation the narrow (almost collapsed) ring
//     resulting in GEOS during execution of the union described
//     in http://trac.osgeo.org/geos/ticket/398
template<>
template<>
void object::test<4>
()
{
    std::istringstream
    wkt("0102000000040000000000000000000000841D588465963540F56BFB214F0341408F26B714B2971B40F66BFB214F0341408C26B714B2971B400000000000000000841D588465963540");
    checkHexOrientationCCW(true, wkt);
}

// 5 - Test orientation the narrow (almost collapsed) ring
//     resulting in JTS during execution of the union described
//     in http://trac.osgeo.org/geos/ticket/398
template<>
template<>
void object::test<5>
()
{
    std::istringstream
    wkt("0102000000040000000000000000000000841D588465963540F56BFB214F0341408F26B714B2971B40F66BFB214F0341408E26B714B2971B400000000000000000841D588465963540");
    checkHexOrientationCCW(true, wkt);
}

// testCCWSmall
template<>
template<>
void object::test<6>
()
{
    const std::string wkt("POLYGON ((1 1, 9 1, 5 9, 1 1))");
    checkCCW(true, wkt);
}

// testFlatTopSegment
template<>
template<>
void object::test<7>
()
{
    const std::string wkt("POLYGON ((100 200, 200 200, 200 100, 100 100, 100 200))");
    checkCCW(false, wkt);
}

// testFlatMultipleTopSegment
template<>
template<>
void object::test<8>
()
{
    const std::string wkt("POLYGON ((100 200, 127 200, 151 200, 173 200, 200 200, 100 100, 100 200))");
    checkCCW(false, wkt);
}

// testDegenerateRingHorizontal
template<>
template<>
void object::test<9>
()
{
    const std::string wkt("POLYGON ((100 200, 100 200, 200 200, 100 200))");
    checkCCW(false, wkt);
}

// testDegenerateRingAngled
template<>
template<>
void object::test<10>
()
{
    const std::string wkt("POLYGON ((100 100, 100 100, 200 200, 100 100))");
    checkCCW(false, wkt);
}

// testDegenerateRingVertical
template<>
template<>
void object::test<11>
()
{
    const std::string wkt("POLYGON ((200 100, 200 100, 200 200, 200 100))");
    checkCCW(false, wkt);
}

/**
* This case is an invalid ring, so answer is a default value
*/
// testTopAngledSegmentCollapse
template<>
template<>
void object::test<12>
()
{
    const std::string wkt("POLYGON ((10 20, 61 20, 20 30, 50 60, 10 20))");
    checkCCW(false, wkt);
}

// testABATopFlatSegmentCollapse
template<>
template<>
void object::test<13>
()
{
    const std::string wkt("POLYGON ((71 0, 40 40, 70 40, 40 40, 20 0, 71 0))");
    checkCCW(true, wkt);
}

// testABATopFlatSegmentCollapseMiddleStart
template<>
template<>
void object::test<14>
()
{
    const std::string wkt("POLYGON ((90 90, 50 90, 10 10, 90 10, 50 90, 90 90))");
    checkCCW(true, wkt);
}

// testMultipleTopFlatSegmentCollapseSinglePoint
template<>
template<>
void object::test<15>
()
{
    const std::string wkt("POLYGON ((100 100, 200 100, 150 200, 170 200, 200 200, 100 200, 150 200, 100 100))");
    checkCCW(true, wkt);
}

// testMultipleTopFlatSegmentCollapseFlatTop
template<>
template<>
void object::test<16>
()
{
    const std::string wkt("POLYGON ((10 10, 90 10, 70 70, 90 70, 10 70, 30 70, 50 70, 10 10))");
    checkCCW(true, wkt);
}




} // namespace tut