covered_by.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 2015, 2017.
  5. // Modifications copyright (c) 2017 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_covered_by.hpp"
  11. #include <boost/geometry/geometries/geometries.hpp>
  12. #include <boost/geometry/geometries/point_xy.hpp>
  13. template <typename P>
  14. void test_all()
  15. {
  16. /*
  17. // trivial case
  18. test_ring<P>("POINT(1 1)", "POLYGON((0 0,0 2,2 2,2 0,0 0))", true, false);
  19. // on border/corner
  20. test_ring<P>("POINT(0 0)", "POLYGON((0 0,0 2,2 2,2 0,0 0))", false, true);
  21. test_ring<P>("POINT(0 1)", "POLYGON((0 0,0 2,2 2,2 0,0 0))", false, true);
  22. // aligned to segment/vertex
  23. test_ring<P>("POINT(1 1)", "POLYGON((0 0,0 3,3 3,3 1,2 1,2 0,0 0))", true, false);
  24. test_ring<P>("POINT(1 1)", "POLYGON((0 0,0 3,4 3,3 1,2 2,2 0,0 0))", true, false);
  25. // same polygon, but point on border
  26. test_ring<P>("POINT(3 3)", "POLYGON((0 0,0 3,3 3,3 1,2 1,2 0,0 0))", false, true);
  27. test_ring<P>("POINT(3 3)", "POLYGON((0 0,0 3,4 3,3 1,2 2,2 0,0 0))", false, true);
  28. // holes
  29. test_geometry<P, bg::model::polygon<P> >("POINT(2 2)",
  30. "POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,3 1,3 3,1 3,1 1))", false);
  31. */
  32. test_geometry<P, P>("POINT(0 0)", "POINT(0 0)", true);
  33. test_geometry<P, P>("POINT(0 0)", "POINT(1 1)", false);
  34. typedef bg::model::multi_point<P> mpt;
  35. test_geometry<P, mpt>("POINT(0 0)", "MULTIPOINT(0 0, 1 1)", true);
  36. test_geometry<mpt, P>("MULTIPOINT(0 0, 1 1)", "POINT(1 1)", false);
  37. test_geometry<mpt, mpt>("MULTIPOINT(0 0, 1 1)", "MULTIPOINT(1 1, 2 2)", false);
  38. typedef bg::model::segment<P> seg;
  39. test_geometry<P, seg>("POINT(1 1)", "LINESTRING(0 0, 2 2)", true);
  40. test_geometry<P, seg>("POINT(0 0)", "LINESTRING(0 0, 1 1)", true);
  41. test_geometry<P, seg>("POINT(1 0)", "LINESTRING(0 0, 1 1)", false);
  42. // linestrings
  43. typedef bg::model::linestring<P> ls;
  44. test_geometry<P, ls>("POINT(0 0)", "LINESTRING(0 0,1 1,2 2)", true);
  45. test_geometry<P, ls>("POINT(3 3)", "LINESTRING(0 0,1 1,2 2)", false);
  46. test_geometry<P, ls>("POINT(1 1)", "LINESTRING(0 0,2 2,3 3)", true);
  47. // multi_linestrings
  48. typedef bg::model::multi_linestring<ls> mls;
  49. test_geometry<P, mls>("POINT(0 0)", "MULTILINESTRING((0 0,1 1,2 2),(0 0,0 1))", true);
  50. test_geometry<P, mls>("POINT(0 0)", "MULTILINESTRING((0 0,1 1,2 2),(0 0,0 1),(0 0,1 0))", true);
  51. // multi_point/segment
  52. typedef bg::model::multi_point<P> mpt;
  53. test_geometry<mpt, seg>("MULTIPOINT(0 0, 1 1)", "LINESTRING(0 0, 2 2)", true);
  54. // multi_point/linestring
  55. test_geometry<mpt, ls>("MULTIPOINT(0 0, 2 2)", "LINESTRING(0 0, 2 2)", true);
  56. test_geometry<mpt, ls>("MULTIPOINT(1 1, 3 3)", "LINESTRING(0 0, 2 2)", false);
  57. // multi_point/multi_linestring
  58. test_geometry<mpt, mls>("MULTIPOINT(0 0, 1 1)", "MULTILINESTRING((0 0, 2 2),(2 2, 3 3))", true);
  59. test_geometry<mpt, mls>("MULTIPOINT(0 0, 2 2)", "MULTILINESTRING((0 0, 2 2),(2 2, 3 3))", true);
  60. test_geometry<mpt, mls>("MULTIPOINT(0 0, 3 3)", "MULTILINESTRING((0 0, 2 2),(2 2, 3 3))", true);
  61. test_geometry<mpt, mls>("MULTIPOINT(1 1, 4 4)", "MULTILINESTRING((0 0, 2 2),(2 2, 3 3))", false);
  62. // point/A
  63. typedef bg::model::ring<P> ring;
  64. typedef bg::model::polygon<P> poly;
  65. typedef bg::model::multi_polygon<poly> mpoly;
  66. test_geometry<P, ring>("POINT(1 1)", "POLYGON((0 0,0 2,2 2,2 0,0 0))", true);
  67. test_geometry<P, poly>("POINT(1 1)", "POLYGON((0 0,0 2,2 2,2 0,0 0))", true);
  68. test_geometry<P, mpoly>("POINT(1 1)", "MULTIPOLYGON(((0 0,0 2,2 2,2 0,0 0)),((2 2,2 3,3 3,3 2,2 2)))", true);
  69. // multi_point/A
  70. test_geometry<mpt, ring>("MULTIPOINT(0 0, 1 1)", "POLYGON((0 0,0 2,2 2,2 0,0 0))", true);
  71. test_geometry<mpt, poly>("MULTIPOINT(0 0, 2 2)", "POLYGON((0 0,0 2,2 2,2 0,0 0))", true);
  72. test_geometry<mpt, poly>("MULTIPOINT(1 1, 3 3)", "POLYGON((0 0,0 2,2 2,2 0,0 0))", false);
  73. test_geometry<mpt, mpoly>("MULTIPOINT(0 0, 1 1)", "MULTIPOLYGON(((0 0,0 2,2 2,2 0,0 0)),((2 2,2 3,3 3,3 2,2 2)))", true);
  74. test_geometry<mpt, mpoly>("MULTIPOINT(0 0, 2 2)", "MULTIPOLYGON(((0 0,0 2,2 2,2 0,0 0)),((2 2,2 3,3 3,3 2,2 2)))", true);
  75. test_geometry<mpt, mpoly>("MULTIPOINT(0 0, 3 3)", "MULTIPOLYGON(((0 0,0 2,2 2,2 0,0 0)),((2 2,2 3,3 3,3 2,2 2)))", true);
  76. test_geometry<mpt, mpoly>("MULTIPOINT(1 1, 4 4)", "MULTIPOLYGON(((0 0,0 2,2 2,2 0,0 0)),((2 2,2 3,3 3,3 2,2 2)))", false);
  77. typedef bg::model::box<P> box_type;
  78. test_geometry<P, box_type>("POINT(1 1)", "BOX(0 0,2 2)", true);
  79. test_geometry<P, box_type>("POINT(0 0)", "BOX(0 0,2 2)", true);
  80. test_geometry<P, box_type>("POINT(2 2)", "BOX(0 0,2 2)", true);
  81. test_geometry<P, box_type>("POINT(0 1)", "BOX(0 0,2 2)", true);
  82. test_geometry<P, box_type>("POINT(1 0)", "BOX(0 0,2 2)", true);
  83. test_geometry<P, box_type>("POINT(3 3)", "BOX(0 0,2 2)", false);
  84. test_geometry<box_type, box_type>("BOX(1 1,2 2)", "BOX(0 0,3 3)", true);
  85. test_geometry<box_type, box_type>("BOX(0 0,3 3)", "BOX(1 1,2 2)", false);
  86. test_geometry<box_type, box_type>("BOX(0 0,2 2)", "BOX(0 0,3 3)", true);
  87. test_geometry<box_type, box_type>("BOX(1 1,3 3)", "BOX(0 0,3 3)", true);
  88. test_geometry<box_type, box_type>("BOX(1 2,3 3)", "BOX(0 0,3 3)", true);
  89. test_geometry<box_type, box_type>("BOX(1 1,4 3)", "BOX(0 0,3 3)", false);
  90. }
  91. void test_3d()
  92. {
  93. typedef boost::geometry::model::point<double, 3, boost::geometry::cs::cartesian> point_type;
  94. typedef boost::geometry::model::box<point_type> box_type;
  95. box_type box(point_type(0, 0, 0), point_type(4, 4, 4));
  96. BOOST_CHECK_EQUAL(bg::covered_by(point_type(2, 2, 2), box), true);
  97. BOOST_CHECK_EQUAL(bg::covered_by(point_type(2, 4, 2), box), true);
  98. BOOST_CHECK_EQUAL(bg::covered_by(point_type(2, 2, 4), box), true);
  99. BOOST_CHECK_EQUAL(bg::covered_by(point_type(2, 2, 5), box), false);
  100. }
  101. template <typename P1, typename P2>
  102. void test_mixed_of()
  103. {
  104. typedef boost::geometry::model::polygon<P1> polygon_type1;
  105. typedef boost::geometry::model::polygon<P2> polygon_type2;
  106. typedef boost::geometry::model::box<P1> box_type1;
  107. typedef boost::geometry::model::box<P2> box_type2;
  108. polygon_type1 poly1;
  109. polygon_type2 poly2;
  110. boost::geometry::read_wkt("POLYGON((0 0,0 5,5 5,5 0,0 0))", poly1);
  111. boost::geometry::read_wkt("POLYGON((0 0,0 5,5 5,5 0,0 0))", poly2);
  112. box_type1 box1(P1(1, 1), P1(4, 4));
  113. box_type2 box2(P2(0, 0), P2(5, 5));
  114. P1 p1(3, 3);
  115. P2 p2(3, 3);
  116. BOOST_CHECK_EQUAL(bg::covered_by(p1, poly2), true);
  117. BOOST_CHECK_EQUAL(bg::covered_by(p2, poly1), true);
  118. BOOST_CHECK_EQUAL(bg::covered_by(p2, box1), true);
  119. BOOST_CHECK_EQUAL(bg::covered_by(p1, box2), true);
  120. BOOST_CHECK_EQUAL(bg::covered_by(box1, box2), true);
  121. BOOST_CHECK_EQUAL(bg::covered_by(box2, box1), false);
  122. }
  123. void test_mixed()
  124. {
  125. // Mixing point types and coordinate types
  126. test_mixed_of
  127. <
  128. boost::geometry::model::d2::point_xy<double>,
  129. boost::geometry::model::point<double, 2, boost::geometry::cs::cartesian>
  130. >();
  131. test_mixed_of
  132. <
  133. boost::geometry::model::d2::point_xy<float>,
  134. boost::geometry::model::point<double, 2, boost::geometry::cs::cartesian>
  135. >();
  136. test_mixed_of
  137. <
  138. boost::geometry::model::d2::point_xy<int>,
  139. boost::geometry::model::d2::point_xy<double>
  140. >();
  141. }
  142. int test_main( int , char* [] )
  143. {
  144. test_all<bg::model::d2::point_xy<int> >();
  145. test_all<bg::model::d2::point_xy<double> >();
  146. //test_spherical<bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > >();
  147. test_mixed();
  148. test_3d();
  149. #if defined(HAVE_TTMATH)
  150. test_all<bg::model::d2::point_xy<ttmath_big> >();
  151. test_eps<bg::model::d2::point_xy<ttmath_big> >();
  152. //test_spherical<bg::model::point<ttmath_big, 2, bg::cs::spherical_equatorial<bg::degree> > >();
  153. #endif
  154. return 0;
  155. }