1 |
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Linq;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
8 | public class ReingoldTilfordLayoutEngine<T> : ILayoutEngine<T> where T : class {
|
---|
9 | private readonly Dictionary<T, LayoutNode<T>> nodeMap; // provides a reverse mapping T => LayoutNode
|
---|
10 | public int NodeWidth { get; set; }
|
---|
11 | public int NodeHeight { get; set; }
|
---|
12 | private int minHorizontalSpacing = 5;
|
---|
13 | public int HorizontalSpacing {
|
---|
14 | get { return minHorizontalSpacing; }
|
---|
15 | set { minHorizontalSpacing = value; }
|
---|
16 | }
|
---|
17 |
|
---|
18 | private int minVerticalSpacing = 5;
|
---|
19 | public int VerticalSpacing {
|
---|
20 | get { return minVerticalSpacing; }
|
---|
21 | set { minVerticalSpacing = value; }
|
---|
22 | }
|
---|
23 |
|
---|
24 | public Func<T, IEnumerable<T>> GetChildren { get; set; }
|
---|
25 | public Func<T, int> GetLength { get; set; }
|
---|
26 | public Func<T, int> GetDepth { get; set; }
|
---|
27 | private LayoutNode<T> layoutRoot;
|
---|
28 |
|
---|
29 | public ReingoldTilfordLayoutEngine() {
|
---|
30 | nodeMap = new Dictionary<T, LayoutNode<T>>();
|
---|
31 | }
|
---|
32 |
|
---|
33 | public ReingoldTilfordLayoutEngine(T root, Func<T, IEnumerable<T>> childrenFunc)
|
---|
34 | : this() {
|
---|
35 | Initialize(root, childrenFunc);
|
---|
36 | }
|
---|
37 |
|
---|
38 | public void Initialize(T root, Func<T, IEnumerable<T>> getChildren, Func<T, int> getLength = null, Func<T, int> getDepth = null) {
|
---|
39 | GetChildren = getChildren;
|
---|
40 | Clear();
|
---|
41 | var node = new LayoutNode<T> { Content = root, Width = NodeWidth, Height = NodeHeight };
|
---|
42 | node.Ancestor = node;
|
---|
43 | layoutRoot = node;
|
---|
44 | Expand(node);
|
---|
45 | }
|
---|
46 |
|
---|
47 | private void Expand(LayoutNode<T> lRoot) {
|
---|
48 | nodeMap.Add(lRoot.Content, lRoot);
|
---|
49 | var children = GetChildren(lRoot.Content).ToList();
|
---|
50 | if (!children.Any()) return;
|
---|
51 | lRoot.Children = new List<LayoutNode<T>>(children.Count);
|
---|
52 | for (int i = 0; i < children.Count; ++i) {
|
---|
53 | var node = new LayoutNode<T> {
|
---|
54 | Content = children[i],
|
---|
55 | Number = i,
|
---|
56 | Parent = lRoot,
|
---|
57 | Level = lRoot.Level + 1,
|
---|
58 | Width = NodeWidth,
|
---|
59 | Height = NodeHeight
|
---|
60 | };
|
---|
61 | node.Ancestor = node;
|
---|
62 | lRoot.Children.Add(node);
|
---|
63 | Expand(node);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public IEnumerable<VisualTreeNode<T>> GetVisualNodes() {
|
---|
68 | return nodeMap.Values.Select(x => new VisualTreeNode<T>(x.Content) {
|
---|
69 | Width = (int)Math.Round(x.Width),
|
---|
70 | Height = (int)Math.Round(x.Height),
|
---|
71 | X = (int)Math.Round(x.X),
|
---|
72 | Y = (int)Math.Round(x.Y)
|
---|
73 | });
|
---|
74 | }
|
---|
75 |
|
---|
76 | public IEnumerable<LayoutNode<T>> GetLayoutNodes() {
|
---|
77 | return nodeMap.Values;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void AddNode(T content) {
|
---|
81 | if (nodeMap.ContainsKey(content)) { throw new ArgumentException("Content already present in the dictionary."); }
|
---|
82 | var node = new LayoutNode<T> { Content = content };
|
---|
83 | nodeMap.Add(content, node);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void AddNode(LayoutNode<T> node) {
|
---|
87 | var content = node.Content;
|
---|
88 | if (nodeMap.ContainsKey(content)) { throw new ArgumentException("Content already present in the dictionary."); }
|
---|
89 | nodeMap.Add(content, node);
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void AddNodes(IEnumerable<LayoutNode<T>> nodes) {
|
---|
93 | foreach (var node in nodes)
|
---|
94 | nodeMap.Add(node.Content, node);
|
---|
95 | }
|
---|
96 |
|
---|
97 | public LayoutNode<T> GetNode(T content) {
|
---|
98 | LayoutNode<T> layoutNode;
|
---|
99 | nodeMap.TryGetValue(content, out layoutNode);
|
---|
100 | return layoutNode;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void ResetCoordinates() {
|
---|
104 | foreach (var node in nodeMap.Values) {
|
---|
105 | node.ResetCoordinates();
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public Dictionary<T, PointF> GetCoordinates() {
|
---|
110 | return nodeMap.ToDictionary(x => x.Key, x => new PointF(x.Value.X, x.Value.Y));
|
---|
111 | }
|
---|
112 |
|
---|
113 | /// <summary>
|
---|
114 | /// Transform LayoutNode coordinates so that all coordinates are positive and start from (0,0)
|
---|
115 | /// </summary>
|
---|
116 | private void NormalizeCoordinates() {
|
---|
117 | var nodes = nodeMap.Values.ToList();
|
---|
118 | float xmin = 0, ymin = 0;
|
---|
119 | foreach (var node in nodes) {
|
---|
120 | if (xmin > node.X) xmin = node.X;
|
---|
121 | if (ymin > node.Y) ymin = node.Y;
|
---|
122 | }
|
---|
123 | foreach (var node in nodes) {
|
---|
124 | node.X -= xmin;
|
---|
125 | node.Y -= ymin;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void Center(float width, float height) {
|
---|
130 | // center layout on screen
|
---|
131 | var bounds = Bounds();
|
---|
132 | float dx = 0, dy = 0;
|
---|
133 | if (width > bounds.Width) { dx = (width - bounds.Width) / 2f; }
|
---|
134 | if (height > bounds.Height) { dy = (height - bounds.Height) / 2f; }
|
---|
135 | foreach (var node in nodeMap.Values) { node.Translate(dx, dy); }
|
---|
136 | }
|
---|
137 |
|
---|
138 | public void FitToBounds(float width, float height) {
|
---|
139 | var bounds = Bounds();
|
---|
140 | var myWidth = bounds.Width;
|
---|
141 | var myHeight = bounds.Height;
|
---|
142 |
|
---|
143 | if (myWidth <= width && myHeight <= height) return; // no need to fit since we are within bounds
|
---|
144 |
|
---|
145 | var layers = nodeMap.Values.GroupBy(node => node.Level, node => node).ToList();
|
---|
146 |
|
---|
147 | if (myWidth > width) {
|
---|
148 | // need to scale horizontally
|
---|
149 | float x = width / myWidth;
|
---|
150 | foreach (var node in layers.SelectMany(g => g)) {
|
---|
151 | node.X *= x;
|
---|
152 | node.Width *= x;
|
---|
153 | }
|
---|
154 | float spacing = minHorizontalSpacing * x;
|
---|
155 | foreach (var layer in layers) {
|
---|
156 | var nodes = layer.ToList();
|
---|
157 | float minWidth = float.MaxValue;
|
---|
158 | for (int i = 0; i < nodes.Count - 1; ++i) { minWidth = Math.Min(minWidth, nodes[i + 1].X - nodes[i].X); }
|
---|
159 | float w = Math.Min(NodeWidth, minWidth - spacing);
|
---|
160 | foreach (var node in nodes) {
|
---|
161 | node.X += (node.Width - w) / 2f;
|
---|
162 | node.Width = w;
|
---|
163 | //this is a simple solution to ensure that the leftmost and rightmost nodes are not drawn partially offscreen due to scaling and offset
|
---|
164 | //this should work well enough 99.9% of the time with no noticeable visual difference
|
---|
165 | if (node.X < 0) {
|
---|
166 | node.Width += node.X;
|
---|
167 | node.X = 0;
|
---|
168 | } else if (node.X + node.Width > width) {
|
---|
169 | node.Width = width - node.X;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 | if (myHeight > height) {
|
---|
175 | // need to scale vertically
|
---|
176 | float x = height / myHeight;
|
---|
177 | foreach (var node in layers.SelectMany(g => g)) {
|
---|
178 | node.Y *= x;
|
---|
179 | node.Height *= x;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | public void Clear() {
|
---|
185 | layoutRoot = null;
|
---|
186 | nodeMap.Clear();
|
---|
187 | }
|
---|
188 |
|
---|
189 | public void Reset() {
|
---|
190 | foreach (var layoutNode in nodeMap.Values) {
|
---|
191 | // reset layout-related parameters
|
---|
192 | layoutNode.Reset();
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | public void CalculateLayout() {
|
---|
197 | if (layoutRoot == null) throw new Exception("Layout layoutRoot cannot be null.");
|
---|
198 | Reset(); // reset node parameters like Mod, Shift etc. and set coordinates to 0
|
---|
199 | FirstWalk(layoutRoot);
|
---|
200 | SecondWalk(layoutRoot, -layoutRoot.Prelim);
|
---|
201 | NormalizeCoordinates();
|
---|
202 | }
|
---|
203 |
|
---|
204 | public void CalculateLayout(float width, float height) {
|
---|
205 | CalculateLayout();
|
---|
206 | FitToBounds(width, height);
|
---|
207 | Center(width, height);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /// <summary>
|
---|
211 | /// Returns the bounding box for this layout. When the layout is normalized, the rectangle should be [0,0,xmin,xmax].
|
---|
212 | /// </summary>
|
---|
213 | /// <returns></returns>
|
---|
214 | public RectangleF Bounds() {
|
---|
215 | float xmin = 0, xmax = 0, ymin = 0, ymax = 0;
|
---|
216 | var list = nodeMap.Values.ToList();
|
---|
217 | foreach (LayoutNode<T> node in list) {
|
---|
218 | float x = node.X, y = node.Y;
|
---|
219 | if (xmin > x) xmin = x;
|
---|
220 | if (xmax < x) xmax = x;
|
---|
221 | if (ymin > y) ymin = y;
|
---|
222 | if (ymax < y) ymax = y;
|
---|
223 | }
|
---|
224 | return new RectangleF(xmin, ymin, xmax + minHorizontalSpacing + NodeWidth, ymax + minVerticalSpacing + NodeHeight);
|
---|
225 | }
|
---|
226 |
|
---|
227 | #region methods specific to the reingold-tilford layout algorithm
|
---|
228 | private void FirstWalk(LayoutNode<T> v) {
|
---|
229 | LayoutNode<T> w;
|
---|
230 | if (v.IsLeaf) {
|
---|
231 | w = v.LeftSibling;
|
---|
232 | if (w != null) {
|
---|
233 | v.Prelim = w.Prelim + minHorizontalSpacing + NodeWidth;
|
---|
234 | }
|
---|
235 | } else {
|
---|
236 | var defaultAncestor = v.Children[0]; // leftmost child
|
---|
237 |
|
---|
238 | foreach (var child in v.Children) {
|
---|
239 | FirstWalk(child);
|
---|
240 | Apportion(child, ref defaultAncestor);
|
---|
241 | }
|
---|
242 | ExecuteShifts(v);
|
---|
243 | var leftmost = v.Children.First();
|
---|
244 | var rightmost = v.Children.Last();
|
---|
245 | float midPoint = (leftmost.Prelim + rightmost.Prelim) / 2;
|
---|
246 | w = v.LeftSibling;
|
---|
247 | if (w != null) {
|
---|
248 | v.Prelim = w.Prelim + minHorizontalSpacing + NodeWidth;
|
---|
249 | v.Mod = v.Prelim - midPoint;
|
---|
250 | } else {
|
---|
251 | v.Prelim = midPoint;
|
---|
252 | }
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | private void SecondWalk(LayoutNode<T> v, float m) {
|
---|
257 | v.X = v.Prelim + m;
|
---|
258 | v.Y = v.Level * (minVerticalSpacing + NodeHeight);
|
---|
259 | if (v.IsLeaf) return;
|
---|
260 | foreach (var child in v.Children) {
|
---|
261 | SecondWalk(child, m + v.Mod);
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | private void Apportion(LayoutNode<T> v, ref LayoutNode<T> defaultAncestor) {
|
---|
266 | var w = v.LeftSibling;
|
---|
267 | if (w == null) return;
|
---|
268 | LayoutNode<T> vip = v;
|
---|
269 | LayoutNode<T> vop = v;
|
---|
270 | LayoutNode<T> vim = w;
|
---|
271 | LayoutNode<T> vom = vip.LeftmostSibling;
|
---|
272 |
|
---|
273 | float sip = vip.Mod;
|
---|
274 | float sop = vop.Mod;
|
---|
275 | float sim = vim.Mod;
|
---|
276 | float som = vom.Mod;
|
---|
277 |
|
---|
278 | while (vim.NextRight != null && vip.NextLeft != null) {
|
---|
279 | vim = vim.NextRight;
|
---|
280 | vip = vip.NextLeft;
|
---|
281 | vom = vom.NextLeft;
|
---|
282 | vop = vop.NextRight;
|
---|
283 | vop.Ancestor = v;
|
---|
284 | float shift = (vim.Prelim + sim) - (vip.Prelim + sip) + minHorizontalSpacing + NodeWidth;
|
---|
285 | if (shift > 0) {
|
---|
286 | var ancestor = Ancestor(vim, v) ?? defaultAncestor;
|
---|
287 | MoveSubtree(ancestor, v, shift);
|
---|
288 | sip += shift;
|
---|
289 | sop += shift;
|
---|
290 | }
|
---|
291 | sim += vim.Mod;
|
---|
292 | sip += vip.Mod;
|
---|
293 | som += vom.Mod;
|
---|
294 | sop += vop.Mod;
|
---|
295 | }
|
---|
296 | if (vim.NextRight != null && vop.NextRight == null) {
|
---|
297 | vop.Thread = vim.NextRight;
|
---|
298 | vop.Mod += (sim - sop);
|
---|
299 | }
|
---|
300 | if (vip.NextLeft != null && vom.NextLeft == null) {
|
---|
301 | vom.Thread = vip.NextLeft;
|
---|
302 | vom.Mod += (sip - som);
|
---|
303 | defaultAncestor = v;
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | private void MoveSubtree(LayoutNode<T> wm, LayoutNode<T> wp, float shift) {
|
---|
308 | int subtrees = wp.Number - wm.Number; // TODO: Investigate possible bug (if the value ever happens to be zero) - happens when the tree is actually a graph (but that's outside the use case of this algorithm which only works with trees)
|
---|
309 | if (subtrees == 0) throw new Exception("MoveSubtree failed: check if object is really a tree (no cycles)");
|
---|
310 | wp.Change -= shift / subtrees;
|
---|
311 | wp.Shift += shift;
|
---|
312 | wm.Change += shift / subtrees;
|
---|
313 | wp.Prelim += shift;
|
---|
314 | wp.Mod += shift;
|
---|
315 | }
|
---|
316 |
|
---|
317 | private void ExecuteShifts(LayoutNode<T> v) {
|
---|
318 | if (v.IsLeaf) return;
|
---|
319 | float shift = 0;
|
---|
320 | float change = 0;
|
---|
321 | for (int i = v.Children.Count - 1; i >= 0; --i) {
|
---|
322 | var w = v.Children[i];
|
---|
323 | w.Prelim += shift;
|
---|
324 | w.Mod += shift;
|
---|
325 | change += w.Change;
|
---|
326 | shift += (w.Shift + change);
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | private LayoutNode<T> Ancestor(LayoutNode<T> u, LayoutNode<T> v) {
|
---|
331 | var ancestor = u.Ancestor;
|
---|
332 | if (ancestor == null) return null;
|
---|
333 | return ancestor.Parent == v.Parent ? ancestor : null;
|
---|
334 | }
|
---|
335 | #endregion
|
---|
336 | }
|
---|
337 | }
|
---|