direction.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 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_GEOMETRY_POLICIES_RELATE_DIRECTION_HPP
  7. #define BOOST_GEOMETRY_GEOMETRY_POLICIES_RELATE_DIRECTION_HPP
  8. #include <cstddef>
  9. #include <string>
  10. #include <boost/concept_check.hpp>
  11. #include <boost/geometry/arithmetic/determinant.hpp>
  12. #include <boost/geometry/strategies/side_info.hpp>
  13. #include <boost/geometry/util/math.hpp>
  14. #include <boost/geometry/util/select_calculation_type.hpp>
  15. #include <boost/geometry/util/select_most_precise.hpp>
  16. namespace boost { namespace geometry
  17. {
  18. namespace policies { namespace relate
  19. {
  20. struct direction_type
  21. {
  22. // NOTE: "char" will be replaced by enum in future version
  23. inline direction_type(side_info const& s, char h,
  24. int ha, int hb,
  25. int da = 0, int db = 0,
  26. bool op = false)
  27. : how(h)
  28. , opposite(op)
  29. , how_a(ha)
  30. , how_b(hb)
  31. , dir_a(da)
  32. , dir_b(db)
  33. , sides(s)
  34. {
  35. arrival[0] = ha;
  36. arrival[1] = hb;
  37. }
  38. inline direction_type(char h, bool op, int ha = 0, int hb = 0)
  39. : how(h)
  40. , opposite(op)
  41. , how_a(ha)
  42. , how_b(hb)
  43. , dir_a(0)
  44. , dir_b(0)
  45. {
  46. arrival[0] = ha;
  47. arrival[1] = hb;
  48. }
  49. // TODO: replace this
  50. // NOTE: "char" will be replaced by enum in future version
  51. // "How" is the intersection formed?
  52. char how;
  53. // Is it opposite (for collinear/equal cases)
  54. bool opposite;
  55. // Information on how A arrives at intersection, how B arrives at intersection
  56. // 1: arrives at intersection
  57. // -1: starts from intersection
  58. int how_a;
  59. int how_b;
  60. // Direction: how is A positioned from B
  61. // 1: points left, seen from IP
  62. // -1: points right, seen from IP
  63. // In case of intersection: B's TO direction
  64. // In case that B's TO direction is at A: B's from direction
  65. // In collinear cases: it is 0
  66. int dir_a; // Direction of A-s TO from IP
  67. int dir_b; // Direction of B-s TO from IP
  68. // New information
  69. side_info sides;
  70. // THIS IS EQUAL TO arrival_a, arrival_b - they probably can go now we have robust fractions
  71. int arrival[2]; // 1=arrival, -1=departure, 0=neutral; == how_a//how_b
  72. // About arrival[0] (== arrival of a2 w.r.t. b) for COLLINEAR cases
  73. // Arrival 1: a1--------->a2 (a arrives within b)
  74. // b1----->b2
  75. // Arrival 1: (a in b)
  76. //
  77. // Arrival -1: a1--------->a2 (a does not arrive within b)
  78. // b1----->b2
  79. // Arrival -1: (b in a) a_1-------------a_2
  80. // b_1---b_2
  81. // Arrival 0: a1------->a2 (a arrives at TO-border of b)
  82. // b1--->b2
  83. };
  84. struct segments_direction
  85. {
  86. typedef direction_type return_type;
  87. template
  88. <
  89. typename Segment1,
  90. typename Segment2,
  91. typename SegmentIntersectionInfo
  92. >
  93. static inline return_type segments_crosses(side_info const& sides,
  94. SegmentIntersectionInfo const& ,
  95. Segment1 const& , Segment2 const& )
  96. {
  97. bool const ra0 = sides.get<0,0>() == 0;
  98. bool const ra1 = sides.get<0,1>() == 0;
  99. bool const rb0 = sides.get<1,0>() == 0;
  100. bool const rb1 = sides.get<1,1>() == 0;
  101. return
  102. // opposite and same starting point (FROM)
  103. ra0 && rb0 ? calculate_side<1>(sides, 'f', -1, -1)
  104. // opposite and point to each other (TO)
  105. : ra1 && rb1 ? calculate_side<0>(sides, 't', 1, 1)
  106. // not opposite, forming an angle, first a then b,
  107. // directed either both left, or both right
  108. // Check side of B2 from A. This is not calculated before
  109. : ra1 && rb0 ? angle<1>(sides, 'a', 1, -1)
  110. // not opposite, forming a angle, first b then a,
  111. // directed either both left, or both right
  112. : ra0 && rb1 ? angle<0>(sides, 'a', -1, 1)
  113. // b starts from interior of a
  114. : rb0 ? starts_from_middle(sides, 'B', 0, -1)
  115. // a starts from interior of b (#39)
  116. : ra0 ? starts_from_middle(sides, 'A', -1, 0)
  117. // b ends at interior of a, calculate direction of A from IP
  118. : rb1 ? b_ends_at_middle(sides)
  119. // a ends at interior of b
  120. : ra1 ? a_ends_at_middle(sides)
  121. // normal intersection
  122. : calculate_side<1>(sides, 'i', -1, -1)
  123. ;
  124. }
  125. template <typename Ratio>
  126. static inline int arrival_value(Ratio const& r_from, Ratio const& r_to)
  127. {
  128. // a1--------->a2
  129. // b1----->b2
  130. // a departs: -1
  131. // a1--------->a2
  132. // b1----->b2
  133. // a arrives: 1
  134. // a1--------->a2
  135. // b1----->b2
  136. // both arrive there -> r-to = 1/1, or 0/1 (on_segment)
  137. // First check the TO (for arrival), then FROM (for departure)
  138. return r_to.in_segment() ? 1
  139. : r_to.on_segment() ? 0
  140. : r_from.on_segment() ? -1
  141. : -1
  142. ;
  143. }
  144. template <typename Ratio>
  145. static inline void analyze(Ratio const& r,
  146. int& in_segment_count,
  147. int& on_end_count,
  148. int& outside_segment_count)
  149. {
  150. if (r.on_end())
  151. {
  152. on_end_count++;
  153. }
  154. else if (r.in_segment())
  155. {
  156. in_segment_count++;
  157. }
  158. else
  159. {
  160. outside_segment_count++;
  161. }
  162. }
  163. static inline int arrival_from_position_value(int /*v_from*/, int v_to)
  164. {
  165. return v_to == 2 ? 1
  166. : v_to == 1 || v_to == 3 ? 0
  167. //: v_from >= 1 && v_from <= 3 ? -1
  168. : -1;
  169. // NOTE: this should be an equivalent of the above for the other order
  170. /* (v_from < 3 && v_to > 3) || (v_from > 3 && v_to < 3) ? 1
  171. : v_from == 3 || v_to == 3 ? 0
  172. : -1;*/
  173. }
  174. static inline void analyse_position_value(int pos_val,
  175. int & in_segment_count,
  176. int & on_end_count,
  177. int & outside_segment_count)
  178. {
  179. if ( pos_val == 1 || pos_val == 3 )
  180. {
  181. on_end_count++;
  182. }
  183. else if ( pos_val == 2 )
  184. {
  185. in_segment_count++;
  186. }
  187. else
  188. {
  189. outside_segment_count++;
  190. }
  191. }
  192. template <typename Segment1, typename Segment2, typename Ratio>
  193. static inline return_type segments_collinear(
  194. Segment1 const& , Segment2 const& , bool opposite,
  195. int a1_wrt_b, int a2_wrt_b, int b1_wrt_a, int b2_wrt_a,
  196. Ratio const& /*ra_from_wrt_b*/, Ratio const& /*ra_to_wrt_b*/,
  197. Ratio const& /*rb_from_wrt_a*/, Ratio const& /*rb_to_wrt_a*/)
  198. {
  199. return_type r('c', opposite);
  200. // IMPORTANT: the order of conditions is different as in intersection_points.hpp
  201. // We assign A in 0 and B in 1
  202. r.arrival[0] = arrival_from_position_value(a1_wrt_b, a2_wrt_b);
  203. r.arrival[1] = arrival_from_position_value(b1_wrt_a, b2_wrt_a);
  204. // Analyse them
  205. int a_in_segment_count = 0;
  206. int a_on_end_count = 0;
  207. int a_outside_segment_count = 0;
  208. int b_in_segment_count = 0;
  209. int b_on_end_count = 0;
  210. int b_outside_segment_count = 0;
  211. analyse_position_value(a1_wrt_b,
  212. a_in_segment_count, a_on_end_count, a_outside_segment_count);
  213. analyse_position_value(a2_wrt_b,
  214. a_in_segment_count, a_on_end_count, a_outside_segment_count);
  215. analyse_position_value(b1_wrt_a,
  216. b_in_segment_count, b_on_end_count, b_outside_segment_count);
  217. analyse_position_value(b2_wrt_a,
  218. b_in_segment_count, b_on_end_count, b_outside_segment_count);
  219. if (a_on_end_count == 1
  220. && b_on_end_count == 1
  221. && a_outside_segment_count == 1
  222. && b_outside_segment_count == 1)
  223. {
  224. // This is a collinear touch
  225. // --------> A (or B)
  226. // <---------- B (or A)
  227. // We adapt the "how"
  228. // TODO: how was to be refactored anyway,
  229. if (! opposite)
  230. {
  231. r.how = 'a';
  232. }
  233. else
  234. {
  235. r.how = r.arrival[0] == 0 ? 't' : 'f';
  236. }
  237. }
  238. else if (a_on_end_count == 2
  239. && b_on_end_count == 2)
  240. {
  241. r.how = 'e';
  242. }
  243. return r;
  244. }
  245. template <typename Segment>
  246. static inline return_type degenerate(Segment const& , bool)
  247. {
  248. return return_type('0', false);
  249. }
  250. template <typename Segment, typename Ratio>
  251. static inline return_type one_degenerate(Segment const& ,
  252. Ratio const& ,
  253. bool)
  254. {
  255. // To be decided
  256. return return_type('0', false);
  257. }
  258. static inline return_type disjoint()
  259. {
  260. return return_type('d', false);
  261. }
  262. static inline return_type error(std::string const&)
  263. {
  264. // Return "E" to denote error
  265. // This will throw an error in get_turn_info
  266. // TODO: change to enum or similar
  267. return return_type('E', false);
  268. }
  269. private :
  270. template <std::size_t I>
  271. static inline return_type calculate_side(side_info const& sides,
  272. char how, int how_a, int how_b)
  273. {
  274. int const dir = sides.get<1, I>() == 1 ? 1 : -1;
  275. return return_type(sides, how, how_a, how_b, -dir, dir);
  276. }
  277. template <std::size_t I>
  278. static inline return_type angle(side_info const& sides,
  279. char how, int how_a, int how_b)
  280. {
  281. int const dir = sides.get<1, I>() == 1 ? 1 : -1;
  282. return return_type(sides, how, how_a, how_b, dir, dir);
  283. }
  284. static inline return_type starts_from_middle(side_info const& sides,
  285. char which,
  286. int how_a, int how_b)
  287. {
  288. // Calculate ARROW of b segment w.r.t. s1
  289. int dir = sides.get<1, 1>() == 1 ? 1 : -1;
  290. // From other perspective, then reverse
  291. bool const is_a = which == 'A';
  292. if (is_a)
  293. {
  294. dir = -dir;
  295. }
  296. return return_type(sides, 's',
  297. how_a,
  298. how_b,
  299. is_a ? dir : -dir,
  300. ! is_a ? dir : -dir);
  301. }
  302. // To be harmonized
  303. static inline return_type a_ends_at_middle(side_info const& sides)
  304. {
  305. // Ending at the middle, one ARRIVES, the other one is NEUTRAL
  306. // (because it both "arrives" and "departs" there)
  307. int const dir = sides.get<1, 1>() == 1 ? 1 : -1;
  308. return return_type(sides, 'm', 1, 0, dir, dir);
  309. }
  310. static inline return_type b_ends_at_middle(side_info const& sides)
  311. {
  312. int const dir = sides.get<0, 1>() == 1 ? 1 : -1;
  313. return return_type(sides, 'm', 0, 1, dir, dir);
  314. }
  315. };
  316. }} // namespace policies::relate
  317. }} // namespace boost::geometry
  318. #endif // BOOST_GEOMETRY_GEOMETRY_POLICIES_RELATE_DIRECTION_HPP