densify.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Boost.Geometry
  2. // Copyright (c) 2017-2021, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, 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_DENSIFY_HPP
  7. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DENSIFY_HPP
  8. #include <boost/geometry/algorithms/detail/convert_point_to_point.hpp>
  9. #include <boost/geometry/algorithms/detail/signed_size_type.hpp>
  10. #include <boost/geometry/arithmetic/arithmetic.hpp>
  11. #include <boost/geometry/arithmetic/dot_product.hpp>
  12. #include <boost/geometry/core/assert.hpp>
  13. #include <boost/geometry/core/coordinate_dimension.hpp>
  14. #include <boost/geometry/core/coordinate_type.hpp>
  15. #include <boost/geometry/geometries/point.hpp>
  16. #include <boost/geometry/strategies/densify.hpp>
  17. #include <boost/geometry/util/algorithm.hpp>
  18. #include <boost/geometry/util/math.hpp>
  19. #include <boost/geometry/util/numeric_cast.hpp>
  20. #include <boost/geometry/util/select_most_precise.hpp>
  21. namespace boost { namespace geometry
  22. {
  23. namespace strategy { namespace densify
  24. {
  25. /*!
  26. \brief Densification of cartesian segment.
  27. \ingroup strategies
  28. \tparam CalculationType \tparam_calculation
  29. \qbk{
  30. [heading See also]
  31. [link geometry.reference.algorithms.densify.densify_4_with_strategy densify (with strategy)]
  32. }
  33. */
  34. template
  35. <
  36. typename CalculationType = void
  37. >
  38. class cartesian
  39. {
  40. public:
  41. template <typename Point, typename AssignPolicy, typename T>
  42. static inline void apply(Point const& p0, Point const& p1, AssignPolicy & policy, T const& length_threshold)
  43. {
  44. typedef typename AssignPolicy::point_type out_point_t;
  45. typedef typename coordinate_type<out_point_t>::type out_coord_t;
  46. typedef typename select_most_precise
  47. <
  48. typename coordinate_type<Point>::type, out_coord_t,
  49. CalculationType
  50. >::type calc_t;
  51. typedef model::point<calc_t, geometry::dimension<Point>::value, cs::cartesian> calc_point_t;
  52. assert_dimension_equal<calc_point_t, out_point_t>();
  53. calc_point_t cp0, dir01;
  54. // dir01 = p1 - p0
  55. geometry::detail::for_each_dimension<calc_point_t>([&](auto index)
  56. {
  57. calc_t const coord0 = util::numeric_cast<calc_t>(get<index>(p0));
  58. calc_t const coord1 = util::numeric_cast<calc_t>(get<index>(p1));
  59. set<index>(cp0, coord0);
  60. set<index>(dir01, coord1 - coord0);
  61. });
  62. calc_t const dot01 = geometry::dot_product(dir01, dir01);
  63. calc_t const len = math::sqrt(dot01);
  64. BOOST_GEOMETRY_ASSERT(length_threshold > T(0));
  65. signed_size_type const n = signed_size_type(len / length_threshold);
  66. if (n <= 0)
  67. {
  68. return;
  69. }
  70. calc_t const den = calc_t(n + 1);
  71. for (signed_size_type i = 0 ; i < n ; ++i)
  72. {
  73. out_point_t out;
  74. calc_t const num = calc_t(i + 1);
  75. geometry::detail::for_each_dimension<out_point_t>([&](auto index)
  76. {
  77. // out = p0 + d * dir01
  78. calc_t const coord = get<index>(cp0) + get<index>(dir01) * num / den;
  79. set<index>(out, util::numeric_cast<out_coord_t>(coord));
  80. });
  81. policy.apply(out);
  82. }
  83. }
  84. };
  85. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  86. namespace services
  87. {
  88. template <>
  89. struct default_strategy<cartesian_tag>
  90. {
  91. typedef strategy::densify::cartesian<> type;
  92. };
  93. } // namespace services
  94. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  95. }} // namespace strategy::densify
  96. }} // namespace boost::geometry
  97. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DENSIFY_HPP