1 | #include <iomanip>
|
---|
2 | #include <iostream>
|
---|
3 | #include <vector>
|
---|
4 | #include <boost/geometry.hpp>
|
---|
5 | #include <boost/geometry/geometries/point_xy.hpp>
|
---|
6 | #include <boost/geometry/geometries/polygon.hpp>
|
---|
7 | #include <boost/geometry/geometries/register/point.hpp>
|
---|
8 | #include <boost/geometry/geometries/register/ring.hpp>
|
---|
9 | #include <boost/geometry/io/wkt/wkt.hpp>
|
---|
10 | #include <boost/geometry/multi/geometries/multi_polygon.hpp>
|
---|
11 |
|
---|
12 | typedef boost::geometry::model::d2::point_xy<double> pt;
|
---|
13 | typedef boost::geometry::model::polygon<pt> polygon;
|
---|
14 | typedef boost::geometry::model::multi_polygon<polygon> multi_polygon;
|
---|
15 |
|
---|
16 | int main()
|
---|
17 | {
|
---|
18 | int num_orig=50;
|
---|
19 | int num_rounds=100000;
|
---|
20 | srand(1234);
|
---|
21 | std::cout<<std::setprecision(16);
|
---|
22 |
|
---|
23 | std::vector<multi_polygon> poly_list;
|
---|
24 |
|
---|
25 | for(int i=0;i<num_orig;i++)
|
---|
26 | {
|
---|
27 | multi_polygon mp;
|
---|
28 | polygon p;
|
---|
29 | for(int j=0;j<3;j++)
|
---|
30 | {
|
---|
31 | double x=(double)rand()/RAND_MAX;
|
---|
32 | double y=(double)rand()/RAND_MAX;
|
---|
33 | p.outer().push_back(pt(x,y));
|
---|
34 | }
|
---|
35 | boost::geometry::correct(p);
|
---|
36 | mp.push_back(p);
|
---|
37 | boost::geometry::detail::overlay::has_self_intersections(mp);
|
---|
38 | poly_list.push_back(mp);
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 | for(int j=0;j<num_rounds;j++)
|
---|
43 | {
|
---|
44 | int a = rand() % poly_list.size();
|
---|
45 | int b = rand() % poly_list.size();
|
---|
46 | multi_polygon mp_i,mp_d,mp_e;
|
---|
47 | boost::geometry::intersection(poly_list[a],poly_list[b],mp_i);
|
---|
48 | boost::geometry::difference(poly_list[a],poly_list[b],mp_d);
|
---|
49 | boost::geometry::difference(poly_list[b],poly_list[a],mp_e);
|
---|
50 | try
|
---|
51 | {
|
---|
52 | boost::geometry::detail::overlay::has_self_intersections(mp_i);
|
---|
53 | }
|
---|
54 | catch(...)
|
---|
55 | {
|
---|
56 | std::cout<<"FAILED TO INTERSECT"<<std::endl;
|
---|
57 | std::cout<<boost::geometry::wkt(poly_list[a])<<std::endl;
|
---|
58 | std::cout<<boost::geometry::wkt(poly_list[b])<<std::endl;
|
---|
59 | std::cout<<boost::geometry::wkt(mp_i)<<std::endl;
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | try
|
---|
63 | {
|
---|
64 | boost::geometry::detail::overlay::has_self_intersections(mp_d);
|
---|
65 | }
|
---|
66 | catch(...)
|
---|
67 | {
|
---|
68 | std::cout<<"FAILED TO SUBTRACT"<<std::endl;
|
---|
69 | std::cout<<boost::geometry::wkt(poly_list[a])<<std::endl;
|
---|
70 | std::cout<<boost::geometry::wkt(poly_list[b])<<std::endl;
|
---|
71 | std::cout<<boost::geometry::wkt(mp_d)<<std::endl;
|
---|
72 | break;
|
---|
73 | }
|
---|
74 | try
|
---|
75 | {
|
---|
76 | boost::geometry::detail::overlay::has_self_intersections(mp_e);
|
---|
77 | }
|
---|
78 | catch(...)
|
---|
79 | {
|
---|
80 | std::cout<<"FAILED TO SUBTRACT"<<std::endl;
|
---|
81 | std::cout<<boost::geometry::wkt(poly_list[b])<<std::endl;
|
---|
82 | std::cout<<boost::geometry::wkt(poly_list[a])<<std::endl;
|
---|
83 | std::cout<<boost::geometry::wkt(mp_e)<<std::endl;
|
---|
84 | break;
|
---|
85 | }
|
---|
86 | if(boost::geometry::area(mp_i)>0) poly_list.push_back(mp_i);
|
---|
87 | if(boost::geometry::area(mp_d)>0) poly_list.push_back(mp_d);
|
---|
88 | if(boost::geometry::area(mp_e)>0) poly_list.push_back(mp_e);
|
---|
89 | }
|
---|
90 |
|
---|
91 | return 0;
|
---|
92 | }
|
---|