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.
9 // Modifications copyright (c) 2014-2017 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& strategy)
69 return ! detail::disjoint::point_point
72 Dimension, DimensionCount
73 >::apply(point1, point2, strategy);
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, Strategy const& )
113 return equals::equals_point_point(
114 indexed_point_view<Segment1 const, 0>(segment1),
115 indexed_point_view<Segment2 const, 0>(segment2) )
116 ? equals::equals_point_point(
117 indexed_point_view<Segment1 const, 1>(segment1),
118 indexed_point_view<Segment2 const, 1>(segment2) )
119 : ( equals::equals_point_point(
120 indexed_point_view<Segment1 const, 0>(segment1),
121 indexed_point_view<Segment2 const, 1>(segment2) )
122 && equals::equals_point_point(
123 indexed_point_view<Segment1 const, 1>(segment1),
124 indexed_point_view<Segment2 const, 0>(segment2) )
132 template <typename Geometry1, typename Geometry2, typename Strategy>
133 static inline bool apply(Geometry1 const& geometry1,
134 Geometry2 const& geometry2,
135 Strategy const& strategy)
137 return geometry::math::equals(
138 geometry::area(geometry1,
139 strategy.template get_area_strategy<Geometry1>()),
140 geometry::area(geometry2,
141 strategy.template get_area_strategy<Geometry2>()));
148 template <typename Geometry1, typename Geometry2, typename Strategy>
149 static inline bool apply(Geometry1 const& geometry1,
150 Geometry2 const& geometry2,
151 Strategy const& strategy)
153 return geometry::math::equals(
154 geometry::length(geometry1,
155 strategy.template get_distance_strategy<Geometry1>()),
156 geometry::length(geometry2,
157 strategy.template get_distance_strategy<Geometry2>()));
162 template <typename Geometry1, typename Geometry2, typename IntersectionStrategy>
163 struct collected_vector
165 typedef typename geometry::select_most_precise
167 typename select_coordinate_type
172 >::type calculation_type;
174 typedef geometry::collected_vector
178 typename IntersectionStrategy::side_strategy_type
182 template <typename TrivialCheck>
183 struct equals_by_collection
185 template <typename Geometry1, typename Geometry2, typename Strategy>
186 static inline bool apply(Geometry1 const& geometry1,
187 Geometry2 const& geometry2,
188 Strategy const& strategy)
190 if (! TrivialCheck::apply(geometry1, geometry2, strategy))
195 typedef typename collected_vector
197 Geometry1, Geometry2, Strategy
198 >::type collected_vector_type;
200 std::vector<collected_vector_type> c1, c2;
202 geometry::collect_vectors(c1, geometry1);
203 geometry::collect_vectors(c2, geometry2);
205 if (boost::size(c1) != boost::size(c2))
210 std::sort(c1.begin(), c1.end());
211 std::sort(c2.begin(), c2.end());
213 // Just check if these vectors are equal.
214 return std::equal(c1.begin(), c1.end(), c2.begin());
218 template<typename Geometry1, typename Geometry2>
219 struct equals_by_relate
220 : detail::relate::relate_impl
222 detail::de9im::static_mask_equals_type,
228 // If collect_vectors which is a SideStrategy-dispatched optimization
229 // is implemented in a way consistent with the Intersection/Side Strategy
230 // then collect_vectors is used, otherwise relate is used.
231 // NOTE: the result could be conceptually different for invalid
232 // geometries in different coordinate systems because collect_vectors
233 // and relate treat invalid geometries differently.
234 template<typename TrivialCheck>
235 struct equals_by_collection_or_relate
237 template <typename Geometry1, typename Geometry2, typename Strategy>
238 static inline bool apply(Geometry1 const& geometry1,
239 Geometry2 const& geometry2,
240 Strategy const& strategy)
242 typedef typename boost::is_base_of
244 nyi::not_implemented_tag,
245 typename collected_vector
247 Geometry1, Geometry2, Strategy
249 >::type enable_relate_type;
251 return apply(geometry1, geometry2, strategy, enable_relate_type());
255 template <typename Geometry1, typename Geometry2, typename Strategy>
256 static inline bool apply(Geometry1 const& geometry1,
257 Geometry2 const& geometry2,
258 Strategy const& strategy,
259 boost::false_type /*enable_relate*/)
261 return equals_by_collection<TrivialCheck>::apply(geometry1, geometry2, strategy);
264 template <typename Geometry1, typename Geometry2, typename Strategy>
265 static inline bool apply(Geometry1 const& geometry1,
266 Geometry2 const& geometry2,
267 Strategy const& strategy,
268 boost::true_type /*enable_relate*/)
270 return equals_by_relate<Geometry1, Geometry2>::apply(geometry1, geometry2, strategy);
275 }} // namespace detail::equals
276 #endif // DOXYGEN_NO_DETAIL
279 #ifndef DOXYGEN_NO_DISPATCH
283 template <typename P1, typename P2, std::size_t DimensionCount, bool Reverse>
284 struct equals<P1, P2, point_tag, point_tag, DimensionCount, Reverse>
285 : detail::equals::point_point<0, DimensionCount>
288 template <typename MultiPoint1, typename MultiPoint2, std::size_t DimensionCount, bool Reverse>
289 struct equals<MultiPoint1, MultiPoint2, multi_point_tag, multi_point_tag, DimensionCount, Reverse>
290 : detail::equals::equals_by_relate<MultiPoint1, MultiPoint2>
293 template <typename MultiPoint, typename Point, std::size_t DimensionCount, bool Reverse>
294 struct equals<Point, MultiPoint, point_tag, multi_point_tag, DimensionCount, Reverse>
295 : detail::equals::equals_by_relate<Point, MultiPoint>
298 template <typename Box1, typename Box2, std::size_t DimensionCount, bool Reverse>
299 struct equals<Box1, Box2, box_tag, box_tag, DimensionCount, Reverse>
300 : detail::equals::box_box<0, DimensionCount>
304 template <typename Ring1, typename Ring2, bool Reverse>
305 struct equals<Ring1, Ring2, ring_tag, ring_tag, 2, Reverse>
306 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
310 template <typename Polygon1, typename Polygon2, bool Reverse>
311 struct equals<Polygon1, Polygon2, polygon_tag, polygon_tag, 2, Reverse>
312 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
316 template <typename Polygon, typename Ring, bool Reverse>
317 struct equals<Polygon, Ring, polygon_tag, ring_tag, 2, Reverse>
318 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
322 template <typename Ring, typename Box, bool Reverse>
323 struct equals<Ring, Box, ring_tag, box_tag, 2, Reverse>
324 : detail::equals::equals_by_collection<detail::equals::area_check>
328 template <typename Polygon, typename Box, bool Reverse>
329 struct equals<Polygon, Box, polygon_tag, box_tag, 2, Reverse>
330 : detail::equals::equals_by_collection<detail::equals::area_check>
333 template <typename Segment1, typename Segment2, std::size_t DimensionCount, bool Reverse>
334 struct equals<Segment1, Segment2, segment_tag, segment_tag, DimensionCount, Reverse>
335 : detail::equals::segment_segment
338 template <typename LineString1, typename LineString2, bool Reverse>
339 struct equals<LineString1, LineString2, linestring_tag, linestring_tag, 2, Reverse>
340 : detail::equals::equals_by_relate<LineString1, LineString2>
343 template <typename LineString, typename MultiLineString, bool Reverse>
344 struct equals<LineString, MultiLineString, linestring_tag, multi_linestring_tag, 2, Reverse>
345 : detail::equals::equals_by_relate<LineString, MultiLineString>
348 template <typename MultiLineString1, typename MultiLineString2, bool Reverse>
349 struct equals<MultiLineString1, MultiLineString2, multi_linestring_tag, multi_linestring_tag, 2, Reverse>
350 : detail::equals::equals_by_relate<MultiLineString1, MultiLineString2>
354 template <typename MultiPolygon1, typename MultiPolygon2, bool Reverse>
357 MultiPolygon1, MultiPolygon2,
358 multi_polygon_tag, multi_polygon_tag,
362 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
366 template <typename Polygon, typename MultiPolygon, bool Reverse>
369 Polygon, MultiPolygon,
370 polygon_tag, multi_polygon_tag,
374 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
377 template <typename MultiPolygon, typename Ring, bool Reverse>
381 multi_polygon_tag, ring_tag,
385 : detail::equals::equals_by_collection_or_relate<detail::equals::area_check>
389 } // namespace dispatch
390 #endif // DOXYGEN_NO_DISPATCH
393 }} // namespace boost::geometry
396 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_EQUALS_IMPLEMENTATION_HPP