1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 //
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Use, modification and distribution is subject to the Boost Software License,
5 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 
8 // Qt Example
9 
10 // Qt is a well-known and often used platform independent windows library
11 
12 // To build and run this example:
13 // 1) download (from http://qt.nokia.com), configure and make QT
14 // 2) if necessary, adapt Qt clause in include path (note there is a Qt property sheet)
15 
16 #include <sstream>
17 
18 #include <QtGui>
19 
20 #include <boost/geometry/geometry.hpp>
21 #include <boost/geometry/geometries/register/point.hpp>
22 #include <boost/geometry/geometries/register/ring.hpp>
23 
24 
25 // Adapt a QPointF such that it can be handled by Boost.Geometry
BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(QPointF,double,cs::cartesian,x,y,setX,setY)26 BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(QPointF, double, cs::cartesian, x, y, setX, setY)
27 
28 // Adapt a QPolygonF as well.
29 // A QPolygonF has no holes (interiors) so it is similar to a Boost.Geometry ring
30 BOOST_GEOMETRY_REGISTER_RING(QPolygonF)
31 
32 
33 int main(int argc, char *argv[])
34 {
35     // This usage QApplication and QLabel is adapted from
36     // http://en.wikipedia.org/wiki/Qt_(toolkit)#Qt_hello_world
37     QApplication app(argc, argv);
38 
39     // Declare a Qt polygon. The Qt Polygon can be used
40     // in Boost.Geometry, just by its oneline registration above.
41     QPolygonF polygon;
42 
43     // Use Qt to add points to polygon
44     polygon
45         << QPointF(10, 20) << QPointF(20, 30)
46         << QPointF(30, 20) << QPointF(20, 10)
47         << QPointF(10, 20);
48 
49     // Use Boost.Geometry e.g. to calculate area
50     std::ostringstream out;
51     out << "Boost.Geometry area: " << boost::geometry::area(polygon) << std::endl;
52 
53     // Some functionality is defined in both Qt and Boost.Geometry
54     QPointF p(20,20);
55     out << "Qt contains: "
56         << (polygon.containsPoint(p, Qt::WindingFill) ? "yes" : "no")
57         << std::endl
58         << "Boost.Geometry within: "
59         << (boost::geometry::within(p, polygon) ? "yes" : "no")
60         << std::endl;
61     // Detail: if point is ON boundary, Qt says yes, Boost.Geometry says no.
62 
63     // Qt defines an iterator
64     // (which is required for of the Boost.Geometry ring-concept)
65     // such that Boost.Geometry can use the points of this polygon
66     QPolygonF::const_iterator it;
67     for (it = polygon.begin(); it != polygon.end(); ++it)
68     {
69         // Stream Delimiter-Separated, just to show something Boost.Geometry can do
70         out << boost::geometry::dsv(*it) << std::endl;
71     }
72 
73     // Stream the polygon as well
74     out << boost::geometry::dsv(polygon) << std::endl;
75 
76     // Just show what we did in a label
77     QLabel label(out.str().c_str());
78     label.show();
79     return app.exec();
80 }
81 
82