1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
5 // This file was modified by Oracle on 2015.
6 // Modifications copyright (c) 2015 Oracle and/or its affiliates.
8 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_CLIP_LINESTRING_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_CLIP_LINESTRING_HPP
17 #include <boost/range.hpp>
19 #include <boost/geometry/algorithms/clear.hpp>
20 #include <boost/geometry/algorithms/convert.hpp>
22 #include <boost/geometry/algorithms/detail/overlay/append_no_duplicates.hpp>
24 #include <boost/geometry/util/select_coordinate_type.hpp>
25 #include <boost/geometry/geometries/segment.hpp>
27 namespace boost { namespace geometry
30 namespace strategy { namespace intersection
34 \brief Strategy: line clipping algorithm after Liang Barsky
36 \details The Liang-Barsky line clipping algorithm clips a line with a clipping box.
37 It is slightly adapted in the sense that it returns which points are clipped
38 \tparam B input box type of clipping box
39 \tparam P input/output point-type of segments to be clipped
40 \note The algorithm is currently only implemented for 2D Cartesian points
41 \note Though it is implemented in namespace strategy, and theoretically another
42 strategy could be used, it is not (yet) updated to the general strategy concepts,
43 and not (yet) splitted into a file in folder strategies
44 \author Barend Gehrels, and the following recourses
45 - A tutorial: http://www.skytopia.com/project/articles/compsci/clipping.html
46 - a German applet (link broken): http://ls7-www.cs.uni-dortmund.de/students/projectgroups/acit/lineclip.shtml
48 template<typename Box, typename Point>
52 typedef model::referring_segment<Point> segment_type;
54 template <typename CoordinateType, typename CalcType>
55 inline bool check_edge(CoordinateType const& p, CoordinateType const& q, CalcType& t1, CalcType& t2) const
61 CalcType const r = static_cast<CalcType>(q) / p;
69 CalcType const r = static_cast<CalcType>(q) / p;
86 inline bool clip_segment(Box const& b, segment_type& s, bool& sp1_clipped, bool& sp2_clipped) const
88 typedef typename select_coordinate_type<Box, Point>::type coordinate_type;
89 typedef typename select_most_precise<coordinate_type, double>::type calc_type;
94 coordinate_type const dx = get<1, 0>(s) - get<0, 0>(s);
95 coordinate_type const dy = get<1, 1>(s) - get<0, 1>(s);
97 coordinate_type const p1 = -dx;
98 coordinate_type const p2 = dx;
99 coordinate_type const p3 = -dy;
100 coordinate_type const p4 = dy;
102 coordinate_type const q1 = get<0, 0>(s) - get<min_corner, 0>(b);
103 coordinate_type const q2 = get<max_corner, 0>(b) - get<0, 0>(s);
104 coordinate_type const q3 = get<0, 1>(s) - get<min_corner, 1>(b);
105 coordinate_type const q4 = get<max_corner, 1>(b) - get<0, 1>(s);
107 if (check_edge(p1, q1, t1, t2) // left
108 && check_edge(p2, q2, t1, t2) // right
109 && check_edge(p3, q3, t1, t2) // bottom
110 && check_edge(p4, q4, t1, t2)) // top
112 sp1_clipped = t1 > 0;
113 sp2_clipped = t2 < 1;
117 set<1, 0>(s, get<0, 0>(s) + t2 * dx);
118 set<1, 1>(s, get<0, 1>(s) + t2 * dy);
123 set<0, 0>(s, get<0, 0>(s) + t1 * dx);
124 set<0, 1>(s, get<0, 1>(s) + t1 * dy);
133 template<typename Linestring, typename OutputIterator>
134 inline void apply(Linestring& line_out, OutputIterator out) const
136 if (!boost::empty(line_out))
140 geometry::clear(line_out);
146 }} // namespace strategy::intersection
149 #ifndef DOXYGEN_NO_DETAIL
150 namespace detail { namespace intersection
154 \brief Clips a linestring with a box
155 \details A linestring is intersected (clipped) by the specified box
156 and the resulting linestring, or pieces of linestrings, are sent to the specified output operator.
157 \tparam OutputLinestring type of the output linestrings
158 \tparam OutputIterator an output iterator which outputs linestrings
159 \tparam Linestring linestring-type, for example a vector of points, matching the output-iterator type,
160 the points should also match the input-iterator type
162 \tparam Strategy strategy, a clipping strategy which should implement the methods "clip_segment" and "apply"
166 typename OutputLinestring,
167 typename OutputIterator,
169 typename RobustPolicy,
173 OutputIterator clip_range_with_box(Box const& b, Range const& range,
175 OutputIterator out, Strategy const& strategy)
177 if (boost::begin(range) == boost::end(range))
182 typedef typename point_type<OutputLinestring>::type point_type;
184 OutputLinestring line_out;
186 typedef typename boost::range_iterator<Range const>::type iterator_type;
187 iterator_type vertex = boost::begin(range);
188 for(iterator_type previous = vertex++;
189 vertex != boost::end(range);
190 ++previous, ++vertex)
193 geometry::convert(*previous, p1);
194 geometry::convert(*vertex, p2);
196 // Clip the segment. Five situations:
197 // 1. Segment is invisible, finish line if any (shouldn't occur)
198 // 2. Segment is completely visible. Add (p1)-p2 to line
199 // 3. Point 1 is invisible (clipped), point 2 is visible. Start new line from p1-p2...
200 // 4. Point 1 is visible, point 2 is invisible (clipped). End the line with ...p2
201 // 5. Point 1 and point 2 are both invisible (clipped). Start/finish an independant line p1-p2
204 // a. if p1 is clipped, start new line
205 // b. if segment is partly or completely visible, add the segment
206 // c. if p2 is clipped, end the line
210 model::referring_segment<point_type> s(p1, p2);
212 if (!strategy.clip_segment(b, s, c1, c2))
214 strategy.apply(line_out, out);
218 // a. If necessary, finish the line and add a start a new one
221 strategy.apply(line_out, out);
224 // b. Add p1 only if it is the first point, then add p2
225 if (boost::empty(line_out))
227 detail::overlay::append_no_duplicates(line_out, p1, true);
229 detail::overlay::append_no_duplicates(line_out, p2);
231 // c. If c2 is clipped, finish the line
234 strategy.apply(line_out, out);
241 strategy.apply(line_out, out);
245 }} // namespace detail::intersection
246 #endif // DOXYGEN_NO_DETAIL
248 }} // namespace boost::geometry
250 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_CLIP_LINESTRING_HPP