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-2021.
9 // Modifications copyright (c) 2014-2021 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
26 #include <type_traits>
29 #include <boost/range/size.hpp>
31 #include <boost/geometry/core/access.hpp>
32 #include <boost/geometry/core/tags.hpp>
34 #include <boost/geometry/algorithms/area.hpp>
35 #include <boost/geometry/algorithms/length.hpp>
36 #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
37 #include <boost/geometry/algorithms/detail/equals/collect_vectors.hpp>
38 #include <boost/geometry/algorithms/detail/equals/interface.hpp>
39 #include <boost/geometry/algorithms/detail/relate/relate_impl.hpp>
40 #include <boost/geometry/algorithms/relate.hpp>
42 #include <boost/geometry/strategies/relate/cartesian.hpp>
43 #include <boost/geometry/strategies/relate/geographic.hpp>
44 #include <boost/geometry/strategies/relate/spherical.hpp>
46 #include <boost/geometry/util/math.hpp>
47 #include <boost/geometry/util/select_coordinate_type.hpp>
48 #include <boost/geometry/util/select_most_precise.hpp>
50 #include <boost/geometry/views/detail/indexed_point_view.hpp>
53 namespace boost { namespace geometry
56 #ifndef DOXYGEN_NO_DETAIL
57 namespace detail { namespace equals
63 std::size_t Dimension,
64 std::size_t DimensionCount
68 template <typename Point1, typename Point2, typename Strategy>
69 static inline bool apply(Point1 const& point1, Point2 const& point2,
70 Strategy const& strategy)
72 typedef decltype(strategy.relate(point1, point2)) strategy_type;
73 return strategy_type::apply(point1, point2);
80 std::size_t Dimension,
81 std::size_t DimensionCount
85 template <typename Box1, typename Box2, typename Strategy>
86 static inline bool apply(Box1 const& box1, Box2 const& box2, Strategy const& strategy)
88 if (!geometry::math::equals(get<min_corner, Dimension>(box1), get<min_corner, Dimension>(box2))
89 || !geometry::math::equals(get<max_corner, Dimension>(box1), get<max_corner, Dimension>(box2)))
93 return box_box<Dimension + 1, DimensionCount>::apply(box1, box2, strategy);
97 template <std::size_t DimensionCount>
98 struct box_box<DimensionCount, DimensionCount>
100 template <typename Box1, typename Box2, typename Strategy>
101 static inline bool apply(Box1 const& , Box2 const& , Strategy const& )
108 struct segment_segment
110 template <typename Segment1, typename Segment2, typename Strategy>
111 static inline bool apply(Segment1 const& segment1, Segment2 const& segment2,
112 Strategy const& strategy)
114 return equals::equals_point_point(
115 indexed_point_view<Segment1 const, 0>(segment1),
116 indexed_point_view<Segment2 const, 0>(segment2),
118 ? equals::equals_point_point(
119 indexed_point_view<Segment1 const, 1>(segment1),
120 indexed_point_view<Segment2 const, 1>(segment2),
122 : ( equals::equals_point_point(
123 indexed_point_view<Segment1 const, 0>(segment1),
124 indexed_point_view<Segment2 const, 1>(segment2),
126 && equals::equals_point_point(
127 indexed_point_view<Segment1 const, 1>(segment1),
128 indexed_point_view<Segment2 const, 0>(segment2),
137 template <typename Geometry1, typename Geometry2, typename Strategy>
138 static inline bool apply(Geometry1 const& geometry1,
139 Geometry2 const& geometry2,
140 Strategy const& strategy)
142 return geometry::math::equals(geometry::area(geometry1, strategy),
143 geometry::area(geometry2, strategy));
151 template <typename Geometry1, typename Geometry2, typename Strategy>
152 static inline bool apply(Geometry1 const& geometry1,
153 Geometry2 const& geometry2,
154 Strategy const& strategy)
156 return geometry::math::equals(geometry::length(geometry1, strategy),
157 geometry::length(geometry2, strategy));
163 // Small helper structure do decide to use collect_vectors, or not
164 template <typename Strategy, typename CsTag>
165 struct use_collect_vectors
167 static constexpr bool value = false;
170 template <typename Strategy>
171 struct use_collect_vectors<Strategy, cartesian_tag>
173 static constexpr bool value = true;
175 template <typename T, typename Point>
176 using type = collected_vector_cartesian<T>;
179 template <typename CV>
180 struct use_collect_vectors<strategy::side::spherical_side_formula<CV>, spherical_tag>
182 static constexpr bool value = true;
184 template <typename T, typename Point>
185 using type = collected_vector_spherical<T, Point>;
189 template <typename TrivialCheck>
190 struct equals_by_collection
192 template <typename Geometry1, typename Geometry2, typename Strategy>
193 static inline bool apply(Geometry1 const& geometry1,
194 Geometry2 const& geometry2,
195 Strategy const& strategy)
197 if (! TrivialCheck::apply(geometry1, geometry2, strategy))
202 using calculation_type = typename geometry::select_most_precise
204 typename select_coordinate_type
211 using collected_vector_type = typename use_collect_vectors
213 decltype(std::declval<Strategy>().side()),
214 typename Strategy::cs_tag
218 typename geometry::point_type<Geometry1>::type
221 std::vector<collected_vector_type> c1, c2;
223 geometry::collect_vectors(c1, geometry1);
224 geometry::collect_vectors(c2, geometry2);
226 if (boost::size(c1) != boost::size(c2))
231 std::sort(c1.begin(), c1.end());
232 std::sort(c2.begin(), c2.end());
234 // Check if these vectors are equal.
235 return std::equal(c1.begin(), c1.end(), c2.begin());
239 template<typename Geometry1, typename Geometry2>
240 struct equals_by_relate
241 : detail::relate::relate_impl
243 detail::de9im::static_mask_equals_type,
249 // Use either collect_vectors or relate
250 // NOTE: the result could be conceptually different for invalid
251 // geometries in different coordinate systems because collect_vectors
252 // and relate treat invalid geometries differently.
253 template<typename TrivialCheck>
254 struct equals_by_collection_or_relate
256 template <typename Strategy>
257 using use_vectors = use_collect_vectors
259 decltype(std::declval<Strategy>().side()),
260 typename Strategy::cs_tag
265 typename Geometry1, typename Geometry2, typename Strategy,
266 std::enable_if_t<use_vectors<Strategy>::value, int> = 0
268 static inline bool apply(Geometry1 const& geometry1,
269 Geometry2 const& geometry2,
270 Strategy const& strategy)
272 return equals_by_collection<TrivialCheck>::apply(geometry1, geometry2, strategy);
277 typename Geometry1, typename Geometry2, typename Strategy,
278 std::enable_if_t<! use_vectors<Strategy>::value, int> = 0
280 static inline bool apply(Geometry1 const& geometry1,
281 Geometry2 const& geometry2,
282 Strategy const& strategy)
284 return equals_by_relate<Geometry1, Geometry2>::apply(geometry1, geometry2, strategy);
288 struct equals_always_false
290 template <typename Geometry1, typename Geometry2, typename Strategy>
291 static inline bool apply(Geometry1 const& , Geometry2 const& , Strategy const& )
298 }} // namespace detail::equals
299 #endif // DOXYGEN_NO_DETAIL
302 #ifndef DOXYGEN_NO_DISPATCH
306 template <typename P1, typename P2, std::size_t DimensionCount, bool Reverse>
307 struct equals<P1, P2, point_tag, point_tag, pointlike_tag, pointlike_tag, DimensionCount, Reverse>
308 : detail::equals::point_point<0, DimensionCount>
311 template <typename MultiPoint1, typename MultiPoint2, std::size_t DimensionCount, bool Reverse>
312 struct equals<MultiPoint1, MultiPoint2, multi_point_tag, multi_point_tag, pointlike_tag, pointlike_tag, DimensionCount, Reverse>
313 : detail::equals::equals_by_relate<MultiPoint1, MultiPoint2>
316 template <typename MultiPoint, typename Point, std::size_t DimensionCount, bool Reverse>
317 struct equals<Point, MultiPoint, point_tag, multi_point_tag, pointlike_tag, pointlike_tag, DimensionCount, Reverse>
318 : detail::equals::equals_by_relate<Point, MultiPoint>
321 template <typename Box1, typename Box2, std::size_t DimensionCount, bool Reverse>
322 struct equals<Box1, Box2, box_tag, box_tag, areal_tag, areal_tag, DimensionCount, Reverse>
323 : detail::equals::box_box<0, DimensionCount>
327 template <typename Ring1, typename Ring2, bool Reverse>
328 struct equals<Ring1, Ring2, ring_tag, ring_tag, areal_tag, areal_tag, 2, Reverse>
329 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
333 template <typename Polygon1, typename Polygon2, bool Reverse>
334 struct equals<Polygon1, Polygon2, polygon_tag, polygon_tag, areal_tag, areal_tag, 2, Reverse>
335 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
339 template <typename Polygon, typename Ring, bool Reverse>
340 struct equals<Polygon, Ring, polygon_tag, ring_tag, areal_tag, areal_tag, 2, Reverse>
341 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
345 template <typename Ring, typename Box, bool Reverse>
346 struct equals<Ring, Box, ring_tag, box_tag, areal_tag, areal_tag, 2, Reverse>
347 : detail::equals::equals_by_collection<detail::equals::area_check>
351 template <typename Polygon, typename Box, bool Reverse>
352 struct equals<Polygon, Box, polygon_tag, box_tag, areal_tag, areal_tag, 2, Reverse>
353 : detail::equals::equals_by_collection<detail::equals::area_check>
356 template <typename Segment1, typename Segment2, std::size_t DimensionCount, bool Reverse>
357 struct equals<Segment1, Segment2, segment_tag, segment_tag, linear_tag, linear_tag, DimensionCount, Reverse>
358 : detail::equals::segment_segment
361 template <typename LineString1, typename LineString2, bool Reverse>
362 struct equals<LineString1, LineString2, linestring_tag, linestring_tag, linear_tag, linear_tag, 2, Reverse>
363 : detail::equals::equals_by_relate<LineString1, LineString2>
366 template <typename LineString, typename MultiLineString, bool Reverse>
367 struct equals<LineString, MultiLineString, linestring_tag, multi_linestring_tag, linear_tag, linear_tag, 2, Reverse>
368 : detail::equals::equals_by_relate<LineString, MultiLineString>
371 template <typename MultiLineString1, typename MultiLineString2, bool Reverse>
372 struct equals<MultiLineString1, MultiLineString2, multi_linestring_tag, multi_linestring_tag, linear_tag, linear_tag, 2, Reverse>
373 : detail::equals::equals_by_relate<MultiLineString1, MultiLineString2>
376 template <typename LineString, typename Segment, bool Reverse>
377 struct equals<LineString, Segment, linestring_tag, segment_tag, linear_tag, linear_tag, 2, Reverse>
378 : detail::equals::equals_by_relate<LineString, Segment>
381 template <typename MultiLineString, typename Segment, bool Reverse>
382 struct equals<MultiLineString, Segment, multi_linestring_tag, segment_tag, linear_tag, linear_tag, 2, Reverse>
383 : detail::equals::equals_by_relate<MultiLineString, Segment>
387 template <typename MultiPolygon1, typename MultiPolygon2, bool Reverse>
390 MultiPolygon1, MultiPolygon2,
391 multi_polygon_tag, multi_polygon_tag,
392 areal_tag, areal_tag,
396 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
400 template <typename Polygon, typename MultiPolygon, bool Reverse>
403 Polygon, MultiPolygon,
404 polygon_tag, multi_polygon_tag,
405 areal_tag, areal_tag,
409 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
412 template <typename MultiPolygon, typename Ring, bool Reverse>
416 multi_polygon_tag, ring_tag,
417 areal_tag, areal_tag,
421 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
425 // NOTE: degenerated linear geometries, e.g. segment or linestring containing
426 // 2 equal points, are considered to be invalid. Though theoretically
427 // degenerated segments and linestrings could be treated as points and
428 // multi-linestrings as multi-points.
429 // This reasoning could also be applied to boxes.
431 template <typename Geometry1, typename Geometry2, typename Tag1, typename Tag2, std::size_t DimensionCount>
432 struct equals<Geometry1, Geometry2, Tag1, Tag2, pointlike_tag, linear_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, linear_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, pointlike_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, pointlike_tag, DimensionCount, false>
448 : detail::equals::equals_always_false
451 template <typename Geometry1, typename Geometry2, typename Tag1, typename Tag2, std::size_t DimensionCount>
452 struct equals<Geometry1, Geometry2, Tag1, Tag2, linear_tag, areal_tag, DimensionCount, false>
453 : detail::equals::equals_always_false
456 template <typename Geometry1, typename Geometry2, typename Tag1, typename Tag2, std::size_t DimensionCount>
457 struct equals<Geometry1, Geometry2, Tag1, Tag2, areal_tag, linear_tag, DimensionCount, false>
458 : detail::equals::equals_always_false
461 } // namespace dispatch
462 #endif // DOXYGEN_NO_DISPATCH
465 }} // namespace boost::geometry
468 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_EQUALS_IMPLEMENTATION_HPP