kamada_kawai_spring_layout.hpp

来自「CGAL is a collaborative effort of severa」· HPP 代码 · 共 543 行 · 第 1/2 页

HPP
543
字号
// Copyright 2004 The Trustees of Indiana University.// Use, modification and distribution is subject to the Boost Software// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)//  Authors: Douglas Gregor//           Andrew Lumsdaine#ifndef BOOST_GRAPH_KAMADA_KAWAI_SPRING_LAYOUT_HPP#define BOOST_GRAPH_KAMADA_KAWAI_SPRING_LAYOUT_HPP#include <boost/graph/graph_traits.hpp>#include <boost/graph/johnson_all_pairs_shortest.hpp>#include <boost/type_traits/is_convertible.hpp>#include <utility>#include <iterator>#include <vector>#include <boost/limits.hpp>#include <cmath>namespace boost {  namespace detail { namespace graph {    /**     * Denotes an edge or display area side length used to scale a     * Kamada-Kawai drawing.     */    template<bool Edge, typename T>    struct edge_or_side    {      explicit edge_or_side(T value) : value(value) {}      T value;    };    /**     * Compute the edge length from an edge length. This is trivial.     */    template<typename Graph, typename DistanceMap, typename IndexMap,              typename T>    T compute_edge_length(const Graph&, DistanceMap, IndexMap,                           edge_or_side<true, T> length)    { return length.value; }    /**     * Compute the edge length based on the display area side       length. We do this by dividing the side length by the largest       shortest distance between any two vertices in the graph.     */    template<typename Graph, typename DistanceMap, typename IndexMap,              typename T>    T    compute_edge_length(const Graph& g, DistanceMap distance, IndexMap index,                        edge_or_side<false, T> length)    {      T result(0);      typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;      for (vertex_iterator ui = vertices(g).first, end = vertices(g).second;           ui != end; ++ui) {        vertex_iterator vi = ui;        for (++vi; vi != end; ++vi) {          T dij = distance[get(index, *ui)][get(index, *vi)];          if (dij > result) result = dij;        }      }      return length.value / result;    }    /**     * Implementation of the Kamada-Kawai spring layout algorithm.     */    template<typename Graph, typename PositionMap, typename WeightMap,             typename EdgeOrSideLength, typename Done,             typename VertexIndexMap, typename DistanceMatrix,             typename SpringStrengthMatrix, typename PartialDerivativeMap>    struct kamada_kawai_spring_layout_impl    {      typedef typename property_traits<WeightMap>::value_type weight_type;      typedef std::pair<weight_type, weight_type> deriv_type;      typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;      typedef typename graph_traits<Graph>::vertex_descriptor        vertex_descriptor;      kamada_kawai_spring_layout_impl(        const Graph& g,         PositionMap position,        WeightMap weight,         EdgeOrSideLength edge_or_side_length,        Done done,        weight_type spring_constant,        VertexIndexMap index,        DistanceMatrix distance,        SpringStrengthMatrix spring_strength,        PartialDerivativeMap partial_derivatives)        : g(g), position(position), weight(weight),           edge_or_side_length(edge_or_side_length), done(done),          spring_constant(spring_constant), index(index), distance(distance),          spring_strength(spring_strength),           partial_derivatives(partial_derivatives) {}      // Compute contribution of vertex i to the first partial      // derivatives (dE/dx_m, dE/dy_m) (for vertex m)      deriv_type      compute_partial_derivative(vertex_descriptor m, vertex_descriptor i)      {#ifndef BOOST_NO_STDC_NAMESPACE        using std::sqrt;#endif // BOOST_NO_STDC_NAMESPACE        deriv_type result(0, 0);        if (i != m) {          weight_type x_diff = position[m].x - position[i].x;          weight_type y_diff = position[m].y - position[i].y;          weight_type dist = sqrt(x_diff * x_diff + y_diff * y_diff);          result.first = spring_strength[get(index, m)][get(index, i)]             * (x_diff - distance[get(index, m)][get(index, i)]*x_diff/dist);          result.second = spring_strength[get(index, m)][get(index, i)]             * (y_diff - distance[get(index, m)][get(index, i)]*y_diff/dist);        }        return result;      }      // Compute partial derivatives dE/dx_m and dE/dy_m      deriv_type       compute_partial_derivatives(vertex_descriptor m)      {#ifndef BOOST_NO_STDC_NAMESPACE        using std::sqrt;#endif // BOOST_NO_STDC_NAMESPACE        deriv_type result(0, 0);        // TBD: looks like an accumulate to me        std::pair<vertex_iterator, vertex_iterator> verts = vertices(g);        for (/* no init */; verts.first != verts.second; ++verts.first) {          vertex_descriptor i = *verts.first;          deriv_type deriv = compute_partial_derivative(m, i);          result.first += deriv.first;          result.second += deriv.second;        }        return result;      }      // The actual Kamada-Kawai spring layout algorithm implementation      bool run()      {#ifndef BOOST_NO_STDC_NAMESPACE        using std::sqrt;#endif // BOOST_NO_STDC_NAMESPACE        // Compute d_{ij} and place it in the distance matrix        if (!johnson_all_pairs_shortest_paths(g, distance, index, weight,                                               weight_type(0)))          return false;        // Compute L based on side length (if needed), or retrieve L        weight_type edge_length =           detail::graph::compute_edge_length(g, distance, index,                                             edge_or_side_length);                // Compute l_{ij} and k_{ij}        const weight_type K = spring_constant;        vertex_iterator ui, end = vertices(g).second;        for (ui = vertices(g).first; ui != end; ++ui) {          vertex_iterator vi = ui;          for (++vi; vi != end; ++vi) {            weight_type dij = distance[get(index, *ui)][get(index, *vi)];            if (dij == std::numeric_limits<weight_type>::max())              return false;            distance[get(index, *ui)][get(index, *vi)] = edge_length * dij;            distance[get(index, *vi)][get(index, *ui)] = edge_length * dij;            spring_strength[get(index, *ui)][get(index, *vi)] = K/(dij*dij);            spring_strength[get(index, *vi)][get(index, *ui)] = K/(dij*dij);          }        }                // Compute Delta_i and find max        vertex_descriptor p = *vertices(g).first;        weight_type delta_p(0);        for (ui = vertices(g).first; ui != end; ++ui) {          deriv_type deriv = compute_partial_derivatives(*ui);          put(partial_derivatives, *ui, deriv);          weight_type delta =             sqrt(deriv.first*deriv.first + deriv.second*deriv.second);          if (delta > delta_p) {            p = *ui;            delta_p = delta;          }        }        while (!done(delta_p, p, g, true)) {          // The contribution p makes to the partial derivatives of          // each vertex. Computing this (at O(n) cost) allows us to          // update the delta_i values in O(n) time instead of O(n^2)          // time.          std::vector<deriv_type> p_partials(num_vertices(g));          for (ui = vertices(g).first; ui != end; ++ui) {            vertex_descriptor i = *ui;            p_partials[get(index, i)] = compute_partial_derivative(i, p);          }          do {            // Compute the 4 elements of the Jacobian            weight_type dE_dx_dx = 0, dE_dx_dy = 0, dE_dy_dx = 0, dE_dy_dy = 0;            for (ui = vertices(g).first; ui != end; ++ui) {              vertex_descriptor i = *ui;              if (i != p) {                weight_type x_diff = position[p].x - position[i].x;                weight_type y_diff = position[p].y - position[i].y;                weight_type dist = sqrt(x_diff * x_diff + y_diff * y_diff);                weight_type dist_cubed = dist * dist * dist;                weight_type k_mi = spring_strength[get(index,p)][get(index,i)];                weight_type l_mi = distance[get(index, p)][get(index, i)];                dE_dx_dx += k_mi * (1 - (l_mi * y_diff * y_diff)/dist_cubed);                dE_dx_dy += k_mi * l_mi * x_diff * y_diff / dist_cubed;                dE_dy_dx += k_mi * l_mi * x_diff * y_diff / dist_cubed;                dE_dy_dy += k_mi * (1 - (l_mi * x_diff * x_diff)/dist_cubed);              }            }            // Solve for delta_x and delta_y            weight_type dE_dx = get(partial_derivatives, p).first;            weight_type dE_dy = get(partial_derivatives, p).second;            weight_type delta_x =               (dE_dx_dy * dE_dy - dE_dy_dy * dE_dx)              / (dE_dx_dx * dE_dy_dy - dE_dx_dy * dE_dy_dx);            weight_type delta_y =               (dE_dx_dx * dE_dy - dE_dy_dx * dE_dx)              / (dE_dy_dx * dE_dx_dy - dE_dx_dx * dE_dy_dy);            // Move p by (delta_x, delta_y)            position[p].x += delta_x;            position[p].y += delta_y;            // Recompute partial derivatives and delta_p            deriv_type deriv = compute_partial_derivatives(p);            put(partial_derivatives, p, deriv);            delta_p =               sqrt(deriv.first*deriv.first + deriv.second*deriv.second);          } while (!done(delta_p, p, g, false));          // Select new p by updating each partial derivative and delta          vertex_descriptor old_p = p;          for (ui = vertices(g).first; ui != end; ++ui) {            deriv_type old_deriv_p = p_partials[get(index, *ui)];            deriv_type old_p_partial =               compute_partial_derivative(*ui, old_p);            deriv_type deriv = get(partial_derivatives, *ui);            deriv.first += old_p_partial.first - old_deriv_p.first;            deriv.second += old_p_partial.second - old_deriv_p.second;            put(partial_derivatives, *ui, deriv);            weight_type delta =               sqrt(deriv.first*deriv.first + deriv.second*deriv.second);            if (delta > delta_p) {              p = *ui;              delta_p = delta;            }          }        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?