Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs @ 12336

Last change on this file since 12336 was 12155, checked in by bburlacu, 10 years ago

#1772: Merged trunk changes.

File size: 18.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Drawing.Imaging;
26using System.IO;
27using System.Linq;
28using System.Windows.Forms;
29
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
32  public partial class SymbolicExpressionTreeChart : UserControl {
33    private Image image;
34    private readonly StringFormat stringFormat;
35    private Dictionary<ISymbolicExpressionTreeNode, VisualTreeNode<ISymbolicExpressionTreeNode>> visualTreeNodes;
36    private Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection> visualLines;
37    private ILayoutEngine<ISymbolicExpressionTreeNode> layoutEngine;
38
39    private const int preferredNodeWidth = 70;
40    private const int preferredNodeHeight = 46;
41    private int minHorizontalDistance = 30;
42    private int minVerticalDistance = 30;
43
44    public SymbolicExpressionTreeChart() {
45      InitializeComponent();
46      this.image = new Bitmap(Width, Height);
47      this.stringFormat = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
48      this.spacing = 5;
49      this.lineColor = Color.Black;
50      this.backgroundColor = Color.White;
51      this.textFont = new Font(FontFamily.GenericSansSerif, 8);
52
53      visualTreeNodes = new Dictionary<ISymbolicExpressionTreeNode, VisualTreeNode<ISymbolicExpressionTreeNode>>();
54      visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection>();
55
56      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
57        NodeWidth = preferredNodeWidth,
58        NodeHeight = preferredNodeHeight,
59        HorizontalSpacing = minHorizontalDistance,
60        VerticalSpacing = minVerticalDistance
61      };
62      reingoldTilfordToolStripMenuItem.Checked = true;
63    }
64
65    public SymbolicExpressionTreeChart(ISymbolicExpressionTree tree)
66      : this() {
67      this.Tree = tree;
68    }
69
70    #region Public properties
71    private int spacing;
72    public int Spacing {
73      get { return this.spacing; }
74      set {
75        this.spacing = value;
76        this.Repaint();
77      }
78    }
79
80    private Color lineColor;
81    public Color LineColor {
82      get { return this.lineColor; }
83      set {
84        this.lineColor = value;
85        this.Repaint();
86      }
87    }
88
89    private Color backgroundColor;
90    public Color BackgroundColor {
91      get { return this.backgroundColor; }
92      set {
93        this.backgroundColor = value;
94        this.Repaint();
95      }
96    }
97
98    private Font textFont;
99    public Font TextFont {
100      get { return this.textFont; }
101      set {
102        this.textFont = value;
103        this.Repaint();
104      }
105    }
106
107    private ISymbolicExpressionTree tree;
108    public ISymbolicExpressionTree Tree {
109      get { return this.tree; }
110      set {
111        tree = value;
112        Repaint();
113      }
114    }
115
116    private bool suspendRepaint;
117    public bool SuspendRepaint {
118      get { return suspendRepaint; }
119      set { suspendRepaint = value; }
120    }
121    #endregion
122
123    protected override void OnPaint(PaintEventArgs e) {
124      e.Graphics.DrawImage(image, 0, 0);
125      base.OnPaint(e);
126    }
127    protected override void OnResize(EventArgs e) {
128      base.OnResize(e);
129      if (this.Width <= 1 || this.Height <= 1)
130        this.image = new Bitmap(1, 1);
131      else {
132        this.image = new Bitmap(Width, Height);
133      }
134      this.Repaint();
135    }
136
137    public event EventHandler Repainted;//expose this event to notify the parent control that the tree was repainted
138    protected virtual void OnRepaint(object sender, EventArgs e) {
139      var repainted = Repainted;
140      if (repainted != null) {
141        repainted(sender, e);
142      }
143    }
144
145    public void Repaint() {
146      if (!suspendRepaint) {
147        this.GenerateImage();
148        this.Refresh();
149        OnRepaint(this, EventArgs.Empty);
150      }
151    }
152
153    public void RepaintNodes() {
154      if (!suspendRepaint) {
155        using (var graphics = Graphics.FromImage(image)) {
156          graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
157          graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
158          foreach (var visualNode in visualTreeNodes.Values) {
159            DrawTreeNode(graphics, visualNode);
160            if (visualNode.Content.SubtreeCount > 0) {
161              foreach (var visualSubtree in visualNode.Content.Subtrees.Select(s => visualTreeNodes[s])) {
162                DrawLine(graphics, visualNode, visualSubtree);
163              }
164            }
165          }
166        }
167        this.Refresh();
168      }
169    }
170
171    public void RepaintNode(VisualTreeNode<ISymbolicExpressionTreeNode> visualNode) {
172      if (!suspendRepaint) {
173        using (var graphics = Graphics.FromImage(image)) {
174          graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
175          graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
176          DrawTreeNode(graphics, visualNode);
177        }
178        this.Refresh();
179      }
180    }
181
182    private void GenerateImage() {
183      using (Graphics graphics = Graphics.FromImage(image)) {
184        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
185        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
186        graphics.Clear(backgroundColor);
187        if (tree != null) {
188          DrawFunctionTree(graphics, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
189        }
190      }
191    }
192
193    public VisualTreeNode<ISymbolicExpressionTreeNode> GetVisualSymbolicExpressionTreeNode(ISymbolicExpressionTreeNode symbolicExpressionTreeNode) {
194      if (visualTreeNodes.ContainsKey(symbolicExpressionTreeNode))
195        return visualTreeNodes[symbolicExpressionTreeNode];
196      return null;
197    }
198
199    public VisualTreeNodeConnection GetVisualSymbolicExpressionTreeNodeConnection(ISymbolicExpressionTreeNode parent, ISymbolicExpressionTreeNode child) {
200      if (child.Parent != parent) throw new ArgumentException();
201      var key = Tuple.Create(parent, child);
202      VisualTreeNodeConnection connection = null;
203      visualLines.TryGetValue(key, out connection);
204      return connection;
205    }
206
207    #region events
208    public event MouseEventHandler SymbolicExpressionTreeNodeClicked;
209    protected virtual void OnSymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
210      var clicked = SymbolicExpressionTreeNodeClicked;
211      if (clicked != null)
212        clicked(sender, e);
213    }
214
215    protected virtual void SymbolicExpressionTreeChart_MouseClick(object sender, MouseEventArgs e) {
216      var visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
217      if (visualTreeNode != null) {
218        OnSymbolicExpressionTreeNodeClicked(visualTreeNode, e);
219      }
220    }
221
222    public event MouseEventHandler SymbolicExpressionTreeNodeDoubleClicked;
223    protected virtual void OnSymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
224      var doubleClicked = SymbolicExpressionTreeNodeDoubleClicked;
225      if (doubleClicked != null)
226        doubleClicked(sender, e);
227    }
228
229    protected virtual void SymbolicExpressionTreeChart_MouseDoubleClick(object sender, MouseEventArgs e) {
230      VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
231      if (visualTreeNode != null)
232        OnSymbolicExpressionTreeNodeDoubleClicked(visualTreeNode, e);
233    }
234
235    public event ItemDragEventHandler SymbolicExpressionTreeNodeDrag;
236    protected virtual void OnSymbolicExpressionTreeNodeDragDrag(object sender, ItemDragEventArgs e) {
237      var dragged = SymbolicExpressionTreeNodeDrag;
238      if (dragged != null)
239        dragged(sender, e);
240    }
241
242    private VisualTreeNode<ISymbolicExpressionTreeNode> draggedSymbolicExpressionTree;
243    private MouseButtons dragButtons;
244    private void SymbolicExpressionTreeChart_MouseDown(object sender, MouseEventArgs e) {
245      this.dragButtons = e.Button;
246      this.draggedSymbolicExpressionTree = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
247    }
248    private void SymbolicExpressionTreeChart_MouseUp(object sender, MouseEventArgs e) {
249      this.draggedSymbolicExpressionTree = null;
250      this.dragButtons = MouseButtons.None;
251    }
252
253    private void SymbolicExpressionTreeChart_MouseMove(object sender, MouseEventArgs e) {
254      VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
255      if (draggedSymbolicExpressionTree != null &&
256        draggedSymbolicExpressionTree != visualTreeNode) {
257        OnSymbolicExpressionTreeNodeDragDrag(draggedSymbolicExpressionTree, new ItemDragEventArgs(dragButtons, draggedSymbolicExpressionTree));
258        draggedSymbolicExpressionTree = null;
259      } else if (draggedSymbolicExpressionTree == null &&
260        visualTreeNode != null) {
261        string tooltipText = visualTreeNode.ToolTip;
262        if (this.toolTip.GetToolTip(this) != tooltipText)
263          this.toolTip.SetToolTip(this, tooltipText);
264
265      } else if (visualTreeNode == null)
266        this.toolTip.SetToolTip(this, "");
267    }
268
269    public VisualTreeNode<ISymbolicExpressionTreeNode> FindVisualSymbolicExpressionTreeNodeAt(int x, int y) {
270      foreach (var visualTreeNode in visualTreeNodes.Values) {
271        if (x >= visualTreeNode.X && x <= visualTreeNode.X + visualTreeNode.Width &&
272            y >= visualTreeNode.Y && y <= visualTreeNode.Y + visualTreeNode.Height)
273          return visualTreeNode;
274      }
275      return null;
276    }
277    #endregion
278
279    private void CalculateLayout(int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
280      layoutEngine.NodeWidth = preferredWidth;
281      layoutEngine.NodeHeight = preferredHeight;
282      layoutEngine.HorizontalSpacing = minHDistance;
283      layoutEngine.VerticalSpacing = minVDistance;
284
285      var actualRoot = tree.Root;
286      if (actualRoot.Symbol is ProgramRootSymbol && actualRoot.SubtreeCount == 1) {
287        actualRoot = tree.Root.GetSubtree(0);
288      }
289
290      var visualNodes = layoutEngine.CalculateLayout(actualRoot, Width, Height).ToList();
291      visualTreeNodes = visualNodes.ToDictionary(x => x.Content, x => x);
292      visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection>();
293      foreach (var node in visualNodes.Select(n => n.Content)) {
294        foreach (var subtree in node.Subtrees) {
295          visualLines.Add(new Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(node, subtree), new VisualTreeNodeConnection());
296        }
297      }
298    }
299
300    #region methods for painting the symbolic expression tree
301    private void DrawFunctionTree(Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance, bool recalculateLayout = true) {
302      if (recalculateLayout)
303        CalculateLayout(preferredWidth, preferredHeight, minHDistance, minVDistance);
304
305      var visualNodes = visualTreeNodes.Values;
306      //draw nodes and connections
307      foreach (var visualNode in visualNodes) {
308        DrawTreeNode(graphics, visualNode);
309        var node = visualNode.Content;
310        foreach (var subtree in node.Subtrees) {
311          var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node, subtree);
312          var visualSubtree = visualTreeNodes[subtree];
313          var origin = new Point(visualNode.X + visualNode.Width / 2, visualNode.Y + visualNode.Height);
314          var target = new Point(visualSubtree.X + visualSubtree.Width / 2, visualSubtree.Y);
315          graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
316          using (var linePen = new Pen(visualLine.LineColor)) {
317            linePen.DashStyle = visualLine.DashStyle;
318            graphics.DrawLine(linePen, origin, target);
319          }
320        }
321      }
322    }
323
324    protected void DrawTreeNode(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode) {
325      graphics.Clip = new Region(new Rectangle(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width + 1, visualTreeNode.Height + 1));
326      graphics.Clear(backgroundColor);
327      var node = visualTreeNode.Content;
328      using (var textBrush = new SolidBrush(visualTreeNode.TextColor))
329      using (var nodeLinePen = new Pen(visualTreeNode.LineColor))
330      using (var nodeFillBrush = new SolidBrush(visualTreeNode.FillColor)) {
331        //draw terminal node
332        if (node.SubtreeCount == 0) {
333          graphics.FillRectangle(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
334          graphics.DrawRectangle(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
335        } else {
336          graphics.FillEllipse(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
337          graphics.DrawEllipse(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
338        }
339        //draw name of symbol
340        var text = ShortLabel(node);
341        graphics.DrawString(text, textFont, textBrush, new RectangleF(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height), stringFormat);
342      }
343    }
344
345    protected void DrawLine(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> startNode, VisualTreeNode<ISymbolicExpressionTreeNode> endNode) {
346      var origin = new Point(startNode.X + startNode.Width / 2, startNode.Y + startNode.Height);
347      var target = new Point(endNode.X + endNode.Width / 2, endNode.Y);
348      graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
349      var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(startNode.Content, endNode.Content);
350      using (var linePen = new Pen(visualLine.LineColor)) {
351        linePen.DashStyle = visualLine.DashStyle;
352        graphics.DrawLine(linePen, origin, target);
353      }
354    }
355    #endregion
356    #region save image
357    private void saveImageToolStripMenuItem_Click(object sender, EventArgs e) {
358      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
359        string filename = saveFileDialog.FileName.ToLower();
360        if (filename.EndsWith("bmp")) SaveImageAsBitmap(filename);
361        else if (filename.EndsWith("emf")) SaveImageAsEmf(filename);
362        else SaveImageAsBitmap(filename);
363      }
364    }
365
366    public void SaveImageAsBitmap(string filename) {
367      if (tree == null) return;
368      Image bitmap = new Bitmap(Width, Height);
369      using (var g = Graphics.FromImage(bitmap)) {
370        DrawFunctionTree(g, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance, false);
371      }
372      bitmap.Save(filename);
373    }
374
375    public void SaveImageAsEmf(string filename) {
376      if (tree == null) return;
377      using (Graphics g = CreateGraphics()) {
378        using (Metafile file = new Metafile(filename, g.GetHdc())) {
379          using (Graphics emfFile = Graphics.FromImage(file)) {
380            DrawFunctionTree(emfFile, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance, false);
381          }
382        }
383        g.ReleaseHdc();
384      }
385    }
386    #endregion
387    #region export pgf/tikz
388    private void exportLatexToolStripMenuItem_Click(object sender, EventArgs e) {
389      var t = Tree;
390      if (t == null) return;
391      using (var dialog = new SaveFileDialog { Filter = "Tex (*.tex)|*.tex" }) {
392        if (dialog.ShowDialog() != DialogResult.OK) return;
393        string filename = dialog.FileName.ToLower();
394        var formatter = new SymbolicExpressionTreeLatexFormatter();
395        File.WriteAllText(filename, formatter.Format(t));
396      }
397    }
398    #endregion
399
400    private void reingoldTilfordToolStripMenuItem_Click(object sender, EventArgs e) {
401      minHorizontalDistance = 30;
402      minVerticalDistance = 30;
403      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
404        NodeWidth = preferredNodeWidth,
405        NodeHeight = preferredNodeHeight,
406        HorizontalSpacing = minHorizontalDistance,
407        VerticalSpacing = minVerticalDistance
408      };
409      reingoldTilfordToolStripMenuItem.Checked = true;
410      boxesToolStripMenuItem.Checked = false;
411      Repaint();
412    }
413
414    private void boxesToolStripMenuItem_Click(object sender, EventArgs e) {
415      minHorizontalDistance = 5;
416      minVerticalDistance = 5;
417      layoutEngine = new BoxesLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees, n => n.GetLength(), n => n.GetDepth()) {
418        NodeWidth = preferredNodeWidth,
419        NodeHeight = preferredNodeHeight,
420        HorizontalSpacing = minHorizontalDistance,
421        VerticalSpacing = minVerticalDistance
422      };
423      reingoldTilfordToolStripMenuItem.Checked = false;
424      boxesToolStripMenuItem.Checked = true;
425      Repaint();
426    }
427
428    private static string ShortLabel(ISymbolicExpressionTreeNode node) {
429      var name = node.ToString();
430
431      if (node.SubtreeCount > 0) {
432        switch (name) {
433          case "Addition":
434            return "Add";
435          case "Subtraction":
436            return "Sub";
437          case "Multiplication":
438            return "Mul";
439          case "Division":
440            return "Div";
441          case "Exponential":
442            return "Exp";
443          case "Logarithm":
444            return "Log";
445        }
446      }
447
448      return name;
449    }
450  }
451}
Note: See TracBrowser for help on using the repository browser.