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

http://hadesmem.googlecode.com/ · C++ · 162 lines · 92 code · 40 blank · 30 comment · 8 complexity · f7e9b15ef2206cca748d1cbce14410ce MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. //
  3. // Copyright (c) 2007-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. #include <algorithms/test_equals.hpp>
  8. #include <boost/geometry/geometries/geometries.hpp>
  9. #include <boost/geometry/geometries/point_xy.hpp>
  10. template <typename P>
  11. void test_all()
  12. {
  13. typedef bg::model::box<P> box;
  14. typedef bg::model::ring<P> ring;
  15. typedef bg::model::polygon<P> polygon;
  16. typedef bg::model::linestring<P> linestring;
  17. std::string case_p1 = "POLYGON((0 0,0 2,2 2,0 0))";
  18. test_geometry<P, P>("p1", "POINT(1 1)", "POINT(1 1)", true);
  19. test_geometry<P, P>("p2", "POINT(1 1)", "POINT(1 2)", false);
  20. test_geometry<box, box>("b1", "BOX(1 1,2 2)", "BOX(1 2,2 2)", false);
  21. test_geometry<box, box>("b1", "BOX(1 2,3 4)", "BOX(1 2,3 4)", true);
  22. // Completely equal
  23. test_geometry<ring, ring>("poly_eq", case_p1, case_p1, true);
  24. // Shifted
  25. test_geometry<ring, ring>("poly_sh", "POLYGON((2 2,0 0,0 2,2 2))", case_p1, true);
  26. test_geometry<polygon, polygon>("poly_sh2", case_p1, "POLYGON((0 2,2 2,0 0,0 2))", true);
  27. // Extra coordinate
  28. test_geometry<ring, ring>("poly_extra", case_p1, "POLYGON((0 0,0 2,2 2,1 1,0 0))", true);
  29. // Shifted + extra (redundant) coordinate
  30. test_geometry<ring, ring>("poly_shifted_extra1", "POLYGON((2 2,1 1,0 0,0 2,2 2))", case_p1, true);
  31. // Shifted + extra (redundant) coordinate being first/last point
  32. test_geometry<ring, ring>("poly_shifted_extra2", "POLYGON((1 1,0 0,0 2,2 2,1 1))", case_p1, true);
  33. // Degenerate (duplicate) points
  34. 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);
  35. // Two different bends, same area, unequal
  36. test_geometry<ring, ring>("poly_bends",
  37. "POLYGON((4 0,5 3,8 4,7 7,4 8,0 4,4 0))",
  38. "POLYGON((4 0,7 1,8 4,5 5,4 8,0 4,4 0))", false);
  39. // Unequal (but same area)
  40. test_geometry<ring, ring>("poly_uneq", case_p1, "POLYGON((1 1,1 3,3 3,1 1))", false);
  41. // One having hole
  42. 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);
  43. // Both having holes
  44. test_geometry<polygon, polygon>("poly_holes",
  45. "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1))",
  46. "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1))", true);
  47. // Both having holes, outer equal, inner not equal
  48. test_geometry<polygon, polygon>("poly_uneq_holes",
  49. "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1))",
  50. "POLYGON((0 0,0 4,4 4,0 0),(2 2,3 2,3 3,2 3,2 2))", false);
  51. // Both having 2 holes, equal but in different order
  52. test_geometry<polygon, polygon>("poly_holes_diff_order",
  53. "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))",
  54. "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);
  55. // Both having 3 holes, equal but in different order
  56. test_geometry<polygon, polygon>("poly_holes_diff_order_3",
  57. "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))",
  58. "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);
  59. // polygon/ring vv
  60. test_geometry<polygon, ring>("poly_sh2_pr", case_p1, case_p1, true);
  61. test_geometry<ring, polygon>("poly_sh2_rp", case_p1, case_p1, true);
  62. // box/ring/poly
  63. test_geometry<box, ring>("boxring1", "BOX(1 1,2 2)", "POLYGON((1 1,1 2,2 2,2 1,1 1))", true);
  64. test_geometry<ring, box>("boxring2", "POLYGON((1 1,1 2,2 2,2 1,1 1))", "BOX(1 1,2 2)", true);
  65. test_geometry<box, polygon>("boxpoly1", "BOX(1 1,2 2)", "POLYGON((1 1,1 2,2 2,2 1,1 1))", true);
  66. test_geometry<polygon, box>("boxpoly2", "POLYGON((1 1,1 2,2 2,2 1,1 1))", "BOX(1 1,2 2)", true);
  67. test_geometry<polygon, box>("boxpoly2", "POLYGON((1 1,1 2,2 2,2 1,1 1))", "BOX(1 1,2 3)", false);
  68. // linestring/linestring
  69. // simplex
  70. test_geometry<linestring, linestring>("ls1", "LINESTRING(1 1,2 2)", "LINESTRING(1 1,2 2)", true);
  71. // REVERSE linestring
  72. // Is this equal? To be decided.
  73. //test_geometry<linestring, linestring>("ls1", "LINESTRING(1 1,2 2)", "LINESTRING(2 2,1 1)", true);
  74. }
  75. template <typename T>
  76. void verify()
  77. {
  78. T dxn1, dyn1, dxn2, dyn2;
  79. {
  80. T x1 = "0", y1 = "0", x2 = "3", y2 = "3";
  81. T dx = x2 - x1, dy = y2 - y1;
  82. T mag = sqrt(dx * dx + dy * dy);
  83. dxn1 = dx / mag;
  84. dyn1 = dy / mag;
  85. }
  86. {
  87. T x1 = "0", y1 = "0", x2 = "1", y2 = "1";
  88. T dx = x2 - x1, dy = y2 - y1;
  89. T mag = sqrt(dx * dx + dy * dy);
  90. dxn2 = dx / mag;
  91. dyn2 = dy / mag;
  92. }
  93. if (dxn1 == dxn2 && dyn1 == dyn2)
  94. {
  95. //std::cout << "vectors are equal, using ==" << std::endl;
  96. }
  97. if (boost::geometry::math::equals(dxn1, dxn2)
  98. && boost::geometry::math::equals(dyn1, dyn2))
  99. {
  100. //std::cout << "vectors are equal, using bg::math::equals" << std::endl;
  101. }
  102. bool equals = boost::geometry::math::equals_with_epsilon(dxn1, dxn2)
  103. && boost::geometry::math::equals_with_epsilon(dyn1, dyn2);
  104. if (equals)
  105. {
  106. //std::cout << "vectors are equal, using bg::math::equals_with_epsilon" << std::endl;
  107. }
  108. BOOST_CHECK_EQUAL(equals, true);
  109. }
  110. int test_main( int , char* [] )
  111. {
  112. //verify<double>();
  113. #if defined(HAVE_TTMATH)
  114. verify<ttmath_big>();
  115. #endif
  116. test_all<bg::model::d2::point_xy<int> >();
  117. test_all<bg::model::d2::point_xy<double> >();
  118. #if defined(HAVE_TTMATH)
  119. test_all<bg::model::d2::point_xy<ttmath_big> >();
  120. #endif
  121. return 0;
  122. }