gursoy_atun_layout_test.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2004 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Jeremiah Willcock
  6. // Douglas Gregor
  7. // Andrew Lumsdaine
  8. #include <boost/graph/gursoy_atun_layout.hpp>
  9. #include "boost/graph/adjacency_list.hpp"
  10. #include "boost/graph/random.hpp"
  11. #include "boost/graph/graphviz.hpp"
  12. #include "boost/random/mersenne_twister.hpp"
  13. #include "boost/random/linear_congruential.hpp"
  14. #include "boost/random/uniform_01.hpp"
  15. #include <iostream>
  16. #include <fstream>
  17. #include <sstream>
  18. #if 0
  19. #include <boost/graph/plod_generator.hpp>
  20. #include <boost/graph/small_world_generator.hpp>
  21. #endif
  22. using namespace boost;
  23. template <class Property, class Vertex>
  24. struct position_writer {
  25. const Property& property;
  26. position_writer(const Property& property): property(property) {}
  27. void operator()(std::ostream& os, const Vertex& v) const {
  28. os << "[pos=\"" << int(property[v][0]) << "," << int(property[v][1]) << "\"]";
  29. }
  30. };
  31. struct graph_writer {
  32. void operator()(std::ostream& os) const {
  33. os << "node [shape=point, width=\".01\", height=\".01\", fixedsize=\"true\"]"
  34. << std::endl;
  35. }
  36. };
  37. int main(int, char*[]) {
  38. // Generate a graph structured like a grid, cylinder, or torus; lay it out in
  39. // a square grid; and output it in dot format
  40. typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
  41. boost::no_property,
  42. boost::property<boost::edge_weight_t, double>
  43. > graph_type;
  44. typedef boost::graph_traits<graph_type>::vertex_descriptor vertex_descriptor;
  45. // boost::mt19937 rng;
  46. // boost::generate_random_graph(graph, 100, 600, rng, false, false);
  47. #if 1
  48. graph_type graph;
  49. // Make grid, like Gursoy and Atun used
  50. std::map<int, std::map<int, vertex_descriptor> > verts;
  51. const int grid_size = 20;
  52. boost::minstd_rand edge_weight_gen;
  53. boost::uniform_01<boost::minstd_rand> random_edge_weight(edge_weight_gen);
  54. for (int i = 0; i < grid_size; ++i)
  55. for (int j = 0; j < grid_size; ++j)
  56. verts[i][j] = add_vertex(graph);
  57. for (int i = 0; i < grid_size; ++i) {
  58. for (int j = 0; j < grid_size; ++j) {
  59. if (i != 0)
  60. add_edge(verts[i][j], verts[i-1][j], random_edge_weight(), graph);
  61. if (j != 0)
  62. add_edge(verts[i][j], verts[i][j-1], random_edge_weight(), graph);
  63. #if 0
  64. // Uncomment parts of this to get a cylinder or torus
  65. if (i == 0)
  66. add_edge(verts[0][j], verts[grid_size-1][j], random_edge_weight(),
  67. graph);
  68. if (j == 0)
  69. add_edge(verts[i][0], verts[i][grid_size-1], random_edge_weight(),
  70. graph);
  71. #endif
  72. }
  73. }
  74. #else
  75. using namespace boost;
  76. #if 0
  77. int n = 10000;
  78. double alpha = 0.4;
  79. double beta = 50;
  80. minstd_rand gen;
  81. graph_type graph(plod_iterator<minstd_rand, graph_type>(gen, n, alpha, beta),
  82. plod_iterator<minstd_rand, graph_type>(),
  83. n);
  84. #else
  85. int n = 1000;
  86. int k = 6;
  87. double p = 0.001;
  88. minstd_rand gen;
  89. graph_type graph(small_world_iterator<minstd_rand>(gen, n, k, p),
  90. small_world_iterator<minstd_rand>(n, k),
  91. n);
  92. #endif
  93. #endif
  94. // boost::read_graphviz(stdin, graph);
  95. typedef boost::property_map<graph_type, boost::vertex_index_t>::type
  96. VertexIndexMap;
  97. VertexIndexMap vertex_index = get(boost::vertex_index_t(), graph);
  98. typedef boost::heart_topology<> topology;
  99. topology space;
  100. typedef topology::point_type point;
  101. std::vector<point> position_vector(num_vertices(graph));
  102. typedef boost::iterator_property_map<std::vector<point>::iterator,
  103. VertexIndexMap, point, point&> Position;
  104. Position position(position_vector.begin(), vertex_index);
  105. boost::gursoy_atun_layout(graph, space, position);
  106. #if 0
  107. std::cerr << "--------Unweighted layout--------\n";
  108. boost::write_graphviz(std::cout, graph,
  109. position_writer<Position, vertex_descriptor>(position),
  110. boost::default_writer(),
  111. graph_writer());
  112. #endif
  113. boost::gursoy_atun_layout(graph, space, position,
  114. weight_map(get(boost::edge_weight, graph)));
  115. #if 0
  116. std::cerr << "--------Weighted layout--------\n";
  117. boost::write_graphviz(std::cout, graph,
  118. position_writer<Position, vertex_descriptor>(position),
  119. boost::default_writer(),
  120. graph_writer());
  121. #endif
  122. return 0;
  123. }