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