demo/descriptor_map_demo.cc
author deba
Wed, 13 Jul 2005 14:05:49 +0000
changeset 1553 434f9add42cd
child 1554 572bc7d0d3e2
permissions -rw-r--r--
Demo about descriptor map.
deba@1553
     1
#include <lemon/list_graph.h>
deba@1553
     2
#include <lemon/graph_utils.h>
deba@1553
     3
#include <lemon/xy.h>
deba@1553
     4
deba@1553
     5
#include <lemon/graph_to_eps.h>
deba@1553
     6
deba@1553
     7
#include <cstdlib>
deba@1553
     8
#include <cmath>
deba@1553
     9
#include <ctime>
deba@1553
    10
deba@1553
    11
using namespace lemon;
deba@1553
    12
deba@1553
    13
// Special map type
deba@1553
    14
// It gives back a position for each node. The position of the nodes 
deba@1553
    15
// are on the circle with the given center and radius.
deba@1553
    16
//
deba@1553
    17
// Because we use the descriptor map it will hold the proprty described above
deba@1553
    18
// even if a node added or deleted. 
deba@1553
    19
template <typename Graph>
deba@1553
    20
class CircleMap {
deba@1553
    21
public:
deba@1553
    22
deba@1553
    23
  typedef xy<double> Value;
deba@1553
    24
  typedef typename Graph::Node Key;
deba@1553
    25
deba@1553
    26
  CircleMap(const Graph& _graph, 
deba@1553
    27
	    const Value& _center = Value(0.0, 0.0), 
deba@1553
    28
	    double _radius = 1.0) 
deba@1553
    29
    : descriptor(_graph), center(_center), radius(_radius) {}
deba@1553
    30
deba@1553
    31
  Value operator[](const Key& key) const {
deba@1553
    32
    double angle = descriptor[key] * 2 * M_PI 
deba@1553
    33
      / (double)descriptor.inverse().size();
deba@1553
    34
    double x = std::cos(angle) * radius + center.x;
deba@1553
    35
    double y = std::sin(angle) * radius + center.y;
deba@1553
    36
    return Value(x, y);
deba@1553
    37
  }
deba@1553
    38
deba@1553
    39
private:
deba@1553
    40
  
deba@1553
    41
  DescriptorMap<Graph, typename Graph::Node> descriptor;
deba@1553
    42
  Value center;
deba@1553
    43
  double radius;
deba@1553
    44
};
deba@1553
    45
deba@1553
    46
int main() {
deba@1553
    47
  std::srand(std::time(0));
deba@1553
    48
  typedef ListGraph Graph;
deba@1553
    49
  typedef Graph::Node Node;
deba@1553
    50
  typedef Graph::Edge Edge;
deba@1553
    51
  
deba@1553
    52
  // Generating a graph
deba@1553
    53
  
deba@1553
    54
  Graph graph;
deba@1553
    55
  
deba@1553
    56
  const int NODE = 16;
deba@1553
    57
  for (int i = 0; i < NODE; ++i) {
deba@1553
    58
    graph.addNode();
deba@1553
    59
  } 
deba@1553
    60
deba@1553
    61
  // Creating descriptor map and inverse
deba@1553
    62
  DescriptorMap<Graph, Node> nodeDesc(graph);
deba@1553
    63
  DescriptorMap<Graph, Node>::InverseMap nodeInv(nodeDesc);
deba@1553
    64
deba@1553
    65
  // Adding edges
deba@1553
    66
  // The descriptor map always maps an integer value for each node.
deba@1553
    67
  // The range of the values is always [0..n - 1] where n is the
deba@1553
    68
  // number of the nodes of the graph. The inverse map gives back the
deba@1553
    69
  // the node by its descriptor value. 
deba@1553
    70
  //
deba@1553
    71
  // The inversemap cannot works without its DescriptorMap because
deba@1553
    72
  // it holds reference to it. 
deba@1553
    73
  const int EDGE = (int)(NODE * std::log(NODE));
deba@1553
    74
  for (int i = 0; i < EDGE; ++i) {
deba@1553
    75
    int si = (int)(std::rand() / (RAND_MAX + 1.0) * NODE);
deba@1553
    76
    int ti = (int)(std::rand() / (RAND_MAX + 1.0) * NODE);
deba@1553
    77
      
deba@1553
    78
    graph.addEdge(nodeInv[si], nodeInv[ti]);
deba@1553
    79
  }
deba@1553
    80
deba@1553
    81
  // Make postscript from the graph.
deba@1553
    82
    
deba@1553
    83
  CircleMap<Graph> coords(graph, xy<double>(0.0, 0.0), 10.0);
deba@1553
    84
    
deba@1553
    85
  graphToEps(graph,"descriptor_map_demo.eps").scaleToA4().
deba@1553
    86
    title("Generated graph").
deba@1553
    87
    copyright("(C) 2005 LEMON Project").
deba@1553
    88
    coords(coords).
deba@1553
    89
    nodeScale(1.0).
deba@1553
    90
    enableParallel().parEdgeDist(1).
deba@1553
    91
    drawArrows().arrowWidth(1).arrowLength(1).
deba@1553
    92
    run();
deba@1553
    93
 
deba@1553
    94
  return 0;
deba@1553
    95
}