Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/21/14 18:20:47 (10 years ago)
Author:
bburlacu
Message:

#1772: Merged trunk changes and added missing frame files (for HeuristicLab.EvolutionTracking and HeuristicLab.EvolutionTracking.Views.

File:
1 edited

Legend:

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

    r10456 r10501  
    3232  public partial class SymbolicExpressionTreeChart : UserControl {
    3333    private Image image;
    34     private StringFormat stringFormat;
     34    private readonly StringFormat stringFormat;
    3535    private Dictionary<ISymbolicExpressionTreeNode, VisualSymbolicExpressionTreeNode> visualTreeNodes;
    3636    private Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection> visualLines;
     
    5151      this.lineColor = Color.Black;
    5252      this.backgroundColor = Color.White;
    53       this.textFont = new Font(FontFamily.GenericSerif, 14, GraphicsUnit.Pixel);
     53      this.textFont = new Font(FontFamily.GenericSansSerif, 12);
    5454      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>();
    5555      layoutAdapter = new SymbolicExpressionTreeLayoutAdapter();
     
    108108        visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection>();
    109109        if (tree != null) {
    110           foreach (ISymbolicExpressionTreeNode node in tree.IterateNodesPrefix()) {
     110          IEnumerable<ISymbolicExpressionTreeNode> nodes;
     111          if (tree.Root.SubtreeCount == 1) nodes = tree.Root.GetSubtree(0).IterateNodesPrefix();
     112          else nodes = tree.Root.IterateNodesPrefix();
     113          foreach (ISymbolicExpressionTreeNode node in nodes) {
    111114            visualTreeNodes[node] = new VisualSymbolicExpressionTreeNode(node);
    112115            if (node.Parent != null) visualLines[Tuple.Create(node.Parent, node)] = new VisualSymbolicExpressionTreeNodeConnection();
     
    166169        this.Refresh();
    167170      }
    168     }
    169 
    170     public void HighlightNode(ISymbolicExpressionTreeNode node, Color color) {
    171       var visualNode = GetVisualSymbolicExpressionTreeNode(node);
    172       if (visualNode == null) return;
    173       visualNode.LineColor = color;
    174       RepaintNode(visualNode);
    175     }
    176 
    177     public void HighlightSubtree(ISymbolicExpressionTreeNode subtree, Color color) {
    178       foreach (var node in subtree.IterateNodesBreadth())
    179         HighlightNode(node, color);
    180171    }
    181172
     
    280271    private void DrawFunctionTree(ISymbolicExpressionTree symbolicExpressionTree, Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
    281272      var layoutNodes = layoutAdapter.Convert(symbolicExpressionTree).ToList();
     273      if (symbolicExpressionTree.Root.SubtreeCount == 1) layoutNodes.RemoveAt(0);
    282274      layoutEngine.Reset();
    283275      layoutEngine.Root = layoutNodes[0];
    284       foreach (var ln in layoutNodes)
    285         layoutEngine.AddNode(ln.Content, ln);
    286       layoutEngine.MinHorizontalSpacing = (preferredNodeWidth + minHDistance);
    287       layoutEngine.MinVerticalSpacing = (preferredNodeHeight + minVDistance);
     276      layoutEngine.AddNodes(layoutNodes);
     277      layoutEngine.MinHorizontalSpacing = (preferredWidth + minHDistance);
     278      layoutEngine.MinVerticalSpacing = (preferredHeight + minVDistance);
    288279      layoutEngine.CalculateLayout();
    289280      var bounds = layoutEngine.Bounds();
    290       double sx = this.Width / (bounds.Width + preferredWidth);
    291       if (sx > 1) sx = 1;
    292       double sy = this.Height / bounds.Height;
    293       if (sy > 1) sy = 1;
    294       double dx = (this.Width - bounds.Width) / 2;
    295       if (dx < 0) dx = 0;
    296       double dy = (this.Height - bounds.Height) / 2;
    297       if (dy < 0) dy = 0;
    298 
    299       var levels = layoutNodes.GroupBy(n => n.Level, n => n);
    300 
    301       foreach (var level in levels) {
    302         var nodes = level.ToList();
    303         double min = 0;
    304         for (int i = 0; i < nodes.Count - 1; ++i) {
    305           var w = (nodes[i + 1].X - nodes[i].X) * sx - preferredWidth;
    306           if (w < min) min = w;
    307         }
    308         if (min > 0) min = 0;
    309 
    310         foreach (var layoutNode in level) {
     281
     282      double verticalScalingFactor = 1.0;
     283      double layoutHeight = (bounds.Height + preferredHeight);
     284      if (this.Height < layoutHeight)
     285        verticalScalingFactor = this.Height / layoutHeight;
     286
     287      double horizontalScalingFactor = 1.0;
     288      double layoutWidth = (bounds.Width + preferredWidth);
     289      if (this.Width < layoutWidth)
     290        horizontalScalingFactor = this.Width / layoutWidth;
     291
     292      double horizontalOffset;
     293      if (this.Width > layoutWidth)
     294        horizontalOffset = (this.Width - layoutWidth) / 2.0;
     295      else
     296        horizontalOffset = preferredWidth / 2.0;
     297
     298      var levels = layoutNodes.GroupBy(n => n.Level, n => n).ToList();
     299      for (int i = levels.Count - 1; i >= 0; --i) {
     300        var nodes = levels[i].ToList();
     301
     302        double minSpacing = double.MaxValue;
     303        if (nodes.Count > 1) {
     304          for (int j = 1; j < nodes.Count() - 1; ++j) {
     305            var distance = nodes[j].X - nodes[j - 1].X; // guaranteed to be > 0
     306            if (minSpacing > distance) minSpacing = distance;
     307          }
     308        }
     309        minSpacing *= horizontalScalingFactor;
     310
     311        int minWidth = (int)Math.Round(preferredWidth * horizontalScalingFactor);
     312        int width = (int)Math.Min(minSpacing, preferredWidth) - 2; // leave some pixels so that node margins don't overlap
     313
     314        foreach (var layoutNode in nodes) {
    311315          var visualNode = visualTreeNodes[layoutNode.Content];
    312           visualNode.Width = (int)Math.Round(preferredWidth + min) - 2; // -2 to allow enough padding (1px on each side) for the node contour to be drawn
    313           visualNode.Height = (int)Math.Round(preferredHeight * sy);
    314           visualNode.X = (int)(Math.Round(layoutNode.X * sx + dx));
    315           visualNode.Y = (int)(Math.Round(layoutNode.Y * sy + dy));
     316          visualNode.Width = width;
     317          visualNode.Height = (int)Math.Round(preferredHeight * verticalScalingFactor);
     318          visualNode.X = (int)Math.Round((layoutNode.X + horizontalOffset) * horizontalScalingFactor + (minWidth - width) / 2.0);
     319          visualNode.Y = (int)Math.Round(layoutNode.Y * verticalScalingFactor);
    316320          DrawTreeNode(graphics, visualNode);
    317321        }
    318322      }
    319 
    320       graphics.ResetClip(); // reset clip region
    321323      // draw node connections
    322324      foreach (var visualNode in visualTreeNodes.Values) {
     
    327329          var origin = new Point(visualNode.X + visualNode.Width / 2, visualNode.Y + visualNode.Height);
    328330          var target = new Point(visualSubtree.X + visualSubtree.Width / 2, visualSubtree.Y);
     331          graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
    329332          using (var linePen = new Pen(visualLine.LineColor)) {
    330333            linePen.DashStyle = visualLine.DashStyle;
     
    378381      Image image = new Bitmap(Width, Height);
    379382      using (Graphics g = Graphics.FromImage(image)) {
    380         int height = this.Height / tree.Depth;
    381         DrawFunctionTree(tree, g, 0, 0, Width, height);
     383        DrawFunctionTree(tree, g, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
    382384      }
    383385      image.Save(filename);
     
    386388    public void SaveImageAsEmf(string filename) {
    387389      if (tree == null) return;
    388       using (var graphics = Graphics.FromImage(image)) {
    389         var rectangle = new Rectangle(0, 0, image.Width, image.Height);
    390         using (var metafile = new Metafile(filename, graphics.GetHdc(), rectangle, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual)) {
    391           graphics.ReleaseHdc();
    392           using (var g = Graphics.FromImage(metafile)) {
    393             DrawFunctionTree(tree, g, 0, 0, image.Width, image.Height);
     390      using (Graphics g = CreateGraphics()) {
     391        using (Metafile file = new Metafile(filename, g.GetHdc())) {
     392          using (Graphics emfFile = Graphics.FromImage(file)) {
     393            DrawFunctionTree(tree, emfFile, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
    394394          }
    395395        }
     396        g.ReleaseHdc();
    396397      }
    397398    }
Note: See TracChangeset for help on using the changeset viewer.