/Src/Dependencies/Boost/libs/geometry/doc/src/examples/algorithms/for_each_segment_const.cpp

http://hadesmem.googlecode.com/ · C++ · 95 lines · 42 code · 23 blank · 30 comment · 2 complexity · 04f1361cc5a783228f172e537c722545 MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // QuickBook Example
  3. // Copyright (c) 2011 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //[for_each_segment_const
  8. //` Sample using for_each_segment, using a functor to get the minimum and maximum length of a segment in a linestring
  9. #include <iostream>
  10. #include <boost/geometry.hpp>
  11. #include <boost/geometry/geometries/linestring.hpp>
  12. #include <boost/geometry/geometries/point_xy.hpp>
  13. #include <boost/assign.hpp>
  14. template <typename Segment>
  15. struct gather_segment_statistics
  16. {
  17. // Remember that if coordinates are integer, the length might be floating point
  18. // So use "double" for integers. In other cases, use coordinate type
  19. typedef typename boost::geometry::select_most_precise
  20. <
  21. typename boost::geometry::coordinate_type<Segment>::type,
  22. double
  23. >::type type;
  24. type min_length, max_length;
  25. // Initialize min and max
  26. gather_segment_statistics()
  27. : min_length(1e38)
  28. , max_length(-1)
  29. {}
  30. // This operator is called for each segment
  31. inline void operator()(Segment const& s)
  32. {
  33. type length = boost::geometry::length(s);
  34. if (length < min_length) min_length = length;
  35. if (length > max_length) max_length = length;
  36. }
  37. };
  38. int main()
  39. {
  40. // Bring "+=" for a vector into scope
  41. using namespace boost::assign;
  42. // Define a type
  43. typedef boost::geometry::model::d2::point_xy<double> point;
  44. // Declare a linestring
  45. boost::geometry::model::linestring<point> polyline;
  46. // Use Boost.Assign to initialize a linestring
  47. polyline += point(0, 0), point(3, 3), point(5, 1), point(6, 2),
  48. point(8, 0), point(4, -4), point(1, -1), point(3, 2);
  49. // Declare the gathering class...
  50. gather_segment_statistics
  51. <
  52. boost::geometry::model::referring_segment<point>
  53. > functor;
  54. // ... and use it, the essention.
  55. // As also in std::for_each it is a const value, so retrieve it as a return value.
  56. functor = boost::geometry::for_each_segment(polyline, functor);
  57. // Output the results
  58. std::cout
  59. << "Min segment length: " << functor.min_length << std::endl
  60. << "Max segment length: " << functor.max_length << std::endl;
  61. return 0;
  62. }
  63. //]
  64. //[for_each_segment_const_output
  65. /*`
  66. Output:
  67. [pre
  68. Min segment length: 1.41421
  69. Max segment length: 5.65685
  70. ]
  71. */
  72. //]