buffer.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_BUFFER_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_BUFFER_HPP
  12. #include <cstddef>
  13. #include <boost/numeric/conversion/cast.hpp>
  14. #include <boost/geometry/algorithms/clear.hpp>
  15. #include <boost/geometry/algorithms/not_implemented.hpp>
  16. #include <boost/geometry/algorithms/detail/disjoint.hpp>
  17. #include <boost/geometry/arithmetic/arithmetic.hpp>
  18. #include <boost/geometry/geometries/concepts/check.hpp>
  19. #include <boost/geometry/geometries/segment.hpp>
  20. #include <boost/geometry/util/math.hpp>
  21. namespace boost { namespace geometry
  22. {
  23. #ifndef DOXYGEN_NO_DETAIL
  24. namespace detail { namespace buffer
  25. {
  26. template <typename BoxIn, typename BoxOut, typename T, std::size_t C, std::size_t D, std::size_t N>
  27. struct box_loop
  28. {
  29. typedef typename coordinate_type<BoxOut>::type coordinate_type;
  30. static inline void apply(BoxIn const& box_in, T const& distance, BoxOut& box_out)
  31. {
  32. coordinate_type d = distance;
  33. set<C, D>(box_out, get<C, D>(box_in) + d);
  34. box_loop<BoxIn, BoxOut, T, C, D + 1, N>::apply(box_in, distance, box_out);
  35. }
  36. };
  37. template <typename BoxIn, typename BoxOut, typename T, std::size_t C, std::size_t N>
  38. struct box_loop<BoxIn, BoxOut, T, C, N, N>
  39. {
  40. static inline void apply(BoxIn const&, T const&, BoxOut&) {}
  41. };
  42. // Extends a box with the same amount in all directions
  43. template<typename BoxIn, typename BoxOut, typename T>
  44. inline void buffer_box(BoxIn const& box_in, T const& distance, BoxOut& box_out)
  45. {
  46. assert_dimension_equal<BoxIn, BoxOut>();
  47. static const std::size_t N = dimension<BoxIn>::value;
  48. box_loop<BoxIn, BoxOut, T, min_corner, 0, N>::apply(box_in, -distance, box_out);
  49. box_loop<BoxIn, BoxOut, T, max_corner, 0, N>::apply(box_in, distance, box_out);
  50. }
  51. }} // namespace detail::buffer
  52. #endif // DOXYGEN_NO_DETAIL
  53. #ifndef DOXYGEN_NO_DISPATCH
  54. namespace dispatch
  55. {
  56. template
  57. <
  58. typename Input,
  59. typename Output,
  60. typename TagIn = typename tag<Input>::type,
  61. typename TagOut = typename tag<Output>::type
  62. >
  63. struct buffer: not_implemented<TagIn, TagOut>
  64. {};
  65. template <typename BoxIn, typename BoxOut>
  66. struct buffer<BoxIn, BoxOut, box_tag, box_tag>
  67. {
  68. template <typename Distance>
  69. static inline void apply(BoxIn const& box_in, Distance const& distance,
  70. Distance const& , BoxIn& box_out)
  71. {
  72. detail::buffer::buffer_box(box_in, distance, box_out);
  73. }
  74. };
  75. // Many things to do. Point is easy, other geometries require self intersections
  76. // For point, note that it should output as a polygon (like the rest). Buffers
  77. // of a set of geometries are often lateron combined using a "dissolve" operation.
  78. // Two points close to each other get a combined kidney shaped buffer then.
  79. } // namespace dispatch
  80. #endif // DOXYGEN_NO_DISPATCH
  81. /*!
  82. \brief \brief_calc{buffer}
  83. \ingroup buffer
  84. \details \details_calc{buffer, \det_buffer}.
  85. \tparam Input \tparam_geometry
  86. \tparam Output \tparam_geometry
  87. \tparam Distance \tparam_numeric
  88. \param geometry_in \param_geometry
  89. \param geometry_out \param_geometry
  90. \param distance The distance to be used for the buffer
  91. \param chord_length (optional) The length of the chord's in the generated arcs around points or bends
  92. \note Currently only implemented for box, the trivial case, but still useful
  93. \qbk{[include reference/algorithms/buffer.qbk]}
  94. */
  95. template <typename Input, typename Output, typename Distance>
  96. inline void buffer(Input const& geometry_in, Output& geometry_out,
  97. Distance const& distance, Distance const& chord_length = -1)
  98. {
  99. concept::check<Input const>();
  100. concept::check<Output>();
  101. dispatch::buffer
  102. <
  103. Input,
  104. Output
  105. >::apply(geometry_in, distance, chord_length, geometry_out);
  106. }
  107. /*!
  108. \brief \brief_calc{buffer}
  109. \ingroup buffer
  110. \details \details_calc{return_buffer, \det_buffer}. \details_return{buffer}.
  111. \tparam Input \tparam_geometry
  112. \tparam Output \tparam_geometry
  113. \tparam Distance \tparam_numeric
  114. \param geometry \param_geometry
  115. \param distance The distance to be used for the buffer
  116. \param chord_length (optional) The length of the chord's in the generated arcs
  117. around points or bends
  118. \return \return_calc{buffer}
  119. */
  120. template <typename Output, typename Input, typename Distance>
  121. Output return_buffer(Input const& geometry, Distance const& distance, Distance const& chord_length = -1)
  122. {
  123. concept::check<Input const>();
  124. concept::check<Output>();
  125. Output geometry_out;
  126. dispatch::buffer
  127. <
  128. Input,
  129. Output
  130. >::apply(geometry, distance, chord_length, geometry_out);
  131. return geometry_out;
  132. }
  133. }} // namespace boost::geometry
  134. #endif // BOOST_GEOMETRY_ALGORITHMS_BUFFER_HPP