File: GEOSVoronoiDiagramTest.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 (186 lines) | stat: -rw-r--r-- 11,166 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
//
// Test Suite for C-API GEOSVoronoiDiagram

#include <tut/tut.hpp>
// geos
#include <geos_c.h>
// std
#include <cfenv>

#include "capi_test_utils.h"

namespace tut {
//
// Test Group
//

// Common data used in test cases.
struct test_capigeosvoronoidiagram_data : public capitest::utility {
    test_capigeosvoronoidiagram_data() {
    }


    void check_voronoi_ordered(const char* wkt)
    {
        GEOSGeometry* input = GEOSGeomFromWKT(wkt);
        GEOSGeometry* result = GEOSVoronoiDiagram(input, nullptr, 0, GEOS_VORONOI_PRESERVE_ORDER);

        ensure_equals(GEOSGetNumGeometries(result), GEOSGetNumCoordinates(input));

        const GEOSCoordSequence* seq = GEOSGeom_getCoordSeq(input);
        double x, y;
        for (int i = 0; i < GEOSGetNumGeometries(result); i++) {
        GEOSCoordSeq_getXY(seq, (unsigned int) i, &x, &y);
        GEOSGeometry* pt = GEOSGeom_createPointFromXY(x, y);
        const GEOSGeometry* cell = GEOSGetGeometryN(result, i);
        ensure(GEOSContains(cell, pt));
        GEOSGeom_destroy(pt);
        }

        GEOSGeom_destroy(result);
        GEOSGeom_destroy(input);
    }
};

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

group test_capigeosvoronoidiagram_group("capi::GEOSVoronoiDiagram");

//
// Test Cases
//

// Single point
template<>
template<>
void object::test<1>
()
{
    std::feclearexcept(FE_ALL_EXCEPT);

    geom1_ = GEOSGeomFromWKT("POINT(10 20)");

    geom2_ = GEOSVoronoiDiagram(geom1_, nullptr, 0, 0);
    ensure_equals(GEOSisEmpty(geom2_), 1);
    ensure_equals(GEOSGeomTypeId(geom2_), GEOS_GEOMETRYCOLLECTION);

    GEOSGeom_destroy(geom2_);
    geom2_ = GEOSVoronoiDiagram(geom1_, nullptr, 0, 1);
    ensure_geometry_equals(geom2_, "MULTILINESTRING EMPTY");

    ensure("FE_INVALID raised", !std::fetestexcept(FE_INVALID));
}

//More points:
template<>
template<>
void object::test<2>
()
{
    geom1_ = GEOSGeomFromWKT("MULTIPOINT ((280 300), (420 330), (380 230), (320 160))");

    geom2_ = GEOSVoronoiDiagram(geom1_, nullptr, 0, 0);
    ensure_geometry_equals(geom2_,
                      "GEOMETRYCOLLECTION (POLYGON ((110 175.71428571428572, 110 500, 310.35714285714283 500, 353.515625 298.59375, 306.875 231.96428571428572, 110 175.71428571428572)), POLYGON ((590 204, 590 -10, 589.1666666666666 -10, 306.875 231.96428571428572, 353.515625 298.59375, 590 204)), POLYGON ((110 -10, 110 175.71428571428572, 306.875 231.96428571428572, 589.1666666666666 -10, 110 -10)), POLYGON ((310.35714285714283 500, 590 500, 590 204, 353.515625 298.59375, 310.35714285714283 500)))");

    GEOSGeom_destroy(geom2_);
    geom2_ = GEOSVoronoiDiagram(geom1_, nullptr, 0, 1);
    ensure_geometry_equals(geom2_,
                           "MULTILINESTRING ((310.3571428571428 500, 353.515625 298.59375), (353.515625 298.59375, 306.875 231.9642857142857), (306.875 231.9642857142857, 110 175.7142857142857), (589.1666666666666 -10, 306.875 231.9642857142857), (353.515625 298.59375, 590 204))");
}
//Larger number of points:
template<>
template<>
void object::test<3>
()
{
    geom1_ = GEOSGeomFromWKT("MULTIPOINT ((170 270), (270 270), (230 310), (180 330), (250 340), (315 318), (330 260), (240 170), (220 220), (270 220))");

    geom2_ = GEOSVoronoiDiagram(geom1_, nullptr, 0, 0);
    ensure_geometry_equals(geom2_,
                           "GEOMETRYCOLLECTION (POLYGON ((0 329.1666666666667, 0 510, 190 510, 213.94736842105263 342.36842105263156, 195.625 296.5625, 0 329.1666666666667)), POLYGON ((0 76.50000000000001, 0 329.1666666666667, 195.625 296.5625, 216 266, 88.33333333333333 138.33333333333334, 0 76.50000000000001)), POLYGON ((216 266, 195.625 296.5625, 213.94736842105263 342.36842105263156, 267 307, 225 265, 216 266)), POLYGON ((245 245, 225 265, 267 307, 275.9160583941606 309.54744525547443, 303.1666666666667 284, 296.6666666666667 245, 245 245)), POLYGON ((225 265, 245 245, 245 201, 88.33333333333333 138.33333333333334, 216 266, 225 265)), POLYGON ((0 0, 0 76.50000000000001, 88.33333333333333 138.33333333333334, 245 201, 380 120, 500 0, 0 0)), POLYGON ((190 510, 343.76153846153846 510, 275.9160583941606 309.54744525547443, 267 307, 213.94736842105263 342.36842105263156, 190 510)), POLYGON ((245 201, 245 245, 296.6666666666667 245, 380 120, 245 201)), POLYGON ((343.76153846153846 510, 500 510, 500 334.9051724137931, 303.1666666666667 284, 275.9160583941606 309.54744525547443, 343.76153846153846 510)), POLYGON ((500 334.9051724137931, 500 0, 380 120, 296.6666666666667 245, 303.1666666666667 284, 500 334.9051724137931)))");

    GEOSGeom_destroy(geom2_);
    geom2_ = GEOSVoronoiDiagram(geom1_, nullptr, 0, 1);
    ensure_geometry_equals(geom2_,
                           "MULTILINESTRING ((190 510, 213.9473684210526 342.3684210526316), (213.9473684210526 342.3684210526316, 195.625 296.5625), (195.625 296.5625, 0 329.1666666666667), (195.625 296.5625, 216 266), (216 266, 88.33333333333333 138.3333333333333), (88.33333333333333 138.3333333333333, 0 76.50000000000001), (213.9473684210526 342.3684210526316, 267 307), (267 307, 225 265), (225 265, 216 266), (245 245, 225 265), (267 307, 275.9160583941606 309.5474452554744), (275.9160583941606 309.5474452554744, 303.1666666666667 284), (303.1666666666667 284, 296.6666666666667 245), (296.6666666666667 245, 245 245), (245 245, 245 201), (245 201, 88.33333333333333 138.3333333333333), (245 201, 380 120), (380 120, 500 0), (343.7615384615385 510, 275.9160583941606 309.5474452554744), (296.6666666666667 245, 380 120), (500 334.9051724137931, 303.1666666666667 284))");
}
//Test with non-zero Tolerance value
template<>
template<>
void object::test<4>
()
{
    geom1_ = GEOSGeomFromWKT("MULTIPOINT ((150 210), (210 270), (150 220), (220 210), (215 269))");

    geom2_ = GEOSVoronoiDiagram(geom1_, nullptr, 10, 0);
    ensure_geometry_equals(geom2_,
                           "GEOMETRYCOLLECTION (POLYGON ((290 252.5, 290 140, 185 140, 185 215, 187.9268292682927 235.4878048780488, 290 252.5)), POLYGON ((80 215, 80 340, 100.83333333333336 340, 187.9268292682927 235.4878048780488, 185 215, 80 215)), POLYGON ((80 140, 80 215, 185 215, 185 140, 80 140)), POLYGON ((100.83333333333336 340, 290 340, 290 252.5, 187.9268292682927 235.4878048780488, 100.83333333333336 340)))");

    GEOSGeom_destroy(geom2_);
    geom2_ = GEOSVoronoiDiagram(geom1_, nullptr, 10, 1);
    ensure_geometry_equals(geom2_,
                           "MULTILINESTRING ((185 215, 187.9268292682927 235.4878048780488), (187.9268292682927 235.4878048780488, 290 252.5), (185 140, 185 215), (185 215, 80 215), (100.8333333333334 340, 187.9268292682927 235.4878048780488))");
}
template<>
template<>
void object::test<5>
()
{
    geom1_ = GEOSGeomFromWKT("MULTIPOINT ((40 420), (50 420), (210 290), (300 360), (350 150), (170 70), (134 135) ,(305 359), (351 145), (175 71))");

    geom2_ = GEOSVoronoiDiagram(geom1_, nullptr, 10, 0);
    ensure_geometry_equals(geom2_,
                           "GEOMETRYCOLLECTION (POLYGON ((-310 146.559649122807, -310 770, 45 770, 45 263.64736842105265, -310 146.559649122807)), POLYGON ((-310 -153.37692307692305, -310 146.559649122807, 45 263.64736842105265, 59.16911764705881 267.8235294117647, 239.43506493506493 179.43506493506493, 241.34156378600824 151.98148148148147, -310 -153.37692307692305)), POLYGON ((45 770, 266.20000000000005 770, 181.94323144104806 418.9301310043668, 59.16911764705881 267.8235294117647, 45 263.64736842105265, 45 770)), POLYGON ((59.16911764705881 267.8235294117647, 181.94323144104806 418.9301310043668, 311.875 251.875, 239.43506493506493 179.43506493506493, 59.16911764705881 267.8235294117647)), POLYGON ((-310 -280, -310 -153.37692307692305, 241.34156378600824 151.98148148148147, 433.3333333333333 -280, -310 -280)), POLYGON ((266.20000000000005 770, 701 770, 701 344.5238095238096, 311.875 251.875, 181.94323144104806 418.9301310043668, 266.20000000000005 770)), POLYGON ((701 344.5238095238096, 701 -280, 433.3333333333333 -280, 241.34156378600824 151.98148148148147, 239.43506493506493 179.43506493506493, 311.875 251.875, 701 344.5238095238096)))");

    GEOSGeom_destroy(geom2_);
    geom2_ = GEOSVoronoiDiagram(geom1_, nullptr, 10, 1);
    ensure_geometry_equals(geom2_,
                           "MULTILINESTRING ((45 770, 45 263.6473684210526), (45 263.6473684210526, -310 146.559649122807), (45 263.6473684210526, 59.16911764705881 267.8235294117647), (59.16911764705881 267.8235294117647, 239.4350649350649 179.4350649350649), (239.4350649350649 179.4350649350649, 241.3415637860082 151.9814814814815), (241.3415637860082 151.9814814814815, -310 -153.376923076923), (266.2 770, 181.9432314410481 418.9301310043668), (181.9432314410481 418.9301310043668, 59.16911764705881 267.8235294117647), (181.9432314410481 418.9301310043668, 311.875 251.875), (311.875 251.875, 239.4350649350649 179.4350649350649), (241.3415637860082 151.9814814814815, 433.3333333333333 -280), (701 344.5238095238096, 311.875 251.875))");
}
template<>
template<>
void object::test<6>
()
{
    geom1_ = GEOSGeomFromWKT("MULTIPOINT ((123 245), (165 313), (240 310), (260 260), (180 210), (240 210))");
    geom2_ = GEOSVoronoiDiagram(geom1_, nullptr, 0, 1);
    ensure_geometry_equals(geom2_,
                           "MULTILINESTRING ((-14 376.5882352941176, 172.3651328095773 261.4803591470258), (172.3651328095773 261.4803591470258, 56.63157894736844 73), (172.3651328095773 261.4803591470258, 200.6640625 265.6015625), (200.6640625 265.6015625, 201 265.4), (201 265.4, 210 251), (210 251, 210 73), (208.04 450, 200.6640625 265.6015625), (397 343.8, 201 265.4), (210 251, 397 176.2))");
}

template<>
template<>
void object::test<7>
()
{
    /* https://trac.osgeo.org/geos/ticket/859 */
    std::string hex = "0104000020110F0000130000000101000000F06D31DFAA2E1B4131A0CD7FB94F604101010000000FE54E38B80E1B41C845F1FBB85260410101000000F06D31DFAA2E1B41CD873BECB854604101010000000FE54E38B80E1B4131A0CD7FB94F604101010000000FE54E38B80E1B410B4DECBCB94E604101010000000FE54E38B80E1B41452CADCDB85360410101000000F06D31DFAA2E1B41BAD5931AB950604101010000000FE54E38B80E1B417251B946BA4D604101010000000FE54E38B80E1B412209A9E2B85560410101000000F06D31DFAA2E1B41C845F1FBB85260410101000000F06D31DFAA2E1B41C2863DA9B95860410101000000F06D31DFAA2E1B413D7BF725B95660410101000000F06D31DFAA2E1B41E9C72241B95760410101000000F06D31DFAA2E1B4172D01602B951604101010000000FE54E38B80E1B4172D01602B951604101010000000FE54E38B80E1B41CD873BECB854604101010000000FE54E38B80E1B41BAD5931AB95060410101000000F06D31DFAA2E1B412209A9E2B85560410101000000F06D31DFAA2E1B41452CADCDB8536041";
    geom1_ = GEOSGeomFromHEX_buf((const unsigned char*)(hex.c_str()), hex.length());
    geom2_ = GEOSVoronoiDiagram(geom1_, nullptr, 0, 1);
}

// Ordered output
template<>
template<>
void object::test<8>
()
{
    check_voronoi_ordered("LINESTRING (1 1, 3 3, 2 2)");
    check_voronoi_ordered("LINESTRING (1 1, 2 2, 3 3)");
}

template<>
template<>
void object::test<9>
()
{
    input_ = fromWKT("CIRCULARSTRING (0 0, 1 1, 2 0)");
    ensure(input_);

    result_ = GEOSVoronoiDiagram(input_, nullptr, 0, 0);
    ensure("curved geometry not supported", result_ == nullptr);
}

} // namespace tut