area.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  6. // This file was modified by Oracle on 2015, 2016, 2017.
  7. // Modifications copyright (c) 2015-2017, Oracle and/or its affiliates.
  8. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  9. // Contributed and/or modified by Adam Wulkiewicz, 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 <algorithms/area/test_area.hpp>
  16. #include <boost/geometry/geometries/point_xy.hpp>
  17. #include <boost/geometry/geometries/point.hpp>
  18. #include <boost/geometry/geometries/box.hpp>
  19. #include <boost/geometry/geometries/ring.hpp>
  20. #include <boost/geometry/geometries/polygon.hpp>
  21. #include <test_geometries/all_custom_ring.hpp>
  22. #include <test_geometries/all_custom_polygon.hpp>
  23. //#define BOOST_GEOMETRY_TEST_DEBUG
  24. #include <boost/variant/variant.hpp>
  25. template <typename Polygon>
  26. void test_polygon()
  27. {
  28. // Rotated square, length=sqrt(2) -> area=2
  29. test_geometry<Polygon>("POLYGON((1 1,2 2,3 1,2 0,1 1))", 2.0);
  30. test_geometry<Polygon>("POLYGON((1 1,2 2,3 1,2 0,1 1))", 2.0);
  31. test_geometry<Polygon>("POLYGON((0 0,0 7,4 2,2 0,0 0))", 16.0);
  32. test_geometry<Polygon>("POLYGON((1 1,2 1,2 2,1 2,1 1))", -1.0);
  33. test_geometry<Polygon>("POLYGON((0 0,0 7,4 2,2 0,0 0), (1 1,2 1,2 2,1 2,1 1))", 15.0);
  34. }
  35. template <typename P>
  36. void test_all()
  37. {
  38. test_geometry<bg::model::box<P> >("POLYGON((0 0,2 2))", 4.0);
  39. test_geometry<bg::model::box<P> >("POLYGON((2 2,0 0))", 4.0);
  40. test_polygon<bg::model::polygon<P> >();
  41. test_polygon<all_custom_polygon<P> >();
  42. // clockwise rings (second is wrongly ordered)
  43. test_geometry<bg::model::ring<P> >("POLYGON((0 0,0 7,4 2,2 0,0 0))", 16.0);
  44. test_geometry<bg::model::ring<P> >("POLYGON((0 0,2 0,4 2,0 7,0 0))", -16.0);
  45. test_geometry<all_custom_ring<P> >("POLYGON((0 0,0 7,4 2,2 0,0 0))", 16.0);
  46. // ccw
  47. test_geometry<bg::model::polygon<P, false> >
  48. ("POLYGON((0 0,0 7,4 2,2 0,0 0), (1 1,2 1,2 2,1 2,1 1))", -15.0);
  49. test_geometry<bg::model::polygon<P, false> >
  50. ("POLYGON((1 0,0 1,-1 0,0 -1,1 0))", 2);
  51. typedef typename bg::coordinate_type<P>::type coord_type;
  52. if (BOOST_GEOMETRY_CONDITION((boost::is_same<coord_type, double>::value)))
  53. {
  54. test_geometry<bg::model::polygon<P, false, false> >
  55. ("POLYGON((100000001 100000000, 100000000 100000001, \
  56. 99999999 100000000, 100000000 99999999))", 2);
  57. }
  58. else if (BOOST_GEOMETRY_CONDITION((boost::is_same<coord_type, float>::value)))
  59. {
  60. test_geometry<bg::model::polygon<P, false, false> >
  61. ("POLYGON((100001 100000, 100000 100001, \
  62. 99999 100000, 100000 99999))", 2);
  63. }
  64. }
  65. template <typename P>
  66. void test_ccw()
  67. {
  68. typedef typename bg::coordinate_type<P>::type ct;
  69. bg::model::polygon<P, false> ccw_polygon;
  70. // counterclockwise rings (second is wrongly ordered)
  71. std::string poly1 = "POLYGON((1 1,2 2,3 1,2 0,1 1))";
  72. std::string poly2 = "POLYGON((1 1,2 0,3 1,2 2,1 1))";
  73. std::string poly3 = "POLYGON((0 0,0 7,4 2,2 0,0 0))";
  74. std::string poly4 = "POLYGON((0 0,2 0,4 2,0 7,0 0))";
  75. bg::read_wkt(poly1, ccw_polygon);
  76. ct area1 = bg::area(ccw_polygon);
  77. bg::read_wkt(poly2, ccw_polygon);
  78. ct area2 = bg::area(ccw_polygon);
  79. bg::read_wkt(poly3, ccw_polygon);
  80. ct area3 = bg::area(ccw_polygon);
  81. bg::read_wkt(poly4, ccw_polygon);
  82. ct area4 = bg::area(ccw_polygon);
  83. BOOST_CHECK_CLOSE(area1, -1 * area2, 0.001);
  84. BOOST_CHECK_CLOSE(area3, -1 * area4, 0.001);
  85. }
  86. template <typename P, typename CT>
  87. void test_open(CT expected_area)
  88. {
  89. typedef bg::model::polygon<P, true, false> open_polygon;
  90. test_geometry<open_polygon>("POLYGON((1 1,2 2,3 1,2 0))", expected_area);
  91. // Note the triangular testcase used in CCW is not sensible for open/close
  92. }
  93. template <typename P, typename CT>
  94. void test_open_ccw(CT expected_area)
  95. {
  96. typedef bg::model::polygon<P, false, false> open_polygon;
  97. test_geometry<open_polygon>("POLYGON((1 1,2 0,3 1,2 2))", expected_area);
  98. // Note the triangular testcase used in CCW is not sensible for open/close
  99. }
  100. template <typename P>
  101. void test_poles_ccw()
  102. {
  103. typedef typename bg::coordinate_type<P>::type ct;
  104. bg::model::polygon<P, false> polygon;
  105. std::string poly1 = "POLYGON((45 45,45 95,95 45,45 45))";
  106. std::string poly2 = "POLYGON((45 45,95 45,45 95,45 45))";
  107. std::string poly3 = "POLYGON((45 -45,45 -95,95 -45,45 -45))";
  108. std::string poly4 = "POLYGON((45 -45,95 -45,45 -95,45 -45))";
  109. bg::read_wkt(poly1, polygon);
  110. ct area1 = bg::area(polygon);
  111. bg::read_wkt(poly2, polygon);
  112. ct area2 = bg::area(polygon);
  113. bg::read_wkt(poly3, polygon);
  114. ct area3 = bg::area(polygon);
  115. bg::read_wkt(poly4, polygon);
  116. ct area4 = bg::area(polygon);
  117. BOOST_CHECK_CLOSE(area1, -1 * area2, 0.001);
  118. BOOST_CHECK_CLOSE(area3, -1 * area4, 0.001);
  119. }
  120. template <typename P>
  121. void test_empty_input()
  122. {
  123. bg::model::polygon<P> poly_empty;
  124. bg::model::ring<P> ring_empty;
  125. test_empty_input(poly_empty);
  126. test_empty_input(ring_empty);
  127. }
  128. void test_large_integers()
  129. {
  130. typedef bg::model::point<int, 2, bg::cs::cartesian> int_point_type;
  131. typedef bg::model::point<double, 2, bg::cs::cartesian> double_point_type;
  132. bg::model::polygon<int_point_type> int_poly;
  133. bg::model::polygon<double_point_type> double_poly;
  134. std::string const polygon_li = "POLYGON((1872000 528000,1872000 192000,\
  135. 1536119 192000,1536000 528000,1200000 528000,\
  136. 1200000 863880,1536000 863880,1872000 863880,\
  137. 1872000 528000))";
  138. bg::read_wkt(polygon_li, int_poly);
  139. bg::read_wkt(polygon_li, double_poly);
  140. double int_area = bg::area(int_poly);
  141. double double_area = bg::area(double_poly);
  142. BOOST_CHECK_CLOSE(int_area, double_area, 0.0001);
  143. }
  144. void test_variant()
  145. {
  146. typedef bg::model::point<double, 2, bg::cs::cartesian> double_point_type;
  147. typedef bg::model::polygon<double_point_type> polygon_type;
  148. typedef bg::model::box<double_point_type> box_type;
  149. polygon_type poly;
  150. std::string const polygon_li = "POLYGON((18 5,18 1,15 1,15 5,12 5,12 8,15 8,18 8,18 5))";
  151. bg::read_wkt(polygon_li, poly);
  152. box_type box;
  153. std::string const box_li = "BOX(0 0,2 2)";
  154. bg::read_wkt(box_li, box);
  155. boost::variant<polygon_type, box_type> v;
  156. v = poly;
  157. BOOST_CHECK_CLOSE(bg::area(v), bg::area(poly), 0.0001);
  158. v = box;
  159. BOOST_CHECK_CLOSE(bg::area(v), bg::area(box), 0.0001);
  160. }
  161. int test_main(int, char* [])
  162. {
  163. test_all<bg::model::point<int, 2, bg::cs::cartesian> >();
  164. test_all<bg::model::point<float, 2, bg::cs::cartesian> >();
  165. test_all<bg::model::point<double, 2, bg::cs::cartesian> >();
  166. typedef bg::model::point<double, 2, bg::cs::cartesian> pt_crt;
  167. typedef bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > pt_sph;
  168. typedef bg::model::point<double, 2, bg::cs::geographic<bg::degree> > pt_geo;
  169. // mean Earth's radius^2
  170. double r2 = bg::math::sqr(bg::get_radius<0>(bg::srs::sphere<double>()));
  171. test_ccw<pt_crt>();
  172. test_ccw<pt_sph>();
  173. test_ccw<pt_geo>();
  174. test_open<pt_crt>(2.0);
  175. test_open<pt_sph>(24726179921.523518 / r2);
  176. test_open<pt_geo >(24615492936.977146);
  177. test_open_ccw<pt_crt>(2.0);
  178. test_open_ccw<pt_sph>(24726179921.523518 / r2);
  179. test_open_ccw<pt_geo >(24615492936.977146);
  180. test_poles_ccw<pt_crt>();
  181. test_poles_ccw<pt_sph>();
  182. test_poles_ccw<pt_geo >();
  183. #ifdef HAVE_TTMATH
  184. test_all<bg::model::d2::point_xy<ttmath_big> >();
  185. test_spherical_geo<ttmath_big>();
  186. #endif
  187. test_large_integers();
  188. test_variant();
  189. // test_empty_input<bg::model::d2::point_xy<int> >();
  190. return 0;
  191. }