rescale_policy.hpp 2.9 KB

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