buffer_side_straight.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. template
  38. <
  39. typename Point,
  40. typename OutputRange,
  41. typename DistanceStrategy
  42. >
  43. static inline result_code apply(
  44. Point const& input_p1, Point const& input_p2,
  45. buffer_side_selector side,
  46. DistanceStrategy const& distance,
  47. OutputRange& output_range)
  48. {
  49. typedef typename coordinate_type<Point>::type coordinate_type;
  50. typedef typename geometry::select_most_precise
  51. <
  52. coordinate_type,
  53. double
  54. >::type promoted_type;
  55. // Generate a block along (left or right of) the segment
  56. // Simulate a vector d (dx,dy)
  57. coordinate_type const dx = get<0>(input_p2) - get<0>(input_p1);
  58. coordinate_type const dy = get<1>(input_p2) - get<1>(input_p1);
  59. // For normalization [0,1] (=dot product d.d, sqrt)
  60. promoted_type const length = geometry::math::sqrt(dx * dx + dy * dy);
  61. if (! boost::math::isfinite(length))
  62. {
  63. // In case of coordinates differences of e.g. 1e300, length
  64. // will overflow and we should not generate output
  65. #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN
  66. std::cout << "Error in length calculation for points "
  67. << geometry::wkt(input_p1) << " " << geometry::wkt(input_p2)
  68. << " length: " << length << std::endl;
  69. #endif
  70. return result_error_numerical;
  71. }
  72. if (geometry::math::equals(length, 0))
  73. {
  74. // Coordinates are simplified and therefore most often not equal.
  75. // But if simplify is skipped, or for lines with two
  76. // equal points, length is 0 and we cannot generate output.
  77. return result_no_output;
  78. }
  79. promoted_type const d = distance.apply(input_p1, input_p2, side);
  80. // Generate the normalized perpendicular p, to the left (ccw)
  81. promoted_type const px = -dy / length;
  82. promoted_type const py = dx / length;
  83. if (geometry::math::equals(px, 0)
  84. && geometry::math::equals(py, 0))
  85. {
  86. // This basically should not occur - because of the checks above.
  87. // There are no unit tests triggering this condition
  88. #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN
  89. std::cout << "Error in perpendicular calculation for points "
  90. << geometry::wkt(input_p1) << " " << geometry::wkt(input_p2)
  91. << " length: " << length
  92. << " distance: " << d
  93. << std::endl;
  94. #endif
  95. return result_no_output;
  96. }
  97. output_range.resize(2);
  98. set<0>(output_range.front(), get<0>(input_p1) + px * d);
  99. set<1>(output_range.front(), get<1>(input_p1) + py * d);
  100. set<0>(output_range.back(), get<0>(input_p2) + px * d);
  101. set<1>(output_range.back(), get<1>(input_p2) + py * d);
  102. return result_normal;
  103. }
  104. #endif // DOXYGEN_SHOULD_SKIP_THIS
  105. };
  106. }} // namespace strategy::buffer
  107. }} // namespace boost::geometry
  108. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_SIDE_STRAIGHT_HPP