azimuth.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2016-2021 Oracle and/or its affiliates.
  3. // Contributed and/or modified by Vissarion Fisikopoulos, 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_STRATEGIES_CARTESIAN_AZIMUTH_HPP
  9. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_AZIMUTH_HPP
  10. #include <cmath>
  11. #include <boost/geometry/core/tags.hpp>
  12. #include <boost/geometry/strategies/azimuth.hpp>
  13. #include <boost/geometry/util/promote_floating_point.hpp>
  14. #include <boost/geometry/util/select_most_precise.hpp>
  15. namespace boost { namespace geometry
  16. {
  17. namespace strategy { namespace azimuth
  18. {
  19. template <typename CalculationType = void>
  20. class cartesian
  21. {
  22. public:
  23. template <typename T1, typename T2>
  24. struct result_type
  25. : geometry::select_most_precise
  26. <
  27. // NOTE: this promotes any integer type to double
  28. typename geometry::promote_floating_point<T1, double>::type,
  29. typename geometry::promote_floating_point<T2, double>::type,
  30. CalculationType
  31. >
  32. {};
  33. template <typename T1, typename T2, typename Result>
  34. static inline void apply(T1 const& x1, T1 const& y1,
  35. T2 const& x2, T2 const& y2,
  36. Result& a1, Result& a2)
  37. {
  38. compute(x1, y1, x2, y2, a1, a2);
  39. }
  40. template <typename T1, typename T2, typename Result>
  41. static inline void apply(T1 const& x1, T1 const& y1,
  42. T2 const& x2, T2 const& y2,
  43. Result& a1)
  44. {
  45. compute(x1, y1, x2, y2, a1, a1);
  46. }
  47. template <typename T1, typename T2, typename Result>
  48. static inline void apply_reverse(T1 const& x1, T1 const& y1,
  49. T2 const& x2, T2 const& y2,
  50. Result& a2)
  51. {
  52. compute(x1, y1, x2, y2, a2, a2);
  53. }
  54. private:
  55. template <typename T1, typename T2, typename Result>
  56. static inline void compute(T1 const& x1, T1 const& y1,
  57. T2 const& x2, T2 const& y2,
  58. Result& a1, Result& a2)
  59. {
  60. typedef typename result_type<T1, T2>::type calc_t;
  61. // NOTE: azimuth 0 is at Y axis, increasing right
  62. // as in spherical/geographic where 0 is at North axis
  63. a1 = a2 = atan2(calc_t(x2) - calc_t(x1), calc_t(y2) - calc_t(y1));
  64. }
  65. };
  66. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  67. namespace services
  68. {
  69. template <>
  70. struct default_strategy<cartesian_tag>
  71. {
  72. typedef strategy::azimuth::cartesian<> type;
  73. };
  74. }
  75. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  76. }} // namespace strategy::azimuth
  77. }} // namespace boost::geometry
  78. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_AZIMUTH_HPP