turns.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2013, 2014, 2015, 2017.
  4. // Modifications copyright (c) 2013-2017 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  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_RELATE_TURNS_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_TURNS_HPP
  12. #include <boost/geometry/strategies/distance.hpp>
  13. #include <boost/geometry/algorithms/detail/overlay/do_reverse.hpp>
  14. #include <boost/geometry/algorithms/detail/overlay/get_turns.hpp>
  15. #include <boost/geometry/algorithms/detail/overlay/get_turn_info.hpp>
  16. #include <boost/geometry/policies/robustness/get_rescale_policy.hpp>
  17. #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
  18. #include <boost/type_traits/is_base_of.hpp>
  19. namespace boost { namespace geometry {
  20. #ifndef DOXYGEN_NO_DETAIL
  21. namespace detail { namespace relate { namespace turns {
  22. template <bool IncludeDegenerate = false>
  23. struct assign_policy
  24. : overlay::assign_null_policy
  25. {
  26. static bool const include_degenerate = IncludeDegenerate;
  27. };
  28. // GET_TURNS
  29. template
  30. <
  31. typename Geometry1,
  32. typename Geometry2,
  33. typename GetTurnPolicy = detail::get_turns::get_turn_info_type
  34. <
  35. Geometry1, Geometry2, assign_policy<>
  36. >,
  37. typename RobustPolicy = detail::no_rescale_policy
  38. >
  39. struct get_turns
  40. {
  41. typedef typename geometry::point_type<Geometry1>::type point1_type;
  42. typedef overlay::turn_info
  43. <
  44. point1_type,
  45. typename segment_ratio_type<point1_type, RobustPolicy>::type,
  46. typename detail::get_turns::turn_operation_type
  47. <
  48. Geometry1, Geometry2,
  49. typename segment_ratio_type
  50. <
  51. point1_type, RobustPolicy
  52. >::type
  53. >::type
  54. > turn_info;
  55. template <typename Turns>
  56. static inline void apply(Turns & turns,
  57. Geometry1 const& geometry1,
  58. Geometry2 const& geometry2)
  59. {
  60. detail::get_turns::no_interrupt_policy interrupt_policy;
  61. typename strategy::intersection::services::default_strategy
  62. <
  63. typename cs_tag<Geometry1>::type
  64. >::type intersection_strategy;
  65. apply(turns, geometry1, geometry2, interrupt_policy, intersection_strategy);
  66. }
  67. template <typename Turns, typename InterruptPolicy, typename IntersectionStrategy>
  68. static inline void apply(Turns & turns,
  69. Geometry1 const& geometry1,
  70. Geometry2 const& geometry2,
  71. InterruptPolicy & interrupt_policy,
  72. IntersectionStrategy const& intersection_strategy)
  73. {
  74. RobustPolicy robust_policy = geometry::get_rescale_policy
  75. <
  76. RobustPolicy
  77. >(geometry1, geometry2);
  78. apply(turns, geometry1, geometry2, interrupt_policy, intersection_strategy, robust_policy);
  79. }
  80. template <typename Turns, typename InterruptPolicy, typename IntersectionStrategy>
  81. static inline void apply(Turns & turns,
  82. Geometry1 const& geometry1,
  83. Geometry2 const& geometry2,
  84. InterruptPolicy & interrupt_policy,
  85. IntersectionStrategy const& intersection_strategy,
  86. RobustPolicy const& robust_policy)
  87. {
  88. static const bool reverse1 = detail::overlay::do_reverse
  89. <
  90. geometry::point_order<Geometry1>::value
  91. >::value;
  92. static const bool reverse2 = detail::overlay::do_reverse
  93. <
  94. geometry::point_order<Geometry2>::value
  95. >::value;
  96. dispatch::get_turns
  97. <
  98. typename geometry::tag<Geometry1>::type,
  99. typename geometry::tag<Geometry2>::type,
  100. Geometry1,
  101. Geometry2,
  102. reverse1,
  103. reverse2,
  104. GetTurnPolicy
  105. >::apply(0, geometry1, 1, geometry2,
  106. intersection_strategy, robust_policy,
  107. turns, interrupt_policy);
  108. }
  109. };
  110. // TURNS SORTING AND SEARCHING
  111. template <int N = 0, int U = 1, int I = 2, int B = 3, int C = 4, int O = 0>
  112. struct op_to_int
  113. {
  114. template <typename Operation>
  115. inline int operator()(Operation const& op) const
  116. {
  117. switch(op.operation)
  118. {
  119. case detail::overlay::operation_none : return N;
  120. case detail::overlay::operation_union : return U;
  121. case detail::overlay::operation_intersection : return I;
  122. case detail::overlay::operation_blocked : return B;
  123. case detail::overlay::operation_continue : return C;
  124. case detail::overlay::operation_opposite : return O;
  125. }
  126. return -1;
  127. }
  128. };
  129. template <std::size_t OpId, typename OpToInt>
  130. struct less_op_xxx_linear
  131. {
  132. template <typename Turn>
  133. inline bool operator()(Turn const& left, Turn const& right) const
  134. {
  135. static OpToInt op_to_int;
  136. return op_to_int(left.operations[OpId]) < op_to_int(right.operations[OpId]);
  137. }
  138. };
  139. template <std::size_t OpId>
  140. struct less_op_linear_linear
  141. : less_op_xxx_linear< OpId, op_to_int<0,2,3,1,4,0> >
  142. {};
  143. template <std::size_t OpId>
  144. struct less_op_linear_areal_single
  145. {
  146. template <typename Turn>
  147. inline bool operator()(Turn const& left, Turn const& right) const
  148. {
  149. static const std::size_t other_op_id = (OpId + 1) % 2;
  150. static turns::op_to_int<0,2,3,1,4,0> op_to_int_xuic;
  151. static turns::op_to_int<0,3,2,1,4,0> op_to_int_xiuc;
  152. segment_identifier const& left_other_seg_id = left.operations[other_op_id].seg_id;
  153. segment_identifier const& right_other_seg_id = right.operations[other_op_id].seg_id;
  154. typedef typename Turn::turn_operation_type operation_type;
  155. operation_type const& left_operation = left.operations[OpId];
  156. operation_type const& right_operation = right.operations[OpId];
  157. if ( left_other_seg_id.ring_index == right_other_seg_id.ring_index )
  158. {
  159. return op_to_int_xuic(left_operation)
  160. < op_to_int_xuic(right_operation);
  161. }
  162. else
  163. {
  164. return op_to_int_xiuc(left_operation)
  165. < op_to_int_xiuc(right_operation);
  166. }
  167. }
  168. };
  169. template <std::size_t OpId>
  170. struct less_op_areal_linear
  171. : less_op_xxx_linear< OpId, op_to_int<0,1,0,0,2,0> >
  172. {};
  173. template <std::size_t OpId>
  174. struct less_op_areal_areal
  175. {
  176. template <typename Turn>
  177. inline bool operator()(Turn const& left, Turn const& right) const
  178. {
  179. static const std::size_t other_op_id = (OpId + 1) % 2;
  180. static op_to_int<0, 1, 2, 3, 4, 0> op_to_int_uixc;
  181. static op_to_int<0, 2, 1, 3, 4, 0> op_to_int_iuxc;
  182. segment_identifier const& left_other_seg_id = left.operations[other_op_id].seg_id;
  183. segment_identifier const& right_other_seg_id = right.operations[other_op_id].seg_id;
  184. typedef typename Turn::turn_operation_type operation_type;
  185. operation_type const& left_operation = left.operations[OpId];
  186. operation_type const& right_operation = right.operations[OpId];
  187. if ( left_other_seg_id.multi_index == right_other_seg_id.multi_index )
  188. {
  189. if ( left_other_seg_id.ring_index == right_other_seg_id.ring_index )
  190. {
  191. return op_to_int_uixc(left_operation) < op_to_int_uixc(right_operation);
  192. }
  193. else
  194. {
  195. if ( left_other_seg_id.ring_index == -1 )
  196. {
  197. if ( left_operation.operation == overlay::operation_union )
  198. return false;
  199. else if ( left_operation.operation == overlay::operation_intersection )
  200. return true;
  201. }
  202. else if ( right_other_seg_id.ring_index == -1 )
  203. {
  204. if ( right_operation.operation == overlay::operation_union )
  205. return true;
  206. else if ( right_operation.operation == overlay::operation_intersection )
  207. return false;
  208. }
  209. return op_to_int_iuxc(left_operation) < op_to_int_iuxc(right_operation);
  210. }
  211. }
  212. else
  213. {
  214. return op_to_int_uixc(left_operation) < op_to_int_uixc(right_operation);
  215. }
  216. }
  217. };
  218. template <std::size_t OpId>
  219. struct less_other_multi_index
  220. {
  221. static const std::size_t other_op_id = (OpId + 1) % 2;
  222. template <typename Turn>
  223. inline bool operator()(Turn const& left, Turn const& right) const
  224. {
  225. return left.operations[other_op_id].seg_id.multi_index
  226. < right.operations[other_op_id].seg_id.multi_index;
  227. }
  228. };
  229. // sort turns by G1 - source_index == 0 by:
  230. // seg_id -> distance -> operation
  231. template <std::size_t OpId = 0,
  232. typename LessOp = less_op_xxx_linear< OpId, op_to_int<> > >
  233. struct less
  234. {
  235. BOOST_STATIC_ASSERT(OpId < 2);
  236. template <typename Turn>
  237. static inline bool use_fraction(Turn const& left, Turn const& right)
  238. {
  239. static LessOp less_op;
  240. return
  241. geometry::math::equals(left.operations[OpId].fraction,
  242. right.operations[OpId].fraction)
  243. ?
  244. less_op(left, right)
  245. :
  246. (left.operations[OpId].fraction < right.operations[OpId].fraction)
  247. ;
  248. }
  249. template <typename Turn>
  250. inline bool operator()(Turn const& left, Turn const& right) const
  251. {
  252. segment_identifier const& sl = left.operations[OpId].seg_id;
  253. segment_identifier const& sr = right.operations[OpId].seg_id;
  254. return sl < sr || ( sl == sr && use_fraction(left, right) );
  255. }
  256. };
  257. }}} // namespace detail::relate::turns
  258. #endif // DOXYGEN_NO_DETAIL
  259. }} // namespace boost::geometry
  260. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_TURNS_HPP