disjoint_segment_box.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Boost.Geometry
  2. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2013-2014 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2013-2019.
  7. // Modifications copyright (c) 2013-2019, Oracle and/or its affiliates.
  8. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  9. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  10. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISJOINT_SEGMENT_BOX_HPP
  15. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISJOINT_SEGMENT_BOX_HPP
  16. #include <cstddef>
  17. #include <utility>
  18. #include <boost/numeric/conversion/cast.hpp>
  19. #include <boost/geometry/util/math.hpp>
  20. #include <boost/geometry/util/calculation_type.hpp>
  21. #include <boost/geometry/core/access.hpp>
  22. #include <boost/geometry/core/tags.hpp>
  23. #include <boost/geometry/core/coordinate_dimension.hpp>
  24. #include <boost/geometry/core/point_type.hpp>
  25. #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
  26. #include <boost/geometry/strategies/cartesian/point_in_box.hpp>
  27. #include <boost/geometry/strategies/disjoint.hpp>
  28. namespace boost { namespace geometry { namespace strategy { namespace disjoint
  29. {
  30. namespace detail
  31. {
  32. template <std::size_t I>
  33. struct compute_tmin_tmax_per_dim
  34. {
  35. template <typename SegmentPoint, typename Box, typename RelativeDistance>
  36. static inline void apply(SegmentPoint const& p0,
  37. SegmentPoint const& p1,
  38. Box const& box,
  39. RelativeDistance& ti_min,
  40. RelativeDistance& ti_max,
  41. RelativeDistance& diff)
  42. {
  43. typedef typename coordinate_type<Box>::type box_coordinate_type;
  44. typedef typename coordinate_type
  45. <
  46. SegmentPoint
  47. >::type point_coordinate_type;
  48. RelativeDistance c_p0 = boost::numeric_cast
  49. <
  50. point_coordinate_type
  51. >( geometry::get<I>(p0) );
  52. RelativeDistance c_p1 = boost::numeric_cast
  53. <
  54. point_coordinate_type
  55. >( geometry::get<I>(p1) );
  56. RelativeDistance c_b_min = boost::numeric_cast
  57. <
  58. box_coordinate_type
  59. >( geometry::get<geometry::min_corner, I>(box) );
  60. RelativeDistance c_b_max = boost::numeric_cast
  61. <
  62. box_coordinate_type
  63. >( geometry::get<geometry::max_corner, I>(box) );
  64. if ( geometry::get<I>(p1) >= geometry::get<I>(p0) )
  65. {
  66. diff = c_p1 - c_p0;
  67. ti_min = c_b_min - c_p0;
  68. ti_max = c_b_max - c_p0;
  69. }
  70. else
  71. {
  72. diff = c_p0 - c_p1;
  73. ti_min = c_p0 - c_b_max;
  74. ti_max = c_p0 - c_b_min;
  75. }
  76. }
  77. };
  78. template
  79. <
  80. typename RelativeDistance,
  81. typename SegmentPoint,
  82. typename Box,
  83. std::size_t I,
  84. std::size_t Dimension
  85. >
  86. struct disjoint_segment_box_impl
  87. {
  88. template <typename RelativeDistancePair>
  89. static inline bool apply(SegmentPoint const& p0,
  90. SegmentPoint const& p1,
  91. Box const& box,
  92. RelativeDistancePair& t_min,
  93. RelativeDistancePair& t_max)
  94. {
  95. RelativeDistance ti_min, ti_max, diff;
  96. compute_tmin_tmax_per_dim<I>::apply(p0, p1, box, ti_min, ti_max, diff);
  97. if ( geometry::math::equals(diff, 0) )
  98. {
  99. if ( (geometry::math::equals(t_min.second, 0)
  100. && t_min.first > ti_max)
  101. ||
  102. (geometry::math::equals(t_max.second, 0)
  103. && t_max.first < ti_min)
  104. ||
  105. (math::sign(ti_min) * math::sign(ti_max) > 0) )
  106. {
  107. return true;
  108. }
  109. }
  110. RelativeDistance t_min_x_diff = t_min.first * diff;
  111. RelativeDistance t_max_x_diff = t_max.first * diff;
  112. if ( t_min_x_diff > ti_max * t_min.second
  113. || t_max_x_diff < ti_min * t_max.second )
  114. {
  115. return true;
  116. }
  117. if ( ti_min * t_min.second > t_min_x_diff )
  118. {
  119. t_min.first = ti_min;
  120. t_min.second = diff;
  121. }
  122. if ( ti_max * t_max.second < t_max_x_diff )
  123. {
  124. t_max.first = ti_max;
  125. t_max.second = diff;
  126. }
  127. if ( t_min.first > t_min.second || t_max.first < 0 )
  128. {
  129. return true;
  130. }
  131. return disjoint_segment_box_impl
  132. <
  133. RelativeDistance,
  134. SegmentPoint,
  135. Box,
  136. I + 1,
  137. Dimension
  138. >::apply(p0, p1, box, t_min, t_max);
  139. }
  140. };
  141. template
  142. <
  143. typename RelativeDistance,
  144. typename SegmentPoint,
  145. typename Box,
  146. std::size_t Dimension
  147. >
  148. struct disjoint_segment_box_impl
  149. <
  150. RelativeDistance, SegmentPoint, Box, 0, Dimension
  151. >
  152. {
  153. static inline bool apply(SegmentPoint const& p0,
  154. SegmentPoint const& p1,
  155. Box const& box)
  156. {
  157. std::pair<RelativeDistance, RelativeDistance> t_min, t_max;
  158. RelativeDistance diff;
  159. compute_tmin_tmax_per_dim<0>::apply(p0, p1, box,
  160. t_min.first, t_max.first, diff);
  161. if ( geometry::math::equals(diff, 0) )
  162. {
  163. if ( geometry::math::equals(t_min.first, 0) ) { t_min.first = -1; }
  164. if ( geometry::math::equals(t_max.first, 0) ) { t_max.first = 1; }
  165. if (math::sign(t_min.first) * math::sign(t_max.first) > 0)
  166. {
  167. return true;
  168. }
  169. }
  170. if ( t_min.first > diff || t_max.first < 0 )
  171. {
  172. return true;
  173. }
  174. t_min.second = t_max.second = diff;
  175. return disjoint_segment_box_impl
  176. <
  177. RelativeDistance, SegmentPoint, Box, 1, Dimension
  178. >::apply(p0, p1, box, t_min, t_max);
  179. }
  180. };
  181. template
  182. <
  183. typename RelativeDistance,
  184. typename SegmentPoint,
  185. typename Box,
  186. std::size_t Dimension
  187. >
  188. struct disjoint_segment_box_impl
  189. <
  190. RelativeDistance, SegmentPoint, Box, Dimension, Dimension
  191. >
  192. {
  193. template <typename RelativeDistancePair>
  194. static inline bool apply(SegmentPoint const&, SegmentPoint const&,
  195. Box const&,
  196. RelativeDistancePair&, RelativeDistancePair&)
  197. {
  198. return false;
  199. }
  200. };
  201. } // namespace detail
  202. // NOTE: This may be temporary place for this or corresponding strategy
  203. // It seems to be more appropriate to implement the opposite of it
  204. // e.g. intersection::segment_box because in disjoint() algorithm
  205. // other strategies that are used are intersection and covered_by strategies.
  206. struct segment_box
  207. {
  208. typedef covered_by::cartesian_point_box disjoint_point_box_strategy_type;
  209. static inline disjoint_point_box_strategy_type get_disjoint_point_box_strategy()
  210. {
  211. return disjoint_point_box_strategy_type();
  212. }
  213. template <typename Segment, typename Box>
  214. static inline bool apply(Segment const& segment, Box const& box)
  215. {
  216. assert_dimension_equal<Segment, Box>();
  217. typedef typename util::calculation_type::geometric::binary
  218. <
  219. Segment, Box, void
  220. >::type relative_distance_type;
  221. typedef typename point_type<Segment>::type segment_point_type;
  222. segment_point_type p0, p1;
  223. geometry::detail::assign_point_from_index<0>(segment, p0);
  224. geometry::detail::assign_point_from_index<1>(segment, p1);
  225. return detail::disjoint_segment_box_impl
  226. <
  227. relative_distance_type, segment_point_type, Box,
  228. 0, dimension<Box>::value
  229. >::apply(p0, p1, box);
  230. }
  231. };
  232. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  233. namespace services
  234. {
  235. template <typename Linear, typename Box, typename LinearTag>
  236. struct default_strategy<Linear, Box, LinearTag, box_tag, 1, 2, cartesian_tag, cartesian_tag>
  237. {
  238. typedef disjoint::segment_box type;
  239. };
  240. template <typename Box, typename Linear, typename LinearTag>
  241. struct default_strategy<Box, Linear, box_tag, LinearTag, 2, 1, cartesian_tag, cartesian_tag>
  242. {
  243. typedef disjoint::segment_box type;
  244. };
  245. } // namespace services
  246. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  247. }}}} // namespace boost::geometry::strategy::disjoint
  248. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISJOINT_SEGMENT_BOX_HPP