equals.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2013, 2014.
  5. // Modifications copyright (c) 2013-2014 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_equals.hpp"
  11. #include <boost/type_traits/is_floating_point.hpp>
  12. #include <boost/geometry/geometries/geometries.hpp>
  13. #include <boost/geometry/geometries/point_xy.hpp>
  14. namespace bgm = bg::model;
  15. template <typename P>
  16. void test_pointlike()
  17. {
  18. typedef bgm::multi_point<P> mpt;
  19. test_geometry<P, P>("ptpt2d_1", "POINT(0 0)", "POINT(0 0)", true);
  20. test_geometry<P, P>("ptpt2d_2", "POINT(0 0)", "POINT(1 1)", false);
  21. test_geometry<P, mpt>("ptmpt2d_1", "POINT(0 0)", "MULTIPOINT(0 0)", true);
  22. test_geometry<P, mpt>("ptmpt2d_1", "POINT(0 0)", "MULTIPOINT(0 0, 1 1)", false);
  23. test_geometry<mpt, P>("mptpt2d_1", "MULTIPOINT(0 0)", "POINT(0 0)", true);
  24. test_geometry<mpt, P>("mptpt2d_1", "MULTIPOINT(0 0, 1 1)", "POINT(0 0)", false);
  25. test_geometry<mpt, mpt>("mptmpt2d_1", "MULTIPOINT(0 0, 1 1)", "MULTIPOINT(0 0, 1 1)", true);
  26. test_geometry<mpt, mpt>("mptmpt2d_1", "MULTIPOINT(0 0, 1 1)", "MULTIPOINT(0 0, 2 2)", false);
  27. test_geometry<mpt, mpt>("mptmpt2d_1", "MULTIPOINT(0 0, 1 1)", "MULTIPOINT(2 2, 3 3)", false);
  28. }
  29. template <typename P>
  30. void test_segment_segment()
  31. {
  32. typedef bgm::segment<P> seg;
  33. test_geometry<seg, seg>("seg2d_1", "LINESTRING(0 0, 3 3)", "LINESTRING(0 0, 3 3)", true);
  34. test_geometry<seg, seg>("seg2d_1", "LINESTRING(0 0, 3 3)", "LINESTRING(3 3, 0 0)", true);
  35. test_geometry<seg, seg>("seg2d_1", "LINESTRING(0 0, 3 3)", "LINESTRING(0 0, 1 1)", false);
  36. test_geometry<seg, seg>("seg2d_1", "LINESTRING(0 0, 3 3)", "LINESTRING(3 3, 2 2)", false);
  37. test_geometry<seg, seg>("seg2d_1", "LINESTRING(0 0, 3 3)", "LINESTRING(1 1, 4 4)", false);
  38. test_geometry<seg, seg>("seg2d_1", "LINESTRING(0 0, 3 3)", "LINESTRING(1 0, 2 0)", false);
  39. }
  40. template <typename P>
  41. void test_linestring_linestring()
  42. {
  43. typedef bgm::linestring<P> ls;
  44. test_geometry<ls, ls>("ls2d_1", "LINESTRING(1 1, 3 3)", "LINESTRING(3 3, 1 1)", true);
  45. test_geometry<ls, ls>("ls2d_2", "LINESTRING(1 1, 3 3, 2 5)", "LINESTRING(1 1, 2 2, 3 3, 2 5)", true);
  46. test_geometry<ls, ls>("ls2d_3", "LINESTRING(1 0, 3 3, 2 5)", "LINESTRING(1 1, 2 2, 3 3, 2 5)", false);
  47. test_geometry<ls, ls>("ls2d_4", "LINESTRING(1 0, 3 3, 2 5)", "LINESTRING(1 1, 3 3, 2 5)", false);
  48. test_geometry<ls, ls>("ls2d_5", "LINESTRING(0 5,5 5,10 5,10 0,5 0,5 5,5 10,10 10,15 10,15 5,10 5,10 10,10 15)",
  49. "LINESTRING(0 5,15 5,15 10,5 10,5 0,10 0,10 15)", true);
  50. test_geometry<ls, ls>("ls2d_6", "LINESTRING(0 5,5 5,10 5,10 10,5 10,5 5,5 0)", "LINESTRING(0 5,5 5,5 10,10 10,10 5,5 5,5 0)", true);
  51. test_geometry<ls, ls>("ls2d_7", "LINESTRING(0 5,10 5,10 10,5 10,5 0)", "LINESTRING(0 5,5 5,5 10,10 10,10 5,5 5,5 0)", true);
  52. test_geometry<ls, ls>("ls2d_8", "LINESTRING(0 0,5 0,5 0,6 0)", "LINESTRING(0 0,6 0)", true);
  53. test_geometry<ls, ls>("ls2d_seg", "LINESTRING(1 1,2 2)", "LINESTRING(1 1,2 2)", true);
  54. test_geometry<ls, ls>("ls2d_rev", "LINESTRING(1 1,2 2)", "LINESTRING(2 2,1 1)", true);
  55. test_geometry<ls, ls>("ls2d_spike", "LINESTRING(0 0,5 0,3 0,6 0)", "LINESTRING(0 0,6 0)", true);
  56. test_geometry<ls, ls>("ls2d_ring1", "LINESTRING(0 0,5 0,5 5,0 5,0 0)", "LINESTRING(5 5,0 5,0 0,5 0,5 5)", true);
  57. test_geometry<ls, ls>("ls2d_ring2", "LINESTRING(0 0,5 0,5 5,0 5,0 0)", "LINESTRING(5 5,5 0,0 0,0 5,5 5)", true);
  58. test_geometry<ls, ls>("ls2d_overl_ring1", "LINESTRING(0 0,5 0,5 5,0 5,0 0)", "LINESTRING(5 5,0 5,0 0,5 0,5 5,0 5)", true);
  59. test_geometry<ls, ls>("ls2d_overl_ring2", "LINESTRING(0 0,5 0,5 5,0 5,0 0)", "LINESTRING(5 5,5 0,0 0,0 5,5 5,5 0)", true);
  60. // https://svn.boost.org/trac/boost/ticket/10904
  61. if ( BOOST_GEOMETRY_CONDITION(
  62. boost::is_floating_point<typename bg::coordinate_type<ls>::type>::value ) )
  63. {
  64. test_geometry<ls, ls>("ls2d_small1",
  65. "LINESTRING(5.6956521739130430148634331999347 -0.60869565217391330413931882503675,5.5 -0.50000000000000066613381477509392)",
  66. "LINESTRING(5.5 -0.50000000000000066613381477509392,5.5 -0.5)",
  67. false);
  68. test_geometry<ls, ls>("ls2d_small2",
  69. "LINESTRING(-3.2333333333333333925452279800083 5.5999999999999978683717927196994,-3.2333333333333333925452279800083 5.5999999999999996447286321199499)",
  70. "LINESTRING(-3.2333333333333325043668082798831 5.5999999999999996447286321199499,-3.2333333333333333925452279800083 5.5999999999999996447286321199499)",
  71. false);
  72. }
  73. }
  74. template <typename P>
  75. void test_linestring_multilinestring()
  76. {
  77. typedef bgm::linestring<P> ls;
  78. typedef bgm::multi_linestring<ls> mls;
  79. test_geometry<ls, mls>("ls_mls_1", "LINESTRING(0 0,1 0,2 0)", "MULTILINESTRING((0 0,2 0))", true);
  80. test_geometry<ls, mls>("ls_mls_1", "LINESTRING(0 0,1 0,2 0)", "MULTILINESTRING((0 0,1 0),(1 0,2 0))", true);
  81. test_geometry<ls, mls>("ls_mls_1", "LINESTRING(0 0,2 0,4 0)", "MULTILINESTRING((0 0,2 0),(2 0,3 0),(3 0,4 0))", true);
  82. test_geometry<ls, mls>("ls_mls_1", "LINESTRING(0 0,2 0,4 0)", "MULTILINESTRING((0 0,2 0),(2 0,3 0),(2 0,3 0),(3 0,4 0))", true);
  83. test_geometry<ls, mls>("ls_mls_1", "LINESTRING(0 0,2 0,4 0)", "MULTILINESTRING((0 0,2 0),(3 0,4 0))", false);
  84. test_geometry<ls, mls>("ls_mls_spike", "LINESTRING(0 0,2 0,2 2,2 0,4 0)", "MULTILINESTRING((0 0,4 0),(2 2,2 0))", true);
  85. test_geometry<ls, mls>("ls_mls_spike", "LINESTRING(0 0,2 0,2 2,2 0,4 0)", "MULTILINESTRING((0 0,4 0),(2 2,2 -1))", false);
  86. test_geometry<ls, mls>("ls_mls_ring1", "LINESTRING(0 0,5 0,5 5,0 5,0 0)", "MULTILINESTRING((5 5,0 5,0 0),(0 0,5 0,5 5))", true);
  87. test_geometry<ls, mls>("ls_mls_ring2", "LINESTRING(0 0,5 0,5 5,0 5,0 0)", "MULTILINESTRING((5 5,5 0,0 0),(0 0,0 5,5 5))", true);
  88. test_geometry<ls, mls>("ls_mls_overl_ring1", "LINESTRING(0 0,5 0,5 5,0 5,0 0)", "MULTILINESTRING((5 5,0 5,0 0),(0 0,5 0,5 5,0 5))", true);
  89. test_geometry<ls, mls>("ls_mls_overl_ring2", "LINESTRING(0 0,5 0,5 5,0 5,0 0)", "MULTILINESTRING((5 5,5 0,0 0),(0 0,0 5,5 5,5 0))", true);
  90. }
  91. template <typename P>
  92. void test_multilinestring_multilinestring()
  93. {
  94. typedef bgm::linestring<P> ls;
  95. typedef bgm::multi_linestring<ls> mls;
  96. test_geometry<mls, mls>("ls_mls_mls",
  97. "MULTILINESTRING((0 5,10 5,10 10,5 10),(5 10,5 0,5 2),(5 2,5 5,0 5))",
  98. "MULTILINESTRING((5 5,0 5),(5 5,5 0),(10 10,10 5,5 5,5 10,10 10))",
  99. true);
  100. }
  101. template <typename P>
  102. void test_polygons()
  103. {
  104. typedef bg::model::polygon<P, true, true> poly_cw_c;
  105. typedef bg::model::polygon<P, true, false> poly_cw_o;
  106. typedef bg::model::polygon<P, false, true> poly_ccw_c;
  107. typedef bg::model::polygon<P, false, false> poly_ccw_o;
  108. typedef bg::model::box<P> box;
  109. std::string wkt1 = "POLYGON((-18 1, -23 1, -23 -3, -18 -3))";
  110. std::string wkt2 = "POLYGON((-23 1, -23 -3, -18 -3, -18 1))";
  111. test_geometry<poly_cw_c, poly_cw_c>("polys_cw_c_cw_c", wkt1, wkt2, true, true);
  112. test_geometry<poly_cw_c, poly_cw_o>("polys_cw_c_cw_o", wkt1, wkt2, true, true);
  113. test_geometry<poly_cw_c, poly_ccw_c>("polys_cw_c_ccw_c", wkt1, wkt2, true, true);
  114. test_geometry<poly_cw_c, poly_ccw_o>("polys_cw_c_ccw_o", wkt1, wkt2, true, true);
  115. test_geometry<poly_cw_c, box>("polys_cw_c_box", wkt1, wkt2, true, true);
  116. test_geometry<poly_cw_o, poly_cw_c>("polys_cw_o_cw_c", wkt1, wkt2, true, true);
  117. test_geometry<poly_cw_o, poly_cw_o>("polys_cw_o_cw_o", wkt1, wkt2, true, true);
  118. test_geometry<poly_cw_o, poly_ccw_c>("polys_cw_o_ccw_c", wkt1, wkt2, true, true);
  119. test_geometry<poly_cw_o, poly_ccw_o>("polys_cw_o_ccw_o", wkt1, wkt2, true, true);
  120. test_geometry<poly_cw_o, box>("polys_cw_o_box", wkt1, wkt2, true, true);
  121. test_geometry<poly_ccw_c, poly_cw_c>("polys_ccw_c_cw_c", wkt1, wkt2, true, true);
  122. test_geometry<poly_ccw_c, poly_cw_o>("polys_ccw_c_cw_o", wkt1, wkt2, true, true);
  123. test_geometry<poly_ccw_c, poly_ccw_c>("polys_ccw_c_ccw_c", wkt1, wkt2, true, true);
  124. test_geometry<poly_ccw_c, poly_ccw_o>("polys_ccw_c_ccw_o", wkt1, wkt2, true, true);
  125. test_geometry<poly_ccw_c, box>("polys_cw_o_box", wkt1, wkt2, true, true);
  126. test_geometry<poly_ccw_o, poly_cw_c>("polys_ccw_o_cw_c", wkt1, wkt2, true, true);
  127. test_geometry<poly_ccw_o, poly_cw_o>("polys_ccw_o_cw_o", wkt1, wkt2, true, true);
  128. test_geometry<poly_ccw_o, poly_ccw_c>("polys_ccw_o_ccw_c", wkt1, wkt2, true, true);
  129. test_geometry<poly_ccw_o, poly_ccw_o>("polys_ccw_o_ccw_o", wkt1, wkt2, true, true);
  130. test_geometry<poly_ccw_o, box>("polys_ccw_o_box", wkt1, wkt2, true, true);
  131. }
  132. template <typename P>
  133. void test_all()
  134. {
  135. typedef bg::model::box<P> box;
  136. typedef bg::model::ring<P> ring;
  137. typedef bg::model::polygon<P> polygon;
  138. //typedef bg::model::linestring<P> linestring;
  139. std::string case_p1 = "POLYGON((0 0,0 2,2 2,0 0))";
  140. test_geometry<P, P>("p1", "POINT(1 1)", "POINT(1 1)", true);
  141. test_geometry<P, P>("p2", "POINT(1 1)", "POINT(1 2)", false);
  142. test_geometry<box, box>("b1", "BOX(1 1,2 2)", "BOX(1 2,2 2)", false);
  143. test_geometry<box, box>("b1", "BOX(1 2,3 4)", "BOX(1 2,3 4)", true);
  144. // Completely equal
  145. test_geometry<ring, ring>("poly_eq", case_p1, case_p1, true);
  146. // Shifted
  147. test_geometry<ring, ring>("poly_sh", "POLYGON((2 2,0 0,0 2,2 2))", case_p1, true);
  148. test_geometry<polygon, polygon>("poly_sh2", case_p1, "POLYGON((0 2,2 2,0 0,0 2))", true);
  149. // Extra coordinate
  150. test_geometry<ring, ring>("poly_extra", case_p1, "POLYGON((0 0,0 2,2 2,1 1,0 0))", true);
  151. // Shifted + extra (redundant) coordinate
  152. test_geometry<ring, ring>("poly_shifted_extra1", "POLYGON((2 2,1 1,0 0,0 2,2 2))", case_p1, true);
  153. // Shifted + extra (redundant) coordinate being first/last point
  154. test_geometry<ring, ring>("poly_shifted_extra2", "POLYGON((1 1,0 0,0 2,2 2,1 1))", case_p1, true);
  155. // Degenerate (duplicate) points
  156. test_geometry<ring, ring>("poly_degenerate", "POLYGON((0 0,0 2,2 2,2 2,0 0))", "POLYGON((0 0,0 2,0 2,2 2,0 0))", true);
  157. // Two different bends, same area, unequal
  158. test_geometry<ring, ring>("poly_bends",
  159. "POLYGON((4 0,5 3,8 4,7 7,4 8,0 4,4 0))",
  160. "POLYGON((4 0,7 1,8 4,5 5,4 8,0 4,4 0))", false);
  161. // Unequal (but same area)
  162. test_geometry<ring, ring>("poly_uneq", case_p1, "POLYGON((1 1,1 3,3 3,1 1))", false);
  163. // One having hole
  164. test_geometry<polygon, polygon>("poly_hole", "POLYGON((0 0,0 4,4 4,0 0))", "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1))", false);
  165. // Both having holes
  166. test_geometry<polygon, polygon>("poly_holes",
  167. "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1))",
  168. "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1))", true);
  169. // Both having holes, outer equal, inner not equal
  170. test_geometry<polygon, polygon>("poly_uneq_holes",
  171. "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1))",
  172. "POLYGON((0 0,0 4,4 4,0 0),(2 2,3 2,3 3,2 3,2 2))", false);
  173. // Both having 2 holes, equal but in different order
  174. test_geometry<polygon, polygon>("poly_holes_diff_order",
  175. "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1),(2 2,3 2,3 3,2 3,2 2))",
  176. "POLYGON((0 0,0 4,4 4,0 0),(2 2,3 2,3 3,2 3,2 2),(1 1,2 1,2 2,1 2,1 1))", true);
  177. // Both having 3 holes, equal but in different order
  178. test_geometry<polygon, polygon>("poly_holes_diff_order_3",
  179. "POLYGON((0 0,0 10,10 10,0 0),(1 1,2 1,2 2,1 2,1 1),(4 1,5 1,5 2,4 2,4 1),(2 2,3 2,3 3,2 3,2 2))",
  180. "POLYGON((0 0,0 10,10 10,0 0),(4 1,5 1,5 2,4 2,4 1),(2 2,3 2,3 3,2 3,2 2),(1 1,2 1,2 2,1 2,1 1))", true);
  181. // polygon/ring vv
  182. test_geometry<polygon, ring>("poly_sh2_pr", case_p1, case_p1, true);
  183. test_geometry<ring, polygon>("poly_sh2_rp", case_p1, case_p1, true);
  184. // box/ring/poly
  185. test_geometry<box, ring>("boxring1", "BOX(1 1,2 2)", "POLYGON((1 1,1 2,2 2,2 1,1 1))", true);
  186. test_geometry<ring, box>("boxring2", "POLYGON((1 1,1 2,2 2,2 1,1 1))", "BOX(1 1,2 2)", true);
  187. test_geometry<box, polygon>("boxpoly1", "BOX(1 1,2 2)", "POLYGON((1 1,1 2,2 2,2 1,1 1))", true);
  188. test_geometry<polygon, box>("boxpoly2", "POLYGON((1 1,1 2,2 2,2 1,1 1))", "BOX(1 1,2 2)", true);
  189. test_geometry<polygon, box>("boxpoly2", "POLYGON((1 1,1 2,2 2,2 1,1 1))", "BOX(1 1,2 3)", false);
  190. test_geometry<polygon, polygon>("poly_holes_shifted_points",
  191. "POLYGON((0 0,0 3,3 3,3 0,0 0),(1 1,2 1,2 2,1 2,1 1))",
  192. "POLYGON((0 0,0 3,3 3,3 0,0 0),(2 2,1 2,1 1,2 1,2 2))", true);
  193. test_pointlike<P>();
  194. test_segment_segment<P>();
  195. test_linestring_linestring<P>();
  196. test_linestring_multilinestring<P>();
  197. test_multilinestring_multilinestring<P>();
  198. test_polygons<P>();
  199. }
  200. template <typename T>
  201. void verify()
  202. {
  203. T dxn1, dyn1, dxn2, dyn2;
  204. {
  205. T x1 = "0", y1 = "0", x2 = "3", y2 = "3";
  206. T dx = x2 - x1, dy = y2 - y1;
  207. T mag = sqrt(dx * dx + dy * dy);
  208. dxn1 = dx / mag;
  209. dyn1 = dy / mag;
  210. }
  211. {
  212. T x1 = "0", y1 = "0", x2 = "1", y2 = "1";
  213. T dx = x2 - x1, dy = y2 - y1;
  214. T mag = sqrt(dx * dx + dy * dy);
  215. dxn2 = dx / mag;
  216. dyn2 = dy / mag;
  217. }
  218. if (dxn1 == dxn2 && dyn1 == dyn2)
  219. {
  220. //std::cout << "vectors are equal, using ==" << std::endl;
  221. }
  222. if (boost::geometry::math::equals(dxn1, dxn2)
  223. && boost::geometry::math::equals(dyn1, dyn2))
  224. {
  225. //std::cout << "vectors are equal, using bg::math::equals" << std::endl;
  226. }
  227. bool equals = boost::geometry::math::equals_with_epsilon(dxn1, dxn2)
  228. && boost::geometry::math::equals_with_epsilon(dyn1, dyn2);
  229. if (equals)
  230. {
  231. //std::cout << "vectors are equal, using bg::math::equals_with_epsilon" << std::endl;
  232. }
  233. BOOST_CHECK_EQUAL(equals, true);
  234. }
  235. int test_main( int , char* [] )
  236. {
  237. //verify<double>();
  238. #if defined(HAVE_TTMATH)
  239. verify<ttmath_big>();
  240. #endif
  241. test_all<bg::model::d2::point_xy<int> >();
  242. test_all<bg::model::d2::point_xy<double> >();
  243. #if defined(HAVE_TTMATH)
  244. test_all<bg::model::d2::point_xy<ttmath_big> >();
  245. #endif
  246. return 0;
  247. }