envelope.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  6. // This file was modified by Oracle on 2015, 2016.
  7. // Modifications copyright (c) 2015, Oracle and/or its affiliates.
  8. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  9. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  10. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  11. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #include <boost/numeric/conversion/bounds.hpp>
  16. #include "test_envelope.hpp"
  17. #include <boost/geometry/geometries/geometries.hpp>
  18. #include <boost/geometry/geometries/point_xy.hpp>
  19. #include <boost/geometry/geometries/adapted/c_array.hpp>
  20. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  21. #include <boost/geometry/geometries/adapted/std_pair_as_segment.hpp>
  22. #include <test_common/test_point.hpp>
  23. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  24. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  25. template <typename P>
  26. void test_2d()
  27. {
  28. test_envelope<P>("POINT(1 1)", 1, 1, 1, 1);
  29. test_envelope<bg::model::linestring<P> >("LINESTRING(1 1,2 2)", 1, 2, 1, 2);
  30. test_envelope<bg::model::polygon<P> >("POLYGON((1 1,1 3,3 3,3 1,1 1))", 1, 3, 1, 3);
  31. test_envelope<bg::model::ring<P> >("POLYGON((1 1,1 3,3 3,3 1,1 1))", 1, 3, 1, 3);
  32. test_envelope<bg::model::box<P> >("BOX(1 1,3 3)", 1, 3, 1, 3);
  33. // Triangle, closed and open, and CCW.
  34. // Note that for the envelope algorithm,
  35. // these combinations should theoretically not differ
  36. test_envelope<bg::model::ring<P> >("POLYGON((4 1,0 7,7 9,4 1))", 0, 7, 1, 9);
  37. test_envelope<bg::model::ring<P, true, false> >("POLYGON((4 1,0 7,7 9))", 0, 7, 1, 9);
  38. test_envelope<bg::model::ring<P, false> >("POLYGON((4 1,7 9,0 7,4 1))", 0, 7, 1, 9);
  39. test_envelope<bg::model::ring<P, false, false> >("POLYGON((4 1,7 9,0 7))", 0, 7, 1, 9);
  40. typedef std::pair<P, P> segment_type;
  41. test_envelope<segment_type>("SEGMENT(1 1,3 3)", 1, 3, 1, 3);
  42. }
  43. template <typename P>
  44. void test_3d()
  45. {
  46. test_envelope<P>("POINT(1 2 3)", 1, 1, 2, 2, 3, 3);
  47. test_envelope<P>("POINT(3 2 1)", 3, 3, 2, 2, 1, 1);
  48. test_envelope<bg::model::linestring<P> >("LINESTRING(1 1 1,2 2 2)", 1, 2, 1, 2, 1, 2);
  49. test_envelope<bg::model::box<P> >("BOX(1 1 1,3 3 3)", 1, 3, 1, 3, 1, 3);
  50. }
  51. template <typename Geometry>
  52. void test_empty_geometry(std::string const& wkt)
  53. {
  54. typedef typename bg::coordinate_type<Geometry>::type ct;
  55. ct high_val = boost::numeric::bounds<ct>::highest();
  56. ct low_val = boost::numeric::bounds<ct>::lowest();
  57. test_envelope<Geometry>(wkt, high_val, low_val, high_val, low_val);
  58. }
  59. template <typename P>
  60. void test_empty()
  61. {
  62. test_empty_geometry<bg::model::linestring<P> >("LINESTRING()");
  63. test_empty_geometry<bg::model::ring<P> >("POLYGON(())");
  64. test_empty_geometry<bg::model::polygon<P> >("POLYGON(())");
  65. test_empty_geometry<bg::model::multi_point<P> >("MULTIPOINT()");
  66. test_empty_geometry
  67. <
  68. bg::model::multi_linestring<bg::model::linestring<P> >
  69. >("MULTILINESTRING()");
  70. test_empty_geometry
  71. <
  72. bg::model::multi_polygon<bg::model::polygon<P> >
  73. >("MULTIPOLYGON()");
  74. }
  75. template <typename P>
  76. void test_invalid()
  77. {
  78. // polygon with empty exterior and interior rings
  79. test_empty_geometry<bg::model::polygon<P> >("POLYGON((),(),())");
  80. // polygon with empty interior rings
  81. test_envelope
  82. <
  83. bg::model::polygon<P>
  84. >("POLYGON((1 2,1 20,22 20,22 2,1 2),(),())",
  85. 1, 22, 2, 20);
  86. // another polygon with empty interior rings
  87. test_envelope
  88. <
  89. bg::model::polygon<P>
  90. >("POLYGON((1 2,1 20,22 20,22 2,1 2),(),(3 4,19 4,19 18,3 18,3 4),())",
  91. 1, 22, 2, 20);
  92. // polygon with empty exterior ring
  93. test_envelope
  94. <
  95. bg::model::polygon<P>
  96. >("POLYGON((),(),(3 4,19 4,19 18,3 18,3 4),())",
  97. 3, 19, 4, 18);
  98. // another polygon with empty exterior ring
  99. test_envelope
  100. <
  101. bg::model::polygon<P>
  102. >("POLYGON((),(),(3 4,19 4,19 18,3 18,3 4),(4 5,18 5,18 17,4 17,4 5))",
  103. 3, 19, 4, 18);
  104. // yet one more polygon with empty exterior ring
  105. test_envelope
  106. <
  107. bg::model::polygon<P>
  108. >("POLYGON((),(),(4 5,18 5,18 17,4 17,4 5),(3 4,19 4,19 18,3 18,3 4))",
  109. 3, 19, 4, 18);
  110. // multilinestring with empty linestrings
  111. test_empty_geometry
  112. <
  113. bg::model::multi_linestring<bg::model::linestring<P> >
  114. >("MULTILINESTRING((),(),())");
  115. // multilinestring with empty and non-empty linestrings
  116. test_envelope
  117. <
  118. bg::model::multi_linestring<bg::model::linestring<P> >
  119. >("MULTILINESTRING((),(10 20),())", 10, 10, 20, 20);
  120. // multipolygon with empty polygon
  121. test_empty_geometry
  122. <
  123. bg::model::multi_polygon<bg::model::polygon<P> >
  124. >("MULTIPOLYGON((()))");
  125. // multipolygon with many empty polygons
  126. test_empty_geometry
  127. <
  128. bg::model::multi_polygon<bg::model::polygon<P> >
  129. >("MULTIPOLYGON(((),(),()),(()),((),(),(),(),()))");
  130. // multipolygon with empty polygons and non-empty (valid) polygon
  131. test_envelope
  132. <
  133. bg::model::multi_polygon<bg::model::polygon<P> >
  134. >("MULTIPOLYGON(((),(),()),((10 30,10 40,20 30,10 30)),\
  135. ((),(),()),(()))", 10, 20, 30, 40);
  136. // multipolygon with empty polygons and non-empty (valid) polygon
  137. // that has an interior ring
  138. test_envelope
  139. <
  140. bg::model::multi_polygon<bg::model::polygon<P> >
  141. >("MULTIPOLYGON(((),(),()),(()),\
  142. ((1 2,1 20,22 20,22 2,1 2),(3 4,19 4,19 18,3 18,3 4)),(()))",
  143. 1, 22, 2, 20);
  144. // multipolygon with empty polygons and non-empty (invalid) polygon
  145. // that has an interior ring but empty exterior ring
  146. test_envelope
  147. <
  148. bg::model::multi_polygon<bg::model::polygon<P> >
  149. >("MULTIPOLYGON(((),(),()),(()),((),(3 4,19 4,19 18,3 18,3 4)),(()))",
  150. 3, 19, 4, 18);
  151. // multipolygon with empty polygons and non-empty (invalid) polygon
  152. // that has an interior ring but empty exterior ring
  153. test_envelope
  154. <
  155. bg::model::multi_polygon<bg::model::polygon<P> >
  156. >("MULTIPOLYGON(((),(),()),((),(),(3 4,19 4,19 18,3 18,3 4),()),(()))",
  157. 3, 19, 4, 18);
  158. // multipolygon with empty polygons and non-empty (invalid) polygon
  159. // that has two non-empty interior rings but empty exterior ring
  160. test_envelope
  161. <
  162. bg::model::multi_polygon<bg::model::polygon<P> >
  163. >("MULTIPOLYGON(((),(),()),\
  164. ((),(),(3 4,19 4,19 18,3 18,3 4),(4 5,18 5,18 17,4 17,4 5),()),\
  165. (()))",
  166. 3, 19, 4, 18);
  167. }
  168. int test_main(int, char* [])
  169. {
  170. //test_2d<int[2]>();
  171. //test_2d<float[2]>();
  172. //test_2d<double[2]>();
  173. test_2d<boost::tuple<float, float> >();
  174. test_2d<bg::model::d2::point_xy<int> >();
  175. test_2d<bg::model::d2::point_xy<float> >();
  176. test_2d<bg::model::d2::point_xy<double> >();
  177. test_3d<test::test_point>();
  178. test_3d<boost::tuple<int, int, int> >();
  179. test_empty<boost::tuple<float, float> >();
  180. test_empty<bg::model::d2::point_xy<int> >();
  181. test_empty<bg::model::d2::point_xy<float> >();
  182. test_empty<bg::model::d2::point_xy<double> >();
  183. test_invalid<boost::tuple<float, float> >();
  184. test_invalid<bg::model::d2::point_xy<int> >();
  185. test_invalid<bg::model::d2::point_xy<float> >();
  186. test_invalid<bg::model::d2::point_xy<double> >();
  187. #ifdef HAVE_TTMATH
  188. test_2d<bg::model::d2::point_xy<ttmath_big> >();
  189. test_3d<boost::tuple<ttmath_big, ttmath_big, ttmath_big> >();
  190. #endif
  191. return 0;
  192. }