line_interpolate.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Boost.Geometry
  2. // Copyright (c) 2018-2021, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_LINE_INTERPOLATE_HPP
  8. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_LINE_INTERPOLATE_HPP
  9. #include <boost/geometry/core/assert.hpp>
  10. #include <boost/geometry/core/coordinate_dimension.hpp>
  11. #include <boost/geometry/core/coordinate_type.hpp>
  12. #include <boost/geometry/strategies/line_interpolate.hpp>
  13. #include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
  14. #include <boost/geometry/util/algorithm.hpp>
  15. #include <boost/geometry/util/select_calculation_type.hpp>
  16. namespace boost { namespace geometry
  17. {
  18. namespace strategy { namespace line_interpolate
  19. {
  20. /*!
  21. \brief Interpolate point on a cartesian segment.
  22. \ingroup strategies
  23. \tparam CalculationType \tparam_calculation
  24. \tparam DistanceStrategy The underlying point-point distance strategy
  25. \qbk{
  26. [heading See also]
  27. \* [link geometry.reference.algorithms.line_interpolate.line_interpolate_4_with_strategy line_interpolate (with strategy)]
  28. }
  29. */
  30. template
  31. <
  32. typename CalculationType = void,
  33. typename DistanceStrategy = distance::pythagoras<CalculationType>
  34. >
  35. class cartesian
  36. {
  37. public:
  38. template <typename Point, typename Fraction, typename Distance>
  39. inline void apply(Point const& p0,
  40. Point const& p1,
  41. Fraction const& fraction,
  42. Point & p,
  43. Distance const&) const
  44. {
  45. typedef typename select_calculation_type_alt
  46. <
  47. CalculationType, Point
  48. >::type calc_t;
  49. typedef typename coordinate_type<Point>::type coord_t;
  50. //segment convex combination: p0*fraction + p1*(1-fraction)
  51. Fraction const one_minus_fraction = 1-fraction;
  52. geometry::detail::for_each_dimension<Point>([&](auto dimension)
  53. {
  54. // NOTE: numeric_cast is a leftover from convert, it could probably be ommited.
  55. // NOTE: the order of points is different than in the formula above
  56. // this is also a leftover from the previous implementation
  57. calc_t coord0 = boost::numeric_cast<calc_t>(get<dimension>(p0));
  58. calc_t coord1 = boost::numeric_cast<calc_t>(get<dimension>(p1));
  59. calc_t result = calc_t(coord1 * fraction) + calc_t(coord0 * one_minus_fraction);
  60. set<dimension>(p, boost::numeric_cast<coord_t>(result));
  61. });
  62. }
  63. };
  64. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  65. namespace services
  66. {
  67. template <>
  68. struct default_strategy<cartesian_tag>
  69. {
  70. typedef strategy::line_interpolate::cartesian<> type;
  71. };
  72. } // namespace services
  73. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  74. }} // namespace strategy::line_interpolate
  75. }} // namespace boost::geometry
  76. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_LINE_INTERPOLATE_HPP