interpolate_point_spherical.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Boost.Geometry
  2. // Copyright (c) 2019-2023 Oracle and/or its affiliates.
  3. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, 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. #ifndef BOOST_GEOMETRY_FORMULAS_INTERPOLATE_POINT_SPHERICAL_HPP
  9. #define BOOST_GEOMETRY_FORMULAS_INTERPOLATE_POINT_SPHERICAL_HPP
  10. #include <boost/geometry/arithmetic/normalize.hpp>
  11. #include <boost/geometry/formulas/spherical.hpp>
  12. #include <boost/geometry/geometries/point.hpp>
  13. namespace boost { namespace geometry { namespace formula
  14. {
  15. template <typename CalculationType>
  16. class interpolate_point_spherical
  17. {
  18. typedef model::point<CalculationType, 3, cs::cartesian> point3d_t;
  19. public :
  20. template <typename Point>
  21. void compute_angle(Point const& p0,
  22. Point const& p1,
  23. CalculationType& angle01)
  24. {
  25. m_xyz0 = formula::sph_to_cart3d<point3d_t>(p0);
  26. m_xyz1 = formula::sph_to_cart3d<point3d_t>(p1);
  27. CalculationType const dot01 = geometry::dot_product(m_xyz0, m_xyz1);
  28. angle01 = acos(dot01);
  29. }
  30. template <typename Point>
  31. void compute_axis(Point const& p0,
  32. CalculationType const& angle01)
  33. {
  34. CalculationType const c0 = 0, c1 = 1;
  35. CalculationType const pi = math::pi<CalculationType>();
  36. if (! math::equals(angle01, pi))
  37. {
  38. m_axis = geometry::cross_product(m_xyz0, m_xyz1);
  39. geometry::detail::vec_normalize(m_axis);
  40. }
  41. else // antipodal
  42. {
  43. CalculationType const half_pi = math::half_pi<CalculationType>();
  44. CalculationType const lat = geometry::get_as_radian<1>(p0);
  45. if (math::equals(lat, half_pi))
  46. {
  47. // pointing east, segment lies on prime meridian, going south
  48. m_axis = point3d_t(c0, c1, c0);
  49. }
  50. else if (math::equals(lat, -half_pi))
  51. {
  52. // pointing west, segment lies on prime meridian, going north
  53. m_axis = point3d_t(c0, -c1, c0);
  54. }
  55. else
  56. {
  57. // lon rotated west by pi/2 at equator
  58. CalculationType const lon = geometry::get_as_radian<0>(p0);
  59. m_axis = point3d_t(sin(lon), -cos(lon), c0);
  60. }
  61. }
  62. }
  63. template <typename Point>
  64. void compute_point(CalculationType const& a, Point& p)
  65. {
  66. CalculationType const c1 = 1;
  67. // Axis-Angle rotation
  68. // see: https://en.wikipedia.org/wiki/Axis-angle_representation
  69. CalculationType const cos_a = cos(a);
  70. CalculationType const sin_a = sin(a);
  71. // cos_a * v
  72. point3d_t s1 = m_xyz0;
  73. geometry::multiply_value(s1, cos_a);
  74. // sin_a * (n x v)
  75. point3d_t s2 = geometry::cross_product(m_axis, m_xyz0);
  76. geometry::multiply_value(s2, sin_a);
  77. // (1 - cos_a)(n.v) * n
  78. point3d_t s3 = m_axis;
  79. geometry::multiply_value(s3, (c1 - cos_a) *
  80. geometry::dot_product(m_axis, m_xyz0));
  81. // v_rot = cos_a * v + sin_a * (n x v) + (1 - cos_a)(n.v) * e
  82. point3d_t v_rot = s1;
  83. geometry::add_point(v_rot, s2);
  84. geometry::add_point(v_rot, s3);
  85. p = formula::cart3d_to_sph<Point>(v_rot);
  86. }
  87. private :
  88. point3d_t m_xyz0;
  89. point3d_t m_xyz1;
  90. point3d_t m_axis;
  91. };
  92. }}} // namespace boost::geometry::formula
  93. #endif // BOOST_GEOMETRY_FORMULAS_INTERPOLATE_POINT_SPHERICAL_HPP