result.hpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2013-2018.
  5. // Modifications copyright (c) 2013-2018 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, 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_RESULT_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_RESULT_HPP
  12. #include <cstddef>
  13. #include <cstring>
  14. #include <boost/mpl/assert.hpp>
  15. #include <boost/mpl/at.hpp>
  16. #include <boost/mpl/begin.hpp>
  17. #include <boost/mpl/deref.hpp>
  18. #include <boost/mpl/end.hpp>
  19. #include <boost/mpl/is_sequence.hpp>
  20. #include <boost/mpl/next.hpp>
  21. #include <boost/mpl/size.hpp>
  22. #include <boost/static_assert.hpp>
  23. #include <boost/throw_exception.hpp>
  24. #include <boost/tuple/tuple.hpp>
  25. #include <boost/type_traits/integral_constant.hpp>
  26. #include <boost/geometry/core/assert.hpp>
  27. #include <boost/geometry/core/coordinate_dimension.hpp>
  28. #include <boost/geometry/core/exception.hpp>
  29. #include <boost/geometry/util/condition.hpp>
  30. namespace boost { namespace geometry {
  31. #ifndef DOXYGEN_NO_DETAIL
  32. namespace detail { namespace relate {
  33. enum field { interior = 0, boundary = 1, exterior = 2 };
  34. // TODO: IF THE RESULT IS UPDATED WITH THE MAX POSSIBLE VALUE FOR SOME PAIR OF GEOEMTRIES
  35. // THE VALUE ALREADY STORED MUSN'T BE CHECKED
  36. // update() calls chould be replaced with set() in those cases
  37. // but for safety reasons (STATIC_ASSERT) we should check if parameter D is valid and set() doesn't do that
  38. // so some additional function could be added, e.g. set_dim()
  39. // --------------- MATRIX ----------------
  40. // matrix
  41. template <std::size_t Height, std::size_t Width = Height>
  42. class matrix
  43. {
  44. public:
  45. typedef char value_type;
  46. typedef std::size_t size_type;
  47. typedef const char * const_iterator;
  48. typedef const_iterator iterator;
  49. static const std::size_t static_width = Width;
  50. static const std::size_t static_height = Height;
  51. static const std::size_t static_size = Width * Height;
  52. inline matrix()
  53. {
  54. ::memset(m_array, 'F', static_size);
  55. }
  56. template <field F1, field F2>
  57. inline char get() const
  58. {
  59. BOOST_STATIC_ASSERT(F1 < Height && F2 < Width);
  60. static const std::size_t index = F1 * Width + F2;
  61. BOOST_STATIC_ASSERT(index < static_size);
  62. return m_array[index];
  63. }
  64. template <field F1, field F2, char V>
  65. inline void set()
  66. {
  67. BOOST_STATIC_ASSERT(F1 < Height && F2 < Width);
  68. static const std::size_t index = F1 * Width + F2;
  69. BOOST_STATIC_ASSERT(index < static_size);
  70. m_array[index] = V;
  71. }
  72. inline char operator[](std::size_t index) const
  73. {
  74. BOOST_GEOMETRY_ASSERT(index < static_size);
  75. return m_array[index];
  76. }
  77. inline const_iterator begin() const
  78. {
  79. return m_array;
  80. }
  81. inline const_iterator end() const
  82. {
  83. return m_array + static_size;
  84. }
  85. inline static std::size_t size()
  86. {
  87. return static_size;
  88. }
  89. inline const char * data() const
  90. {
  91. return m_array;
  92. }
  93. inline std::string str() const
  94. {
  95. return std::string(m_array, static_size);
  96. }
  97. private:
  98. char m_array[static_size];
  99. };
  100. // matrix_handler
  101. template <typename Matrix>
  102. class matrix_handler
  103. {
  104. public:
  105. typedef Matrix result_type;
  106. static const bool interrupt = false;
  107. matrix_handler()
  108. {}
  109. result_type const& result() const
  110. {
  111. return m_matrix;
  112. }
  113. result_type const& matrix() const
  114. {
  115. return m_matrix;
  116. }
  117. result_type & matrix()
  118. {
  119. return m_matrix;
  120. }
  121. template <field F1, field F2, char D>
  122. inline bool may_update() const
  123. {
  124. BOOST_STATIC_ASSERT('0' <= D && D <= '9');
  125. char const c = m_matrix.template get<F1, F2>();
  126. return D > c || c > '9';
  127. }
  128. template <field F1, field F2, char V>
  129. inline void set()
  130. {
  131. static const bool in_bounds = F1 < Matrix::static_height
  132. && F2 < Matrix::static_width;
  133. typedef boost::integral_constant<bool, in_bounds> in_bounds_t;
  134. set_dispatch<F1, F2, V>(in_bounds_t());
  135. }
  136. template <field F1, field F2, char D>
  137. inline void update()
  138. {
  139. static const bool in_bounds = F1 < Matrix::static_height
  140. && F2 < Matrix::static_width;
  141. typedef boost::integral_constant<bool, in_bounds> in_bounds_t;
  142. update_dispatch<F1, F2, D>(in_bounds_t());
  143. }
  144. private:
  145. template <field F1, field F2, char V>
  146. inline void set_dispatch(integral_constant<bool, true>)
  147. {
  148. static const std::size_t index = F1 * Matrix::static_width + F2;
  149. BOOST_STATIC_ASSERT(index < Matrix::static_size);
  150. BOOST_STATIC_ASSERT(('0' <= V && V <= '9') || V == 'T' || V == 'F');
  151. m_matrix.template set<F1, F2, V>();
  152. }
  153. template <field F1, field F2, char V>
  154. inline void set_dispatch(integral_constant<bool, false>)
  155. {}
  156. template <field F1, field F2, char D>
  157. inline void update_dispatch(integral_constant<bool, true>)
  158. {
  159. static const std::size_t index = F1 * Matrix::static_width + F2;
  160. BOOST_STATIC_ASSERT(index < Matrix::static_size);
  161. BOOST_STATIC_ASSERT('0' <= D && D <= '9');
  162. char const c = m_matrix.template get<F1, F2>();
  163. if ( D > c || c > '9')
  164. m_matrix.template set<F1, F2, D>();
  165. }
  166. template <field F1, field F2, char D>
  167. inline void update_dispatch(integral_constant<bool, false>)
  168. {}
  169. Matrix m_matrix;
  170. };
  171. // --------------- RUN-TIME MASK ----------------
  172. // run-time mask
  173. template <std::size_t Height, std::size_t Width = Height>
  174. class mask
  175. {
  176. public:
  177. static const std::size_t static_width = Width;
  178. static const std::size_t static_height = Height;
  179. static const std::size_t static_size = Width * Height;
  180. inline mask(const char * s)
  181. {
  182. char * it = m_array;
  183. char * const last = m_array + static_size;
  184. for ( ; it != last && *s != '\0' ; ++it, ++s )
  185. {
  186. char c = *s;
  187. check_char(c);
  188. *it = c;
  189. }
  190. if ( it != last )
  191. {
  192. ::memset(it, '*', last - it);
  193. }
  194. }
  195. inline mask(const char * s, std::size_t count)
  196. {
  197. if ( count > static_size )
  198. {
  199. count = static_size;
  200. }
  201. if ( count > 0 )
  202. {
  203. std::for_each(s, s + count, check_char);
  204. ::memcpy(m_array, s, count);
  205. }
  206. if ( count < static_size )
  207. {
  208. ::memset(m_array + count, '*', static_size - count);
  209. }
  210. }
  211. template <field F1, field F2>
  212. inline char get() const
  213. {
  214. BOOST_STATIC_ASSERT(F1 < Height && F2 < Width);
  215. static const std::size_t index = F1 * Width + F2;
  216. BOOST_STATIC_ASSERT(index < static_size);
  217. return m_array[index];
  218. }
  219. private:
  220. static inline void check_char(char c)
  221. {
  222. bool const is_valid = c == '*' || c == 'T' || c == 'F'
  223. || ( c >= '0' && c <= '9' );
  224. if ( !is_valid )
  225. {
  226. BOOST_THROW_EXCEPTION(geometry::invalid_input_exception());
  227. }
  228. }
  229. char m_array[static_size];
  230. };
  231. // interrupt()
  232. template <typename Mask, bool InterruptEnabled>
  233. struct interrupt_dispatch
  234. {
  235. template <field F1, field F2, char V>
  236. static inline bool apply(Mask const&)
  237. {
  238. return false;
  239. }
  240. };
  241. template <typename Mask>
  242. struct interrupt_dispatch<Mask, true>
  243. {
  244. template <field F1, field F2, char V>
  245. static inline bool apply(Mask const& mask)
  246. {
  247. char m = mask.template get<F1, F2>();
  248. return check_element<V>(m);
  249. }
  250. template <char V>
  251. static inline bool check_element(char m)
  252. {
  253. if ( BOOST_GEOMETRY_CONDITION(V >= '0' && V <= '9') )
  254. {
  255. return m == 'F' || ( m < V && m >= '0' && m <= '9' );
  256. }
  257. else if ( BOOST_GEOMETRY_CONDITION(V == 'T') )
  258. {
  259. return m == 'F';
  260. }
  261. return false;
  262. }
  263. };
  264. template <typename Masks, int I = 0, int N = boost::tuples::length<Masks>::value>
  265. struct interrupt_dispatch_tuple
  266. {
  267. template <field F1, field F2, char V>
  268. static inline bool apply(Masks const& masks)
  269. {
  270. typedef typename boost::tuples::element<I, Masks>::type mask_type;
  271. mask_type const& mask = boost::get<I>(masks);
  272. return interrupt_dispatch<mask_type, true>::template apply<F1, F2, V>(mask)
  273. && interrupt_dispatch_tuple<Masks, I+1>::template apply<F1, F2, V>(masks);
  274. }
  275. };
  276. template <typename Masks, int N>
  277. struct interrupt_dispatch_tuple<Masks, N, N>
  278. {
  279. template <field F1, field F2, char V>
  280. static inline bool apply(Masks const& )
  281. {
  282. return true;
  283. }
  284. };
  285. //template <typename T0, typename T1, typename T2, typename T3, typename T4,
  286. // typename T5, typename T6, typename T7, typename T8, typename T9>
  287. //struct interrupt_dispatch<boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>, true>
  288. //{
  289. // typedef boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> mask_type;
  290. // template <field F1, field F2, char V>
  291. // static inline bool apply(mask_type const& mask)
  292. // {
  293. // return interrupt_dispatch_tuple<mask_type>::template apply<F1, F2, V>(mask);
  294. // }
  295. //};
  296. template <typename Head, typename Tail>
  297. struct interrupt_dispatch<boost::tuples::cons<Head, Tail>, true>
  298. {
  299. typedef boost::tuples::cons<Head, Tail> mask_type;
  300. template <field F1, field F2, char V>
  301. static inline bool apply(mask_type const& mask)
  302. {
  303. return interrupt_dispatch_tuple<mask_type>::template apply<F1, F2, V>(mask);
  304. }
  305. };
  306. template <field F1, field F2, char V, bool InterruptEnabled, typename Mask>
  307. inline bool interrupt(Mask const& mask)
  308. {
  309. return interrupt_dispatch<Mask, InterruptEnabled>
  310. ::template apply<F1, F2, V>(mask);
  311. }
  312. // may_update()
  313. template <typename Mask>
  314. struct may_update_dispatch
  315. {
  316. template <field F1, field F2, char D, typename Matrix>
  317. static inline bool apply(Mask const& mask, Matrix const& matrix)
  318. {
  319. BOOST_STATIC_ASSERT('0' <= D && D <= '9');
  320. char const m = mask.template get<F1, F2>();
  321. if ( m == 'F' )
  322. {
  323. return true;
  324. }
  325. else if ( m == 'T' )
  326. {
  327. char const c = matrix.template get<F1, F2>();
  328. return c == 'F'; // if it's T or between 0 and 9, the result will be the same
  329. }
  330. else if ( m >= '0' && m <= '9' )
  331. {
  332. char const c = matrix.template get<F1, F2>();
  333. return D > c || c > '9';
  334. }
  335. return false;
  336. }
  337. };
  338. template <typename Masks, int I = 0, int N = boost::tuples::length<Masks>::value>
  339. struct may_update_dispatch_tuple
  340. {
  341. template <field F1, field F2, char D, typename Matrix>
  342. static inline bool apply(Masks const& masks, Matrix const& matrix)
  343. {
  344. typedef typename boost::tuples::element<I, Masks>::type mask_type;
  345. mask_type const& mask = boost::get<I>(masks);
  346. return may_update_dispatch<mask_type>::template apply<F1, F2, D>(mask, matrix)
  347. || may_update_dispatch_tuple<Masks, I+1>::template apply<F1, F2, D>(masks, matrix);
  348. }
  349. };
  350. template <typename Masks, int N>
  351. struct may_update_dispatch_tuple<Masks, N, N>
  352. {
  353. template <field F1, field F2, char D, typename Matrix>
  354. static inline bool apply(Masks const& , Matrix const& )
  355. {
  356. return false;
  357. }
  358. };
  359. //template <typename T0, typename T1, typename T2, typename T3, typename T4,
  360. // typename T5, typename T6, typename T7, typename T8, typename T9>
  361. //struct may_update_dispatch< boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> >
  362. //{
  363. // typedef boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> mask_type;
  364. // template <field F1, field F2, char D, typename Matrix>
  365. // static inline bool apply(mask_type const& mask, Matrix const& matrix)
  366. // {
  367. // return may_update_dispatch_tuple<mask_type>::template apply<F1, F2, D>(mask, matrix);
  368. // }
  369. //};
  370. template <typename Head, typename Tail>
  371. struct may_update_dispatch< boost::tuples::cons<Head, Tail> >
  372. {
  373. typedef boost::tuples::cons<Head, Tail> mask_type;
  374. template <field F1, field F2, char D, typename Matrix>
  375. static inline bool apply(mask_type const& mask, Matrix const& matrix)
  376. {
  377. return may_update_dispatch_tuple<mask_type>::template apply<F1, F2, D>(mask, matrix);
  378. }
  379. };
  380. template <field F1, field F2, char D, typename Mask, typename Matrix>
  381. inline bool may_update(Mask const& mask, Matrix const& matrix)
  382. {
  383. return may_update_dispatch<Mask>
  384. ::template apply<F1, F2, D>(mask, matrix);
  385. }
  386. // check_matrix()
  387. template <typename Mask>
  388. struct check_dispatch
  389. {
  390. template <typename Matrix>
  391. static inline bool apply(Mask const& mask, Matrix const& matrix)
  392. {
  393. return per_one<interior, interior>(mask, matrix)
  394. && per_one<interior, boundary>(mask, matrix)
  395. && per_one<interior, exterior>(mask, matrix)
  396. && per_one<boundary, interior>(mask, matrix)
  397. && per_one<boundary, boundary>(mask, matrix)
  398. && per_one<boundary, exterior>(mask, matrix)
  399. && per_one<exterior, interior>(mask, matrix)
  400. && per_one<exterior, boundary>(mask, matrix)
  401. && per_one<exterior, exterior>(mask, matrix);
  402. }
  403. template <field F1, field F2, typename Matrix>
  404. static inline bool per_one(Mask const& mask, Matrix const& matrix)
  405. {
  406. const char mask_el = mask.template get<F1, F2>();
  407. const char el = matrix.template get<F1, F2>();
  408. if ( mask_el == 'F' )
  409. {
  410. return el == 'F';
  411. }
  412. else if ( mask_el == 'T' )
  413. {
  414. return el == 'T' || ( el >= '0' && el <= '9' );
  415. }
  416. else if ( mask_el >= '0' && mask_el <= '9' )
  417. {
  418. return el == mask_el;
  419. }
  420. return true;
  421. }
  422. };
  423. template <typename Masks, int I = 0, int N = boost::tuples::length<Masks>::value>
  424. struct check_dispatch_tuple
  425. {
  426. template <typename Matrix>
  427. static inline bool apply(Masks const& masks, Matrix const& matrix)
  428. {
  429. typedef typename boost::tuples::element<I, Masks>::type mask_type;
  430. mask_type const& mask = boost::get<I>(masks);
  431. return check_dispatch<mask_type>::apply(mask, matrix)
  432. || check_dispatch_tuple<Masks, I+1>::apply(masks, matrix);
  433. }
  434. };
  435. template <typename Masks, int N>
  436. struct check_dispatch_tuple<Masks, N, N>
  437. {
  438. template <typename Matrix>
  439. static inline bool apply(Masks const&, Matrix const&)
  440. {
  441. return false;
  442. }
  443. };
  444. //template <typename T0, typename T1, typename T2, typename T3, typename T4,
  445. // typename T5, typename T6, typename T7, typename T8, typename T9>
  446. //struct check_dispatch< boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> >
  447. //{
  448. // typedef boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> mask_type;
  449. // template <typename Matrix>
  450. // static inline bool apply(mask_type const& mask, Matrix const& matrix)
  451. // {
  452. // return check_dispatch_tuple<mask_type>::apply(mask, matrix);
  453. // }
  454. //};
  455. template <typename Head, typename Tail>
  456. struct check_dispatch< boost::tuples::cons<Head, Tail> >
  457. {
  458. typedef boost::tuples::cons<Head, Tail> mask_type;
  459. template <typename Matrix>
  460. static inline bool apply(mask_type const& mask, Matrix const& matrix)
  461. {
  462. return check_dispatch_tuple<mask_type>::apply(mask, matrix);
  463. }
  464. };
  465. template <typename Mask, typename Matrix>
  466. inline bool check_matrix(Mask const& mask, Matrix const& matrix)
  467. {
  468. return check_dispatch<Mask>::apply(mask, matrix);
  469. }
  470. // matrix_width
  471. template <typename MatrixOrMask>
  472. struct matrix_width
  473. {
  474. static const std::size_t value = MatrixOrMask::static_width;
  475. };
  476. template <typename Tuple,
  477. int I = 0,
  478. int N = boost::tuples::length<Tuple>::value>
  479. struct matrix_width_tuple
  480. {
  481. static const std::size_t
  482. current = matrix_width<typename boost::tuples::element<I, Tuple>::type>::value;
  483. static const std::size_t
  484. next = matrix_width_tuple<Tuple, I+1>::value;
  485. static const std::size_t
  486. value = current > next ? current : next;
  487. };
  488. template <typename Tuple, int N>
  489. struct matrix_width_tuple<Tuple, N, N>
  490. {
  491. static const std::size_t value = 0;
  492. };
  493. template <typename Head, typename Tail>
  494. struct matrix_width< boost::tuples::cons<Head, Tail> >
  495. {
  496. static const std::size_t
  497. value = matrix_width_tuple< boost::tuples::cons<Head, Tail> >::value;
  498. };
  499. // mask_handler
  500. template <typename Mask, bool Interrupt>
  501. class mask_handler
  502. : private matrix_handler
  503. <
  504. relate::matrix<matrix_width<Mask>::value>
  505. >
  506. {
  507. typedef matrix_handler
  508. <
  509. relate::matrix<matrix_width<Mask>::value>
  510. > base_t;
  511. public:
  512. typedef bool result_type;
  513. bool interrupt;
  514. inline explicit mask_handler(Mask const& m)
  515. : interrupt(false)
  516. , m_mask(m)
  517. {}
  518. result_type result() const
  519. {
  520. return !interrupt
  521. && check_matrix(m_mask, base_t::matrix());
  522. }
  523. template <field F1, field F2, char D>
  524. inline bool may_update() const
  525. {
  526. return detail::relate::may_update<F1, F2, D>(
  527. m_mask, base_t::matrix()
  528. );
  529. }
  530. template <field F1, field F2, char V>
  531. inline void set()
  532. {
  533. if ( relate::interrupt<F1, F2, V, Interrupt>(m_mask) )
  534. {
  535. interrupt = true;
  536. }
  537. else
  538. {
  539. base_t::template set<F1, F2, V>();
  540. }
  541. }
  542. template <field F1, field F2, char V>
  543. inline void update()
  544. {
  545. if ( relate::interrupt<F1, F2, V, Interrupt>(m_mask) )
  546. {
  547. interrupt = true;
  548. }
  549. else
  550. {
  551. base_t::template update<F1, F2, V>();
  552. }
  553. }
  554. private:
  555. Mask const& m_mask;
  556. };
  557. // --------------- COMPILE-TIME MASK ----------------
  558. // static_check_characters
  559. template
  560. <
  561. typename Seq,
  562. typename First = typename boost::mpl::begin<Seq>::type,
  563. typename Last = typename boost::mpl::end<Seq>::type
  564. >
  565. struct static_check_characters
  566. : static_check_characters
  567. <
  568. Seq,
  569. typename boost::mpl::next<First>::type
  570. >
  571. {
  572. typedef typename boost::mpl::deref<First>::type type;
  573. static const char value = type::value;
  574. static const bool is_valid = (value >= '0' && value <= '9')
  575. || value == 'T' || value == 'F' || value == '*';
  576. BOOST_MPL_ASSERT_MSG((is_valid),
  577. INVALID_STATIC_MASK_CHARACTER,
  578. (type));
  579. };
  580. template <typename Seq, typename Last>
  581. struct static_check_characters<Seq, Last, Last>
  582. {};
  583. // static_mask
  584. template
  585. <
  586. typename Seq,
  587. std::size_t Height,
  588. std::size_t Width = Height
  589. >
  590. struct static_mask
  591. {
  592. static const std::size_t static_width = Width;
  593. static const std::size_t static_height = Height;
  594. static const std::size_t static_size = Width * Height;
  595. BOOST_STATIC_ASSERT(
  596. std::size_t(boost::mpl::size<Seq>::type::value) == static_size);
  597. template <detail::relate::field F1, detail::relate::field F2>
  598. struct static_get
  599. {
  600. BOOST_STATIC_ASSERT(std::size_t(F1) < static_height);
  601. BOOST_STATIC_ASSERT(std::size_t(F2) < static_width);
  602. static const char value
  603. = boost::mpl::at_c<Seq, F1 * static_width + F2>::type::value;
  604. };
  605. private:
  606. // check static_mask characters
  607. enum { mask_check = sizeof(static_check_characters<Seq>) };
  608. };
  609. // static_should_handle_element
  610. template <typename StaticMask, field F1, field F2, bool IsSequence>
  611. struct static_should_handle_element_dispatch
  612. {
  613. static const char mask_el = StaticMask::template static_get<F1, F2>::value;
  614. static const bool value = mask_el == 'F'
  615. || mask_el == 'T'
  616. || ( mask_el >= '0' && mask_el <= '9' );
  617. };
  618. template <typename First, typename Last, field F1, field F2>
  619. struct static_should_handle_element_sequence
  620. {
  621. typedef typename boost::mpl::deref<First>::type StaticMask;
  622. static const bool value
  623. = static_should_handle_element_dispatch
  624. <
  625. StaticMask,
  626. F1, F2,
  627. boost::mpl::is_sequence<StaticMask>::value
  628. >::value
  629. || static_should_handle_element_sequence
  630. <
  631. typename boost::mpl::next<First>::type,
  632. Last,
  633. F1, F2
  634. >::value;
  635. };
  636. template <typename Last, field F1, field F2>
  637. struct static_should_handle_element_sequence<Last, Last, F1, F2>
  638. {
  639. static const bool value = false;
  640. };
  641. template <typename StaticMask, field F1, field F2>
  642. struct static_should_handle_element_dispatch<StaticMask, F1, F2, true>
  643. {
  644. static const bool value
  645. = static_should_handle_element_sequence
  646. <
  647. typename boost::mpl::begin<StaticMask>::type,
  648. typename boost::mpl::end<StaticMask>::type,
  649. F1, F2
  650. >::value;
  651. };
  652. template <typename StaticMask, field F1, field F2>
  653. struct static_should_handle_element
  654. {
  655. static const bool value
  656. = static_should_handle_element_dispatch
  657. <
  658. StaticMask,
  659. F1, F2,
  660. boost::mpl::is_sequence<StaticMask>::value
  661. >::value;
  662. };
  663. // static_interrupt
  664. template <typename StaticMask, char V, field F1, field F2, bool InterruptEnabled, bool IsSequence>
  665. struct static_interrupt_dispatch
  666. {
  667. static const bool value = false;
  668. };
  669. template <typename StaticMask, char V, field F1, field F2, bool IsSequence>
  670. struct static_interrupt_dispatch<StaticMask, V, F1, F2, true, IsSequence>
  671. {
  672. static const char mask_el = StaticMask::template static_get<F1, F2>::value;
  673. static const bool value
  674. = ( V >= '0' && V <= '9' ) ?
  675. ( mask_el == 'F' || ( mask_el < V && mask_el >= '0' && mask_el <= '9' ) ) :
  676. ( ( V == 'T' ) ? mask_el == 'F' : false );
  677. };
  678. template <typename First, typename Last, char V, field F1, field F2>
  679. struct static_interrupt_sequence
  680. {
  681. typedef typename boost::mpl::deref<First>::type StaticMask;
  682. static const bool value
  683. = static_interrupt_dispatch
  684. <
  685. StaticMask,
  686. V, F1, F2,
  687. true,
  688. boost::mpl::is_sequence<StaticMask>::value
  689. >::value
  690. && static_interrupt_sequence
  691. <
  692. typename boost::mpl::next<First>::type,
  693. Last,
  694. V, F1, F2
  695. >::value;
  696. };
  697. template <typename Last, char V, field F1, field F2>
  698. struct static_interrupt_sequence<Last, Last, V, F1, F2>
  699. {
  700. static const bool value = true;
  701. };
  702. template <typename StaticMask, char V, field F1, field F2>
  703. struct static_interrupt_dispatch<StaticMask, V, F1, F2, true, true>
  704. {
  705. static const bool value
  706. = static_interrupt_sequence
  707. <
  708. typename boost::mpl::begin<StaticMask>::type,
  709. typename boost::mpl::end<StaticMask>::type,
  710. V, F1, F2
  711. >::value;
  712. };
  713. template <typename StaticMask, char V, field F1, field F2, bool EnableInterrupt>
  714. struct static_interrupt
  715. {
  716. static const bool value
  717. = static_interrupt_dispatch
  718. <
  719. StaticMask,
  720. V, F1, F2,
  721. EnableInterrupt,
  722. boost::mpl::is_sequence<StaticMask>::value
  723. >::value;
  724. };
  725. // static_may_update
  726. template <typename StaticMask, char D, field F1, field F2, bool IsSequence>
  727. struct static_may_update_dispatch
  728. {
  729. static const char mask_el = StaticMask::template static_get<F1, F2>::value;
  730. static const int version
  731. = mask_el == 'F' ? 0
  732. : mask_el == 'T' ? 1
  733. : mask_el >= '0' && mask_el <= '9' ? 2
  734. : 3;
  735. template <typename Matrix>
  736. static inline bool apply(Matrix const& matrix)
  737. {
  738. return apply_dispatch(matrix, integral_constant<int, version>());
  739. }
  740. // mask_el == 'F'
  741. template <typename Matrix>
  742. static inline bool apply_dispatch(Matrix const& , integral_constant<int, 0>)
  743. {
  744. return true;
  745. }
  746. // mask_el == 'T'
  747. template <typename Matrix>
  748. static inline bool apply_dispatch(Matrix const& matrix, integral_constant<int, 1>)
  749. {
  750. char const c = matrix.template get<F1, F2>();
  751. return c == 'F'; // if it's T or between 0 and 9, the result will be the same
  752. }
  753. // mask_el >= '0' && mask_el <= '9'
  754. template <typename Matrix>
  755. static inline bool apply_dispatch(Matrix const& matrix, integral_constant<int, 2>)
  756. {
  757. char const c = matrix.template get<F1, F2>();
  758. return D > c || c > '9';
  759. }
  760. // else
  761. template <typename Matrix>
  762. static inline bool apply_dispatch(Matrix const&, integral_constant<int, 3>)
  763. {
  764. return false;
  765. }
  766. };
  767. template <typename First, typename Last, char D, field F1, field F2>
  768. struct static_may_update_sequence
  769. {
  770. typedef typename boost::mpl::deref<First>::type StaticMask;
  771. template <typename Matrix>
  772. static inline bool apply(Matrix const& matrix)
  773. {
  774. return static_may_update_dispatch
  775. <
  776. StaticMask,
  777. D, F1, F2,
  778. boost::mpl::is_sequence<StaticMask>::value
  779. >::apply(matrix)
  780. || static_may_update_sequence
  781. <
  782. typename boost::mpl::next<First>::type,
  783. Last,
  784. D, F1, F2
  785. >::apply(matrix);
  786. }
  787. };
  788. template <typename Last, char D, field F1, field F2>
  789. struct static_may_update_sequence<Last, Last, D, F1, F2>
  790. {
  791. template <typename Matrix>
  792. static inline bool apply(Matrix const& /*matrix*/)
  793. {
  794. return false;
  795. }
  796. };
  797. template <typename StaticMask, char D, field F1, field F2>
  798. struct static_may_update_dispatch<StaticMask, D, F1, F2, true>
  799. {
  800. template <typename Matrix>
  801. static inline bool apply(Matrix const& matrix)
  802. {
  803. return static_may_update_sequence
  804. <
  805. typename boost::mpl::begin<StaticMask>::type,
  806. typename boost::mpl::end<StaticMask>::type,
  807. D, F1, F2
  808. >::apply(matrix);
  809. }
  810. };
  811. template <typename StaticMask, char D, field F1, field F2>
  812. struct static_may_update
  813. {
  814. template <typename Matrix>
  815. static inline bool apply(Matrix const& matrix)
  816. {
  817. return static_may_update_dispatch
  818. <
  819. StaticMask,
  820. D, F1, F2,
  821. boost::mpl::is_sequence<StaticMask>::value
  822. >::apply(matrix);
  823. }
  824. };
  825. // static_check_matrix
  826. template <typename StaticMask, bool IsSequence>
  827. struct static_check_dispatch
  828. {
  829. template <typename Matrix>
  830. static inline bool apply(Matrix const& matrix)
  831. {
  832. return per_one<interior, interior>::apply(matrix)
  833. && per_one<interior, boundary>::apply(matrix)
  834. && per_one<interior, exterior>::apply(matrix)
  835. && per_one<boundary, interior>::apply(matrix)
  836. && per_one<boundary, boundary>::apply(matrix)
  837. && per_one<boundary, exterior>::apply(matrix)
  838. && per_one<exterior, interior>::apply(matrix)
  839. && per_one<exterior, boundary>::apply(matrix)
  840. && per_one<exterior, exterior>::apply(matrix);
  841. }
  842. template <field F1, field F2>
  843. struct per_one
  844. {
  845. static const char mask_el = StaticMask::template static_get<F1, F2>::value;
  846. static const int version
  847. = mask_el == 'F' ? 0
  848. : mask_el == 'T' ? 1
  849. : mask_el >= '0' && mask_el <= '9' ? 2
  850. : 3;
  851. template <typename Matrix>
  852. static inline bool apply(Matrix const& matrix)
  853. {
  854. const char el = matrix.template get<F1, F2>();
  855. return apply_dispatch(el, integral_constant<int, version>());
  856. }
  857. // mask_el == 'F'
  858. static inline bool apply_dispatch(char el, integral_constant<int, 0>)
  859. {
  860. return el == 'F';
  861. }
  862. // mask_el == 'T'
  863. static inline bool apply_dispatch(char el, integral_constant<int, 1>)
  864. {
  865. return el == 'T' || ( el >= '0' && el <= '9' );
  866. }
  867. // mask_el >= '0' && mask_el <= '9'
  868. static inline bool apply_dispatch(char el, integral_constant<int, 2>)
  869. {
  870. return el == mask_el;
  871. }
  872. // else
  873. static inline bool apply_dispatch(char /*el*/, integral_constant<int, 3>)
  874. {
  875. return true;
  876. }
  877. };
  878. };
  879. template <typename First, typename Last>
  880. struct static_check_sequence
  881. {
  882. typedef typename boost::mpl::deref<First>::type StaticMask;
  883. template <typename Matrix>
  884. static inline bool apply(Matrix const& matrix)
  885. {
  886. return static_check_dispatch
  887. <
  888. StaticMask,
  889. boost::mpl::is_sequence<StaticMask>::value
  890. >::apply(matrix)
  891. || static_check_sequence
  892. <
  893. typename boost::mpl::next<First>::type,
  894. Last
  895. >::apply(matrix);
  896. }
  897. };
  898. template <typename Last>
  899. struct static_check_sequence<Last, Last>
  900. {
  901. template <typename Matrix>
  902. static inline bool apply(Matrix const& /*matrix*/)
  903. {
  904. return false;
  905. }
  906. };
  907. template <typename StaticMask>
  908. struct static_check_dispatch<StaticMask, true>
  909. {
  910. template <typename Matrix>
  911. static inline bool apply(Matrix const& matrix)
  912. {
  913. return static_check_sequence
  914. <
  915. typename boost::mpl::begin<StaticMask>::type,
  916. typename boost::mpl::end<StaticMask>::type
  917. >::apply(matrix);
  918. }
  919. };
  920. template <typename StaticMask>
  921. struct static_check_matrix
  922. {
  923. template <typename Matrix>
  924. static inline bool apply(Matrix const& matrix)
  925. {
  926. return static_check_dispatch
  927. <
  928. StaticMask,
  929. boost::mpl::is_sequence<StaticMask>::value
  930. >::apply(matrix);
  931. }
  932. };
  933. // static_mask_handler
  934. template <typename StaticMask, bool Interrupt>
  935. class static_mask_handler
  936. : private matrix_handler< matrix<3> >
  937. {
  938. typedef matrix_handler< relate::matrix<3> > base_type;
  939. public:
  940. typedef bool result_type;
  941. bool interrupt;
  942. inline static_mask_handler()
  943. : interrupt(false)
  944. {}
  945. inline explicit static_mask_handler(StaticMask const& /*dummy*/)
  946. : interrupt(false)
  947. {}
  948. result_type result() const
  949. {
  950. return (!Interrupt || !interrupt)
  951. && static_check_matrix<StaticMask>::apply(base_type::matrix());
  952. }
  953. template <field F1, field F2, char D>
  954. inline bool may_update() const
  955. {
  956. return static_may_update<StaticMask, D, F1, F2>::
  957. apply(base_type::matrix());
  958. }
  959. template <field F1, field F2>
  960. static inline bool expects()
  961. {
  962. return static_should_handle_element<StaticMask, F1, F2>::value;
  963. }
  964. template <field F1, field F2, char V>
  965. inline void set()
  966. {
  967. static const bool interrupt_c = static_interrupt<StaticMask, V, F1, F2, Interrupt>::value;
  968. static const bool should_handle = static_should_handle_element<StaticMask, F1, F2>::value;
  969. static const int version = interrupt_c ? 0
  970. : should_handle ? 1
  971. : 2;
  972. set_dispatch<F1, F2, V>(integral_constant<int, version>());
  973. }
  974. template <field F1, field F2, char V>
  975. inline void update()
  976. {
  977. static const bool interrupt_c = static_interrupt<StaticMask, V, F1, F2, Interrupt>::value;
  978. static const bool should_handle = static_should_handle_element<StaticMask, F1, F2>::value;
  979. static const int version = interrupt_c ? 0
  980. : should_handle ? 1
  981. : 2;
  982. update_dispatch<F1, F2, V>(integral_constant<int, version>());
  983. }
  984. private:
  985. // Interrupt && interrupt
  986. template <field F1, field F2, char V>
  987. inline void set_dispatch(integral_constant<int, 0>)
  988. {
  989. interrupt = true;
  990. }
  991. // else should_handle
  992. template <field F1, field F2, char V>
  993. inline void set_dispatch(integral_constant<int, 1>)
  994. {
  995. base_type::template set<F1, F2, V>();
  996. }
  997. // else
  998. template <field F1, field F2, char V>
  999. inline void set_dispatch(integral_constant<int, 2>)
  1000. {}
  1001. // Interrupt && interrupt
  1002. template <field F1, field F2, char V>
  1003. inline void update_dispatch(integral_constant<int, 0>)
  1004. {
  1005. interrupt = true;
  1006. }
  1007. // else should_handle
  1008. template <field F1, field F2, char V>
  1009. inline void update_dispatch(integral_constant<int, 1>)
  1010. {
  1011. base_type::template update<F1, F2, V>();
  1012. }
  1013. // else
  1014. template <field F1, field F2, char V>
  1015. inline void update_dispatch(integral_constant<int, 2>)
  1016. {}
  1017. };
  1018. // --------------- UTIL FUNCTIONS ----------------
  1019. // set
  1020. template <field F1, field F2, char V, typename Result>
  1021. inline void set(Result & res)
  1022. {
  1023. res.template set<F1, F2, V>();
  1024. }
  1025. template <field F1, field F2, char V, bool Transpose>
  1026. struct set_dispatch
  1027. {
  1028. template <typename Result>
  1029. static inline void apply(Result & res)
  1030. {
  1031. res.template set<F1, F2, V>();
  1032. }
  1033. };
  1034. template <field F1, field F2, char V>
  1035. struct set_dispatch<F1, F2, V, true>
  1036. {
  1037. template <typename Result>
  1038. static inline void apply(Result & res)
  1039. {
  1040. res.template set<F2, F1, V>();
  1041. }
  1042. };
  1043. template <field F1, field F2, char V, bool Transpose, typename Result>
  1044. inline void set(Result & res)
  1045. {
  1046. set_dispatch<F1, F2, V, Transpose>::apply(res);
  1047. }
  1048. // update
  1049. template <field F1, field F2, char D, typename Result>
  1050. inline void update(Result & res)
  1051. {
  1052. res.template update<F1, F2, D>();
  1053. }
  1054. template <field F1, field F2, char D, bool Transpose>
  1055. struct update_result_dispatch
  1056. {
  1057. template <typename Result>
  1058. static inline void apply(Result & res)
  1059. {
  1060. update<F1, F2, D>(res);
  1061. }
  1062. };
  1063. template <field F1, field F2, char D>
  1064. struct update_result_dispatch<F1, F2, D, true>
  1065. {
  1066. template <typename Result>
  1067. static inline void apply(Result & res)
  1068. {
  1069. update<F2, F1, D>(res);
  1070. }
  1071. };
  1072. template <field F1, field F2, char D, bool Transpose, typename Result>
  1073. inline void update(Result & res)
  1074. {
  1075. update_result_dispatch<F1, F2, D, Transpose>::apply(res);
  1076. }
  1077. // may_update
  1078. template <field F1, field F2, char D, typename Result>
  1079. inline bool may_update(Result const& res)
  1080. {
  1081. return res.template may_update<F1, F2, D>();
  1082. }
  1083. template <field F1, field F2, char D, bool Transpose>
  1084. struct may_update_result_dispatch
  1085. {
  1086. template <typename Result>
  1087. static inline bool apply(Result const& res)
  1088. {
  1089. return may_update<F1, F2, D>(res);
  1090. }
  1091. };
  1092. template <field F1, field F2, char D>
  1093. struct may_update_result_dispatch<F1, F2, D, true>
  1094. {
  1095. template <typename Result>
  1096. static inline bool apply(Result const& res)
  1097. {
  1098. return may_update<F2, F1, D>(res);
  1099. }
  1100. };
  1101. template <field F1, field F2, char D, bool Transpose, typename Result>
  1102. inline bool may_update(Result const& res)
  1103. {
  1104. return may_update_result_dispatch<F1, F2, D, Transpose>::apply(res);
  1105. }
  1106. // result_dimension
  1107. template <typename Geometry>
  1108. struct result_dimension
  1109. {
  1110. BOOST_STATIC_ASSERT(geometry::dimension<Geometry>::value >= 0);
  1111. static const char value
  1112. = ( geometry::dimension<Geometry>::value <= 9 ) ?
  1113. ( '0' + geometry::dimension<Geometry>::value ) :
  1114. 'T';
  1115. };
  1116. }} // namespace detail::relate
  1117. #endif // DOXYGEN_NO_DETAIL
  1118. }} // namespace boost::geometry
  1119. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_RESULT_HPP