rescale_policy.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2014-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2014-2015 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2014-2015 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2014-2015 Adam Wulkiewicz, Lodz, Poland.
  6. // Copyright (c) 2015, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #ifndef BOOST_GEOMETRY_POLICIES_ROBUSTNESS_RESCALE_POLICY_HPP
  12. #define BOOST_GEOMETRY_POLICIES_ROBUSTNESS_RESCALE_POLICY_HPP
  13. #include <cstddef>
  14. #include <boost/geometry/policies/robustness/segment_ratio.hpp>
  15. #include <boost/geometry/policies/robustness/segment_ratio_type.hpp>
  16. #include <boost/geometry/policies/robustness/robust_point_type.hpp>
  17. #include <boost/geometry/util/math.hpp>
  18. namespace boost { namespace geometry
  19. {
  20. #ifndef DOXYGEN_NO_DETAIL
  21. namespace detail
  22. {
  23. template <typename FpPoint, typename IntPoint, typename CalculationType>
  24. struct robust_policy
  25. {
  26. static bool const enabled = true;
  27. typedef typename geometry::coordinate_type<IntPoint>::type output_ct;
  28. robust_policy(FpPoint const& fp_min, IntPoint const& int_min, CalculationType const& the_factor)
  29. : m_fp_min(fp_min)
  30. , m_int_min(int_min)
  31. , m_multiplier(the_factor)
  32. {
  33. }
  34. template <std::size_t Dimension, typename Value>
  35. inline output_ct apply(Value const& value) const
  36. {
  37. // a + (v-b)*f
  38. CalculationType const a = static_cast<CalculationType>(get<Dimension>(m_int_min));
  39. CalculationType const b = static_cast<CalculationType>(get<Dimension>(m_fp_min));
  40. CalculationType const result = a + (value - b) * m_multiplier;
  41. return geometry::math::rounding_cast<output_ct>(result);
  42. }
  43. FpPoint m_fp_min;
  44. IntPoint m_int_min;
  45. CalculationType m_multiplier;
  46. };
  47. } // namespace detail
  48. #endif
  49. // Implement meta-functions for this policy
  50. // Define the IntPoint as a robust-point type
  51. template <typename Point, typename FpPoint, typename IntPoint, typename CalculationType>
  52. struct robust_point_type<Point, detail::robust_policy<FpPoint, IntPoint, CalculationType> >
  53. {
  54. typedef IntPoint type;
  55. };
  56. // Meta function for rescaling, if rescaling is done segment_ratio is based on long long
  57. template <typename Point, typename FpPoint, typename IntPoint, typename CalculationType>
  58. struct segment_ratio_type<Point, detail::robust_policy<FpPoint, IntPoint, CalculationType> >
  59. {
  60. typedef segment_ratio<boost::long_long_type> type;
  61. };
  62. }} // namespace boost::geometry
  63. #endif // BOOST_GEOMETRY_POLICIES_ROBUSTNESS_RESCALE_POLICY_HPP