within.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2013-2015 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2014, 2015, 2016, 2018.
  5. // Modifications copyright (c) 2014-2018 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  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. #include "test_within.hpp"
  11. #include <boost/geometry/geometries/geometries.hpp>
  12. #include <boost/geometry/geometries/point_xy.hpp>
  13. #include <boost/geometry/geometries/multi_point.hpp>
  14. #include <boost/geometry/geometries/multi_linestring.hpp>
  15. #include <boost/geometry/geometries/multi_polygon.hpp>
  16. template <typename P1, typename P2>
  17. void test_point_box()
  18. {
  19. typedef bg::model::box<P1> box_type1;
  20. typedef bg::model::box<P2> box_type2;
  21. test_geometry<P1, box_type2>("POINT(1 1)", "BOX(0 0,2 2)", true);
  22. test_geometry<P1, box_type2>("POINT(0 0)", "BOX(0 0,2 2)", false);
  23. test_geometry<P1, box_type2>("POINT(2 2)", "BOX(0 0,2 2)", false);
  24. test_geometry<P1, box_type2>("POINT(0 1)", "BOX(0 0,2 2)", false);
  25. test_geometry<P1, box_type2>("POINT(1 0)", "BOX(0 0,2 2)", false);
  26. test_geometry<P1, box_type2>("POINT(3 3)", "BOX(1 1,4 4)", true);
  27. test_geometry<P2, box_type1>("POINT(3 3)", "BOX(0 0,5 5)", true);
  28. test_geometry<box_type1, box_type2>("BOX(1 1,2 2)", "BOX(0 0,3 3)", true);
  29. test_geometry<box_type1, box_type2>("BOX(0 0,3 3)", "BOX(1 1,2 2)", false);
  30. test_geometry<box_type1, box_type2>("BOX(1 1,3 3)", "BOX(0 0,3 3)", true);
  31. test_geometry<box_type1, box_type2>("BOX(3 1,3 3)", "BOX(0 0,3 3)", false);
  32. test_geometry<box_type1, box_type2>("BOX(1 1,4 4)", "BOX(0 0,5 5)", true);
  33. test_geometry<box_type2, box_type1>("BOX(0 0,5 5)", "BOX(1 1,4 4)", false);
  34. /*
  35. test_within_code<P, box_type>("POINT(1 1)", "BOX(0 0,2 2)", 1);
  36. test_within_code<P, box_type>("POINT(1 0)", "BOX(0 0,2 2)", 0);
  37. test_within_code<P, box_type>("POINT(0 1)", "BOX(0 0,2 2)", 0);
  38. test_within_code<P, box_type>("POINT(0 3)", "BOX(0 0,2 2)", -1);
  39. test_within_code<P, box_type>("POINT(3 3)", "BOX(0 0,2 2)", -1);
  40. test_within_code<box_type, box_type>("BOX(1 1,2 2)", "BOX(0 0,3 3)", 1);
  41. test_within_code<box_type, box_type>("BOX(0 1,2 2)", "BOX(0 0,3 3)", 0);
  42. test_within_code<box_type, box_type>("BOX(1 0,2 2)", "BOX(0 0,3 3)", 0);
  43. test_within_code<box_type, box_type>("BOX(1 1,2 3)", "BOX(0 0,3 3)", 0);
  44. test_within_code<box_type, box_type>("BOX(1 1,3 2)", "BOX(0 0,3 3)", 0);
  45. test_within_code<box_type, box_type>("BOX(1 1,3 4)", "BOX(0 0,3 3)", -1);
  46. */
  47. }
  48. void test_point_box_3d()
  49. {
  50. typedef boost::geometry::model::point<double, 3, boost::geometry::cs::cartesian> point_type;
  51. typedef boost::geometry::model::box<point_type> box_type;
  52. box_type box(point_type(0, 0, 0), point_type(4, 4, 4));
  53. BOOST_CHECK_EQUAL(bg::within(point_type(2, 2, 2), box), true);
  54. BOOST_CHECK_EQUAL(bg::within(point_type(2, 4, 2), box), false);
  55. BOOST_CHECK_EQUAL(bg::within(point_type(2, 2, 4), box), false);
  56. BOOST_CHECK_EQUAL(bg::within(point_type(2, 2, 5), box), false);
  57. box_type box2(point_type(2, 2, 2), point_type(3, 3, 3));
  58. BOOST_CHECK_EQUAL(bg::within(box2, box), true);
  59. }
  60. template <typename P1, typename P2>
  61. void test_point_poly()
  62. {
  63. typedef boost::geometry::model::polygon<P1> poly1;
  64. typedef boost::geometry::model::polygon<P2> poly2;
  65. test_geometry<P1, poly2>("POINT(3 3)", "POLYGON((0 0,0 5,5 5,5 0,0 0))", true);
  66. test_geometry<P2, poly1>("POINT(3 3)", "POLYGON((0 0,0 5,5 5,5 0,0 0))", true);
  67. }
  68. template <typename P1, typename P2>
  69. void test_all()
  70. {
  71. test_point_box<P1, P2>();
  72. test_point_poly<P1, P2>();
  73. }
  74. template <typename P>
  75. void test_all()
  76. {
  77. test_all<P, P>();
  78. }
  79. void test_strategy()
  80. {
  81. // Test by explicitly specifying a strategy
  82. typedef bg::model::d2::point_xy<double> point_type;
  83. typedef bg::model::box<point_type> box_type;
  84. point_type p(3, 3);
  85. box_type b(point_type(0, 0), point_type(5, 5));
  86. box_type b0(point_type(0, 0), point_type(5, 0));
  87. bool r = bg::within(p, b,
  88. bg::strategy::within::cartesian_point_box());
  89. BOOST_CHECK_EQUAL(r, true);
  90. r = bg::within(b, b,
  91. bg::strategy::within::box_in_box<box_type, box_type>());
  92. BOOST_CHECK_EQUAL(r, true);
  93. r = bg::within(b0, b0,
  94. bg::strategy::within::box_in_box<box_type, box_type>());
  95. BOOST_CHECK_EQUAL(r, false);
  96. r = bg::within(p, b,
  97. bg::strategy::within::point_in_box_by_side<>());
  98. BOOST_CHECK_EQUAL(r, true);
  99. }
  100. int test_main( int , char* [] )
  101. {
  102. typedef boost::geometry::model::d2::point_xy<double> xyd;
  103. typedef boost::geometry::model::d2::point_xy<float> xyf;
  104. typedef boost::geometry::model::d2::point_xy<int> xyi;
  105. typedef boost::geometry::model::point<double, 2, boost::geometry::cs::cartesian> p2d;
  106. test_all<xyd, p2d>();
  107. test_all<xyf, p2d>();
  108. test_all<xyi, xyd>();
  109. test_all<xyi>();
  110. test_all<xyd>();
  111. test_point_box_3d();
  112. test_strategy();
  113. #if defined(HAVE_TTMATH)
  114. test_all<bg::model::d2::point_xy<ttmath_big> >();
  115. #endif
  116. return 0;
  117. }