line_interpolate.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/numeric_cast.hpp>
  16. #include <boost/geometry/util/select_calculation_type.hpp>
  17. namespace boost { namespace geometry
  18. {
  19. namespace strategy { namespace line_interpolate
  20. {
  21. /*!
  22. \brief Interpolate point on a cartesian segment.
  23. \ingroup strategies
  24. \tparam CalculationType \tparam_calculation
  25. \tparam DistanceStrategy The underlying point-point distance strategy
  26. \qbk{
  27. [heading See also]
  28. \* [link geometry.reference.algorithms.line_interpolate.line_interpolate_4_with_strategy line_interpolate (with strategy)]
  29. }
  30. */
  31. template
  32. <
  33. typename CalculationType = void,
  34. typename DistanceStrategy = distance::pythagoras<CalculationType>
  35. >
  36. class cartesian
  37. {
  38. public:
  39. template <typename Point, typename Fraction, typename Distance>
  40. inline void apply(Point const& p0,
  41. Point const& p1,
  42. Fraction const& fraction,
  43. Point & p,
  44. Distance const&) const
  45. {
  46. typedef typename select_calculation_type_alt
  47. <
  48. CalculationType, Point
  49. >::type calc_t;
  50. typedef typename coordinate_type<Point>::type coord_t;
  51. //segment convex combination: p0*fraction + p1*(1-fraction)
  52. Fraction const one_minus_fraction = 1-fraction;
  53. geometry::detail::for_each_dimension<Point>([&](auto dimension)
  54. {
  55. // NOTE: numeric_cast is a leftover from convert, it could probably be ommited.
  56. // NOTE: the order of points is different than in the formula above
  57. // this is also a leftover from the previous implementation
  58. calc_t coord0 = util::numeric_cast<calc_t>(get<dimension>(p0));
  59. calc_t coord1 = util::numeric_cast<calc_t>(get<dimension>(p1));
  60. calc_t result = calc_t(coord1 * fraction) + calc_t(coord0 * one_minus_fraction);
  61. set<dimension>(p, util::numeric_cast<coord_t>(result));
  62. });
  63. }
  64. };
  65. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  66. namespace services
  67. {
  68. template <>
  69. struct default_strategy<cartesian_tag>
  70. {
  71. typedef strategy::line_interpolate::cartesian<> type;
  72. };
  73. } // namespace services
  74. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  75. }} // namespace strategy::line_interpolate
  76. }} // namespace boost::geometry
  77. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_LINE_INTERPOLATE_HPP