1 | #include <iostream>
|
---|
2 | #include <vector>
|
---|
3 |
|
---|
4 | #define BOOST_NO_CXX11_NOEXCEPT
|
---|
5 |
|
---|
6 | #include <boost/multiprecision/gmp.hpp>
|
---|
7 | //#include <boost/multiprecision/cpp_int.hpp>
|
---|
8 | //#include <boost/multiprecision/number.hpp>
|
---|
9 | //#include <boost/numeric/conversion/converter.hpp>
|
---|
10 |
|
---|
11 | //typedef boost::multiprecision::mpq_rational GeometryCoord;
|
---|
12 | //typedef boost::multiprecision::cpp_rational GeometryCoord;
|
---|
13 | //typedef boost::numeric::conversion_traits<double , GeometryCoord> MyFloat_to_double_Traits;
|
---|
14 |
|
---|
15 | /*namespace boost {
|
---|
16 | namespace numeric {
|
---|
17 | template<>
|
---|
18 | struct raw_converter<MyFloat_to_double_Traits>
|
---|
19 | {
|
---|
20 | static double low_level_convert ( GeometryCoord const& s ) { return( 0.0 ); } // Must be real conversion
|
---|
21 | };
|
---|
22 | }
|
---|
23 | }*/
|
---|
24 |
|
---|
25 | #include <boost/geometry.hpp>
|
---|
26 | #include <boost/geometry/geometries/point_xy.hpp>
|
---|
27 | #include <boost/geometry/geometries/polygon.hpp>
|
---|
28 | #include <boost/geometry/io/wkt/wkt.hpp>
|
---|
29 |
|
---|
30 | #include <boost/foreach.hpp>
|
---|
31 |
|
---|
32 | typedef boost::multiprecision::number<boost::multiprecision::gmp_rational, boost::multiprecision::et_off> GeometryCoord;
|
---|
33 | //typedef boost::multiprecision::number<boost::multiprecision::gmp_rational> GeometryCoord;
|
---|
34 | //typedef boost::multiprecision::mpq_rational GeometryCoord;
|
---|
35 | //typedef boost::multiprecision::number<boost::multiprecision::cpp_rational, boost::multiprecision::et_off> GeometryCoord;
|
---|
36 |
|
---|
37 | typedef boost::geometry::model::d2::point_xy<GeometryCoord> GeometryPoint;
|
---|
38 | typedef boost::geometry::model::multi_point<GeometryPoint> GeometryMultiPoint;
|
---|
39 | typedef boost::geometry::model::segment<GeometryPoint> GeometrySegment;
|
---|
40 | typedef boost::geometry::model::box<GeometryPoint> GeometryBox;
|
---|
41 | typedef boost::geometry::model::polygon<GeometryPoint> GeometryPolygon;
|
---|
42 |
|
---|
43 | int main()
|
---|
44 | {
|
---|
45 | /*std::vector<GeometryPoint> points;
|
---|
46 |
|
---|
47 | GeometrySegment Segment1(GeometryPoint(0, 3), GeometryPoint(3, 2));
|
---|
48 | GeometrySegment Segment2(GeometryPoint(2, 0), GeometryPoint(0, 5));
|
---|
49 |
|
---|
50 | boost::geometry::intersection(Segment1, Segment2, points);*/
|
---|
51 | /*
|
---|
52 | for (std::vector<point>::const_iterator i = points.begin(); i != points.end(); ++i) {
|
---|
53 | point p = *i;
|
---|
54 |
|
---|
55 | std::cout << "x = " << p.x() << " y = " << p.y() << std::endl;
|
---|
56 | }
|
---|
57 |
|
---|
58 | std::cout << std::endl;*/
|
---|
59 |
|
---|
60 | GeometryCoord c(3, 4);
|
---|
61 |
|
---|
62 | std::cout << " c = " << c << std::endl;
|
---|
63 |
|
---|
64 | GeometryPolygon green, blue;
|
---|
65 |
|
---|
66 | /*boost::geometry::read_wkt(
|
---|
67 | "POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))", green);
|
---|
68 |
|
---|
69 | boost::geometry::read_wkt(
|
---|
70 | "POLYGON((10 10, 10 20, 20 20, 20 10, 10 10))", blue);*/
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 | /*boost::geometry::read_wkt(
|
---|
76 | "POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))", green);
|
---|
77 |
|
---|
78 | boost::geometry::read_wkt(
|
---|
79 | "POLYGON((0 0, 0 20, 20 20, 20 0, 0 0))", blue);*/
|
---|
80 |
|
---|
81 | /*boost::geometry::read_wkt(
|
---|
82 | "POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))", green);
|
---|
83 |
|
---|
84 | boost::geometry::read_wkt(
|
---|
85 | "POLYGON((1 1, 1 2, 2 2, 2 1, 1 1))", blue);*/
|
---|
86 |
|
---|
87 | green.outer().push_back(GeometryPoint(GeometryCoord(0), GeometryCoord(0)));
|
---|
88 | green.outer().push_back(GeometryPoint(GeometryCoord(0), GeometryCoord(101,10)));
|
---|
89 | green.outer().push_back(GeometryPoint(GeometryCoord(101, 10), GeometryCoord(101, 10)));
|
---|
90 | green.outer().push_back(GeometryPoint(GeometryCoord(101,10), GeometryCoord(0)));
|
---|
91 | green.outer().push_back(GeometryPoint(GeometryCoord(0), GeometryCoord(0)));
|
---|
92 |
|
---|
93 | {
|
---|
94 | const boost::geometry::model::ring<GeometryPoint> &ring = green.outer();
|
---|
95 |
|
---|
96 | for(boost::geometry::model::ring<GeometryPoint>::const_iterator i = ring.begin(); i != ring.end(); ++i ) {
|
---|
97 | GeometryPoint Point = *i;
|
---|
98 |
|
---|
99 | std::cout << "x = " << Point.x() << " y = " << Point.y() << std::endl;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | std::cout << std::endl;
|
---|
104 |
|
---|
105 | blue.outer().push_back(GeometryPoint(GeometryCoord(10), GeometryCoord(10)));
|
---|
106 | blue.outer().push_back(GeometryPoint(GeometryCoord(10), GeometryCoord(20)));
|
---|
107 | blue.outer().push_back(GeometryPoint(GeometryCoord(20), GeometryCoord(20)));
|
---|
108 | blue.outer().push_back(GeometryPoint(GeometryCoord(20), GeometryCoord(10)));
|
---|
109 | blue.outer().push_back(GeometryPoint(GeometryCoord(10), GeometryCoord(10)));
|
---|
110 |
|
---|
111 | {
|
---|
112 | const boost::geometry::model::ring<GeometryPoint> &ring = blue.outer();
|
---|
113 |
|
---|
114 | for(boost::geometry::model::ring<GeometryPoint>::const_iterator i = ring.begin(); i != ring.end(); ++i ) {
|
---|
115 | GeometryPoint Point = *i;
|
---|
116 |
|
---|
117 | std::cout << "x = " << Point.x() << " y = " << Point.y() << std::endl;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | std::cout << std::endl;
|
---|
122 |
|
---|
123 | std::vector<GeometryPolygon> output;
|
---|
124 | //boost::geometry::union_(green, blue, output);
|
---|
125 |
|
---|
126 | boost::geometry::intersection(green, blue, output);
|
---|
127 |
|
---|
128 | //boost::geometry::correct(green);
|
---|
129 |
|
---|
130 | std::cout << "size = " << output.size() << std::endl;
|
---|
131 |
|
---|
132 | //std::cout << "Intersects = " << boost::geometry::intersects(green, blue) << std::endl;
|
---|
133 |
|
---|
134 | BOOST_FOREACH(GeometryPolygon const& p, output) {
|
---|
135 | //std::cout << boost::geometry::wkt<polygon>(p) << std::endl;
|
---|
136 |
|
---|
137 | const boost::geometry::model::ring<GeometryPoint> &ring = p.outer();
|
---|
138 |
|
---|
139 | for(boost::geometry::model::ring<GeometryPoint>::const_iterator i = ring.begin(); i != ring.end(); ++i ) {
|
---|
140 | GeometryPoint Point = *i;
|
---|
141 |
|
---|
142 | std::cout << "x = " << Point.x() << " y = " << Point.y() << std::endl;
|
---|
143 | }
|
---|
144 |
|
---|
145 | //std::cout << "Point inside: " << boost::geometry::within( point(0, 0), p ) << std::endl;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /*GeometryPoint p1(GeometryCoord(-1), GeometryCoord(-1));
|
---|
149 | GeometryPoint p2(GeometryCoord(1), GeometryCoord(1));
|
---|
150 |
|
---|
151 | boost::geometry::distance(p1, p2);*/
|
---|
152 |
|
---|
153 | /*boost::geometry::strategy::buffer::distance_symmetric<GeometryCoord> distance_strategy(10);
|
---|
154 | boost::geometry::strategy::buffer::join_round join_strategy;
|
---|
155 | boost::geometry::strategy::buffer::end_round end_strategy;
|
---|
156 | boost::geometry::strategy::buffer::point_circle point_strategy;
|
---|
157 | boost::geometry::strategy::buffer::side_straight side_strategy;
|
---|
158 |
|
---|
159 | boost::geometry::model::multi_polygon<GeometryPolygon> result;
|
---|
160 |
|
---|
161 | boost::geometry::buffer(green, result, distance_strategy, side_strategy, join_strategy, end_strategy, point_strategy);
|
---|
162 |
|
---|
163 | if (result.size() != 1) {
|
---|
164 | std::cout << "result.size() != 1" << std::endl;
|
---|
165 | }*/
|
---|
166 |
|
---|
167 | GeometryMultiPoint MultiPoint;
|
---|
168 |
|
---|
169 | const boost::geometry::model::ring<GeometryPoint> &ring = blue.outer();
|
---|
170 |
|
---|
171 | for(boost::geometry::model::ring<GeometryPoint>::const_iterator i = ring.begin(); i != ring.end(); ++i ) {
|
---|
172 | GeometryPoint Point = *i;
|
---|
173 |
|
---|
174 | boost::geometry::append(MultiPoint, Point);
|
---|
175 | }
|
---|
176 |
|
---|
177 | GeometryPolygon Hull;
|
---|
178 |
|
---|
179 | boost::geometry::convex_hull(MultiPoint, Hull);
|
---|
180 |
|
---|
181 | boost::geometry::index::rtree< std::pair< GeometryBox, const GeometryPolygon * >, boost::geometry::index::quadratic<16, 4> > rtree;
|
---|
182 |
|
---|
183 | GeometryBox Box( GeometryPoint( 10, 10 ), GeometryPoint( 11, 11 ) );
|
---|
184 | boost::geometry::envelope( green, Box );
|
---|
185 | rtree.insert(std::make_pair(Box, &green));
|
---|
186 |
|
---|
187 | GeometryPoint center;
|
---|
188 |
|
---|
189 | boost::geometry::centroid( green, center );
|
---|
190 |
|
---|
191 | std::vector< std::pair< GeometryBox, const GeometryPolygon * > > result;
|
---|
192 |
|
---|
193 | GeometryBox searchBox( GeometryPoint( GeometryCoord( center.x() - 1 ), GeometryCoord( center.y() - 1 ) ),
|
---|
194 | GeometryPoint( GeometryCoord( center.x() + 1 ), GeometryCoord( center.y() + 1 ) ) );
|
---|
195 |
|
---|
196 | rtree.query( boost::geometry::index::intersects( searchBox ), std::back_inserter( result ) );
|
---|
197 |
|
---|
198 | std::cout << "Found " << result.size() << " polygone(s)" << std::endl;
|
---|
199 |
|
---|
200 | return 0;
|
---|
201 | }
|
---|