/Src/Dependencies/Boost/libs/geometry/test/multi/algorithms/multi_distance.cpp

http://hadesmem.googlecode.com/ · C++ · 150 lines · 103 code · 36 blank · 11 comment · 0 complexity · 28a7f0dab8340fb932166b6899f61862 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 <string>
  8. #include <geometry_test_common.hpp>
  9. #include <boost/geometry/algorithms/distance.hpp>
  10. #include <boost/geometry/domains/gis/io/wkt/read_wkt.hpp>
  11. #include <boost/geometry/strategies/strategies.hpp>
  12. #include <boost/geometry/multi/algorithms/distance.hpp>
  13. #include <boost/geometry/multi/geometries/multi_point.hpp>
  14. #include <boost/geometry/multi/geometries/multi_linestring.hpp>
  15. #include <boost/geometry/multi/geometries/multi_polygon.hpp>
  16. #include <boost/geometry/domains/gis/io/wkt/read_wkt_multi.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 <test_common/test_point.hpp>
  22. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  23. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  24. template <typename Geometry1, typename Geometry2>
  25. void test_distance(std::string const& wkt1, std::string const& wkt2, double expected)
  26. {
  27. Geometry1 g1;
  28. Geometry2 g2;
  29. bg::read_wkt(wkt1, g1);
  30. bg::read_wkt(wkt2, g2);
  31. typename bg::default_distance_result<Geometry1, Geometry2>::type d = bg::distance(g1, g2);
  32. BOOST_CHECK_CLOSE(d, expected, 0.0001);
  33. }
  34. template <typename Geometry1, typename Geometry2, typename Strategy>
  35. void test_distance(Strategy const& strategy, std::string const& wkt1,
  36. std::string const& wkt2, double expected)
  37. {
  38. Geometry1 g1;
  39. Geometry2 g2;
  40. bg::read_wkt(wkt1, g1);
  41. bg::read_wkt(wkt2, g2);
  42. typename bg::default_distance_result<Geometry1, Geometry2>::type d = bg::distance(g1, g2, strategy);
  43. BOOST_CHECK_CLOSE(d, expected, 0.0001);
  44. }
  45. template <typename P>
  46. void test_2d()
  47. {
  48. typedef bg::model::multi_point<P> mp;
  49. typedef bg::model::multi_linestring<bg::model::linestring<P> > ml;
  50. test_distance<P, P>("POINT(0 0)", "POINT(1 1)", sqrt(2.0));
  51. test_distance<P, mp>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
  52. test_distance<mp, P>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
  53. test_distance<mp, mp>("MULTIPOINT((1 1),(1 0),(0 2))", "MULTIPOINT((2 2),(2 3))", sqrt(2.0));
  54. test_distance<P, ml>("POINT(0 0)", "MULTILINESTRING((1 1,2 2),(1 0,2 0),(0 2,0 3))", 1.0);
  55. test_distance<ml, P>("MULTILINESTRING((1 1,2 2),(1 0,2 0),(0 2,0 3))", "POINT(0 0)", 1.0);
  56. test_distance<ml, mp>("MULTILINESTRING((1 1,2 2),(1 0,2 0),(0 2,0 3))", "MULTIPOINT((0 0),(1 1))", 0.0);
  57. // Test with a strategy
  58. bg::strategy::distance::pythagoras<P, P> pyth;
  59. test_distance<P, P>(pyth, "POINT(0 0)", "POINT(1 1)", sqrt(2.0));
  60. test_distance<P, mp>(pyth, "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
  61. test_distance<mp, P>(pyth, "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
  62. }
  63. template <typename P>
  64. void test_3d()
  65. {
  66. typedef bg::model::multi_point<P> mp;
  67. test_distance<P, P>("POINT(0 0 0)", "POINT(1 1 1)", sqrt(3.0));
  68. test_distance<P, mp>("POINT(0 0 0)", "MULTIPOINT((1 1 1),(1 0 0),(0 1 2))", 1.0);
  69. test_distance<mp, mp>("MULTIPOINT((1 1 1),(1 0 0),(0 0 2))", "MULTIPOINT((2 2 2),(2 3 4))", sqrt(3.0));
  70. }
  71. template <typename P1, typename P2>
  72. void test_mixed()
  73. {
  74. typedef bg::model::multi_point<P1> mp1;
  75. typedef bg::model::multi_point<P2> mp2;
  76. test_distance<P1, P2>("POINT(0 0)", "POINT(1 1)", sqrt(2.0));
  77. test_distance<P1, mp1>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
  78. test_distance<P1, mp2>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
  79. test_distance<P2, mp1>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
  80. test_distance<P2, mp2>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
  81. // Test automatic reversal
  82. test_distance<mp1, P1>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
  83. test_distance<mp1, P2>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
  84. test_distance<mp2, P1>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
  85. test_distance<mp2, P2>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
  86. // Test multi-multi using different point types for each
  87. test_distance<mp1, mp2>("MULTIPOINT((1 1),(1 0),(0 2))", "MULTIPOINT((2 2),(2 3))", sqrt(2.0));
  88. // Test with a strategy
  89. using namespace bg::strategy::distance;
  90. test_distance<P1, P2>(pythagoras<P1, P2>(), "POINT(0 0)", "POINT(1 1)", sqrt(2.0));
  91. test_distance<P1, mp1>(pythagoras<P1, P1>(), "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
  92. test_distance<P1, mp2>(pythagoras<P1, P2>(), "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
  93. test_distance<P2, mp1>(pythagoras<P2, P1>(), "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
  94. test_distance<P2, mp2>(pythagoras<P2, P2>(), "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
  95. // Most interesting: reversal AND a strategy (note that the stategy must be reversed automatically
  96. test_distance<mp1, P1>(pythagoras<P1, P1>(), "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
  97. test_distance<mp1, P2>(pythagoras<P1, P2>(), "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
  98. test_distance<mp2, P1>(pythagoras<P2, P1>(), "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
  99. test_distance<mp2, P2>(pythagoras<P2, P2>(), "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
  100. }
  101. int test_main( int , char* [] )
  102. {
  103. test_2d<boost::tuple<float, float> >();
  104. test_2d<bg::model::d2::point_xy<float> >();
  105. test_2d<bg::model::d2::point_xy<double> >();
  106. test_3d<boost::tuple<float, float, float> >();
  107. test_3d<bg::model::point<double, 3, bg::cs::cartesian> >();
  108. test_mixed<bg::model::d2::point_xy<float>, bg::model::d2::point_xy<double> >();
  109. #ifdef HAVE_TTMATH
  110. test_2d<bg::model::d2::point_xy<ttmath_big> >();
  111. test_mixed<bg::model::d2::point_xy<ttmath_big>, bg::model::d2::point_xy<double> >();
  112. #endif
  113. return 0;
  114. }