Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/08/14 13:58:54 (11 years ago)
Author:
bburlacu
Message:

#1772: - Added a ViewHost in the right side of the GenealogyGraphView which displays the encoding-specific content when a GenealogyGraphNode is clicked.

  • Migrated new SymbolicExpressionTreeChart (drawing the tree using the ReingoldTilfordLayoutEngine) to the new branch
  • Copied SymbolicDataAnalysisExpressionTreeMatching.cs and SymbolicDataAnalysisExpressionTreeSimilarityCalculator.cs to the new branch
Location:
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.Designer.cs

    r9456 r10302  
    5050      this.saveImageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
    5151      this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
     52      this.exportLatexToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
    5253      this.contextMenuStrip.SuspendLayout();
    5354      this.SuspendLayout();
     
    5657      //
    5758      this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
    58             this.saveImageToolStripMenuItem});
     59            this.saveImageToolStripMenuItem,
     60            this.exportLatexToolStripMenuItem});
    5961      this.contextMenuStrip.Name = "contextMenuStrip";
    60       this.contextMenuStrip.Size = new System.Drawing.Size(135, 26);
     62      this.contextMenuStrip.Size = new System.Drawing.Size(161, 70);
    6163      //
    6264      // saveImageToolStripMenuItem
    6365      //
    6466      this.saveImageToolStripMenuItem.Name = "saveImageToolStripMenuItem";
    65       this.saveImageToolStripMenuItem.Size = new System.Drawing.Size(134, 22);
     67      this.saveImageToolStripMenuItem.Size = new System.Drawing.Size(160, 22);
    6668      this.saveImageToolStripMenuItem.Text = "Save Image";
    6769      this.saveImageToolStripMenuItem.Click += new System.EventHandler(this.saveImageToolStripMenuItem_Click);
     
    7072      //
    7173      this.saveFileDialog.Filter = "Bitmap (*.bmp)|*.bmp|EMF (*.emf)|*.emf";
    72       this.saveFileDialog.FilterIndex = 1;
     74      //
     75      // exportLatexToolStripMenuItem
     76      //
     77      this.exportLatexToolStripMenuItem.Name = "exportLatexToolStripMenuItem";
     78      this.exportLatexToolStripMenuItem.Size = new System.Drawing.Size(160, 22);
     79      this.exportLatexToolStripMenuItem.Text = "Export Pgf/Latex";
     80      this.exportLatexToolStripMenuItem.Click += new System.EventHandler(this.exportLatexToolStripMenuItem_Click);
     81      //
    7382      // SymbolicExpressionTreeChart
    7483      //
    75       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    7684      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
    7785      this.ContextMenuStrip = this.contextMenuStrip;
     86      this.DoubleBuffered = true;
    7887      this.Name = "SymbolicExpressionTreeChart";
    7988      this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.SymbolicExpressionTreeChart_MouseClick);
     
    93102    protected System.Windows.Forms.ToolStripMenuItem saveImageToolStripMenuItem;
    94103    protected System.Windows.Forms.SaveFileDialog saveFileDialog;
     104    private System.Windows.Forms.ToolStripMenuItem exportLatexToolStripMenuItem;
    95105  }
    96106}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs

    r9587 r10302  
    2424using System.Drawing;
    2525using System.Drawing.Imaging;
     26using System.IO;
     27using System.Linq;
    2628using System.Windows.Forms;
     29using Point = System.Drawing.Point;
    2730
    2831namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
     
    3235    private Dictionary<ISymbolicExpressionTreeNode, VisualSymbolicExpressionTreeNode> visualTreeNodes;
    3336    private Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection> visualLines;
     37    private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine;
     38    private readonly SymbolicExpressionTreeLayoutAdapter layoutAdapter;
     39
     40    private const int preferredNodeWidth = 70;
     41    private const int preferredNodeHeight = 46;
     42    private const int minHorizontalDistance = 20;
     43    private const int minVerticalDistance = 20;
     44
    3445
    3546    public SymbolicExpressionTreeChart() {
     
    4051      this.lineColor = Color.Black;
    4152      this.backgroundColor = Color.White;
    42       this.textFont = new Font("Times New Roman", 8);
     53      this.textFont = new Font("Times New Roman", 14);
     54      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>();
     55      layoutAdapter = new SymbolicExpressionTreeLayoutAdapter();
    4356    }
    4457
    4558    public SymbolicExpressionTreeChart(ISymbolicExpressionTree tree)
    4659      : this() {
     60      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>();
     61      layoutAdapter = new SymbolicExpressionTreeLayoutAdapter();
    4762      this.Tree = tree;
    4863    }
    4964
     65    #region Public properties
    5066    private int spacing;
    5167    public int Spacing {
     
    106122      set { suspendRepaint = value; }
    107123    }
     124    #endregion
    108125
    109126    protected override void OnPaint(PaintEventArgs e) {
     
    157174        graphics.Clear(backgroundColor);
    158175        if (tree != null) {
    159           int height = this.Height / tree.Depth;
    160           DrawFunctionTree(tree, graphics, 0, 0, this.Width, height);
     176          DrawFunctionTree(tree, graphics, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
    161177        }
    162178      }
     
    250266
    251267    #region methods for painting the symbolic expression tree
    252     private void DrawFunctionTree(ISymbolicExpressionTree tree, Graphics graphics, int x, int y, int width, int height) {
    253       DrawFunctionTree(tree.Root, graphics, x, y, width, height, Point.Empty);
    254     }
    255 
    256     /// <summary>
    257     ///
    258     /// </summary>
    259     /// <param name="functionTree"> function tree to draw</param>
    260     /// <param name="graphics">graphics object to draw on</param>
    261     /// <param name="x">x coordinate of drawing area</param>
    262     /// <param name="y">y coordinate of drawing area</param>
    263     /// <param name="width">width of drawing area</param>
    264     /// <param name="height">height of drawing area</param>
    265     private void DrawFunctionTree(ISymbolicExpressionTreeNode node, Graphics graphics, int x, int y, int width, int height, Point connectionPoint) {
    266       VisualSymbolicExpressionTreeNode visualTreeNode = visualTreeNodes[node];
    267       float center_x = x + width / 2;
    268       float center_y = y + height / 2;
    269       int actualWidth = width - spacing;
    270       int actualHeight = height - spacing;
    271 
    272       using (var textBrush = new SolidBrush(visualTreeNode.TextColor))
    273       using (var nodeLinePen = new Pen(visualTreeNode.LineColor))
    274       using (var nodeFillBrush = new SolidBrush(visualTreeNode.FillColor)) {
    275 
    276         //calculate size of node
    277         if (actualWidth >= visualTreeNode.PreferredWidth && actualHeight >= visualTreeNode.PreferredHeight) {
    278           visualTreeNode.Width = visualTreeNode.PreferredWidth;
    279           visualTreeNode.Height = visualTreeNode.PreferredHeight;
    280           visualTreeNode.X = (int)center_x - visualTreeNode.Width / 2;
    281           visualTreeNode.Y = (int)center_y - visualTreeNode.Height / 2;
    282         }
    283           //width too small to draw in desired sized
    284         else if (actualWidth < visualTreeNode.PreferredWidth && actualHeight >= visualTreeNode.PreferredHeight) {
    285           visualTreeNode.Width = actualWidth;
    286           visualTreeNode.Height = visualTreeNode.PreferredHeight;
    287           visualTreeNode.X = x;
    288           visualTreeNode.Y = (int)center_y - visualTreeNode.Height / 2;
    289         }
    290           //height too small to draw in desired sized
    291         else if (actualWidth >= visualTreeNode.PreferredWidth && actualHeight < visualTreeNode.PreferredHeight) {
    292           visualTreeNode.Width = visualTreeNode.PreferredWidth;
    293           visualTreeNode.Height = actualHeight;
    294           visualTreeNode.X = (int)center_x - visualTreeNode.Width / 2;
    295           visualTreeNode.Y = y;
    296         }
    297           //width and height too small to draw in desired size
    298         else {
    299           visualTreeNode.Width = actualWidth;
    300           visualTreeNode.Height = actualHeight;
    301           visualTreeNode.X = x;
    302           visualTreeNode.Y = y;
    303         }
    304 
    305         //draw terminal node
    306         if (node.SubtreeCount == 0) {
    307           graphics.FillRectangle(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
    308           graphics.DrawRectangle(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
    309         } else {
    310           graphics.FillEllipse(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
    311           graphics.DrawEllipse(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
    312         }
    313 
    314         //draw name of symbol
    315         var text = node.ToString();
    316         graphics.DrawString(text, textFont, textBrush, new RectangleF(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height), stringFormat);
    317 
    318         //draw connection line to parent node
    319         if (!connectionPoint.IsEmpty && node.Parent != null) {
    320           var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node.Parent, node);
    321           using (Pen linePen = new Pen(visualLine.LineColor)) {
     268
     269    private void DrawFunctionTree(ISymbolicExpressionTree tree, Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
     270      var layoutNodes = layoutAdapter.Convert(tree).ToList();
     271      layoutEngine.Reset();
     272      layoutEngine.Root = layoutNodes[0];
     273      foreach (var ln in layoutNodes)
     274        layoutEngine.AddNode(ln.Content, ln);
     275      layoutEngine.MinHorizontalSpacing = (preferredNodeWidth + minHDistance);
     276      layoutEngine.MinVerticalSpacing = (preferredNodeHeight + minVDistance);
     277      layoutEngine.CalculateLayout();
     278      var bounds = layoutEngine.Bounds();
     279      double sx = this.Width / (bounds.Width + preferredWidth);
     280      if (sx > 1) sx = 1;
     281      double sy = this.Height / bounds.Height;
     282      if (sy > 1) sy = 1;
     283      double dx = (this.Width - bounds.Width) / 2;
     284      if (dx < 0) dx = 0;
     285      double dy = (this.Height - bounds.Height) / 2;
     286      if (dy < 0) dy = 0;
     287
     288      var levels = layoutNodes.GroupBy(n => n.Level, n => n);
     289
     290      foreach (var level in levels) {
     291        var nodes = level.ToList();
     292        double min = 0;
     293        for (int i = 0; i < nodes.Count - 1; ++i) {
     294          var w = (nodes[i + 1].X - nodes[i].X) * sx - preferredWidth;
     295          if (w < min) min = w;
     296        }
     297        if (min > 0) min = 0;
     298
     299        foreach (var layoutNode in level) {
     300          var visualNode = visualTreeNodes[layoutNode.Content];
     301          visualNode.Width = (int)Math.Round(preferredWidth + min) - 2; // -2 to allow enough padding (1px on each side) for the node contour to be drawn
     302          visualNode.Height = (int)Math.Round(preferredHeight * sy);
     303          visualNode.X = (int)(Math.Round(layoutNode.X * sx + dx));
     304          visualNode.Y = (int)(Math.Round(layoutNode.Y * sy + dy));
     305          DrawTreeNode(graphics, visualNode);
     306        }
     307      }
     308
     309      graphics.ResetClip(); // reset clip region
     310      // draw node connections
     311      foreach (var visualNode in visualTreeNodes.Values) {
     312        var node = visualNode.SymbolicExpressionTreeNode;
     313        foreach (var subtree in node.Subtrees) {
     314          var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node, subtree);
     315          var visualSubtree = visualTreeNodes[subtree];
     316          var origin = new Point(visualNode.X + visualNode.Width / 2, visualNode.Y + visualNode.Height);
     317          var target = new Point(visualSubtree.X + visualSubtree.Width / 2, visualSubtree.Y);
     318          using (var linePen = new Pen(visualLine.LineColor)) {
    322319            linePen.DashStyle = visualLine.DashStyle;
    323             graphics.DrawLine(linePen, connectionPoint, new Point(visualTreeNode.X + visualTreeNode.Width / 2, visualTreeNode.Y));
     320            graphics.DrawLine(linePen, origin, target);
    324321          }
    325         }
    326 
    327         //calculate areas for the subtrees according to their tree size and call drawFunctionTree
    328         Point connectFrom = new Point(visualTreeNode.X + visualTreeNode.Width / 2, visualTreeNode.Y + visualTreeNode.Height);
    329         int[] xBoundaries = new int[node.SubtreeCount + 1];
    330         xBoundaries[0] = x;
    331         for (int i = 0; i < node.SubtreeCount; i++) {
    332           xBoundaries[i + 1] = (int)(xBoundaries[i] + (width * (double)node.GetSubtree(i).GetLength()) / (node.GetLength() - 1));
    333           DrawFunctionTree(node.GetSubtree(i), graphics, xBoundaries[i], y + height, xBoundaries[i + 1] - xBoundaries[i], height, connectFrom);
    334322        }
    335323      }
     
    365353    }
    366354    #endregion
    367 
    368355    #region save image
    369356    private void saveImageToolStripMenuItem_Click(object sender, EventArgs e) {
     
    388375    public void SaveImageAsEmf(string filename) {
    389376      if (tree == null) return;
    390       using (Graphics g = CreateGraphics()) {
    391         using (Metafile file = new Metafile(filename, g.GetHdc())) {
    392           using (Graphics emfFile = Graphics.FromImage(file)) {
    393             int height = this.Height / tree.Depth;
    394             DrawFunctionTree(tree, emfFile, 0, 0, Width, height);
     377      using (var graphics = Graphics.FromImage(image)) {
     378        var rectangle = new Rectangle(0, 0, image.Width, image.Height);
     379        using (var metafile = new Metafile(filename, graphics.GetHdc(), rectangle, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual)) {
     380          graphics.ReleaseHdc();
     381          using (var g = Graphics.FromImage(metafile)) {
     382            DrawFunctionTree(tree, g, 0, 0, image.Width, image.Height);
    395383          }
    396384        }
    397         g.ReleaseHdc();
     385      }
     386    }
     387    #endregion
     388    #region export pgf/tikz
     389    private void exportLatexToolStripMenuItem_Click(object sender, EventArgs e) {
     390      using (var dialog = new SaveFileDialog { Filter = "Tex (*.tex)|*.tex" }) {
     391        if (dialog.ShowDialog() != DialogResult.OK) return;
     392        string filename = dialog.FileName.ToLower();
     393        var formatter = new SymbolicExpressionTreeLatexFormatter();
     394        File.WriteAllText(filename, formatter.Format(Tree));
    398395      }
    399396    }
Note: See TracChangeset for help on using the changeset viewer.