• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Quickbook Examples, for main page
3 
4 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
6 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
7 
8 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
9 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
10 
11 // Use, modification and distribution is subject to the Boost Software License,
12 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
14 //
15 
16 
17 #if defined(_MSC_VER)
18 // We deliberately mix float/double's here so turn off warning
19 //#pragma warning( disable : 4244 )
20 #endif // defined(_MSC_VER)
21 
22 #include <iostream>
23 
24 //[quickstart_include
25 
26 #include <boost/geometry.hpp>
27 #include <boost/geometry/geometries/point_xy.hpp>
28 #include <boost/geometry/geometries/polygon.hpp>
29 
30 using namespace boost::geometry;
31 //]
32 
33 #include <boost/geometry/geometries/register/point.hpp>
34 
35 
36 //[quickstart_register_c_array
37 #include <boost/geometry/geometries/adapted/c_array.hpp>
38 
39 BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
40 //]
41 
42 //[quickstart_register_boost_tuple
43 #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
44 
45 BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
46 //]
47 
48 // Small QRect simulations following http://doc.trolltech.com/4.4/qrect.html
49 // Todo: once work the traits out further, would be nice if there is a real example of this.
50 // However for the example it makes no difference, it will work any way.
51 struct QPoint
52 {
53     int x, y;
54     // In Qt these are methods but for example below it makes no difference
55 };
56 
57 struct QRect
58 {
59     int x, y, width, height;
QRectQRect60     QRect(int _x, int _y, int w, int h)
61         : x(_x), y(_y), width(w), height(h)
62     {}
63     // In Qt these are methods but that will work as well, requires changing traits below
64 };
65 
66 
67 // Would be get/set with x(),y(),setX(),setY()
68 BOOST_GEOMETRY_REGISTER_POINT_2D(QPoint, int, cs::cartesian, x, y)
69 
70 
71 // Register the QT rectangle. The macro(s) does not offer (yet) enough flexibility to do this in one line,
72 // but the traits classes do their job perfectly.
73 namespace boost { namespace geometry { namespace traits
74 {
75 
76 template <> struct tag<QRect> { typedef box_tag type; };
77 template <> struct point_type<QRect> { typedef QPoint type; };
78 
79 template <size_t C, size_t D>
80 struct indexed_access<QRect, C, D>
81 {
getboost::geometry::traits::indexed_access82     static inline int get(const QRect& qr)
83     {
84         // Would be: x(), y(), width(), height()
85         return C == min_corner && D == 0 ? qr.x
86                 : C == min_corner && D == 1 ? qr.y
87                 : C == max_corner && D == 0 ? qr.x + qr.width
88                 : C == max_corner && D == 1 ? qr.y + qr.height
89                 : 0;
90     }
91 
setboost::geometry::traits::indexed_access92     static inline void set(QRect& qr, const int& value)
93     {
94         // Would be: setX, setY, setWidth, setHeight
95         if (C == min_corner && D == 0) qr.x = value;
96         else if (C == min_corner && D == 1) qr.y = value;
97         else if (C == max_corner && D == 0) qr.width = value - qr.x;
98         else if (C == max_corner && D == 1) qr.height = value - qr.y;
99     }
100 };
101 
102 
103 }}}
104 
105 
main(void)106 int main(void)
107 {
108     //[quickstart_distance
109     model::d2::point_xy<int> p1(1, 1), p2(2, 2);
110     std::cout << "Distance p1-p2 is: " << distance(p1, p2) << std::endl;
111     //]
112 
113     //[quickstart_distance_c_array
114     int a[2] = {1,1};
115     int b[2] = {2,3};
116     double d = distance(a, b);
117     std::cout << "Distance a-b is: " << d << std::endl;
118     //]
119 
120     //[quickstart_point_in_polygon
121     double points[][2] = {{2.0, 1.3}, {4.1, 3.0}, {5.3, 2.6}, {2.9, 0.7}, {2.0, 1.3}};
122     model::polygon<model::d2::point_xy<double> > poly;
123     append(poly, points);
124     boost::tuple<double, double> p = boost::make_tuple(3.7, 2.0);
125     std::cout << "Point p is in polygon? " << std::boolalpha << within(p, poly) << std::endl;
126     //]
127 
128     //[quickstart_area
129     std::cout << "Area: " << area(poly) << std::endl;
130     //]
131 
132     //[quickstart_distance_mixed
133     double d2 = distance(a, p);
134     std::cout << "Distance a-p is: " << d2 << std::endl;
135     //]
136 
137     //[quick_start_spherical
138     typedef boost::geometry::model::point
139         <
140             double, 2, boost::geometry::cs::spherical_equatorial<boost::geometry::degree>
141         > spherical_point;
142 
143     spherical_point amsterdam(4.90, 52.37);
144     spherical_point paris(2.35, 48.86);
145 
146     double const earth_radius = 3959; // miles
147     std::cout << "Distance in miles: " << distance(amsterdam, paris) * earth_radius << std::endl;
148     //]
149 
150     /***
151     Now extension
152     point_ll_deg  amsterdam, paris;
153     parse(amsterdam, "52 22 23 N", "4 53 32 E");
154     parse(paris, "48 52 0 N", "2 19 59 E");
155     std::cout << "Distance A'dam-Paris: " << distance(amsterdam, paris) / 1000.0 << " kilometers " << std::endl;
156     ***/
157 
158     //[quickstart_qt
159     QRect r1(100, 200, 15, 15);
160     QRect r2(110, 210, 20, 20);
161     if (overlaps(r1, r2))
162     {
163         assign_values(r2, 200, 300, 220, 320);
164     }
165     //]
166 
167     return 0;
168 }
169 
170