chiark / gitweb /
ae36ea4abb5d6ecf8a8027a00598aff5048a95c4
[moebius2.git] / bgl.cpp
1 /*
2  * Everything that needs the Boost Graph Library and C++ templates etc.
3  * (and what a crazy set of stuff that all is)
4  */
5
6 #include <math.h>
7
8 #include <iterator>
9
10 #include <boost/config.hpp>
11 #include <boost/iterator/iterator_facade.hpp>
12 #include <boost/graph/graph_traits.hpp>
13 #include <boost/graph/graph_concepts.hpp>
14 #include <boost/graph/dijkstra_shortest_paths.hpp>
15 #include <boost/graph/properties.hpp>
16 #include <boost/iterator/counting_iterator.hpp>
17 #include <boost/iterator/iterator_categories.hpp>
18
19 extern "C" {
20 #include "bgl.h"
21 #include "mgraph.h"
22 }
23
24 /*
25  * edge descriptor f =   00 | e | y     | x
26  *                            3  YBITS   XBITS
27  *
28  * e is 0..5.  The edge is edge e out of vertex (x,y).
29  *
30  * BGL expects an undirected graph's edges to have two descriptors
31  * each, one in each direction.
32  */
33
34 /*
35  * We use BGL's implementation of Dijkstra's single source shortest
36  * paths.  We really want all pairs shortest paths, so Johnson All
37  * Pairs Shortest Paths would seem sensible.  But actually Johnson's
38  * algorithm is just a wrapper around Dijkstra's; the extra
39  * functionality is just to deal with -ve edge weights, which we don't
40  * have.  So we can use Dijkstra directly and save some cpu (and some
41  * code: we don't have to supply all of the machinery needed for
42  * Johnson's invocation of Bellman-Ford).  The overall time cost is
43  * O(VE log V); I think the space used is O(E).
44  */
45
46 #define VMASK (YMASK|XMASK)
47 #define ESHIFT (YBITS|XBITS)
48
49 class Graph { }; // this is a dummy as our graph has no actual representation
50
51 using namespace boost;
52
53 struct OutEdgeIterator :
54   public iterator_facade<
55     OutEdgeIterator,
56     int const,
57     forward_traversal_tag
58 > {
59   int f;
60   void increment() { f += 1<<ESHIFT; }
61   bool equal(OutEdgeIterator const& other) const { return f == other.f; }
62   int const& dereference() const { return f; }
63   OutEdgeIterator() { }
64   OutEdgeIterator(int _f) : f(_f) { }
65   OutEdgeIterator(int v, int e) : f(e << ESHIFT | v) { }
66 };
67  
68 typedef counting_iterator<int> VertexIterator;
69
70 namespace boost {
71   // We make Graph a model of various BGL Graph concepts.
72   // This mainly means that graph_traits<Graph> has lots of stuff.
73
74   // First, some definitions used later:
75   
76   struct layout_graph_traversal_category : 
77     public virtual incidence_graph_tag,
78     public virtual vertex_list_graph_tag,
79     public virtual edge_list_graph_tag { };
80
81   struct graph_traits<Graph> {
82     // Concept Graph:
83     typedef int vertex_descriptor; /* vertex number, -1 => none */
84     typedef int edge_descriptor; /* see above */
85     typedef undirected_tag directed_category;
86     typedef disallow_parallel_edge_tag edge_parallel_category;
87     typedef layout_graph_traversal_category traversal_category;
88
89     // Concept IncidenceGraph:
90     typedef OutEdgeIterator out_edge_iterator;
91     typedef unsigned degree_size_type;
92
93     // Concept VertexListGraph:
94     typedef VertexIterator vertex_iterator;
95     typedef unsigned vertices_size_type;
96   };
97     
98   // Concept Graph:
99   inline int null_vertex() { return -1; }
100
101   // Concept IncidenceGraph:
102   inline int source(int f, const Graph&) { return f&VMASK; }
103   inline int target(int f, const Graph&) { return EDGE_END2(f&VMASK, f>>ESHIFT); }
104   inline std::pair<OutEdgeIterator,OutEdgeIterator>
105   out_edges(int v, const Graph&) {
106     return std::make_pair(OutEdgeIterator(v, VE_MIN(v)),
107                           OutEdgeIterator(v, VE_MAX(v)));
108   }
109   inline unsigned out_degree(int v, const Graph&) {
110     return VE_MAX(v) - VE_MIN(v);
111   }
112
113   // Concept VertexListGraph:
114   inline std::pair<VertexIterator,VertexIterator> vertices(const Graph&) {
115     return std::make_pair(VertexIterator(0), VertexIterator(N));
116   }
117   inline unsigned num_vertices(const Graph&) { return N; }
118 };
119
120 static void single_source_shortest_paths(int v1,
121                                          const double edge_weights[/*f*/],
122                                          double vertex_distances[/*v*/]) {
123   Graph g;
124
125   dijkstra_shortest_paths(g, v1,
126      weight_map(edge_weights).
127      vertex_index_map(identity_property_map()).
128      distance_map(vertex_distances));
129 }
130     
131 double graph_layout_cost(const Vertices v, const double vertex_areas[N]) {
132   /* For each (vi,vj) computes shortest path s_ij = |vi..vj|
133    * along edges, and actual distance d_ij = |vi-vj|.
134    *
135    * We will also use the `vertex areas': for each vertex vi the
136    * vertex area a_vi is the mean area of the incident triangles.
137    * This is computed elsewhere.
138    *
139    * Energy contribution is proportional to
140    *
141    *               -4          2
142    *    a  a   .  d   . [ (s/d)  - 1 ]
143    *     vi vj
144    *
145    * (In practice we compute d^2+epsilon and use it for the
146    *  divisions, to avoid division by zero.)
147    */
148   static const double d2_epsilon= 1e-6;
149   
150   double edge_weights[N*V6], vertex_distances[N], total_cost=0;
151   int v1,v2,e,f;
152
153   FOR_VERTEX(v1)
154     FOR_VEDGE_X(v1,e,v2,
155                 f= v1 | e << ESHIFT,
156                 edge_weights[f]= NAN)
157       edge_weights[f]= hypotD(v[v1], v[v2]);
158
159   FOR_VERTEX(v1) {
160     double a1= vertex_areas[v1];
161     single_source_shortest_paths(v1, edge_weights, vertex_distances);
162     FOR_VERTEX(v2) {
163       double a2= vertex_areas[v2];
164       double d2= hypotD2plus(v[v1],v[v2], d2_epsilon);
165       double sd= vertex_distances[v2] / d2;
166       double sd2= sd*sd;
167       total_cost += a1*a2 * (sd2 - 1) / (d2*d2);
168     }
169   }
170   return total_cost;
171 }