equals.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  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)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_EQUALS_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_EQUALS_HPP
  12. #include <cstddef>
  13. #include <vector>
  14. #include <boost/range.hpp>
  15. #include <boost/geometry/core/access.hpp>
  16. #include <boost/geometry/core/coordinate_dimension.hpp>
  17. #include <boost/geometry/core/reverse_dispatch.hpp>
  18. #include <boost/geometry/geometries/concepts/check.hpp>
  19. #include <boost/geometry/algorithms/detail/disjoint.hpp>
  20. #include <boost/geometry/algorithms/detail/not.hpp>
  21. #include <boost/geometry/algorithms/not_implemented.hpp>
  22. // For trivial checks
  23. #include <boost/geometry/algorithms/area.hpp>
  24. #include <boost/geometry/algorithms/length.hpp>
  25. #include <boost/geometry/util/math.hpp>
  26. #include <boost/geometry/util/select_coordinate_type.hpp>
  27. #include <boost/geometry/util/select_most_precise.hpp>
  28. #include <boost/geometry/algorithms/detail/equals/collect_vectors.hpp>
  29. #include <boost/variant/static_visitor.hpp>
  30. #include <boost/variant/apply_visitor.hpp>
  31. namespace boost { namespace geometry
  32. {
  33. #ifndef DOXYGEN_NO_DETAIL
  34. namespace detail { namespace equals
  35. {
  36. template
  37. <
  38. std::size_t Dimension,
  39. std::size_t DimensionCount
  40. >
  41. struct box_box
  42. {
  43. template <typename Box1, typename Box2>
  44. static inline bool apply(Box1 const& box1, Box2 const& box2)
  45. {
  46. if (!geometry::math::equals(get<min_corner, Dimension>(box1), get<min_corner, Dimension>(box2))
  47. || !geometry::math::equals(get<max_corner, Dimension>(box1), get<max_corner, Dimension>(box2)))
  48. {
  49. return false;
  50. }
  51. return box_box<Dimension + 1, DimensionCount>::apply(box1, box2);
  52. }
  53. };
  54. template <std::size_t DimensionCount>
  55. struct box_box<DimensionCount, DimensionCount>
  56. {
  57. template <typename Box1, typename Box2>
  58. static inline bool apply(Box1 const& , Box2 const& )
  59. {
  60. return true;
  61. }
  62. };
  63. struct area_check
  64. {
  65. template <typename Geometry1, typename Geometry2>
  66. static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2)
  67. {
  68. return geometry::math::equals(
  69. geometry::area(geometry1),
  70. geometry::area(geometry2));
  71. }
  72. };
  73. struct length_check
  74. {
  75. template <typename Geometry1, typename Geometry2>
  76. static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2)
  77. {
  78. return geometry::math::equals(
  79. geometry::length(geometry1),
  80. geometry::length(geometry2));
  81. }
  82. };
  83. template <typename TrivialCheck>
  84. struct equals_by_collection
  85. {
  86. template <typename Geometry1, typename Geometry2>
  87. static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2)
  88. {
  89. if (! TrivialCheck::apply(geometry1, geometry2))
  90. {
  91. return false;
  92. }
  93. typedef typename geometry::select_most_precise
  94. <
  95. typename select_coordinate_type
  96. <
  97. Geometry1, Geometry2
  98. >::type,
  99. double
  100. >::type calculation_type;
  101. typedef std::vector<collected_vector<calculation_type> > v;
  102. v c1, c2;
  103. geometry::collect_vectors(c1, geometry1);
  104. geometry::collect_vectors(c2, geometry2);
  105. if (boost::size(c1) != boost::size(c2))
  106. {
  107. return false;
  108. }
  109. std::sort(c1.begin(), c1.end());
  110. std::sort(c2.begin(), c2.end());
  111. // Just check if these vectors are equal.
  112. return std::equal(c1.begin(), c1.end(), c2.begin());
  113. }
  114. };
  115. }} // namespace detail::equals
  116. #endif // DOXYGEN_NO_DETAIL
  117. #ifndef DOXYGEN_NO_DISPATCH
  118. namespace dispatch
  119. {
  120. template
  121. <
  122. typename Geometry1,
  123. typename Geometry2,
  124. typename Tag1 = typename tag<Geometry1>::type,
  125. typename Tag2 = typename tag<Geometry2>::type,
  126. std::size_t DimensionCount = dimension<Geometry1>::type::value,
  127. bool Reverse = reverse_dispatch<Geometry1, Geometry2>::type::value
  128. >
  129. struct equals: not_implemented<Tag1, Tag2>
  130. {};
  131. // If reversal is needed, perform it
  132. template
  133. <
  134. typename Geometry1, typename Geometry2,
  135. typename Tag1, typename Tag2,
  136. std::size_t DimensionCount
  137. >
  138. struct equals<Geometry1, Geometry2, Tag1, Tag2, DimensionCount, true>
  139. : equals<Geometry2, Geometry1, Tag2, Tag1, DimensionCount, false>
  140. {
  141. static inline bool apply(Geometry1 const& g1, Geometry2 const& g2)
  142. {
  143. return equals
  144. <
  145. Geometry2, Geometry1,
  146. Tag2, Tag1,
  147. DimensionCount,
  148. false
  149. >::apply(g2, g1);
  150. }
  151. };
  152. template <typename P1, typename P2, std::size_t DimensionCount, bool Reverse>
  153. struct equals<P1, P2, point_tag, point_tag, DimensionCount, Reverse>
  154. : geometry::detail::not_
  155. <
  156. P1,
  157. P2,
  158. detail::disjoint::point_point<P1, P2, 0, DimensionCount>
  159. >
  160. {};
  161. template <typename Box1, typename Box2, std::size_t DimensionCount, bool Reverse>
  162. struct equals<Box1, Box2, box_tag, box_tag, DimensionCount, Reverse>
  163. : detail::equals::box_box<0, DimensionCount>
  164. {};
  165. template <typename Ring1, typename Ring2, bool Reverse>
  166. struct equals<Ring1, Ring2, ring_tag, ring_tag, 2, Reverse>
  167. : detail::equals::equals_by_collection<detail::equals::area_check>
  168. {};
  169. template <typename Polygon1, typename Polygon2, bool Reverse>
  170. struct equals<Polygon1, Polygon2, polygon_tag, polygon_tag, 2, Reverse>
  171. : detail::equals::equals_by_collection<detail::equals::area_check>
  172. {};
  173. template <typename LineString1, typename LineString2, bool Reverse>
  174. struct equals<LineString1, LineString2, linestring_tag, linestring_tag, 2, Reverse>
  175. : detail::equals::equals_by_collection<detail::equals::length_check>
  176. {};
  177. template <typename Polygon, typename Ring, bool Reverse>
  178. struct equals<Polygon, Ring, polygon_tag, ring_tag, 2, Reverse>
  179. : detail::equals::equals_by_collection<detail::equals::area_check>
  180. {};
  181. template <typename Ring, typename Box, bool Reverse>
  182. struct equals<Ring, Box, ring_tag, box_tag, 2, Reverse>
  183. : detail::equals::equals_by_collection<detail::equals::area_check>
  184. {};
  185. template <typename Polygon, typename Box, bool Reverse>
  186. struct equals<Polygon, Box, polygon_tag, box_tag, 2, Reverse>
  187. : detail::equals::equals_by_collection<detail::equals::area_check>
  188. {};
  189. template <typename Geometry1, typename Geometry2>
  190. struct devarianted_equals
  191. {
  192. static inline bool apply(Geometry1 const& geometry1,
  193. Geometry2 const& geometry2)
  194. {
  195. concept::check_concepts_and_equal_dimensions
  196. <
  197. Geometry1 const,
  198. Geometry2 const
  199. >();
  200. return equals<Geometry1, Geometry2>::apply(geometry1, geometry2);
  201. }
  202. };
  203. template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
  204. struct devarianted_equals<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
  205. {
  206. struct visitor: static_visitor<bool>
  207. {
  208. Geometry2 const& m_geometry2;
  209. visitor(Geometry2 const& geometry2)
  210. : m_geometry2(geometry2)
  211. {}
  212. template <typename Geometry1>
  213. inline bool operator()(Geometry1 const& geometry1) const
  214. {
  215. return devarianted_equals<Geometry1, Geometry2>
  216. ::apply(geometry1, m_geometry2);
  217. }
  218. };
  219. static inline bool apply(
  220. boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
  221. Geometry2 const& geometry2
  222. )
  223. {
  224. return apply_visitor(visitor(geometry2), geometry1);
  225. }
  226. };
  227. template <typename Geometry1, BOOST_VARIANT_ENUM_PARAMS(typename T)>
  228. struct devarianted_equals<Geometry1, boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  229. {
  230. struct visitor: static_visitor<bool>
  231. {
  232. Geometry1 const& m_geometry1;
  233. visitor(Geometry1 const& geometry1)
  234. : m_geometry1(geometry1)
  235. {}
  236. template <typename Geometry2>
  237. inline bool operator()(Geometry2 const& geometry2) const
  238. {
  239. return devarianted_equals<Geometry1, Geometry2>
  240. ::apply(m_geometry1, geometry2);
  241. }
  242. };
  243. static inline bool apply(
  244. Geometry1 const& geometry1,
  245. boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry2
  246. )
  247. {
  248. return apply_visitor(visitor(geometry1), geometry2);
  249. }
  250. };
  251. template <
  252. BOOST_VARIANT_ENUM_PARAMS(typename T1),
  253. BOOST_VARIANT_ENUM_PARAMS(typename T2)
  254. >
  255. struct devarianted_equals<
  256. boost::variant<BOOST_VARIANT_ENUM_PARAMS(T1)>,
  257. boost::variant<BOOST_VARIANT_ENUM_PARAMS(T2)>
  258. >
  259. {
  260. struct visitor: static_visitor<bool>
  261. {
  262. template <typename Geometry1, typename Geometry2>
  263. inline bool operator()(Geometry1 const& geometry1,
  264. Geometry2 const& geometry2) const
  265. {
  266. return devarianted_equals<Geometry1, Geometry2>
  267. ::apply(geometry1, geometry2);
  268. }
  269. };
  270. static inline bool apply(
  271. boost::variant<BOOST_VARIANT_ENUM_PARAMS(T1)> const& geometry1,
  272. boost::variant<BOOST_VARIANT_ENUM_PARAMS(T2)> const& geometry2
  273. )
  274. {
  275. return apply_visitor(visitor(), geometry1, geometry2);
  276. }
  277. };
  278. } // namespace dispatch
  279. #endif // DOXYGEN_NO_DISPATCH
  280. /*!
  281. \brief \brief_check{are spatially equal}
  282. \details \details_check12{equals, is spatially equal}. Spatially equal means
  283. that the same point set is included. A box can therefore be spatially equal
  284. to a ring or a polygon, or a linestring can be spatially equal to a
  285. multi-linestring or a segment. This only works theoretically, not all
  286. combinations are implemented yet.
  287. \ingroup equals
  288. \tparam Geometry1 \tparam_geometry
  289. \tparam Geometry2 \tparam_geometry
  290. \param geometry1 \param_geometry
  291. \param geometry2 \param_geometry
  292. \return \return_check2{are spatially equal}
  293. \qbk{[include reference/algorithms/equals.qbk]}
  294. */
  295. template <typename Geometry1, typename Geometry2>
  296. inline bool equals(Geometry1 const& geometry1, Geometry2 const& geometry2)
  297. {
  298. return dispatch::devarianted_equals<Geometry1, Geometry2>
  299. ::apply(geometry1, geometry2);
  300. }
  301. }} // namespace boost::geometry
  302. #endif // BOOST_GEOMETRY_ALGORITHMS_EQUALS_HPP