File: ticket_9081.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (239 lines) | stat: -rw-r--r-- 8,123 bytes parent folder | download | duplicates (7)
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
// Boost.Geometry (aka GGL, Generic Geometry Library) // Robustness Test

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

// 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)

// Adapted from: the attachment of ticket 9081

#define CHECK_SELF_INTERSECTIONS
#define LIST_WKT

 #include <iomanip>
 #include <iostream>
 #include <vector>
 #include <boost/geometry.hpp>
 #include <boost/geometry/geometries/point_xy.hpp>
 #include <boost/geometry/geometries/polygon.hpp>
 #include <boost/geometry/geometries/register/point.hpp>
 #include <boost/geometry/geometries/register/ring.hpp>
 #include <boost/geometry/io/wkt/wkt.hpp>
 #include <boost/geometry/geometries/multi_polygon.hpp>

#include <boost/foreach.hpp>
#include <boost/timer.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/geometry/io/svg/svg_mapper.hpp>
#include <fstream>


typedef boost::geometry::model::d2::point_xy<double> pt;
typedef boost::geometry::model::polygon<pt> polygon;
typedef boost::geometry::model::segment<pt> segment;
typedef boost::geometry::model::multi_polygon<polygon> multi_polygon;

template <typename Geometry>
inline void debug_with_svg(int index, char method, Geometry const& a, Geometry const& b, std::string const& headera, std::string const& headerb)
{
    multi_polygon output;
    try
    {
        switch(method)
        {
            case 'i': boost::geometry::intersection(a, b, output); break;
            case 'u': boost::geometry::union_(a, b, output); break;
            case 'd': boost::geometry::difference(a, b, output); break;
            case 'v': boost::geometry::difference(b, a, output); break;
            default : return;
        }
    }
    catch(...)
    {}

    std::ostringstream filename;
    filename << "ticket_9081_" << method << "_" << (1000000 + index) << ".svg";
    std::ofstream svg(filename.str().c_str());

    boost::geometry::svg_mapper<pt> mapper(svg, 400, 400);
    mapper.add(a);
    mapper.add(b);

    mapper.map(a, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2");
    mapper.map(b, "fill-opacity:0.3;fill:rgb(51,51,153);stroke:rgb(51,51,153);stroke-width:2");
    BOOST_FOREACH(polygon const& g, output)
    {
        mapper.map(g, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
    }

    std::ostringstream out;
    out << headera << std::endl << headerb;
    mapper.map(boost::geometry::return_centroid<pt>(a), "fill:rgb(152,204,0);stroke:rgb(153,204,0);stroke-width:0.1", 3);
    mapper.map(boost::geometry::return_centroid<pt>(b), "fill:rgb(51,51,153);stroke:rgb(153,204,0);stroke-width:0.1", 3);
    mapper.text(boost::geometry::return_centroid<pt>(a), headera, "fill:rgb(0,0,0);font-family:Arial;font-size:10px");
    mapper.text(boost::geometry::return_centroid<pt>(b), headerb, "fill:rgb(0,0,0);font-family:Arial;font-size:10px");
}

int main()
{
    int num_orig = 50;
    int num_rounds = 30000;
    srand(1234);
    std::cout << std::setprecision(16);
    std::map<int, std::string> genesis;
    int pj;


    std::string wkt1, wkt2, operation;

    try
    {


    boost::timer t;
    std::vector<multi_polygon> poly_list;

    for(int i=0;i<num_orig;i++)
    {
        multi_polygon mp;
        polygon p;
        for(int j=0;j<3;j++)
        {
            double x=(double)rand()/RAND_MAX;
            double y=(double)rand()/RAND_MAX;
            p.outer().push_back(pt(x,y));
        }
        boost::geometry::correct(p);
        mp.push_back(p);
        boost::geometry::detail::overlay::has_self_intersections(mp);

        std::ostringstream out;
        out << "original " << poly_list.size();
        genesis[poly_list.size()] = out.str();
        poly_list.push_back(mp);

#ifdef LIST_WKT
        std::cout << "Original " << i << " " << boost::geometry::wkt(p) << std::endl;
#endif
    }


    for(int j=0;j<num_rounds;j++)
    {
        if (j % 100 == 0) { std::cout << " " << j; }
        pj = j;
        int a = rand() % poly_list.size();
        int b = rand() % poly_list.size();

        debug_with_svg(j, 'i', poly_list[a], poly_list[b], genesis[a], genesis[b]);

        { std::ostringstream out; out << boost::geometry::wkt(poly_list[a]); wkt1 = out.str(); }
        { std::ostringstream out; out << boost::geometry::wkt(poly_list[b]); wkt2 = out.str(); }

        multi_polygon mp_i, mp_u, mp_d, mp_e;
        operation = "intersection";
        boost::geometry::intersection(poly_list[a],poly_list[b],mp_i);
        operation = "intersection";
        boost::geometry::union_(poly_list[a],poly_list[b],mp_u);
        operation = "difference";
        boost::geometry::difference(poly_list[a],poly_list[b],mp_d);
        boost::geometry::difference(poly_list[b],poly_list[a],mp_e);

#ifdef LIST_WKT
        std::cout << j << std::endl;
        std::cout << "  Genesis a " << genesis[a] << std::endl;
        std::cout << "  Genesis b " << genesis[b] << std::endl;
        std::cout << "  Intersection " << boost::geometry::wkt(mp_i) << std::endl;
        std::cout << "  Difference a " << boost::geometry::wkt(mp_d) << std::endl;
        std::cout << "  Difference b " << boost::geometry::wkt(mp_e) << std::endl;
#endif

#ifdef CHECK_SELF_INTERSECTIONS
        try
        {
            boost::geometry::detail::overlay::has_self_intersections(mp_i);
        }
        catch(...)
        {
            std::cout << "FAILED TO INTERSECT " << j << std::endl;
            std::cout << boost::geometry::wkt(poly_list[a]) << std::endl;
            std::cout << boost::geometry::wkt(poly_list[b]) << std::endl;
            std::cout << boost::geometry::wkt(mp_i) << std::endl;
            try
            {
                boost::geometry::detail::overlay::has_self_intersections(mp_i);
            }
            catch(...)
            {
            }
            break;
        }

        try
        {
            boost::geometry::detail::overlay::has_self_intersections(mp_d);
        }
        catch(...)
        {
            std::cout << "FAILED TO SUBTRACT " << j << std::endl;
            std::cout << boost::geometry::wkt(poly_list[a]) << std::endl;
            std::cout << boost::geometry::wkt(poly_list[b]) << std::endl;
            std::cout << boost::geometry::wkt(mp_d) << std::endl;
            break;
        }
        try
        {
            boost::geometry::detail::overlay::has_self_intersections(mp_e);
        }
        catch(...)
        {
            std::cout << "FAILED TO SUBTRACT " << j << std::endl;
            std::cout << boost::geometry::wkt(poly_list[b]) << std::endl;
            std::cout << boost::geometry::wkt(poly_list[a]) << std::endl;
            std::cout << boost::geometry::wkt(mp_e) << std::endl;
            break;
        }
#endif

        if(boost::geometry::area(mp_i) > 0)
        {
            std::ostringstream out;
            out << j << " intersection(" << genesis[a] << " , " << genesis[b] << ")";
            genesis[poly_list.size()] = out.str();
            poly_list.push_back(mp_i);
        }
        if(boost::geometry::area(mp_d) > 0)
        {
            std::ostringstream out;
            out << j << " difference(" << genesis[a] << " - " << genesis[b] << ")";
            genesis[poly_list.size()] = out.str();
            poly_list.push_back(mp_d);
        }
        if(boost::geometry::area(mp_e) > 0)
        {
            std::ostringstream out;
            out << j << " difference(" << genesis[b] << " - " << genesis[a] << ")";
            genesis[poly_list.size()] = out.str();
            poly_list.push_back(mp_e);
        }
    }

    std::cout << "FINISHED " << t.elapsed() << std::endl;

    }
    catch(std::exception const& e)
    {
        std::cout << e.what()
            << " in " << operation << " at " << pj << std::endl
            << wkt1 << std::endl
            << wkt2 << std::endl
            << std::endl;
    }
    catch(...)
    {
        std::cout << "Other exception" << std::endl;
    }

    return 0;
}