vertex_longitude.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Boost.Geometry
  2. // Unit Test
  3. // Copyright (c) 2017 Oracle and/or its affiliates.
  4. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #include <geometry_test_common.hpp>
  9. #include "test_formula.hpp"
  10. #include "vertex_longitude_cases.hpp"
  11. #include <boost/geometry/formulas/vertex_latitude.hpp>
  12. #include <boost/geometry/formulas/vertex_longitude.hpp>
  13. #include <boost/geometry/formulas/vincenty_inverse.hpp>
  14. #include <boost/geometry/formulas/thomas_inverse.hpp>
  15. #include <boost/geometry/formulas/andoyer_inverse.hpp>
  16. #include <boost/geometry/strategies/strategies.hpp>
  17. #include <boost/geometry/util/math.hpp>
  18. #define BOOST_GEOMETRY_TEST_DEBUG
  19. namespace bg = boost::geometry;
  20. template<typename CT>
  21. CT test_vrt_lon_sph(CT lon1r,
  22. CT lat1r,
  23. CT lon2r,
  24. CT lat2r)
  25. {
  26. CT a1 = bg::formula::spherical_azimuth(lon1r, lat1r, lon2r, lat2r);
  27. typedef bg::model::point<CT, 2,
  28. bg::cs::spherical_equatorial<bg::radian> > point;
  29. bg::model::segment<point> segment(point(lon1r, lat1r),
  30. point(lon2r, lat2r));
  31. bg::model::box<point> box;
  32. bg::envelope(segment, box);
  33. CT vertex_lat;
  34. CT lat_sum = lat1r + lat2r;
  35. if (lat_sum > CT(0))
  36. {
  37. vertex_lat = bg::get_as_radian<bg::max_corner, 1>(box);
  38. } else {
  39. vertex_lat = bg::get_as_radian<bg::min_corner, 1>(box);
  40. }
  41. bg::strategy::azimuth::spherical<> azimuth;
  42. return bg::formula::vertex_longitude
  43. <CT, bg::spherical_equatorial_tag>::
  44. apply(lon1r, lat1r,
  45. lon2r, lat2r,
  46. vertex_lat,
  47. a1,
  48. azimuth);
  49. }
  50. template
  51. <
  52. template <typename, bool, bool, bool, bool, bool> class FormulaPolicy,
  53. typename CT
  54. >
  55. CT test_vrt_lon_geo(CT lon1r,
  56. CT lat1r,
  57. CT lon2r,
  58. CT lat2r)
  59. {
  60. // WGS84
  61. bg::srs::spheroid<CT> spheroid(6378137.0, 6356752.3142451793);
  62. typedef FormulaPolicy<CT, false, true, false, false, false> formula;
  63. CT a1 = formula::apply(lon1r, lat1r, lon2r, lat2r, spheroid).azimuth;
  64. typedef bg::model::point<CT, 2, bg::cs::geographic<bg::radian> > geo_point;
  65. bg::model::segment<geo_point> segment(geo_point(lon1r, lat1r),
  66. geo_point(lon2r, lat2r));
  67. bg::model::box<geo_point> box;
  68. bg::envelope(segment, box);
  69. CT vertex_lat;
  70. CT lat_sum = lat1r + lat2r;
  71. if (lat_sum > CT(0))
  72. {
  73. vertex_lat = bg::get_as_radian<bg::max_corner, 1>(box);
  74. } else {
  75. vertex_lat = bg::get_as_radian<bg::min_corner, 1>(box);
  76. }
  77. bg::strategy::azimuth::geographic<> azimuth_geographic;
  78. return bg::formula::vertex_longitude
  79. <CT, bg::geographic_tag>::apply(lon1r, lat1r,
  80. lon2r, lat2r,
  81. vertex_lat,
  82. a1,
  83. azimuth_geographic);
  84. }
  85. void test_all(expected_results const& results)
  86. {
  87. double const d2r = bg::math::d2r<double>();
  88. double lon1r = results.p1.lon * d2r;
  89. double lat1r = results.p1.lat * d2r;
  90. double lon2r = results.p2.lon * d2r;
  91. double lat2r = results.p2.lat * d2r;
  92. if(lon1r > lon2r)
  93. {
  94. std::swap(lon1r, lon2r);
  95. std::swap(lat1r, lat2r);
  96. }
  97. double res_an = test_vrt_lon_geo<bg::formula::andoyer_inverse>
  98. (lon1r, lat1r, lon2r, lat2r);
  99. double res_th = test_vrt_lon_geo<bg::formula::thomas_inverse>
  100. (lon1r, lat1r, lon2r, lat2r);
  101. double res_vi = test_vrt_lon_geo<bg::formula::vincenty_inverse>
  102. (lon1r, lat1r, lon2r, lat2r);
  103. double res_sh = test_vrt_lon_sph(lon1r, lat1r, lon2r, lat2r);
  104. bg::math::normalize_longitude<bg::radian, double>(res_an);
  105. bg::math::normalize_longitude<bg::radian, double>(res_th);
  106. bg::math::normalize_longitude<bg::radian, double>(res_vi);
  107. bg::math::normalize_longitude<bg::radian, double>(res_sh);
  108. check_one(res_an, results.andoyer * d2r, res_vi, 0.001);
  109. check_one(res_th, results.thomas * d2r, res_vi, 0.01);//in some tests thomas gives low accuracy
  110. check_one(res_vi, results.vincenty * d2r, res_vi, 0.0000001);
  111. check_one(res_sh, results.spherical * d2r, res_vi, 1);
  112. }
  113. int test_main(int, char*[])
  114. {
  115. for (size_t i = 0; i < expected_size; ++i)
  116. {
  117. test_all(expected[i]);
  118. }
  119. return 0;
  120. }