pythagoras_point_box.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_TEST_MODULE
  15. #define BOOST_TEST_MODULE test_pythagoras_point_box
  16. #endif
  17. #include <boost/test/included/unit_test.hpp>
  18. #if defined(_MSC_VER)
  19. # pragma warning( disable : 4101 )
  20. #endif
  21. #include <boost/core/ignore_unused.hpp>
  22. #include <boost/timer.hpp>
  23. #include <boost/concept/requires.hpp>
  24. #include <boost/concept_check.hpp>
  25. #include <boost/geometry/algorithms/assign.hpp>
  26. #include <boost/geometry/algorithms/expand.hpp>
  27. #include <boost/geometry/strategies/cartesian/distance_pythagoras_point_box.hpp>
  28. #include <boost/geometry/strategies/concepts/distance_concept.hpp>
  29. #include <boost/geometry/geometries/point.hpp>
  30. #include <boost/geometry/geometries/box.hpp>
  31. #include <boost/geometry/geometries/adapted/c_array.hpp>
  32. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  33. #include <test_common/test_point.hpp>
  34. #ifdef HAVE_TTMATH
  35. # include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
  36. #endif
  37. namespace bg = boost::geometry;
  38. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  39. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  40. template <typename Box, typename Coordinate>
  41. inline void assign_values(Box& box,
  42. Coordinate const& x1,
  43. Coordinate const& y1,
  44. Coordinate const& z1,
  45. Coordinate const& x2,
  46. Coordinate const& y2,
  47. Coordinate const& z2)
  48. {
  49. typename bg::point_type<Box>::type p1, p2;
  50. bg::assign_values(p1, x1, y1, z1);
  51. bg::assign_values(p2, x2, y2, z2);
  52. bg::assign(box, p1);
  53. bg::expand(box, p2);
  54. }
  55. template <typename Point, typename Box>
  56. inline void test_null_distance_3d()
  57. {
  58. Point p;
  59. bg::assign_values(p, 1, 2, 4);
  60. Box b;
  61. assign_values(b, 1, 2, 3, 4, 5, 6);
  62. typedef bg::strategy::distance::pythagoras_point_box<> pythagoras_pb_type;
  63. typedef typename bg::strategy::distance::services::return_type
  64. <
  65. pythagoras_pb_type, Point, Box
  66. >::type return_type;
  67. pythagoras_pb_type pythagoras_pb;
  68. return_type result = pythagoras_pb.apply(p, b);
  69. BOOST_CHECK_EQUAL(result, return_type(0));
  70. bg::assign_values(p, 1, 3, 4);
  71. result = pythagoras_pb.apply(p, b);
  72. BOOST_CHECK_EQUAL(result, return_type(0));
  73. bg::assign_values(p, 2, 3, 4);
  74. result = pythagoras_pb.apply(p, b);
  75. BOOST_CHECK_EQUAL(result, return_type(0));
  76. }
  77. template <typename Point, typename Box>
  78. inline void test_axis_3d()
  79. {
  80. Box b;
  81. assign_values(b, 0, 0, 0, 1, 1, 1);
  82. Point p;
  83. bg::assign_values(p, 2, 0, 0);
  84. typedef bg::strategy::distance::pythagoras_point_box<> pythagoras_pb_type;
  85. typedef typename bg::strategy::distance::services::return_type
  86. <
  87. pythagoras_pb_type, Point, Box
  88. >::type return_type;
  89. pythagoras_pb_type pythagoras_pb;
  90. return_type result = pythagoras_pb.apply(p, b);
  91. BOOST_CHECK_EQUAL(result, return_type(1));
  92. bg::assign_values(p, 0, 2, 0);
  93. result = pythagoras_pb.apply(p, b);
  94. BOOST_CHECK_EQUAL(result, return_type(1));
  95. bg::assign_values(p, 0, 0, 2);
  96. result = pythagoras_pb.apply(p, b);
  97. BOOST_CHECK_CLOSE(result, return_type(1), 0.001);
  98. }
  99. template <typename Point, typename Box>
  100. inline void test_arbitrary_3d()
  101. {
  102. Box b;
  103. assign_values(b, 0, 0, 0, 1, 2, 3);
  104. Point p;
  105. bg::assign_values(p, 9, 8, 7);
  106. {
  107. typedef bg::strategy::distance::pythagoras_point_box<> strategy_type;
  108. typedef typename bg::strategy::distance::services::return_type
  109. <
  110. strategy_type, Point, Box
  111. >::type return_type;
  112. strategy_type strategy;
  113. return_type result = strategy.apply(p, b);
  114. BOOST_CHECK_CLOSE(result, return_type(10.77032961427), 0.001);
  115. }
  116. {
  117. // Check comparable distance
  118. typedef bg::strategy::distance::comparable::pythagoras_point_box<>
  119. strategy_type;
  120. typedef typename bg::strategy::distance::services::return_type
  121. <
  122. strategy_type, Point, Box
  123. >::type return_type;
  124. strategy_type strategy;
  125. return_type result = strategy.apply(p, b);
  126. BOOST_CHECK_EQUAL(result, return_type(116));
  127. }
  128. }
  129. template <typename Point, typename Box, typename CalculationType>
  130. inline void test_services()
  131. {
  132. namespace bgsd = bg::strategy::distance;
  133. namespace services = bg::strategy::distance::services;
  134. {
  135. // Compile-check if there is a strategy for this type
  136. typedef typename services::default_strategy
  137. <
  138. bg::point_tag, bg::box_tag, Point, Box
  139. >::type pythagoras_pb_strategy_type;
  140. // reverse geometry tags
  141. typedef typename services::default_strategy
  142. <
  143. bg::box_tag, bg::point_tag, Box, Point
  144. >::type reversed_pythagoras_pb_strategy_type;
  145. boost::ignore_unused
  146. <
  147. pythagoras_pb_strategy_type,
  148. reversed_pythagoras_pb_strategy_type
  149. >();
  150. }
  151. Point p;
  152. bg::assign_values(p, 1, 2, 3);
  153. Box b;
  154. assign_values(b, 4, 5, 6, 14, 15, 16);
  155. double const sqr_expected = 3*3 + 3*3 + 3*3; // 27
  156. double const expected = sqrt(sqr_expected); // sqrt(27)=5.1961524227
  157. // 1: normal, calculate distance:
  158. typedef bgsd::pythagoras_point_box<CalculationType> strategy_type;
  159. BOOST_CONCEPT_ASSERT
  160. ( (bg::concepts::PointDistanceStrategy<strategy_type, Point, Box>) );
  161. typedef typename bgsd::services::return_type
  162. <
  163. strategy_type, Point, Box
  164. >::type return_type;
  165. strategy_type strategy;
  166. return_type result = strategy.apply(p, b);
  167. BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
  168. // 2: the strategy should return the same result if we reverse parameters
  169. // result = strategy.apply(p2, p1);
  170. // BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
  171. // 3: "comparable" to construct a "comparable strategy" for Point/Box
  172. // a "comparable strategy" is a strategy which does not calculate the exact distance, but
  173. // which returns results which can be mutually compared (e.g. avoid sqrt)
  174. // 3a: "comparable_type"
  175. typedef typename services::comparable_type
  176. <
  177. strategy_type
  178. >::type comparable_type;
  179. // 3b: "get_comparable"
  180. comparable_type comparable = bgsd::services::get_comparable
  181. <
  182. strategy_type
  183. >::apply(strategy);
  184. typedef typename bgsd::services::return_type
  185. <
  186. comparable_type, Point, Box
  187. >::type comparable_return_type;
  188. comparable_return_type c_result = comparable.apply(p, b);
  189. BOOST_CHECK_CLOSE(c_result, return_type(sqr_expected), 0.001);
  190. // 4: the comparable_type should have a distance_strategy_constructor as well,
  191. // knowing how to compare something with a fixed distance
  192. comparable_return_type c_dist5 = services::result_from_distance
  193. <
  194. comparable_type, Point, Box
  195. >::apply(comparable, 5.0);
  196. comparable_return_type c_dist6 = services::result_from_distance
  197. <
  198. comparable_type, Point, Box
  199. >::apply(comparable, 6.0);
  200. // If this is the case:
  201. BOOST_CHECK(c_dist5 < c_result && c_result < c_dist6);
  202. // This should also be the case
  203. return_type dist5 = services::result_from_distance
  204. <
  205. strategy_type, Point, Box
  206. >::apply(strategy, 5.0);
  207. return_type dist6 = services::result_from_distance
  208. <
  209. strategy_type, Point, Box
  210. >::apply(strategy, 6.0);
  211. BOOST_CHECK(dist5 < result && result < dist6);
  212. }
  213. template
  214. <
  215. typename CoordinateType,
  216. typename CalculationType,
  217. typename AssignType
  218. >
  219. inline void test_big_2d_with(AssignType const& x1, AssignType const& y1,
  220. AssignType const& x2, AssignType const& y2,
  221. AssignType const& zero)
  222. {
  223. typedef bg::model::point<CoordinateType, 2, bg::cs::cartesian> point_type;
  224. typedef bg::model::box<point_type> box_type;
  225. typedef bg::strategy::distance::pythagoras_point_box
  226. <
  227. CalculationType
  228. > pythagoras_pb_type;
  229. pythagoras_pb_type pythagoras_pb;
  230. typedef typename bg::strategy::distance::services::return_type
  231. <
  232. pythagoras_pb_type, point_type, box_type
  233. >::type return_type;
  234. point_type p;
  235. box_type b;
  236. bg::assign_values(b, zero, zero, x1, y1);
  237. bg::assign_values(p, x2, y2);
  238. return_type d = pythagoras_pb.apply(p, b);
  239. BOOST_CHECK_CLOSE(d, return_type(1076554.5485833955678294387789057), 0.001);
  240. }
  241. template <typename CoordinateType, typename CalculationType>
  242. inline void test_big_2d()
  243. {
  244. test_big_2d_with<CoordinateType, CalculationType>
  245. (123456.78900001, 234567.89100001,
  246. 987654.32100001, 876543.21900001,
  247. 0.0);
  248. }
  249. template <typename CoordinateType, typename CalculationType>
  250. inline void test_big_2d_string()
  251. {
  252. test_big_2d_with<CoordinateType, CalculationType>
  253. ("123456.78900001", "234567.89100001",
  254. "987654.32100001", "876543.21900001",
  255. "0.0000000000000");
  256. }
  257. template <typename CoordinateType>
  258. inline void test_integer(bool check_types)
  259. {
  260. typedef bg::model::point<CoordinateType, 2, bg::cs::cartesian> point_type;
  261. typedef bg::model::box<point_type> box_type;
  262. point_type p;
  263. box_type b;
  264. bg::assign_values(b, 0, 0, 12345678, 23456789);
  265. bg::assign_values(p, 98765432, 87654321);
  266. typedef bg::strategy::distance::pythagoras_point_box<> pythagoras_type;
  267. typedef typename bg::strategy::distance::services::comparable_type
  268. <
  269. pythagoras_type
  270. >::type comparable_type;
  271. typedef typename bg::strategy::distance::services::return_type
  272. <
  273. pythagoras_type, point_type, box_type
  274. >::type distance_type;
  275. typedef typename bg::strategy::distance::services::return_type
  276. <
  277. comparable_type, point_type, box_type
  278. >::type cdistance_type;
  279. pythagoras_type pythagoras;
  280. distance_type distance = pythagoras.apply(p, b);
  281. BOOST_CHECK_CLOSE(distance, 107655455.02347542, 0.001);
  282. comparable_type comparable;
  283. cdistance_type cdistance = comparable.apply(p, b);
  284. BOOST_CHECK_EQUAL(cdistance, 11589696996311540.0);
  285. distance_type distance2 = sqrt(distance_type(cdistance));
  286. BOOST_CHECK_CLOSE(distance, distance2, 0.001);
  287. if (check_types)
  288. {
  289. BOOST_CHECK((boost::is_same<distance_type, double>::type::value));
  290. BOOST_CHECK((boost::is_same<cdistance_type, boost::long_long_type>::type::value));
  291. }
  292. }
  293. template <typename P1, typename P2>
  294. void test_all_3d()
  295. {
  296. test_null_distance_3d<P1, bg::model::box<P2> >();
  297. test_axis_3d<P1, bg::model::box<P2> >();
  298. test_arbitrary_3d<P1, bg::model::box<P2> >();
  299. test_null_distance_3d<P2, bg::model::box<P1> >();
  300. test_axis_3d<P2, bg::model::box<P1> >();
  301. test_arbitrary_3d<P2, bg::model::box<P1> >();
  302. }
  303. template <typename P>
  304. void test_all_3d()
  305. {
  306. test_all_3d<P, int[3]>();
  307. test_all_3d<P, float[3]>();
  308. test_all_3d<P, double[3]>();
  309. test_all_3d<P, test::test_point>();
  310. test_all_3d<P, bg::model::point<int, 3, bg::cs::cartesian> >();
  311. test_all_3d<P, bg::model::point<float, 3, bg::cs::cartesian> >();
  312. test_all_3d<P, bg::model::point<double, 3, bg::cs::cartesian> >();
  313. }
  314. template <typename P, typename Strategy>
  315. void time_compare_s(int const n)
  316. {
  317. typedef bg::model::box<P> box_type;
  318. boost::timer t;
  319. P p;
  320. box_type b;
  321. bg::assign_values(b, 0, 0, 1, 1);
  322. bg::assign_values(p, 2, 2);
  323. Strategy strategy;
  324. typename bg::strategy::distance::services::return_type
  325. <
  326. Strategy, P, box_type
  327. >::type s = 0;
  328. for (int i = 0; i < n; i++)
  329. {
  330. for (int j = 0; j < n; j++)
  331. {
  332. bg::set<0>(p, bg::get<0>(p) + 0.001);
  333. s += strategy.apply(p, b);
  334. }
  335. }
  336. std::cout << "s: " << s << " t: " << t.elapsed() << std::endl;
  337. }
  338. template <typename P>
  339. inline void time_compare(int const n)
  340. {
  341. time_compare_s<P, bg::strategy::distance::pythagoras_point_box<> >(n);
  342. time_compare_s
  343. <
  344. P, bg::strategy::distance::comparable::pythagoras_point_box<>
  345. >(n);
  346. }
  347. BOOST_AUTO_TEST_CASE( test_integer_all )
  348. {
  349. test_integer<int>(true);
  350. test_integer<boost::long_long_type>(true);
  351. test_integer<double>(false);
  352. }
  353. BOOST_AUTO_TEST_CASE( test_3d_all )
  354. {
  355. test_all_3d<int[3]>();
  356. test_all_3d<float[3]>();
  357. test_all_3d<double[3]>();
  358. test_all_3d<test::test_point>();
  359. test_all_3d<bg::model::point<int, 3, bg::cs::cartesian> >();
  360. test_all_3d<bg::model::point<float, 3, bg::cs::cartesian> >();
  361. test_all_3d<bg::model::point<double, 3, bg::cs::cartesian> >();
  362. }
  363. BOOST_AUTO_TEST_CASE( test_big_2d_all )
  364. {
  365. test_big_2d<float, float>();
  366. test_big_2d<double, double>();
  367. test_big_2d<long double, long double>();
  368. test_big_2d<float, long double>();
  369. }
  370. BOOST_AUTO_TEST_CASE( test_services_all )
  371. {
  372. test_services
  373. <
  374. bg::model::point<float, 3, bg::cs::cartesian>,
  375. bg::model::box<double[3]>,
  376. long double
  377. >();
  378. test_services<double[3], bg::model::box<test::test_point>, float>();
  379. // reverse the point and box types
  380. test_services
  381. <
  382. double[3],
  383. bg::model::box<bg::model::point<float, 3, bg::cs::cartesian> >,
  384. long double
  385. >();
  386. test_services<test::test_point, bg::model::box<double[3]>, float>();
  387. }
  388. BOOST_AUTO_TEST_CASE( test_time_compare )
  389. {
  390. // TODO move this to another non-unit test
  391. // time_compare<bg::model::point<double, 2, bg::cs::cartesian> >(10000);
  392. }
  393. #if defined(HAVE_TTMATH)
  394. BOOST_AUTO_TEST_CASE( test_ttmath_all )
  395. {
  396. typedef ttmath::Big<1,4> tt;
  397. typedef bg::model::point<tt, 3, bg::cs::cartesian> tt_point;
  398. //test_all_3d<tt[3]>();
  399. test_all_3d<tt_point>();
  400. test_all_3d<tt_point, tt_point>();
  401. test_big_2d<tt, tt>();
  402. test_big_2d_string<tt, tt>();
  403. }
  404. #endif