[10649] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[9970] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
| 26 |
|
---|
[10520] | 27 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
| 28 | public class ReingoldTilfordLayoutEngine<T> : ILayoutEngine<T> where T : class {
|
---|
| 29 | public int NodeWidth { get; set; }
|
---|
| 30 | public int NodeHeight { get; set; }
|
---|
| 31 | private int minHorizontalSpacing = 5;
|
---|
| 32 | public int HorizontalSpacing {
|
---|
| 33 | get { return minHorizontalSpacing; }
|
---|
| 34 | set { minHorizontalSpacing = value; }
|
---|
| 35 | }
|
---|
[9970] | 36 |
|
---|
[10520] | 37 | private int minVerticalSpacing = 5;
|
---|
| 38 | public int VerticalSpacing {
|
---|
| 39 | get { return minVerticalSpacing; }
|
---|
| 40 | set { minVerticalSpacing = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[10649] | 43 | private readonly Func<T, IEnumerable<T>> GetChildren;
|
---|
[10520] | 44 |
|
---|
[10649] | 45 | public ReingoldTilfordLayoutEngine(Func<T, IEnumerable<T>> GetChildren) {
|
---|
| 46 | this.GetChildren = GetChildren;
|
---|
[9970] | 47 | }
|
---|
| 48 |
|
---|
[10649] | 49 | public IEnumerable<VisualTreeNode<T>> CalculateLayout(T root) {
|
---|
| 50 | return CalculateLayout(root, 0, 0);
|
---|
[10520] | 51 | }
|
---|
[9970] | 52 |
|
---|
[10649] | 53 | public IEnumerable<VisualTreeNode<T>> CalculateLayout(T root, float width, float height) {
|
---|
| 54 | Dictionary<T, LayoutNode<T>> layoutNodeMap = new Dictionary<T, LayoutNode<T>>();
|
---|
| 55 | var layoutRoot = new LayoutNode<T> { Content = root, Width = NodeWidth, Height = NodeHeight, };
|
---|
| 56 | layoutRoot.Ancestor = layoutRoot;
|
---|
| 57 | Expand(layoutRoot, layoutNodeMap);
|
---|
| 58 |
|
---|
| 59 | FirstWalk(layoutRoot);
|
---|
| 60 | SecondWalk(layoutRoot, -layoutRoot.Prelim);
|
---|
| 61 | NormalizeCoordinates(layoutNodeMap.Values);
|
---|
| 62 | if (height != 0 && width != 0) {
|
---|
| 63 | FitToBounds(width, height, layoutNodeMap.Values);
|
---|
| 64 | Center(width, height, layoutNodeMap.Values);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | return layoutNodeMap.Values.Select(x => new VisualTreeNode<T>(x.Content) {
|
---|
| 68 | Width = (int)Math.Round(x.Width),
|
---|
| 69 | Height = (int)Math.Round(x.Height),
|
---|
| 70 | X = (int)Math.Round(x.X),
|
---|
| 71 | Y = (int)Math.Round(x.Y)
|
---|
| 72 | });
|
---|
[10520] | 73 | }
|
---|
| 74 |
|
---|
[10649] | 75 | private void Expand(LayoutNode<T> lRoot, Dictionary<T, LayoutNode<T>> map) {
|
---|
| 76 | map.Add(lRoot.Content, lRoot);
|
---|
[10520] | 77 | var children = GetChildren(lRoot.Content).ToList();
|
---|
| 78 | if (!children.Any()) return;
|
---|
| 79 | lRoot.Children = new List<LayoutNode<T>>(children.Count);
|
---|
| 80 | for (int i = 0; i < children.Count; ++i) {
|
---|
| 81 | var node = new LayoutNode<T> {
|
---|
| 82 | Content = children[i],
|
---|
| 83 | Number = i,
|
---|
| 84 | Parent = lRoot,
|
---|
| 85 | Level = lRoot.Level + 1,
|
---|
| 86 | Width = NodeWidth,
|
---|
| 87 | Height = NodeHeight
|
---|
| 88 | };
|
---|
| 89 | node.Ancestor = node;
|
---|
| 90 | lRoot.Children.Add(node);
|
---|
[10649] | 91 | Expand(node, map);
|
---|
[10520] | 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
[9970] | 96 | /// <summary>
|
---|
[10520] | 97 | /// Transform LayoutNode coordinates so that all coordinates are positive and start from (0,0)
|
---|
[9970] | 98 | /// </summary>
|
---|
[10649] | 99 | private static void NormalizeCoordinates(IEnumerable<LayoutNode<T>> nodes) {
|
---|
[9970] | 100 | float xmin = 0, ymin = 0;
|
---|
[10520] | 101 | foreach (var node in nodes) {
|
---|
| 102 | if (xmin > node.X) xmin = node.X;
|
---|
| 103 | if (ymin > node.Y) ymin = node.Y;
|
---|
[9970] | 104 | }
|
---|
[10520] | 105 | foreach (var node in nodes) {
|
---|
| 106 | node.X -= xmin;
|
---|
| 107 | node.Y -= ymin;
|
---|
[9970] | 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[10649] | 111 | private void Center(float width, float height, IEnumerable<LayoutNode<T>> nodes) {
|
---|
[10520] | 112 | // center layout on screen
|
---|
[10649] | 113 | var bounds = Bounds(nodes);
|
---|
[10520] | 114 | float dx = 0, dy = 0;
|
---|
| 115 | if (width > bounds.Width) { dx = (width - bounds.Width) / 2f; }
|
---|
| 116 | if (height > bounds.Height) { dy = (height - bounds.Height) / 2f; }
|
---|
[10649] | 117 | foreach (var node in nodes) { node.Translate(dx, dy); }
|
---|
[10520] | 118 | }
|
---|
| 119 |
|
---|
[10649] | 120 | private void FitToBounds(float width, float height, IEnumerable<LayoutNode<T>> nodes) {
|
---|
| 121 | var bounds = Bounds(nodes);
|
---|
[10520] | 122 | var myWidth = bounds.Width;
|
---|
| 123 | var myHeight = bounds.Height;
|
---|
| 124 |
|
---|
| 125 | if (myWidth <= width && myHeight <= height) return; // no need to fit since we are within bounds
|
---|
| 126 |
|
---|
[10649] | 127 | var layers = nodes.GroupBy(node => node.Level, node => node).ToList();
|
---|
[10520] | 128 |
|
---|
| 129 | if (myWidth > width) {
|
---|
| 130 | // need to scale horizontally
|
---|
| 131 | float x = width / myWidth;
|
---|
| 132 | foreach (var node in layers.SelectMany(g => g)) {
|
---|
| 133 | node.X *= x;
|
---|
| 134 | node.Width *= x;
|
---|
| 135 | }
|
---|
| 136 | float spacing = minHorizontalSpacing * x;
|
---|
| 137 | foreach (var layer in layers) {
|
---|
[10649] | 138 | var nodesLayer = layer.ToList();
|
---|
[10520] | 139 | float minWidth = float.MaxValue;
|
---|
[10649] | 140 | for (int i = 0; i < nodesLayer.Count - 1; ++i) { minWidth = Math.Min(minWidth, nodesLayer[i + 1].X - nodesLayer[i].X); }
|
---|
[10520] | 141 | float w = Math.Min(NodeWidth, minWidth - spacing);
|
---|
[10649] | 142 | foreach (var node in nodesLayer) {
|
---|
[10520] | 143 | node.X += (node.Width - w) / 2f;
|
---|
| 144 | node.Width = w;
|
---|
| 145 | //this is a simple solution to ensure that the leftmost and rightmost nodes are not drawn partially offscreen due to scaling and offset
|
---|
| 146 | //this should work well enough 99.9% of the time with no noticeable visual difference
|
---|
| 147 | if (node.X < 0) {
|
---|
| 148 | node.Width += node.X;
|
---|
| 149 | node.X = 0;
|
---|
| 150 | } else if (node.X + node.Width > width) {
|
---|
| 151 | node.Width = width - node.X;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | if (myHeight > height) {
|
---|
| 157 | // need to scale vertically
|
---|
| 158 | float x = height / myHeight;
|
---|
| 159 | foreach (var node in layers.SelectMany(g => g)) {
|
---|
| 160 | node.Y *= x;
|
---|
| 161 | node.Height *= x;
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[9970] | 166 |
|
---|
| 167 | /// <summary>
|
---|
| 168 | /// Returns the bounding box for this layout. When the layout is normalized, the rectangle should be [0,0,xmin,xmax].
|
---|
| 169 | /// </summary>
|
---|
| 170 | /// <returns></returns>
|
---|
[10649] | 171 | private RectangleF Bounds(IEnumerable<LayoutNode<T>> nodes) {
|
---|
[10520] | 172 | float xmin = 0, xmax = 0, ymin = 0, ymax = 0;
|
---|
[10649] | 173 | foreach (LayoutNode<T> node in nodes) {
|
---|
[10520] | 174 | float x = node.X, y = node.Y;
|
---|
[9970] | 175 | if (xmin > x) xmin = x;
|
---|
| 176 | if (xmax < x) xmax = x;
|
---|
| 177 | if (ymin > y) ymin = y;
|
---|
| 178 | if (ymax < y) ymax = y;
|
---|
| 179 | }
|
---|
[10520] | 180 | return new RectangleF(xmin, ymin, xmax + minHorizontalSpacing + NodeWidth, ymax + minVerticalSpacing + NodeHeight);
|
---|
[9970] | 181 | }
|
---|
| 182 |
|
---|
[10520] | 183 | #region methods specific to the reingold-tilford layout algorithm
|
---|
[10471] | 184 | private void FirstWalk(LayoutNode<T> v) {
|
---|
| 185 | LayoutNode<T> w;
|
---|
[9970] | 186 | if (v.IsLeaf) {
|
---|
| 187 | w = v.LeftSibling;
|
---|
| 188 | if (w != null) {
|
---|
[10520] | 189 | v.Prelim = w.Prelim + minHorizontalSpacing + NodeWidth;
|
---|
[9970] | 190 | }
|
---|
| 191 | } else {
|
---|
| 192 | var defaultAncestor = v.Children[0]; // leftmost child
|
---|
| 193 |
|
---|
| 194 | foreach (var child in v.Children) {
|
---|
| 195 | FirstWalk(child);
|
---|
| 196 | Apportion(child, ref defaultAncestor);
|
---|
| 197 | }
|
---|
| 198 | ExecuteShifts(v);
|
---|
| 199 | var leftmost = v.Children.First();
|
---|
| 200 | var rightmost = v.Children.Last();
|
---|
| 201 | float midPoint = (leftmost.Prelim + rightmost.Prelim) / 2;
|
---|
| 202 | w = v.LeftSibling;
|
---|
| 203 | if (w != null) {
|
---|
[10520] | 204 | v.Prelim = w.Prelim + minHorizontalSpacing + NodeWidth;
|
---|
[9970] | 205 | v.Mod = v.Prelim - midPoint;
|
---|
| 206 | } else {
|
---|
| 207 | v.Prelim = midPoint;
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[10471] | 212 | private void SecondWalk(LayoutNode<T> v, float m) {
|
---|
[9970] | 213 | v.X = v.Prelim + m;
|
---|
[10520] | 214 | v.Y = v.Level * (minVerticalSpacing + NodeHeight);
|
---|
[9970] | 215 | if (v.IsLeaf) return;
|
---|
| 216 | foreach (var child in v.Children) {
|
---|
| 217 | SecondWalk(child, m + v.Mod);
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[10471] | 221 | private void Apportion(LayoutNode<T> v, ref LayoutNode<T> defaultAncestor) {
|
---|
[9970] | 222 | var w = v.LeftSibling;
|
---|
| 223 | if (w == null) return;
|
---|
[10471] | 224 | LayoutNode<T> vip = v;
|
---|
| 225 | LayoutNode<T> vop = v;
|
---|
| 226 | LayoutNode<T> vim = w;
|
---|
| 227 | LayoutNode<T> vom = vip.LeftmostSibling;
|
---|
[9970] | 228 |
|
---|
| 229 | float sip = vip.Mod;
|
---|
| 230 | float sop = vop.Mod;
|
---|
| 231 | float sim = vim.Mod;
|
---|
| 232 | float som = vom.Mod;
|
---|
| 233 |
|
---|
| 234 | while (vim.NextRight != null && vip.NextLeft != null) {
|
---|
| 235 | vim = vim.NextRight;
|
---|
| 236 | vip = vip.NextLeft;
|
---|
| 237 | vom = vom.NextLeft;
|
---|
| 238 | vop = vop.NextRight;
|
---|
| 239 | vop.Ancestor = v;
|
---|
[10520] | 240 | float shift = (vim.Prelim + sim) - (vip.Prelim + sip) + minHorizontalSpacing + NodeWidth;
|
---|
[9970] | 241 | if (shift > 0) {
|
---|
| 242 | var ancestor = Ancestor(vim, v) ?? defaultAncestor;
|
---|
| 243 | MoveSubtree(ancestor, v, shift);
|
---|
| 244 | sip += shift;
|
---|
| 245 | sop += shift;
|
---|
| 246 | }
|
---|
| 247 | sim += vim.Mod;
|
---|
| 248 | sip += vip.Mod;
|
---|
| 249 | som += vom.Mod;
|
---|
| 250 | sop += vop.Mod;
|
---|
| 251 | }
|
---|
| 252 | if (vim.NextRight != null && vop.NextRight == null) {
|
---|
| 253 | vop.Thread = vim.NextRight;
|
---|
| 254 | vop.Mod += (sim - sop);
|
---|
| 255 | }
|
---|
| 256 | if (vip.NextLeft != null && vom.NextLeft == null) {
|
---|
| 257 | vom.Thread = vip.NextLeft;
|
---|
| 258 | vom.Mod += (sip - som);
|
---|
| 259 | defaultAncestor = v;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[10471] | 263 | private void MoveSubtree(LayoutNode<T> wm, LayoutNode<T> wp, float shift) {
|
---|
| 264 | 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)
|
---|
| 265 | if (subtrees == 0) throw new Exception("MoveSubtree failed: check if object is really a tree (no cycles)");
|
---|
[9970] | 266 | wp.Change -= shift / subtrees;
|
---|
| 267 | wp.Shift += shift;
|
---|
| 268 | wm.Change += shift / subtrees;
|
---|
| 269 | wp.Prelim += shift;
|
---|
| 270 | wp.Mod += shift;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[10471] | 273 | private void ExecuteShifts(LayoutNode<T> v) {
|
---|
[9970] | 274 | if (v.IsLeaf) return;
|
---|
| 275 | float shift = 0;
|
---|
| 276 | float change = 0;
|
---|
| 277 | for (int i = v.Children.Count - 1; i >= 0; --i) {
|
---|
| 278 | var w = v.Children[i];
|
---|
| 279 | w.Prelim += shift;
|
---|
| 280 | w.Mod += shift;
|
---|
| 281 | change += w.Change;
|
---|
| 282 | shift += (w.Shift + change);
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[10471] | 286 | private LayoutNode<T> Ancestor(LayoutNode<T> u, LayoutNode<T> v) {
|
---|
[9970] | 287 | var ancestor = u.Ancestor;
|
---|
| 288 | if (ancestor == null) return null;
|
---|
| 289 | return ancestor.Parent == v.Parent ? ancestor : null;
|
---|
| 290 | }
|
---|
[10520] | 291 | #endregion
|
---|
[9970] | 292 | }
|
---|
| 293 | }
|
---|