check_geometry.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Boost.Geometry
  2. // Unit Test
  3. // Copyright (c) 2017, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_GEOMETRY_TEST_SRS_CHECK_GEOMETRY_HPP
  9. #define BOOST_GEOMETRY_TEST_SRS_CHECK_GEOMETRY_HPP
  10. #include <geometry_test_common.hpp>
  11. #include <boost/geometry/core/access.hpp>
  12. #include <boost/geometry/core/coordinate_type.hpp>
  13. #include <boost/geometry/core/tag.hpp>
  14. #include <boost/geometry/core/tags.hpp>
  15. #include <boost/geometry/io/wkt/read.hpp>
  16. #include <boost/geometry/views/detail/indexed_point_view.hpp>
  17. #include <boost/range/begin.hpp>
  18. #include <boost/range/end.hpp>
  19. #include <boost/range/size.hpp>
  20. #include <boost/range/value_type.hpp>
  21. namespace test
  22. {
  23. struct check_point
  24. {
  25. template <typename Point, typename T>
  26. static void apply(Point const& point1, Point const& point2, T tol)
  27. {
  28. typename bg::coordinate_type<Point>::type
  29. x1 = bg::get<0>(point1),
  30. y1 = bg::get<1>(point1),
  31. x2 = bg::get<0>(point2),
  32. y2 = bg::get<1>(point2);
  33. BOOST_CHECK_CLOSE(x1, x2, tol);
  34. BOOST_CHECK_CLOSE(y1, y2, tol);
  35. }
  36. };
  37. template <typename Policy = check_point>
  38. struct check_range
  39. {
  40. template <typename Range, typename T>
  41. static void apply(Range const& range1, Range const& range2, T tol)
  42. {
  43. size_t range1_count = boost::size(range1);
  44. size_t range2_count = boost::size(range2);
  45. BOOST_CHECK_EQUAL(range1_count, range2_count);
  46. if (range1_count == range2_count)
  47. {
  48. apply(boost::begin(range1), boost::end(range1),
  49. boost::begin(range2), tol);
  50. }
  51. }
  52. template <typename It, typename T>
  53. static void apply(It first1, It last1, It first2, T tol)
  54. {
  55. for ( ; first1 != last1 ; ++first1, ++first2)
  56. Policy::apply(*first1, *first2, tol);
  57. }
  58. };
  59. template <typename Geometry, typename Tag = typename bg::tag<Geometry>::type>
  60. struct check_geometry_impl
  61. {};
  62. template <typename Point>
  63. struct check_geometry_impl<Point, bg::point_tag>
  64. : check_point
  65. {};
  66. template <typename Segment>
  67. struct check_geometry_impl<Segment, bg::segment_tag>
  68. {
  69. template <typename T>
  70. static void apply(Segment const& g1, Segment const& g2, T tol)
  71. {
  72. bg::detail::indexed_point_view<Segment const, 0> p1(g1);
  73. bg::detail::indexed_point_view<Segment const, 1> p2(g1);
  74. bg::detail::indexed_point_view<Segment const, 0> q1(g2);
  75. bg::detail::indexed_point_view<Segment const, 1> q2(g2);
  76. check_point::apply(p1, q1, tol);
  77. check_point::apply(p2, q2, tol);
  78. }
  79. };
  80. template <typename MultiPoint>
  81. struct check_geometry_impl<MultiPoint, bg::multi_point_tag>
  82. : check_range<>
  83. {};
  84. template <typename Linestring>
  85. struct check_geometry_impl<Linestring, bg::linestring_tag>
  86. : check_range<>
  87. {};
  88. template <typename MultiLinestring>
  89. struct check_geometry_impl<MultiLinestring, bg::multi_linestring_tag>
  90. : check_range< check_range<> >
  91. {};
  92. template <typename Ring>
  93. struct check_geometry_impl<Ring, bg::ring_tag>
  94. : check_range<>
  95. {};
  96. template <typename Polygon>
  97. struct check_geometry_impl<Polygon, bg::polygon_tag>
  98. {
  99. template <typename T>
  100. static void apply(Polygon const& g1, Polygon const& g2, T tol)
  101. {
  102. check_range<>::apply(bg::exterior_ring(g1), bg::exterior_ring(g2), tol);
  103. check_range< check_range<> >::apply(bg::interior_rings(g1), bg::interior_rings(g2), tol);
  104. }
  105. };
  106. template <typename MultiPolygon>
  107. struct check_geometry_impl<MultiPolygon, bg::multi_polygon_tag>
  108. : check_range
  109. <
  110. check_geometry_impl
  111. <
  112. typename boost::range_value<MultiPolygon>::type,
  113. bg::polygon_tag
  114. >
  115. >
  116. {};
  117. template <typename Geometry, typename T>
  118. inline void check_geometry(Geometry const& g1, Geometry const& g2, T tol)
  119. {
  120. check_geometry_impl<Geometry>::apply(g1, g2, tol);
  121. }
  122. template <typename Geometry, typename T>
  123. inline void check_geometry(Geometry const& g1, std::string const& wkt2, T tol)
  124. {
  125. Geometry g2;
  126. bg::read_wkt(wkt2, g2);
  127. check_geometry_impl<Geometry>::apply(g1, g2, tol);
  128. }
  129. } // namespace test
  130. #endif // BOOST_GEOMETRY_TEST_SRS_CHECK_GEOMETRY_HPP