1 | #include <iostream>
|
---|
2 |
|
---|
3 | #include <boost/assert.hpp>
|
---|
4 |
|
---|
5 | #include <boost/geometry/geometry.hpp>
|
---|
6 | #include <boost/geometry/geometries/geometries.hpp>
|
---|
7 | #include <boost/geometry/multi/geometries/multi_geometries.hpp>
|
---|
8 |
|
---|
9 | namespace bg = boost::geometry;
|
---|
10 |
|
---|
11 | typedef bg::model::point<int, 2, bg::cs::cartesian> point_type;
|
---|
12 | typedef bg::model::polygon<point_type, false, false> polygon_type; //ccw, open
|
---|
13 | typedef bg::model::multi_polygon<polygon_type> multi_polygon_type; //ccw, open
|
---|
14 |
|
---|
15 | int main()
|
---|
16 | {
|
---|
17 | polygon_type p1, p2;
|
---|
18 | multi_polygon_type result;
|
---|
19 | bg::read_wkt("POLYGON((1374 1092,1734 1092,3174 2526,3174 2886,1374 2886))", p1);
|
---|
20 | bg::read_wkt("POLYGON((2817 2449,2817 2603,3134 2603,3134 2449,2817 2449))", p2);
|
---|
21 |
|
---|
22 | bool is_valid1 = bg::is_valid(p1);
|
---|
23 | bool is_valid2 = bg::is_valid(p2);
|
---|
24 |
|
---|
25 | if ( is_valid1 )
|
---|
26 | {
|
---|
27 | std::cout << "1st polygon is valid ..." << std::endl;
|
---|
28 | }
|
---|
29 | else
|
---|
30 | {
|
---|
31 | std::cout << "1st polygon is NOT valid ..." << std::endl;
|
---|
32 | bg::correct(p1);
|
---|
33 | BOOST_ASSERT( bg::is_valid(p1) );
|
---|
34 | }
|
---|
35 |
|
---|
36 | if ( is_valid2 )
|
---|
37 | {
|
---|
38 | std::cout << "2nd polygon is valid ..." << std::endl;
|
---|
39 | }
|
---|
40 | else
|
---|
41 | {
|
---|
42 | std::cout << "2nd polygon is NOT valid ..." << std::endl;
|
---|
43 | bg::correct(p2);
|
---|
44 | BOOST_ASSERT( bg::is_valid(p2) );
|
---|
45 | }
|
---|
46 |
|
---|
47 | bg::intersection(p1, p2, result);
|
---|
48 |
|
---|
49 | std::cout << bg::wkt(result) << std::endl;
|
---|
50 | std::cout << bg::dsv(result) << std::endl;
|
---|
51 | std::cout << "# of points: " << bg::num_points(result) << std::endl;
|
---|
52 |
|
---|
53 | return 0;
|
---|
54 | }
|
---|