test_projected_point.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
  6. // This file was modified by Oracle on 2014.
  7. // Modifications copyright (c) 2014, Oracle and/or its affiliates.
  8. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  11. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #ifndef BOOST_GEOMETRY_TEST_STRATEGIES_TEST_PROJECTED_POINT_HPP
  16. #define BOOST_GEOMETRY_TEST_STRATEGIES_TEST_PROJECTED_POINT_HPP
  17. #include <geometry_test_common.hpp>
  18. #include <boost/core/ignore_unused.hpp>
  19. #include <boost/geometry/strategies/cartesian/distance_projected_point.hpp>
  20. #include <boost/geometry/strategies/cartesian/distance_projected_point_ax.hpp>
  21. #include <boost/geometry/strategies/concepts/distance_concept.hpp>
  22. #include <boost/geometry/io/wkt/read.hpp>
  23. #include <boost/geometry/geometries/point.hpp>
  24. #include <boost/geometry/geometries/adapted/c_array.hpp>
  25. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  26. #include <test_common/test_point.hpp>
  27. #ifdef HAVE_TTMATH
  28. # include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
  29. #endif
  30. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  31. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  32. template <typename P, typename PS, typename CalculationType>
  33. void test_services()
  34. {
  35. PS p1, p2;
  36. bg::assign_values(p1, 0, 0);
  37. bg::assign_values(p2, 0, 4);
  38. P p;
  39. bg::assign_values(p, 2, 0);
  40. CalculationType const sqr_expected = 4;
  41. CalculationType const expected = 2;
  42. namespace bgsd = bg::strategy::distance;
  43. namespace services = bg::strategy::distance::services;
  44. {
  45. // compile-check if there is a strategy for this type
  46. typedef typename services::default_strategy
  47. <
  48. bg::point_tag, bg::segment_tag, P, PS
  49. >::type projected_point_strategy_type;
  50. typedef typename services::default_strategy
  51. <
  52. bg::segment_tag, bg::point_tag, PS, P
  53. >::type reversed_tags_projected_point_strategy_type;
  54. boost::ignore_unused<projected_point_strategy_type,
  55. reversed_tags_projected_point_strategy_type>();
  56. }
  57. // 1: normal, calculate distance:
  58. typedef bgsd::projected_point<CalculationType> strategy_type;
  59. BOOST_CONCEPT_ASSERT( (bg::concepts::PointSegmentDistanceStrategy<strategy_type, P, PS>) );
  60. typedef typename services::return_type<strategy_type, P, PS>::type return_type;
  61. strategy_type strategy;
  62. return_type result = strategy.apply(p, p1, p2);
  63. BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
  64. // 2: the strategy should return the same result if we reverse parameters
  65. result = strategy.apply(p, p2, p1);
  66. BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
  67. // 3: "comparable" to construct a "comparable strategy" for P1/P2
  68. // a "comparable strategy" is a strategy which does not calculate the exact distance, but
  69. // which returns results which can be mutually compared (e.g. avoid sqrt)
  70. // 3a: "comparable_type"
  71. typedef typename services::comparable_type<strategy_type>::type comparable_type;
  72. // 3b: "get_comparable"
  73. comparable_type comparable = bgsd::services::get_comparable<strategy_type>::apply(strategy);
  74. return_type c_result = comparable.apply(p, p1, p2);
  75. BOOST_CHECK_CLOSE(c_result, return_type(sqr_expected), 0.001);
  76. }
  77. template <typename T1, typename T2>
  78. void test_check_close(T1 const& v1, T2 const& v2, double f)
  79. {
  80. BOOST_CHECK_CLOSE(v1, v2, f);
  81. }
  82. template <typename T1, typename T2>
  83. void test_check_close(bg::strategy::distance::detail::projected_point_ax_result<T1> const& v1,
  84. bg::strategy::distance::detail::projected_point_ax_result<T2> const& v2,
  85. double f)
  86. {
  87. BOOST_CHECK_CLOSE(v1.atd, v2.atd, f);
  88. BOOST_CHECK_CLOSE(v1.xtd, v2.xtd, f);
  89. }
  90. template <typename P1, typename P2, typename T, typename Strategy, typename ComparableStrategy>
  91. void test_2d(std::string const& wkt_p,
  92. std::string const& wkt_sp1,
  93. std::string const& wkt_sp2,
  94. T expected_distance,
  95. T expected_comparable_distance,
  96. Strategy strategy,
  97. ComparableStrategy comparable_strategy)
  98. {
  99. P1 p;
  100. P2 sp1, sp2;
  101. bg::read_wkt(wkt_p, p);
  102. bg::read_wkt(wkt_sp1, sp1);
  103. bg::read_wkt(wkt_sp2, sp2);
  104. BOOST_CONCEPT_ASSERT
  105. (
  106. (bg::concepts::PointSegmentDistanceStrategy<Strategy, P1, P2>)
  107. );
  108. BOOST_CONCEPT_ASSERT
  109. (
  110. (bg::concepts::PointSegmentDistanceStrategy<ComparableStrategy, P1, P2>)
  111. );
  112. {
  113. typedef typename bg::strategy::distance::services::return_type<Strategy, P1, P2>::type return_type;
  114. return_type d = strategy.apply(p, sp1, sp2);
  115. test_check_close(d, expected_distance, 0.001);
  116. }
  117. // Test combination with the comparable strategy
  118. {
  119. typedef typename bg::strategy::distance::services::return_type<ComparableStrategy, P1, P2>::type return_type;
  120. return_type d = comparable_strategy.apply(p, sp1, sp2);
  121. test_check_close(d, expected_comparable_distance, 0.01);
  122. }
  123. }
  124. template <typename P1, typename P2, typename T>
  125. void test_2d(std::string const& wkt_p,
  126. std::string const& wkt_sp1,
  127. std::string const& wkt_sp2,
  128. T expected_distance)
  129. {
  130. typedef bg::strategy::distance::projected_point<> strategy_type;
  131. typedef bg::strategy::distance::projected_point
  132. <
  133. void,
  134. bg::strategy::distance::comparable::pythagoras<>
  135. > comparable_strategy_type;
  136. strategy_type strategy;
  137. comparable_strategy_type comparable_strategy;
  138. T expected_squared_distance = expected_distance * expected_distance;
  139. test_2d<P1, P2>(wkt_p, wkt_sp1, wkt_sp2, expected_distance, expected_squared_distance, strategy, comparable_strategy);
  140. }
  141. #endif // BOOST_GEOMETRY_TEST_STRATEGIES_TEST_PROJECTED_POINT_HPP