doxygen_2.cpp 4.5 KB

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