05_b_overlay_linestring_polygon_example.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // Linestring Polygon Overlay Example
  10. // NOTE: this example is obsolete. Boost.Geometry can now
  11. // overlay linestrings/polygons.
  12. // This sample will be removed in next version.
  13. #include <fstream>
  14. #include <iostream>
  15. #include <string>
  16. #include <vector>
  17. #include <boost/foreach.hpp>
  18. #include <boost/geometry/geometry.hpp>
  19. #include <boost/geometry/geometries/linestring.hpp>
  20. #include <boost/geometry/geometries/point_xy.hpp>
  21. #include <boost/geometry/geometries/polygon.hpp>
  22. #include <boost/geometry/geometries/adapted/c_array.hpp>
  23. #if defined(HAVE_SVG)
  24. # include <boost/geometry/io/svg/svg_mapper.hpp>
  25. #endif
  26. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian);
  27. int main(void)
  28. {
  29. namespace bg = boost::geometry;
  30. typedef bg::model::d2::point_xy<double> point_2d;
  31. bg::model::linestring<point_2d> ls;
  32. {
  33. const double c[][2] = { {0, 1}, {2, 5}, {5, 3} };
  34. bg::assign_points(ls, c);
  35. }
  36. bg::model::polygon<point_2d> p;
  37. {
  38. const double c[][2] = { {3, 0}, {0, 3}, {4, 5}, {3, 0} };
  39. bg::assign_points(p, c);
  40. }
  41. bg::correct(p);
  42. #if defined(HAVE_SVG)
  43. // Create SVG-mapper
  44. std::ofstream stream("05_b_overlay_linestring_polygon_example.svg");
  45. bg::svg_mapper<point_2d> svg(stream, 500, 500);
  46. // Determine extend by adding geometries
  47. svg.add(p);
  48. svg.add(ls);
  49. // Map geometries
  50. svg.map(ls, "opacity:0.6;stroke:rgb(255,0,0);stroke-width:2;");
  51. svg.map(p, "opacity:0.6;fill:rgb(0,0,255);");
  52. #endif
  53. // Calculate intersection points (turn points)
  54. typedef bg::segment_ratio_type<point_2d, bg::detail::no_rescale_policy>::type segment_ratio;
  55. typedef bg::detail::overlay::turn_info<point_2d, segment_ratio> turn_info;
  56. std::vector<turn_info> turns;
  57. bg::detail::get_turns::no_interrupt_policy policy;
  58. bg::detail::no_rescale_policy rescale_policy;
  59. bg::get_turns<false, false, bg::detail::overlay::assign_null_policy>(ls, p, rescale_policy, turns, policy);
  60. std::cout << "Intersection of linestring/polygon" << std::endl;
  61. BOOST_FOREACH(turn_info const& turn, turns)
  62. {
  63. std::string action = "intersecting";
  64. if (turn.operations[0].operation
  65. == bg::detail::overlay::operation_intersection)
  66. {
  67. action = "entering";
  68. }
  69. else if (turn.operations[0].operation
  70. == bg::detail::overlay::operation_union)
  71. {
  72. action = "leaving";
  73. }
  74. std::cout << action << " polygon at " << bg::dsv(turn.point) << std::endl;
  75. #if defined(HAVE_SVG)
  76. svg.map(turn.point, "fill:rgb(255,128,0);stroke:rgb(0,0,100);stroke-width:1");
  77. svg.text(turn.point, action, "fill:rgb(0,0,0);font-family:Arial;font-size:10px");
  78. #endif
  79. }
  80. return 0;
  81. }