]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/doc/src/examples/quick_start.cpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Quickbook Examples, for main page
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.
8 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
9 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
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)
18 // We deliberately mix float/double's here so turn off warning
19 //#pragma warning( disable : 4244 )
20 #endif // defined(_MSC_VER)
24 #include <boost/geometry.hpp>
25 #include <boost/geometry/geometries/point_xy.hpp>
26 #include <boost/geometry/geometries/polygon.hpp>
28 using namespace boost::geometry
;
31 #include <boost/geometry/geometries/register/point.hpp>
34 //[quickstart_register_c_array
35 #include <boost/geometry/geometries/adapted/c_array.hpp>
37 BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian
)
40 //[quickstart_register_boost_tuple
41 #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
43 BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian
)
46 // Small QRect simulations following http://doc.trolltech.com/4.4/qrect.html
47 // Todo: once work the traits out further, would be nice if there is a real example of this.
48 // However for the example it makes no difference, it will work any way.
52 // In Qt these are methods but for example below it makes no difference
57 int x
, y
, width
, height
;
58 QRect(int _x
, int _y
, int w
, int h
)
59 : x(_x
), y(_y
), width(w
), height(h
)
61 // In Qt these are methods but that will work as well, requires changing traits below
65 // Would be get/set with x(),y(),setX(),setY()
66 BOOST_GEOMETRY_REGISTER_POINT_2D(QPoint
, int, cs::cartesian
, x
, y
)
69 // Register the QT rectangle. The macro(s) does not offer (yet) enough flexibility to do this in one line,
70 // but the traits classes do their job perfectly.
71 namespace boost
{ namespace geometry
{ namespace traits
74 template <> struct tag
<QRect
> { typedef box_tag type
; };
75 template <> struct point_type
<QRect
> { typedef QPoint type
; };
77 template <size_t C
, size_t D
>
78 struct indexed_access
<QRect
, C
, D
>
80 static inline int get(const QRect
& qr
)
82 // Would be: x(), y(), width(), height()
83 return C
== min_corner
&& D
== 0 ? qr
.x
84 : C
== min_corner
&& D
== 1 ? qr
.y
85 : C
== max_corner
&& D
== 0 ? qr
.x
+ qr
.width
86 : C
== max_corner
&& D
== 1 ? qr
.y
+ qr
.height
90 static inline void set(QRect
& qr
, const int& value
)
92 // Would be: setX, setY, setWidth, setHeight
93 if (C
== min_corner
&& D
== 0) qr
.x
= value
;
94 else if (C
== min_corner
&& D
== 1) qr
.y
= value
;
95 else if (C
== max_corner
&& D
== 0) qr
.width
= value
- qr
.x
;
96 else if (C
== max_corner
&& D
== 1) qr
.height
= value
- qr
.y
;
106 //[quickstart_distance
107 model::d2::point_xy
<int> p1(1, 1), p2(2, 2);
108 std::cout
<< "Distance p1-p2 is: " << distance(p1
, p2
) << std::endl
;
111 //[quickstart_distance_c_array
114 double d
= distance(a
, b
);
115 std::cout
<< "Distance a-b is: " << d
<< std::endl
;
118 //[quickstart_point_in_polygon
119 double points
[][2] = {{2.0, 1.3}, {4.1, 3.0}, {5.3, 2.6}, {2.9, 0.7}, {2.0, 1.3}};
120 model::polygon
<model::d2::point_xy
<double> > poly
;
121 append(poly
, points
);
122 boost::tuple
<double, double> p
= boost::make_tuple(3.7, 2.0);
123 std::cout
<< "Point p is in polygon? " << std::boolalpha
<< within(p
, poly
) << std::endl
;
127 std::cout
<< "Area: " << area(poly
) << std::endl
;
130 //[quickstart_distance_mixed
131 double d2
= distance(a
, p
);
132 std::cout
<< "Distance a-p is: " << d2
<< std::endl
;
135 //[quick_start_spherical
136 typedef boost::geometry::model::point
138 double, 2, boost::geometry::cs::spherical_equatorial
<boost::geometry::degree
>
141 spherical_point
amsterdam(4.90, 52.37);
142 spherical_point
paris(2.35, 48.86);
144 double const earth_radius
= 3959; // miles
145 std::cout
<< "Distance in miles: " << distance(amsterdam
, paris
) * earth_radius
<< std::endl
;
150 point_ll_deg amsterdam, paris;
151 parse(amsterdam, "52 22 23 N", "4 53 32 E");
152 parse(paris, "48 52 0 N", "2 19 59 E");
153 std::cout << "Distance A'dam-Paris: " << distance(amsterdam, paris) / 1000.0 << " kilometers " << std::endl;
157 QRect
r1(100, 200, 15, 15);
158 QRect
r2(110, 210, 20, 20);
159 if (overlaps(r1
, r2
))
161 assign_values(r2
, 200, 300, 220, 320);