densify.hpp 3.7 KB

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