within_sph_geo.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Boost.Geometry
  2. // Copyright (c) 2016-2018 Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  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 "test_within.hpp"
  8. #include <boost/geometry/geometries/geometries.hpp>
  9. template <typename Point>
  10. void test_point_box_by_side()
  11. {
  12. // Test spherical boxes
  13. // See also http://www.gcmap.com/mapui?P=1E45N-19E45N-19E55N-1E55N-1E45N,10E55.1N,10E45.1N
  14. typedef bg::model::box<Point> box_t;
  15. bg::strategy::within::point_in_box_by_side<> by_side;
  16. box_t box;
  17. bg::read_wkt("POLYGON((1 45,19 55))", box);
  18. BOOST_CHECK_EQUAL(bg::within(Point(10, 55.1), box, by_side), true);
  19. BOOST_CHECK_EQUAL(bg::within(Point(10, 55.2), box, by_side), true);
  20. BOOST_CHECK_EQUAL(bg::within(Point(10, 55.3), box, by_side), true);
  21. BOOST_CHECK_EQUAL(bg::within(Point(10, 55.4), box, by_side), false);
  22. BOOST_CHECK_EQUAL(bg::within(Point(10, 45.1), box, by_side), false);
  23. BOOST_CHECK_EQUAL(bg::within(Point(10, 45.2), box, by_side), false);
  24. BOOST_CHECK_EQUAL(bg::within(Point(10, 45.3), box, by_side), false);
  25. BOOST_CHECK_EQUAL(bg::within(Point(10, 45.4), box, by_side), true);
  26. // By default Box is not a polygon in spherical CS, edges are defined by small circles
  27. BOOST_CHECK_EQUAL(bg::within(Point(10, 45.1), box), true);
  28. BOOST_CHECK_EQUAL(bg::within(Point(10, 54.9), box), true);
  29. BOOST_CHECK_EQUAL(bg::within(Point(10, 55), box), false);
  30. BOOST_CHECK_EQUAL(bg::within(Point(10, 45), box), false);
  31. // Crossing the dateline (Near Tuvalu)
  32. // http://www.gcmap.com/mapui?P=178E10S-178W10S-178W6S-178E6S-178E10S,180W5.999S,180E9.999S
  33. // http://en.wikipedia.org/wiki/Tuvalu
  34. box_t tuvalu(Point(178, -10), Point(-178, -6));
  35. BOOST_CHECK_EQUAL(bg::within(Point(180, -8), tuvalu, by_side), true);
  36. BOOST_CHECK_EQUAL(bg::within(Point(-180, -8), tuvalu, by_side), true);
  37. BOOST_CHECK_EQUAL(bg::within(Point(180, -5.999), tuvalu, by_side), false);
  38. BOOST_CHECK_EQUAL(bg::within(Point(180, -10.001), tuvalu, by_side), true);
  39. // The above definition of a Box is not valid
  40. // min should be lesser than max
  41. // By default Box is not a polygon in spherical CS, edges are defined by small circles
  42. box_t tuvalu2(Point(178, -10), Point(182, -6));
  43. BOOST_CHECK_EQUAL(bg::within(Point(180, -8), tuvalu2), true);
  44. BOOST_CHECK_EQUAL(bg::within(Point(-180, -8), tuvalu2), true);
  45. BOOST_CHECK_EQUAL(bg::within(Point(180, -6.001), tuvalu2), true);
  46. BOOST_CHECK_EQUAL(bg::within(Point(180, -5.999), tuvalu2), false);
  47. BOOST_CHECK_EQUAL(bg::within(Point(180, -9.999), tuvalu2), true);
  48. BOOST_CHECK_EQUAL(bg::within(Point(180, -10.001), tuvalu2), false);
  49. }
  50. template <typename P>
  51. void test_point_box()
  52. {
  53. typedef bg::model::box<P> box_t;
  54. test_geometry<P, box_t>("POINT(0 0)", "BOX(0 0, 1 1)", false);
  55. test_geometry<P, box_t>("POINT(1 1)", "BOX(0 0, 2 2)", true);
  56. test_geometry<P, box_t>("POINT(180 1)", "BOX(170 0, 190 2)", true);
  57. test_geometry<P, box_t>("POINT(-180 1)", "BOX(170 0, 190 2)", true);
  58. test_geometry<P, box_t>("POINT(180 1)", "BOX(170 0, 180 2)", false);
  59. test_geometry<P, box_t>("POINT(-180 1)", "BOX(170 0, 180 2)", false);
  60. test_geometry<P, box_t>("POINT(179 1)", "BOX(170 0, 190 2)", true);
  61. test_geometry<P, box_t>("POINT(-179 1)", "BOX(170 0, 190 2)", true);
  62. test_geometry<P, box_t>("POINT(179 1)", "BOX(170 0, 180 2)", true);
  63. test_geometry<P, box_t>("POINT(-179 1)", "BOX(170 0, 180 2)", false);
  64. test_geometry<P, box_t>("POINT(169 1)", "BOX(170 0, 180 2)", false);
  65. test_point_box_by_side<P>();
  66. }
  67. template <typename P>
  68. void test_box_box()
  69. {
  70. typedef bg::model::box<P> box_t;
  71. test_geometry<box_t, box_t>("BOX(0 0, 1 1)", "BOX(0 0, 1 1)", true);
  72. test_geometry<box_t, box_t>("BOX(-170 0,-160 1)", "BOX(-180 0, 180 1)", true);
  73. test_geometry<box_t, box_t>("BOX(-170 0,-160 1)", "BOX(170 0, 200 1)", true);
  74. test_geometry<box_t, box_t>("BOX(-170 0,-150 1)", "BOX(170 0, 200 1)", false);
  75. test_geometry<box_t, box_t>("BOX(0 0,1 1)", "BOX(170 0, 370 1)", true);
  76. test_geometry<box_t, box_t>("BOX(0 0,10 1)", "BOX(170 0, 370 1)", true);
  77. test_geometry<box_t, box_t>("BOX(-180 0,10 1)", "BOX(170 0, 370 1)", true);
  78. test_geometry<box_t, box_t>("BOX(-180 0,20 1)", "BOX(170 0, 370 1)", false);
  79. test_geometry<box_t, box_t>("BOX(10 0,20 1)", "BOX(170 0, 370 1)", false);
  80. test_geometry<box_t, box_t>("BOX(160 0,180 1)", "BOX(170 0, 370 1)", false);
  81. test_geometry<box_t, box_t>("BOX(-180 0,-170 1)", "BOX(180 0, 190 1)", true); // invalid?
  82. test_geometry<box_t, box_t>("BOX(-180 0,-170 1)", "BOX(180 0, 191 1)", true); // invalid?
  83. test_geometry<box_t, box_t>("BOX(-180 0,-170 1)", "BOX(179 0, 190 1)", true);
  84. test_geometry<box_t, box_t>("BOX(-180 0,-170 1)", "BOX(181 0, 190 1)", false); // invalid?
  85. test_geometry<box_t, box_t>("BOX(-180 0,-170 1)", "BOX(180 0, 189 1)", false); // invalid?
  86. }
  87. template <typename P>
  88. void test_cs()
  89. {
  90. test_point_box<P>();
  91. test_box_box<P>();
  92. }
  93. int test_main( int , char* [] )
  94. {
  95. test_cs<bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > >();
  96. test_cs<bg::model::point<double, 2, bg::cs::geographic<bg::degree> > >();
  97. #if defined(HAVE_TTMATH)
  98. test_cs<bg::model::point<ttmath_big, 2, bg::cs::spherical_equatorial<bg::degree> > >();
  99. test_cs<bg::model::point<ttmath_big, 2, bg::cs::geographic<bg::degree> > >();
  100. #endif
  101. return 0;
  102. }