side_info.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2024.
  6. // Modifications copyright (c) 2024 Oracle and/or its affiliates.
  7. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_STRATEGIES_SIDE_INFO_HPP
  14. #define BOOST_GEOMETRY_STRATEGIES_SIDE_INFO_HPP
  15. #include <cmath>
  16. #include <utility>
  17. #if defined(BOOST_GEOMETRY_DEBUG_INTERSECTION)
  18. # include <iostream>
  19. #endif
  20. namespace boost { namespace geometry
  21. {
  22. // Silence warning C4127: conditional expression is constant
  23. #if defined(_MSC_VER)
  24. #pragma warning(push)
  25. #pragma warning(disable : 4127)
  26. #endif
  27. /*!
  28. \brief Class side_info: small class wrapping for sides (-1,0,1)
  29. */
  30. class side_info
  31. {
  32. public :
  33. inline side_info(int side_a1 = 0, int side_a2 = 0,
  34. int side_b1 = 0, int side_b2 = 0)
  35. {
  36. sides[0].first = side_a1;
  37. sides[0].second = side_a2;
  38. sides[1].first = side_b1;
  39. sides[1].second = side_b2;
  40. }
  41. template <int Which>
  42. inline void set(int first, int second)
  43. {
  44. sides[Which].first = first;
  45. sides[Which].second = second;
  46. }
  47. template <int Which, int Index>
  48. inline void correct_to_zero()
  49. {
  50. if (Index == 0)
  51. {
  52. sides[Which].first = 0;
  53. }
  54. else
  55. {
  56. sides[Which].second = 0;
  57. }
  58. }
  59. template <int Which, int Index>
  60. inline int get() const
  61. {
  62. return Index == 0 ? sides[Which].first : sides[Which].second;
  63. }
  64. // Returns true if both lying on the same side WRT the other
  65. // (so either 1,1 or -1-1)
  66. template <int Which>
  67. inline bool same() const
  68. {
  69. return sides[Which].first * sides[Which].second == 1;
  70. }
  71. inline bool collinear() const
  72. {
  73. return sides[0].first == 0
  74. && sides[0].second == 0
  75. && sides[1].first == 0
  76. && sides[1].second == 0;
  77. }
  78. inline bool crossing() const
  79. {
  80. return sides[0].first * sides[0].second == -1
  81. && sides[1].first * sides[1].second == -1;
  82. }
  83. inline bool touching() const
  84. {
  85. return (sides[0].first * sides[1].first == -1
  86. && sides[0].second == 0 && sides[1].second == 0)
  87. || (sides[1].first * sides[0].first == -1
  88. && sides[1].second == 0 && sides[0].second == 0);
  89. }
  90. template <int Which>
  91. inline bool one_touching() const
  92. {
  93. // This is normally a situation which can't occur:
  94. // If one is completely left or right, the other cannot touch
  95. return one_zero<Which>()
  96. && sides[1 - Which].first * sides[1 - Which].second == 1;
  97. }
  98. inline bool meeting() const
  99. {
  100. // Two of them (in each segment) zero, two not
  101. return one_zero<0>() && one_zero<1>();
  102. }
  103. template <int Which>
  104. inline bool zero() const
  105. {
  106. return sides[Which].first == 0 && sides[Which].second == 0;
  107. }
  108. template <int Which>
  109. inline bool one_zero() const
  110. {
  111. return (sides[Which].first == 0 && sides[Which].second != 0)
  112. || (sides[Which].first != 0 && sides[Which].second == 0);
  113. }
  114. inline bool one_of_all_zero() const
  115. {
  116. int const sum = std::abs(sides[0].first)
  117. + std::abs(sides[0].second)
  118. + std::abs(sides[1].first)
  119. + std::abs(sides[1].second);
  120. return sum == 3;
  121. }
  122. template <int Which>
  123. inline int zero_index() const
  124. {
  125. return sides[Which].first == 0 ? 0 : 1;
  126. }
  127. #if defined(BOOST_GEOMETRY_DEBUG_INTERSECTION)
  128. inline void debug() const
  129. {
  130. std::cout << sides[0].first << " "
  131. << sides[0].second << " "
  132. << sides[1].first << " "
  133. << sides[1].second
  134. << std::endl;
  135. }
  136. #endif
  137. inline void reverse()
  138. {
  139. std::swap(sides[0], sides[1]);
  140. }
  141. //private :
  142. std::pair<int, int> sides[2];
  143. };
  144. #if defined(_MSC_VER)
  145. #pragma warning(pop)
  146. #endif
  147. }} // namespace boost::geometry
  148. #endif // BOOST_GEOMETRY_STRATEGIES_SIDE_INFO_HPP