1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
6 // Copyright (c) 2014-2015 Adam Wulkiewicz, Lodz, Poland.
8 // This file was modified by Oracle on 2014, 2015, 2016, 2017, 2018, 2019.
9 // Modifications copyright (c) 2014-2019 Oracle and/or its affiliates.
11 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
12 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
14 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
15 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
17 // Use, modification and distribution is subject to the Boost Software License,
18 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
19 // http://www.boost.org/LICENSE_1_0.txt)
21 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_EQUALS_IMPLEMENTATION_HPP
22 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_EQUALS_IMPLEMENTATION_HPP
28 #include <boost/range.hpp>
29 #include <boost/type_traits/is_base_of.hpp>
31 #include <boost/geometry/core/access.hpp>
32 #include <boost/geometry/core/tags.hpp>
34 #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
37 #include <boost/geometry/algorithms/area.hpp>
38 #include <boost/geometry/algorithms/length.hpp>
39 #include <boost/geometry/util/math.hpp>
40 #include <boost/geometry/util/select_coordinate_type.hpp>
41 #include <boost/geometry/util/select_most_precise.hpp>
43 #include <boost/geometry/algorithms/detail/equals/collect_vectors.hpp>
44 #include <boost/geometry/algorithms/detail/equals/interface.hpp>
45 #include <boost/geometry/algorithms/detail/relate/relate_impl.hpp>
46 #include <boost/geometry/algorithms/relate.hpp>
48 #include <boost/geometry/views/detail/indexed_point_view.hpp>
51 namespace boost { namespace geometry
54 #ifndef DOXYGEN_NO_DETAIL
55 namespace detail { namespace equals
61 std::size_t Dimension,
62 std::size_t DimensionCount
66 template <typename Point1, typename Point2, typename Strategy>
67 static inline bool apply(Point1 const& point1, Point2 const& point2, Strategy const& )
69 return Strategy::apply(point1, point2);
76 std::size_t Dimension,
77 std::size_t DimensionCount
81 template <typename Box1, typename Box2, typename Strategy>
82 static inline bool apply(Box1 const& box1, Box2 const& box2, Strategy const& strategy)
84 if (!geometry::math::equals(get<min_corner, Dimension>(box1), get<min_corner, Dimension>(box2))
85 || !geometry::math::equals(get<max_corner, Dimension>(box1), get<max_corner, Dimension>(box2)))
89 return box_box<Dimension + 1, DimensionCount>::apply(box1, box2, strategy);
93 template <std::size_t DimensionCount>
94 struct box_box<DimensionCount, DimensionCount>
96 template <typename Box1, typename Box2, typename Strategy>
97 static inline bool apply(Box1 const& , Box2 const& , Strategy const& )
104 struct segment_segment
106 template <typename Segment1, typename Segment2, typename Strategy>
107 static inline bool apply(Segment1 const& segment1, Segment2 const& segment2,
108 Strategy const& strategy)
110 typename Strategy::point_in_point_strategy_type const&
111 pt_pt_strategy = strategy.get_point_in_point_strategy();
113 return equals::equals_point_point(
114 indexed_point_view<Segment1 const, 0>(segment1),
115 indexed_point_view<Segment2 const, 0>(segment2),
117 ? equals::equals_point_point(
118 indexed_point_view<Segment1 const, 1>(segment1),
119 indexed_point_view<Segment2 const, 1>(segment2),
121 : ( equals::equals_point_point(
122 indexed_point_view<Segment1 const, 0>(segment1),
123 indexed_point_view<Segment2 const, 1>(segment2),
125 && equals::equals_point_point(
126 indexed_point_view<Segment1 const, 1>(segment1),
127 indexed_point_view<Segment2 const, 0>(segment2),
136 template <typename Geometry1, typename Geometry2, typename Strategy>
137 static inline bool apply(Geometry1 const& geometry1,
138 Geometry2 const& geometry2,
139 Strategy const& strategy)
141 return geometry::math::equals(
142 geometry::area(geometry1,
143 strategy.template get_area_strategy<Geometry1>()),
144 geometry::area(geometry2,
145 strategy.template get_area_strategy<Geometry2>()));
152 template <typename Geometry1, typename Geometry2, typename Strategy>
153 static inline bool apply(Geometry1 const& geometry1,
154 Geometry2 const& geometry2,
155 Strategy const& strategy)
157 return geometry::math::equals(
158 geometry::length(geometry1,
159 strategy.template get_distance_strategy<Geometry1>()),
160 geometry::length(geometry2,
161 strategy.template get_distance_strategy<Geometry2>()));
166 template <typename Geometry1, typename Geometry2, typename IntersectionStrategy>
167 struct collected_vector
169 typedef typename geometry::select_most_precise
171 typename select_coordinate_type
176 >::type calculation_type;
178 typedef geometry::collected_vector
182 typename IntersectionStrategy::side_strategy_type
186 template <typename TrivialCheck>
187 struct equals_by_collection
189 template <typename Geometry1, typename Geometry2, typename Strategy>
190 static inline bool apply(Geometry1 const& geometry1,
191 Geometry2 const& geometry2,
192 Strategy const& strategy)
194 if (! TrivialCheck::apply(geometry1, geometry2, strategy))
199 typedef typename collected_vector
201 Geometry1, Geometry2, Strategy
202 >::type collected_vector_type;
204 std::vector<collected_vector_type> c1, c2;
206 geometry::collect_vectors(c1, geometry1);
207 geometry::collect_vectors(c2, geometry2);
209 if (boost::size(c1) != boost::size(c2))
214 std::sort(c1.begin(), c1.end());
215 std::sort(c2.begin(), c2.end());
217 // Just check if these vectors are equal.
218 return std::equal(c1.begin(), c1.end(), c2.begin());
222 template<typename Geometry1, typename Geometry2>
223 struct equals_by_relate
224 : detail::relate::relate_impl
226 detail::de9im::static_mask_equals_type,
232 // If collect_vectors which is a SideStrategy-dispatched optimization
233 // is implemented in a way consistent with the Intersection/Side Strategy
234 // then collect_vectors is used, otherwise relate is used.
235 // NOTE: the result could be conceptually different for invalid
236 // geometries in different coordinate systems because collect_vectors
237 // and relate treat invalid geometries differently.
238 template<typename TrivialCheck>
239 struct equals_by_collection_or_relate
241 template <typename Geometry1, typename Geometry2, typename Strategy>
242 static inline bool apply(Geometry1 const& geometry1,
243 Geometry2 const& geometry2,
244 Strategy const& strategy)
246 typedef typename boost::is_base_of
248 nyi::not_implemented_tag,
249 typename collected_vector
251 Geometry1, Geometry2, Strategy
253 >::type enable_relate_type;
255 return apply(geometry1, geometry2, strategy, enable_relate_type());
259 template <typename Geometry1, typename Geometry2, typename Strategy>
260 static inline bool apply(Geometry1 const& geometry1,
261 Geometry2 const& geometry2,
262 Strategy const& strategy,
263 boost::false_type /*enable_relate*/)
265 return equals_by_collection<TrivialCheck>::apply(geometry1, geometry2, strategy);
268 template <typename Geometry1, typename Geometry2, typename Strategy>
269 static inline bool apply(Geometry1 const& geometry1,
270 Geometry2 const& geometry2,
271 Strategy const& strategy,
272 boost::true_type /*enable_relate*/)
274 return equals_by_relate<Geometry1, Geometry2>::apply(geometry1, geometry2, strategy);
278 struct equals_always_false
280 template <typename Geometry1, typename Geometry2, typename Strategy>
281 static inline bool apply(Geometry1 const& , Geometry2 const& , Strategy const& )
288 }} // namespace detail::equals
289 #endif // DOXYGEN_NO_DETAIL
292 #ifndef DOXYGEN_NO_DISPATCH
296 template <typename P1, typename P2, std::size_t DimensionCount, bool Reverse>
297 struct equals<P1, P2, point_tag, point_tag, pointlike_tag, pointlike_tag, DimensionCount, Reverse>
298 : detail::equals::point_point<0, DimensionCount>
301 template <typename MultiPoint1, typename MultiPoint2, std::size_t DimensionCount, bool Reverse>
302 struct equals<MultiPoint1, MultiPoint2, multi_point_tag, multi_point_tag, pointlike_tag, pointlike_tag, DimensionCount, Reverse>
303 : detail::equals::equals_by_relate<MultiPoint1, MultiPoint2>
306 template <typename MultiPoint, typename Point, std::size_t DimensionCount, bool Reverse>
307 struct equals<Point, MultiPoint, point_tag, multi_point_tag, pointlike_tag, pointlike_tag, DimensionCount, Reverse>
308 : detail::equals::equals_by_relate<Point, MultiPoint>
311 template <typename Box1, typename Box2, std::size_t DimensionCount, bool Reverse>
312 struct equals<Box1, Box2, box_tag, box_tag, areal_tag, areal_tag, DimensionCount, Reverse>
313 : detail::equals::box_box<0, DimensionCount>
317 template <typename Ring1, typename Ring2, bool Reverse>
318 struct equals<Ring1, Ring2, ring_tag, ring_tag, areal_tag, areal_tag, 2, Reverse>
319 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
323 template <typename Polygon1, typename Polygon2, bool Reverse>
324 struct equals<Polygon1, Polygon2, polygon_tag, polygon_tag, areal_tag, areal_tag, 2, Reverse>
325 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
329 template <typename Polygon, typename Ring, bool Reverse>
330 struct equals<Polygon, Ring, polygon_tag, ring_tag, areal_tag, areal_tag, 2, Reverse>
331 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
335 template <typename Ring, typename Box, bool Reverse>
336 struct equals<Ring, Box, ring_tag, box_tag, areal_tag, areal_tag, 2, Reverse>
337 : detail::equals::equals_by_collection<detail::equals::area_check>
341 template <typename Polygon, typename Box, bool Reverse>
342 struct equals<Polygon, Box, polygon_tag, box_tag, areal_tag, areal_tag, 2, Reverse>
343 : detail::equals::equals_by_collection<detail::equals::area_check>
346 template <typename Segment1, typename Segment2, std::size_t DimensionCount, bool Reverse>
347 struct equals<Segment1, Segment2, segment_tag, segment_tag, linear_tag, linear_tag, DimensionCount, Reverse>
348 : detail::equals::segment_segment
351 template <typename LineString1, typename LineString2, bool Reverse>
352 struct equals<LineString1, LineString2, linestring_tag, linestring_tag, linear_tag, linear_tag, 2, Reverse>
353 : detail::equals::equals_by_relate<LineString1, LineString2>
356 template <typename LineString, typename MultiLineString, bool Reverse>
357 struct equals<LineString, MultiLineString, linestring_tag, multi_linestring_tag, linear_tag, linear_tag, 2, Reverse>
358 : detail::equals::equals_by_relate<LineString, MultiLineString>
361 template <typename MultiLineString1, typename MultiLineString2, bool Reverse>
362 struct equals<MultiLineString1, MultiLineString2, multi_linestring_tag, multi_linestring_tag, linear_tag, linear_tag, 2, Reverse>
363 : detail::equals::equals_by_relate<MultiLineString1, MultiLineString2>
366 template <typename LineString, typename Segment, bool Reverse>
367 struct equals<LineString, Segment, linestring_tag, segment_tag, linear_tag, linear_tag, 2, Reverse>
368 : detail::equals::equals_by_relate<LineString, Segment>
371 template <typename MultiLineString, typename Segment, bool Reverse>
372 struct equals<MultiLineString, Segment, multi_linestring_tag, segment_tag, linear_tag, linear_tag, 2, Reverse>
373 : detail::equals::equals_by_relate<MultiLineString, Segment>
377 template <typename MultiPolygon1, typename MultiPolygon2, bool Reverse>
380 MultiPolygon1, MultiPolygon2,
381 multi_polygon_tag, multi_polygon_tag,
382 areal_tag, areal_tag,
386 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
390 template <typename Polygon, typename MultiPolygon, bool Reverse>
393 Polygon, MultiPolygon,
394 polygon_tag, multi_polygon_tag,
395 areal_tag, areal_tag,
399 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
402 template <typename MultiPolygon, typename Ring, bool Reverse>
406 multi_polygon_tag, ring_tag,
407 areal_tag, areal_tag,
411 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
415 // NOTE: degenerated linear geometries, e.g. segment or linestring containing
416 // 2 equal points, are considered to be invalid. Though theoretically
417 // degenerated segments and linestrings could be treated as points and
418 // multi-linestrings as multi-points.
419 // This reasoning could also be applied to boxes.
421 template <typename Geometry1, typename Geometry2, typename Tag1, typename Tag2, std::size_t DimensionCount>
422 struct equals<Geometry1, Geometry2, Tag1, Tag2, pointlike_tag, linear_tag, DimensionCount, false>
423 : detail::equals::equals_always_false
426 template <typename Geometry1, typename Geometry2, typename Tag1, typename Tag2, std::size_t DimensionCount>
427 struct equals<Geometry1, Geometry2, Tag1, Tag2, linear_tag, pointlike_tag, DimensionCount, false>
428 : detail::equals::equals_always_false
431 template <typename Geometry1, typename Geometry2, typename Tag1, typename Tag2, std::size_t DimensionCount>
432 struct equals<Geometry1, Geometry2, Tag1, Tag2, pointlike_tag, areal_tag, DimensionCount, false>
433 : detail::equals::equals_always_false
436 template <typename Geometry1, typename Geometry2, typename Tag1, typename Tag2, std::size_t DimensionCount>
437 struct equals<Geometry1, Geometry2, Tag1, Tag2, areal_tag, pointlike_tag, DimensionCount, false>
438 : detail::equals::equals_always_false
441 template <typename Geometry1, typename Geometry2, typename Tag1, typename Tag2, std::size_t DimensionCount>
442 struct equals<Geometry1, Geometry2, Tag1, Tag2, linear_tag, areal_tag, DimensionCount, false>
443 : detail::equals::equals_always_false
446 template <typename Geometry1, typename Geometry2, typename Tag1, typename Tag2, std::size_t DimensionCount>
447 struct equals<Geometry1, Geometry2, Tag1, Tag2, areal_tag, linear_tag, DimensionCount, false>
448 : detail::equals::equals_always_false
451 } // namespace dispatch
452 #endif // DOXYGEN_NO_DISPATCH
455 }} // namespace boost::geometry
458 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_EQUALS_IMPLEMENTATION_HPP