/libs/geometry/test/strategies/projected_point.cpp

https://github.com/delalond/boost_1_54_0-bgq · C++ · 188 lines · 120 code · 43 blank · 25 comment · 0 complexity · 8d51a792c9ec6fdc4ea10fcd1265a709 MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2012 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 <geometry_test_common.hpp>
  12. #include <boost/geometry/strategies/cartesian/distance_projected_point.hpp>
  13. #include <boost/geometry/strategies/concepts/distance_concept.hpp>
  14. #include <boost/geometry/io/wkt/read.hpp>
  15. #include <boost/geometry/geometries/point.hpp>
  16. #include <boost/geometry/geometries/adapted/c_array.hpp>
  17. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  18. #include <test_common/test_point.hpp>
  19. #ifdef HAVE_TTMATH
  20. # include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
  21. #endif
  22. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  23. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  24. template <typename P, typename PS, typename CalculationType>
  25. void test_services()
  26. {
  27. PS p1, p2;
  28. bg::assign_values(p1, 0, 0);
  29. bg::assign_values(p2, 0, 4);
  30. P p;
  31. bg::assign_values(p, 2, 0);
  32. CalculationType const sqr_expected = 4;
  33. CalculationType const expected = 2;
  34. namespace bgsd = bg::strategy::distance;
  35. namespace services = bg::strategy::distance::services;
  36. // 1: normal, calculate distance:
  37. typedef bgsd::projected_point<P, PS, CalculationType> strategy_type;
  38. BOOST_CONCEPT_ASSERT( (bg::concept::PointSegmentDistanceStrategy<strategy_type>) );
  39. typedef typename services::return_type<strategy_type>::type return_type;
  40. strategy_type strategy;
  41. return_type result = strategy.apply(p, p1, p2);
  42. BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
  43. // 2: "similar" to construct a similar strategy (similar but with other template-parameters) for, e.g., the reverse P2/P1
  44. // 2a: similar_type:
  45. typedef typename services::similar_type<strategy_type, P, PS>::type similar_type;
  46. // 2b: get_similar
  47. similar_type similar = services::get_similar<strategy_type, P, PS>::apply(strategy);
  48. result = similar.apply(p, p1, p2);
  49. BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
  50. // 3: "comparable" to construct a "comparable strategy" for P1/P2
  51. // a "comparable strategy" is a strategy which does not calculate the exact distance, but
  52. // which returns results which can be mutually compared (e.g. avoid sqrt)
  53. // 3a: "comparable_type"
  54. typedef typename services::comparable_type<strategy_type>::type comparable_type;
  55. // 3b: "get_comparable"
  56. comparable_type comparable = bgsd::services::get_comparable<strategy_type>::apply(strategy);
  57. return_type c_result = comparable.apply(p, p1, p2);
  58. BOOST_CHECK_CLOSE(c_result, return_type(sqr_expected), 0.001);
  59. }
  60. template <typename P1, typename P2, typename T>
  61. void test_all_2d(std::string const& wkt_p,
  62. std::string const& wkt_sp1,
  63. std::string const& wkt_sp2,
  64. T expected_distance)
  65. {
  66. P1 p;
  67. P2 sp1, sp2;
  68. bg::read_wkt(wkt_p, p);
  69. bg::read_wkt(wkt_sp1, sp1);
  70. bg::read_wkt(wkt_sp2, sp2);
  71. {
  72. typedef bg::strategy::distance::projected_point
  73. <
  74. P1,
  75. P2
  76. > strategy_type;
  77. BOOST_CONCEPT_ASSERT
  78. (
  79. (bg::concept::PointSegmentDistanceStrategy<strategy_type>)
  80. );
  81. strategy_type strategy;
  82. typedef typename bg::strategy::distance::services::return_type<strategy_type>::type return_type;
  83. return_type d = strategy.apply(p, sp1, sp2);
  84. BOOST_CHECK_CLOSE(d, expected_distance, 0.001);
  85. }
  86. // Test combination with the comparable strategy
  87. {
  88. typedef bg::strategy::distance::projected_point
  89. <
  90. P1,
  91. P2,
  92. void,
  93. bg::strategy::distance::comparable::pythagoras<P1, P2>
  94. > strategy_type;
  95. strategy_type strategy;
  96. typedef typename bg::strategy::distance::services::return_type<strategy_type>::type return_type;
  97. return_type d = strategy.apply(p, sp1, sp2);
  98. T expected_squared_distance = expected_distance * expected_distance;
  99. BOOST_CHECK_CLOSE(d, expected_squared_distance, 0.01);
  100. }
  101. }
  102. template <typename P1, typename P2>
  103. void test_all_2d()
  104. {
  105. test_all_2d<P1, P2>("POINT(1 1)", "POINT(0 0)", "POINT(2 3)", 0.27735203958327);
  106. test_all_2d<P1, P2>("POINT(2 2)", "POINT(1 4)", "POINT(4 1)", 0.5 * sqrt(2.0));
  107. test_all_2d<P1, P2>("POINT(6 1)", "POINT(1 4)", "POINT(4 1)", 2.0);
  108. test_all_2d<P1, P2>("POINT(1 6)", "POINT(1 4)", "POINT(4 1)", 2.0);
  109. }
  110. template <typename P>
  111. void test_all_2d()
  112. {
  113. //test_all_2d<P, int[2]>();
  114. //test_all_2d<P, float[2]>();
  115. //test_all_2d<P, double[2]>();
  116. //test_all_2d<P, test::test_point>();
  117. test_all_2d<P, bg::model::point<int, 2, bg::cs::cartesian> >();
  118. test_all_2d<P, bg::model::point<float, 2, bg::cs::cartesian> >();
  119. test_all_2d<P, bg::model::point<double, 2, bg::cs::cartesian> >();
  120. test_all_2d<P, bg::model::point<long double, 2, bg::cs::cartesian> >();
  121. }
  122. int test_main(int, char* [])
  123. {
  124. test_all_2d<int[2]>();
  125. test_all_2d<float[2]>();
  126. test_all_2d<double[2]>();
  127. //test_all_2d<test::test_point>();
  128. test_all_2d<bg::model::point<int, 2, bg::cs::cartesian> >();
  129. test_all_2d<bg::model::point<float, 2, bg::cs::cartesian> >();
  130. test_all_2d<bg::model::point<double, 2, bg::cs::cartesian> >();
  131. test_services
  132. <
  133. bg::model::point<double, 2, bg::cs::cartesian>,
  134. bg::model::point<float, 2, bg::cs::cartesian>,
  135. long double
  136. >();
  137. #if defined(HAVE_TTMATH)
  138. test_all_2d
  139. <
  140. bg::model::point<ttmath_big, 2, bg::cs::cartesian>,
  141. bg::model::point<ttmath_big, 2, bg::cs::cartesian>
  142. >();
  143. #endif
  144. return 0;
  145. }