rational.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2011-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2011-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2011-2012 Mateusz Loskot, London, UK.
  5. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_UTIL_RATIONAL_HPP
  11. #define BOOST_GEOMETRY_UTIL_RATIONAL_HPP
  12. #include <boost/rational.hpp>
  13. #include <boost/numeric/conversion/bounds.hpp>
  14. #include <boost/geometry/util/coordinate_cast.hpp>
  15. #include <boost/geometry/util/select_most_precise.hpp>
  16. namespace boost{ namespace geometry
  17. {
  18. // Specialize for Boost.Geometry's coordinate cast
  19. // (from string to coordinate type)
  20. namespace detail
  21. {
  22. template <typename T>
  23. struct coordinate_cast<rational<T> >
  24. {
  25. static inline void split_parts(std::string const& source, std::string::size_type p,
  26. T& before, T& after, bool& negate, std::string::size_type& len)
  27. {
  28. std::string before_part = source.substr(0, p);
  29. std::string const after_part = source.substr(p + 1);
  30. negate = false;
  31. if (before_part.size() > 0 && before_part[0] == '-')
  32. {
  33. negate = true;
  34. before_part.erase(0, 1);
  35. }
  36. before = atol(before_part.c_str());
  37. after = atol(after_part.c_str());
  38. len = after_part.length();
  39. }
  40. static inline rational<T> apply(std::string const& source)
  41. {
  42. T before, after;
  43. bool negate;
  44. std::string::size_type len;
  45. // Note: decimal comma is not (yet) supported, it does (and should) not
  46. // occur in a WKT, where points are comma separated.
  47. std::string::size_type p = source.find(".");
  48. if (p == std::string::npos)
  49. {
  50. p = source.find("/");
  51. if (p == std::string::npos)
  52. {
  53. return rational<T>(atol(source.c_str()));
  54. }
  55. split_parts(source, p, before, after, negate, len);
  56. return negate
  57. ? -rational<T>(before, after)
  58. : rational<T>(before, after)
  59. ;
  60. }
  61. split_parts(source, p, before, after, negate, len);
  62. T den = 1;
  63. for (std::string::size_type i = 0; i < len; i++)
  64. {
  65. den *= 10;
  66. }
  67. return negate
  68. ? -rational<T>(before) - rational<T>(after, den)
  69. : rational<T>(before) + rational<T>(after, den)
  70. ;
  71. }
  72. };
  73. } // namespace detail
  74. // Specialize for Boost.Geometry's select_most_precise
  75. template <typename T1, typename T2>
  76. struct select_most_precise<boost::rational<T1>, boost::rational<T2> >
  77. {
  78. typedef typename boost::rational
  79. <
  80. typename select_most_precise<T1, T2>::type
  81. > type;
  82. };
  83. template <typename T>
  84. struct select_most_precise<boost::rational<T>, double>
  85. {
  86. typedef typename boost::rational<T> type;
  87. };
  88. }} // namespace boost::geometry
  89. // Specializes boost::rational to boost::numeric::bounds
  90. namespace boost { namespace numeric
  91. {
  92. template<class T>
  93. struct bounds<rational<T> >
  94. {
  95. static inline rational<T> lowest()
  96. {
  97. return rational<T>(bounds<T>::lowest(), 1);
  98. }
  99. static inline rational<T> highest()
  100. {
  101. return rational<T>(bounds<T>::highest(), 1);
  102. }
  103. };
  104. }} // namespace boost::numeric
  105. // Support for boost::numeric_cast to int and to double (necessary for SVG-mapper)
  106. namespace boost { namespace numeric
  107. {
  108. template
  109. <
  110. typename T,
  111. typename Traits,
  112. typename OverflowHandler,
  113. typename Float2IntRounder,
  114. typename RawConverter,
  115. typename UserRangeChecker
  116. >
  117. struct converter<int, rational<T>, Traits, OverflowHandler, Float2IntRounder, RawConverter, UserRangeChecker>
  118. {
  119. static inline int convert(rational<T> const& arg)
  120. {
  121. return int(rational_cast<double>(arg));
  122. }
  123. };
  124. template
  125. <
  126. typename T,
  127. typename Traits,
  128. typename OverflowHandler,
  129. typename Float2IntRounder,
  130. typename RawConverter,
  131. typename UserRangeChecker
  132. >
  133. struct converter<double, rational<T>, Traits, OverflowHandler, Float2IntRounder, RawConverter, UserRangeChecker>
  134. {
  135. static inline double convert(rational<T> const& arg)
  136. {
  137. return rational_cast<double>(arg);
  138. }
  139. };
  140. }}
  141. #endif // BOOST_GEOMETRY_UTIL_RATIONAL_HPP