interval_concept.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. // Boost.Polygon library interval_concept.hpp header file
  2. // Copyright (c) Intel Corporation 2008.
  3. // Copyright (c) 2008-2012 Simonson Lucanus.
  4. // Copyright (c) 2012-2012 Andrii Sydorchuk.
  5. // See http://www.boost.org for updates, documentation, and revision history.
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_POLYGON_INTERVAL_CONCEPT_HPP
  10. #define BOOST_POLYGON_INTERVAL_CONCEPT_HPP
  11. #include "isotropy.hpp"
  12. #include "interval_traits.hpp"
  13. namespace boost {
  14. namespace polygon {
  15. struct interval_concept {};
  16. template <typename ConceptType>
  17. struct is_interval_concept {
  18. typedef gtl_no type;
  19. };
  20. template <>
  21. struct is_interval_concept<interval_concept> {
  22. typedef gtl_yes type;
  23. };
  24. template <typename ConceptType>
  25. struct is_mutable_interval_concept {
  26. typedef gtl_no type;
  27. };
  28. template <>
  29. struct is_mutable_interval_concept<interval_concept> {
  30. typedef gtl_yes type;
  31. };
  32. template <typename GeometryType, typename BoolType>
  33. struct interval_coordinate_type_by_concept {
  34. typedef void type;
  35. };
  36. template <typename GeometryType>
  37. struct interval_coordinate_type_by_concept<GeometryType, gtl_yes> {
  38. typedef typename interval_traits<GeometryType>::coordinate_type type;
  39. };
  40. template <typename GeometryType>
  41. struct interval_coordinate_type {
  42. typedef typename interval_coordinate_type_by_concept<
  43. GeometryType,
  44. typename is_interval_concept<
  45. typename geometry_concept<GeometryType>::type
  46. >::type
  47. >::type type;
  48. };
  49. template <typename GeometryType, typename BoolType>
  50. struct interval_difference_type_by_concept {
  51. typedef void type;
  52. };
  53. template <typename GeometryType>
  54. struct interval_difference_type_by_concept<GeometryType, gtl_yes> {
  55. typedef typename coordinate_traits<
  56. typename interval_traits<GeometryType>::coordinate_type
  57. >::coordinate_difference type;
  58. };
  59. template <typename GeometryType>
  60. struct interval_difference_type {
  61. typedef typename interval_difference_type_by_concept<
  62. GeometryType,
  63. typename is_interval_concept<
  64. typename geometry_concept<GeometryType>::type
  65. >::type
  66. >::type type;
  67. };
  68. struct y_i_get : gtl_yes {};
  69. template <typename IntervalType>
  70. typename enable_if<
  71. typename gtl_and<
  72. y_i_get,
  73. typename is_interval_concept<
  74. typename geometry_concept<IntervalType>::type
  75. >::type
  76. >::type,
  77. typename interval_coordinate_type<IntervalType>::type
  78. >::type get(const IntervalType& interval, direction_1d dir) {
  79. return interval_traits<IntervalType>::get(interval, dir);
  80. }
  81. struct y_i_set : gtl_yes {};
  82. template <typename IntervalType>
  83. typename enable_if<
  84. typename gtl_and<
  85. y_i_set,
  86. typename is_mutable_interval_concept<
  87. typename geometry_concept<IntervalType>::type
  88. >::type
  89. >::type,
  90. void
  91. >::type set(IntervalType& interval, direction_1d dir,
  92. typename interval_mutable_traits<IntervalType>::coordinate_type value) {
  93. interval_mutable_traits<IntervalType>::set(interval, dir, value);
  94. }
  95. struct y_i_construct : gtl_yes {};
  96. template <typename IntervalType>
  97. typename enable_if<
  98. typename gtl_and<
  99. y_i_construct,
  100. typename is_mutable_interval_concept<
  101. typename geometry_concept<IntervalType>::type
  102. >::type
  103. >::type,
  104. IntervalType
  105. >::type construct(
  106. typename interval_mutable_traits<IntervalType>::coordinate_type low,
  107. typename interval_mutable_traits<IntervalType>::coordinate_type high) {
  108. if (low > high) {
  109. (std::swap)(low, high);
  110. }
  111. return interval_mutable_traits<IntervalType>::construct(low, high);
  112. }
  113. struct y_i_copy_construct : gtl_yes {};
  114. template <typename IntervalType1, typename IntervalType2>
  115. typename enable_if<
  116. typename gtl_and_3<
  117. y_i_copy_construct,
  118. typename is_mutable_interval_concept<
  119. typename geometry_concept<IntervalType1>::type
  120. >::type,
  121. typename is_interval_concept<
  122. typename geometry_concept<IntervalType2>::type
  123. >::type
  124. >::type,
  125. IntervalType1
  126. >::type copy_construct(const IntervalType2& interval) {
  127. return construct<IntervalType1>(get(interval, LOW), get(interval, HIGH));
  128. }
  129. struct y_i_assign : gtl_yes {};
  130. template <typename IntervalType1, typename IntervalType2>
  131. typename enable_if<
  132. typename gtl_and_3<
  133. y_i_assign,
  134. typename is_mutable_interval_concept<
  135. typename geometry_concept<IntervalType1>::type
  136. >::type,
  137. typename is_interval_concept<
  138. typename geometry_concept<IntervalType2>::type
  139. >::type
  140. >::type,
  141. IntervalType1
  142. >::type& assign(IntervalType1& lvalue, const IntervalType2& rvalue) {
  143. set(lvalue, LOW, get(rvalue, LOW));
  144. set(lvalue, HIGH, get(rvalue, HIGH));
  145. return lvalue;
  146. }
  147. struct y_i_low : gtl_yes {};
  148. template <typename IntervalType>
  149. typename enable_if<
  150. typename gtl_and<
  151. y_i_low,
  152. typename is_interval_concept<
  153. typename geometry_concept<IntervalType>::type
  154. >::type
  155. >::type,
  156. typename interval_coordinate_type<IntervalType>::type
  157. >::type low(const IntervalType& interval) {
  158. return get(interval, LOW);
  159. }
  160. struct y_i_high : gtl_yes {};
  161. template <typename IntervalType>
  162. typename enable_if<
  163. typename gtl_and<
  164. y_i_high,
  165. typename is_interval_concept<
  166. typename geometry_concept<IntervalType>::type
  167. >::type
  168. >::type,
  169. typename interval_coordinate_type<IntervalType>::type
  170. >::type high(const IntervalType& interval) {
  171. return get(interval, HIGH);
  172. }
  173. struct y_i_low2 : gtl_yes {};
  174. template <typename IntervalType>
  175. typename enable_if<
  176. typename gtl_and<
  177. y_i_low2,
  178. typename is_mutable_interval_concept<
  179. typename geometry_concept<IntervalType>::type
  180. >::type
  181. >::type,
  182. void
  183. >::type low(IntervalType& interval,
  184. typename interval_mutable_traits<IntervalType>::coordinate_type value) {
  185. set(interval, LOW, value);
  186. }
  187. struct y_i_high2 : gtl_yes {};
  188. template <typename IntervalType>
  189. typename enable_if<
  190. typename gtl_and<
  191. y_i_high2,
  192. typename is_mutable_interval_concept<
  193. typename geometry_concept<IntervalType>::type
  194. >::type
  195. >::type,
  196. void
  197. >::type high(IntervalType& interval,
  198. typename interval_mutable_traits<IntervalType>::coordinate_type value) {
  199. set(interval, HIGH, value);
  200. }
  201. struct y_i_equivalence : gtl_yes {};
  202. template <typename IntervalType1, typename IntervalType2>
  203. typename enable_if<
  204. typename gtl_and_3<
  205. y_i_equivalence,
  206. typename is_interval_concept<
  207. typename geometry_concept<IntervalType1>::type
  208. >::type,
  209. typename is_interval_concept<
  210. typename geometry_concept<IntervalType2>::type
  211. >::type
  212. >::type,
  213. bool
  214. >::type equivalence(
  215. const IntervalType1& interval1,
  216. const IntervalType2& interval2) {
  217. return (get(interval1, LOW) == get(interval2, LOW)) &&
  218. (get(interval1, HIGH) == get(interval2, HIGH));
  219. }
  220. struct y_i_contains : gtl_yes {};
  221. template <typename IntervalType>
  222. typename enable_if<
  223. typename gtl_and<
  224. y_i_contains,
  225. typename is_interval_concept<
  226. typename geometry_concept<IntervalType>::type
  227. >::type
  228. >::type,
  229. bool
  230. >::type contains(
  231. const IntervalType& interval,
  232. typename interval_coordinate_type<IntervalType>::type value,
  233. bool consider_touch = true ) {
  234. if (consider_touch) {
  235. return value <= high(interval) && value >= low(interval);
  236. } else {
  237. return value < high(interval) && value > low(interval);
  238. }
  239. }
  240. struct y_i_contains2 : gtl_yes {};
  241. template <typename IntervalType1, typename IntervalType2>
  242. typename enable_if<
  243. typename gtl_and_3<
  244. y_i_contains2,
  245. typename is_interval_concept<
  246. typename geometry_concept<IntervalType1>::type
  247. >::type,
  248. typename is_interval_concept<
  249. typename geometry_concept<IntervalType2>::type
  250. >::type
  251. >::type,
  252. bool
  253. >::type contains(
  254. const IntervalType1& interval1,
  255. const IntervalType2& interval2,
  256. bool consider_touch = true) {
  257. return contains(interval1, get(interval2, LOW), consider_touch) &&
  258. contains(interval1, get(interval2, HIGH), consider_touch);
  259. }
  260. struct y_i_center : gtl_yes {};
  261. template <typename IntervalType>
  262. typename enable_if<
  263. typename gtl_and<
  264. y_i_center,
  265. typename is_interval_concept<
  266. typename geometry_concept<IntervalType>::type
  267. >::type
  268. >::type,
  269. typename interval_coordinate_type<IntervalType>::type
  270. >::type center(const IntervalType& interval) {
  271. return (high(interval) + low(interval)) / 2;
  272. }
  273. struct y_i_delta : gtl_yes {};
  274. template <typename IntervalType>
  275. typename enable_if<
  276. typename gtl_and<
  277. y_i_delta,
  278. typename is_interval_concept<
  279. typename geometry_concept<IntervalType>::type
  280. >::type
  281. >::type,
  282. typename interval_difference_type<IntervalType>::type
  283. >::type delta(const IntervalType& interval) {
  284. typedef typename interval_difference_type<IntervalType>::type diff_type;
  285. return static_cast<diff_type>(high(interval)) -
  286. static_cast<diff_type>(low(interval));
  287. }
  288. struct y_i_flip : gtl_yes {};
  289. template <typename IntervalType>
  290. typename enable_if<
  291. typename gtl_and<
  292. y_i_flip,
  293. typename is_mutable_interval_concept<
  294. typename geometry_concept<IntervalType>::type
  295. >::type
  296. >::type,
  297. IntervalType>::type& flip(
  298. IntervalType& interval,
  299. typename interval_coordinate_type<IntervalType>::type axis = 0) {
  300. typename interval_coordinate_type<IntervalType>::type newLow, newHigh;
  301. newLow = 2 * axis - high(interval);
  302. newHigh = 2 * axis - low(interval);
  303. low(interval, newLow);
  304. high(interval, newHigh);
  305. return interval;
  306. }
  307. struct y_i_scale_up : gtl_yes {};
  308. template <typename IntervalType>
  309. typename enable_if<
  310. typename gtl_and<
  311. y_i_scale_up,
  312. typename is_mutable_interval_concept<
  313. typename geometry_concept<IntervalType>::type
  314. >::type
  315. >::type,
  316. IntervalType
  317. >::type& scale_up(
  318. IntervalType& interval,
  319. typename interval_coordinate_type<IntervalType>::type factor) {
  320. typename interval_coordinate_type<IntervalType>::type newHigh =
  321. high(interval) * factor;
  322. low(interval, low(interval) * factor);
  323. high(interval, (newHigh));
  324. return interval;
  325. }
  326. struct y_i_scale_down : gtl_yes {};
  327. template <typename IntervalType>
  328. typename enable_if<
  329. typename gtl_and<
  330. y_i_scale_down,
  331. typename is_mutable_interval_concept<
  332. typename geometry_concept<IntervalType>::type
  333. >::type
  334. >::type,
  335. IntervalType
  336. >::type& scale_down(
  337. IntervalType& interval,
  338. typename interval_coordinate_type<IntervalType>::type factor) {
  339. typedef typename interval_coordinate_type<IntervalType>::type Unit;
  340. typename interval_coordinate_type<IntervalType>::type newHigh =
  341. high(interval) / factor;
  342. low(interval, low(interval) / factor);
  343. high(interval, (newHigh));
  344. return interval;
  345. }
  346. // TODO(asydorchuk): Deprecated.
  347. struct y_i_scale : gtl_yes {};
  348. template <typename IntervalType>
  349. typename enable_if<
  350. typename gtl_and<
  351. y_i_scale,
  352. typename is_mutable_interval_concept<
  353. typename geometry_concept<IntervalType>::type
  354. >::type
  355. >::type,
  356. IntervalType
  357. >::type& scale(IntervalType& interval, double factor) {
  358. typedef typename interval_coordinate_type<IntervalType>::type Unit;
  359. Unit newHigh = scaling_policy<Unit>::round(
  360. static_cast<double>(high(interval)) * factor);
  361. low(interval, scaling_policy<Unit>::round(
  362. static_cast<double>(low(interval)) * factor));
  363. high(interval, (newHigh));
  364. return interval;
  365. }
  366. struct y_i_move : gtl_yes {};
  367. template <typename IntervalType>
  368. typename enable_if<
  369. typename gtl_and<
  370. y_i_move,
  371. typename is_mutable_interval_concept<
  372. typename geometry_concept<IntervalType>::type
  373. >::type
  374. >::type,
  375. IntervalType
  376. >::type& move(
  377. IntervalType& interval,
  378. typename interval_difference_type<IntervalType>::type displacement) {
  379. typedef typename interval_coordinate_type<IntervalType>::type ctype;
  380. typedef typename coordinate_traits<ctype>::coordinate_difference Unit;
  381. low(interval, static_cast<ctype>(
  382. static_cast<Unit>(low(interval)) + displacement));
  383. high(interval, static_cast<ctype>(
  384. static_cast<Unit>(high(interval)) + displacement));
  385. return interval;
  386. }
  387. struct y_i_convolve : gtl_yes {};
  388. template <typename IntervalType>
  389. typename enable_if<
  390. typename gtl_and<
  391. y_i_convolve,
  392. typename is_mutable_interval_concept<
  393. typename geometry_concept<IntervalType>::type
  394. >::type
  395. >::type,
  396. IntervalType
  397. >::type& convolve(
  398. IntervalType& interval,
  399. typename interval_coordinate_type<IntervalType>::type value) {
  400. typedef typename interval_coordinate_type<IntervalType>::type Unit;
  401. Unit newLow = low(interval) + value;
  402. Unit newHigh = high(interval) + value;
  403. low(interval, newLow);
  404. high(interval, newHigh);
  405. return interval;
  406. }
  407. struct y_i_deconvolve : gtl_yes {};
  408. template <typename IntervalType>
  409. typename enable_if<
  410. typename gtl_and<
  411. y_i_deconvolve,
  412. typename is_mutable_interval_concept<
  413. typename geometry_concept<IntervalType>::type
  414. >::type
  415. >::type,
  416. IntervalType
  417. >::type& deconvolve(
  418. IntervalType& interval,
  419. typename interval_coordinate_type<IntervalType>::type value) {
  420. typedef typename interval_coordinate_type<IntervalType>::type Unit;
  421. Unit newLow = low(interval) - value;
  422. Unit newHigh = high(interval) - value;
  423. low(interval, newLow);
  424. high(interval, newHigh);
  425. return interval;
  426. }
  427. struct y_i_convolve2 : gtl_yes {};
  428. template <typename IntervalType1, typename IntervalType2>
  429. typename enable_if<
  430. typename gtl_and_3<
  431. y_i_convolve2,
  432. typename is_mutable_interval_concept<
  433. typename geometry_concept<IntervalType1>::type
  434. >::type,
  435. typename is_interval_concept<
  436. typename geometry_concept<IntervalType2>::type
  437. >::type
  438. >::type,
  439. IntervalType1
  440. >::type& convolve(IntervalType1& lvalue, const IntervalType2& rvalue) {
  441. typedef typename interval_coordinate_type<IntervalType1>::type Unit;
  442. Unit newLow = low(lvalue) + low(rvalue);
  443. Unit newHigh = high(lvalue) + high(rvalue);
  444. low(lvalue, newLow);
  445. high(lvalue, newHigh);
  446. return lvalue;
  447. }
  448. struct y_i_deconvolve2 : gtl_yes {};
  449. template <typename IntervalType1, typename IntervalType2>
  450. typename enable_if<
  451. typename gtl_and_3<
  452. y_i_deconvolve2,
  453. typename is_mutable_interval_concept<
  454. typename geometry_concept<IntervalType1>::type
  455. >::type,
  456. typename is_interval_concept<
  457. typename geometry_concept<IntervalType2>::type
  458. >::type
  459. >::type,
  460. IntervalType1
  461. >::type& deconvolve(IntervalType1& lvalue, const IntervalType2& rvalue) {
  462. typedef typename interval_coordinate_type<IntervalType1>::type Unit;
  463. Unit newLow = low(lvalue) - low(rvalue);
  464. Unit newHigh = high(lvalue) - high(rvalue);
  465. low(lvalue, newLow);
  466. high(lvalue, newHigh);
  467. return lvalue;
  468. }
  469. struct y_i_reconvolve : gtl_yes {};
  470. template <typename IntervalType1, typename IntervalType2>
  471. typename enable_if<
  472. typename gtl_and_3<
  473. y_i_reconvolve,
  474. typename is_mutable_interval_concept<
  475. typename geometry_concept<IntervalType1>::type
  476. >::type,
  477. typename is_interval_concept<
  478. typename geometry_concept<IntervalType2>::type
  479. >::type
  480. >::type,
  481. IntervalType1
  482. >::type& reflected_convolve(
  483. IntervalType1& lvalue,
  484. const IntervalType2& rvalue) {
  485. typedef typename interval_coordinate_type<IntervalType1>::type Unit;
  486. Unit newLow = low(lvalue) - high(rvalue);
  487. Unit newHigh = high(lvalue) - low(rvalue);
  488. low(lvalue, newLow);
  489. high(lvalue, newHigh);
  490. return lvalue;
  491. }
  492. struct y_i_redeconvolve : gtl_yes {};
  493. template <typename IntervalType1, typename IntervalType2>
  494. typename enable_if<
  495. typename gtl_and_3<
  496. y_i_redeconvolve,
  497. typename is_mutable_interval_concept<
  498. typename geometry_concept<IntervalType1>::type
  499. >::type,
  500. typename is_interval_concept<
  501. typename geometry_concept<IntervalType2>::type
  502. >::type
  503. >::type,
  504. IntervalType1
  505. >::type& reflected_deconvolve(
  506. IntervalType1& lvalue,
  507. const IntervalType2& rvalue) {
  508. typedef typename interval_coordinate_type<IntervalType1>::type Unit;
  509. Unit newLow = low(lvalue) + high(rvalue);
  510. Unit newHigh = high(lvalue) + low(rvalue);
  511. low(lvalue, newLow);
  512. high(lvalue, newHigh);
  513. return lvalue;
  514. }
  515. struct y_i_e_dist1 : gtl_yes {};
  516. template <typename IntervalType>
  517. typename enable_if<
  518. typename gtl_and<y_i_e_dist1,
  519. typename is_interval_concept<
  520. typename geometry_concept<IntervalType>::type
  521. >::type
  522. >::type,
  523. typename interval_difference_type<IntervalType>::type
  524. >::type euclidean_distance(
  525. const IntervalType& interval,
  526. typename interval_coordinate_type<IntervalType>::type position) {
  527. typedef typename interval_difference_type<IntervalType>::type Unit;
  528. Unit dist[3] = {
  529. 0,
  530. (Unit)low(interval) - (Unit)position,
  531. (Unit)position - (Unit)high(interval)
  532. };
  533. return dist[(dist[1] > 0) + ((dist[2] > 0) << 1)];
  534. }
  535. struct y_i_e_dist2 : gtl_yes {};
  536. template <typename IntervalType1, typename IntervalType2>
  537. typename enable_if<
  538. typename gtl_and_3<
  539. y_i_e_dist2,
  540. typename is_interval_concept<
  541. typename geometry_concept<IntervalType1>::type
  542. >::type,
  543. typename is_interval_concept<
  544. typename geometry_concept<IntervalType2>::type
  545. >::type
  546. >::type,
  547. typename interval_difference_type<IntervalType1>::type
  548. >::type euclidean_distance(
  549. const IntervalType1& interval1,
  550. const IntervalType2& interval2) {
  551. typedef typename interval_difference_type<IntervalType1>::type Unit;
  552. Unit dist[3] = {
  553. 0,
  554. (Unit)low(interval1) - (Unit)high(interval2),
  555. (Unit)low(interval2) - (Unit)high(interval1)
  556. };
  557. return dist[(dist[1] > 0) + ((dist[2] > 0) << 1)];
  558. }
  559. struct y_i_e_intersects : gtl_yes {};
  560. template <typename IntervalType1, typename IntervalType2>
  561. typename enable_if<
  562. typename gtl_and_3<
  563. y_i_e_intersects,
  564. typename is_interval_concept<
  565. typename geometry_concept<IntervalType1>::type
  566. >::type,
  567. typename is_interval_concept<
  568. typename geometry_concept<IntervalType2>::type
  569. >::type
  570. >::type,
  571. bool
  572. >::type intersects(
  573. const IntervalType1& interval1,
  574. const IntervalType2& interval2,
  575. bool consider_touch = true) {
  576. return consider_touch ?
  577. (low(interval1) <= high(interval2)) &&
  578. (high(interval1) >= low(interval2)) :
  579. (low(interval1) < high(interval2)) &&
  580. (high(interval1) > low(interval2));
  581. }
  582. struct y_i_e_bintersect : gtl_yes {};
  583. template <typename IntervalType1, typename IntervalType2>
  584. typename enable_if<
  585. typename gtl_and_3<
  586. y_i_e_bintersect,
  587. typename is_interval_concept<
  588. typename geometry_concept<IntervalType1>::type
  589. >::type,
  590. typename is_interval_concept<
  591. typename geometry_concept<IntervalType2>::type
  592. >::type
  593. >::type,
  594. bool
  595. >::type boundaries_intersect(
  596. const IntervalType1& interval1,
  597. const IntervalType2& interval2,
  598. bool consider_touch = true) {
  599. return (contains(interval1, low(interval2), consider_touch) ||
  600. contains(interval1, high(interval2), consider_touch)) &&
  601. (contains(interval2, low(interval1), consider_touch) ||
  602. contains(interval2, high(interval1), consider_touch));
  603. }
  604. struct y_i_intersect : gtl_yes {};
  605. template <typename IntervalType1, typename IntervalType2>
  606. typename enable_if<
  607. typename gtl_and_3<
  608. y_i_intersect,
  609. typename is_mutable_interval_concept<
  610. typename geometry_concept<IntervalType1>::type
  611. >::type,
  612. typename is_interval_concept<
  613. typename geometry_concept<IntervalType2>::type
  614. >::type
  615. >::type,
  616. bool
  617. >::type intersect(
  618. IntervalType1& lvalue,
  619. const IntervalType2& rvalue,
  620. bool consider_touch = true) {
  621. typedef typename interval_coordinate_type<IntervalType1>::type Unit;
  622. Unit lowVal = (std::max)(low(lvalue), low(rvalue));
  623. Unit highVal = (std::min)(high(lvalue), high(rvalue));
  624. bool valid = consider_touch ? lowVal <= highVal : lowVal < highVal;
  625. if (valid) {
  626. low(lvalue, lowVal);
  627. high(lvalue, highVal);
  628. }
  629. return valid;
  630. }
  631. struct y_i_g_intersect : gtl_yes {};
  632. // TODO(asydorchuk): Deprecated.
  633. template <typename IntervalType1, typename IntervalType2>
  634. typename enable_if<
  635. typename gtl_and_3<
  636. y_i_g_intersect,
  637. typename is_mutable_interval_concept<
  638. typename geometry_concept<IntervalType1>::type
  639. >::type,
  640. typename is_interval_concept<
  641. typename geometry_concept<IntervalType2>::type
  642. >::type
  643. >::type,
  644. IntervalType1
  645. >::type& generalized_intersect(
  646. IntervalType1& lvalue,
  647. const IntervalType2& rvalue) {
  648. typedef typename interval_coordinate_type<IntervalType1>::type Unit;
  649. Unit coords[4] = {low(lvalue), high(lvalue), low(rvalue), high(rvalue)};
  650. // TODO(asydorchuk): consider implementing faster sorting of small
  651. // fixed length range.
  652. polygon_sort(coords, coords+4);
  653. low(lvalue, coords[1]);
  654. high(lvalue, coords[2]);
  655. return lvalue;
  656. }
  657. struct y_i_abuts1 : gtl_yes {};
  658. template <typename IntervalType1, typename IntervalType2>
  659. typename enable_if<
  660. typename gtl_and_3<
  661. y_i_abuts1,
  662. typename is_interval_concept<
  663. typename geometry_concept<IntervalType1>::type
  664. >::type,
  665. typename is_interval_concept<
  666. typename geometry_concept<IntervalType2>::type
  667. >::type
  668. >::type,
  669. bool
  670. >::type abuts(
  671. const IntervalType1& interval1,
  672. const IntervalType2& interval2,
  673. direction_1d dir) {
  674. return dir.to_int() ? low(interval2) == high(interval1) :
  675. low(interval1) == high(interval2);
  676. }
  677. struct y_i_abuts2 : gtl_yes {};
  678. template <typename IntervalType1, typename IntervalType2>
  679. typename enable_if<
  680. typename gtl_and_3<
  681. y_i_abuts2,
  682. typename is_interval_concept<
  683. typename geometry_concept<IntervalType1>::type
  684. >::type,
  685. typename is_interval_concept<
  686. typename geometry_concept<IntervalType2>::type
  687. >::type
  688. >::type,
  689. bool
  690. >::type abuts(
  691. const IntervalType1& interval1,
  692. const IntervalType2& interval2) {
  693. return abuts(interval1, interval2, HIGH) ||
  694. abuts(interval1, interval2, LOW);
  695. }
  696. struct y_i_bloat : gtl_yes {};
  697. template <typename IntervalType>
  698. typename enable_if<
  699. typename gtl_and<
  700. y_i_bloat,
  701. typename is_mutable_interval_concept<
  702. typename geometry_concept<IntervalType>::type
  703. >::type
  704. >::type,
  705. IntervalType
  706. >::type& bloat(
  707. IntervalType& interval,
  708. typename interval_coordinate_type<IntervalType>::type bloating) {
  709. low(interval, low(interval) - bloating);
  710. high(interval, high(interval) + bloating);
  711. return interval;
  712. }
  713. struct y_i_bloat2 : gtl_yes {};
  714. template <typename IntervalType>
  715. typename enable_if<
  716. typename gtl_and<
  717. y_i_bloat2,
  718. typename is_mutable_interval_concept<
  719. typename geometry_concept<IntervalType>::type
  720. >::type
  721. >::type,
  722. IntervalType
  723. >::type& bloat(
  724. IntervalType& interval,
  725. direction_1d dir,
  726. typename interval_coordinate_type<IntervalType>::type bloating) {
  727. set(interval, dir, get(interval, dir) + dir.get_sign() * bloating);
  728. return interval;
  729. }
  730. struct y_i_shrink : gtl_yes {};
  731. template <typename IntervalType>
  732. typename enable_if<
  733. typename gtl_and<
  734. y_i_shrink,
  735. typename is_mutable_interval_concept<
  736. typename geometry_concept<IntervalType>::type
  737. >::type
  738. >::type,
  739. IntervalType
  740. >::type& shrink(
  741. IntervalType& interval,
  742. typename interval_coordinate_type<IntervalType>::type shrinking) {
  743. return bloat(interval, -shrinking);
  744. }
  745. struct y_i_shrink2 : gtl_yes {};
  746. template <typename IntervalType>
  747. typename enable_if<
  748. typename gtl_and<
  749. y_i_shrink2,
  750. typename is_mutable_interval_concept<
  751. typename geometry_concept<IntervalType>::type
  752. >::type
  753. >::type,
  754. IntervalType
  755. >::type& shrink(
  756. IntervalType& interval,
  757. direction_1d dir,
  758. typename interval_coordinate_type<IntervalType>::type shrinking) {
  759. return bloat(interval, dir, -shrinking);
  760. }
  761. struct y_i_encompass : gtl_yes {};
  762. template <typename IntervalType1, typename IntervalType2>
  763. typename enable_if<
  764. typename gtl_and_3<
  765. y_i_encompass,
  766. typename is_mutable_interval_concept<
  767. typename geometry_concept<IntervalType1>::type
  768. >::type,
  769. typename is_interval_concept<
  770. typename geometry_concept<IntervalType2>::type
  771. >::type
  772. >::type,
  773. bool
  774. >::type encompass(IntervalType1& interval1, const IntervalType2& interval2) {
  775. bool retval = !contains(interval1, interval2, true);
  776. low(interval1, (std::min)(low(interval1), low(interval2)));
  777. high(interval1, (std::max)(high(interval1), high(interval2)));
  778. return retval;
  779. }
  780. struct y_i_encompass2 : gtl_yes {};
  781. template <typename IntervalType>
  782. typename enable_if<
  783. typename gtl_and<
  784. y_i_encompass2,
  785. typename is_mutable_interval_concept<
  786. typename geometry_concept<IntervalType>::type
  787. >::type
  788. >::type,
  789. bool
  790. >::type encompass(
  791. IntervalType& interval,
  792. typename interval_coordinate_type<IntervalType>::type value) {
  793. bool retval = !contains(interval, value, true);
  794. low(interval, (std::min)(low(interval), value));
  795. high(interval, (std::max)(high(interval), value));
  796. return retval;
  797. }
  798. struct y_i_get_half : gtl_yes {};
  799. template <typename IntervalType>
  800. typename enable_if<
  801. typename gtl_and<
  802. y_i_get_half,
  803. typename is_mutable_interval_concept<
  804. typename geometry_concept<IntervalType>::type
  805. >::type
  806. >::type,
  807. IntervalType
  808. >::type get_half(const IntervalType& interval, direction_1d dir) {
  809. typedef typename interval_coordinate_type<IntervalType>::type Unit;
  810. Unit c = (get(interval, LOW) + get(interval, HIGH)) / 2;
  811. return construct<IntervalType>(
  812. (dir == LOW) ? get(interval, LOW) : c,
  813. (dir == LOW) ? c : get(interval, HIGH));
  814. }
  815. struct y_i_join_with : gtl_yes {};
  816. template <typename IntervalType1, typename IntervalType2>
  817. typename enable_if<
  818. typename gtl_and_3<
  819. y_i_join_with,
  820. typename is_mutable_interval_concept<
  821. typename geometry_concept<IntervalType1>::type
  822. >::type,
  823. typename is_interval_concept<
  824. typename geometry_concept<IntervalType2>::type
  825. >::type>::type,
  826. bool
  827. >::type join_with(IntervalType1& interval1, const IntervalType2& interval2) {
  828. if (abuts(interval1, interval2)) {
  829. encompass(interval1, interval2);
  830. return true;
  831. }
  832. return false;
  833. }
  834. } // polygon
  835. } // boost
  836. #endif // BOOST_POLYGON_INTERVAL_CONCEPT_HPP