c07_custom_ring_pointer_example.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // Custom pointer-to-point example
  10. #include <iostream>
  11. #include <boost/foreach.hpp>
  12. #include <boost/geometry.hpp>
  13. #include <boost/geometry/geometries/geometries.hpp>
  14. #include <boost/geometry/geometries/point_xy.hpp>
  15. #include <boost/geometry/geometries/register/ring.hpp>
  16. BOOST_GEOMETRY_REGISTER_RING_TEMPLATED(std::vector)
  17. // Sample point, having x/y
  18. struct my_point
  19. {
  20. my_point(double a = 0, double b = 0)
  21. : x(a), y(b)
  22. {}
  23. double x,y;
  24. };
  25. namespace boost { namespace geometry { namespace traits {
  26. template<> struct tag<my_point>
  27. { typedef point_tag type; };
  28. template<> struct coordinate_type<my_point>
  29. { typedef double type; };
  30. template<> struct coordinate_system<my_point>
  31. { typedef cs::cartesian type; };
  32. template<> struct dimension<my_point> : boost::mpl::int_<2> {};
  33. template<>
  34. struct access<my_point, 0>
  35. {
  36. static double get(my_point const& p)
  37. {
  38. return p.x;
  39. }
  40. static void set(my_point& p, double const& value)
  41. {
  42. p.x = value;
  43. }
  44. };
  45. template<>
  46. struct access<my_point, 1>
  47. {
  48. static double get(my_point const& p)
  49. {
  50. return p.y;
  51. }
  52. static void set(my_point& p, double const& value)
  53. {
  54. p.y = value;
  55. }
  56. };
  57. }}} // namespace boost::geometry::traits
  58. int main()
  59. {
  60. typedef std::vector<my_point*> ring_type;
  61. ring_type a, b;
  62. a.push_back(new my_point(0, 1));
  63. a.push_back(new my_point(2, 5));
  64. a.push_back(new my_point(5, 3));
  65. a.push_back(new my_point(0, 1));
  66. b.push_back(new my_point(3, 0));
  67. b.push_back(new my_point(0, 3));
  68. b.push_back(new my_point(4, 5));
  69. b.push_back(new my_point(3, 0));
  70. double aa = boost::geometry::area(a);
  71. double ab = boost::geometry::area(b);
  72. std::cout << "a: " << aa << std::endl;
  73. std::cout << "b: " << ab << std::endl;
  74. // This will NOT work because would need dynamicly allocating memory for point* in algorithms:
  75. //std::vector<ring_type> unioned;
  76. //boost::geometry::union<ring_type>(a, b, unioned);
  77. // BEGIN TODO
  78. // This compiles (and once worked) using pointers, but has to be fixed or deprecated
  79. // The problem is now the cart_intersect/side where a temporary point is generated
  80. //typedef boost::geometry::model::ring<boost::geometry::model::d2::point_xy<double> > ring_2d;
  81. //std::vector<ring_2d> unioned;
  82. //std::vector<ring_2d> intersected;
  83. //boost::geometry::intersection(a, b, intersected);
  84. //boost::geometry::union_(a, b, unioned);
  85. //double ai = 0, au = 0;
  86. //BOOST_FOREACH(ring_2d const& ring, intersected)
  87. //{
  88. // ai += boost::geometry::area(ring);
  89. //}
  90. //BOOST_FOREACH(ring_2d const& ring, unioned)
  91. //{
  92. // au += boost::geometry::area(ring);
  93. //}
  94. //std::cout << "a: " << aa << std::endl;
  95. //std::cout << "b: " << ab << std::endl;
  96. //std::cout << "a & b: " << ai << std::endl;
  97. //std::cout << "a | b: " << au << std::endl;
  98. //std::cout << "a + b - (a & b): " << (aa + ab - ai) << std::endl;
  99. // END TODO
  100. // free
  101. BOOST_FOREACH(my_point* p, a)
  102. {
  103. delete p;
  104. }
  105. BOOST_FOREACH(my_point* p, b)
  106. {
  107. delete p;
  108. }
  109. return 0;
  110. }