path_intersection.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Boost.Geometry Index
  2. // Unit Test
  3. // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <geometry_index_test_common.hpp>
  8. #include <boost/geometry/index/detail/algorithms/path_intersection.hpp>
  9. #include <boost/geometry/geometries/point_xy.hpp>
  10. #include <boost/geometry/geometries/point.hpp>
  11. #include <boost/geometry/geometries/box.hpp>
  12. #include <boost/geometry/geometries/linestring.hpp>
  13. #include <boost/geometry/geometries/segment.hpp>
  14. //#include <boost/geometry/io/wkt/read.hpp>
  15. template <typename Box, typename Linestring>
  16. void test_path_intersection(Box const& box, Linestring const& path,
  17. bool expected_result,
  18. typename bg::default_length_result<Linestring>::type expected_dist)
  19. {
  20. typename bgi::detail::default_path_intersection_distance_type<Box, Linestring>::type dist;
  21. bool value = bgi::detail::path_intersection(box, path, dist);
  22. BOOST_CHECK(value == expected_result);
  23. if ( value && expected_result )
  24. BOOST_CHECK_CLOSE(dist, expected_dist, 0.0001);
  25. if ( ::boost::size(path) == 2 )
  26. {
  27. typedef typename ::boost::range_value<Linestring>::type P;
  28. typedef bg::model::segment<P> Seg;
  29. typename bgi::detail::default_path_intersection_distance_type<Box, Seg>::type dist;
  30. Seg seg(*::boost::begin(path), *(::boost::begin(path)+1));
  31. bool value = bgi::detail::path_intersection(box, seg, dist);
  32. BOOST_CHECK(value == expected_result);
  33. if ( value && expected_result )
  34. BOOST_CHECK_CLOSE(dist, expected_dist, 0.0001);
  35. }
  36. }
  37. template <typename Box, typename Linestring>
  38. void test_geometry(std::string const& wkt_g, std::string const& wkt_path,
  39. bool expected_result,
  40. typename bg::default_length_result<Linestring>::type expected_dist)
  41. {
  42. Box box;
  43. bg::read_wkt(wkt_g, box);
  44. Linestring path;
  45. bg::read_wkt(wkt_path, path);
  46. test_path_intersection(box, path, expected_result, expected_dist);
  47. }
  48. void test_large_integers()
  49. {
  50. typedef bg::model::point<int, 2, bg::cs::cartesian> int_point_type;
  51. typedef bg::model::point<double, 2, bg::cs::cartesian> double_point_type;
  52. bg::model::box<int_point_type> int_box;
  53. bg::model::box<double_point_type> double_box;
  54. typedef bg::model::linestring<int_point_type> IP;
  55. IP int_path;
  56. typedef bg::model::linestring<double_point_type> DP;
  57. DP double_path;
  58. std::string const str_box = "POLYGON((1536119 192000, 1872000 528000))";
  59. std::string const str_path = "LINESTRING(1535000 191000, 1873000 191000, 1873000 300000, 1536119 300000)";
  60. bg::read_wkt(str_box, int_box);
  61. bg::read_wkt(str_box, double_box);
  62. bg::read_wkt(str_path, int_path);
  63. bg::read_wkt(str_path, double_path);
  64. bg::default_length_result<IP>::type int_value;
  65. bool int_result = bgi::detail::path_intersection(int_box, int_path, int_value);
  66. bg::default_length_result<DP>::type double_value;
  67. bool double_result = bgi::detail::path_intersection(double_box, double_path, double_value);
  68. BOOST_CHECK(int_result == double_result);
  69. if ( int_result && double_result )
  70. BOOST_CHECK_CLOSE(int_value, double_value, 0.0001);
  71. }
  72. int test_main(int, char* [])
  73. {
  74. typedef bg::model::point<int, 2, bg::cs::cartesian> P2ic;
  75. typedef bg::model::point<float, 2, bg::cs::cartesian> P2fc;
  76. typedef bg::model::point<double, 2, bg::cs::cartesian> P2dc;
  77. typedef bg::model::point<int, 3, bg::cs::cartesian> P3ic;
  78. typedef bg::model::point<float, 3, bg::cs::cartesian> P3fc;
  79. typedef bg::model::point<double, 3, bg::cs::cartesian> P3dc;
  80. typedef bg::model::linestring<P2ic> L2ic;
  81. typedef bg::model::linestring<P2fc> L2fc;
  82. typedef bg::model::linestring<P2dc> L2dc;
  83. typedef bg::model::linestring<P3ic> L3ic;
  84. typedef bg::model::linestring<P3fc> L3fc;
  85. typedef bg::model::linestring<P3dc> L3dc;
  86. // IMPORTANT! For 2-point linestrings comparable distance optimization is enabled!
  87. test_geometry<bg::model::box<P2ic>, L2ic>("POLYGON((0 1,2 4))", "LINESTRING(0 0, 2 5)", true, 1.0f/5);
  88. test_geometry<bg::model::box<P2fc>, L2fc>("POLYGON((0 1,2 4))", "LINESTRING(0 0, 2 5)", true, 1.0f/5);
  89. test_geometry<bg::model::box<P2dc>, L2dc>("POLYGON((0 1,2 4))", "LINESTRING(0 0, 2 5)", true, 1.0/5);
  90. test_geometry<bg::model::box<P3ic>, L3ic>("POLYGON((0 1 2,2 4 6))", "LINESTRING(0 0 0, 2 5 7)", true, 2.0f/7);
  91. test_geometry<bg::model::box<P3fc>, L3fc>("POLYGON((0 1 2,2 4 6))", "LINESTRING(0 0 0, 2 5 7)", true, 2.0f/7);
  92. test_geometry<bg::model::box<P3dc>, L3dc>("POLYGON((0 1 2,2 4 6))", "LINESTRING(0 0 0, 2 5 7)", true, 2.0/7);
  93. test_geometry<bg::model::box<P2fc>, L2fc>("POLYGON((0 1,2 4))", "LINESTRING(0 0, 1 0, 1 5)", true, 2);
  94. test_geometry<bg::model::box<P2fc>, L2fc>("POLYGON((0 1,2 4))", "LINESTRING(0 0, 3 0, 3 2, 0 2)", true, 6);
  95. test_geometry<bg::model::box<P2fc>, L2fc>("POLYGON((0 1,2 4))", "LINESTRING(1 2, 3 3, 0 3)", true, 0);
  96. #ifdef HAVE_TTMATH
  97. typedef bg::model::point<ttmath_big, 2, bg::cs::cartesian> P2ttmc;
  98. typedef bg::model::point<ttmath_big, 3, bg::cs::cartesian> P3ttmc;
  99. typedef bg::model::linestring<P2ttmc> L2ttmc;
  100. typedef bg::model::linestring<P3ttmc> L3ttmc;
  101. test_geometry<bg::model::box<P2ttmc>, L2ttmc>("POLYGON((0 1,2 4))", "LINESTRING(0 0, 2 5)", true, 1.0/5);
  102. test_geometry<bg::model::box<P3ttmc>, L3ttmc>("POLYGON((0 1 2,2 4 6))", "LINESTRING(0 0 0, 2 5 7)", true, 2.0/7);
  103. #endif
  104. test_large_integers();
  105. return 0;
  106. }