c05_custom_point_pointer_example.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/algorithms/distance.hpp>
  13. #include <boost/geometry/algorithms/length.hpp>
  14. #include <boost/geometry/algorithms/make.hpp>
  15. #include <boost/geometry/algorithms/intersection.hpp>
  16. #include <boost/geometry/geometries/geometries.hpp>
  17. #include <boost/geometry/geometries/point_xy.hpp>
  18. #include <boost/geometry/geometries/register/linestring.hpp>
  19. #include <boost/geometry/strategies/strategies.hpp>
  20. BOOST_GEOMETRY_REGISTER_LINESTRING_TEMPLATED(std::vector)
  21. BOOST_GEOMETRY_REGISTER_LINESTRING_TEMPLATED(std::deque)
  22. // Sample point, having x/y
  23. struct my_point
  24. {
  25. double x,y;
  26. };
  27. namespace boost { namespace geometry { namespace traits {
  28. template<> struct tag<my_point>
  29. { typedef point_tag type; };
  30. template<> struct coordinate_type<my_point>
  31. { typedef double type; };
  32. template<> struct coordinate_system<my_point>
  33. { typedef cs::cartesian type; };
  34. template<> struct dimension<my_point> : boost::mpl::int_<2> {};
  35. template<>
  36. struct access<my_point, 0>
  37. {
  38. static double get(my_point const& p)
  39. {
  40. return p.x;
  41. }
  42. static void set(my_point& p, double const& value)
  43. {
  44. p.x = value;
  45. }
  46. };
  47. template<>
  48. struct access<my_point, 1>
  49. {
  50. static double get(my_point const& p)
  51. {
  52. return p.y;
  53. }
  54. static void set(my_point& p, double const& value)
  55. {
  56. p.y = value;
  57. }
  58. };
  59. }}} // namespace boost::geometry::traits
  60. int main()
  61. {
  62. typedef std::vector<my_point*> ln;
  63. ln myline;
  64. for (float i = 0.0; i < 10.0; i++)
  65. {
  66. my_point* p = new my_point;
  67. p->x = i;
  68. p->y = i + 1;
  69. myline.push_back(p);
  70. }
  71. std::cout << boost::geometry::length(myline) << std::endl;
  72. typedef boost::geometry::model::d2::point_xy<double> point_2d;
  73. typedef boost::geometry::model::box<point_2d> box_2d;
  74. box_2d cb(point_2d(1.5, 1.5), point_2d(4.5, 4.5));
  75. // This will NOT work because would need dynamicly allocating memory for point* in algorithms:
  76. // std::vector<ln> clipped;
  77. //boost::geometry::intersection(cb, myline, clipped);
  78. // This works because outputs to a normal struct point, no point*
  79. typedef boost::geometry::model::linestring<point_2d> linestring_2d;
  80. std::vector<linestring_2d> clipped;
  81. boost::geometry::strategy::intersection::liang_barsky<box_2d, point_2d> strategy;
  82. boost::geometry::detail::intersection::clip_range_with_box<linestring_2d>(cb,
  83. myline, std::back_inserter(clipped), strategy);
  84. std::cout << boost::geometry::length(clipped.front()) << std::endl;
  85. // free
  86. BOOST_FOREACH(my_point* p, myline)
  87. {
  88. delete p;
  89. }
  90. return 0;
  91. }