result.hpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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-2019.
  5. // Modifications copyright (c) 2013-2019 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. // --------------- FALSE MASK ----------------
  558. struct false_mask {};
  559. // --------------- COMPILE-TIME MASK ----------------
  560. // static_check_characters
  561. template
  562. <
  563. typename Seq,
  564. typename First = typename boost::mpl::begin<Seq>::type,
  565. typename Last = typename boost::mpl::end<Seq>::type
  566. >
  567. struct static_check_characters
  568. : static_check_characters
  569. <
  570. Seq,
  571. typename boost::mpl::next<First>::type
  572. >
  573. {
  574. typedef typename boost::mpl::deref<First>::type type;
  575. static const char value = type::value;
  576. static const bool is_valid = (value >= '0' && value <= '9')
  577. || value == 'T' || value == 'F' || value == '*';
  578. BOOST_MPL_ASSERT_MSG((is_valid),
  579. INVALID_STATIC_MASK_CHARACTER,
  580. (type));
  581. };
  582. template <typename Seq, typename Last>
  583. struct static_check_characters<Seq, Last, Last>
  584. {};
  585. // static_mask
  586. template
  587. <
  588. typename Seq,
  589. std::size_t Height,
  590. std::size_t Width = Height
  591. >
  592. struct static_mask
  593. {
  594. static const std::size_t static_width = Width;
  595. static const std::size_t static_height = Height;
  596. static const std::size_t static_size = Width * Height;
  597. BOOST_STATIC_ASSERT(
  598. std::size_t(boost::mpl::size<Seq>::type::value) == static_size);
  599. template <detail::relate::field F1, detail::relate::field F2>
  600. struct static_get
  601. {
  602. BOOST_STATIC_ASSERT(std::size_t(F1) < static_height);
  603. BOOST_STATIC_ASSERT(std::size_t(F2) < static_width);
  604. static const char value
  605. = boost::mpl::at_c<Seq, F1 * static_width + F2>::type::value;
  606. };
  607. private:
  608. // check static_mask characters
  609. enum { mask_check = sizeof(static_check_characters<Seq>) };
  610. };
  611. // static_should_handle_element
  612. template <typename StaticMask, field F1, field F2, bool IsSequence>
  613. struct static_should_handle_element_dispatch
  614. {
  615. static const char mask_el = StaticMask::template static_get<F1, F2>::value;
  616. static const bool value = mask_el == 'F'
  617. || mask_el == 'T'
  618. || ( mask_el >= '0' && mask_el <= '9' );
  619. };
  620. template <typename First, typename Last, field F1, field F2>
  621. struct static_should_handle_element_sequence
  622. {
  623. typedef typename boost::mpl::deref<First>::type StaticMask;
  624. static const bool value
  625. = static_should_handle_element_dispatch
  626. <
  627. StaticMask,
  628. F1, F2,
  629. boost::mpl::is_sequence<StaticMask>::value
  630. >::value
  631. || static_should_handle_element_sequence
  632. <
  633. typename boost::mpl::next<First>::type,
  634. Last,
  635. F1, F2
  636. >::value;
  637. };
  638. template <typename Last, field F1, field F2>
  639. struct static_should_handle_element_sequence<Last, Last, F1, F2>
  640. {
  641. static const bool value = false;
  642. };
  643. template <typename StaticMask, field F1, field F2>
  644. struct static_should_handle_element_dispatch<StaticMask, F1, F2, true>
  645. {
  646. static const bool value
  647. = static_should_handle_element_sequence
  648. <
  649. typename boost::mpl::begin<StaticMask>::type,
  650. typename boost::mpl::end<StaticMask>::type,
  651. F1, F2
  652. >::value;
  653. };
  654. template <typename StaticMask, field F1, field F2>
  655. struct static_should_handle_element
  656. {
  657. static const bool value
  658. = static_should_handle_element_dispatch
  659. <
  660. StaticMask,
  661. F1, F2,
  662. boost::mpl::is_sequence<StaticMask>::value
  663. >::value;
  664. };
  665. // static_interrupt
  666. template <typename StaticMask, char V, field F1, field F2, bool InterruptEnabled, bool IsSequence>
  667. struct static_interrupt_dispatch
  668. {
  669. static const bool value = false;
  670. };
  671. template <typename StaticMask, char V, field F1, field F2, bool IsSequence>
  672. struct static_interrupt_dispatch<StaticMask, V, F1, F2, true, IsSequence>
  673. {
  674. static const char mask_el = StaticMask::template static_get<F1, F2>::value;
  675. static const bool value
  676. = ( V >= '0' && V <= '9' ) ?
  677. ( mask_el == 'F' || ( mask_el < V && mask_el >= '0' && mask_el <= '9' ) ) :
  678. ( ( V == 'T' ) ? mask_el == 'F' : false );
  679. };
  680. template <typename First, typename Last, char V, field F1, field F2>
  681. struct static_interrupt_sequence
  682. {
  683. typedef typename boost::mpl::deref<First>::type StaticMask;
  684. static const bool value
  685. = static_interrupt_dispatch
  686. <
  687. StaticMask,
  688. V, F1, F2,
  689. true,
  690. boost::mpl::is_sequence<StaticMask>::value
  691. >::value
  692. && static_interrupt_sequence
  693. <
  694. typename boost::mpl::next<First>::type,
  695. Last,
  696. V, F1, F2
  697. >::value;
  698. };
  699. template <typename Last, char V, field F1, field F2>
  700. struct static_interrupt_sequence<Last, Last, V, F1, F2>
  701. {
  702. static const bool value = true;
  703. };
  704. template <typename StaticMask, char V, field F1, field F2>
  705. struct static_interrupt_dispatch<StaticMask, V, F1, F2, true, true>
  706. {
  707. static const bool value
  708. = static_interrupt_sequence
  709. <
  710. typename boost::mpl::begin<StaticMask>::type,
  711. typename boost::mpl::end<StaticMask>::type,
  712. V, F1, F2
  713. >::value;
  714. };
  715. template <typename StaticMask, char V, field F1, field F2, bool EnableInterrupt>
  716. struct static_interrupt
  717. {
  718. static const bool value
  719. = static_interrupt_dispatch
  720. <
  721. StaticMask,
  722. V, F1, F2,
  723. EnableInterrupt,
  724. boost::mpl::is_sequence<StaticMask>::value
  725. >::value;
  726. };
  727. // static_may_update
  728. template <typename StaticMask, char D, field F1, field F2, bool IsSequence>
  729. struct static_may_update_dispatch
  730. {
  731. static const char mask_el = StaticMask::template static_get<F1, F2>::value;
  732. static const int version
  733. = mask_el == 'F' ? 0
  734. : mask_el == 'T' ? 1
  735. : mask_el >= '0' && mask_el <= '9' ? 2
  736. : 3;
  737. template <typename Matrix>
  738. static inline bool apply(Matrix const& matrix)
  739. {
  740. return apply_dispatch(matrix, integral_constant<int, version>());
  741. }
  742. // mask_el == 'F'
  743. template <typename Matrix>
  744. static inline bool apply_dispatch(Matrix const& , integral_constant<int, 0>)
  745. {
  746. return true;
  747. }
  748. // mask_el == 'T'
  749. template <typename Matrix>
  750. static inline bool apply_dispatch(Matrix const& matrix, integral_constant<int, 1>)
  751. {
  752. char const c = matrix.template get<F1, F2>();
  753. return c == 'F'; // if it's T or between 0 and 9, the result will be the same
  754. }
  755. // mask_el >= '0' && mask_el <= '9'
  756. template <typename Matrix>
  757. static inline bool apply_dispatch(Matrix const& matrix, integral_constant<int, 2>)
  758. {
  759. char const c = matrix.template get<F1, F2>();
  760. return D > c || c > '9';
  761. }
  762. // else
  763. template <typename Matrix>
  764. static inline bool apply_dispatch(Matrix const&, integral_constant<int, 3>)
  765. {
  766. return false;
  767. }
  768. };
  769. template <typename First, typename Last, char D, field F1, field F2>
  770. struct static_may_update_sequence
  771. {
  772. typedef typename boost::mpl::deref<First>::type StaticMask;
  773. template <typename Matrix>
  774. static inline bool apply(Matrix const& matrix)
  775. {
  776. return static_may_update_dispatch
  777. <
  778. StaticMask,
  779. D, F1, F2,
  780. boost::mpl::is_sequence<StaticMask>::value
  781. >::apply(matrix)
  782. || static_may_update_sequence
  783. <
  784. typename boost::mpl::next<First>::type,
  785. Last,
  786. D, F1, F2
  787. >::apply(matrix);
  788. }
  789. };
  790. template <typename Last, char D, field F1, field F2>
  791. struct static_may_update_sequence<Last, Last, D, F1, F2>
  792. {
  793. template <typename Matrix>
  794. static inline bool apply(Matrix const& /*matrix*/)
  795. {
  796. return false;
  797. }
  798. };
  799. template <typename StaticMask, char D, field F1, field F2>
  800. struct static_may_update_dispatch<StaticMask, D, F1, F2, true>
  801. {
  802. template <typename Matrix>
  803. static inline bool apply(Matrix const& matrix)
  804. {
  805. return static_may_update_sequence
  806. <
  807. typename boost::mpl::begin<StaticMask>::type,
  808. typename boost::mpl::end<StaticMask>::type,
  809. D, F1, F2
  810. >::apply(matrix);
  811. }
  812. };
  813. template <typename StaticMask, char D, field F1, field F2>
  814. struct static_may_update
  815. {
  816. template <typename Matrix>
  817. static inline bool apply(Matrix const& matrix)
  818. {
  819. return static_may_update_dispatch
  820. <
  821. StaticMask,
  822. D, F1, F2,
  823. boost::mpl::is_sequence<StaticMask>::value
  824. >::apply(matrix);
  825. }
  826. };
  827. // static_check_matrix
  828. template <typename StaticMask, bool IsSequence>
  829. struct static_check_dispatch
  830. {
  831. template <typename Matrix>
  832. static inline bool apply(Matrix const& matrix)
  833. {
  834. return per_one<interior, interior>::apply(matrix)
  835. && per_one<interior, boundary>::apply(matrix)
  836. && per_one<interior, exterior>::apply(matrix)
  837. && per_one<boundary, interior>::apply(matrix)
  838. && per_one<boundary, boundary>::apply(matrix)
  839. && per_one<boundary, exterior>::apply(matrix)
  840. && per_one<exterior, interior>::apply(matrix)
  841. && per_one<exterior, boundary>::apply(matrix)
  842. && per_one<exterior, exterior>::apply(matrix);
  843. }
  844. template <field F1, field F2>
  845. struct per_one
  846. {
  847. static const char mask_el = StaticMask::template static_get<F1, F2>::value;
  848. static const int version
  849. = mask_el == 'F' ? 0
  850. : mask_el == 'T' ? 1
  851. : mask_el >= '0' && mask_el <= '9' ? 2
  852. : 3;
  853. template <typename Matrix>
  854. static inline bool apply(Matrix const& matrix)
  855. {
  856. const char el = matrix.template get<F1, F2>();
  857. return apply_dispatch(el, integral_constant<int, version>());
  858. }
  859. // mask_el == 'F'
  860. static inline bool apply_dispatch(char el, integral_constant<int, 0>)
  861. {
  862. return el == 'F';
  863. }
  864. // mask_el == 'T'
  865. static inline bool apply_dispatch(char el, integral_constant<int, 1>)
  866. {
  867. return el == 'T' || ( el >= '0' && el <= '9' );
  868. }
  869. // mask_el >= '0' && mask_el <= '9'
  870. static inline bool apply_dispatch(char el, integral_constant<int, 2>)
  871. {
  872. return el == mask_el;
  873. }
  874. // else
  875. static inline bool apply_dispatch(char /*el*/, integral_constant<int, 3>)
  876. {
  877. return true;
  878. }
  879. };
  880. };
  881. template <typename First, typename Last>
  882. struct static_check_sequence
  883. {
  884. typedef typename boost::mpl::deref<First>::type StaticMask;
  885. template <typename Matrix>
  886. static inline bool apply(Matrix const& matrix)
  887. {
  888. return static_check_dispatch
  889. <
  890. StaticMask,
  891. boost::mpl::is_sequence<StaticMask>::value
  892. >::apply(matrix)
  893. || static_check_sequence
  894. <
  895. typename boost::mpl::next<First>::type,
  896. Last
  897. >::apply(matrix);
  898. }
  899. };
  900. template <typename Last>
  901. struct static_check_sequence<Last, Last>
  902. {
  903. template <typename Matrix>
  904. static inline bool apply(Matrix const& /*matrix*/)
  905. {
  906. return false;
  907. }
  908. };
  909. template <typename StaticMask>
  910. struct static_check_dispatch<StaticMask, true>
  911. {
  912. template <typename Matrix>
  913. static inline bool apply(Matrix const& matrix)
  914. {
  915. return static_check_sequence
  916. <
  917. typename boost::mpl::begin<StaticMask>::type,
  918. typename boost::mpl::end<StaticMask>::type
  919. >::apply(matrix);
  920. }
  921. };
  922. template <typename StaticMask>
  923. struct static_check_matrix
  924. {
  925. template <typename Matrix>
  926. static inline bool apply(Matrix const& matrix)
  927. {
  928. return static_check_dispatch
  929. <
  930. StaticMask,
  931. boost::mpl::is_sequence<StaticMask>::value
  932. >::apply(matrix);
  933. }
  934. };
  935. // static_mask_handler
  936. template <typename StaticMask, bool Interrupt>
  937. class static_mask_handler
  938. : private matrix_handler< matrix<3> >
  939. {
  940. typedef matrix_handler< relate::matrix<3> > base_type;
  941. public:
  942. typedef bool result_type;
  943. bool interrupt;
  944. inline static_mask_handler()
  945. : interrupt(false)
  946. {}
  947. inline explicit static_mask_handler(StaticMask const& /*dummy*/)
  948. : interrupt(false)
  949. {}
  950. result_type result() const
  951. {
  952. return (!Interrupt || !interrupt)
  953. && static_check_matrix<StaticMask>::apply(base_type::matrix());
  954. }
  955. template <field F1, field F2, char D>
  956. inline bool may_update() const
  957. {
  958. return static_may_update<StaticMask, D, F1, F2>::
  959. apply(base_type::matrix());
  960. }
  961. template <field F1, field F2>
  962. static inline bool expects()
  963. {
  964. return static_should_handle_element<StaticMask, F1, F2>::value;
  965. }
  966. template <field F1, field F2, char V>
  967. inline void set()
  968. {
  969. static const bool interrupt_c = static_interrupt<StaticMask, V, F1, F2, Interrupt>::value;
  970. static const bool should_handle = static_should_handle_element<StaticMask, F1, F2>::value;
  971. static const int version = interrupt_c ? 0
  972. : should_handle ? 1
  973. : 2;
  974. set_dispatch<F1, F2, V>(integral_constant<int, version>());
  975. }
  976. template <field F1, field F2, char V>
  977. inline void update()
  978. {
  979. static const bool interrupt_c = static_interrupt<StaticMask, V, F1, F2, Interrupt>::value;
  980. static const bool should_handle = static_should_handle_element<StaticMask, F1, F2>::value;
  981. static const int version = interrupt_c ? 0
  982. : should_handle ? 1
  983. : 2;
  984. update_dispatch<F1, F2, V>(integral_constant<int, version>());
  985. }
  986. private:
  987. // Interrupt && interrupt
  988. template <field F1, field F2, char V>
  989. inline void set_dispatch(integral_constant<int, 0>)
  990. {
  991. interrupt = true;
  992. }
  993. // else should_handle
  994. template <field F1, field F2, char V>
  995. inline void set_dispatch(integral_constant<int, 1>)
  996. {
  997. base_type::template set<F1, F2, V>();
  998. }
  999. // else
  1000. template <field F1, field F2, char V>
  1001. inline void set_dispatch(integral_constant<int, 2>)
  1002. {}
  1003. // Interrupt && interrupt
  1004. template <field F1, field F2, char V>
  1005. inline void update_dispatch(integral_constant<int, 0>)
  1006. {
  1007. interrupt = true;
  1008. }
  1009. // else should_handle
  1010. template <field F1, field F2, char V>
  1011. inline void update_dispatch(integral_constant<int, 1>)
  1012. {
  1013. base_type::template update<F1, F2, V>();
  1014. }
  1015. // else
  1016. template <field F1, field F2, char V>
  1017. inline void update_dispatch(integral_constant<int, 2>)
  1018. {}
  1019. };
  1020. // --------------- UTIL FUNCTIONS ----------------
  1021. // set
  1022. template <field F1, field F2, char V, typename Result>
  1023. inline void set(Result & res)
  1024. {
  1025. res.template set<F1, F2, V>();
  1026. }
  1027. template <field F1, field F2, char V, bool Transpose>
  1028. struct set_dispatch
  1029. {
  1030. template <typename Result>
  1031. static inline void apply(Result & res)
  1032. {
  1033. res.template set<F1, F2, V>();
  1034. }
  1035. };
  1036. template <field F1, field F2, char V>
  1037. struct set_dispatch<F1, F2, V, true>
  1038. {
  1039. template <typename Result>
  1040. static inline void apply(Result & res)
  1041. {
  1042. res.template set<F2, F1, V>();
  1043. }
  1044. };
  1045. template <field F1, field F2, char V, bool Transpose, typename Result>
  1046. inline void set(Result & res)
  1047. {
  1048. set_dispatch<F1, F2, V, Transpose>::apply(res);
  1049. }
  1050. // update
  1051. template <field F1, field F2, char D, typename Result>
  1052. inline void update(Result & res)
  1053. {
  1054. res.template update<F1, F2, D>();
  1055. }
  1056. template <field F1, field F2, char D, bool Transpose>
  1057. struct update_result_dispatch
  1058. {
  1059. template <typename Result>
  1060. static inline void apply(Result & res)
  1061. {
  1062. update<F1, F2, D>(res);
  1063. }
  1064. };
  1065. template <field F1, field F2, char D>
  1066. struct update_result_dispatch<F1, F2, D, true>
  1067. {
  1068. template <typename Result>
  1069. static inline void apply(Result & res)
  1070. {
  1071. update<F2, F1, D>(res);
  1072. }
  1073. };
  1074. template <field F1, field F2, char D, bool Transpose, typename Result>
  1075. inline void update(Result & res)
  1076. {
  1077. update_result_dispatch<F1, F2, D, Transpose>::apply(res);
  1078. }
  1079. // may_update
  1080. template <field F1, field F2, char D, typename Result>
  1081. inline bool may_update(Result const& res)
  1082. {
  1083. return res.template may_update<F1, F2, D>();
  1084. }
  1085. template <field F1, field F2, char D, bool Transpose>
  1086. struct may_update_result_dispatch
  1087. {
  1088. template <typename Result>
  1089. static inline bool apply(Result const& res)
  1090. {
  1091. return may_update<F1, F2, D>(res);
  1092. }
  1093. };
  1094. template <field F1, field F2, char D>
  1095. struct may_update_result_dispatch<F1, F2, D, true>
  1096. {
  1097. template <typename Result>
  1098. static inline bool apply(Result const& res)
  1099. {
  1100. return may_update<F2, F1, D>(res);
  1101. }
  1102. };
  1103. template <field F1, field F2, char D, bool Transpose, typename Result>
  1104. inline bool may_update(Result const& res)
  1105. {
  1106. return may_update_result_dispatch<F1, F2, D, Transpose>::apply(res);
  1107. }
  1108. // result_dimension
  1109. template <typename Geometry>
  1110. struct result_dimension
  1111. {
  1112. BOOST_STATIC_ASSERT(geometry::dimension<Geometry>::value >= 0);
  1113. static const char value
  1114. = ( geometry::dimension<Geometry>::value <= 9 ) ?
  1115. ( '0' + geometry::dimension<Geometry>::value ) :
  1116. 'T';
  1117. };
  1118. }} // namespace detail::relate
  1119. #endif // DOXYGEN_NO_DETAIL
  1120. }} // namespace boost::geometry
  1121. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_RESULT_HPP