kamada_kawai_spring_layout.hpp 24 KB

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