04_boost_example.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // Example combining Boost.Geometry with Boost.Assign and Boost.Range and Boost.Tuple
  10. #include <iostream>
  11. #include <boost/geometry/geometry.hpp>
  12. #include <boost/geometry/geometries/point_xy.hpp>
  13. #include <boost/geometry/geometries/linestring.hpp>
  14. #include <boost/geometry/geometries/polygon.hpp>
  15. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  16. #include <boost/assign.hpp>
  17. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian);
  18. int main(void)
  19. {
  20. using namespace boost::geometry;
  21. using namespace boost::assign;
  22. {
  23. typedef model::d2::point_xy<double> point;
  24. typedef model::polygon<point> polygon;
  25. typedef model::linestring<point> linestring;
  26. // Boost.Assign automatically works for linestring, rings, multi-geometries
  27. // It works because these are std:: containers
  28. // Using Boost.Assign operator +=
  29. linestring ls1;
  30. ls1 += point(1,2);
  31. ls1 += point(3,4), point(5,6), point(7,8);
  32. std::cout << dsv(ls1) << std::endl;
  33. // Using Boost.Assign operator()
  34. linestring ls2;
  35. push_back(ls2)(point(1, 2))(point(3, 4));
  36. std::cout << dsv(ls2) << std::endl;
  37. // Using Boost.Assign list_of
  38. linestring ls3 = list_of(point(1,2))(point(3,4));
  39. std::cout << dsv(ls3) << std::endl;
  40. // Using Boost.Assign + Boost.Range
  41. linestring ls4;
  42. push_back(ls4)(point(0, 0)).range(ls2).range(ls3);
  43. std::cout << dsv(ls4) << std::endl;
  44. // For a ring, it is similar to a linestring.
  45. // For a multi-point or multi-linestring, it is also similar
  46. // For a polygon, take the exterior ring or one of the interiors
  47. polygon p;
  48. push_back(exterior_ring(p))
  49. (point(0, 0))
  50. (point(0, 2))
  51. (point(2, 2))
  52. (point(2, 0))
  53. (point(0, 0))
  54. ;
  55. std::cout << dsv(p) << std::endl;
  56. }
  57. {
  58. // It is convenient to combine Boost.Assign on a geometry (e.g. polygon) with tuples.
  59. typedef model::polygon<boost::tuple<double,double> > polygon;
  60. polygon p;
  61. exterior_ring(p) = tuple_list_of(0, 0)(0, 5)(5, 5)(5, 0)(0, 0);
  62. std::cout << dsv(p) << std::endl;
  63. // And let it work on the interior_rings as well
  64. push_back(interior_rings(p))
  65. (tuple_list_of(1, 1)(2, 1)(2, 2)(1, 2)(1, 1))
  66. (tuple_list_of(3, 3)(4, 3)(4, 4)(3, 4)(3, 3))
  67. ;
  68. std::cout << "Area of " << dsv(p) << " is " << area(p) << std::endl;
  69. }
  70. return 0;
  71. }