/Src/Dependencies/Boost/libs/geometry/test/algorithms/difference.cpp

http://hadesmem.googlecode.com/ · C++ · 365 lines · 192 code · 66 blank · 107 comment · 0 complexity · 9c48b272c05c1027525574272729f993 MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2010-2011 Barend Gehrels, Amsterdam, the Netherlands.
  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. //#define BOOST_GEOMETRY_CHECK_WITH_POSTGIS
  8. //#define BOOST_GEOMETRY_DEBUG_ASSEMBLE
  9. #include <iostream>
  10. #include <string>
  11. #include <iomanip>
  12. #include <boost/geometry/algorithms/correct.hpp>
  13. #include <boost/geometry/algorithms/perimeter.hpp>
  14. #include <boost/geometry/multi/algorithms/correct.hpp>
  15. #include <boost/geometry/multi/algorithms/intersection.hpp>
  16. #include <boost/geometry/multi/algorithms/within.hpp>
  17. #include <boost/geometry/geometries/point_xy.hpp>
  18. //#include <boost/geometry/extensions/gis/io/wkb/read_wkb.hpp>
  19. //#include <boost/geometry/extensions/gis/io/wkb/utility.hpp>
  20. #include <algorithms/test_difference.hpp>
  21. #include <algorithms/test_overlay.hpp>
  22. #include <algorithms/overlay/overlay_cases.hpp>
  23. #include <multi/algorithms/overlay/multi_overlay_cases.hpp>
  24. #ifdef HAVE_TTMATH
  25. # include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
  26. #endif
  27. template <typename P>
  28. void test_all()
  29. {
  30. typedef bg::model::box<P> box;
  31. typedef bg::model::polygon<P> polygon;
  32. typedef bg::model::ring<P> ring;
  33. bool const is_float =
  34. boost::is_same<typename bg::coordinate_type<P>::type, float>::value;
  35. test_one<polygon, polygon, polygon>("simplex_normal",
  36. simplex_normal[0], simplex_normal[1],
  37. 3, 3, 2.52636706856656,
  38. 3, 3, 3.52636706856656);
  39. test_one<polygon, polygon, polygon>("simplex_with_empty",
  40. simplex_normal[0], polygon_empty,
  41. 1, 4, 8.0,
  42. 0, 0, 0.0);
  43. test_one<polygon, polygon, polygon>(
  44. "star_ring", example_star, example_ring,
  45. 5, 22, 1.1901714,
  46. 5, 27, 1.6701714);
  47. test_one<polygon, polygon, polygon>("two_bends",
  48. two_bends[0], two_bends[1],
  49. 1, 7, 8.0,
  50. 1, 7, 8.0);
  51. test_one<polygon, polygon, polygon>("star_comb_15",
  52. star_comb_15[0], star_comb_15[1],
  53. 30, 150, 227.658275102812,
  54. 30, 150, 480.485775259312);
  55. test_one<polygon, polygon, polygon>("new_hole",
  56. new_hole[0], new_hole[1],
  57. 1, 10, 7.0,
  58. 1, 10, 14.0);
  59. test_one<polygon, polygon, polygon>("crossed",
  60. crossed[0], crossed[1],
  61. 1, 0, 19.5,
  62. 1, 0, 2.5);
  63. test_one<polygon, polygon, polygon>("disjoint",
  64. disjoint[0], disjoint[1],
  65. 1, 5, 1.0,
  66. 1, 5, 1.0);
  67. test_one<polygon, polygon, polygon>("distance_zero",
  68. distance_zero[0], distance_zero[1],
  69. 2, 0, 8.7048386,
  70. is_float ? 1 : 2, // The too small one is discarded for floating point
  71. 0, 0.0098387);
  72. test_one<polygon, polygon, polygon>("equal_holes_disjoint",
  73. equal_holes_disjoint[0], equal_holes_disjoint[1],
  74. 1, 0, 9.0,
  75. 1, 0, 9.0);
  76. test_one<polygon, polygon, polygon>("only_hole_intersections1",
  77. only_hole_intersections[0], only_hole_intersections[1],
  78. 2, 0, 1.9090909,
  79. 4, 0, 10.9090909);
  80. test_one<polygon, polygon, polygon>("only_hole_intersection2",
  81. only_hole_intersections[0], only_hole_intersections[2],
  82. 3, 0, 30.9090909,
  83. 4, 0, 10.9090909);
  84. test_one<polygon, polygon, polygon>("first_within_second",
  85. first_within_second[1], first_within_second[0],
  86. 1, 1, 24,
  87. 0, 0, 0);
  88. test_one<polygon, polygon, polygon>("fitting",
  89. fitting[0], fitting[1],
  90. 1, 0, 21.0,
  91. 1, 0, 4.0);
  92. test_one<polygon, polygon, polygon>("identical",
  93. identical[0], identical[1],
  94. 0, 0, 0.0,
  95. 0, 0, 0.0);
  96. test_one<polygon, polygon, polygon>("intersect_exterior_and_interiors_winded",
  97. intersect_exterior_and_interiors_winded[0], intersect_exterior_and_interiors_winded[1],
  98. 4, 0, 11.533333,
  99. 5, 0, 29.783333);
  100. test_one<polygon, polygon, polygon>("intersect_holes_intersect_and_disjoint",
  101. intersect_holes_intersect_and_disjoint[0], intersect_holes_intersect_and_disjoint[1],
  102. 2, 0, 15.75,
  103. 3, 0, 6.75);
  104. test_one<polygon, polygon, polygon>("intersect_holes_intersect_and_touch",
  105. intersect_holes_intersect_and_touch[0], intersect_holes_intersect_and_touch[1],
  106. 3, 0, 16.25,
  107. 3, 0, 6.25);
  108. test_one<polygon, polygon, polygon>("intersect_holes_new_ring",
  109. intersect_holes_new_ring[0], intersect_holes_new_ring[1],
  110. 3, 0, 9.8961,
  111. 4, 0, 121.8961, 0.01);
  112. test_one<polygon, polygon, polygon>("first_within_hole_of_second",
  113. first_within_hole_of_second[0], first_within_hole_of_second[1],
  114. 1, -1, 1,
  115. 1, -1, 16);
  116. test_one<polygon, polygon, polygon>("intersect_holes_disjoint",
  117. intersect_holes_disjoint[0], intersect_holes_disjoint[1],
  118. 2, 15, 16.0,
  119. 2, 15, 6.0);
  120. test_one<polygon, polygon, polygon>("intersect_holes_intersect",
  121. intersect_holes_intersect[0], intersect_holes_intersect[1],
  122. 2, 14, 15.75,
  123. 2, 14, 5.75);
  124. test_one<polygon, polygon, polygon>(
  125. "case4", case_4[0], case_4[1],
  126. 6, 22, 2.77878787878788,
  127. 4, 27, 4.77878787878788);
  128. test_one<polygon, polygon, polygon>(
  129. "case5", case_5[0], case_5[1],
  130. 8, 22, 2.43452380952381,
  131. 7, 27, 3.18452380952381);
  132. test_one<polygon, polygon, polygon>("winded",
  133. winded[0], winded[1],
  134. 3, 1, 61,
  135. 1, 0, 13);
  136. test_one<polygon, polygon, polygon>("within_holes_disjoint",
  137. within_holes_disjoint[0], within_holes_disjoint[1],
  138. 2, 1, 25,
  139. 1, 0, 1);
  140. test_one<polygon, polygon, polygon>("side_side",
  141. side_side[0], side_side[1],
  142. 1, 0, 1,
  143. 1, 0, 1);
  144. /*** TODO: self-tangencies for difference
  145. test_one<polygon, polygon, polygon>("wrapped_a",
  146. wrapped[0], wrapped[1],
  147. 3, 1, 61,
  148. 1, 0, 13);
  149. test_one<polygon, polygon, polygon>("wrapped_b",
  150. wrapped[0], wrapped[2],
  151. 3, 1, 61,
  152. 1, 0, 13);
  153. ***/
  154. #ifdef _MSC_VER
  155. {
  156. // Isovist (submitted by Brandon during Formal Review)
  157. std::string tn = string_from_type<typename bg::coordinate_type<polygon>::type>::name();
  158. test_one<polygon, polygon, polygon>("isovist",
  159. isovist1[0], isovist1[1],
  160. 4, 0, 0.279121891701124,
  161. 4, 0, 224.889211358929,
  162. 0.01);
  163. }
  164. #endif
  165. // Other combi's
  166. {
  167. test_one<polygon, polygon, ring>(
  168. "star_ring_ring", example_star, example_ring,
  169. 5, 22, 1.1901714, 5, 27, 1.6701714);
  170. test_one<polygon, ring, polygon>(
  171. "ring_star_ring", example_ring, example_star,
  172. 5, 22, 1.6701714, 5, 27, 1.1901714);
  173. static std::string const clip = "POLYGON((2.5 0.5,5.5 2.5))";
  174. test_one<polygon, box, ring>("star_box",
  175. clip, example_star,
  176. 4, 11, 2.833333, 4, 11, 0.833333);
  177. test_one<polygon, ring, box>("box_star",
  178. example_star, clip,
  179. 4, 11, 0.833333, 4, 11, 2.833333);
  180. }
  181. // Counter clockwise
  182. {
  183. typedef bg::model::polygon<P, false> polygon_ccw;
  184. test_one<polygon, polygon_ccw, polygon_ccw>(
  185. "star_ring_ccw", example_star, example_ring,
  186. 5, 22, 1.1901714, 5, 27, 1.6701714);
  187. test_one<polygon, polygon, polygon_ccw>(
  188. "star_ring_ccw1", example_star, example_ring,
  189. 5, 22, 1.1901714, 5, 27, 1.6701714);
  190. test_one<polygon, polygon_ccw, polygon>(
  191. "star_ring_ccw2", example_star, example_ring,
  192. 5, 22, 1.1901714, 5, 27, 1.6701714);
  193. }
  194. // Multi
  195. {
  196. typedef bg::model::multi_polygon<polygon> mp;
  197. static std::string const clip = "POLYGON((2 2,4 4))";
  198. test_one<polygon, box, mp>("simplex_multi_box_mp",
  199. clip, case_multi_simplex[0],
  200. 3, 11, 4.53333, 3, 11, 8.53333);
  201. test_one<polygon, mp, box>("simplex_multi_mp_box",
  202. case_multi_simplex[0], clip,
  203. 3, 11, 8.53333, 3, 11, 4.53333);
  204. }
  205. /***
  206. Experimental (cut), does not work:
  207. test_one<polygon, polygon, polygon>(
  208. "polygon_pseudo_line",
  209. "POLYGON((0 0,0 4,4 4,4 0,0 0))",
  210. "POLYGON((2 -2,2 -1,2 6,2 -2))",
  211. 5, 22, 1.1901714,
  212. 5, 27, 1.6701714);
  213. ***/
  214. }
  215. /*******
  216. // To be moved to another file
  217. template <typename T>
  218. void test_difference_parcel_precision()
  219. {
  220. typedef bg::model::d2::point_xy<T> point_type;
  221. typedef bg::model::polygon<point_type> polygon_type;
  222. typedef bg::model::linestring<point_type> linestring_type;
  223. typedef std::vector<boost::uint8_t> byte_vector;
  224. polygon_type parcel, buffer;
  225. {
  226. byte_vector wkb;
  227. bg::hex2wkb("0103000000010000001A00000023DBF97EE316064146B6F3FDD32513415A643BDFD216064175931804E225134196438B6C52150641C976BE9F34261341F6285C8F06150641022B871641261341F853E3A5D3140641E17A14AE50261341B81E85EBFB120641A4703D8A142713414E6210584D120641AC1C5A64602713414E621058431106414E621058C9271341A01A2FDD1B11064121B07268D8271341F4FDD478571006411904560ECF271341448B6CE7DD0F06418195438BC1271341F6285C8F6A0F06413BDF4F0DA72713410E2DB29D360F06416F1283C091271341E5D022DB070F0641F6285C0F7227134160E5D022DA0E06416F1283404F271341448B6CE7D30E0641F853E3A52427134154E3A59BE60E06411904568E07271341643BDF4F0C0F0641D7A3703DFC2613414A0C022B100F064125068115FB26134191ED7C3F310F0641B6F3FDD4F42613414C378941F11006414A0C022BA0261341EC51B81ECC1206413BDF4F0D40261341022B87167514064125068115F1251341B4C876BE8C160641C74B37897F25134121B07268C6160641508D976E7525134123DBF97EE316064146B6F3FDD3251341", std::back_inserter(wkb));
  228. bg::read_wkb(wkb.begin(), wkb.end(), parcel);
  229. }
  230. {
  231. byte_vector wkb;
  232. bg::hex2wkb("01030000000100000083000000000032FCD716064100009E998F25134100000706D81606410040A4998F2513410000DA0FD816064100C0E6998F2513410000A819D81606410080659A8F25134100806A23D816064100C0209B8F25134100801E2DD81606410080189C8F2513410000BE36D816064100404D9D8F25134100004540D816064100C0BE9E8F2513410000AF49D816064100806DA08F2513410000F752D8160641008059A28F2513410000195CD816064100C082A48F25134100800F65D81606410080E9A68F2513410000D66DD816064100408EA98F25134100006876D816064100C070AC8F2513410000C17ED8160641000091AF8F2513410080DC86D816064100C0EFB28F25134100009E8ED816064100C081B68F2513410080EC95D816064100803ABA8F2513410080C79CD8160641000018BE8F25134100002FA3D8160641008017C28F251341008022A9D8160641000037C68F2513410080A1AED8160641000074CA8F2513410000ACB3D81606410040CCCE8F251341000042B8D816064100403DD38F251341000062BCD81606410000C5D78F25134100000DC0D8160641000061DC8F251341000042C3D816064100000FE18F251341000001C6D81606410080CCE58F251341008049C8D8160641004097EA8F25134100001BCAD816064100006DEF8F251341008075CBD816064100804BF48F251341008058CCD8160641004030F98F2513410000C4CCD8160641000019FE8F2513410080B7CCD81606410080030390251341008032CCD81606410000ED0790251341000035CBD81606410000D40C902513410080BEC9D81606410040B511902513410000CFC7D816064100408F1690251341008065C5D816064100005F1B90251341008082C2D81606410080222090251341000025BFD81606410080D7249025134100004DBBD816064100807B29902513410080FAB6D816064100800C2E9025134100002DB2D816064100C08732902513410080E3ACD81606410000EB369025134100801EA7D81606410000343B902513410000DEA0D81606410080603F902513410080209AD816064100406E43902513410080209AC015064100406E43302613410080FC92C015064100004F473026134100008B8BC01506410040F64A302613410000D083C015064100C0634E302613410000D17BC0150641008097513026134100009273C015064100409154302613410000186BC015064100C050573026134100806762C01506410000D6593026134100808559C01506410000215C3026134100007650C01506410000315E3026134100003E47C015064100800660302613410000E23DC01506410000A1613026134100006734C015064100800063302613410080D12AC015064100C024643026134100002621C015064100800D653026134100006917C015064100C0BA653026134100809F0DC015064100402C66302613410000CE03C015064100006266302613410000F9F9BF15064100C05B6630261341000026F0BF1506410040196630261341000058E6BF15064100809A6530261341008095DCBF1506410040DF64302613410080E1D2BF1506410080E76330261341000042C9BF15064100C0B262302613410000BBBFBF1506410040416130261341000051B6BF1506410080925F30261341000009ADBF1506410080A65D302613410000E7A3BF15064100407D5B302613410080F09ABF150641008016593026134100002A92BF15064100C071563026134100009889BF15064100408F533026134100003F81BF15064100006F503026134100802379BF1506410040104D3026134100006271BF15064100407E49302613410080136ABF1506410080C5453026134100803863BF1506410000E841302613410000D15CBF1506410080E83D302613410080DD56BF1506410000C9393026134100805E51BF15064100008C35302613410000544CBF15064100C03331302613410000BE47BF15064100C0C22C3026134100009E43BF15064100003B28302613410000F33FBF15064100009F23302613410000BE3CBF1506410000F11E302613410000FF39BF1506410080331A302613410080B637BF15064100C06815302613410000E535BF150641000093103026134100808A34BF1506410080B40B302613410080A733BF15064100C0CF063026134100003C33BF1506410000E7013026134100804833BF1506410080FCFC2F2613410080CD33BF150641000013F82F2613410000CB34BF15064100002CF32F26134100804136BF15064100C04AEE2F26134100003138BF15064100C070E92F26134100809A3ABF1506410000A1E42F26134100807D3DBF1506410080DDDF2F2613410000DB40BF150641008028DB2F2613410000B344BF150641008084D62F26134100800549BF1506410080F3D12F2613410000D34DBF150641004078CD2F26134100801C53BF150641000015C92F2613410080E158BF1506410000CCC42F2613410000225FBF15064100809FC02F2613410080DF65BF15064100C091BC2F2613410080DF65D716064100C091BC8F2513410080036DD71606410000B1B88F25134100007574D716064100C009B58F2513410000307CD716064100409CB18F25134100002F84D7160641008068AE8F25134100006E8CD716064100C06EAB8F2513410000E894D71606410040AFA88F2513410080989DD716064100002AA68F25134100807AA6D71606410000DFA38F25134100008AAFD71606410000CFA18F2513410000C2B8D71606410080F99F8F25134100001EC2D716064100005F9E8F251341000099CBD71606410080FF9C8F25134100802ED5D71606410040DB9B8F2513410000DADED71606410080F29A8F251341000097E8D71606410040459A8F251341008060F2D716064100C0D3998F251341000032FCD716064100009E998F251341", std::back_inserter(wkb));
  233. bg::read_wkb(wkb.begin(), wkb.end(), buffer);
  234. }
  235. bg::correct(parcel);
  236. bg::correct(buffer);
  237. std::vector<polygon_type> pieces;
  238. bg::difference(parcel, buffer, pieces);
  239. std::vector<polygon_type> filled_out;
  240. bg::difference(parcel, pieces.back(), filled_out);
  241. #if defined(TEST_OUTPUT)
  242. std::cout << bg::area(parcel) << std::endl;
  243. std::cout << bg::area(buffer) << std::endl;
  244. std::cout << pieces.size() << std::endl;
  245. std::cout << bg::area(pieces.front()) << std::endl;
  246. std::cout << filled_out.size() << std::endl;
  247. std::cout << std::setprecision(16) << bg::wkt(filled_out.front()) << std::endl;
  248. std::cout << bg::wkt(filled_out.front()) << std::endl;
  249. std::cout << bg::area(filled_out.front()) << std::endl;
  250. std::cout << bg::perimeter(filled_out.front()) << std::endl;
  251. #endif
  252. #if defined(TEST_WITH_SVG)
  253. {
  254. linestring_type cut_line;
  255. bg::read_wkt("linestring(180955 313700,180920 313740)", cut_line);
  256. std::ostringstream filename;
  257. filename << "difference_precision_"
  258. << string_from_type<T>::name()
  259. << ".svg";
  260. std::ofstream svg(filename.str().c_str());
  261. bg::svg_mapper<point_type> mapper(svg, 500, 500);
  262. mapper.add(cut_line);
  263. //mapper.map(parcel, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:3");
  264. mapper.map(pieces.front(), "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:1");
  265. mapper.map(pieces.back(), "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:1");
  266. mapper.map(filled_out.front(), "fill-opacity:0.3;fill:rgb(51,51,153);stroke:rgb(51,51,153);stroke-width:3");
  267. mapper.map(cut_line, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:5;stroke-dasharray:1,7;stroke-linecap:round");
  268. //mapper.map(cut_line, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:2");
  269. }
  270. #endif
  271. }
  272. *****/
  273. int test_main(int, char* [])
  274. {
  275. //test_difference_parcel_precision<float>();
  276. //test_difference_parcel_precision<double>();
  277. test_all<bg::model::d2::point_xy<double> >();
  278. #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
  279. test_all<bg::model::d2::point_xy<float> >();
  280. #ifdef HAVE_TTMATH
  281. test_all<bg::model::d2::point_xy<ttmath_big> >();
  282. //test_difference_parcel_precision<ttmath_big>();
  283. #endif
  284. #endif
  285. return 0;
  286. }