interval_concept.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. typename interval_coordinate_type<IntervalType>::type newHigh =
  340. high(interval) / factor;
  341. low(interval, low(interval) / factor);
  342. high(interval, (newHigh));
  343. return interval;
  344. }
  345. // TODO(asydorchuk): Deprecated.
  346. struct y_i_scale : gtl_yes {};
  347. template <typename IntervalType>
  348. typename enable_if<
  349. typename gtl_and<
  350. y_i_scale,
  351. typename is_mutable_interval_concept<
  352. typename geometry_concept<IntervalType>::type
  353. >::type
  354. >::type,
  355. IntervalType
  356. >::type& scale(IntervalType& interval, double factor) {
  357. typedef typename interval_coordinate_type<IntervalType>::type Unit;
  358. Unit newHigh = scaling_policy<Unit>::round(
  359. static_cast<double>(high(interval)) * factor);
  360. low(interval, scaling_policy<Unit>::round(
  361. static_cast<double>(low(interval)) * factor));
  362. high(interval, (newHigh));
  363. return interval;
  364. }
  365. struct y_i_move : gtl_yes {};
  366. template <typename IntervalType>
  367. typename enable_if<
  368. typename gtl_and<
  369. y_i_move,
  370. typename is_mutable_interval_concept<
  371. typename geometry_concept<IntervalType>::type
  372. >::type
  373. >::type,
  374. IntervalType
  375. >::type& move(
  376. IntervalType& interval,
  377. typename interval_difference_type<IntervalType>::type displacement) {
  378. typedef typename interval_coordinate_type<IntervalType>::type ctype;
  379. typedef typename coordinate_traits<ctype>::coordinate_difference Unit;
  380. low(interval, static_cast<ctype>(
  381. static_cast<Unit>(low(interval)) + displacement));
  382. high(interval, static_cast<ctype>(
  383. static_cast<Unit>(high(interval)) + displacement));
  384. return interval;
  385. }
  386. struct y_i_convolve : gtl_yes {};
  387. template <typename IntervalType>
  388. typename enable_if<
  389. typename gtl_and<
  390. y_i_convolve,
  391. typename is_mutable_interval_concept<
  392. typename geometry_concept<IntervalType>::type
  393. >::type
  394. >::type,
  395. IntervalType
  396. >::type& convolve(
  397. IntervalType& interval,
  398. typename interval_coordinate_type<IntervalType>::type value) {
  399. typedef typename interval_coordinate_type<IntervalType>::type Unit;
  400. Unit newLow = low(interval) + value;
  401. Unit newHigh = high(interval) + value;
  402. low(interval, newLow);
  403. high(interval, newHigh);
  404. return interval;
  405. }
  406. struct y_i_deconvolve : gtl_yes {};
  407. template <typename IntervalType>
  408. typename enable_if<
  409. typename gtl_and<
  410. y_i_deconvolve,
  411. typename is_mutable_interval_concept<
  412. typename geometry_concept<IntervalType>::type
  413. >::type
  414. >::type,
  415. IntervalType
  416. >::type& deconvolve(
  417. IntervalType& interval,
  418. typename interval_coordinate_type<IntervalType>::type value) {
  419. typedef typename interval_coordinate_type<IntervalType>::type Unit;
  420. Unit newLow = low(interval) - value;
  421. Unit newHigh = high(interval) - value;
  422. low(interval, newLow);
  423. high(interval, newHigh);
  424. return interval;
  425. }
  426. struct y_i_convolve2 : gtl_yes {};
  427. template <typename IntervalType1, typename IntervalType2>
  428. typename enable_if<
  429. typename gtl_and_3<
  430. y_i_convolve2,
  431. typename is_mutable_interval_concept<
  432. typename geometry_concept<IntervalType1>::type
  433. >::type,
  434. typename is_interval_concept<
  435. typename geometry_concept<IntervalType2>::type
  436. >::type
  437. >::type,
  438. IntervalType1
  439. >::type& convolve(IntervalType1& lvalue, const IntervalType2& rvalue) {
  440. typedef typename interval_coordinate_type<IntervalType1>::type Unit;
  441. Unit newLow = low(lvalue) + low(rvalue);
  442. Unit newHigh = high(lvalue) + high(rvalue);
  443. low(lvalue, newLow);
  444. high(lvalue, newHigh);
  445. return lvalue;
  446. }
  447. struct y_i_deconvolve2 : gtl_yes {};
  448. template <typename IntervalType1, typename IntervalType2>
  449. typename enable_if<
  450. typename gtl_and_3<
  451. y_i_deconvolve2,
  452. typename is_mutable_interval_concept<
  453. typename geometry_concept<IntervalType1>::type
  454. >::type,
  455. typename is_interval_concept<
  456. typename geometry_concept<IntervalType2>::type
  457. >::type
  458. >::type,
  459. IntervalType1
  460. >::type& deconvolve(IntervalType1& lvalue, const IntervalType2& rvalue) {
  461. typedef typename interval_coordinate_type<IntervalType1>::type Unit;
  462. Unit newLow = low(lvalue) - low(rvalue);
  463. Unit newHigh = high(lvalue) - high(rvalue);
  464. low(lvalue, newLow);
  465. high(lvalue, newHigh);
  466. return lvalue;
  467. }
  468. struct y_i_reconvolve : gtl_yes {};
  469. template <typename IntervalType1, typename IntervalType2>
  470. typename enable_if<
  471. typename gtl_and_3<
  472. y_i_reconvolve,
  473. typename is_mutable_interval_concept<
  474. typename geometry_concept<IntervalType1>::type
  475. >::type,
  476. typename is_interval_concept<
  477. typename geometry_concept<IntervalType2>::type
  478. >::type
  479. >::type,
  480. IntervalType1
  481. >::type& reflected_convolve(
  482. IntervalType1& lvalue,
  483. const IntervalType2& rvalue) {
  484. typedef typename interval_coordinate_type<IntervalType1>::type Unit;
  485. Unit newLow = low(lvalue) - high(rvalue);
  486. Unit newHigh = high(lvalue) - low(rvalue);
  487. low(lvalue, newLow);
  488. high(lvalue, newHigh);
  489. return lvalue;
  490. }
  491. struct y_i_redeconvolve : gtl_yes {};
  492. template <typename IntervalType1, typename IntervalType2>
  493. typename enable_if<
  494. typename gtl_and_3<
  495. y_i_redeconvolve,
  496. typename is_mutable_interval_concept<
  497. typename geometry_concept<IntervalType1>::type
  498. >::type,
  499. typename is_interval_concept<
  500. typename geometry_concept<IntervalType2>::type
  501. >::type
  502. >::type,
  503. IntervalType1
  504. >::type& reflected_deconvolve(
  505. IntervalType1& lvalue,
  506. const IntervalType2& rvalue) {
  507. typedef typename interval_coordinate_type<IntervalType1>::type Unit;
  508. Unit newLow = low(lvalue) + high(rvalue);
  509. Unit newHigh = high(lvalue) + low(rvalue);
  510. low(lvalue, newLow);
  511. high(lvalue, newHigh);
  512. return lvalue;
  513. }
  514. struct y_i_e_dist1 : gtl_yes {};
  515. template <typename IntervalType>
  516. typename enable_if<
  517. typename gtl_and<y_i_e_dist1,
  518. typename is_interval_concept<
  519. typename geometry_concept<IntervalType>::type
  520. >::type
  521. >::type,
  522. typename interval_difference_type<IntervalType>::type
  523. >::type euclidean_distance(
  524. const IntervalType& interval,
  525. typename interval_coordinate_type<IntervalType>::type position) {
  526. typedef typename interval_difference_type<IntervalType>::type Unit;
  527. Unit dist[3] = {
  528. 0,
  529. (Unit)low(interval) - (Unit)position,
  530. (Unit)position - (Unit)high(interval)
  531. };
  532. return dist[(dist[1] > 0) + ((dist[2] > 0) << 1)];
  533. }
  534. struct y_i_e_dist2 : gtl_yes {};
  535. template <typename IntervalType1, typename IntervalType2>
  536. typename enable_if<
  537. typename gtl_and_3<
  538. y_i_e_dist2,
  539. typename is_interval_concept<
  540. typename geometry_concept<IntervalType1>::type
  541. >::type,
  542. typename is_interval_concept<
  543. typename geometry_concept<IntervalType2>::type
  544. >::type
  545. >::type,
  546. typename interval_difference_type<IntervalType1>::type
  547. >::type euclidean_distance(
  548. const IntervalType1& interval1,
  549. const IntervalType2& interval2) {
  550. typedef typename interval_difference_type<IntervalType1>::type Unit;
  551. Unit dist[3] = {
  552. 0,
  553. (Unit)low(interval1) - (Unit)high(interval2),
  554. (Unit)low(interval2) - (Unit)high(interval1)
  555. };
  556. return dist[(dist[1] > 0) + ((dist[2] > 0) << 1)];
  557. }
  558. struct y_i_e_intersects : gtl_yes {};
  559. template <typename IntervalType1, typename IntervalType2>
  560. typename enable_if<
  561. typename gtl_and_3<
  562. y_i_e_intersects,
  563. typename is_interval_concept<
  564. typename geometry_concept<IntervalType1>::type
  565. >::type,
  566. typename is_interval_concept<
  567. typename geometry_concept<IntervalType2>::type
  568. >::type
  569. >::type,
  570. bool
  571. >::type intersects(
  572. const IntervalType1& interval1,
  573. const IntervalType2& interval2,
  574. bool consider_touch = true) {
  575. return consider_touch ?
  576. (low(interval1) <= high(interval2)) &&
  577. (high(interval1) >= low(interval2)) :
  578. (low(interval1) < high(interval2)) &&
  579. (high(interval1) > low(interval2));
  580. }
  581. struct y_i_e_bintersect : gtl_yes {};
  582. template <typename IntervalType1, typename IntervalType2>
  583. typename enable_if<
  584. typename gtl_and_3<
  585. y_i_e_bintersect,
  586. typename is_interval_concept<
  587. typename geometry_concept<IntervalType1>::type
  588. >::type,
  589. typename is_interval_concept<
  590. typename geometry_concept<IntervalType2>::type
  591. >::type
  592. >::type,
  593. bool
  594. >::type boundaries_intersect(
  595. const IntervalType1& interval1,
  596. const IntervalType2& interval2,
  597. bool consider_touch = true) {
  598. return (contains(interval1, low(interval2), consider_touch) ||
  599. contains(interval1, high(interval2), consider_touch)) &&
  600. (contains(interval2, low(interval1), consider_touch) ||
  601. contains(interval2, high(interval1), consider_touch));
  602. }
  603. struct y_i_intersect : gtl_yes {};
  604. template <typename IntervalType1, typename IntervalType2>
  605. typename enable_if<
  606. typename gtl_and_3<
  607. y_i_intersect,
  608. typename is_mutable_interval_concept<
  609. typename geometry_concept<IntervalType1>::type
  610. >::type,
  611. typename is_interval_concept<
  612. typename geometry_concept<IntervalType2>::type
  613. >::type
  614. >::type,
  615. bool
  616. >::type intersect(
  617. IntervalType1& lvalue,
  618. const IntervalType2& rvalue,
  619. bool consider_touch = true) {
  620. typedef typename interval_coordinate_type<IntervalType1>::type Unit;
  621. Unit lowVal = (std::max)(low(lvalue), low(rvalue));
  622. Unit highVal = (std::min)(high(lvalue), high(rvalue));
  623. bool valid = consider_touch ? lowVal <= highVal : lowVal < highVal;
  624. if (valid) {
  625. low(lvalue, lowVal);
  626. high(lvalue, highVal);
  627. }
  628. return valid;
  629. }
  630. struct y_i_g_intersect : gtl_yes {};
  631. // TODO(asydorchuk): Deprecated.
  632. template <typename IntervalType1, typename IntervalType2>
  633. typename enable_if<
  634. typename gtl_and_3<
  635. y_i_g_intersect,
  636. typename is_mutable_interval_concept<
  637. typename geometry_concept<IntervalType1>::type
  638. >::type,
  639. typename is_interval_concept<
  640. typename geometry_concept<IntervalType2>::type
  641. >::type
  642. >::type,
  643. IntervalType1
  644. >::type& generalized_intersect(
  645. IntervalType1& lvalue,
  646. const IntervalType2& rvalue) {
  647. typedef typename interval_coordinate_type<IntervalType1>::type Unit;
  648. Unit coords[4] = {low(lvalue), high(lvalue), low(rvalue), high(rvalue)};
  649. // TODO(asydorchuk): consider implementing faster sorting of small
  650. // fixed length range.
  651. polygon_sort(coords, coords+4);
  652. low(lvalue, coords[1]);
  653. high(lvalue, coords[2]);
  654. return lvalue;
  655. }
  656. struct y_i_abuts1 : gtl_yes {};
  657. template <typename IntervalType1, typename IntervalType2>
  658. typename enable_if<
  659. typename gtl_and_3<
  660. y_i_abuts1,
  661. typename is_interval_concept<
  662. typename geometry_concept<IntervalType1>::type
  663. >::type,
  664. typename is_interval_concept<
  665. typename geometry_concept<IntervalType2>::type
  666. >::type
  667. >::type,
  668. bool
  669. >::type abuts(
  670. const IntervalType1& interval1,
  671. const IntervalType2& interval2,
  672. direction_1d dir) {
  673. return dir.to_int() ? low(interval2) == high(interval1) :
  674. low(interval1) == high(interval2);
  675. }
  676. struct y_i_abuts2 : gtl_yes {};
  677. template <typename IntervalType1, typename IntervalType2>
  678. typename enable_if<
  679. typename gtl_and_3<
  680. y_i_abuts2,
  681. typename is_interval_concept<
  682. typename geometry_concept<IntervalType1>::type
  683. >::type,
  684. typename is_interval_concept<
  685. typename geometry_concept<IntervalType2>::type
  686. >::type
  687. >::type,
  688. bool
  689. >::type abuts(
  690. const IntervalType1& interval1,
  691. const IntervalType2& interval2) {
  692. return abuts(interval1, interval2, HIGH) ||
  693. abuts(interval1, interval2, LOW);
  694. }
  695. struct y_i_bloat : gtl_yes {};
  696. template <typename IntervalType>
  697. typename enable_if<
  698. typename gtl_and<
  699. y_i_bloat,
  700. typename is_mutable_interval_concept<
  701. typename geometry_concept<IntervalType>::type
  702. >::type
  703. >::type,
  704. IntervalType
  705. >::type& bloat(
  706. IntervalType& interval,
  707. typename interval_coordinate_type<IntervalType>::type bloating) {
  708. low(interval, low(interval) - bloating);
  709. high(interval, high(interval) + bloating);
  710. return interval;
  711. }
  712. struct y_i_bloat2 : gtl_yes {};
  713. template <typename IntervalType>
  714. typename enable_if<
  715. typename gtl_and<
  716. y_i_bloat2,
  717. typename is_mutable_interval_concept<
  718. typename geometry_concept<IntervalType>::type
  719. >::type
  720. >::type,
  721. IntervalType
  722. >::type& bloat(
  723. IntervalType& interval,
  724. direction_1d dir,
  725. typename interval_coordinate_type<IntervalType>::type bloating) {
  726. set(interval, dir, get(interval, dir) + dir.get_sign() * bloating);
  727. return interval;
  728. }
  729. struct y_i_shrink : gtl_yes {};
  730. template <typename IntervalType>
  731. typename enable_if<
  732. typename gtl_and<
  733. y_i_shrink,
  734. typename is_mutable_interval_concept<
  735. typename geometry_concept<IntervalType>::type
  736. >::type
  737. >::type,
  738. IntervalType
  739. >::type& shrink(
  740. IntervalType& interval,
  741. typename interval_coordinate_type<IntervalType>::type shrinking) {
  742. return bloat(interval, -shrinking);
  743. }
  744. struct y_i_shrink2 : gtl_yes {};
  745. template <typename IntervalType>
  746. typename enable_if<
  747. typename gtl_and<
  748. y_i_shrink2,
  749. typename is_mutable_interval_concept<
  750. typename geometry_concept<IntervalType>::type
  751. >::type
  752. >::type,
  753. IntervalType
  754. >::type& shrink(
  755. IntervalType& interval,
  756. direction_1d dir,
  757. typename interval_coordinate_type<IntervalType>::type shrinking) {
  758. return bloat(interval, dir, -shrinking);
  759. }
  760. struct y_i_encompass : gtl_yes {};
  761. template <typename IntervalType1, typename IntervalType2>
  762. typename enable_if<
  763. typename gtl_and_3<
  764. y_i_encompass,
  765. typename is_mutable_interval_concept<
  766. typename geometry_concept<IntervalType1>::type
  767. >::type,
  768. typename is_interval_concept<
  769. typename geometry_concept<IntervalType2>::type
  770. >::type
  771. >::type,
  772. bool
  773. >::type encompass(IntervalType1& interval1, const IntervalType2& interval2) {
  774. bool retval = !contains(interval1, interval2, true);
  775. low(interval1, (std::min)(low(interval1), low(interval2)));
  776. high(interval1, (std::max)(high(interval1), high(interval2)));
  777. return retval;
  778. }
  779. struct y_i_encompass2 : gtl_yes {};
  780. template <typename IntervalType>
  781. typename enable_if<
  782. typename gtl_and<
  783. y_i_encompass2,
  784. typename is_mutable_interval_concept<
  785. typename geometry_concept<IntervalType>::type
  786. >::type
  787. >::type,
  788. bool
  789. >::type encompass(
  790. IntervalType& interval,
  791. typename interval_coordinate_type<IntervalType>::type value) {
  792. bool retval = !contains(interval, value, true);
  793. low(interval, (std::min)(low(interval), value));
  794. high(interval, (std::max)(high(interval), value));
  795. return retval;
  796. }
  797. struct y_i_get_half : gtl_yes {};
  798. template <typename IntervalType>
  799. typename enable_if<
  800. typename gtl_and<
  801. y_i_get_half,
  802. typename is_mutable_interval_concept<
  803. typename geometry_concept<IntervalType>::type
  804. >::type
  805. >::type,
  806. IntervalType
  807. >::type get_half(const IntervalType& interval, direction_1d dir) {
  808. typedef typename interval_coordinate_type<IntervalType>::type Unit;
  809. Unit c = (get(interval, LOW) + get(interval, HIGH)) / 2;
  810. return construct<IntervalType>(
  811. (dir == LOW) ? get(interval, LOW) : c,
  812. (dir == LOW) ? c : get(interval, HIGH));
  813. }
  814. struct y_i_join_with : gtl_yes {};
  815. template <typename IntervalType1, typename IntervalType2>
  816. typename enable_if<
  817. typename gtl_and_3<
  818. y_i_join_with,
  819. typename is_mutable_interval_concept<
  820. typename geometry_concept<IntervalType1>::type
  821. >::type,
  822. typename is_interval_concept<
  823. typename geometry_concept<IntervalType2>::type
  824. >::type>::type,
  825. bool
  826. >::type join_with(IntervalType1& interval1, const IntervalType2& interval2) {
  827. if (abuts(interval1, interval2)) {
  828. encompass(interval1, interval2);
  829. return true;
  830. }
  831. return false;
  832. }
  833. } // polygon
  834. } // boost
  835. #endif // BOOST_POLYGON_INTERVAL_CONCEPT_HPP