File: dissolver.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (220 lines) | stat: -rw-r--r-- 6,777 bytes parent folder | download | duplicates (5)
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
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test

// Copyright (c) 2010-2015 Barend Gehrels, Amsterdam, the Netherlands.

// This file was modified by Oracle on 2021.
// Modifications copyright (c) 2021 Oracle and/or its affiliates.
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle

// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <iostream>
#include <string>


#include <geometry_test_common.hpp>

#include <boost/geometry/algorithms/detail/overlay/dissolver.hpp>

#include <boost/geometry/strategies/strategies.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>
#include <boost/geometry/io/wkt/read.hpp>


#include <test_common/test_point.hpp>


#if defined(TEST_WITH_SVG)
#  include <boost/geometry/io/svg/svg_mapper.hpp>
#  include <boost/geometry/io/svg/write_svg_multi.hpp>
#endif

// Collection might be a multi-geometry, or std::vector<ring>
template <typename GeometryOut, typename Collection, typename T>
void test_dissolve_plusmin(std::string const& caseid, Collection const& input,
            T const& expected_positive_area,
            T const& expected_negative_area)
{
    using geometry_type = typename boost::range_value<GeometryOut>::type;
    using point_type = bg::point_type_t<geometry_type>;

    GeometryOut output;
    bg::dissolver(input, output);

    T zero = T();
    T positive_area = T();
    T negative_area = T();

    for (geometry_type const& geometry : output)
    {
        T a = bg::area(geometry);
        if (a > zero)
        {
            positive_area += a;
        }
        else
        {
            negative_area += a;
        }
    }

    BOOST_CHECK_CLOSE(positive_area, expected_positive_area, 0.001);
    BOOST_CHECK_CLOSE(negative_area, expected_negative_area, 0.001);


#if defined(TEST_WITH_SVG)
    {
        std::ostringstream filename;
        filename << "dissolve_plusmin_"
            << caseid << ".svg";

        std::ofstream svg(filename.str().c_str());

        bg::svg_mapper<point_type> mapper(svg, 500, 500);

        typedef typename boost::range_value<Collection>::type value_type;
        for (value_type const& geometry : input)
        {
            mapper.add(geometry);
        }

        for (value_type const& geometry : input)
        {
            mapper.map(geometry,
                "opacity:0.6;fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:0.5");
        }
        for (geometry_type const& geometry : output)
        {
            mapper.map(geometry,
                bg::area(geometry) > 0
                ? "opacity:0.5;fill:none;stroke:rgb(255,0,0);stroke-width:5"
                : "opacity:0.5;fill:none;stroke:rgb(0,0,255);stroke-width:5"
                );
        }
    }
#endif

}

template <typename MultiPolygon, typename T>
void test_geometry(std::string const& caseid, std::string const& wkt,
            T const& expected_positive_area,
            T const& expected_negative_area = T())
{

    MultiPolygon multi_polygon;
    bg::read_wkt(wkt, multi_polygon);

    // Test std::vector<Polygon> (= multi_polygon)
    test_dissolve_plusmin<MultiPolygon>(caseid, multi_polygon,
        expected_positive_area,
        expected_negative_area);

    // Test std::vector<ring>
    {
        using polygon_type = typename boost::range_value<MultiPolygon>::type;
        using ring_type = bg::ring_type_t<MultiPolygon>;
        std::vector<ring_type> rings;
        for (polygon_type const& polygon : multi_polygon)
        {
            rings.push_back(bg::exterior_ring(polygon));
        }

        test_dissolve_plusmin<MultiPolygon>(caseid + "_rings", rings,
            expected_positive_area,
            expected_negative_area);
    }

    // Test different combinations
#define BOOST_GEOMETRY_TEST_PERMUTATIONS
#ifdef BOOST_GEOMETRY_TEST_PERMUTATIONS

    int n = multi_polygon.size();

    // test them in all orders
    std::vector<int> indices;
    for (int i = 0; i < n; i++)
    {
        indices.push_back(i);
    }
    int permutation = 0;
    do
    {
        std::ostringstream out;
        out << caseid;
        MultiPolygon multi_polygon2;
        for (int i = 0; i < n; i++)
        {
            int index = indices[i];
            out << "_" << index;
            multi_polygon2.push_back(multi_polygon[index]);
        }
        test_dissolve_plusmin<MultiPolygon>(out.str(), multi_polygon2, expected_positive_area,
                expected_negative_area);
    } while (std::next_permutation(indices.begin(), indices.end()));
#endif
}

template <typename Point>
void test_all()
{
    using polygon = bg::model::polygon<Point>;
    using multi_polygon = bg::model::multi_polygon<polygon>;

    test_geometry<multi_polygon>("simplex_one",
        "MULTIPOLYGON(((0 0,1 4,4 1,0 0)))",
        7.5);

    test_geometry<multi_polygon>("simplex_two",
        "MULTIPOLYGON(((0 0,1 4,4 1,0 0)),((2 2,3 6,6 3,2 2)))",
        14.7);
    test_geometry<multi_polygon>("simplex_three",
        "MULTIPOLYGON(((0 0,1 4,4 1,0 0)),((2 2,3 6,6 3,2 2)),((3 4,5 6,6 2,3 4)))",
        16.7945);
    test_geometry<multi_polygon>("simplex_four",
        "MULTIPOLYGON(((0 0,1 4,4 1,0 0)),((2 2,3 6,6 3,2 2)),((3 4,5 6,6 2,3 4)),((5 5,7 7,8 4,5 5)))",
        20.7581);

    // disjoint
    test_geometry<multi_polygon>("simplex_disjoint",
        "MULTIPOLYGON(((0 0,1 4,4 1,0 0)),((1 6,2 10,5 7,1 6)),((3 4,5 6,6 2,3 4)),((6 5,8 7,9 4,6 5)))",
        24.0);

    // new hole of four
    test_geometry<multi_polygon>("new_hole",
        "MULTIPOLYGON(((0 0,1 4,4 1,0 0)),((2 2,3 6,6 3,2 2)),((3 4,5 6,6 2,3 4)),((3 1,5 4,8 4,3 1)))",
        19.5206);

    // intersection of positive/negative ring
    test_geometry<multi_polygon>("plus_min_one",
        "MULTIPOLYGON(((0 0,1 4,4 1,0 0)),((2 2,6 3,3 6,2 2)))",
        7.5, -7.2);

    // negative ring within a positive ring
    test_geometry<multi_polygon>("plus_min_one_within",
        "MULTIPOLYGON(((0 0,1 7,7 3,0 0)),((1 2,4 4,2 5,1 2)))",
        23.0, -3.5);

    // from buffer
    test_geometry<multi_polygon>("from_buffer_1",
        "MULTIPOLYGON(((2.4 3.03431,1.71716 3.71716,2.4 4,2.4 3.03431))"
            ",((2.4 1.96569,2.4 1,1.71716 1.28284,2.4 1.96569))"
            ",((2.93431 2.5,2.4 3.03431,2.4 1.96569,2.93431 2.5))"
            ",((3.06569 2.5,3 2.43431,2.93431 2.5,3 2.56569,3.06569 2.5))"
            ",((-0.4 5.4,4.4 5.4,4.4 3.83431,3.06569 2.5,4.4 1.16569,4.4 -0.4,-0.4 -0.4,-0.4 5.4)))"
            ,
        26.0596168239, -0.2854871761);

}

int test_main(int, char* [])
{
    test_all<bg::model::d2::point_xy<double> >();
    return 0;
}