buffer_distance_asymmetric.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2012-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_STRATEGIES_AGNOSTIC_BUFFER_DISTANCE_ASYMMETRIC_HPP
  7. #define BOOST_GEOMETRY_STRATEGIES_AGNOSTIC_BUFFER_DISTANCE_ASYMMETRIC_HPP
  8. #include <boost/core/ignore_unused.hpp>
  9. #include <boost/geometry/strategies/buffer.hpp>
  10. #include <boost/geometry/util/math.hpp>
  11. namespace boost { namespace geometry
  12. {
  13. namespace strategy { namespace buffer
  14. {
  15. /*!
  16. \brief Let the buffer for linestrings be asymmetric
  17. \ingroup strategies
  18. \tparam NumericType \tparam_numeric
  19. \details This strategy can be used as DistanceStrategy for the buffer algorithm.
  20. It can be applied for (multi)linestrings. It uses a (potentially) different
  21. distances for left and for right. This means the (multi)linestrings are
  22. interpreted having a direction.
  23. \qbk{
  24. [heading Example]
  25. [buffer_distance_asymmetric]
  26. [heading Output]
  27. [$img/strategies/buffer_distance_asymmetric.png]
  28. [heading See also]
  29. \* [link geometry.reference.algorithms.buffer.buffer_7_with_strategies buffer (with strategies)]
  30. \* [link geometry.reference.strategies.strategy_buffer_distance_symmetric distance_symmetric]
  31. }
  32. */
  33. template<typename NumericType>
  34. class distance_asymmetric
  35. {
  36. public :
  37. //! \brief Constructs the strategy, two distances must be specified
  38. //! \param left The distance (or radius) of the buffer on the left side
  39. //! \param right The distance on the right side
  40. distance_asymmetric(NumericType const& left,
  41. NumericType const& right)
  42. : m_left(left)
  43. , m_right(right)
  44. {}
  45. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  46. //! Returns the distance-value for the specified side
  47. template <typename Point>
  48. inline NumericType apply(Point const& , Point const& ,
  49. buffer_side_selector side) const
  50. {
  51. NumericType result = side == buffer_side_left ? m_left : m_right;
  52. return negative() ? math::abs(result) : result;
  53. }
  54. //! Used internally, returns -1 for deflate, 1 for inflate
  55. inline int factor() const
  56. {
  57. return negative() ? -1 : 1;
  58. }
  59. //! Returns true if both distances are negative
  60. inline bool negative() const
  61. {
  62. return m_left < 0 && m_right < 0;
  63. }
  64. //! Returns the max distance distance up to the buffer will reach
  65. template <typename JoinStrategy, typename EndStrategy>
  66. inline NumericType max_distance(JoinStrategy const& join_strategy,
  67. EndStrategy const& end_strategy) const
  68. {
  69. boost::ignore_unused(join_strategy, end_strategy);
  70. NumericType const left = geometry::math::abs(m_left);
  71. NumericType const right = geometry::math::abs(m_right);
  72. NumericType const dist = (std::max)(left, right);
  73. return (std::max)(join_strategy.max_distance(dist),
  74. end_strategy.max_distance(dist));
  75. }
  76. //! Returns the distance at which the input is simplified before the buffer process
  77. inline NumericType simplify_distance() const
  78. {
  79. NumericType const left = geometry::math::abs(m_left);
  80. NumericType const right = geometry::math::abs(m_right);
  81. return (std::min)(left, right) / 1000.0;
  82. }
  83. #endif // DOXYGEN_SHOULD_SKIP_THIS
  84. private :
  85. NumericType m_left;
  86. NumericType m_right;
  87. };
  88. }} // namespace strategy::buffer
  89. }} // namespace boost::geometry
  90. #endif // BOOST_GEOMETRY_STRATEGIES_AGNOSTIC_BUFFER_DISTANCE_ASYMMETRIC_HPP