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