segment_intersection.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/segment_intersection.hpp>
  9. //#include <boost/geometry/io/wkt/read.hpp>
  10. template <typename Box, typename Point, typename RelativeDistance>
  11. void test_segment_intersection(Box const& box, Point const& p0, Point const& p1,
  12. bool expected_result,
  13. RelativeDistance expected_rel_dist)
  14. {
  15. RelativeDistance rel_dist;
  16. bool value = bgi::detail::segment_intersection(box, p0, p1, rel_dist);
  17. BOOST_CHECK(value == expected_result);
  18. if ( value && expected_result )
  19. BOOST_CHECK_CLOSE(rel_dist, expected_rel_dist, 0.0001);
  20. }
  21. template <typename Box, typename Point, typename RelativeDistance>
  22. void test_geometry(std::string const& wkt_g, std::string const& wkt_p0, std::string const& wkt_p1,
  23. bool expected_result,
  24. RelativeDistance expected_rel_dist)
  25. {
  26. Box box;
  27. bg::read_wkt(wkt_g, box);
  28. Point p0, p1;
  29. bg::read_wkt(wkt_p0, p0);
  30. bg::read_wkt(wkt_p1, p1);
  31. test_segment_intersection(box, p0, p1, expected_result, expected_rel_dist);
  32. }
  33. #include <boost/geometry/geometries/point_xy.hpp>
  34. #include <boost/geometry/geometries/point.hpp>
  35. #include <boost/geometry/geometries/box.hpp>
  36. void test_large_integers()
  37. {
  38. typedef bg::model::point<int, 2, bg::cs::cartesian> int_point_type;
  39. typedef bg::model::point<double, 2, bg::cs::cartesian> double_point_type;
  40. bg::model::box<int_point_type> int_box;
  41. bg::model::box<double_point_type> double_box;
  42. int_point_type int_p0, int_p1;
  43. double_point_type double_p0, double_p1;
  44. std::string const str_box = "POLYGON((1536119 192000, 1872000 528000))";
  45. std::string const str_p0 = "POINT(1535000 191000)";
  46. std::string const str_p1 = "POINT(1873000 529000)";
  47. bg::read_wkt(str_box, int_box);
  48. bg::read_wkt(str_box, double_box);
  49. bg::read_wkt(str_p0, int_p0);
  50. bg::read_wkt(str_p1, int_p1);
  51. bg::read_wkt(str_p0, double_p0);
  52. bg::read_wkt(str_p1, double_p1);
  53. float int_value;
  54. bool int_result = bgi::detail::segment_intersection(int_box, int_p0, int_p1, int_value);
  55. double double_value;
  56. bool double_result = bgi::detail::segment_intersection(double_box, double_p0, double_p1, double_value);
  57. BOOST_CHECK(int_result == double_result);
  58. if ( int_result && double_result )
  59. BOOST_CHECK_CLOSE(int_value, double_value, 0.0001);
  60. }
  61. int test_main(int, char* [])
  62. {
  63. typedef bg::model::point<int, 2, bg::cs::cartesian> P2ic;
  64. typedef bg::model::point<float, 2, bg::cs::cartesian> P2fc;
  65. typedef bg::model::point<double, 2, bg::cs::cartesian> P2dc;
  66. typedef bg::model::point<int, 3, bg::cs::cartesian> P3ic;
  67. typedef bg::model::point<float, 3, bg::cs::cartesian> P3fc;
  68. typedef bg::model::point<double, 3, bg::cs::cartesian> P3dc;
  69. test_geometry<bg::model::box<P2ic>, P2ic>("POLYGON((0 1,2 4))", "POINT(0 0)", "POINT(2 5)", true, 1.0f/5);
  70. test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(0 0)", "POINT(2 5)", true, 1.0f/5);
  71. test_geometry<bg::model::box<P2dc>, P2dc>("POLYGON((0 1,2 4))", "POINT(0 0)", "POINT(2 5)", true, 1.0/5);
  72. test_geometry<bg::model::box<P3ic>, P3ic>("POLYGON((0 1 2,2 4 6))", "POINT(0 0 0)", "POINT(2 5 7)", true, 2.0f/7);
  73. test_geometry<bg::model::box<P3fc>, P3fc>("POLYGON((0 1 2,2 4 6))", "POINT(0 0 0)", "POINT(2 5 7)", true, 2.0f/7);
  74. test_geometry<bg::model::box<P3dc>, P3dc>("POLYGON((0 1 2,2 4 6))", "POINT(0 0 0)", "POINT(2 5 7)", true, 2.0/7);
  75. test_geometry<bg::model::box<P2ic>, P2ic>("POLYGON((0 1,2 4))", "POINT(3 4)", "POINT(0 0)", true, 1.0f/3);
  76. test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(3 4)", "POINT(0 2)", true, 1.0f/3);
  77. test_geometry<bg::model::box<P2dc>, P2dc>("POLYGON((0 1,2 4))", "POINT(3 4)", "POINT(0 2)", true, 1.0/3);
  78. test_geometry<bg::model::box<P3ic>, P3ic>("POLYGON((0 1 2,2 4 6))", "POINT(3 5 6)", "POINT(0 3 3)", true, 1.0f/2);
  79. test_geometry<bg::model::box<P3fc>, P3fc>("POLYGON((0 1 2,2 4 6))", "POINT(3 5 6)", "POINT(0 3 3)", true, 1.0f/2);
  80. test_geometry<bg::model::box<P3dc>, P3dc>("POLYGON((0 1 2,2 4 6))", "POINT(3 5 6)", "POINT(0 3 3)", true, 1.0/2);
  81. test_geometry<bg::model::box<P2ic>, P2ic>("POLYGON((0 1,2 4))", "POINT(1 0)", "POINT(1 5)", true, 1.0f/5);
  82. test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(1 5)", "POINT(1 0)", true, 1.0f/5);
  83. test_geometry<bg::model::box<P2dc>, P2dc>("POLYGON((0 1,2 4))", "POINT(1 0)", "POINT(1 5)", true, 1.0/5);
  84. test_geometry<bg::model::box<P3ic>, P3ic>("POLYGON((0 1 2,2 4 6))", "POINT(1 3 0)", "POINT(1 3 7)", true, 2.0f/7);
  85. test_geometry<bg::model::box<P3fc>, P3fc>("POLYGON((0 1 2,2 4 6))", "POINT(1 3 7)", "POINT(1 3 0)", true, 1.0f/7);
  86. test_geometry<bg::model::box<P3dc>, P3dc>("POLYGON((0 1 2,2 4 6))", "POINT(1 3 0)", "POINT(1 3 7)", true, 2.0/7);
  87. test_geometry<bg::model::box<P2ic>, P2ic>("POLYGON((0 1,2 4))", "POINT(0 0)", "POINT(0 5)", true, 0.2f);
  88. test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(0 5)", "POINT(0 0)", true, 0.2f);
  89. test_geometry<bg::model::box<P2dc>, P2dc>("POLYGON((0 1,2 4))", "POINT(0 0)", "POINT(0 5)", true, 0.2);
  90. test_geometry<bg::model::box<P2ic>, P2ic>("POLYGON((0 1,2 4))", "POINT(3 0)", "POINT(3 5)", false, 0.0f);
  91. test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(3 5)", "POINT(3 0)", false, 0.0f);
  92. test_geometry<bg::model::box<P2dc>, P2dc>("POLYGON((0 1,2 4))", "POINT(3 0)", "POINT(3 5)", false, 0.0);
  93. test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(1 0)", "POINT(1 1)", true, 1.0f);
  94. test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(1 4)", "POINT(1 5)", true, 0.0f);
  95. test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(0.5 2)", "POINT(1.5 3)", true, 0.0f);
  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. test_geometry<bg::model::box<P2ttmc>, P2ttmc>("POLYGON((0 1,2 4))", "POINT(0 0)", "POINT(2 5)", true, 1.0f/5);
  100. test_geometry<bg::model::box<P3ttmc>, P3ttmc>("POLYGON((0 1 2,2 4 6))", "POINT(0 0 0)", "POINT(2 5 7)", true, 2.0f/7);
  101. #endif
  102. test_large_integers();
  103. return 0;
  104. }