File: MaximumInscribedCircleTest.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 (321 lines) | stat: -rw-r--r-- 8,017 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// Test Suite for geos::algorithm::construct::MaximumInscribedCircle

#include <tut/tut.hpp>
// geos
#include <geos/operation/distance/IndexedFacetDistance.h>
#include <geos/algorithm/construct/MaximumInscribedCircle.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/Dimension.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/LineString.h>
#include <geos/geom/PrecisionModel.h>
#include <geos/io/WKTReader.h>
#include <geos/io/WKTWriter.h>
#include <geos/util.h>
// std
#include <string>
#include <memory>



using namespace geos;
using namespace geos::geom;

using geos::algorithm::construct::MaximumInscribedCircle;

namespace tut {
//
// Test Group
//

// dummy data, not used
struct test_mic_data {
    geos::geom::Geometry* geom_;
    geos::geom::PrecisionModel pm_;
    geos::geom::GeometryFactory::Ptr factory_;
    geos::io::WKTReader reader_;
    geos::io::WKTWriter writer_;

    test_mic_data():
        geom_(nullptr),
        pm_(geos::geom::PrecisionModel::FLOATING),
        factory_(GeometryFactory::create(&pm_, 0)),
        reader_(factory_.get())
    {}

    ~test_mic_data()
    {
        factory_->destroyGeometry(geom_);
        geom_ = nullptr;
    }

    void
    ensure_equals_coordinate(const geos::geom::Coordinate &lhs,
                             const geos::geom::Coordinate &rhs, double tolerance)
    {
        ensure_equals("x coordinate does not match", lhs.x, rhs.x, tolerance);
        ensure_equals("y coordinate does not match", lhs.y, rhs.y, tolerance);
    }

    /**
     * A coarse distance check, mainly testing
     * that there is not a huge number of iterations.
     * (This will be revealed by CI taking a very long time!)
     */
    void
    checkCircle(std::string wkt, double tolerance)
    {
        std::unique_ptr<Geometry> geom(reader_.read(wkt));
        MaximumInscribedCircle mic(geom.get(), tolerance);
        std::unique_ptr<Point> centerPoint = mic.getCenter();
        std::unique_ptr<Geometry> bdy = geom->getBoundary();
        double dist = bdy->distance(centerPoint.get());
        //std::cout << dist << std::endl;
        ensure(dist < 2 * tolerance);
    }

    void
    checkCircle(const Geometry *geom, double tolerance, double x, double y, double expectedRadius)
    {
        MaximumInscribedCircle mic(geom, tolerance);
        std::unique_ptr<Point> centerPoint = mic.getCenter();
        Coordinate centerPt(*centerPoint->getCoordinate());
        Coordinate expectedCenter(x, y);
        ensure_equals_coordinate(centerPt, expectedCenter, tolerance);
        std::unique_ptr<LineString> radiusLine = mic.getRadiusLine();
        double actualRadius = radiusLine->getLength();
        ensure_equals("radius", actualRadius, expectedRadius, tolerance);
        const Coordinate& linePt0 = radiusLine->getCoordinateN(0);
        const Coordinate& linePt1 = radiusLine->getCoordinateN(1);

        // std::cout << std::endl;
        // std::cout << writer_.write(geom) << std::endl;
        // std::cout << writer_.write(radiusLine.get()) << std::endl;

        ensure_equals_coordinate(centerPt, linePt0, tolerance);
        Coordinate radiusPt(*mic.getRadiusPoint()->getCoordinate());
        ensure_equals_coordinate(radiusPt, linePt1, tolerance);
    }

    void
    checkCircle(std::string wkt, double tolerance, double x, double y, double expectedRadius)
    {
        std::unique_ptr<Geometry> geom(reader_.read(wkt));
        checkCircle(geom.get(), tolerance, x, y, expectedRadius);
    }

};

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

group test_mic_group("geos::algorithm::construct::MaximumInscribedCircle");

//
// testSquare
//
template<>
template<>
void object::test<1> ()
{
    checkCircle("POLYGON ((100 200, 200 200, 200 100, 100 100, 100 200))",
                0.001, 150, 150, 50);
}

//
// testDiamond
//
template<>
template<>
void object::test<2> ()
{
    checkCircle("POLYGON ((150 250, 50 150, 150 50, 250 150, 150 250))",
                0.001, 150, 150, 70.71 );
}

//
// testCircle
//
template<>
template<>
void object::test<3> ()
{
    std::unique_ptr<Geometry> geom(reader_.read("POINT (100 100)"));
    std::unique_ptr<Geometry> circle(geom->buffer(100, 20));
    // MIC radius is less than 100 because buffer boundary segments lie inside circle
    checkCircle(circle.get(), 0.01, 100, 100, 99.9229);
}

//
// testDoubleKite
//
template<>
template<>
void object::test<4> ()
{
    checkCircle("MULTIPOLYGON (((150 200, 100 150, 150 100, 250 150, 150 200)), ((400 250, 300 150, 400 50, 560 150, 400 250)))",
       0.001, 411.38877, 149.9996185, 78.7634662 );
}

//
// Invalid polygon collapsed to a line
//
template<>
template<>
void object::test<5> ()
{
    checkCircle("POLYGON ((100 100, 200 200, 100 100, 100 100))",
       0.01, 100, 100, 0 );
}


// //
// // Invalid polygon collapsed to a point
// //
template<>
template<>
void object::test<6> ()
{
     checkCircle("POLYGON ((100 100, 100 100, 100 100, 100 100))",
       0.01, 100, 100, 0 );
}

// //
// // Invalid polygon collapsed to a line
// //
template<>
template<>
void object::test<7> ()
{
     checkCircle("POLYGON((1 2, 1 2, 1 2, 1 2, 3 2, 1 2))",
       0.01, 1, 2, 0 );
}

// Exception thrown to avoid infinite loop with infinite envelope
template<>
template<>
void object::test<8> ()
{
    auto g1 = reader_.read("POLYGON ((0 0, 1 0, 1 1, 0 Inf, 0 0))");
    try {
        MaximumInscribedCircle mic(g1.get(), 1);
        mic.getCenter();
    } catch (const util::GEOSException & e) {
        ::geos::ignore_unused_variable_warning(e);
    }

    auto g2 = reader_.read("POLYGON ((0 0, 1 0, 2 NaN,  0 0))");
    try {
        MaximumInscribedCircle mic(g1.get(), 1);
        mic.getCenter();
    } catch (const util::GEOSException & e) {
        ::geos::ignore_unused_variable_warning(e);
    }
}

  /**
   * Tests that a nearly flat geometry doesn't make the initial cell grid huge.
   *
   * See https://github.com/libgeos/geos/issues/875
   */
// testNearlyFlat
template<>
template<>
void object::test<9> ()
{
    checkCircle("POLYGON ((59.3 100.00000000000001, 99.7 100.00000000000001, 99.7 100, 59.3 100, 59.3 100.00000000000001))",
       0.01 );
}

// testVeryThin
template<>
template<>
void object::test<10> ()
{
    checkCircle("POLYGON ((100 100, 200 300, 300 100, 450 250, 300 99.999999, 200 299.99999, 100 100))",
       0.01 );
}

//
// https://github.com/libgeos/geos/pull/1225
//
template<>
template<>
void object::test<11> ()
{
    checkCircle("POLYGON((0 -10,-7.07107 -7.07107,-10 0,-7.07107 7.07107,0 10,7.07107 7.07107,10 0,7.07107 -7.07107,0 -10))",
        0.1, 0, 0, 9.2387);
}


/* testTriangleRight() */
template<>
template<>
void object::test<12> ()
{
    checkCircle("POLYGON ((1 1, 1 9, 9 1, 1 1))",
        0.001, 3.343, 3.343, 2.343 );
}

/* testTriangleObtuse */
template<>
template<>
void object::test<13> ()
{
    checkCircle("POLYGON ((1 1, 1 9, 2 2, 1 1))",
        0.01, 1.485, 2.173, 0.485 );
}

/* testThinQuad */
template<>
template<>
void object::test<14> ()
{
    checkCircle("POLYGON ((1 2, 9 3, 9 1, 1 1, 1 2))",
        0.001, 8.0623, 1.9377, 0.93774 );
}

/* testChevron */
template<>
template<>
void object::test<15> ()
{
    checkCircle("POLYGON ((1 1, 6 9, 3.7 2.5, 9 1, 1 1))",
        0.001, 2.82, 2.008, 1.008 );
}

/* testChevronFat */
template<>
template<>
void object::test<16> ()
{
    checkCircle("POLYGON ((1 1, 6 9, 5.9 5, 9 1, 1 1))",
        0.001, 4.7545, 3.0809, 2.081 );
}

/* testQuadWithCollinearVertex */
template<>
template<>
void object::test<17> ()
{
    checkCircle("POLYGON ((1 5, 5 5, 9 5, 5 1, 1 5))",
        0.001, 5.0, 3.3431,  1.65685 );
}

/* postgis mic-box test */
template<>
template<>
void object::test<18> ()
{
    checkCircle("Polygon((0 0, 100 0, 99 98, 0 100, 0 0))",
        0.01, 49.495001, 49.495001, 49.495001 );
}

//-mic-box|POINT(49.5117 49.5117)|POINT(50.5111 98.9796)|49.4779
//+mic-box|POINT(49.495 49.495)|POINT(50.4947 98.9799)|49.4950



} // namespace tut