pythagoras.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #if defined(_MSC_VER)
  13. # pragma warning( disable : 4101 )
  14. #endif
  15. #include <boost/timer.hpp>
  16. #include <boost/concept/requires.hpp>
  17. #include <boost/concept_check.hpp>
  18. #include <boost/core/ignore_unused.hpp>
  19. #include <boost/geometry/algorithms/assign.hpp>
  20. #include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
  21. #include <boost/geometry/strategies/concepts/distance_concept.hpp>
  22. #include <boost/geometry/geometries/point.hpp>
  23. #include <boost/geometry/geometries/adapted/c_array.hpp>
  24. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  25. #include <test_common/test_point.hpp>
  26. #ifdef HAVE_TTMATH
  27. # include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
  28. #endif
  29. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  30. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  31. template <typename P1, typename P2>
  32. void test_null_distance_3d()
  33. {
  34. P1 p1;
  35. bg::assign_values(p1, 1, 2, 3);
  36. P2 p2;
  37. bg::assign_values(p2, 1, 2, 3);
  38. typedef bg::strategy::distance::pythagoras<> pythagoras_type;
  39. typedef typename bg::strategy::distance::services::return_type<pythagoras_type, P1, P2>::type return_type;
  40. pythagoras_type pythagoras;
  41. return_type result = pythagoras.apply(p1, p2);
  42. BOOST_CHECK_EQUAL(result, return_type(0));
  43. }
  44. template <typename P1, typename P2>
  45. void test_axis_3d()
  46. {
  47. P1 p1;
  48. bg::assign_values(p1, 0, 0, 0);
  49. P2 p2;
  50. bg::assign_values(p2, 1, 0, 0);
  51. typedef bg::strategy::distance::pythagoras<> pythagoras_type;
  52. typedef typename bg::strategy::distance::services::return_type<pythagoras_type, P1, P2>::type return_type;
  53. pythagoras_type pythagoras;
  54. return_type result = pythagoras.apply(p1, p2);
  55. BOOST_CHECK_EQUAL(result, return_type(1));
  56. bg::assign_values(p2, 0, 1, 0);
  57. result = pythagoras.apply(p1, p2);
  58. BOOST_CHECK_EQUAL(result, return_type(1));
  59. bg::assign_values(p2, 0, 0, 1);
  60. result = pythagoras.apply(p1, p2);
  61. BOOST_CHECK_CLOSE(result, return_type(1), 0.001);
  62. }
  63. template <typename P1, typename P2>
  64. void test_arbitrary_3d()
  65. {
  66. P1 p1;
  67. bg::assign_values(p1, 1, 2, 3);
  68. P2 p2;
  69. bg::assign_values(p2, 9, 8, 7);
  70. {
  71. typedef bg::strategy::distance::pythagoras<> strategy_type;
  72. typedef typename bg::strategy::distance::services::return_type<strategy_type, P1, P2>::type return_type;
  73. strategy_type strategy;
  74. return_type result = strategy.apply(p1, p2);
  75. BOOST_CHECK_CLOSE(result, return_type(10.77032961427), 0.001);
  76. }
  77. {
  78. // Check comparable distance
  79. typedef bg::strategy::distance::comparable::pythagoras<> strategy_type;
  80. typedef typename bg::strategy::distance::services::return_type<strategy_type, P1, P2>::type return_type;
  81. strategy_type strategy;
  82. return_type result = strategy.apply(p1, p2);
  83. BOOST_CHECK_EQUAL(result, return_type(116));
  84. }
  85. }
  86. template <typename P1, typename P2, typename CalculationType>
  87. void test_services()
  88. {
  89. namespace bgsd = bg::strategy::distance;
  90. namespace services = bg::strategy::distance::services;
  91. {
  92. // Compile-check if there is a strategy for this type
  93. typedef typename services::default_strategy
  94. <
  95. bg::point_tag, bg::point_tag, P1, P2
  96. >::type pythagoras_strategy_type;
  97. boost::ignore_unused<pythagoras_strategy_type>();
  98. }
  99. P1 p1;
  100. bg::assign_values(p1, 1, 2, 3);
  101. P2 p2;
  102. bg::assign_values(p2, 4, 5, 6);
  103. double const sqr_expected = 3*3 + 3*3 + 3*3; // 27
  104. double const expected = sqrt(sqr_expected); // sqrt(27)=5.1961524227
  105. // 1: normal, calculate distance:
  106. typedef bgsd::pythagoras<CalculationType> strategy_type;
  107. BOOST_CONCEPT_ASSERT( (bg::concepts::PointDistanceStrategy<strategy_type, P1, P2>) );
  108. typedef typename bgsd::services::return_type<strategy_type, P1, P2>::type return_type;
  109. strategy_type strategy;
  110. return_type result = strategy.apply(p1, p2);
  111. BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
  112. // 2: the strategy should return the same result if we reverse parameters
  113. result = strategy.apply(p2, p1);
  114. BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
  115. // 3: "comparable" to construct a "comparable strategy" for P1/P2
  116. // a "comparable strategy" is a strategy which does not calculate the exact distance, but
  117. // which returns results which can be mutually compared (e.g. avoid sqrt)
  118. // 3a: "comparable_type"
  119. typedef typename services::comparable_type<strategy_type>::type comparable_type;
  120. // 3b: "get_comparable"
  121. comparable_type comparable = bgsd::services::get_comparable<strategy_type>::apply(strategy);
  122. return_type c_result = comparable.apply(p1, p2);
  123. BOOST_CHECK_CLOSE(c_result, return_type(sqr_expected), 0.001);
  124. // 4: the comparable_type should have a distance_strategy_constructor as well,
  125. // knowing how to compare something with a fixed distance
  126. return_type c_dist5 = services::result_from_distance<comparable_type, P1, P2>::apply(comparable, 5.0);
  127. return_type c_dist6 = services::result_from_distance<comparable_type, P1, P2>::apply(comparable, 6.0);
  128. // If this is the case:
  129. BOOST_CHECK(c_dist5 < c_result && c_result < c_dist6);
  130. // This should also be the case
  131. return_type dist5 = services::result_from_distance<strategy_type, P1, P2>::apply(strategy, 5.0);
  132. return_type dist6 = services::result_from_distance<strategy_type, P1, P2>::apply(strategy, 6.0);
  133. BOOST_CHECK(dist5 < result && result < dist6);
  134. }
  135. template <typename CoordinateType, typename CalculationType, typename AssignType>
  136. void test_big_2d_with(AssignType const& x1, AssignType const& y1,
  137. AssignType const& x2, AssignType const& y2)
  138. {
  139. typedef bg::model::point<CoordinateType, 2, bg::cs::cartesian> point_type;
  140. typedef bg::strategy::distance::pythagoras<CalculationType> pythagoras_type;
  141. pythagoras_type pythagoras;
  142. typedef typename bg::strategy::distance::services::return_type<pythagoras_type, point_type, point_type>::type return_type;
  143. point_type p1, p2;
  144. bg::assign_values(p1, x1, y1);
  145. bg::assign_values(p2, x2, y2);
  146. return_type d = pythagoras.apply(p1, p2);
  147. /***
  148. std::cout << typeid(CalculationType).name()
  149. << " " << std::fixed << std::setprecision(20) << d
  150. << std::endl << std::endl;
  151. ***/
  152. BOOST_CHECK_CLOSE(d, return_type(1076554.5485833955678294387789057), 0.001);
  153. }
  154. template <typename CoordinateType, typename CalculationType>
  155. void test_big_2d()
  156. {
  157. test_big_2d_with<CoordinateType, CalculationType>
  158. (123456.78900001, 234567.89100001,
  159. 987654.32100001, 876543.21900001);
  160. }
  161. template <typename CoordinateType, typename CalculationType>
  162. void test_big_2d_string()
  163. {
  164. test_big_2d_with<CoordinateType, CalculationType>
  165. ("123456.78900001", "234567.89100001",
  166. "987654.32100001", "876543.21900001");
  167. }
  168. template <typename CoordinateType>
  169. void test_integer(bool check_types)
  170. {
  171. typedef bg::model::point<CoordinateType, 2, bg::cs::cartesian> point_type;
  172. point_type p1, p2;
  173. bg::assign_values(p1, 12345678, 23456789);
  174. bg::assign_values(p2, 98765432, 87654321);
  175. typedef bg::strategy::distance::pythagoras<> pythagoras_type;
  176. typedef typename bg::strategy::distance::services::comparable_type
  177. <
  178. pythagoras_type
  179. >::type comparable_type;
  180. typedef typename bg::strategy::distance::services::return_type
  181. <
  182. pythagoras_type, point_type, point_type
  183. >::type distance_type;
  184. typedef typename bg::strategy::distance::services::return_type
  185. <
  186. comparable_type, point_type, point_type
  187. >::type cdistance_type;
  188. pythagoras_type pythagoras;
  189. distance_type distance = pythagoras.apply(p1, p2);
  190. BOOST_CHECK_CLOSE(distance, 107655455.02347542, 0.001);
  191. comparable_type comparable;
  192. cdistance_type cdistance = comparable.apply(p1, p2);
  193. BOOST_CHECK_EQUAL(cdistance, 11589696996311540.0);
  194. distance_type distance2 = sqrt(distance_type(cdistance));
  195. BOOST_CHECK_CLOSE(distance, distance2, 0.001);
  196. if (check_types)
  197. {
  198. BOOST_CHECK((boost::is_same<distance_type, double>::type::value));
  199. // comparable_distance results in now double too, obviously because
  200. // comp.distance point-segment can be fraction, even for integer input
  201. BOOST_CHECK((boost::is_same<cdistance_type, double>::type::value));
  202. }
  203. }
  204. template <typename P1, typename P2>
  205. void test_all_3d()
  206. {
  207. test_null_distance_3d<P1, P2>();
  208. test_axis_3d<P1, P2>();
  209. test_arbitrary_3d<P1, P2>();
  210. }
  211. template <typename P>
  212. void test_all_3d()
  213. {
  214. test_all_3d<P, int[3]>();
  215. test_all_3d<P, float[3]>();
  216. test_all_3d<P, double[3]>();
  217. test_all_3d<P, test::test_point>();
  218. test_all_3d<P, bg::model::point<int, 3, bg::cs::cartesian> >();
  219. test_all_3d<P, bg::model::point<float, 3, bg::cs::cartesian> >();
  220. test_all_3d<P, bg::model::point<double, 3, bg::cs::cartesian> >();
  221. }
  222. template <typename P, typename Strategy>
  223. void time_compare_s(int const n)
  224. {
  225. boost::timer t;
  226. P p1, p2;
  227. bg::assign_values(p1, 1, 1);
  228. bg::assign_values(p2, 2, 2);
  229. Strategy strategy;
  230. typename bg::strategy::distance::services::return_type<Strategy, P, P>::type s = 0;
  231. for (int i = 0; i < n; i++)
  232. {
  233. for (int j = 0; j < n; j++)
  234. {
  235. bg::set<0>(p2, bg::get<0>(p2) + 0.001);
  236. s += strategy.apply(p1, p2);
  237. }
  238. }
  239. std::cout << "s: " << s << " t: " << t.elapsed() << std::endl;
  240. }
  241. template <typename P>
  242. void time_compare(int const n)
  243. {
  244. time_compare_s<P, bg::strategy::distance::pythagoras<> >(n);
  245. time_compare_s<P, bg::strategy::distance::comparable::pythagoras<> >(n);
  246. }
  247. int test_main(int, char* [])
  248. {
  249. test_integer<int>(true);
  250. test_integer<boost::long_long_type>(true);
  251. test_integer<double>(false);
  252. test_all_3d<int[3]>();
  253. test_all_3d<float[3]>();
  254. test_all_3d<double[3]>();
  255. test_all_3d<test::test_point>();
  256. test_all_3d<bg::model::point<int, 3, bg::cs::cartesian> >();
  257. test_all_3d<bg::model::point<float, 3, bg::cs::cartesian> >();
  258. test_all_3d<bg::model::point<double, 3, bg::cs::cartesian> >();
  259. test_big_2d<float, float>();
  260. test_big_2d<double, double>();
  261. test_big_2d<long double, long double>();
  262. test_big_2d<float, long double>();
  263. test_services<bg::model::point<float, 3, bg::cs::cartesian>, double[3], long double>();
  264. test_services<double[3], test::test_point, float>();
  265. // TODO move this to another non-unit test
  266. // time_compare<bg::model::point<double, 2, bg::cs::cartesian> >(10000);
  267. #if defined(HAVE_TTMATH)
  268. typedef ttmath::Big<1,4> tt;
  269. typedef bg::model::point<tt, 3, bg::cs::cartesian> tt_point;
  270. //test_all_3d<tt[3]>();
  271. test_all_3d<tt_point>();
  272. test_all_3d<tt_point, tt_point>();
  273. test_big_2d<tt, tt>();
  274. test_big_2d_string<tt, tt>();
  275. #endif
  276. return 0;
  277. }