result.hpp 32 KB

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