line_interpolate.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Boost.Geometry
  2. // Copyright (c) 2018, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  4. // Licensed under the Boost Software License version 1.0.
  5. // http://www.boost.org/users/license.html
  6. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_LINE_INTERPOLATE_HPP
  7. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_LINE_INTERPOLATE_HPP
  8. #include <boost/geometry/core/assert.hpp>
  9. #include <boost/geometry/core/coordinate_dimension.hpp>
  10. #include <boost/geometry/core/coordinate_type.hpp>
  11. #include <boost/geometry/strategies/line_interpolate.hpp>
  12. #include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
  13. #include <boost/geometry/util/select_calculation_type.hpp>
  14. namespace boost { namespace geometry
  15. {
  16. namespace strategy { namespace line_interpolate
  17. {
  18. /*!
  19. \brief Interpolate point on a cartesian segment.
  20. \ingroup strategies
  21. \tparam CalculationType \tparam_calculation
  22. \tparam DistanceStrategy The underlying point-point distance strategy
  23. \qbk{
  24. [heading See also]
  25. \* [link geometry.reference.algorithms.line_interpolate.line_interpolate_4_with_strategy line_interpolate (with strategy)]
  26. }
  27. */
  28. template
  29. <
  30. typename CalculationType = void,
  31. typename DistanceStrategy = distance::pythagoras<CalculationType>
  32. >
  33. class cartesian
  34. {
  35. public:
  36. // point-point strategy getters
  37. struct distance_pp_strategy
  38. {
  39. typedef DistanceStrategy type;
  40. };
  41. inline typename distance_pp_strategy::type get_distance_pp_strategy() const
  42. {
  43. typedef typename distance_pp_strategy::type distance_type;
  44. return distance_type();
  45. }
  46. template <typename Point, typename Fraction, typename Distance>
  47. inline void apply(Point const& p0,
  48. Point const& p1,
  49. Fraction const& fraction,
  50. Point & p,
  51. Distance const&) const
  52. {
  53. typedef typename select_calculation_type_alt
  54. <
  55. CalculationType,
  56. Point
  57. >::type calc_t;
  58. typedef model::point
  59. <
  60. calc_t,
  61. geometry::dimension<Point>::value,
  62. cs::cartesian
  63. > calc_point_t;
  64. calc_point_t cp0, cp1;
  65. geometry::detail::conversion::convert_point_to_point(p0, cp0);
  66. geometry::detail::conversion::convert_point_to_point(p1, cp1);
  67. //segment convex combination: p0*fraction + p1*(1-fraction)
  68. Fraction const one_minus_fraction = 1-fraction;
  69. for_each_coordinate(cp1, detail::value_operation
  70. <
  71. Fraction,
  72. std::multiplies
  73. >(fraction));
  74. for_each_coordinate(cp0, detail::value_operation
  75. <
  76. Fraction,
  77. std::multiplies
  78. >(one_minus_fraction));
  79. for_each_coordinate(cp1, detail::point_operation
  80. <
  81. calc_point_t,
  82. std::plus
  83. >(cp0));
  84. assert_dimension_equal<calc_point_t, Point>();
  85. geometry::detail::conversion::convert_point_to_point(cp1, p);
  86. }
  87. };
  88. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  89. namespace services
  90. {
  91. template <>
  92. struct default_strategy<cartesian_tag>
  93. {
  94. typedef strategy::line_interpolate::cartesian<> type;
  95. };
  96. } // namespace services
  97. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  98. }} // namespace strategy::line_interpolate
  99. }} // namespace boost::geometry
  100. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_LINE_INTERPOLATE_HPP