side_rounded_input.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2021 Tinko Bartels, Berlin, Germany.
  3. // This file was modified by Oracle on 2025.
  4. // Modifications copyright (c) 2025 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_STRATEGY_CARTESIAN_SIDE_ROUNDED_INPUT_HPP
  10. #define BOOST_GEOMETRY_STRATEGY_CARTESIAN_SIDE_ROUNDED_INPUT_HPP
  11. #include <limits>
  12. #include <boost/geometry/core/access.hpp>
  13. #include <boost/geometry/core/config.hpp>
  14. #include <boost/geometry/util/math.hpp>
  15. #include <boost/geometry/strategies/side.hpp>
  16. #include <boost/geometry/util/select_calculation_type.hpp>
  17. namespace boost { namespace geometry
  18. {
  19. namespace strategy { namespace side
  20. {
  21. template <typename CalculationType = void, int Coeff1 = 5, int Coeff2 = 32>
  22. struct side_rounded_input
  23. {
  24. private:
  25. template <typename Point>
  26. static inline auto get_coordinate(Point const& point, std::size_t i)
  27. {
  28. switch(i) {
  29. case 0: return geometry::get<0>(point);
  30. case 1: return geometry::get<1>(point);
  31. case 2: return geometry::get<2>(point);
  32. default: BOOST_ASSERT(false);
  33. }
  34. return geometry::get<0>(point);
  35. }
  36. public:
  37. using cs_tag = cartesian_tag;
  38. template <typename P1, typename P2, typename P>
  39. static inline int apply(P1 const& p1, P2 const& p2, P const& p, int i, int j)
  40. {
  41. using coor_t = typename select_calculation_type_alt<CalculationType, P1, P2, P>::type;
  42. coor_t const p1_x = get_coordinate(p1, i);
  43. coor_t const p1_y = get_coordinate(p1, j);
  44. coor_t const p2_x = get_coordinate(p2, i);
  45. coor_t const p2_y = get_coordinate(p2, j);
  46. coor_t const p_x = get_coordinate(p, i);
  47. coor_t const p_y = get_coordinate(p, j);
  48. return apply(p1_x, p1_y, p2_x, p2_y, p_x, p_y);
  49. }
  50. template <typename P1, typename P2, typename P>
  51. static inline int apply(P1 const& p1, P2 const& p2, P const& p)
  52. {
  53. using coor_t = typename select_calculation_type_alt<CalculationType, P1, P2, P>::type;
  54. coor_t const p1_x = geometry::get<0>(p1);
  55. coor_t const p1_y = geometry::get<1>(p1);
  56. coor_t const p2_x = geometry::get<0>(p2);
  57. coor_t const p2_y = geometry::get<1>(p2);
  58. coor_t const p_x = geometry::get<0>(p);
  59. coor_t const p_y = geometry::get<1>(p);
  60. return apply(p1_x, p1_y, p2_x, p2_y, p_x, p_y);
  61. }
  62. template <typename CT>
  63. static inline int apply(CT const& p1_x, CT const& p1_y,
  64. CT const& p2_x, CT const& p2_y,
  65. CT const& p_x, CT const& p_y)
  66. {
  67. static CT const eps = std::numeric_limits<CT>::epsilon() / 2;
  68. CT const det = (p1_x - p_x) * (p2_y - p_y) - (p1_y - p_y) * (p2_x - p_x);
  69. CT const err_bound = (Coeff1 * eps + Coeff2 * eps * eps) *
  70. ( (geometry::math::abs(p1_x) + geometry::math::abs(p_x))
  71. * (geometry::math::abs(p2_y) + geometry::math::abs(p_y))
  72. + (geometry::math::abs(p2_x) + geometry::math::abs(p_x))
  73. * (geometry::math::abs(p1_y) + geometry::math::abs(p_y)));
  74. return (det > err_bound) - (det < -err_bound);
  75. }
  76. };
  77. }} // namespace strategy::side
  78. }} // namespace boost::geometry
  79. #endif // BOOST_GEOMETRY_STRATEGY_CARTESIAN_SIDE_ROUNDED_INPUT_HPP