Skip to content

Commit

Permalink
add WpfApplicationSample_OnnxViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
beru authored and levnach committed Apr 25, 2023
1 parent 7228c38 commit cdb3276
Show file tree
Hide file tree
Showing 8 changed files with 8,156 additions and 1 deletion.
29 changes: 28 additions & 1 deletion GraphLayout/GraphLayout.sln
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DrawingFromGeometryWithGtk"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EdgeDirectionTest", "Samples\EdgeDirectionTest\EdgeDirectionTest.csproj", "{02EAF869-FB5A-40BD-AD78-92E193946B5D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBB}") = "FormForGViewer", "tools\FormForGViewer\FormForGViewer.csproj", "{AF9121C3-E4B7-464F-8EBB-96C9CF627C07}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FormForGViewer", "tools\FormForGViewer\FormForGViewer.csproj", "{AF9121C3-E4B7-464F-8EBB-96C9CF627C07}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfApplicationSample_OnnxViewer", "Samples\WpfApplicationSample_OnnxViewer\WpfApplicationSample_OnnxViewer.csproj", "{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}"
ProjectSection(ProjectDependencies) = postProject
{415D3E3F-7105-46C1-84D2-7ECB34213D92} = {415D3E3F-7105-46C1-84D2-7ECB34213D92}
{B76F8F71-4B00-4242-BE36-C9F0732511F7} = {B76F8F71-4B00-4242-BE36-C9F0732511F7}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -1066,6 +1072,26 @@ Global
{AF9121C3-E4B7-464F-8EBB-96C9CF627C07}.Release|x64.Build.0 = Release|x64
{AF9121C3-E4B7-464F-8EBB-96C9CF627C07}.Release|x86.ActiveCfg = Release|x86
{AF9121C3-E4B7-464F-8EBB-96C9CF627C07}.Release|x86.Build.0 = Release|x86
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Debug|ARM.ActiveCfg = Debug|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Debug|ARM.Build.0 = Debug|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Debug|ARM64.Build.0 = Debug|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Debug|x64.ActiveCfg = Debug|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Debug|x64.Build.0 = Debug|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Debug|x86.ActiveCfg = Debug|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Debug|x86.Build.0 = Debug|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Release|Any CPU.Build.0 = Release|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Release|ARM.ActiveCfg = Release|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Release|ARM.Build.0 = Release|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Release|ARM64.ActiveCfg = Release|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Release|ARM64.Build.0 = Release|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Release|x64.ActiveCfg = Release|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Release|x64.Build.0 = Release|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Release|x86.ActiveCfg = Release|Any CPU
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1116,6 +1142,7 @@ Global
{AA8D2135-1BD2-47DD-9DC9-2FC266711536} = {CAAD5476-353C-41F5-A6E7-88BA0C97C2C9}
{02EAF869-FB5A-40BD-AD78-92E193946B5D} = {CAAD5476-353C-41F5-A6E7-88BA0C97C2C9}
{AF9121C3-E4B7-464F-8EBB-96C9CF627C07} = {9E51C177-3DF6-474E-AC7A-ED036ED32F05}
{79FB22C1-EACD-4808-AEBB-B0D3A6C5F46F} = {CAAD5476-353C-41F5-A6E7-88BA0C97C2C9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0D81D757-911D-4F8B-A4B3-4C4F1CBD5B60}
Expand Down
9 changes: 9 additions & 0 deletions GraphLayout/Samples/WpfApplicationSample_OnnxViewer/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="WpfApplicationSample_OnnxViewer.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplicationSample_OnnxViewer"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
15 changes: 15 additions & 0 deletions GraphLayout/Samples/WpfApplicationSample_OnnxViewer/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApplicationSample_OnnxViewer {
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Window x:Class="WpfApplicationSample_OnnxViewer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplicationSample_OnnxViewer"
mc:Ignorable="d"
Title="WpfApplicationSample OnnxViewer" Height="450" Width="800">
<Grid x:Name="mainGrid" AllowDrop="True" Drop="Grid_Drop" DragOver="Grid_DragOver" Background="Transparent"></Grid>
</Window>
162 changes: 162 additions & 0 deletions GraphLayout/Samples/WpfApplicationSample_OnnxViewer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using Microsoft.Msagl.Drawing;
using Microsoft.Msagl.Layout.Layered;
using Microsoft.Msagl.WpfGraphControl;
using Microsoft.Msagl.Core.DataStructures;
using Color = Microsoft.Msagl.Drawing.Color;
using Onnx;
using Microsoft.Msagl.Core.Geometry.Curves;

namespace WpfApplicationSample_OnnxViewer {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {

DockPanel graphViewerPanel = new DockPanel();
GraphViewer graphViewer = new GraphViewer();
int downX;
int downY;
PlaneTransformation downTransform;

public MainWindow() {
InitializeComponent();

mainGrid.Children.Add(graphViewerPanel);
graphViewerPanel.Focusable = true;
graphViewerPanel.KeyDown += Graph_KeyDown;
graphViewerPanel.Focus();
graphViewer.BindToPanel(graphViewerPanel);
graphViewer.MouseDown += MouseDown;
graphViewer.MouseMove += MouseMove;
}

void Grid_DragOver(object sender, DragEventArgs e) {
e.Effects = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.All : DragDropEffects.None;
e.Handled = true;
}

void Grid_Drop(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
var fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
LoadOnnxFile(fileNames[0]);
}
}

private void Graph_KeyDown(object sender, KeyEventArgs e) {
double dx = 0;
double dy = 0;
if (Keyboard.IsKeyDown(Key.W)) dy += 10;
if (Keyboard.IsKeyDown(Key.A)) dx -= 10;
if (Keyboard.IsKeyDown(Key.S)) dy -= 10;
if (Keyboard.IsKeyDown(Key.D)) dx += 10;
var scale = graphViewer.CurrentScale;
dx /= scale;
dy /= scale;
var translate = new PlaneTransformation(1, 0, dx, 0, 1, dy);
graphViewer.Transform *= translate;
}

void MouseDown(object sender, MsaglMouseEventArgs e) {
this.downX = e.X;
this.downY = e.Y;
this.downTransform = graphViewer.Transform;
graphViewer.GraphCanvas.CaptureMouse();
}

void MouseMove(object sender, MsaglMouseEventArgs e) {
if (!e.LeftButtonIsPressed || this.downTransform == null)
return;
var scale = graphViewer.CurrentScale;
var dx = (e.X - this.downX) / scale;
var dy = (this.downY - e.Y) / scale;
var translate = new PlaneTransformation(1, 0, dx, 0, 1, dy);
graphViewer.Transform = this.downTransform * translate;
}

void LoadOnnxFile(string filePath) {
try
{
var model = ModelProto.Parser.ParseFrom(File.ReadAllBytes(filePath));
var modelGraph = model.Graph;
Graph graph = new Graph("graph");
graph.LayerConstraints.RemoveAllConstraints();
SugiyamaLayoutSettings layoutSettings = (SugiyamaLayoutSettings)graph.LayoutAlgorithmSettings;
var inputs = new Dictionary<string, List<string>>();
var outputs = new Dictionary<string, List<string>>();
var nodes = new Set<string>();
var initializers = new Set<string>();
foreach(var initializer in modelGraph.Initializer) {
initializers.Insert(initializer.Name);
}
var limit = 3000;
var cnt = 0;
foreach(var node in modelGraph.Node) {
var gNode = new Node(node.Name);
gNode.LabelText = node.OpType.ToString();
graph.AddNode(gNode);
nodes.Insert(node.Name);
foreach(var input in node.Input) {
if(!inputs.ContainsKey(input)) {
inputs.Add(input, new List<string>());
}
inputs[input].Add(node.Name);
}
foreach(var output in node.Output) {
if(!outputs.ContainsKey(output)) {
outputs.Add(output, new List<string>());
}
outputs[output].Add(node.Name);
}
if(++cnt > limit)
break;
}
cnt = 0;
foreach(var node in modelGraph.Node) {
foreach(var output in node.Output) {
if(!inputs.ContainsKey(output)) {
if(nodes.Contains(output)) {
graph.AddNode(output);
graph.AddEdge(node.Name, output);
}
}
else {
var inputs2 = inputs[output];
inputs2.Reverse();
foreach(var input in inputs2) {
if(nodes.Contains(input)) {
graph.AddEdge(node.Name, input);
}
}
}
}
if(++cnt > limit)
break;
}
graph.Attr.BackgroundColor = Color.Transparent;
graphViewer.Graph = graph;
this.Title = filePath + " - WpfApplicationSample OnnxViewer";
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Load Failed", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

}
}
Loading

0 comments on commit cdb3276

Please sign in to comment.