1 | /*
|
---|
2 | Thai Dao
|
---|
3 | AWR Corp
|
---|
4 | thai@awrcorp.com
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include <boost/polygon/polygon.hpp>
|
---|
8 | #include <iostream>
|
---|
9 | namespace gtl = boost::polygon;
|
---|
10 | using namespace boost::polygon::operators;
|
---|
11 |
|
---|
12 | // points_array_A_1 (-92810838,3618230) (-94606872,1822196) (-94999302,2214626) (-93203268,4010660) (-92810838,3618230)
|
---|
13 | int points_array_A_1[] = {-92810838,3618230,-94606872,1822196,-94999302,2214626,-93203268,4010660,-92810838,3618230};
|
---|
14 | int points_array_A_1_size =5;
|
---|
15 |
|
---|
16 | // points_array_B_1 (-95269304,222758) (-95260668,419862) (-95234760,615696) (-95192088,808228) (-95132906,996442) (-95057214,1178814) (-94966028,1354074) (-94860110,1520444) (-94739968,1676908) (-94606618,1822450) (-94999048,2214880) (-95165164,2033778) (-95314770,1838706) (-95446850,1631442) (-95560388,1413510) (-95654368,1186434) (-95728282,951992) (-95781368,711962) (-95813626,468376) (-95824294,222758) (-95269304,222758)
|
---|
17 | int points_array_B_1[] = {-95269304,222758,-95260668,419862,-95234760,615696,-95192088,808228,-95132906,996442,-95057214,1178814,-94966028,1354074,-94860110,1520444,-94739968,1676908,-94606618,1822450,-94999048,2214880,-95165164,2033778,-95314770,1838706,-95446850,1631442,-95560388,1413510,-95654368,1186434,-95728282,951992,-95781368,711962,-95813626,468376,-95824294,222758,-95269304,222758};
|
---|
18 | int points_array_B_1_size =21;
|
---|
19 |
|
---|
20 |
|
---|
21 | namespace{
|
---|
22 | class Output //for printing debug info
|
---|
23 | {
|
---|
24 | public:
|
---|
25 | template<class T>
|
---|
26 | static void Print(const gtl::polygon_data<T>& polyData)
|
---|
27 | {
|
---|
28 | gtl::polygon_data<T>::iterator_type pit = polyData.begin();
|
---|
29 | gtl::polygon_data<T>::iterator_type pit_end = polyData.end();
|
---|
30 | while(pit!=pit_end)
|
---|
31 | {
|
---|
32 | gtl::point_data<T> p = (*pit++);
|
---|
33 | std::cout<<"("<<p.x()<<","<<p.y()<<")";
|
---|
34 | std::cout<<" ";
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | template<class T>
|
---|
39 | static void Print(std::vector<gtl::polygon_data<T> >& polygonVec)
|
---|
40 | {
|
---|
41 | int size = polygonVec.size();
|
---|
42 | for(int i=0;i<size;i++)
|
---|
43 | {
|
---|
44 | gtl::polygon_data<T>& poly = polygonVec[i];
|
---|
45 | std::cout<<"Polygon "<<i+1<<": ";
|
---|
46 | Output::Print(poly);
|
---|
47 | std::cout<<std::endl;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | };
|
---|
51 | }
|
---|
52 |
|
---|
53 | static void AddPolygonData(std::vector<gtl::polygon_data<int> >& group, int* points_array, int size)
|
---|
54 | {
|
---|
55 | //convert c array to boost polygon data
|
---|
56 | if(size>0)
|
---|
57 | {
|
---|
58 | gtl::polygon_data<int> poly_data;
|
---|
59 | std::vector<gtl::point_data<int> > poly_points(size);
|
---|
60 | int pi=0;
|
---|
61 | for(int i=0;i<size;i++)
|
---|
62 | {
|
---|
63 | int i1 = i*2;
|
---|
64 | int i2 = i1+1;
|
---|
65 | poly_points[i]=gtl::point_data<int>(points_array[i1],points_array[i2]);
|
---|
66 | }
|
---|
67 | poly_data.set(poly_points.begin(),poly_points.end());
|
---|
68 | group.push_back(poly_data);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | void testBooleanOps()
|
---|
73 | {
|
---|
74 | //lets declare ourselves a polygon set
|
---|
75 | using namespace gtl; //because of operators
|
---|
76 | typedef std::vector<polygon_data<int> > PolygonVec;
|
---|
77 | PolygonVec group_A;
|
---|
78 | PolygonVec group_B;
|
---|
79 | PolygonVec group_U;
|
---|
80 |
|
---|
81 | //load group A;
|
---|
82 | AddPolygonData(group_A,points_array_A_1,points_array_A_1_size);
|
---|
83 |
|
---|
84 | //load group B;
|
---|
85 | AddPolygonData(group_B,points_array_B_1,points_array_B_1_size);
|
---|
86 |
|
---|
87 |
|
---|
88 | std::cout<<"union\n";
|
---|
89 | //the result of the union is tore in group U;
|
---|
90 | assign(group_U, group_A + group_B);
|
---|
91 | Output::Print(group_U);
|
---|
92 | }
|
---|
93 |
|
---|
94 | int main() {
|
---|
95 |
|
---|
96 | testBooleanOps();
|
---|
97 | return 0;
|
---|
98 | }
|
---|
99 |
|
---|
100 | //Now you know how to use the polygon set concept with library polygons
|
---|