densify.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Boost.Geometry
  2. // Copyright (c) 2017-2018, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  4. // Licensed under the Boost Software License version 1.0.
  5. // http://www.boost.org/users/license.html
  6. #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DENSIFY_HPP
  7. #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DENSIFY_HPP
  8. #include <boost/geometry/algorithms/detail/convert_point_to_point.hpp>
  9. #include <boost/geometry/algorithms/detail/signed_size_type.hpp>
  10. #include <boost/geometry/arithmetic/arithmetic.hpp>
  11. #include <boost/geometry/arithmetic/cross_product.hpp>
  12. #include <boost/geometry/arithmetic/dot_product.hpp>
  13. #include <boost/geometry/arithmetic/normalize.hpp>
  14. #include <boost/geometry/core/assert.hpp>
  15. #include <boost/geometry/core/coordinate_dimension.hpp>
  16. #include <boost/geometry/core/coordinate_type.hpp>
  17. #include <boost/geometry/core/radian_access.hpp>
  18. #include <boost/geometry/formulas/spherical.hpp>
  19. #include <boost/geometry/srs/sphere.hpp>
  20. #include <boost/geometry/strategies/densify.hpp>
  21. #include <boost/geometry/strategies/spherical/get_radius.hpp>
  22. #include <boost/geometry/util/math.hpp>
  23. #include <boost/geometry/util/select_most_precise.hpp>
  24. namespace boost { namespace geometry
  25. {
  26. namespace strategy { namespace densify
  27. {
  28. /*!
  29. \brief Densification of spherical segment.
  30. \ingroup strategies
  31. \tparam RadiusTypeOrSphere \tparam_radius_or_sphere
  32. \tparam CalculationType \tparam_calculation
  33. \qbk{
  34. [heading See also]
  35. [link geometry.reference.algorithms.densify.densify_4_with_strategy densify (with strategy)]
  36. }
  37. */
  38. template
  39. <
  40. typename RadiusTypeOrSphere = double,
  41. typename CalculationType = void
  42. >
  43. class spherical
  44. {
  45. public:
  46. // For consistency with area strategy the radius is set to 1
  47. inline spherical()
  48. : m_radius(1.0)
  49. {}
  50. template <typename RadiusOrSphere>
  51. explicit inline spherical(RadiusOrSphere const& radius_or_sphere)
  52. : m_radius(strategy_detail::get_radius
  53. <
  54. RadiusOrSphere
  55. >::apply(radius_or_sphere))
  56. {}
  57. template <typename Point, typename AssignPolicy, typename T>
  58. inline void apply(Point const& p0, Point const& p1, AssignPolicy & policy, T const& length_threshold) const
  59. {
  60. typedef typename AssignPolicy::point_type out_point_t;
  61. typedef typename select_most_precise
  62. <
  63. typename coordinate_type<Point>::type,
  64. typename coordinate_type<out_point_t>::type,
  65. CalculationType
  66. >::type calc_t;
  67. calc_t const c0 = 0;
  68. calc_t const c1 = 1;
  69. calc_t const pi = math::pi<calc_t>();
  70. typedef model::point<calc_t, 3, cs::cartesian> point3d_t;
  71. point3d_t const xyz0 = formula::sph_to_cart3d<point3d_t>(p0);
  72. point3d_t const xyz1 = formula::sph_to_cart3d<point3d_t>(p1);
  73. calc_t const dot01 = geometry::dot_product(xyz0, xyz1);
  74. calc_t const angle01 = acos(dot01);
  75. BOOST_GEOMETRY_ASSERT(length_threshold > T(0));
  76. signed_size_type n = signed_size_type(angle01 * m_radius / length_threshold);
  77. if (n <= 0)
  78. return;
  79. point3d_t axis;
  80. if (! math::equals(angle01, pi))
  81. {
  82. axis = geometry::cross_product(xyz0, xyz1);
  83. geometry::detail::vec_normalize(axis);
  84. }
  85. else // antipodal
  86. {
  87. calc_t const half_pi = math::half_pi<calc_t>();
  88. calc_t const lat = geometry::get_as_radian<1>(p0);
  89. if (math::equals(lat, half_pi))
  90. {
  91. // pointing east, segment lies on prime meridian, going south
  92. axis = point3d_t(c0, c1, c0);
  93. }
  94. else if (math::equals(lat, -half_pi))
  95. {
  96. // pointing west, segment lies on prime meridian, going north
  97. axis = point3d_t(c0, -c1, c0);
  98. }
  99. else
  100. {
  101. // lon rotated west by pi/2 at equator
  102. calc_t const lon = geometry::get_as_radian<0>(p0);
  103. axis = point3d_t(sin(lon), -cos(lon), c0);
  104. }
  105. }
  106. calc_t step = angle01 / (n + 1);
  107. calc_t a = step;
  108. for (signed_size_type i = 0 ; i < n ; ++i, a += step)
  109. {
  110. // Axis-Angle rotation
  111. // see: https://en.wikipedia.org/wiki/Axis-angle_representation
  112. calc_t const cos_a = cos(a);
  113. calc_t const sin_a = sin(a);
  114. // cos_a * v
  115. point3d_t s1 = xyz0;
  116. geometry::multiply_value(s1, cos_a);
  117. // sin_a * (n x v)
  118. point3d_t s2 = geometry::cross_product(axis, xyz0);
  119. geometry::multiply_value(s2, sin_a);
  120. // (1 - cos_a)(n.v) * n
  121. point3d_t s3 = axis;
  122. geometry::multiply_value(s3, (c1 - cos_a) * geometry::dot_product(axis, xyz0));
  123. // v_rot = cos_a * v + sin_a * (n x v) + (1 - cos_a)(n.v) * e
  124. point3d_t v_rot = s1;
  125. geometry::add_point(v_rot, s2);
  126. geometry::add_point(v_rot, s3);
  127. out_point_t p = formula::cart3d_to_sph<out_point_t>(v_rot);
  128. geometry::detail::conversion::point_to_point
  129. <
  130. Point, out_point_t,
  131. 2, dimension<out_point_t>::value
  132. >::apply(p0, p);
  133. policy.apply(p);
  134. }
  135. }
  136. private:
  137. typename strategy_detail::get_radius
  138. <
  139. RadiusTypeOrSphere
  140. >::type m_radius;
  141. };
  142. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  143. namespace services
  144. {
  145. template <>
  146. struct default_strategy<spherical_equatorial_tag>
  147. {
  148. typedef strategy::densify::spherical<> type;
  149. };
  150. } // namespace services
  151. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  152. }} // namespace strategy::densify
  153. }} // namespace boost::geometry
  154. #endif // BOOST_GEOMETRY_ALGORITHMS_DENSIFY_HPP