clip_linestring.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2015-2020.
  4. // Modifications copyright (c) 2015-2020 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  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_DETAIL_OVERLAY_CLIP_LINESTRING_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_CLIP_LINESTRING_HPP
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/empty.hpp>
  14. #include <boost/range/end.hpp>
  15. #include <boost/geometry/algorithms/clear.hpp>
  16. #include <boost/geometry/algorithms/convert.hpp>
  17. #include <boost/geometry/algorithms/detail/overlay/append_no_duplicates.hpp>
  18. #include <boost/geometry/util/select_coordinate_type.hpp>
  19. #include <boost/geometry/geometries/segment.hpp>
  20. #include <boost/geometry/strategies/cartesian/point_in_point.hpp>
  21. namespace boost { namespace geometry
  22. {
  23. namespace strategy { namespace intersection
  24. {
  25. /*!
  26. \brief Strategy: line clipping algorithm after Liang Barsky
  27. \ingroup overlay
  28. \details The Liang-Barsky line clipping algorithm clips a line with a clipping box.
  29. It is slightly adapted in the sense that it returns which points are clipped
  30. \tparam B input box type of clipping box
  31. \tparam P input/output point-type of segments to be clipped
  32. \note The algorithm is currently only implemented for 2D Cartesian points
  33. \note Though it is implemented in namespace strategy, and theoretically another
  34. strategy could be used, it is not (yet) updated to the general strategy concepts,
  35. and not (yet) splitted into a file in folder strategies
  36. \author Barend Gehrels, and the following recourses
  37. - A tutorial: http://www.skytopia.com/project/articles/compsci/clipping.html
  38. - a German applet (link broken): http://ls7-www.cs.uni-dortmund.de/students/projectgroups/acit/lineclip.shtml
  39. */
  40. template<typename Box, typename Point>
  41. class liang_barsky
  42. {
  43. private:
  44. typedef model::referring_segment<Point> segment_type;
  45. template <typename CoordinateType, typename CalcType>
  46. inline bool check_edge(CoordinateType const& p, CoordinateType const& q, CalcType& t1, CalcType& t2) const
  47. {
  48. bool visible = true;
  49. if(p < 0)
  50. {
  51. CalcType const r = static_cast<CalcType>(q) / p;
  52. if (r > t2)
  53. visible = false;
  54. else if (r > t1)
  55. t1 = r;
  56. }
  57. else if(p > 0)
  58. {
  59. CalcType const r = static_cast<CalcType>(q) / p;
  60. if (r < t1)
  61. visible = false;
  62. else if (r < t2)
  63. t2 = r;
  64. }
  65. else
  66. {
  67. if (q < 0)
  68. visible = false;
  69. }
  70. return visible;
  71. }
  72. public:
  73. // TODO: Temporary, this strategy should be moved, it is cartesian-only
  74. typedef strategy::within::cartesian_point_point equals_point_point_strategy_type;
  75. static inline equals_point_point_strategy_type get_equals_point_point_strategy()
  76. {
  77. return equals_point_point_strategy_type();
  78. }
  79. inline bool clip_segment(Box const& b, segment_type& s, bool& sp1_clipped, bool& sp2_clipped) const
  80. {
  81. typedef typename select_coordinate_type<Box, Point>::type coordinate_type;
  82. typedef typename select_most_precise<coordinate_type, double>::type calc_type;
  83. calc_type t1 = 0;
  84. calc_type t2 = 1;
  85. coordinate_type const dx = get<1, 0>(s) - get<0, 0>(s);
  86. coordinate_type const dy = get<1, 1>(s) - get<0, 1>(s);
  87. coordinate_type const p1 = -dx;
  88. coordinate_type const p2 = dx;
  89. coordinate_type const p3 = -dy;
  90. coordinate_type const p4 = dy;
  91. coordinate_type const q1 = get<0, 0>(s) - get<min_corner, 0>(b);
  92. coordinate_type const q2 = get<max_corner, 0>(b) - get<0, 0>(s);
  93. coordinate_type const q3 = get<0, 1>(s) - get<min_corner, 1>(b);
  94. coordinate_type const q4 = get<max_corner, 1>(b) - get<0, 1>(s);
  95. if (check_edge(p1, q1, t1, t2) // left
  96. && check_edge(p2, q2, t1, t2) // right
  97. && check_edge(p3, q3, t1, t2) // bottom
  98. && check_edge(p4, q4, t1, t2)) // top
  99. {
  100. sp1_clipped = t1 > 0;
  101. sp2_clipped = t2 < 1;
  102. if (sp2_clipped)
  103. {
  104. set<1, 0>(s, get<0, 0>(s) + t2 * dx);
  105. set<1, 1>(s, get<0, 1>(s) + t2 * dy);
  106. }
  107. if(sp1_clipped)
  108. {
  109. set<0, 0>(s, get<0, 0>(s) + t1 * dx);
  110. set<0, 1>(s, get<0, 1>(s) + t1 * dy);
  111. }
  112. return true;
  113. }
  114. return false;
  115. }
  116. template<typename Linestring, typename OutputIterator>
  117. inline void apply(Linestring& line_out, OutputIterator out) const
  118. {
  119. if (!boost::empty(line_out))
  120. {
  121. *out = line_out;
  122. ++out;
  123. geometry::clear(line_out);
  124. }
  125. }
  126. };
  127. }} // namespace strategy::intersection
  128. #ifndef DOXYGEN_NO_DETAIL
  129. namespace detail { namespace intersection
  130. {
  131. /*!
  132. \brief Clips a linestring with a box
  133. \details A linestring is intersected (clipped) by the specified box
  134. and the resulting linestring, or pieces of linestrings, are sent to the specified output operator.
  135. \tparam OutputLinestring type of the output linestrings
  136. \tparam OutputIterator an output iterator which outputs linestrings
  137. \tparam Linestring linestring-type, for example a vector of points, matching the output-iterator type,
  138. the points should also match the input-iterator type
  139. \tparam Box box type
  140. \tparam Strategy strategy, a clipping strategy which should implement the methods "clip_segment" and "apply"
  141. */
  142. template
  143. <
  144. typename OutputLinestring,
  145. typename OutputIterator,
  146. typename Range,
  147. typename RobustPolicy,
  148. typename Box,
  149. typename Strategy
  150. >
  151. OutputIterator clip_range_with_box(Box const& b, Range const& range,
  152. RobustPolicy const&,
  153. OutputIterator out, Strategy const& strategy)
  154. {
  155. if (boost::begin(range) == boost::end(range))
  156. {
  157. return out;
  158. }
  159. typedef typename point_type<OutputLinestring>::type point_type;
  160. OutputLinestring line_out;
  161. auto vertex = boost::begin(range);
  162. for (auto previous = vertex++;
  163. vertex != boost::end(range);
  164. ++previous, ++vertex)
  165. {
  166. point_type p1, p2;
  167. geometry::convert(*previous, p1);
  168. geometry::convert(*vertex, p2);
  169. // Clip the segment. Five situations:
  170. // 1. Segment is invisible, finish line if any (shouldn't occur)
  171. // 2. Segment is completely visible. Add (p1)-p2 to line
  172. // 3. Point 1 is invisible (clipped), point 2 is visible. Start new line from p1-p2...
  173. // 4. Point 1 is visible, point 2 is invisible (clipped). End the line with ...p2
  174. // 5. Point 1 and point 2 are both invisible (clipped). Start/finish an independant line p1-p2
  175. //
  176. // This results in:
  177. // a. if p1 is clipped, start new line
  178. // b. if segment is partly or completely visible, add the segment
  179. // c. if p2 is clipped, end the line
  180. bool c1 = false;
  181. bool c2 = false;
  182. model::referring_segment<point_type> s(p1, p2);
  183. if (!strategy.clip_segment(b, s, c1, c2))
  184. {
  185. strategy.apply(line_out, out);
  186. }
  187. else
  188. {
  189. // a. If necessary, finish the line and add a start a new one
  190. if (c1)
  191. {
  192. strategy.apply(line_out, out);
  193. }
  194. // b. Add p1 only if it is the first point, then add p2
  195. if (boost::empty(line_out))
  196. {
  197. detail::overlay::append_with_duplicates(line_out, p1);
  198. }
  199. detail::overlay::append_no_duplicates(line_out, p2,
  200. strategy.get_equals_point_point_strategy());
  201. // c. If c2 is clipped, finish the line
  202. if (c2)
  203. {
  204. strategy.apply(line_out, out);
  205. }
  206. }
  207. }
  208. // Add last part
  209. strategy.apply(line_out, out);
  210. return out;
  211. }
  212. }} // namespace detail::intersection
  213. #endif // DOXYGEN_NO_DETAIL
  214. }} // namespace boost::geometry
  215. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_CLIP_LINESTRING_HPP