intersection.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Boost.Geometry
  2. // Unit Test
  3. // Copyright (c) 2016-2019 Oracle and/or its affiliates.
  4. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #include "test_formula.hpp"
  10. #include "intersection_cases.hpp"
  11. #include <boost/geometry/formulas/andoyer_inverse.hpp>
  12. #include <boost/geometry/formulas/geographic.hpp>
  13. #include <boost/geometry/formulas/gnomonic_intersection.hpp>
  14. #include <boost/geometry/formulas/sjoberg_intersection.hpp>
  15. #include <boost/geometry/formulas/thomas_direct.hpp>
  16. #include <boost/geometry/formulas/thomas_inverse.hpp>
  17. #include <boost/geometry/formulas/vincenty_direct.hpp>
  18. #include <boost/geometry/formulas/vincenty_inverse.hpp>
  19. #include <boost/geometry/strategies/geographic/parameters.hpp>
  20. #include <boost/geometry/srs/spheroid.hpp>
  21. void check_result(expected_result const& result, expected_result const& expected,
  22. expected_result const& reference, double reference_error,
  23. bool check_reference_only)
  24. {
  25. //BOOST_CHECK_MESSAGE((false), "(" << result.lon << " " << result.lat << ") vs (" << expected.lon << " " << expected.lat << ")");
  26. check_one(result.lon, expected.lon, reference.lon, reference_error, false, check_reference_only);
  27. check_one(result.lat, expected.lat, reference.lat, reference_error, false, check_reference_only);
  28. }
  29. void test_formulas(expected_results const& results, bool check_reference_only)
  30. {
  31. // reference result
  32. if (results.sjoberg_vincenty.lon == ND)
  33. {
  34. return;
  35. }
  36. double const d2r = bg::math::d2r<double>();
  37. double const r2d = bg::math::r2d<double>();
  38. double lona1r = results.p1.lon * d2r;
  39. double lata1r = results.p1.lat * d2r;
  40. double lona2r = results.p2.lon * d2r;
  41. double lata2r = results.p2.lat * d2r;
  42. double lonb1r = results.q1.lon * d2r;
  43. double latb1r = results.q1.lat * d2r;
  44. double lonb2r = results.q2.lon * d2r;
  45. double latb2r = results.q2.lat * d2r;
  46. expected_result result;
  47. // WGS84
  48. bg::srs::spheroid<double> spheroid(6378137.0, 6356752.3142451793);
  49. if (results.gnomonic_vincenty.lon != ND)
  50. {
  51. bg::formula::gnomonic_intersection<double, bg::formula::vincenty_inverse, bg::formula::vincenty_direct>
  52. ::apply(lona1r, lata1r, lona2r, lata2r, lonb1r, latb1r, lonb2r, latb2r, result.lon, result.lat, spheroid);
  53. result.lon *= r2d;
  54. result.lat *= r2d;
  55. check_result(result, results.gnomonic_vincenty, results.sjoberg_vincenty, 0.00000001, check_reference_only);
  56. }
  57. if (results.gnomonic_thomas.lon != ND)
  58. {
  59. bg::formula::gnomonic_intersection<double, bg::strategy::thomas::inverse, bg::strategy::thomas::direct>
  60. ::apply(lona1r, lata1r, lona2r, lata2r, lonb1r, latb1r, lonb2r, latb2r, result.lon, result.lat, spheroid);
  61. result.lon *= r2d;
  62. result.lat *= r2d;
  63. check_result(result, results.gnomonic_thomas, results.sjoberg_vincenty, 0.0000001, check_reference_only);
  64. }
  65. if (results.sjoberg_vincenty.lon != ND)
  66. {
  67. bg::formula::sjoberg_intersection<double, bg::formula::vincenty_inverse, 4>
  68. ::apply(lona1r, lata1r, lona2r, lata2r, lonb1r, latb1r, lonb2r, latb2r, result.lon, result.lat, spheroid);
  69. result.lon *= r2d;
  70. result.lat *= r2d;
  71. check_result(result, results.sjoberg_vincenty, results.sjoberg_vincenty, 0.00000001, check_reference_only);
  72. }
  73. if (results.sjoberg_thomas.lon != ND)
  74. {
  75. bg::formula::sjoberg_intersection<double, bg::formula::thomas_inverse, 2>
  76. ::apply(lona1r, lata1r, lona2r, lata2r, lonb1r, latb1r, lonb2r, latb2r, result.lon, result.lat, spheroid);
  77. result.lon *= r2d;
  78. result.lat *= r2d;
  79. check_result(result, results.sjoberg_thomas, results.sjoberg_vincenty, 0.0000001, check_reference_only);
  80. }
  81. if (results.sjoberg_andoyer.lon != ND)
  82. {
  83. bg::formula::sjoberg_intersection<double, bg::formula::andoyer_inverse, 1>
  84. ::apply(lona1r, lata1r, lona2r, lata2r, lonb1r, latb1r, lonb2r, latb2r, result.lon, result.lat, spheroid);
  85. result.lon *= r2d;
  86. result.lat *= r2d;
  87. check_result(result, results.sjoberg_andoyer, results.sjoberg_vincenty, 0.0001, check_reference_only);
  88. }
  89. if (results.great_elliptic.lon != ND)
  90. {
  91. typedef bg::model::point<double, 2, bg::cs::geographic<bg::degree> > point_geo;
  92. typedef bg::model::point<double, 3, bg::cs::cartesian> point_3d;
  93. point_geo a1(results.p1.lon, results.p1.lat);
  94. point_geo a2(results.p2.lon, results.p2.lat);
  95. point_geo b1(results.q1.lon, results.q1.lat);
  96. point_geo b2(results.q2.lon, results.q2.lat);
  97. point_3d a1v = bg::formula::geo_to_cart3d<point_3d>(a1, spheroid);
  98. point_3d a2v = bg::formula::geo_to_cart3d<point_3d>(a2, spheroid);
  99. point_3d b1v = bg::formula::geo_to_cart3d<point_3d>(b1, spheroid);
  100. point_3d b2v = bg::formula::geo_to_cart3d<point_3d>(b2, spheroid);
  101. point_3d resv(0, 0, 0);
  102. point_geo res(0, 0);
  103. bg::formula::great_elliptic_intersection(a1v, a2v, b1v, b2v, resv, spheroid);
  104. res = bg::formula::cart3d_to_geo<point_geo>(resv, spheroid);
  105. result.lon = bg::get<0>(res);
  106. result.lat = bg::get<1>(res);
  107. check_result(result, results.great_elliptic, results.sjoberg_vincenty, 0.01, check_reference_only);
  108. }
  109. }
  110. void test_4_input_combinations(expected_results const& results, bool check_reference_only)
  111. {
  112. test_formulas(results, check_reference_only);
  113. #ifdef BOOST_GEOMETRY_TEST_GEO_INTERSECTION_TEST_SIMILAR
  114. {
  115. expected_results results_alt = results;
  116. std::swap(results_alt.p1, results_alt.p2);
  117. test_formulas(results_alt, true);
  118. }
  119. {
  120. expected_results results_alt = results;
  121. std::swap(results_alt.q1, results_alt.q2);
  122. test_formulas(results_alt, true);
  123. }
  124. {
  125. expected_results results_alt = results;
  126. std::swap(results_alt.p1, results_alt.p2);
  127. std::swap(results_alt.q1, results_alt.q2);
  128. test_formulas(results_alt, true);
  129. }
  130. #endif
  131. }
  132. void test_all(expected_results const& results)
  133. {
  134. test_4_input_combinations(results, false);
  135. #ifdef BOOST_GEOMETRY_TEST_GEO_INTERSECTION_TEST_SIMILAR
  136. expected_results results_alt = results;
  137. results_alt.p1.lat *= -1;
  138. results_alt.p2.lat *= -1;
  139. results_alt.q1.lat *= -1;
  140. results_alt.q2.lat *= -1;
  141. results_alt.gnomonic_vincenty.lat *= -1;
  142. results_alt.gnomonic_thomas.lat *= -1;
  143. results_alt.sjoberg_vincenty.lat *= -1;
  144. results_alt.sjoberg_thomas.lat *= -1;
  145. results_alt.sjoberg_andoyer.lat *= -1;
  146. results_alt.great_elliptic.lat *= -1;
  147. test_4_input_combinations(results_alt, true);
  148. #endif
  149. }
  150. void test_bugs()
  151. {
  152. // https://github.com/boostorg/geometry/issues/612
  153. {
  154. double lon, lat;
  155. bg::formula::sjoberg_intersection<double, bg::formula::andoyer_inverse, 1>
  156. ::apply(-0.0872665, -0.0872665, -0.0872665, 0.0872665,
  157. 0.0, 1.57e-07, -0.392699, 1.57e-07,
  158. lon, lat, bg::srs::spheroid<double>());
  159. check_one("issue 612", lon, -0.087266500535674751);
  160. check_one("issue 612", lat, 1.5892499139622920e-07);
  161. }
  162. }
  163. int test_main(int, char*[])
  164. {
  165. for (size_t i = 0; i < expected_size; ++i)
  166. {
  167. test_all(expected[i]);
  168. }
  169. test_bugs();
  170. return 0;
  171. }