1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2014-2018, Oracle and/or its affiliates.
5 // 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)
11 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_TOPOLOGY_CHECK_HPP
12 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_TOPOLOGY_CHECK_HPP
15 #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
16 #include <boost/geometry/algorithms/not_implemented.hpp>
18 #include <boost/geometry/policies/compare.hpp>
20 #include <boost/geometry/util/has_nan_coordinate.hpp>
21 #include <boost/geometry/util/range.hpp>
24 namespace boost { namespace geometry {
26 #ifndef DOXYGEN_NO_DETAIL
27 namespace detail { namespace relate {
29 // TODO: change the name for e.g. something with the word "exterior"
31 template <typename Geometry,
32 typename EqPPStrategy,
33 typename Tag = typename geometry::tag<Geometry>::type>
35 : not_implemented<Tag>
38 //template <typename Point>
39 //struct topology_check<Point, point_tag>
41 // static const char interior = '0';
42 // static const char boundary = 'F';
44 // static const bool has_interior = true;
45 // static const bool has_boundary = false;
47 // topology_check(Point const&) {}
48 // template <typename IgnoreBoundaryPoint>
49 // topology_check(Point const&, IgnoreBoundaryPoint const&) {}
52 template <typename Linestring, typename EqPPStrategy>
53 struct topology_check<Linestring, EqPPStrategy, linestring_tag>
55 static const char interior = '1';
56 static const char boundary = '0';
58 topology_check(Linestring const& ls)
60 , m_is_initialized(false)
63 bool has_interior() const
66 return m_has_interior;
69 bool has_boundary() const
72 return m_has_boundary;
75 /*template <typename Point>
76 bool check_boundary_point(Point const& point) const
80 && ( equals::equals_point_point(point, range::front(m_ls))
81 || equals::equals_point_point(point, range::back(m_ls)) );
84 template <typename Visitor>
85 void for_each_boundary_point(Visitor & visitor) const
90 if (visitor.apply(range::front(m_ls)))
91 visitor.apply(range::back(m_ls));
101 std::size_t count = boost::size(m_ls);
102 m_has_interior = count > 0;
103 // NOTE: Linestring with all points equal is treated as 1d linear ring
104 m_has_boundary = count > 1
105 && ! detail::equals::equals_point_point(range::front(m_ls),
109 m_is_initialized = true;
112 Linestring const& m_ls;
113 mutable bool m_is_initialized;
115 mutable bool m_has_interior;
116 mutable bool m_has_boundary;
119 template <typename MultiLinestring, typename EqPPStrategy>
120 struct topology_check<MultiLinestring, EqPPStrategy, multi_linestring_tag>
122 static const char interior = '1';
123 static const char boundary = '0';
125 topology_check(MultiLinestring const& mls)
127 , m_is_initialized(false)
130 bool has_interior() const
133 return m_has_interior;
136 bool has_boundary() const
139 return m_has_boundary;
142 template <typename Point>
143 bool check_boundary_point(Point const& point) const
147 if (! m_has_boundary)
150 std::size_t count = count_equal(m_endpoints.begin(), m_endpoints.end(), point);
152 return count % 2 != 0; // odd count -> boundary
155 template <typename Visitor>
156 void for_each_boundary_point(Visitor & visitor) const
161 for_each_boundary_point(m_endpoints.begin(), m_endpoints.end(), visitor);
166 typedef geometry::less<void, -1, typename EqPPStrategy::cs_tag> less_type;
170 if (m_is_initialized)
173 m_endpoints.reserve(boost::size(m_mls) * 2);
175 m_has_interior = false;
177 typedef typename boost::range_iterator<MultiLinestring const>::type ls_iterator;
178 for ( ls_iterator it = boost::begin(m_mls) ; it != boost::end(m_mls) ; ++it )
180 typename boost::range_reference<MultiLinestring const>::type
183 std::size_t count = boost::size(ls);
187 m_has_interior = true;
192 typedef typename boost::range_reference
194 typename boost::range_value<MultiLinestring const>::type const
195 >::type point_reference;
197 point_reference front_pt = range::front(ls);
198 point_reference back_pt = range::back(ls);
200 // don't store boundaries of linear rings, this doesn't change anything
201 if (! equals::equals_point_point(front_pt, back_pt, EqPPStrategy()))
203 // do not add points containing NaN coordinates
204 // because they cannot be reasonably compared, e.g. with MSVC
205 // an assertion failure is reported in std::equal_range()
206 // NOTE: currently ignoring_counter calling std::equal_range()
207 // is not used anywhere in the code, still it's safer this way
208 if (! geometry::has_nan_coordinate(front_pt))
210 m_endpoints.push_back(front_pt);
212 if (! geometry::has_nan_coordinate(back_pt))
214 m_endpoints.push_back(back_pt);
220 m_has_boundary = false;
222 if (! m_endpoints.empty() )
224 std::sort(m_endpoints.begin(), m_endpoints.end(), less_type());
225 m_has_boundary = find_odd_count(m_endpoints.begin(), m_endpoints.end());
228 m_is_initialized = true;
231 template <typename It, typename Point>
232 static inline std::size_t count_equal(It first, It last, Point const& point)
234 std::pair<It, It> rng = std::equal_range(first, last, point, less_type());
235 return (std::size_t)std::distance(rng.first, rng.second);
238 template <typename It>
239 static inline bool find_odd_count(It first, It last)
241 interrupting_visitor visitor;
242 for_each_boundary_point(first, last, visitor);
243 return visitor.found;
246 struct interrupting_visitor
249 interrupting_visitor() : found(false) {}
250 template <typename Point>
251 bool apply(Point const&)
258 template <typename It, typename Visitor>
259 static void for_each_boundary_point(It first, It last, Visitor& visitor)
264 std::size_t count = 1;
267 for ( ; first != last ; ++first, ++prev )
269 // the end of the equal points subrange
270 if ( ! equals::equals_point_point(*first, *prev, EqPPStrategy()) )
272 // odd count -> boundary
273 if ( count % 2 != 0 )
275 if (! visitor.apply(*prev))
289 // odd count -> boundary
290 if ( count % 2 != 0 )
292 visitor.apply(*prev);
297 MultiLinestring const& m_mls;
298 mutable bool m_is_initialized;
300 mutable bool m_has_interior;
301 mutable bool m_has_boundary;
303 typedef typename geometry::point_type<MultiLinestring>::type point_type;
304 mutable std::vector<point_type> m_endpoints;
307 struct topology_check_areal
309 static const char interior = '2';
310 static const char boundary = '1';
312 static bool has_interior() { return true; }
313 static bool has_boundary() { return true; }
316 template <typename Ring, typename EqPPStrategy>
317 struct topology_check<Ring, EqPPStrategy, ring_tag>
318 : topology_check_areal
320 topology_check(Ring const&) {}
323 template <typename Polygon, typename EqPPStrategy>
324 struct topology_check<Polygon, EqPPStrategy, polygon_tag>
325 : topology_check_areal
327 topology_check(Polygon const&) {}
330 template <typename MultiPolygon, typename EqPPStrategy>
331 struct topology_check<MultiPolygon, EqPPStrategy, multi_polygon_tag>
332 : topology_check_areal
334 topology_check(MultiPolygon const&) {}
336 template <typename Point>
337 static bool check_boundary_point(Point const& ) { return true; }
340 }} // namespace detail::relate
341 #endif // DOXYGEN_NO_DETAIL
343 }} // namespace boost::geometry
345 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_TOPOLOGY_CHECK_HPP