disjoint.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_HPP
  12. // Note: contrary to most files, the geometry::detail::disjoint namespace
  13. // is partly implemented in a separate file, to avoid circular references
  14. // disjoint -> get_turns -> disjoint
  15. #include <cstddef>
  16. #include <boost/range.hpp>
  17. #include <boost/geometry/core/access.hpp>
  18. #include <boost/geometry/core/coordinate_dimension.hpp>
  19. #include <boost/geometry/core/reverse_dispatch.hpp>
  20. #include <boost/geometry/algorithms/covered_by.hpp>
  21. #include <boost/geometry/util/math.hpp>
  22. namespace boost { namespace geometry
  23. {
  24. #ifndef DOXYGEN_NO_DETAIL
  25. namespace detail { namespace disjoint
  26. {
  27. struct disjoint_interrupt_policy
  28. {
  29. static bool const enabled = true;
  30. bool has_intersections;
  31. inline disjoint_interrupt_policy()
  32. : has_intersections(false)
  33. {}
  34. template <typename Range>
  35. inline bool apply(Range const& range)
  36. {
  37. // If there is any IP in the range, it is NOT disjoint
  38. if (boost::size(range) > 0)
  39. {
  40. has_intersections = true;
  41. return true;
  42. }
  43. return false;
  44. }
  45. };
  46. template
  47. <
  48. typename Point1, typename Point2,
  49. std::size_t Dimension, std::size_t DimensionCount
  50. >
  51. struct point_point
  52. {
  53. static inline bool apply(Point1 const& p1, Point2 const& p2)
  54. {
  55. if (! geometry::math::equals(get<Dimension>(p1), get<Dimension>(p2)))
  56. {
  57. return true;
  58. }
  59. return point_point
  60. <
  61. Point1, Point2,
  62. Dimension + 1, DimensionCount
  63. >::apply(p1, p2);
  64. }
  65. };
  66. template <typename Point1, typename Point2, std::size_t DimensionCount>
  67. struct point_point<Point1, Point2, DimensionCount, DimensionCount>
  68. {
  69. static inline bool apply(Point1 const& , Point2 const& )
  70. {
  71. return false;
  72. }
  73. };
  74. template
  75. <
  76. typename Point, typename Box,
  77. std::size_t Dimension, std::size_t DimensionCount
  78. >
  79. struct point_box
  80. {
  81. static inline bool apply(Point const& point, Box const& box)
  82. {
  83. if (get<Dimension>(point) < get<min_corner, Dimension>(box)
  84. || get<Dimension>(point) > get<max_corner, Dimension>(box))
  85. {
  86. return true;
  87. }
  88. return point_box
  89. <
  90. Point, Box,
  91. Dimension + 1, DimensionCount
  92. >::apply(point, box);
  93. }
  94. };
  95. template <typename Point, typename Box, std::size_t DimensionCount>
  96. struct point_box<Point, Box, DimensionCount, DimensionCount>
  97. {
  98. static inline bool apply(Point const& , Box const& )
  99. {
  100. return false;
  101. }
  102. };
  103. template
  104. <
  105. typename Box1, typename Box2,
  106. std::size_t Dimension, std::size_t DimensionCount
  107. >
  108. struct box_box
  109. {
  110. static inline bool apply(Box1 const& box1, Box2 const& box2)
  111. {
  112. if (get<max_corner, Dimension>(box1) < get<min_corner, Dimension>(box2))
  113. {
  114. return true;
  115. }
  116. if (get<min_corner, Dimension>(box1) > get<max_corner, Dimension>(box2))
  117. {
  118. return true;
  119. }
  120. return box_box
  121. <
  122. Box1, Box2,
  123. Dimension + 1, DimensionCount
  124. >::apply(box1, box2);
  125. }
  126. };
  127. template <typename Box1, typename Box2, std::size_t DimensionCount>
  128. struct box_box<Box1, Box2, DimensionCount, DimensionCount>
  129. {
  130. static inline bool apply(Box1 const& , Box2 const& )
  131. {
  132. return false;
  133. }
  134. };
  135. // Segment - Box intersection
  136. // Based on Ray-AABB intersection
  137. // http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter3.htm
  138. // TODO - later maybe move to strategy::intersects and add a policy to conditionally extract intersection points
  139. template <typename Point, typename Box, size_t I>
  140. struct segment_box_intersection_dim
  141. {
  142. //BOOST_STATIC_ASSERT(I < dimension<Box>::value);
  143. //BOOST_STATIC_ASSERT(I < dimension<Point>::value);
  144. //BOOST_STATIC_ASSERT(dimension<Point>::value == dimension<Box>::value);
  145. typedef typename coordinate_type<Point>::type point_coordinate;
  146. template <typename RelativeDistance> static inline
  147. bool apply(Point const& p0, Point const& p1, Box const& b, RelativeDistance & t_near, RelativeDistance & t_far)
  148. {
  149. //// WARNING! - RelativeDistance must be IEEE float for this to work (division by 0)
  150. //BOOST_STATIC_ASSERT(boost::is_float<RelativeDistance>::value);
  151. //// Ray origin is in segment point 0
  152. //RelativeDistance ray_d = geometry::get<I>(p1) - geometry::get<I>(p0);
  153. //RelativeDistance tn = ( geometry::get<min_corner, I>(b) - geometry::get<I>(p0) ) / ray_d;
  154. //RelativeDistance tf = ( geometry::get<max_corner, I>(b) - geometry::get<I>(p0) ) / ray_d;
  155. // TODO - should we support also unsigned integers?
  156. BOOST_STATIC_ASSERT(!boost::is_unsigned<point_coordinate>::value);
  157. point_coordinate ray_d = geometry::get<I>(p1) - geometry::get<I>(p0);
  158. RelativeDistance tn, tf;
  159. if ( is_zero(ray_d) )
  160. {
  161. tn = dist_div_by_zero<RelativeDistance>(geometry::get<min_corner, I>(b) - geometry::get<I>(p0));
  162. tf = dist_div_by_zero<RelativeDistance>(geometry::get<max_corner, I>(b) - geometry::get<I>(p0));
  163. }
  164. else
  165. {
  166. tn = static_cast<RelativeDistance>(geometry::get<min_corner, I>(b) - geometry::get<I>(p0)) / ray_d;
  167. tf = static_cast<RelativeDistance>(geometry::get<max_corner, I>(b) - geometry::get<I>(p0)) / ray_d;
  168. }
  169. if ( tf < tn )
  170. ::std::swap(tn, tf);
  171. if ( t_near < tn )
  172. t_near = tn;
  173. if ( tf < t_far )
  174. t_far = tf;
  175. return 0 <= t_far && t_near <= t_far && t_near <= 1;
  176. }
  177. template <typename R, typename T> static inline
  178. R dist_div_by_zero(T const& val)
  179. {
  180. if ( is_zero(val) )
  181. return 0;
  182. else if ( val < 0 )
  183. return -(::std::numeric_limits<R>::max)();
  184. else
  185. return (::std::numeric_limits<R>::max)();
  186. }
  187. template <typename T> static inline
  188. bool is_zero(T const& val)
  189. {
  190. // ray_d == 0 is here because eps of rational<int> is 0 which isn't < than 0
  191. return val == 0 || math::abs(val) < ::std::numeric_limits<T>::epsilon();
  192. }
  193. };
  194. template <typename Point, typename Box, size_t CurrentDimension>
  195. struct segment_box_intersection_impl
  196. {
  197. BOOST_STATIC_ASSERT(0 < CurrentDimension);
  198. typedef segment_box_intersection_dim<Point, Box, CurrentDimension - 1> for_dim;
  199. template <typename RelativeDistance>
  200. static inline bool apply(Point const& p0, Point const& p1, Box const& b,
  201. RelativeDistance & t_near, RelativeDistance & t_far)
  202. {
  203. return segment_box_intersection_impl<Point, Box, CurrentDimension - 1>::apply(p0, p1, b, t_near, t_far)
  204. && for_dim::apply(p0, p1, b, t_near, t_far);
  205. }
  206. };
  207. template <typename Point, typename Box>
  208. struct segment_box_intersection_impl<Point, Box, 1>
  209. {
  210. typedef segment_box_intersection_dim<Point, Box, 0> for_dim;
  211. template <typename RelativeDistance>
  212. static inline bool apply(Point const& p0, Point const& p1, Box const& b,
  213. RelativeDistance & t_near, RelativeDistance & t_far)
  214. {
  215. return for_dim::apply(p0, p1, b, t_near, t_far);
  216. }
  217. };
  218. template <typename Point, typename Box>
  219. struct segment_box_intersection
  220. {
  221. typedef segment_box_intersection_impl<Point, Box, dimension<Box>::value> impl;
  222. static inline bool apply(Point const& p0, Point const& p1, Box const& b)
  223. {
  224. typedef
  225. typename geometry::promote_floating_point<
  226. typename geometry::select_most_precise<
  227. typename coordinate_type<Point>::type,
  228. typename coordinate_type<Box>::type
  229. >::type
  230. >::type relative_distance_type;
  231. relative_distance_type t_near = -(::std::numeric_limits<relative_distance_type>::max)();
  232. relative_distance_type t_far = (::std::numeric_limits<relative_distance_type>::max)();
  233. // relative_distance = 0 < t_near ? t_near : 0;
  234. return impl::apply(p0, p1, b, t_near, t_far);
  235. }
  236. };
  237. template
  238. <
  239. typename Geometry1, typename Geometry2
  240. >
  241. struct reverse_covered_by
  242. {
  243. static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2)
  244. {
  245. return ! geometry::covered_by(geometry1, geometry2);
  246. }
  247. };
  248. /*!
  249. \brief Internal utility function to detect of boxes are disjoint
  250. \note Is used from other algorithms, declared separately
  251. to avoid circular references
  252. */
  253. template <typename Box1, typename Box2>
  254. inline bool disjoint_box_box(Box1 const& box1, Box2 const& box2)
  255. {
  256. return box_box
  257. <
  258. Box1, Box2,
  259. 0, dimension<Box1>::type::value
  260. >::apply(box1, box2);
  261. }
  262. /*!
  263. \brief Internal utility function to detect of points are disjoint
  264. \note To avoid circular references
  265. */
  266. template <typename Point1, typename Point2>
  267. inline bool disjoint_point_point(Point1 const& point1, Point2 const& point2)
  268. {
  269. return point_point
  270. <
  271. Point1, Point2,
  272. 0, dimension<Point1>::type::value
  273. >::apply(point1, point2);
  274. }
  275. }} // namespace detail::disjoint
  276. #endif // DOXYGEN_NO_DETAIL
  277. #ifndef DOXYGEN_NO_DETAIL
  278. namespace detail { namespace equals
  279. {
  280. /*!
  281. \brief Internal utility function to detect of points are disjoint
  282. \note To avoid circular references
  283. */
  284. template <typename Point1, typename Point2>
  285. inline bool equals_point_point(Point1 const& point1, Point2 const& point2)
  286. {
  287. return ! detail::disjoint::disjoint_point_point(point1, point2);
  288. }
  289. }} // namespace detail::equals
  290. #endif // DOXYGEN_NO_DETAIL
  291. }} // namespace boost::geometry
  292. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_HPP