topology.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. // Copyright 2009 The Trustees of Indiana University.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Jeremiah Willcock
  6. // Douglas Gregor
  7. // Andrew Lumsdaine
  8. #ifndef BOOST_GRAPH_TOPOLOGY_HPP
  9. #define BOOST_GRAPH_TOPOLOGY_HPP
  10. #include <boost/algorithm/minmax.hpp>
  11. #include <boost/config.hpp> // For BOOST_STATIC_CONSTANT
  12. #include <boost/config/no_tr1/cmath.hpp>
  13. #include <boost/math/constants/constants.hpp> // For root_two
  14. #include <boost/math/special_functions/hypot.hpp>
  15. #include <boost/random/uniform_01.hpp>
  16. #include <boost/random/linear_congruential.hpp>
  17. #include <boost/shared_ptr.hpp>
  18. #include <cmath>
  19. // Classes and concepts to represent points in a space, with distance and move
  20. // operations (used for Gurson-Atun layout), plus other things like bounding
  21. // boxes used for other layout algorithms.
  22. namespace boost
  23. {
  24. /***********************************************************
  25. * Topologies *
  26. ***********************************************************/
  27. template < std::size_t Dims > class convex_topology
  28. {
  29. public: // For VisualAge C++
  30. struct point
  31. {
  32. BOOST_STATIC_CONSTANT(std::size_t, dimensions = Dims);
  33. point() {}
  34. double& operator[](std::size_t i) { return values[i]; }
  35. const double& operator[](std::size_t i) const { return values[i]; }
  36. private:
  37. double values[Dims];
  38. };
  39. public: // For VisualAge C++
  40. struct point_difference
  41. {
  42. BOOST_STATIC_CONSTANT(std::size_t, dimensions = Dims);
  43. point_difference()
  44. {
  45. for (std::size_t i = 0; i < Dims; ++i)
  46. values[i] = 0.;
  47. }
  48. double& operator[](std::size_t i) { return values[i]; }
  49. const double& operator[](std::size_t i) const { return values[i]; }
  50. friend point_difference operator+(
  51. const point_difference& a, const point_difference& b)
  52. {
  53. point_difference result;
  54. for (std::size_t i = 0; i < Dims; ++i)
  55. result[i] = a[i] + b[i];
  56. return result;
  57. }
  58. friend point_difference& operator+=(
  59. point_difference& a, const point_difference& b)
  60. {
  61. for (std::size_t i = 0; i < Dims; ++i)
  62. a[i] += b[i];
  63. return a;
  64. }
  65. friend point_difference operator-(const point_difference& a)
  66. {
  67. point_difference result;
  68. for (std::size_t i = 0; i < Dims; ++i)
  69. result[i] = -a[i];
  70. return result;
  71. }
  72. friend point_difference operator-(
  73. const point_difference& a, const point_difference& b)
  74. {
  75. point_difference result;
  76. for (std::size_t i = 0; i < Dims; ++i)
  77. result[i] = a[i] - b[i];
  78. return result;
  79. }
  80. friend point_difference& operator-=(
  81. point_difference& a, const point_difference& b)
  82. {
  83. for (std::size_t i = 0; i < Dims; ++i)
  84. a[i] -= b[i];
  85. return a;
  86. }
  87. friend point_difference operator*(
  88. const point_difference& a, const point_difference& b)
  89. {
  90. point_difference result;
  91. for (std::size_t i = 0; i < Dims; ++i)
  92. result[i] = a[i] * b[i];
  93. return result;
  94. }
  95. friend point_difference operator*(const point_difference& a, double b)
  96. {
  97. point_difference result;
  98. for (std::size_t i = 0; i < Dims; ++i)
  99. result[i] = a[i] * b;
  100. return result;
  101. }
  102. friend point_difference operator*(double a, const point_difference& b)
  103. {
  104. point_difference result;
  105. for (std::size_t i = 0; i < Dims; ++i)
  106. result[i] = a * b[i];
  107. return result;
  108. }
  109. friend point_difference operator/(
  110. const point_difference& a, const point_difference& b)
  111. {
  112. point_difference result;
  113. for (std::size_t i = 0; i < Dims; ++i)
  114. result[i] = (b[i] == 0.) ? 0. : a[i] / b[i];
  115. return result;
  116. }
  117. friend double dot(const point_difference& a, const point_difference& b)
  118. {
  119. double result = 0;
  120. for (std::size_t i = 0; i < Dims; ++i)
  121. result += a[i] * b[i];
  122. return result;
  123. }
  124. private:
  125. double values[Dims];
  126. };
  127. public:
  128. typedef point point_type;
  129. typedef point_difference point_difference_type;
  130. double distance(point a, point b) const
  131. {
  132. double dist = 0.;
  133. for (std::size_t i = 0; i < Dims; ++i)
  134. {
  135. double diff = b[i] - a[i];
  136. dist = boost::math::hypot(dist, diff);
  137. }
  138. // Exact properties of the distance are not important, as long as
  139. // < on what this returns matches real distances; l_2 is used because
  140. // Fruchterman-Reingold also uses this code and it relies on l_2.
  141. return dist;
  142. }
  143. point move_position_toward(point a, double fraction, point b) const
  144. {
  145. point result;
  146. for (std::size_t i = 0; i < Dims; ++i)
  147. result[i] = a[i] + (b[i] - a[i]) * fraction;
  148. return result;
  149. }
  150. point_difference difference(point a, point b) const
  151. {
  152. point_difference result;
  153. for (std::size_t i = 0; i < Dims; ++i)
  154. result[i] = a[i] - b[i];
  155. return result;
  156. }
  157. point adjust(point a, point_difference delta) const
  158. {
  159. point result;
  160. for (std::size_t i = 0; i < Dims; ++i)
  161. result[i] = a[i] + delta[i];
  162. return result;
  163. }
  164. point pointwise_min(point a, point b) const
  165. {
  166. BOOST_USING_STD_MIN();
  167. point result;
  168. for (std::size_t i = 0; i < Dims; ++i)
  169. result[i] = min BOOST_PREVENT_MACRO_SUBSTITUTION(a[i], b[i]);
  170. return result;
  171. }
  172. point pointwise_max(point a, point b) const
  173. {
  174. BOOST_USING_STD_MAX();
  175. point result;
  176. for (std::size_t i = 0; i < Dims; ++i)
  177. result[i] = max BOOST_PREVENT_MACRO_SUBSTITUTION(a[i], b[i]);
  178. return result;
  179. }
  180. double norm(point_difference delta) const
  181. {
  182. double n = 0.;
  183. for (std::size_t i = 0; i < Dims; ++i)
  184. n = boost::math::hypot(n, delta[i]);
  185. return n;
  186. }
  187. double volume(point_difference delta) const
  188. {
  189. double n = 1.;
  190. for (std::size_t i = 0; i < Dims; ++i)
  191. n *= delta[i];
  192. return n;
  193. }
  194. };
  195. template < std::size_t Dims, typename RandomNumberGenerator = minstd_rand >
  196. class hypercube_topology : public convex_topology< Dims >
  197. {
  198. typedef uniform_01< RandomNumberGenerator, double > rand_t;
  199. public:
  200. typedef typename convex_topology< Dims >::point_type point_type;
  201. typedef typename convex_topology< Dims >::point_difference_type
  202. point_difference_type;
  203. explicit hypercube_topology(double scaling = 1.0)
  204. : gen_ptr(new RandomNumberGenerator)
  205. , rand(new rand_t(*gen_ptr))
  206. , scaling(scaling)
  207. {
  208. }
  209. hypercube_topology(RandomNumberGenerator& gen, double scaling = 1.0)
  210. : gen_ptr(), rand(new rand_t(gen)), scaling(scaling)
  211. {
  212. }
  213. point_type random_point() const
  214. {
  215. point_type p;
  216. for (std::size_t i = 0; i < Dims; ++i)
  217. p[i] = (*rand)() * scaling;
  218. return p;
  219. }
  220. point_type bound(point_type a) const
  221. {
  222. BOOST_USING_STD_MIN();
  223. BOOST_USING_STD_MAX();
  224. point_type p;
  225. for (std::size_t i = 0; i < Dims; ++i)
  226. p[i] = min BOOST_PREVENT_MACRO_SUBSTITUTION(
  227. scaling, max BOOST_PREVENT_MACRO_SUBSTITUTION(-scaling, a[i]));
  228. return p;
  229. }
  230. double distance_from_boundary(point_type a) const
  231. {
  232. BOOST_USING_STD_MIN();
  233. BOOST_USING_STD_MAX();
  234. #ifndef BOOST_NO_STDC_NAMESPACE
  235. using std::abs;
  236. #endif
  237. BOOST_STATIC_ASSERT(Dims >= 1);
  238. double dist = abs(scaling - a[0]);
  239. for (std::size_t i = 1; i < Dims; ++i)
  240. dist = min BOOST_PREVENT_MACRO_SUBSTITUTION(
  241. dist, abs(scaling - a[i]));
  242. return dist;
  243. }
  244. point_type center() const
  245. {
  246. point_type result;
  247. for (std::size_t i = 0; i < Dims; ++i)
  248. result[i] = scaling * .5;
  249. return result;
  250. }
  251. point_type origin() const
  252. {
  253. point_type result;
  254. for (std::size_t i = 0; i < Dims; ++i)
  255. result[i] = 0;
  256. return result;
  257. }
  258. point_difference_type extent() const
  259. {
  260. point_difference_type result;
  261. for (std::size_t i = 0; i < Dims; ++i)
  262. result[i] = scaling;
  263. return result;
  264. }
  265. private:
  266. shared_ptr< RandomNumberGenerator > gen_ptr;
  267. shared_ptr< rand_t > rand;
  268. double scaling;
  269. };
  270. template < typename RandomNumberGenerator = minstd_rand >
  271. class square_topology : public hypercube_topology< 2, RandomNumberGenerator >
  272. {
  273. typedef hypercube_topology< 2, RandomNumberGenerator > inherited;
  274. public:
  275. explicit square_topology(double scaling = 1.0) : inherited(scaling) {}
  276. square_topology(RandomNumberGenerator& gen, double scaling = 1.0)
  277. : inherited(gen, scaling)
  278. {
  279. }
  280. };
  281. template < typename RandomNumberGenerator = minstd_rand >
  282. class rectangle_topology : public convex_topology< 2 >
  283. {
  284. typedef uniform_01< RandomNumberGenerator, double > rand_t;
  285. public:
  286. rectangle_topology(double left, double top, double right, double bottom)
  287. : gen_ptr(new RandomNumberGenerator)
  288. , rand(new rand_t(*gen_ptr))
  289. , left(std::min BOOST_PREVENT_MACRO_SUBSTITUTION(left, right))
  290. , top(std::min BOOST_PREVENT_MACRO_SUBSTITUTION(top, bottom))
  291. , right(std::max BOOST_PREVENT_MACRO_SUBSTITUTION(left, right))
  292. , bottom(std::max BOOST_PREVENT_MACRO_SUBSTITUTION(top, bottom))
  293. {
  294. }
  295. rectangle_topology(RandomNumberGenerator& gen, double left, double top,
  296. double right, double bottom)
  297. : gen_ptr()
  298. , rand(new rand_t(gen))
  299. , left(std::min BOOST_PREVENT_MACRO_SUBSTITUTION(left, right))
  300. , top(std::min BOOST_PREVENT_MACRO_SUBSTITUTION(top, bottom))
  301. , right(std::max BOOST_PREVENT_MACRO_SUBSTITUTION(left, right))
  302. , bottom(std::max BOOST_PREVENT_MACRO_SUBSTITUTION(top, bottom))
  303. {
  304. }
  305. typedef typename convex_topology< 2 >::point_type point_type;
  306. typedef typename convex_topology< 2 >::point_difference_type
  307. point_difference_type;
  308. point_type random_point() const
  309. {
  310. point_type p;
  311. p[0] = (*rand)() * (right - left) + left;
  312. p[1] = (*rand)() * (bottom - top) + top;
  313. return p;
  314. }
  315. point_type bound(point_type a) const
  316. {
  317. BOOST_USING_STD_MIN();
  318. BOOST_USING_STD_MAX();
  319. point_type p;
  320. p[0] = min BOOST_PREVENT_MACRO_SUBSTITUTION(
  321. right, max BOOST_PREVENT_MACRO_SUBSTITUTION(left, a[0]));
  322. p[1] = min BOOST_PREVENT_MACRO_SUBSTITUTION(
  323. bottom, max BOOST_PREVENT_MACRO_SUBSTITUTION(top, a[1]));
  324. return p;
  325. }
  326. double distance_from_boundary(point_type a) const
  327. {
  328. BOOST_USING_STD_MIN();
  329. BOOST_USING_STD_MAX();
  330. #ifndef BOOST_NO_STDC_NAMESPACE
  331. using std::abs;
  332. #endif
  333. double dist = abs(left - a[0]);
  334. dist = min BOOST_PREVENT_MACRO_SUBSTITUTION(dist, abs(right - a[0]));
  335. dist = min BOOST_PREVENT_MACRO_SUBSTITUTION(dist, abs(top - a[1]));
  336. dist = min BOOST_PREVENT_MACRO_SUBSTITUTION(dist, abs(bottom - a[1]));
  337. return dist;
  338. }
  339. point_type center() const
  340. {
  341. point_type result;
  342. result[0] = (left + right) / 2.;
  343. result[1] = (top + bottom) / 2.;
  344. return result;
  345. }
  346. point_type origin() const
  347. {
  348. point_type result;
  349. result[0] = left;
  350. result[1] = top;
  351. return result;
  352. }
  353. point_difference_type extent() const
  354. {
  355. point_difference_type result;
  356. result[0] = right - left;
  357. result[1] = bottom - top;
  358. return result;
  359. }
  360. private:
  361. shared_ptr< RandomNumberGenerator > gen_ptr;
  362. shared_ptr< rand_t > rand;
  363. double left, top, right, bottom;
  364. };
  365. template < typename RandomNumberGenerator = minstd_rand >
  366. class cube_topology : public hypercube_topology< 3, RandomNumberGenerator >
  367. {
  368. typedef hypercube_topology< 3, RandomNumberGenerator > inherited;
  369. public:
  370. explicit cube_topology(double scaling = 1.0) : inherited(scaling) {}
  371. cube_topology(RandomNumberGenerator& gen, double scaling = 1.0)
  372. : inherited(gen, scaling)
  373. {
  374. }
  375. };
  376. template < std::size_t Dims, typename RandomNumberGenerator = minstd_rand >
  377. class ball_topology : public convex_topology< Dims >
  378. {
  379. typedef uniform_01< RandomNumberGenerator, double > rand_t;
  380. public:
  381. typedef typename convex_topology< Dims >::point_type point_type;
  382. typedef typename convex_topology< Dims >::point_difference_type
  383. point_difference_type;
  384. explicit ball_topology(double radius = 1.0)
  385. : gen_ptr(new RandomNumberGenerator)
  386. , rand(new rand_t(*gen_ptr))
  387. , radius(radius)
  388. {
  389. }
  390. ball_topology(RandomNumberGenerator& gen, double radius = 1.0)
  391. : gen_ptr(), rand(new rand_t(gen)), radius(radius)
  392. {
  393. }
  394. point_type random_point() const
  395. {
  396. point_type p;
  397. double dist_sum;
  398. do
  399. {
  400. dist_sum = 0.0;
  401. for (std::size_t i = 0; i < Dims; ++i)
  402. {
  403. double x = (*rand)() * 2 * radius - radius;
  404. p[i] = x;
  405. dist_sum += x * x;
  406. }
  407. } while (dist_sum > radius * radius);
  408. return p;
  409. }
  410. point_type bound(point_type a) const
  411. {
  412. BOOST_USING_STD_MIN();
  413. BOOST_USING_STD_MAX();
  414. double r = 0.;
  415. for (std::size_t i = 0; i < Dims; ++i)
  416. r = boost::math::hypot(r, a[i]);
  417. if (r <= radius)
  418. return a;
  419. double scaling_factor = radius / r;
  420. point_type p;
  421. for (std::size_t i = 0; i < Dims; ++i)
  422. p[i] = a[i] * scaling_factor;
  423. return p;
  424. }
  425. double distance_from_boundary(point_type a) const
  426. {
  427. double r = 0.;
  428. for (std::size_t i = 0; i < Dims; ++i)
  429. r = boost::math::hypot(r, a[i]);
  430. return radius - r;
  431. }
  432. point_type center() const
  433. {
  434. point_type result;
  435. for (std::size_t i = 0; i < Dims; ++i)
  436. result[i] = 0;
  437. return result;
  438. }
  439. point_type origin() const
  440. {
  441. point_type result;
  442. for (std::size_t i = 0; i < Dims; ++i)
  443. result[i] = -radius;
  444. return result;
  445. }
  446. point_difference_type extent() const
  447. {
  448. point_difference_type result;
  449. for (std::size_t i = 0; i < Dims; ++i)
  450. result[i] = 2. * radius;
  451. return result;
  452. }
  453. private:
  454. shared_ptr< RandomNumberGenerator > gen_ptr;
  455. shared_ptr< rand_t > rand;
  456. double radius;
  457. };
  458. template < typename RandomNumberGenerator = minstd_rand >
  459. class circle_topology : public ball_topology< 2, RandomNumberGenerator >
  460. {
  461. typedef ball_topology< 2, RandomNumberGenerator > inherited;
  462. public:
  463. explicit circle_topology(double radius = 1.0) : inherited(radius) {}
  464. circle_topology(RandomNumberGenerator& gen, double radius = 1.0)
  465. : inherited(gen, radius)
  466. {
  467. }
  468. };
  469. template < typename RandomNumberGenerator = minstd_rand >
  470. class sphere_topology : public ball_topology< 3, RandomNumberGenerator >
  471. {
  472. typedef ball_topology< 3, RandomNumberGenerator > inherited;
  473. public:
  474. explicit sphere_topology(double radius = 1.0) : inherited(radius) {}
  475. sphere_topology(RandomNumberGenerator& gen, double radius = 1.0)
  476. : inherited(gen, radius)
  477. {
  478. }
  479. };
  480. template < typename RandomNumberGenerator = minstd_rand > class heart_topology
  481. {
  482. // Heart is defined as the union of three shapes:
  483. // Square w/ corners (+-1000, -1000), (0, 0), (0, -2000)
  484. // Circle centered at (-500, -500) radius 500*sqrt(2)
  485. // Circle centered at (500, -500) radius 500*sqrt(2)
  486. // Bounding box (-1000, -2000) - (1000, 500*(sqrt(2) - 1))
  487. struct point
  488. {
  489. point()
  490. {
  491. values[0] = 0.0;
  492. values[1] = 0.0;
  493. }
  494. point(double x, double y)
  495. {
  496. values[0] = x;
  497. values[1] = y;
  498. }
  499. double& operator[](std::size_t i) { return values[i]; }
  500. double operator[](std::size_t i) const { return values[i]; }
  501. private:
  502. double values[2];
  503. };
  504. bool in_heart(point p) const
  505. {
  506. #ifndef BOOST_NO_STDC_NAMESPACE
  507. using std::abs;
  508. #endif
  509. if (p[1] < abs(p[0]) - 2000)
  510. return false; // Bottom
  511. if (p[1] <= -1000)
  512. return true; // Diagonal of square
  513. if (boost::math::hypot(p[0] - -500, p[1] - -500)
  514. <= 500. * boost::math::constants::root_two< double >())
  515. return true; // Left circle
  516. if (boost::math::hypot(p[0] - 500, p[1] - -500)
  517. <= 500. * boost::math::constants::root_two< double >())
  518. return true; // Right circle
  519. return false;
  520. }
  521. bool segment_within_heart(point p1, point p2) const
  522. {
  523. // Assumes that p1 and p2 are within the heart
  524. if ((p1[0] < 0) == (p2[0] < 0))
  525. return true; // Same side of symmetry line
  526. if (p1[0] == p2[0])
  527. return true; // Vertical
  528. double slope = (p2[1] - p1[1]) / (p2[0] - p1[0]);
  529. double intercept = p1[1] - p1[0] * slope;
  530. if (intercept > 0)
  531. return false; // Crosses between circles
  532. return true;
  533. }
  534. typedef uniform_01< RandomNumberGenerator, double > rand_t;
  535. public:
  536. typedef point point_type;
  537. heart_topology()
  538. : gen_ptr(new RandomNumberGenerator), rand(new rand_t(*gen_ptr))
  539. {
  540. }
  541. heart_topology(RandomNumberGenerator& gen)
  542. : gen_ptr(), rand(new rand_t(gen))
  543. {
  544. }
  545. point random_point() const
  546. {
  547. point result;
  548. do
  549. {
  550. result[0] = (*rand)()
  551. * (1000
  552. + 1000 * boost::math::constants::root_two< double >())
  553. - (500 + 500 * boost::math::constants::root_two< double >());
  554. result[1] = (*rand)()
  555. * (2000
  556. + 500
  557. * (boost::math::constants::root_two< double >()
  558. - 1))
  559. - 2000;
  560. } while (!in_heart(result));
  561. return result;
  562. }
  563. // Not going to provide clipping to bounding region or distance from
  564. // boundary
  565. double distance(point a, point b) const
  566. {
  567. if (segment_within_heart(a, b))
  568. {
  569. // Straight line
  570. return boost::math::hypot(b[0] - a[0], b[1] - a[1]);
  571. }
  572. else
  573. {
  574. // Straight line bending around (0, 0)
  575. return boost::math::hypot(a[0], a[1])
  576. + boost::math::hypot(b[0], b[1]);
  577. }
  578. }
  579. point move_position_toward(point a, double fraction, point b) const
  580. {
  581. if (segment_within_heart(a, b))
  582. {
  583. // Straight line
  584. return point(a[0] + (b[0] - a[0]) * fraction,
  585. a[1] + (b[1] - a[1]) * fraction);
  586. }
  587. else
  588. {
  589. double distance_to_point_a = boost::math::hypot(a[0], a[1]);
  590. double distance_to_point_b = boost::math::hypot(b[0], b[1]);
  591. double location_of_point = distance_to_point_a
  592. / (distance_to_point_a + distance_to_point_b);
  593. if (fraction < location_of_point)
  594. return point(a[0] * (1 - fraction / location_of_point),
  595. a[1] * (1 - fraction / location_of_point));
  596. else
  597. return point(b[0]
  598. * ((fraction - location_of_point)
  599. / (1 - location_of_point)),
  600. b[1]
  601. * ((fraction - location_of_point)
  602. / (1 - location_of_point)));
  603. }
  604. }
  605. private:
  606. shared_ptr< RandomNumberGenerator > gen_ptr;
  607. shared_ptr< rand_t > rand;
  608. };
  609. } // namespace boost
  610. #endif // BOOST_GRAPH_TOPOLOGY_HPP