fruchterman_reingold.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // Copyright 2004, 2005 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: Douglas Gregor
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_GRAPH_FRUCHTERMAN_REINGOLD_FORCE_DIRECTED_LAYOUT_HPP
  8. #define BOOST_GRAPH_FRUCHTERMAN_REINGOLD_FORCE_DIRECTED_LAYOUT_HPP
  9. #include <boost/config/no_tr1/cmath.hpp>
  10. #include <boost/graph/graph_traits.hpp>
  11. #include <boost/graph/named_function_params.hpp>
  12. #include <boost/graph/iteration_macros.hpp>
  13. #include <boost/graph/topology.hpp> // For topology concepts
  14. #include <vector>
  15. #include <list>
  16. #include <algorithm> // for std::min and std::max
  17. #include <numeric> // for std::accumulate
  18. #include <cmath> // for std::sqrt and std::fabs
  19. #include <functional>
  20. namespace boost {
  21. struct square_distance_attractive_force {
  22. template<typename Graph, typename T>
  23. T
  24. operator()(typename graph_traits<Graph>::edge_descriptor,
  25. T k,
  26. T d,
  27. const Graph&) const
  28. {
  29. return d * d / k;
  30. }
  31. };
  32. struct square_distance_repulsive_force {
  33. template<typename Graph, typename T>
  34. T
  35. operator()(typename graph_traits<Graph>::vertex_descriptor,
  36. typename graph_traits<Graph>::vertex_descriptor,
  37. T k,
  38. T d,
  39. const Graph&) const
  40. {
  41. return k * k / d;
  42. }
  43. };
  44. template<typename T>
  45. struct linear_cooling {
  46. typedef T result_type;
  47. linear_cooling(std::size_t iterations)
  48. : temp(T(iterations) / T(10)), step(0.1) { }
  49. linear_cooling(std::size_t iterations, T temp)
  50. : temp(temp), step(temp / T(iterations)) { }
  51. T operator()()
  52. {
  53. T old_temp = temp;
  54. temp -= step;
  55. if (temp < T(0)) temp = T(0);
  56. return old_temp;
  57. }
  58. private:
  59. T temp;
  60. T step;
  61. };
  62. struct all_force_pairs
  63. {
  64. template<typename Graph, typename ApplyForce >
  65. void operator()(const Graph& g, ApplyForce apply_force)
  66. {
  67. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
  68. vertex_iterator v, end;
  69. for (boost::tie(v, end) = vertices(g); v != end; ++v) {
  70. vertex_iterator u = v;
  71. for (++u; u != end; ++u) {
  72. apply_force(*u, *v);
  73. apply_force(*v, *u);
  74. }
  75. }
  76. }
  77. };
  78. template<typename Topology, typename PositionMap>
  79. struct grid_force_pairs
  80. {
  81. typedef typename property_traits<PositionMap>::value_type Point;
  82. BOOST_STATIC_ASSERT (Point::dimensions == 2);
  83. typedef typename Topology::point_difference_type point_difference_type;
  84. template<typename Graph>
  85. explicit
  86. grid_force_pairs(const Topology& topology,
  87. PositionMap position, const Graph& g)
  88. : topology(topology), position(position)
  89. {
  90. two_k = 2. * this->topology.volume(this->topology.extent()) / std::sqrt((double)num_vertices(g));
  91. }
  92. template<typename Graph, typename ApplyForce >
  93. void operator()(const Graph& g, ApplyForce apply_force)
  94. {
  95. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
  96. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  97. typedef std::list<vertex_descriptor> bucket_t;
  98. typedef std::vector<bucket_t> buckets_t;
  99. std::size_t columns = std::size_t(topology.extent()[0] / two_k + 1.);
  100. std::size_t rows = std::size_t(topology.extent()[1] / two_k + 1.);
  101. buckets_t buckets(rows * columns);
  102. vertex_iterator v, v_end;
  103. for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v) {
  104. std::size_t column =
  105. std::size_t((get(position, *v)[0] + topology.extent()[0] / 2) / two_k);
  106. std::size_t row =
  107. std::size_t((get(position, *v)[1] + topology.extent()[1] / 2) / two_k);
  108. if (column >= columns) column = columns - 1;
  109. if (row >= rows) row = rows - 1;
  110. buckets[row * columns + column].push_back(*v);
  111. }
  112. for (std::size_t row = 0; row < rows; ++row)
  113. for (std::size_t column = 0; column < columns; ++column) {
  114. bucket_t& bucket = buckets[row * columns + column];
  115. typedef typename bucket_t::iterator bucket_iterator;
  116. for (bucket_iterator u = bucket.begin(); u != bucket.end(); ++u) {
  117. // Repulse vertices in this bucket
  118. bucket_iterator v = u;
  119. for (++v; v != bucket.end(); ++v) {
  120. apply_force(*u, *v);
  121. apply_force(*v, *u);
  122. }
  123. std::size_t adj_start_row = row == 0? 0 : row - 1;
  124. std::size_t adj_end_row = row == rows - 1? row : row + 1;
  125. std::size_t adj_start_column = column == 0? 0 : column - 1;
  126. std::size_t adj_end_column = column == columns - 1? column : column + 1;
  127. for (std::size_t other_row = adj_start_row; other_row <= adj_end_row;
  128. ++other_row)
  129. for (std::size_t other_column = adj_start_column;
  130. other_column <= adj_end_column; ++other_column)
  131. if (other_row != row || other_column != column) {
  132. // Repulse vertices in this bucket
  133. bucket_t& other_bucket
  134. = buckets[other_row * columns + other_column];
  135. for (v = other_bucket.begin(); v != other_bucket.end(); ++v) {
  136. double dist =
  137. topology.distance(get(position, *u), get(position, *v));
  138. if (dist < two_k) apply_force(*u, *v);
  139. }
  140. }
  141. }
  142. }
  143. }
  144. private:
  145. const Topology& topology;
  146. PositionMap position;
  147. double two_k;
  148. };
  149. template<typename PositionMap, typename Topology, typename Graph>
  150. inline grid_force_pairs<Topology, PositionMap>
  151. make_grid_force_pairs
  152. (const Topology& topology,
  153. const PositionMap& position, const Graph& g)
  154. { return grid_force_pairs<Topology, PositionMap>(topology, position, g); }
  155. template<typename Graph, typename PositionMap, typename Topology>
  156. void
  157. scale_graph(const Graph& g, PositionMap position, const Topology& topology,
  158. typename Topology::point_type upper_left, typename Topology::point_type lower_right)
  159. {
  160. if (num_vertices(g) == 0) return;
  161. typedef typename Topology::point_type Point;
  162. typedef typename Topology::point_difference_type point_difference_type;
  163. // Find min/max ranges
  164. Point min_point = get(position, *vertices(g).first), max_point = min_point;
  165. BGL_FORALL_VERTICES_T(v, g, Graph) {
  166. min_point = topology.pointwise_min(min_point, get(position, v));
  167. max_point = topology.pointwise_max(max_point, get(position, v));
  168. }
  169. Point old_origin = topology.move_position_toward(min_point, 0.5, max_point);
  170. Point new_origin = topology.move_position_toward(upper_left, 0.5, lower_right);
  171. point_difference_type old_size = topology.difference(max_point, min_point);
  172. point_difference_type new_size = topology.difference(lower_right, upper_left);
  173. // Scale to bounding box provided
  174. BGL_FORALL_VERTICES_T(v, g, Graph) {
  175. point_difference_type relative_loc = topology.difference(get(position, v), old_origin);
  176. relative_loc = (relative_loc / old_size) * new_size;
  177. put(position, v, topology.adjust(new_origin, relative_loc));
  178. }
  179. }
  180. namespace detail {
  181. template<typename Topology, typename PropMap, typename Vertex>
  182. void
  183. maybe_jitter_point(const Topology& topology,
  184. const PropMap& pm, Vertex v,
  185. const typename Topology::point_type& p2)
  186. {
  187. double too_close = topology.norm(topology.extent()) / 10000.;
  188. if (topology.distance(get(pm, v), p2) < too_close) {
  189. put(pm, v,
  190. topology.move_position_toward(get(pm, v), 1./200,
  191. topology.random_point()));
  192. }
  193. }
  194. template<typename Topology, typename PositionMap, typename DisplacementMap,
  195. typename RepulsiveForce, typename Graph>
  196. struct fr_apply_force
  197. {
  198. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  199. typedef typename Topology::point_type Point;
  200. typedef typename Topology::point_difference_type PointDiff;
  201. fr_apply_force(const Topology& topology,
  202. const PositionMap& position,
  203. const DisplacementMap& displacement,
  204. RepulsiveForce repulsive_force, double k, const Graph& g)
  205. : topology(topology), position(position), displacement(displacement),
  206. repulsive_force(repulsive_force), k(k), g(g)
  207. { }
  208. void operator()(vertex_descriptor u, vertex_descriptor v)
  209. {
  210. if (u != v) {
  211. // When the vertices land on top of each other, move the
  212. // first vertex away from the boundaries.
  213. maybe_jitter_point(topology, position, u, get(position, v));
  214. double dist = topology.distance(get(position, u), get(position, v));
  215. typename Topology::point_difference_type dispv = get(displacement, v);
  216. if (dist == 0.) {
  217. for (std::size_t i = 0; i < Point::dimensions; ++i) {
  218. dispv[i] += 0.01;
  219. }
  220. } else {
  221. double fr = repulsive_force(u, v, k, dist, g);
  222. dispv += (fr / dist) *
  223. topology.difference(get(position, v), get(position, u));
  224. }
  225. put(displacement, v, dispv);
  226. }
  227. }
  228. private:
  229. const Topology& topology;
  230. PositionMap position;
  231. DisplacementMap displacement;
  232. RepulsiveForce repulsive_force;
  233. double k;
  234. const Graph& g;
  235. };
  236. } // end namespace detail
  237. template<typename Topology, typename Graph, typename PositionMap,
  238. typename AttractiveForce, typename RepulsiveForce,
  239. typename ForcePairs, typename Cooling, typename DisplacementMap>
  240. void
  241. fruchterman_reingold_force_directed_layout
  242. (const Graph& g,
  243. PositionMap position,
  244. const Topology& topology,
  245. AttractiveForce attractive_force,
  246. RepulsiveForce repulsive_force,
  247. ForcePairs force_pairs,
  248. Cooling cool,
  249. DisplacementMap displacement)
  250. {
  251. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
  252. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  253. typedef typename graph_traits<Graph>::edge_iterator edge_iterator;
  254. double volume = topology.volume(topology.extent());
  255. // assume positions are initialized randomly
  256. double k = pow(volume / num_vertices(g), 1. / (double)(Topology::point_difference_type::dimensions));
  257. detail::fr_apply_force<Topology, PositionMap, DisplacementMap,
  258. RepulsiveForce, Graph>
  259. apply_force(topology, position, displacement, repulsive_force, k, g);
  260. do {
  261. // Calculate repulsive forces
  262. vertex_iterator v, v_end;
  263. for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v)
  264. put(displacement, *v, typename Topology::point_difference_type());
  265. force_pairs(g, apply_force);
  266. // Calculate attractive forces
  267. edge_iterator e, e_end;
  268. for (boost::tie(e, e_end) = edges(g); e != e_end; ++e) {
  269. vertex_descriptor v = source(*e, g);
  270. vertex_descriptor u = target(*e, g);
  271. // When the vertices land on top of each other, move the
  272. // first vertex away from the boundaries.
  273. ::boost::detail::maybe_jitter_point(topology, position, u, get(position, v));
  274. typename Topology::point_difference_type delta =
  275. topology.difference(get(position, v), get(position, u));
  276. double dist = topology.distance(get(position, u), get(position, v));
  277. double fa = attractive_force(*e, k, dist, g);
  278. put(displacement, v, get(displacement, v) - (fa / dist) * delta);
  279. put(displacement, u, get(displacement, u) + (fa / dist) * delta);
  280. }
  281. if (double temp = cool()) {
  282. // Update positions
  283. BGL_FORALL_VERTICES_T (v, g, Graph) {
  284. BOOST_USING_STD_MIN();
  285. BOOST_USING_STD_MAX();
  286. double disp_size = topology.norm(get(displacement, v));
  287. put(position, v, topology.adjust(get(position, v), get(displacement, v) * (min BOOST_PREVENT_MACRO_SUBSTITUTION (disp_size, temp) / disp_size)));
  288. put(position, v, topology.bound(get(position, v)));
  289. }
  290. } else {
  291. break;
  292. }
  293. } while (true);
  294. }
  295. namespace detail {
  296. template<typename DisplacementMap>
  297. struct fr_force_directed_layout
  298. {
  299. template<typename Topology, typename Graph, typename PositionMap,
  300. typename AttractiveForce, typename RepulsiveForce,
  301. typename ForcePairs, typename Cooling,
  302. typename Param, typename Tag, typename Rest>
  303. static void
  304. run(const Graph& g,
  305. PositionMap position,
  306. const Topology& topology,
  307. AttractiveForce attractive_force,
  308. RepulsiveForce repulsive_force,
  309. ForcePairs force_pairs,
  310. Cooling cool,
  311. DisplacementMap displacement,
  312. const bgl_named_params<Param, Tag, Rest>&)
  313. {
  314. fruchterman_reingold_force_directed_layout
  315. (g, position, topology, attractive_force, repulsive_force,
  316. force_pairs, cool, displacement);
  317. }
  318. };
  319. template<>
  320. struct fr_force_directed_layout<param_not_found>
  321. {
  322. template<typename Topology, typename Graph, typename PositionMap,
  323. typename AttractiveForce, typename RepulsiveForce,
  324. typename ForcePairs, typename Cooling,
  325. typename Param, typename Tag, typename Rest>
  326. static void
  327. run(const Graph& g,
  328. PositionMap position,
  329. const Topology& topology,
  330. AttractiveForce attractive_force,
  331. RepulsiveForce repulsive_force,
  332. ForcePairs force_pairs,
  333. Cooling cool,
  334. param_not_found,
  335. const bgl_named_params<Param, Tag, Rest>& params)
  336. {
  337. typedef typename Topology::point_difference_type PointDiff;
  338. std::vector<PointDiff> displacements(num_vertices(g));
  339. fruchterman_reingold_force_directed_layout
  340. (g, position, topology, attractive_force, repulsive_force,
  341. force_pairs, cool,
  342. make_iterator_property_map
  343. (displacements.begin(),
  344. choose_const_pmap(get_param(params, vertex_index), g,
  345. vertex_index),
  346. PointDiff()));
  347. }
  348. };
  349. } // end namespace detail
  350. template<typename Topology, typename Graph, typename PositionMap, typename Param,
  351. typename Tag, typename Rest>
  352. void
  353. fruchterman_reingold_force_directed_layout
  354. (const Graph& g,
  355. PositionMap position,
  356. const Topology& topology,
  357. const bgl_named_params<Param, Tag, Rest>& params)
  358. {
  359. typedef typename get_param_type<vertex_displacement_t, bgl_named_params<Param,Tag,Rest> >::type D;
  360. detail::fr_force_directed_layout<D>::run
  361. (g, position, topology,
  362. choose_param(get_param(params, attractive_force_t()),
  363. square_distance_attractive_force()),
  364. choose_param(get_param(params, repulsive_force_t()),
  365. square_distance_repulsive_force()),
  366. choose_param(get_param(params, force_pairs_t()),
  367. make_grid_force_pairs(topology, position, g)),
  368. choose_param(get_param(params, cooling_t()),
  369. linear_cooling<double>(100)),
  370. get_param(params, vertex_displacement_t()),
  371. params);
  372. }
  373. template<typename Topology, typename Graph, typename PositionMap>
  374. void
  375. fruchterman_reingold_force_directed_layout
  376. (const Graph& g,
  377. PositionMap position,
  378. const Topology& topology)
  379. {
  380. fruchterman_reingold_force_directed_layout
  381. (g, position, topology,
  382. attractive_force(square_distance_attractive_force()));
  383. }
  384. } // end namespace boost
  385. #ifdef BOOST_GRAPH_USE_MPI
  386. # include <boost/graph/distributed/fruchterman_reingold.hpp>
  387. #endif
  388. #endif // BOOST_GRAPH_FRUCHTERMAN_REINGOLD_FORCE_DIRECTED_LAYOUT_HPP