densify.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Boost.Geometry
  2. // Copyright (c) 2017-2018, 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/strategies/densify.hpp>
  16. #include <boost/geometry/util/math.hpp>
  17. #include <boost/geometry/util/select_most_precise.hpp>
  18. namespace boost { namespace geometry
  19. {
  20. namespace strategy { namespace densify
  21. {
  22. /*!
  23. \brief Densification of cartesian segment.
  24. \ingroup strategies
  25. \tparam CalculationType \tparam_calculation
  26. \qbk{
  27. [heading See also]
  28. [link geometry.reference.algorithms.densify.densify_4_with_strategy densify (with strategy)]
  29. }
  30. */
  31. template
  32. <
  33. typename CalculationType = void
  34. >
  35. class cartesian
  36. {
  37. public:
  38. template <typename Point, typename AssignPolicy, typename T>
  39. static inline void apply(Point const& p0, Point const& p1, AssignPolicy & policy, T const& length_threshold)
  40. {
  41. typedef typename AssignPolicy::point_type out_point_t;
  42. typedef typename select_most_precise
  43. <
  44. typename coordinate_type<Point>::type,
  45. typename coordinate_type<out_point_t>::type,
  46. CalculationType
  47. >::type calc_t;
  48. typedef model::point<calc_t, geometry::dimension<Point>::value, cs::cartesian> calc_point_t;
  49. calc_point_t cp0, cp1;
  50. geometry::detail::conversion::convert_point_to_point(p0, cp0);
  51. geometry::detail::conversion::convert_point_to_point(p1, cp1);
  52. // dir01 = xy1 - xy0
  53. calc_point_t dir01 = cp1;
  54. geometry::subtract_point(dir01, cp0);
  55. calc_t const dot01 = geometry::dot_product(dir01, dir01);
  56. calc_t const len = math::sqrt(dot01);
  57. BOOST_GEOMETRY_ASSERT(length_threshold > T(0));
  58. signed_size_type n = signed_size_type(len / length_threshold);
  59. if (n <= 0)
  60. {
  61. return;
  62. }
  63. // NOTE: Normalization will not work for integral coordinates
  64. // normalize
  65. //geometry::divide_value(dir01, len);
  66. calc_t step = len / (n + 1);
  67. calc_t d = step;
  68. for (signed_size_type i = 0 ; i < n ; ++i, d += step)
  69. {
  70. // pd = xy0 + d * dir01
  71. calc_point_t pd = dir01;
  72. // without normalization
  73. geometry::multiply_value(pd, calc_t(i + 1));
  74. geometry::divide_value(pd, calc_t(n + 1));
  75. // with normalization
  76. //geometry::multiply_value(pd, d);
  77. geometry::add_point(pd, cp0);
  78. // NOTE: Only needed if types calc_point_t and out_point_t are different
  79. // otherwise pd could simply be passed into policy
  80. out_point_t p;
  81. assert_dimension_equal<calc_point_t, out_point_t>();
  82. geometry::detail::conversion::convert_point_to_point(pd, p);
  83. policy.apply(p);
  84. }
  85. }
  86. };
  87. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  88. namespace services
  89. {
  90. template <>
  91. struct default_strategy<cartesian_tag>
  92. {
  93. typedef strategy::densify::cartesian<> type;
  94. };
  95. } // namespace services
  96. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  97. }} // namespace strategy::densify
  98. }} // namespace boost::geometry
  99. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DENSIFY_HPP