Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/12/14 13:26:18 (10 years ago)
Author:
pfleck
Message:
  • Merged trunk into preprocessing branch.
Location:
branches/DataPreprocessing
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing

  • branches/DataPreprocessing/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views

  • branches/DataPreprocessing/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/LayoutEngines/BoxesLayoutEngine.cs

    r10520 r11009  
    1 
     1#region License Information
     2
     3/* HeuristicLab
     4 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     5 *
     6 * This file is part of HeuristicLab.
     7 *
     8 * HeuristicLab is free software: you can redistribute it and/or modify
     9 * it under the terms of the GNU General Public License as published by
     10 * the Free Software Foundation, either version 3 of the License, or
     11 * (at your option) any later version.
     12 *
     13 * HeuristicLab is distributed in the hope that it will be useful,
     14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 * GNU General Public License for more details.
     17 *
     18 * You should have received a copy of the GNU General Public License
     19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     20 */
     21
     22#endregion
     23
    224using System;
    325using System.Collections.Generic;
    4 using System.Drawing;
    526using System.Linq;
    627
    728namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
    829  public class BoxesLayoutEngine<T> : ILayoutEngine<T> where T : class {
    9     private readonly Dictionary<T, VisualTreeNode<T>> nodeMap;
    10 
    1130    public int NodeWidth { get; set; }
    1231    public int NodeHeight { get; set; }
    1332    public int HorizontalSpacing { get; set; }
    1433    public int VerticalSpacing { get; set; }
    15     private VisualTreeNode<T> layoutRoot;
    1634
    17     public int Width { get; private set; }
    18     public int Height { get; private set; }
     35    private readonly Func<T, IEnumerable<T>> GetChildren;
     36    private readonly Func<T, int> GetLength;
     37    private readonly Func<T, int> GetDepth;
    1938
    20     public Func<T, IEnumerable<T>> GetChildren { get; set; }
    21     public Func<T, int> GetLength { get; set; }
    22     public Func<T, int> GetDepth { get; set; }
     39    public BoxesLayoutEngine(Func<T, IEnumerable<T>> GetChildren, Func<T, int> GetLength, Func<T, int> GetDepth) {
     40      if (GetChildren == null) throw new ArgumentNullException("GetChildren");
     41      if (GetLength == null) throw new ArgumentNullException("GetLength");
     42      if (GetDepth == null) throw new ArgumentNullException("GetDepth");
    2343
    24     public BoxesLayoutEngine() {
    25       nodeMap = new Dictionary<T, VisualTreeNode<T>>();
     44      this.GetChildren = GetChildren;
     45      this.GetLength = GetLength;
     46      this.GetDepth = GetDepth;
    2647    }
    2748
    28     public void CalculateLayout() {
    29       throw new Exception("The BoxesLayoutEngine does not support arbitrary bounds. Please use method CalculateLayout(Width, Height)");
     49
     50    public IEnumerable<VisualTreeNode<T>> CalculateLayout(T root, float width, float height) {
     51      var nodeMap = new Dictionary<T, VisualTreeNode<T>>();
     52      CreateVisualNodes(root, nodeMap);
     53      RecursiveLayout(nodeMap, nodeMap[root], 0, 0, (int)Math.Round(width), (int)Math.Round(height) / GetDepth(root));
     54      return nodeMap.Values;
    3055    }
    3156
    32     public void CalculateLayout(float width, float height) {
    33       Width = (int)Math.Round(width);
    34       Height = (int)Math.Round(height);
    35       Reset();
    36       RecursiveLayout(layoutRoot, 0, 0, Width, Height / GetDepth(layoutRoot.Content));
    37     }
    38 
    39     public void Initialize(T root, Func<T, IEnumerable<T>> getChildren, Func<T, int> getLength, Func<T, int> getDepth) {
    40       if (getChildren == null || getLength == null || getDepth == null)
    41         throw new ArgumentNullException("The BoxesLayoutEngine requires all of the lambdas: (getChildren, getLength and getDepth) to be defined.");
    42       GetChildren = getChildren;
    43       GetLength = getLength;
    44       GetDepth = getDepth;
    45       Clear();
    46       Expand(root); // produce the nodeMap
    47       layoutRoot = nodeMap[root];
    48     }
    49 
    50     private void Expand(T root) {
     57    private void CreateVisualNodes(T root, Dictionary<T, VisualTreeNode<T>> map) {
    5158      var node = new VisualTreeNode<T>(root) {
    5259        PreferredWidth = NodeWidth,
    5360        PreferredHeight = NodeHeight
    5461      };
    55       nodeMap.Add(root, node);
     62
     63      map.Add(root, node);
    5664      var children = GetChildren(root).ToList();
    5765      if (children.Any()) {
    5866        foreach (var child in children) {
    59           Expand(child);
     67          CreateVisualNodes(child, map);
    6068        }
    6169      }
    6270    }
    6371
    64     public void Center(float width, float height) {
    65       // does nothing because the BoxesLayout centers the tree by default
    66     }
    67 
    68     public void Clear() {
    69       nodeMap.Clear();
    70       layoutRoot = null;
    71     }
    72 
    73     public void Reset() {
    74       foreach (var node in nodeMap.Values) {
    75         node.X = 0;
    76         node.Y = 0;
    77       }
    78     }
    79 
    80     public Dictionary<T, PointF> GetCoordinates() {
    81       return nodeMap.ToDictionary(x => x.Key, x => new PointF(x.Value.X, x.Value.Y));
    82     }
    83 
    84     public void FitToBounds(float width, float height) {
    85       // does nothing because the BoxesLayout is by default stretched on the whole drawing area
    86     }
    87 
    88     private void RecursiveLayout(VisualTreeNode<T> visualTreeNode, int x, int y, int width, int height) {
     72    private void RecursiveLayout(Dictionary<T, VisualTreeNode<T>> nodeMap, VisualTreeNode<T> visualTreeNode, int x, int y, int width, int height) {
    8973      float center_x = x + width / 2;
    9074      float center_y = y + height / 2;
    91       int actualWidth = width - VerticalSpacing;
     75      int actualWidth = width - HorizontalSpacing;
    9276      int actualHeight = height - VerticalSpacing;
    9377
     
    127111      for (int i = 0; i < children.Count; i++) {
    128112        xBoundaries[i + 1] = (int)(xBoundaries[i] + (width * (double)GetLength(children[i])) / (GetLength(node) - 1));
    129         RecursiveLayout(nodeMap[children[i]], xBoundaries[i], y + height, xBoundaries[i + 1] - xBoundaries[i], height);
     113        RecursiveLayout(nodeMap, nodeMap[children[i]], xBoundaries[i], y + height, xBoundaries[i + 1] - xBoundaries[i], height);
    130114      }
    131     }
    132 
    133     public IEnumerable<VisualTreeNode<T>> GetVisualNodes() {
    134       return nodeMap.Values;
    135115    }
    136116  }
Note: See TracChangeset for help on using the changeset viewer.