kamada_kawai_spring_layout.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // Copyright 2004 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_KAMADA_KAWAI_SPRING_LAYOUT_HPP
  8. #define BOOST_GRAPH_KAMADA_KAWAI_SPRING_LAYOUT_HPP
  9. #include <boost/graph/graph_traits.hpp>
  10. #include <boost/graph/topology.hpp>
  11. #include <boost/graph/iteration_macros.hpp>
  12. #include <boost/graph/johnson_all_pairs_shortest.hpp>
  13. #include <boost/type_traits/is_convertible.hpp>
  14. #include <utility>
  15. #include <iterator>
  16. #include <vector>
  17. #include <iostream>
  18. #include <boost/limits.hpp>
  19. #include <boost/config/no_tr1/cmath.hpp>
  20. namespace boost
  21. {
  22. namespace detail
  23. {
  24. namespace graph
  25. {
  26. /**
  27. * Denotes an edge or display area side length used to scale a
  28. * Kamada-Kawai drawing.
  29. */
  30. template < bool Edge, typename T > struct edge_or_side
  31. {
  32. explicit edge_or_side(T value) : value(value) {}
  33. T value;
  34. };
  35. /**
  36. * Compute the edge length from an edge length. This is trivial.
  37. */
  38. template < typename Graph, typename DistanceMap, typename IndexMap,
  39. typename T >
  40. T compute_edge_length(
  41. const Graph&, DistanceMap, IndexMap, edge_or_side< true, T > length)
  42. {
  43. return length.value;
  44. }
  45. /**
  46. * Compute the edge length based on the display area side
  47. length. We do this by dividing the side length by the largest
  48. shortest distance between any two vertices in the graph.
  49. */
  50. template < typename Graph, typename DistanceMap, typename IndexMap,
  51. typename T >
  52. T compute_edge_length(const Graph& g, DistanceMap distance,
  53. IndexMap index, edge_or_side< false, T > length)
  54. {
  55. T result(0);
  56. typedef
  57. typename graph_traits< Graph >::vertex_iterator vertex_iterator;
  58. for (vertex_iterator ui = vertices(g).first,
  59. end = vertices(g).second;
  60. ui != end; ++ui)
  61. {
  62. vertex_iterator vi = ui;
  63. for (++vi; vi != end; ++vi)
  64. {
  65. T dij = distance[get(index, *ui)][get(index, *vi)];
  66. if (dij > result)
  67. result = dij;
  68. }
  69. }
  70. return length.value / result;
  71. }
  72. /**
  73. * Dense linear solver for fixed-size matrices.
  74. */
  75. template < std::size_t Size > struct linear_solver
  76. {
  77. // Indices in mat are (row, column)
  78. // template <typename Vec>
  79. // static Vec solve(double mat[Size][Size], Vec rhs);
  80. };
  81. template <> struct linear_solver< 1 >
  82. {
  83. template < typename Vec >
  84. static Vec solve(double mat[1][1], Vec rhs)
  85. {
  86. return rhs / mat[0][0];
  87. }
  88. };
  89. // These are from http://en.wikipedia.org/wiki/Cramer%27s_rule
  90. template <> struct linear_solver< 2 >
  91. {
  92. template < typename Vec >
  93. static Vec solve(double mat[2][2], Vec rhs)
  94. {
  95. double denom = mat[0][0] * mat[1][1] - mat[1][0] * mat[0][1];
  96. double x_num = rhs[0] * mat[1][1] - rhs[1] * mat[0][1];
  97. double y_num = mat[0][0] * rhs[1] - mat[1][0] * rhs[0];
  98. Vec result;
  99. result[0] = x_num / denom;
  100. result[1] = y_num / denom;
  101. return result;
  102. }
  103. };
  104. template <> struct linear_solver< 3 >
  105. {
  106. template < typename Vec >
  107. static Vec solve(double mat[3][3], Vec rhs)
  108. {
  109. double denom = mat[0][0]
  110. * (mat[1][1] * mat[2][2] - mat[2][1] * mat[1][2])
  111. - mat[1][0]
  112. * (mat[0][1] * mat[2][2] - mat[2][1] * mat[0][2])
  113. + mat[2][0]
  114. * (mat[0][1] * mat[1][2] - mat[1][1] * mat[0][2]);
  115. double x_num
  116. = rhs[0] * (mat[1][1] * mat[2][2] - mat[2][1] * mat[1][2])
  117. - rhs[1] * (mat[0][1] * mat[2][2] - mat[2][1] * mat[0][2])
  118. + rhs[2] * (mat[0][1] * mat[1][2] - mat[1][1] * mat[0][2]);
  119. double y_num
  120. = mat[0][0] * (rhs[1] * mat[2][2] - rhs[2] * mat[1][2])
  121. - mat[1][0] * (rhs[0] * mat[2][2] - rhs[2] * mat[0][2])
  122. + mat[2][0] * (rhs[0] * mat[1][2] - rhs[1] * mat[0][2]);
  123. double z_num
  124. = mat[0][0] * (mat[1][1] * rhs[2] - mat[2][1] * rhs[1])
  125. - mat[1][0] * (mat[0][1] * rhs[2] - mat[2][1] * rhs[0])
  126. + mat[2][0] * (mat[0][1] * rhs[1] - mat[1][1] * rhs[0]);
  127. Vec result;
  128. result[0] = x_num / denom;
  129. result[1] = y_num / denom;
  130. result[2] = z_num / denom;
  131. return result;
  132. }
  133. };
  134. /**
  135. * Implementation of the Kamada-Kawai spring layout algorithm.
  136. */
  137. template < typename Topology, typename Graph, typename PositionMap,
  138. typename WeightMap, typename EdgeOrSideLength, typename Done,
  139. typename VertexIndexMap, typename DistanceMatrix,
  140. typename SpringStrengthMatrix, typename PartialDerivativeMap >
  141. struct kamada_kawai_spring_layout_impl
  142. {
  143. typedef
  144. typename property_traits< WeightMap >::value_type weight_type;
  145. typedef typename Topology::point_type Point;
  146. typedef
  147. typename Topology::point_difference_type point_difference_type;
  148. typedef point_difference_type deriv_type;
  149. typedef
  150. typename graph_traits< Graph >::vertex_iterator vertex_iterator;
  151. typedef typename graph_traits< Graph >::vertex_descriptor
  152. vertex_descriptor;
  153. kamada_kawai_spring_layout_impl(const Topology& topology,
  154. const Graph& g, PositionMap position, WeightMap weight,
  155. EdgeOrSideLength edge_or_side_length, Done done,
  156. weight_type spring_constant, VertexIndexMap index,
  157. DistanceMatrix distance, SpringStrengthMatrix spring_strength,
  158. PartialDerivativeMap partial_derivatives)
  159. : topology(topology)
  160. , g(g)
  161. , position(position)
  162. , weight(weight)
  163. , edge_or_side_length(edge_or_side_length)
  164. , done(done)
  165. , spring_constant(spring_constant)
  166. , index(index)
  167. , distance(distance)
  168. , spring_strength(spring_strength)
  169. , partial_derivatives(partial_derivatives)
  170. {
  171. }
  172. // Compute contribution of vertex i to the first partial
  173. // derivatives (dE/dx_m, dE/dy_m) (for vertex m)
  174. deriv_type compute_partial_derivative(
  175. vertex_descriptor m, vertex_descriptor i)
  176. {
  177. #ifndef BOOST_NO_STDC_NAMESPACE
  178. using std::sqrt;
  179. #endif // BOOST_NO_STDC_NAMESPACE
  180. deriv_type result;
  181. if (i != m)
  182. {
  183. point_difference_type diff
  184. = topology.difference(position[m], position[i]);
  185. weight_type dist = topology.norm(diff);
  186. result = spring_strength[get(index, m)][get(index, i)]
  187. * (diff
  188. - distance[get(index, m)][get(index, i)] / dist
  189. * diff);
  190. }
  191. return result;
  192. }
  193. // Compute partial derivatives dE/dx_m and dE/dy_m
  194. deriv_type compute_partial_derivatives(vertex_descriptor m)
  195. {
  196. #ifndef BOOST_NO_STDC_NAMESPACE
  197. using std::sqrt;
  198. #endif // BOOST_NO_STDC_NAMESPACE
  199. deriv_type result;
  200. // TBD: looks like an accumulate to me
  201. BGL_FORALL_VERTICES_T(i, g, Graph)
  202. {
  203. deriv_type deriv = compute_partial_derivative(m, i);
  204. result += deriv;
  205. }
  206. return result;
  207. }
  208. // The actual Kamada-Kawai spring layout algorithm implementation
  209. bool run()
  210. {
  211. #ifndef BOOST_NO_STDC_NAMESPACE
  212. using std::sqrt;
  213. #endif // BOOST_NO_STDC_NAMESPACE
  214. // Compute d_{ij} and place it in the distance matrix
  215. if (!johnson_all_pairs_shortest_paths(
  216. g, distance, index, weight, weight_type(0)))
  217. return false;
  218. // Compute L based on side length (if needed), or retrieve L
  219. weight_type edge_length = detail::graph::compute_edge_length(
  220. g, distance, index, edge_or_side_length);
  221. // std::cerr << "edge_length = " << edge_length << std::endl;
  222. // Compute l_{ij} and k_{ij}
  223. const weight_type K = spring_constant;
  224. vertex_iterator ui, end;
  225. for (ui = vertices(g).first, end = vertices(g).second;
  226. ui != end; ++ui)
  227. {
  228. vertex_iterator vi = ui;
  229. for (++vi; vi != end; ++vi)
  230. {
  231. weight_type dij
  232. = distance[get(index, *ui)][get(index, *vi)];
  233. if (dij == (std::numeric_limits< weight_type >::max)())
  234. return false;
  235. distance[get(index, *ui)][get(index, *vi)]
  236. = edge_length * dij;
  237. distance[get(index, *vi)][get(index, *ui)]
  238. = edge_length * dij;
  239. spring_strength[get(index, *ui)][get(index, *vi)]
  240. = K / (dij * dij);
  241. spring_strength[get(index, *vi)][get(index, *ui)]
  242. = K / (dij * dij);
  243. }
  244. }
  245. // Compute Delta_i and find max
  246. vertex_descriptor p = *vertices(g).first;
  247. weight_type delta_p(0);
  248. for (ui = vertices(g).first, end = vertices(g).second;
  249. ui != end; ++ui)
  250. {
  251. deriv_type deriv = compute_partial_derivatives(*ui);
  252. put(partial_derivatives, *ui, deriv);
  253. weight_type delta = topology.norm(deriv);
  254. if (delta > delta_p)
  255. {
  256. p = *ui;
  257. delta_p = delta;
  258. }
  259. }
  260. while (!done(delta_p, p, g, true))
  261. {
  262. // The contribution p makes to the partial derivatives of
  263. // each vertex. Computing this (at O(n) cost) allows us to
  264. // update the delta_i values in O(n) time instead of O(n^2)
  265. // time.
  266. std::vector< deriv_type > p_partials(num_vertices(g));
  267. for (ui = vertices(g).first, end = vertices(g).second;
  268. ui != end; ++ui)
  269. {
  270. vertex_descriptor i = *ui;
  271. p_partials[get(index, i)]
  272. = compute_partial_derivative(i, p);
  273. }
  274. do
  275. {
  276. // For debugging, compute the energy value E
  277. double E = 0.;
  278. for (ui = vertices(g).first, end = vertices(g).second;
  279. ui != end; ++ui)
  280. {
  281. vertex_iterator vi = ui;
  282. for (++vi; vi != end; ++vi)
  283. {
  284. double dist = topology.distance(
  285. position[*ui], position[*vi]);
  286. weight_type k_ij = spring_strength[get(
  287. index, *ui)][get(index, *vi)];
  288. weight_type l_ij = distance[get(index, *ui)]
  289. [get(index, *vi)];
  290. E += .5 * k_ij * (dist - l_ij) * (dist - l_ij);
  291. }
  292. }
  293. // std::cerr << "E = " << E << std::endl;
  294. // Compute the elements of the Jacobian
  295. // From
  296. // http://www.cs.panam.edu/~rfowler/papers/1994_kumar_fowler_A_Spring_UTPACSTR.pdf
  297. // with the bugs fixed in the off-diagonal case
  298. weight_type dE_d_d[Point::dimensions]
  299. [Point::dimensions];
  300. for (std::size_t i = 0; i < Point::dimensions; ++i)
  301. for (std::size_t j = 0; j < Point::dimensions; ++j)
  302. dE_d_d[i][j] = 0.;
  303. for (ui = vertices(g).first, end = vertices(g).second;
  304. ui != end; ++ui)
  305. {
  306. vertex_descriptor i = *ui;
  307. if (i != p)
  308. {
  309. point_difference_type diff
  310. = topology.difference(
  311. position[p], position[i]);
  312. weight_type dist = topology.norm(diff);
  313. weight_type dist_squared = dist * dist;
  314. weight_type inv_dist_cubed
  315. = 1. / (dist_squared * dist);
  316. weight_type k_mi = spring_strength[get(
  317. index, p)][get(index, i)];
  318. weight_type l_mi
  319. = distance[get(index, p)][get(index, i)];
  320. for (std::size_t i = 0; i < Point::dimensions;
  321. ++i)
  322. {
  323. for (std::size_t j = 0;
  324. j < Point::dimensions; ++j)
  325. {
  326. if (i == j)
  327. {
  328. dE_d_d[i][i] += k_mi
  329. * (1
  330. + (l_mi
  331. * (diff[i] * diff[i]
  332. - dist_squared)
  333. * inv_dist_cubed));
  334. }
  335. else
  336. {
  337. dE_d_d[i][j] += k_mi * l_mi
  338. * diff[i] * diff[j]
  339. * inv_dist_cubed;
  340. // dE_d_d[i][j] += k_mi * l_mi *
  341. // sqrt(hypot(diff[i], diff[j])) *
  342. // inv_dist_cubed;
  343. }
  344. }
  345. }
  346. }
  347. }
  348. deriv_type dE_d = get(partial_derivatives, p);
  349. // Solve dE_d_d * delta = -dE_d to get delta
  350. point_difference_type delta
  351. = -linear_solver< Point::dimensions >::solve(
  352. dE_d_d, dE_d);
  353. // Move p by delta
  354. position[p] = topology.adjust(position[p], delta);
  355. // Recompute partial derivatives and delta_p
  356. deriv_type deriv = compute_partial_derivatives(p);
  357. put(partial_derivatives, p, deriv);
  358. delta_p = topology.norm(deriv);
  359. } while (!done(delta_p, p, g, false));
  360. // Select new p by updating each partial derivative and
  361. // delta
  362. vertex_descriptor old_p = p;
  363. for (ui = vertices(g).first, end = vertices(g).second;
  364. ui != end; ++ui)
  365. {
  366. deriv_type old_deriv_p = p_partials[get(index, *ui)];
  367. deriv_type old_p_partial
  368. = compute_partial_derivative(*ui, old_p);
  369. deriv_type deriv = get(partial_derivatives, *ui);
  370. deriv += old_p_partial - old_deriv_p;
  371. put(partial_derivatives, *ui, deriv);
  372. weight_type delta = topology.norm(deriv);
  373. if (delta > delta_p)
  374. {
  375. p = *ui;
  376. delta_p = delta;
  377. }
  378. }
  379. }
  380. return true;
  381. }
  382. const Topology& topology;
  383. const Graph& g;
  384. PositionMap position;
  385. WeightMap weight;
  386. EdgeOrSideLength edge_or_side_length;
  387. Done done;
  388. weight_type spring_constant;
  389. VertexIndexMap index;
  390. DistanceMatrix distance;
  391. SpringStrengthMatrix spring_strength;
  392. PartialDerivativeMap partial_derivatives;
  393. };
  394. }
  395. } // end namespace detail::graph
  396. /// States that the given quantity is an edge length.
  397. template < typename T >
  398. inline detail::graph::edge_or_side< true, T > edge_length(T x)
  399. {
  400. return detail::graph::edge_or_side< true, T >(x);
  401. }
  402. /// States that the given quantity is a display area side length.
  403. template < typename T >
  404. inline detail::graph::edge_or_side< false, T > side_length(T x)
  405. {
  406. return detail::graph::edge_or_side< false, T >(x);
  407. }
  408. /**
  409. * \brief Determines when to terminate layout of a particular graph based
  410. * on a given relative tolerance.
  411. */
  412. template < typename T = double > struct layout_tolerance
  413. {
  414. layout_tolerance(const T& tolerance = T(0.001))
  415. : tolerance(tolerance)
  416. , last_energy((std::numeric_limits< T >::max)())
  417. , last_local_energy((std::numeric_limits< T >::max)())
  418. {
  419. }
  420. template < typename Graph >
  421. bool operator()(T delta_p,
  422. typename boost::graph_traits< Graph >::vertex_descriptor p,
  423. const Graph& g, bool global)
  424. {
  425. if (global)
  426. {
  427. if (last_energy == (std::numeric_limits< T >::max)())
  428. {
  429. last_energy = delta_p;
  430. return false;
  431. }
  432. T diff = last_energy - delta_p;
  433. if (diff < T(0))
  434. diff = -diff;
  435. bool done = (delta_p == T(0) || diff / last_energy < tolerance);
  436. last_energy = delta_p;
  437. return done;
  438. }
  439. else
  440. {
  441. if (last_local_energy == (std::numeric_limits< T >::max)())
  442. {
  443. last_local_energy = delta_p;
  444. return delta_p == T(0);
  445. }
  446. T diff = last_local_energy - delta_p;
  447. bool done
  448. = (delta_p == T(0) || (diff / last_local_energy) < tolerance);
  449. last_local_energy = delta_p;
  450. return done;
  451. }
  452. }
  453. private:
  454. T tolerance;
  455. T last_energy;
  456. T last_local_energy;
  457. };
  458. /** \brief Kamada-Kawai spring layout for undirected graphs.
  459. *
  460. * This algorithm performs graph layout (in two dimensions) for
  461. * connected, undirected graphs. It operates by relating the layout
  462. * of graphs to a dynamic spring system and minimizing the energy
  463. * within that system. The strength of a spring between two vertices
  464. * is inversely proportional to the square of the shortest distance
  465. * (in graph terms) between those two vertices. Essentially,
  466. * vertices that are closer in the graph-theoretic sense (i.e., by
  467. * following edges) will have stronger springs and will therefore be
  468. * placed closer together.
  469. *
  470. * Prior to invoking this algorithm, it is recommended that the
  471. * vertices be placed along the vertices of a regular n-sided
  472. * polygon.
  473. *
  474. * \param g (IN) must be a model of Vertex List Graph, Edge List
  475. * Graph, and Incidence Graph and must be undirected.
  476. *
  477. * \param position (OUT) must be a model of Lvalue Property Map,
  478. * where the value type is a class containing fields @c x and @c y
  479. * that will be set to the @c x and @c y coordinates of each vertex.
  480. *
  481. * \param weight (IN) must be a model of Readable Property Map,
  482. * which provides the weight of each edge in the graph @p g.
  483. *
  484. * \param topology (IN) must be a topology object (see topology.hpp),
  485. * which provides operations on points and differences between them.
  486. *
  487. * \param edge_or_side_length (IN) provides either the unit length
  488. * @c e of an edge in the layout or the length of a side @c s of the
  489. * display area, and must be either @c boost::edge_length(e) or @c
  490. * boost::side_length(s), respectively.
  491. *
  492. * \param done (IN) is a 4-argument function object that is passed
  493. * the current value of delta_p (i.e., the energy of vertex @p p),
  494. * the vertex @p p, the graph @p g, and a boolean flag indicating
  495. * whether @p delta_p is the maximum energy in the system (when @c
  496. * true) or the energy of the vertex being moved. Defaults to @c
  497. * layout_tolerance instantiated over the value type of the weight
  498. * map.
  499. *
  500. * \param spring_constant (IN) is the constant multiplied by each
  501. * spring's strength. Larger values create systems with more energy
  502. * that can take longer to stabilize; smaller values create systems
  503. * with less energy that stabilize quickly but do not necessarily
  504. * result in pleasing layouts. The default value is 1.
  505. *
  506. * \param index (IN) is a mapping from vertices to index values
  507. * between 0 and @c num_vertices(g). The default is @c
  508. * get(vertex_index,g).
  509. *
  510. * \param distance (UTIL/OUT) will be used to store the distance
  511. * from every vertex to every other vertex, which is computed in the
  512. * first stages of the algorithm. This value's type must be a model
  513. * of BasicMatrix with value type equal to the value type of the
  514. * weight map. The default is a vector of vectors.
  515. *
  516. * \param spring_strength (UTIL/OUT) will be used to store the
  517. * strength of the spring between every pair of vertices. This
  518. * value's type must be a model of BasicMatrix with value type equal
  519. * to the value type of the weight map. The default is a vector of
  520. * vectors.
  521. *
  522. * \param partial_derivatives (UTIL) will be used to store the
  523. * partial derivates of each vertex with respect to the @c x and @c
  524. * y coordinates. This must be a Read/Write Property Map whose value
  525. * type is a pair with both types equivalent to the value type of
  526. * the weight map. The default is an iterator property map.
  527. *
  528. * \returns @c true if layout was successful or @c false if a
  529. * negative weight cycle was detected.
  530. */
  531. template < typename Topology, typename Graph, typename PositionMap,
  532. typename WeightMap, typename T, bool EdgeOrSideLength, typename Done,
  533. typename VertexIndexMap, typename DistanceMatrix,
  534. typename SpringStrengthMatrix, typename PartialDerivativeMap >
  535. bool kamada_kawai_spring_layout(const Graph& g, PositionMap position,
  536. WeightMap weight, const Topology& topology,
  537. detail::graph::edge_or_side< EdgeOrSideLength, T > edge_or_side_length,
  538. Done done,
  539. typename property_traits< WeightMap >::value_type spring_constant,
  540. VertexIndexMap index, DistanceMatrix distance,
  541. SpringStrengthMatrix spring_strength,
  542. PartialDerivativeMap partial_derivatives)
  543. {
  544. BOOST_STATIC_ASSERT(
  545. (is_convertible< typename graph_traits< Graph >::directed_category*,
  546. undirected_tag* >::value));
  547. detail::graph::kamada_kawai_spring_layout_impl< Topology, Graph,
  548. PositionMap, WeightMap,
  549. detail::graph::edge_or_side< EdgeOrSideLength, T >, Done,
  550. VertexIndexMap, DistanceMatrix, SpringStrengthMatrix,
  551. PartialDerivativeMap >
  552. alg(topology, g, position, weight, edge_or_side_length, done,
  553. spring_constant, index, distance, spring_strength,
  554. partial_derivatives);
  555. return alg.run();
  556. }
  557. /**
  558. * \overload
  559. */
  560. template < typename Topology, typename Graph, typename PositionMap,
  561. typename WeightMap, typename T, bool EdgeOrSideLength, typename Done,
  562. typename VertexIndexMap >
  563. bool kamada_kawai_spring_layout(const Graph& g, PositionMap position,
  564. WeightMap weight, const Topology& topology,
  565. detail::graph::edge_or_side< EdgeOrSideLength, T > edge_or_side_length,
  566. Done done,
  567. typename property_traits< WeightMap >::value_type spring_constant,
  568. VertexIndexMap index)
  569. {
  570. typedef typename property_traits< WeightMap >::value_type weight_type;
  571. typename graph_traits< Graph >::vertices_size_type n = num_vertices(g);
  572. typedef std::vector< weight_type > weight_vec;
  573. std::vector< weight_vec > distance(n, weight_vec(n));
  574. std::vector< weight_vec > spring_strength(n, weight_vec(n));
  575. std::vector< typename Topology::point_difference_type > partial_derivatives(
  576. n);
  577. return kamada_kawai_spring_layout(g, position, weight, topology,
  578. edge_or_side_length, done, spring_constant, index, distance.begin(),
  579. spring_strength.begin(),
  580. make_iterator_property_map(partial_derivatives.begin(), index,
  581. typename Topology::point_difference_type()));
  582. }
  583. /**
  584. * \overload
  585. */
  586. template < typename Topology, typename Graph, typename PositionMap,
  587. typename WeightMap, typename T, bool EdgeOrSideLength, typename Done >
  588. bool kamada_kawai_spring_layout(const Graph& g, PositionMap position,
  589. WeightMap weight, const Topology& topology,
  590. detail::graph::edge_or_side< EdgeOrSideLength, T > edge_or_side_length,
  591. Done done,
  592. typename property_traits< WeightMap >::value_type spring_constant)
  593. {
  594. return kamada_kawai_spring_layout(g, position, weight, topology,
  595. edge_or_side_length, done, spring_constant, get(vertex_index, g));
  596. }
  597. /**
  598. * \overload
  599. */
  600. template < typename Topology, typename Graph, typename PositionMap,
  601. typename WeightMap, typename T, bool EdgeOrSideLength, typename Done >
  602. bool kamada_kawai_spring_layout(const Graph& g, PositionMap position,
  603. WeightMap weight, const Topology& topology,
  604. detail::graph::edge_or_side< EdgeOrSideLength, T > edge_or_side_length,
  605. Done done)
  606. {
  607. typedef typename property_traits< WeightMap >::value_type weight_type;
  608. return kamada_kawai_spring_layout(g, position, weight, topology,
  609. edge_or_side_length, done, weight_type(1));
  610. }
  611. /**
  612. * \overload
  613. */
  614. template < typename Topology, typename Graph, typename PositionMap,
  615. typename WeightMap, typename T, bool EdgeOrSideLength >
  616. bool kamada_kawai_spring_layout(const Graph& g, PositionMap position,
  617. WeightMap weight, const Topology& topology,
  618. detail::graph::edge_or_side< EdgeOrSideLength, T > edge_or_side_length)
  619. {
  620. typedef typename property_traits< WeightMap >::value_type weight_type;
  621. return kamada_kawai_spring_layout(g, position, weight, topology,
  622. edge_or_side_length, layout_tolerance< weight_type >(),
  623. weight_type(1.0), get(vertex_index, g));
  624. }
  625. } // end namespace boost
  626. #endif // BOOST_GRAPH_KAMADA_KAWAI_SPRING_LAYOUT_HPP