coordinate_promotion.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Boost.Geometry
  2. // Copyright (c) 2021 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_CORE_COORDINATE_PROMOTION_HPP
  7. #define BOOST_GEOMETRY_CORE_COORDINATE_PROMOTION_HPP
  8. #include <boost/geometry/core/coordinate_type.hpp>
  9. // TODO: move this to a future headerfile implementing traits for these types
  10. #include <boost/rational.hpp>
  11. #include <boost/multiprecision/cpp_bin_float.hpp>
  12. #include <boost/multiprecision/cpp_int.hpp>
  13. namespace boost { namespace geometry
  14. {
  15. namespace traits
  16. {
  17. // todo
  18. } // namespace traits
  19. /*!
  20. \brief Meta-function converting, if necessary, to "a floating point" type
  21. \details
  22. - if input type is integer, type is double
  23. - else type is input type
  24. \ingroup utility
  25. */
  26. // TODO: replace with, or call, promoted_to_floating_point
  27. template <typename T, typename PromoteIntegerTo = double>
  28. struct promote_floating_point
  29. {
  30. typedef std::conditional_t
  31. <
  32. std::is_integral<T>::value,
  33. PromoteIntegerTo,
  34. T
  35. > type;
  36. };
  37. // TODO: replace with promoted_to_floating_point
  38. template <typename Geometry>
  39. struct fp_coordinate_type
  40. {
  41. typedef typename promote_floating_point
  42. <
  43. typename coordinate_type<Geometry>::type
  44. >::type type;
  45. };
  46. namespace detail
  47. {
  48. // Promote any integral type to double. Floating point
  49. // and other user defined types stay as they are, unless specialized.
  50. // TODO: we shold add a coordinate_promotion traits for promotion to
  51. // floating point or (larger) integer types.
  52. template <typename Type>
  53. struct promoted_to_floating_point
  54. {
  55. using type = std::conditional_t
  56. <
  57. std::is_integral<Type>::value, double, Type
  58. >;
  59. };
  60. // Boost.Rational goes to double
  61. template <typename T>
  62. struct promoted_to_floating_point<boost::rational<T>>
  63. {
  64. using type = double;
  65. };
  66. // Any Boost.Multiprecision goes to double (for example int128_t),
  67. // unless specialized
  68. template <typename Backend>
  69. struct promoted_to_floating_point<boost::multiprecision::number<Backend>>
  70. {
  71. using type = double;
  72. };
  73. // Boost.Multiprecision binary floating point numbers are used as FP.
  74. template <unsigned Digits>
  75. struct promoted_to_floating_point
  76. <
  77. boost::multiprecision::number
  78. <
  79. boost::multiprecision::cpp_bin_float<Digits>
  80. >
  81. >
  82. {
  83. using type = boost::multiprecision::number
  84. <
  85. boost::multiprecision::cpp_bin_float<Digits>
  86. >;
  87. };
  88. }
  89. }} // namespace boost::geometry
  90. #endif // BOOST_GEOMETRY_CORE_COORDINATE_PROMOTION_HPP