1 | /* -*- C++ -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2006 |
---|
6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
8 | * |
---|
9 | * Permission to use, modify and distribute this software is granted |
---|
10 | * provided that this copyright notice appears in all copies. For |
---|
11 | * precise terms see the accompanying LICENSE file. |
---|
12 | * |
---|
13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
14 | * express or implied, and with no claim as to its suitability for any |
---|
15 | * purpose. |
---|
16 | * |
---|
17 | */ |
---|
18 | |
---|
19 | #include <lemon/list_graph.h> |
---|
20 | #include <lemon/graph_utils.h> |
---|
21 | #include <lemon/graph_writer.h> |
---|
22 | #include <lemon/xy.h> |
---|
23 | #include <lemon/graph_to_eps.h> |
---|
24 | |
---|
25 | #include <iostream> |
---|
26 | |
---|
27 | #include <cstdlib> |
---|
28 | #include <cmath> |
---|
29 | #include <ctime> |
---|
30 | |
---|
31 | using namespace lemon; |
---|
32 | |
---|
33 | // Special map type |
---|
34 | // It gives back a position for each node. The position of the nodes |
---|
35 | // are on the circle with the given center and radius. |
---|
36 | // |
---|
37 | // Because we use the descriptor map it will hold the proprty described above |
---|
38 | // even if a node added or deleted. |
---|
39 | template <typename Graph> |
---|
40 | class CircleMap { |
---|
41 | public: |
---|
42 | |
---|
43 | typedef xy<double> Value; |
---|
44 | typedef typename Graph::Node Key; |
---|
45 | |
---|
46 | CircleMap(const Graph& _graph, |
---|
47 | const Value& _center = Value(0.0, 0.0), |
---|
48 | double _radius = 1.0) |
---|
49 | : descriptor(_graph), center(_center), radius(_radius) {} |
---|
50 | |
---|
51 | Value operator[](const Key& key) const { |
---|
52 | double angle = descriptor[key] * 2 * M_PI |
---|
53 | / (double)descriptor.inverse().size(); |
---|
54 | double x = std::cos(angle) * radius + center.x; |
---|
55 | double y = std::sin(angle) * radius + center.y; |
---|
56 | return Value(x, y); |
---|
57 | } |
---|
58 | |
---|
59 | private: |
---|
60 | |
---|
61 | DescriptorMap<Graph, typename Graph::Node> descriptor; |
---|
62 | Value center; |
---|
63 | double radius; |
---|
64 | }; |
---|
65 | |
---|
66 | int main() { |
---|
67 | std::srand(std::time(0)); |
---|
68 | typedef ListGraph Graph; |
---|
69 | typedef Graph::Node Node; |
---|
70 | typedef Graph::Edge Edge; |
---|
71 | |
---|
72 | // Generating a graph |
---|
73 | |
---|
74 | std::cout << "Generating graph: " << std::endl; |
---|
75 | |
---|
76 | Graph graph; |
---|
77 | |
---|
78 | const int NODE = 16; |
---|
79 | for (int i = 0; i < NODE; ++i) { |
---|
80 | graph.addNode(); |
---|
81 | } |
---|
82 | |
---|
83 | // Creating descriptor map and inverse |
---|
84 | DescriptorMap<Graph, Node> nodeDesc(graph); |
---|
85 | DescriptorMap<Graph, Node>::InverseMap nodeInv(nodeDesc); |
---|
86 | |
---|
87 | // Adding edges |
---|
88 | // The descriptor map always maps an integer value for each node. |
---|
89 | // The range of the values is always [0..n - 1] where n is the |
---|
90 | // number of the nodes of the graph. The inverse map gives back the |
---|
91 | // the node by its descriptor value. |
---|
92 | // |
---|
93 | // The inversemap cannot works without its DescriptorMap because |
---|
94 | // it holds reference to it. |
---|
95 | const int EDGE = (int)(NODE * std::log(double(NODE))); |
---|
96 | for (int i = 0; i < EDGE; ++i) { |
---|
97 | int si = (int)(std::rand() / (RAND_MAX + 1.0) * NODE); |
---|
98 | int ti = (int)(std::rand() / (RAND_MAX + 1.0) * NODE); |
---|
99 | |
---|
100 | graph.addEdge(nodeInv[si], nodeInv[ti]); |
---|
101 | } |
---|
102 | |
---|
103 | GraphWriter<Graph>(std::cout, graph).run(); |
---|
104 | |
---|
105 | std::cout << std::endl; |
---|
106 | std::cout << "Postscript file: descriptor_map_demo.eps" << std::endl; |
---|
107 | |
---|
108 | // Make postscript from the graph. |
---|
109 | |
---|
110 | CircleMap<Graph> coords(graph, xy<double>(0.0, 0.0), 10.0); |
---|
111 | |
---|
112 | graphToEps(graph,"descriptor_map_demo.eps").scaleToA4(). |
---|
113 | title("Generated graph"). |
---|
114 | copyright("(C) 2006 LEMON Project"). |
---|
115 | coords(coords). |
---|
116 | nodeScale(1.0). |
---|
117 | enableParallel().parEdgeDist(1). |
---|
118 | drawArrows().arrowWidth(1).arrowLength(1). |
---|
119 | run(); |
---|
120 | |
---|
121 | return 0; |
---|
122 | } |
---|