author | wenzelm |
Sun, 04 Jan 2015 21:29:52 +0100 | |
changeset 59263 | 03aedb32a763 |
parent 59262 | 5cd92c743958 |
child 59264 | fecf1d5a2454 |
permissions | -rw-r--r-- |
59232 | 1 |
/* Title: Tools/Graphview/layout.scala |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
2 |
Author: Markus Kaiser, TU Muenchen |
59240 | 3 |
Author: Makarius |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
4 |
|
59261 | 5 |
DAG layout algorithm, according to: |
6 |
||
7 |
Georg Sander, "Graph Layout through the VCG Tool", in: Graph Drawing, |
|
8 |
DIMACS International Workshop (GD'94), Springer LNCS 894, 1995. |
|
9 |
||
10 |
http://dx.doi.org/10.1007/3-540-58950-3_371 |
|
11 |
ftp://ftp.cs.uni-sb.de/pub/graphics/vcg/doc/tr-A03-94.ps.gz |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
12 |
*/ |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
13 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
14 |
package isabelle.graphview |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
15 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
16 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
17 |
import isabelle._ |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
18 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
19 |
|
59232 | 20 |
object Layout |
49744 | 21 |
{ |
59262 | 22 |
object Point { val zero: Point = Point(0.0, 0.0) } |
23 |
case class Point(x: Double, y: Double) |
|
50469 | 24 |
|
59262 | 25 |
private type Key = Graph_Display.Node |
26 |
private type Coordinates = Map[Key, Point] |
|
27 |
private type Level = List[Key] |
|
28 |
private type Levels = List[Level] |
|
29 |
||
30 |
val empty = new Layout(Map.empty, Map.empty) |
|
50470 | 31 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
32 |
val pendulum_iterations = 10 |
49737
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
33 |
val minimize_crossings_iterations = 40 |
50469 | 34 |
|
59263 | 35 |
def make(metrics: Visualizer.Metrics, visible_graph: Graph_Display.Graph): Layout = |
49737
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
36 |
{ |
59263 | 37 |
if (visible_graph.is_empty) empty |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
38 |
else { |
59260 | 39 |
def box_width(key: Key): Double = |
40 |
(metrics.string_bounds(key.toString).getWidth + metrics.pad).ceil |
|
41 |
||
59263 | 42 |
val box_distance = (visible_graph.keys_iterator.map(box_width(_)).max + metrics.gap).ceil |
59260 | 43 |
|
44 |
def box_height(level: Level): Double = |
|
45 |
(metrics.char_width * 1.5 * (5 max level.length)).ceil |
|
46 |
||
47 |
||
59263 | 48 |
val initial_levels = level_map(visible_graph) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
49 |
|
49744 | 50 |
val (dummy_graph, dummies, dummy_levels) = |
59263 | 51 |
((visible_graph, Map.empty[(Key, Key), List[Key]], initial_levels) /: |
52 |
visible_graph.edges_iterator) { |
|
53 |
case ((graph, dummies, levels), (from, to)) => |
|
54 |
if (levels(to) - levels(from) <= 1) (graph, dummies, levels) |
|
55 |
else { |
|
56 |
val (graph1, ds, levels1) = add_dummies(graph, from, to, levels) |
|
57 |
(graph1, dummies + ((from, to) -> ds), levels1) |
|
58 |
} |
|
59 |
} |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
60 |
|
50469 | 61 |
val levels = minimize_crossings(dummy_graph, level_list(dummy_levels)) |
62 |
||
50474
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50470
diff
changeset
|
63 |
val initial_coordinates: Coordinates = |
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50470
diff
changeset
|
64 |
(((Map.empty[Key, Point], 0.0) /: levels) { |
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50470
diff
changeset
|
65 |
case ((coords1, y), level) => |
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50470
diff
changeset
|
66 |
((((coords1, 0.0) /: level) { |
59262 | 67 |
case ((coords2, x), key) => (coords2 + (key -> Point(x, y)), x + box_distance) |
59260 | 68 |
})._1, y + box_height(level)) |
50474
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50470
diff
changeset
|
69 |
})._1 |
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50470
diff
changeset
|
70 |
|
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50470
diff
changeset
|
71 |
val coords = pendulum(dummy_graph, box_distance, levels, initial_coordinates) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
72 |
|
49737
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
73 |
val dummy_coords = |
50467 | 74 |
(Map.empty[(Key, Key), List[Point]] /: dummies.keys) { |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
75 |
case (map, key) => map + (key -> dummies(key).map(coords(_))) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
76 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
77 |
|
59262 | 78 |
new Layout(coords, dummy_coords) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
79 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
80 |
} |
49737
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
81 |
|
49744 | 82 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
83 |
def add_dummies(graph: Graph_Display.Graph, from: Key, to: Key, levels: Map[Key, Int]) |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
84 |
: (Graph_Display.Graph, List[Key], Map[Key, Int]) = |
49744 | 85 |
{ |
86 |
val ds = |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
87 |
((levels(from) + 1) until levels(to)).map(l => { |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
88 |
// FIXME !? |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
89 |
val ident = "%s$%s$%d".format(from.ident, to.ident, l) |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
90 |
Graph_Display.Node(ident, ident) |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
91 |
}).toList |
49744 | 92 |
|
93 |
val ls = |
|
94 |
(levels /: ((levels(from) + 1) until levels(to)).zip(ds)) { |
|
95 |
case (ls, (l, d)) => ls + (d -> l) |
|
96 |
} |
|
97 |
||
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
98 |
val graph1 = (graph /: ds)(_.new_node(_, Nil)) |
49744 | 99 |
val graph2 = |
100 |
(graph1.del_edge(from, to) /: (from :: ds ::: List(to)).sliding(2)) { |
|
101 |
case (g, List(x, y)) => g.add_edge(x, y) |
|
102 |
} |
|
103 |
(graph2, ds, ls) |
|
104 |
} |
|
105 |
||
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
106 |
def level_map(graph: Graph_Display.Graph): Map[Key, Int] = |
50467 | 107 |
(Map.empty[Key, Int] /: graph.topological_order) { |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
108 |
(levels, key) => { |
49737
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
109 |
val lev = 1 + (-1 /: graph.imm_preds(key)) { case (m, key) => m max levels(key) } |
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
110 |
levels + (key -> lev) |
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
111 |
} |
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
112 |
} |
50469 | 113 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
114 |
def level_list(map: Map[Key, Int]): Levels = |
49737
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
115 |
{ |
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
116 |
val max_lev = (-1 /: map) { case (m, (_, l)) => m max l } |
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
117 |
val buckets = new Array[Level](max_lev + 1) |
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
118 |
for (l <- 0 to max_lev) { buckets(l) = Nil } |
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
119 |
for ((key, l) <- map) { buckets(l) = key :: buckets(l) } |
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
120 |
buckets.iterator.map(_.sorted(Graph_Display.Node.Ordering)).toList |
49737
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
121 |
} |
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
122 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
123 |
def count_crossings(graph: Graph_Display.Graph, levels: Levels): Int = |
49746 | 124 |
{ |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
125 |
def in_level(ls: Levels): Int = ls match { |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
126 |
case List(top, bot) => |
49746 | 127 |
top.iterator.zipWithIndex.map { |
128 |
case (outer_parent, outer_parent_index) => |
|
129 |
graph.imm_succs(outer_parent).iterator.map(bot.indexOf(_)) |
|
130 |
.map(outer_child => |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
131 |
(0 until outer_parent_index) |
49746 | 132 |
.map(inner_parent => |
133 |
graph.imm_succs(top(inner_parent)).iterator.map(bot.indexOf(_)) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
134 |
.filter(inner_child => outer_child < inner_child) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
135 |
.size |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
136 |
).sum |
49746 | 137 |
).sum |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
138 |
}.sum |
49746 | 139 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
140 |
case _ => 0 |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
141 |
} |
49746 | 142 |
|
143 |
levels.iterator.sliding(2).map(ls => in_level(ls.toList)).sum |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
144 |
} |
50469 | 145 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
146 |
def minimize_crossings(graph: Graph_Display.Graph, levels: Levels): Levels = |
49745 | 147 |
{ |
50469 | 148 |
def resort_level(parent: Level, child: Level, top_down: Boolean): Level = |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
149 |
child.map(k => { |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
150 |
val ps = if (top_down) graph.imm_preds(k) else graph.imm_succs(k) |
50469 | 151 |
val weight = |
50468 | 152 |
(0.0 /: ps) { (w, p) => w + (0 max parent.indexOf(p)) } / (ps.size max 1) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
153 |
(k, weight) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
154 |
}).sortBy(_._2).map(_._1) |
50469 | 155 |
|
59263 | 156 |
def resort(levels: Levels, top_down: Boolean) = |
157 |
if (top_down) |
|
158 |
(List(levels.head) /: levels.tail)((tops, bot) => |
|
159 |
resort_level(tops.head, bot, top_down) :: tops).reverse |
|
160 |
else { |
|
161 |
val rev_levels = levels.reverse |
|
162 |
(List(rev_levels.head) /: rev_levels.tail)((bots, top) => |
|
163 |
resort_level(bots.head, top, top_down) :: bots) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
164 |
} |
50469 | 165 |
|
49737
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
166 |
((levels, count_crossings(graph, levels), true) /: (1 to minimize_crossings_iterations)) { |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
167 |
case ((old_levels, old_crossings, top_down), _) => { |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
168 |
val new_levels = resort(old_levels, top_down) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
169 |
val new_crossings = count_crossings(graph, new_levels) |
49737
dd6fc7c9504a
avoid somewhat heavy rebuilding of SortedMap via map where plain iteration is sufficient;
wenzelm
parents:
49565
diff
changeset
|
170 |
if (new_crossings < old_crossings) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
171 |
(new_levels, new_crossings, !top_down) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
172 |
else |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
173 |
(old_levels, old_crossings, !top_down) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
174 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
175 |
}._1 |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
176 |
} |
50469 | 177 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
178 |
def pendulum(graph: Graph_Display.Graph, box_distance: Double, |
50474
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50470
diff
changeset
|
179 |
levels: Levels, coords: Map[Key, Point]): Coordinates = |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
180 |
{ |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
181 |
type Regions = List[List[Region]] |
50469 | 182 |
|
59263 | 183 |
def iteration( |
184 |
coords: Coordinates, regions: Regions, top_down: Boolean): (Coordinates, Regions, Boolean) = |
|
50469 | 185 |
{ |
59263 | 186 |
val (coords1, regions1, moved) = |
187 |
((coords, List.empty[List[Region]], false) /: (if (top_down) regions else regions.reverse)) { |
|
188 |
case ((coords, tops, moved), bot) => |
|
189 |
val bot1 = collapse(coords, bot, top_down) |
|
190 |
val (coords1, moved1) = deflect(coords, bot1, top_down) |
|
191 |
(coords1, bot1 :: tops, moved || moved1) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
192 |
} |
59263 | 193 |
(coords1, regions1.reverse, moved) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
194 |
} |
50469 | 195 |
|
196 |
def collapse(coords: Coordinates, level: List[Region], top_down: Boolean): List[Region] = |
|
197 |
{ |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
198 |
if (level.size <= 1) level |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
199 |
else { |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
200 |
var next = level |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
201 |
var regions_changed = true |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
202 |
while (regions_changed) { |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
203 |
regions_changed = false |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
204 |
for (i <- (next.length to 1)) { |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
205 |
val (r1, r2) = (next(i-1), next(i)) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
206 |
val d1 = r1.deflection(coords, top_down) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
207 |
val d2 = r2.deflection(coords, top_down) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
208 |
|
50469 | 209 |
if (// Do regions touch? |
50474
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50470
diff
changeset
|
210 |
r1.distance(coords, r2) <= box_distance && |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
211 |
// Do they influence each other? |
50469 | 212 |
(d1 <= 0 && d2 < d1 || d2 > 0 && d1 > d2 || d1 > 0 && d2 < 0)) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
213 |
{ |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
214 |
regions_changed = true |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
215 |
r1.nodes = r1.nodes ::: r2.nodes |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
216 |
next = next.filter(next.indexOf(_) != i) |
50469 | 217 |
} |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
218 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
219 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
220 |
next |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
221 |
} |
50469 | 222 |
} |
223 |
||
59263 | 224 |
def deflect( |
225 |
coords: Coordinates, level: List[Region], top_down: Boolean): (Coordinates, Boolean) = |
|
50469 | 226 |
{ |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
227 |
((coords, false) /: (0 until level.length)) { |
59263 | 228 |
case ((coords, moved), i) => |
229 |
val r = level(i) |
|
230 |
val d = r.deflection(coords, top_down) |
|
231 |
val offset = |
|
232 |
{ |
|
233 |
if (i == 0 && d <= 0) d |
|
234 |
else if (i == level.length - 1 && d >= 0) d |
|
235 |
else if (d < 0) { |
|
236 |
val prev = level(i - 1) |
|
237 |
(- (r.distance(coords, prev) - box_distance)) max d |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
238 |
} |
59263 | 239 |
else { |
240 |
val next = level(i + 1) |
|
241 |
(r.distance(coords, next) - box_distance) min d |
|
242 |
} |
|
243 |
} |
|
244 |
(r.move(coords, offset), moved || d != 0) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
245 |
} |
50469 | 246 |
} |
247 |
||
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
248 |
val regions = levels.map(level => level.map(new Region(graph, _))) |
50469 | 249 |
|
59263 | 250 |
((coords, regions, true, true) /: (1 to pendulum_iterations)) { |
251 |
case ((coords, regions, top_down, moved), _) => |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
252 |
if (moved) { |
59263 | 253 |
val (coords1, regions1, m) = iteration(coords, regions, top_down) |
254 |
(coords1, regions1, !top_down, m) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
255 |
} |
59263 | 256 |
else (coords, regions, !top_down, moved) |
257 |
}._1 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
258 |
} |
50469 | 259 |
|
59261 | 260 |
/*This is an auxiliary class which is used by the layout algorithm when |
261 |
calculating coordinates with the "pendulum method". A "region" is a |
|
262 |
group of nodes which "stick together".*/ |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
263 |
private class Region(val graph: Graph_Display.Graph, node: Key) |
50469 | 264 |
{ |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
265 |
var nodes: List[Key] = List(node) |
50469 | 266 |
|
59262 | 267 |
def left(coords: Coordinates): Double = nodes.iterator.map(coords(_).x).min |
268 |
def right(coords: Coordinates): Double = nodes.iterator.map(coords(_).x).max |
|
50469 | 269 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
270 |
def distance(coords: Coordinates, to: Region): Double = |
50468 | 271 |
math.abs(left(coords) - to.left(coords)) min |
272 |
math.abs(right(coords) - to.right(coords)) |
|
50469 | 273 |
|
59257 | 274 |
def deflection(coords: Coordinates, top_down: Boolean): Double = |
275 |
(for { |
|
276 |
k <- nodes.iterator |
|
59262 | 277 |
x = coords(k).x |
59257 | 278 |
as = if (top_down) graph.imm_preds(k) else graph.imm_succs(k) |
59262 | 279 |
} yield as.iterator.map(coords(_).x - x).sum / (as.size max 1)).sum / nodes.length |
50469 | 280 |
|
59262 | 281 |
def move(coords: Coordinates, dx: Double): Coordinates = |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
282 |
(coords /: nodes) { |
50469 | 283 |
case (cs, node) => |
59262 | 284 |
val p = cs(node) |
285 |
cs + (node -> Point(p.x + dx, p.y)) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
286 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
287 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
288 |
} |
59262 | 289 |
|
290 |
final class Layout private( |
|
291 |
nodes: Map[Graph_Display.Node, Layout.Point], |
|
292 |
dummies: Map[Graph_Display.Edge, List[Layout.Point]]) |
|
293 |
{ |
|
294 |
def get_node(node: Graph_Display.Node): Layout.Point = |
|
295 |
nodes.getOrElse(node, Layout.Point.zero) |
|
296 |
||
297 |
def map_node(node: Graph_Display.Node, f: Layout.Point => Layout.Point): Layout = |
|
298 |
nodes.get(node) match { |
|
299 |
case None => this |
|
300 |
case Some(p) => new Layout(nodes + (node -> f(p)), dummies) |
|
301 |
} |
|
302 |
||
303 |
||
304 |
def get_dummies(edge: Graph_Display.Edge): List[Layout.Point] = |
|
305 |
dummies.getOrElse(edge, Nil) |
|
306 |
||
307 |
def map_dummies(edge: Graph_Display.Edge, f: List[Layout.Point] => List[Layout.Point]): Layout = |
|
308 |
dummies.get(edge) match { |
|
309 |
case None => this |
|
310 |
case Some(ds) => new Layout(nodes, dummies + (edge -> f(ds))) |
|
311 |
} |
|
312 |
} |
|
313 |