interpolate_point_spherical.hpp 3.5 KB

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