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

http://hadesmem.googlecode.com/ · C++ · 146 lines · 94 code · 38 blank · 14 comment · 0 complexity · c16114ac514a3a3452acacbc432b4bea MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2011 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2011 Mateusz Loskot, London, UK.
  6. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  7. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #include <sstream>
  12. #include <boost/mpl/if.hpp>
  13. #include <geometry_test_common.hpp>
  14. #include <boost/geometry/geometry.hpp>
  15. #include <boost/geometry/geometries/point_xy.hpp>
  16. #include <boost/geometry/geometries/linestring.hpp>
  17. #include <boost/geometry/algorithms/comparable_distance.hpp>
  18. #include <boost/geometry/strategies/strategies.hpp>
  19. #include <boost/geometry/geometries/geometries.hpp>
  20. template <typename P>
  21. void test_distance_result()
  22. {
  23. typedef typename bg::default_distance_result<P, P>::type distance_type;
  24. P p1 = bg::make<P>(0, 0);
  25. P p2 = bg::make<P>(3, 0);
  26. P p3 = bg::make<P>(0, 4);
  27. distance_type dr12 = bg::comparable_distance(p1, p2);
  28. distance_type dr13 = bg::comparable_distance(p1, p3);
  29. distance_type dr23 = bg::comparable_distance(p2, p3);
  30. BOOST_CHECK_CLOSE(dr12, 9.000, 0.001);
  31. BOOST_CHECK_CLOSE(dr13, 16.000, 0.001);
  32. BOOST_CHECK_CLOSE(dr23, 25.000, 0.001);
  33. }
  34. template <typename P>
  35. void test_distance_point()
  36. {
  37. P p1;
  38. bg::set<0>(p1, 1);
  39. bg::set<1>(p1, 1);
  40. P p2;
  41. bg::set<0>(p2, 2);
  42. bg::set<1>(p2, 2);
  43. typename bg::coordinate_type<P>::type d = bg::comparable_distance(p1, p2);
  44. BOOST_CHECK_CLOSE(d, 2.0, 0.001);
  45. }
  46. template <typename P>
  47. void test_distance_segment()
  48. {
  49. typedef typename bg::coordinate_type<P>::type coordinate_type;
  50. P s1 = bg::make<P>(2, 2);
  51. P s2 = bg::make<P>(3, 3);
  52. // Check points left, right, projected-left, projected-right, on segment
  53. P p1 = bg::make<P>(0, 0);
  54. P p2 = bg::make<P>(4, 4);
  55. P p3 = bg::make<P>(2.4, 2.6);
  56. P p4 = bg::make<P>(2.6, 2.4);
  57. P p5 = bg::make<P>(2.5, 2.5);
  58. bg::model::referring_segment<P const> const seg(s1, s2);
  59. coordinate_type d1 = bg::comparable_distance(p1, seg); BOOST_CHECK_CLOSE(d1, 8.0, 0.001);
  60. coordinate_type d2 = bg::comparable_distance(p2, seg); BOOST_CHECK_CLOSE(d2, 2.0, 0.001);
  61. coordinate_type d3 = bg::comparable_distance(p3, seg); BOOST_CHECK_CLOSE(d3, 0.02, 0.001);
  62. coordinate_type d4 = bg::comparable_distance(p4, seg); BOOST_CHECK_CLOSE(d4, 0.02, 0.001);
  63. coordinate_type d5 = bg::comparable_distance(p5, seg); BOOST_CHECK_CLOSE(d5, 0.0, 0.001);
  64. // Reverse case
  65. coordinate_type dr1 = bg::comparable_distance(seg, p1); BOOST_CHECK_CLOSE(dr1, d1, 0.001);
  66. coordinate_type dr2 = bg::comparable_distance(seg, p2); BOOST_CHECK_CLOSE(dr2, d2, 0.001);
  67. }
  68. template <typename P>
  69. void test_distance_linestring()
  70. {
  71. bg::model::linestring<P> points;
  72. points.push_back(bg::make<P>(1, 1));
  73. points.push_back(bg::make<P>(3, 3));
  74. P p = bg::make<P>(2, 1);
  75. typename bg::coordinate_type<P>::type d = bg::comparable_distance(p, points);
  76. BOOST_CHECK_CLOSE(d, 0.70710678, 0.001);
  77. p = bg::make<P>(5, 5);
  78. d = bg::comparable_distance(p, points);
  79. BOOST_CHECK_CLOSE(d, 2.828427, 0.001);
  80. bg::model::linestring<P> line;
  81. line.push_back(bg::make<P>(1,1));
  82. line.push_back(bg::make<P>(2,2));
  83. line.push_back(bg::make<P>(3,3));
  84. p = bg::make<P>(5, 5);
  85. d = bg::comparable_distance(p, line);
  86. BOOST_CHECK_CLOSE(d, 2.828427, 0.001);
  87. // Reverse case
  88. d = bg::comparable_distance(line, p);
  89. BOOST_CHECK_CLOSE(d, 2.828427, 0.001);
  90. }
  91. template <typename P>
  92. void test_all()
  93. {
  94. test_distance_result<P>();
  95. test_distance_point<P>();
  96. test_distance_segment<P>();
  97. test_distance_linestring<P>();
  98. }
  99. int test_main(int, char* [])
  100. {
  101. //test_all<bg::model::d2::point_xy<int> >();
  102. test_all<bg::model::d2::point_xy<float> >();
  103. test_all<bg::model::d2::point_xy<double> >();
  104. #ifdef HAVE_TTMATH
  105. test_all<bg::model::d2::point_xy<ttmath_big> >();
  106. #endif
  107. return 0;
  108. }