buffer_side_straight.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_CARTESIAN_BUFFER_SIDE_STRAIGHT_HPP
  7. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_SIDE_STRAIGHT_HPP
  8. #include <cstddef>
  9. #include <boost/math/special_functions/fpclassify.hpp>
  10. #include <boost/geometry/core/coordinate_type.hpp>
  11. #include <boost/geometry/core/access.hpp>
  12. #include <boost/geometry/util/math.hpp>
  13. #include <boost/geometry/util/select_most_precise.hpp>
  14. #include <boost/geometry/strategies/buffer.hpp>
  15. #include <boost/geometry/strategies/side.hpp>
  16. namespace boost { namespace geometry
  17. {
  18. namespace strategy { namespace buffer
  19. {
  20. /*!
  21. \brief Let the buffer use straight sides along segments (the default)
  22. \ingroup strategies
  23. \details This strategy can be used as SideStrategy for the buffer algorithm.
  24. It is currently the only provided strategy for this purpose
  25. \qbk{
  26. [heading Example]
  27. See the examples for other buffer strategies\, for example
  28. [link geometry.reference.strategies.strategy_buffer_join_round join_round]
  29. [heading See also]
  30. \* [link geometry.reference.algorithms.buffer.buffer_7_with_strategies buffer (with strategies)]
  31. }
  32. */
  33. class side_straight
  34. {
  35. public :
  36. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  37. // Returns true if the buffer distance is always the same
  38. static inline bool equidistant()
  39. {
  40. return true;
  41. }
  42. template
  43. <
  44. typename Point,
  45. typename OutputRange,
  46. typename DistanceStrategy
  47. >
  48. static inline result_code apply(
  49. Point const& input_p1, Point const& input_p2,
  50. buffer_side_selector side,
  51. DistanceStrategy const& distance,
  52. OutputRange& output_range)
  53. {
  54. typedef typename coordinate_type<Point>::type coordinate_type;
  55. typedef typename geometry::select_most_precise
  56. <
  57. coordinate_type,
  58. double
  59. >::type promoted_type;
  60. // Generate a block along (left or right of) the segment
  61. // Simulate a vector d (dx,dy)
  62. promoted_type const dx = get<0>(input_p2) - get<0>(input_p1);
  63. promoted_type const dy = get<1>(input_p2) - get<1>(input_p1);
  64. // For normalization [0,1] (=dot product d.d, sqrt)
  65. promoted_type const length = geometry::math::sqrt(dx * dx + dy * dy);
  66. if (! boost::math::isfinite(length))
  67. {
  68. // In case of coordinates differences of e.g. 1e300, length
  69. // will overflow and we should not generate output
  70. #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN
  71. std::cout << "Error in length calculation for points "
  72. << geometry::wkt(input_p1) << " " << geometry::wkt(input_p2)
  73. << " length: " << length << std::endl;
  74. #endif
  75. return result_error_numerical;
  76. }
  77. if (geometry::math::equals(length, 0))
  78. {
  79. // Coordinates are simplified and therefore most often not equal.
  80. // But if simplify is skipped, or for lines with two
  81. // equal points, length is 0 and we cannot generate output.
  82. return result_no_output;
  83. }
  84. promoted_type const d = distance.apply(input_p1, input_p2, side);
  85. // Generate the normalized perpendicular p, to the left (ccw)
  86. promoted_type const px = -dy / length;
  87. promoted_type const py = dx / length;
  88. if (geometry::math::equals(px, 0)
  89. && geometry::math::equals(py, 0))
  90. {
  91. // This basically should not occur - because of the checks above.
  92. // There are no unit tests triggering this condition
  93. #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN
  94. std::cout << "Error in perpendicular calculation for points "
  95. << geometry::wkt(input_p1) << " " << geometry::wkt(input_p2)
  96. << " length: " << length
  97. << " distance: " << d
  98. << std::endl;
  99. #endif
  100. return result_no_output;
  101. }
  102. output_range.resize(2);
  103. set<0>(output_range.front(), get<0>(input_p1) + px * d);
  104. set<1>(output_range.front(), get<1>(input_p1) + py * d);
  105. set<0>(output_range.back(), get<0>(input_p2) + px * d);
  106. set<1>(output_range.back(), get<1>(input_p2) + py * d);
  107. return result_normal;
  108. }
  109. #endif // DOXYGEN_SHOULD_SKIP_THIS
  110. };
  111. }} // namespace strategy::buffer
  112. }} // namespace boost::geometry
  113. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_SIDE_STRAIGHT_HPP