result.hpp 35 KB

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