Changeset 9963 for branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views
- Timestamp:
- 09/13/13 14:42:38 (11 years ago)
- Location:
- branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4
- Files:
-
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views-3.4.csproj
r9835 r9963 230 230 <DependentUpon>SymbolicExpressionView.cs</DependentUpon> 231 231 </Compile> 232 <Compile Include="TreeLayout.cs" />233 232 <Compile Include="VisualSymbolicExpressionTreeNode.cs" /> 234 233 <Compile Include="VisualSymbolicExpressionTreeNodeConnection.cs" /> … … 267 266 <Install>true</Install> 268 267 </BootstrapperPackage> 269 </ItemGroup>270 <ItemGroup>271 <EmbeddedResource Include="SymbolicExpressionTreeChart.resx">272 <DependentUpon>SymbolicExpressionTreeChart.cs</DependentUpon>273 </EmbeddedResource>274 268 </ItemGroup> 275 269 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs
r9835 r9963 24 24 using System.Drawing; 25 25 using System.Drawing.Imaging; 26 using System.IO; 27 using System.Linq; 26 28 using System.Windows.Forms; 27 29 using Point = System.Drawing.Point; … … 33 35 private Dictionary<ISymbolicExpressionTreeNode, VisualSymbolicExpressionTreeNode> visualTreeNodes; 34 36 private Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection> visualLines; 37 private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> { MinHorizontalSpacing = 5, MinVerticalSpacing = 5 }; 38 private readonly SymbolicExpressionTreeLayoutAdapter layoutAdapter = new SymbolicExpressionTreeLayoutAdapter(); 39 35 40 36 41 public SymbolicExpressionTreeChart() { … … 160 165 graphics.Clear(backgroundColor); 161 166 if (tree != null) { 162 int height = this.Height / tree.Depth; 163 DrawFunctionTree(tree, graphics, 0, 0, this.Width, height); 167 DrawFunctionTree(tree, graphics, 70, 46, 20, 50); 164 168 } 165 169 } … … 254 258 #region methods for painting the symbolic expression tree 255 259 256 private void DrawFunctionTree(ISymbolicExpressionTree tree, Graphics graphics, int x, int y, int width, int height) { 257 // DrawFunctionTree(tree.Root, graphics, x, y, width, height, Point.Empty); 258 AlternateDraw(tree, graphics, 70, 46, 20, 50); 259 } 260 261 private void AlternateDraw(ISymbolicExpressionTree tree, Graphics graphics, int preferredWidth, int preferredHeight, int minDistance, int maxDistance) { 262 var tl = new TreeLayout(); 263 tl.Distance = 5; 264 tl.SymbolicExpressionTree = tree; 265 266 var nodePositions = tl.GetNodeCoordinates(); 267 var bounds = tl.Bounds(); 260 private void DrawFunctionTree(ISymbolicExpressionTree tree, Graphics graphics, int preferredWidth, int preferredHeight, int minDistance, int maxDistance) { 261 var layoutNodes = layoutAdapter.Convert(tree).ToList(); 262 layoutEngine.Reset(); 263 layoutEngine.Root = layoutNodes[0]; 264 foreach (var ln in layoutNodes) 265 layoutEngine.AddNode(ln.Content, ln); 266 layoutEngine.CalculateLayout(); 267 var nodePositions = layoutEngine.GetNodeCoordinates(); 268 var bounds = layoutEngine.Bounds(); 268 269 269 270 double sx = Width / bounds.Width; 270 271 double sy = Height / bounds.Height; 271 272 272 double dx = tl.Distance* sx; // scaled horizontal distance273 double dy = tl.Distance* sy; // scaled vertical distance273 double dx = layoutEngine.MinHorizontalSpacing * sx; // scaled horizontal distance 274 double dy = layoutEngine.MinVerticalSpacing * sy; // scaled vertical distance 274 275 275 276 int maxWidth = (int)Math.Round(dx); … … 307 308 graphics.DrawLine(linePen, origin, target); 308 309 } 309 }310 }311 }312 313 /// <summary>314 ///315 /// </summary>316 /// <param name="node">the root of the function tree to draw</param>317 /// <param name="graphics">graphics object to draw on</param>318 /// <param name="x">x coordinate of drawing area</param>319 /// <param name="y">y coordinate of drawing area</param>320 /// <param name="width">width of drawing area</param>321 /// <param name="height">height of drawing area</param>322 private void DrawFunctionTree(ISymbolicExpressionTreeNode node, Graphics graphics, int x, int y, int width, int height, Point connectionPoint) {323 VisualSymbolicExpressionTreeNode visualTreeNode = visualTreeNodes[node];324 float center_x = x + width / 2;325 float center_y = y + height / 2;326 int actualWidth = width - spacing;327 int actualHeight = height - spacing;328 329 using (var textBrush = new SolidBrush(visualTreeNode.TextColor))330 using (var nodeLinePen = new Pen(visualTreeNode.LineColor))331 using (var nodeFillBrush = new SolidBrush(visualTreeNode.FillColor)) {332 333 //calculate size of node334 if (actualWidth >= visualTreeNode.PreferredWidth && actualHeight >= visualTreeNode.PreferredHeight) {335 visualTreeNode.Width = visualTreeNode.PreferredWidth;336 visualTreeNode.Height = visualTreeNode.PreferredHeight;337 visualTreeNode.X = (int)center_x - visualTreeNode.Width / 2;338 visualTreeNode.Y = (int)center_y - visualTreeNode.Height / 2;339 }340 //width too small to draw in desired sized341 else if (actualWidth < visualTreeNode.PreferredWidth && actualHeight >= visualTreeNode.PreferredHeight) {342 visualTreeNode.Width = actualWidth;343 visualTreeNode.Height = visualTreeNode.PreferredHeight;344 visualTreeNode.X = x;345 visualTreeNode.Y = (int)center_y - visualTreeNode.Height / 2;346 }347 //height too small to draw in desired sized348 else if (actualWidth >= visualTreeNode.PreferredWidth && actualHeight < visualTreeNode.PreferredHeight) {349 visualTreeNode.Width = visualTreeNode.PreferredWidth;350 visualTreeNode.Height = actualHeight;351 visualTreeNode.X = (int)center_x - visualTreeNode.Width / 2;352 visualTreeNode.Y = y;353 }354 //width and height too small to draw in desired size355 else {356 visualTreeNode.Width = actualWidth;357 visualTreeNode.Height = actualHeight;358 visualTreeNode.X = x;359 visualTreeNode.Y = y;360 }361 362 //draw terminal node363 if (node.SubtreeCount == 0) {364 graphics.FillRectangle(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);365 graphics.DrawRectangle(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);366 } else {367 graphics.FillEllipse(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);368 graphics.DrawEllipse(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);369 }370 371 //draw name of symbol372 var text = node.ToString();373 graphics.DrawString(text, textFont, textBrush, new RectangleF(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height), stringFormat);374 375 //draw connection line to parent node376 if (!connectionPoint.IsEmpty && node.Parent != null) {377 var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node.Parent, node);378 using (Pen linePen = new Pen(visualLine.LineColor)) {379 linePen.DashStyle = visualLine.DashStyle;380 graphics.DrawLine(linePen, connectionPoint, new Point(visualTreeNode.X + visualTreeNode.Width / 2, visualTreeNode.Y));381 }382 }383 384 //calculate areas for the subtrees according to their tree size and call drawFunctionTree385 Point connectFrom = new Point(visualTreeNode.X + visualTreeNode.Width / 2, visualTreeNode.Y + visualTreeNode.Height);386 int[] xBoundaries = new int[node.SubtreeCount + 1];387 xBoundaries[0] = x;388 for (int i = 0; i < node.SubtreeCount; i++) {389 xBoundaries[i + 1] = (int)(xBoundaries[i] + (width * (double)node.GetSubtree(i).GetLength()) / (node.GetLength() - 1));390 DrawFunctionTree(node.GetSubtree(i), graphics, xBoundaries[i], y + height, xBoundaries[i + 1] - xBoundaries[i], height, connectFrom);391 310 } 392 311 } … … 455 374 } 456 375 #endregion 376 #region export pgf/tikz 377 private void exportLatexToolStripMenuItem_Click(object sender, EventArgs e) { 378 using (var dialog = new SaveFileDialog { Filter = "Tex (*.tex)|*.tex" }) { 379 if (dialog.ShowDialog() != DialogResult.OK) return; 380 string filename = dialog.FileName.ToLower(); 381 var formatter = new SymbolicExpressionTreeLatexFormatter(); 382 File.WriteAllText(filename, formatter.Format(Tree)); 383 } 384 } 385 #endregion 457 386 } 458 387 }
Note: See TracChangeset
for help on using the changeset viewer.