clip_linestring.hpp 8.5 KB

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