1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
// 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
#include <boost/graph/fruchterman_reingold.hpp>
#include <boost/graph/random_layout.hpp>
#include <boost/graph/kamada_kawai_spring_layout.hpp>
#include <boost/graph/circle_layout.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/random/linear_congruential.hpp>
#include <boost/test/minimal.hpp>
#include <iostream>
#include <boost/limits.hpp>
#include <fstream>
#include <string>
using namespace boost;
enum vertex_position_t { vertex_position };
namespace boost { BOOST_INSTALL_PROPERTY(vertex, position); }
struct point
{
double x;
double y;
};
template<typename Graph, typename PositionMap>
void print_graph_layout(const Graph& g, PositionMap position)
{
typename graph_traits<Graph>::vertex_iterator vi, vi_end;
int xmin = 0, xmax = 0, ymin = 0, ymax = 0;
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
if ((int)position[*vi].x < xmin) xmin = (int)position[*vi].x;
if ((int)position[*vi].x > xmax) xmax = (int)position[*vi].x;
if ((int)position[*vi].y < ymin) ymin = (int)position[*vi].y;
if ((int)position[*vi].y > ymax) ymax = (int)position[*vi].y;
}
for (int y = ymin; y <= ymax; ++y) {
for (int x = xmin; x <= xmax; ++x) {
// Find vertex at this position
typename graph_traits<Graph>::vertices_size_type index = 0;
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi, ++index) {
if ((int)position[*vi].x == x && (int)position[*vi].y == y)
break;
}
if (vi == vi_end) std::cout << ' ';
else std::cout << (char)(index + 'A');
}
std::cout << std::endl;
}
}
template<typename Graph, typename PositionMap>
void dump_graph_layout(std::string name, const Graph& g, PositionMap position)
{
std::ofstream out((name + ".dot").c_str());
out << "graph " << name << " {" << std::endl;
typename graph_traits<Graph>::vertex_iterator vi, vi_end;
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
out << " n" << get(vertex_index, g, *vi) << "[ pos=\""
<< (int)position[*vi].x + 25 << ", " << (int)position[*vi].y + 25
<< "\" ];\n";
}
typename graph_traits<Graph>::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
out << " n" << get(vertex_index, g, source(*ei, g)) << " -- n"
<< get(vertex_index, g, target(*ei, g)) << ";\n";
}
out << "}\n";
}
template<typename Graph>
void
test_circle_layout(Graph*, typename graph_traits<Graph>::vertices_size_type n)
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex;
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
typedef typename graph_traits<Graph>::edges_size_type edges_size_type;
Graph g(n);
// Initialize vertex indices
vertex_iterator vi = vertices(g).first;
for (vertices_size_type i = 0; i < n; ++i, ++vi)
put(vertex_index, g, *vi, i);
circle_graph_layout(g, get(vertex_position, g), 10.0);
std::cout << "Regular polygon layout with " << n << " points.\n";
print_graph_layout(g, get(vertex_position, g));
}
struct simple_edge
{
int first, second;
};
struct kamada_kawai_done
{
kamada_kawai_done() : last_delta() {}
template<typename Graph>
bool operator()(double delta_p,
typename boost::graph_traits<Graph>::vertex_descriptor p,
const Graph& g,
bool global)
{
if (global) {
double diff = last_delta - delta_p;
if (diff < 0) diff = -diff;
last_delta = delta_p;
return diff < 0.01;
} else {
return delta_p < 0.01;
}
}
double last_delta;
};
template<typename Graph>
void
test_triangle(Graph*)
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
Graph g;
vertex_descriptor u = add_vertex(g); put(vertex_index, g, u, 0);
vertex_descriptor v = add_vertex(g); put(vertex_index, g, v, 1);
vertex_descriptor w = add_vertex(g); put(vertex_index, g, w, 2);
edge_descriptor e1 = add_edge(u, v, g).first; put(edge_weight, g, e1, 1.0);
edge_descriptor e2 = add_edge(v, w, g).first; put(edge_weight, g, e2, 1.0);
edge_descriptor e3 = add_edge(w, u, g).first; put(edge_weight, g, e3, 1.0);
circle_graph_layout(g, get(vertex_position, g), 25.0);
bool ok = kamada_kawai_spring_layout(g,
get(vertex_position, g),
get(edge_weight, g),
side_length(50.0));
BOOST_CHECK(ok);
std::cout << "Triangle layout (Kamada-Kawai).\n";
print_graph_layout(g, get(vertex_position, g));
}
template<typename Graph>
void
test_cube(Graph*)
{
enum {A, B, C, D, E, F, G, H};
simple_edge cube_edges[12] = {
{A, E}, {A, B}, {A, D}, {B, F}, {B, C}, {C, D}, {C, G}, {D, H},
{E, H}, {E, F}, {F, G}, {G, H}
};
Graph g(&cube_edges[0], &cube_edges[12], 8);
typedef typename graph_traits<Graph>::edge_iterator edge_iterator;
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
vertex_iterator vi, vi_end;
int i = 0;
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
put(vertex_index, g, *vi, i++);
edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
put(edge_weight, g, *ei, 1.0);
std::cerr << "(" << (char)(get(vertex_index, g, source(*ei, g)) + 'A')
<< ", " << (char)(get(vertex_index, g, target(*ei, g)) + 'A')
<< ") ";
}
std::cerr << std::endl;
circle_graph_layout(g, get(vertex_position, g), 25.0);
bool ok = kamada_kawai_spring_layout(g,
get(vertex_position, g),
get(edge_weight, g),
side_length(50.0),
kamada_kawai_done());
BOOST_CHECK(ok);
std::cout << "Cube layout (Kamada-Kawai).\n";
print_graph_layout(g, get(vertex_position, g));
dump_graph_layout("cube", g, get(vertex_position, g));
minstd_rand gen;
random_graph_layout(g, get(vertex_position, g), -25.0, 25.0, -25.0, 25.0,
gen);
std::vector<point> displacements(num_vertices(g));
fruchterman_reingold_force_directed_layout
(g,
get(vertex_position, g),
50.0,
50.0,
square_distance_attractive_force(),
square_distance_repulsive_force(),
all_force_pairs(),
linear_cooling<double>(100),
make_iterator_property_map(displacements.begin(),
get(vertex_index, g),
point()));
std::cout << "Cube layout (Fruchterman-Reingold).\n";
print_graph_layout(g, get(vertex_position, g));
dump_graph_layout("cube-fr", g, get(vertex_position, g));
}
template<typename Graph>
void
test_triangular(Graph*)
{
enum {A, B, C, D, E, F, G, H, I, J};
simple_edge triangular_edges[18] = {
{A, B}, {A, C}, {B, C}, {B, D}, {B, E}, {C, E}, {C, F}, {D, E}, {D, G},
{D, H}, {E, F}, {E, H}, {E, I}, {F, I}, {F, J}, {G, H}, {H, I}, {I, J}
};
Graph g(&triangular_edges[0], &triangular_edges[18], 10);
typedef typename graph_traits<Graph>::edge_iterator edge_iterator;
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
vertex_iterator vi, vi_end;
int i = 0;
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
put(vertex_index, g, *vi, i++);
edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
put(edge_weight, g, *ei, 1.0);
std::cerr << "(" << (char)(get(vertex_index, g, source(*ei, g)) + 'A')
<< ", " << (char)(get(vertex_index, g, target(*ei, g)) + 'A')
<< ") ";
}
std::cerr << std::endl;
circle_graph_layout(g, get(vertex_position, g), 25.0);
bool ok = kamada_kawai_spring_layout(g,
get(vertex_position, g),
get(edge_weight, g),
side_length(50.0),
kamada_kawai_done());
BOOST_CHECK(ok);
std::cout << "Triangular layout (Kamada-Kawai).\n";
print_graph_layout(g, get(vertex_position, g));
dump_graph_layout("triangular-kk", g, get(vertex_position, g));
minstd_rand gen;
random_graph_layout(g, get(vertex_position, g), -25.0, 25.0, -25.0, 25.0,
gen);
dump_graph_layout("random", g, get(vertex_position, g));
std::vector<point> displacements(num_vertices(g));
fruchterman_reingold_force_directed_layout
(g,
get(vertex_position, g),
50.0,
50.0,
attractive_force(square_distance_attractive_force()).
cooling(linear_cooling<double>(100)));
std::cout << "Triangular layout (Fruchterman-Reingold).\n";
print_graph_layout(g, get(vertex_position, g));
dump_graph_layout("triangular-fr", g, get(vertex_position, g));
}
template<typename Graph>
void
test_disconnected(Graph*)
{
enum {A, B, C, D, E, F, G, H};
simple_edge triangular_edges[13] = {
{A, B}, {B, C}, {C, A},
{D, E}, {E, F}, {F, G}, {G, H}, {H, D},
{D, F}, {F, H}, {H, E}, {E, G}, {G, D}
};
Graph g(&triangular_edges[0], &triangular_edges[13], 8);
typedef typename graph_traits<Graph>::edge_iterator edge_iterator;
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
vertex_iterator vi, vi_end;
int i = 0;
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
put(vertex_index, g, *vi, i++);
edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
put(edge_weight, g, *ei, 1.0);
std::cerr << "(" << (char)(get(vertex_index, g, source(*ei, g)) + 'A')
<< ", " << (char)(get(vertex_index, g, target(*ei, g)) + 'A')
<< ") ";
}
std::cerr << std::endl;
circle_graph_layout(g, get(vertex_position, g), 25.0);
bool ok = kamada_kawai_spring_layout(g,
get(vertex_position, g),
get(edge_weight, g),
side_length(50.0),
kamada_kawai_done());
BOOST_CHECK(!ok);
minstd_rand gen;
random_graph_layout(g, get(vertex_position, g), -25.0, 25.0, -25.0, 25.0,
gen);
std::vector<point> displacements(num_vertices(g));
fruchterman_reingold_force_directed_layout
(g,
get(vertex_position, g),
50.0,
50.0,
attractive_force(square_distance_attractive_force()).
cooling(linear_cooling<double>(50)));
std::cout << "Disconnected layout (Fruchterman-Reingold).\n";
print_graph_layout(g, get(vertex_position, g));
dump_graph_layout("disconnected-fr", g, get(vertex_position, g));
}
int test_main(int, char*[])
{
typedef adjacency_list<listS, listS, undirectedS,
// Vertex properties
property<vertex_index_t, int,
property<vertex_position_t, point> >,
// Edge properties
property<edge_weight_t, double> > Graph;
test_circle_layout((Graph*)0, 5);
test_cube((Graph*)0);
test_triangular((Graph*)0);
test_disconnected((Graph*)0);
return 0;
}
|