approximately_equals.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2021 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPROXIMATELY_EQUALS_HPP
  7. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPROXIMATELY_EQUALS_HPP
  8. #include <boost/geometry/core/access.hpp>
  9. #include <boost/geometry/util/math.hpp>
  10. #include <boost/geometry/util/select_coordinate_type.hpp>
  11. #include <boost/geometry/util/select_most_precise.hpp>
  12. namespace boost { namespace geometry
  13. {
  14. #ifndef DOXYGEN_NO_DETAIL
  15. namespace detail { namespace overlay
  16. {
  17. // Value for approximately_equals used by get_cluster and sort_by_side
  18. // This is an "epsilon_multiplier" and, therefore, multiplied the epsilon
  19. // belonging to the used floating point type with this value.
  20. template <typename T>
  21. struct common_approximately_equals_epsilon_multiplier
  22. {
  23. static T value()
  24. {
  25. // The value is (a bit) arbitrary. For sort_by_side it should be large
  26. // enough to not take a point which is too close by, to calculate the
  27. // side value correctly. For get_cluster it is arbitrary as well, points
  28. // close to each other should form a cluster, which is also important
  29. // for subsequent side calculations. Points too far apart should not be
  30. // clustered.
  31. //
  32. // The value of 100 is currently considered as a sweet spot.
  33. // If the value changes (as of 2023-09-13):
  34. // 10: too small, failing unit test(s):
  35. // - union: issue_1108
  36. // 50: this would be fine, no tests failing
  37. // 1000: this would be fine, no tests failing
  38. return T(100);
  39. }
  40. };
  41. template <typename Point1, typename Point2, typename E>
  42. inline bool approximately_equals(Point1 const& a, Point2 const& b,
  43. E const& epsilon_multiplier)
  44. {
  45. using coor_t = typename select_coordinate_type<Point1, Point2>::type;
  46. using calc_t = typename geometry::select_most_precise<coor_t, E>::type;
  47. calc_t const& a0 = geometry::get<0>(a);
  48. calc_t const& b0 = geometry::get<0>(b);
  49. calc_t const& a1 = geometry::get<1>(a);
  50. calc_t const& b1 = geometry::get<1>(b);
  51. math::detail::equals_factor_policy<calc_t> policy(a0, b0, a1, b1);
  52. policy.multiply_epsilon(epsilon_multiplier);
  53. return math::detail::equals_by_policy(a0, b0, policy)
  54. && math::detail::equals_by_policy(a1, b1, policy);
  55. }
  56. }} // namespace detail::overlay
  57. #endif //DOXYGEN_NO_DETAIL
  58. }} // namespace boost::geometry
  59. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPROXIMATELY_EQUALS_HPP