00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using System.Windows;
00006 using System.Windows.Controls;
00007 using System.Windows.Data;
00008 using System.Windows.Documents;
00009 using System.Windows.Input;
00010 using System.Windows.Media;
00011 using System.Windows.Media.Imaging;
00012 using System.Windows.Navigation;
00013 using System.Windows.Shapes;
00014 using System.Collections;
00015
00016 namespace EdgeClustering
00017 {
00018
00019
00020
00021 public partial class MainWindow : Window
00022 {
00023 private Point mousePoint;
00024 private ScaleTransform transform;
00025 private Graph _graph;
00026 private GraphDrawer gdrawer;
00027 public void LoadGraph(String filename){
00028
00029 }
00030
00031 public void LoadRandomGraph()
00032 {
00033 _graph = new Graph(20, 1);
00034
00035
00036 gdrawer.Graph = _graph;
00037
00038
00039 gdrawer.InvalidateVisual();
00040
00041 }
00042
00043 public MainWindow()
00044 {
00045 InitializeComponent();
00046
00047 gdrawer = new GraphDrawer((int)canvas1.Width);
00048 canvas1.Children.Add(gdrawer);
00049
00050 transform = new ScaleTransform();
00051 gdrawer.RenderTransform = transform;
00052 transform.CenterX = canvas1.Width / 2;
00053 transform.CenterY = canvas1.Height / 2;
00054
00055 try
00056 {
00057
00058 XMLData.ReadSettings("Config.xml");
00059 UpdateGUI();
00060 gdrawer.InvalidateVisual();
00061
00062 }
00063 catch (System.IO.FileNotFoundException ex)
00064 {
00065 MessageBox.Show("No \"Config.xml\" found. Save settings for next start.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
00066 }
00067 catch (Exception e)
00068 {
00069 MessageBox.Show("Unable to load settings from the configuration file.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
00070 }
00071
00072
00073 InitSettings();
00074 }
00075
00076
00077
00078 private void IsInitialized(object sender, EventArgs e)
00079 {
00080
00081 }
00082
00083 private void canvas1_MouseDown(object sender, MouseButtonEventArgs e)
00084 {
00085 mousePoint = e.GetPosition(canvas1);
00086
00087
00088
00089
00090
00091
00092
00093
00094 }
00095
00096 private void canvas1_MouseUp(object sender, MouseButtonEventArgs e)
00097 {
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 }
00108
00109 private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
00110 {
00111
00112
00113 transform.ScaleX = e.NewValue;
00114 transform.ScaleY = e.NewValue;
00115 }
00116
00117 private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
00118 {
00119 int d = (Math.Sign(e.Delta)> 0)?1:-1;
00120
00121
00122 transform.ScaleX += d;
00123 transform.ScaleY += d;
00124
00125 if (transform.ScaleX < 1)
00126 transform.ScaleX = 1;
00127 if (transform.ScaleY < 1)
00128 transform.ScaleY = 1;
00129 }
00130
00131 private void Window_MouseMove(object sender, MouseEventArgs e)
00132 {
00133
00134 Point current = e.GetPosition(canvas1);
00135
00136
00137
00138
00139 if ( !(current.X > canvas1.Width))
00140 {
00141 transform.CenterX = current.X;
00142 transform.CenterY = current.Y;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152 mousePoint = e.GetPosition(canvas1);
00153 }
00154
00155 private void GraphImport_Click(object sender, RoutedEventArgs e)
00156 {
00157
00158 Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
00159 dlg.FileName = "Graph";
00160 dlg.DefaultExt = ".xml";
00161 dlg.Filter = "XML Graph (.xml)|*.xml";
00162
00163
00164 Nullable<bool> result = dlg.ShowDialog();
00165
00166
00167 if (result == true)
00168 {
00169
00170 string filename = dlg.FileName;
00171 _graph = new Graph(filename);
00172 }
00173
00174 gdrawer.Graph = _graph;
00175 gdrawer.InvalidateVisual();
00176 }
00177
00178 private void LoadConfig_Click(object sender, RoutedEventArgs e)
00179 {
00180
00181 Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
00182 dlg.FileName = "Config";
00183 dlg.DefaultExt = ".xml";
00184 dlg.Filter = "XML Graph (.xml)|*.xml";
00185
00186
00187 Nullable<bool> result = dlg.ShowDialog();
00188
00189
00190 if (result == true)
00191 {
00192 try {
00193
00194 XMLData.ReadSettings(dlg.FileName);
00195 }
00196 catch (Exception ex)
00197 {
00198 MessageBox.Show("Unable to load settings from the configuration file.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
00199 }
00200 }
00201
00202 UpdateGUI();
00203 gdrawer.InvalidateVisual();
00204 }
00205
00206 private void SaveConfig_Click(object sender, RoutedEventArgs e)
00207 {
00208 Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
00209 dlg.FileName = "Config";
00210 dlg.DefaultExt = ".xml";
00211 dlg.Filter = "XML Graph (.xml)|*.xml";
00212
00213
00214 Nullable<bool> result = dlg.ShowDialog();
00215
00216
00217 if (result == true)
00218 {
00219 ComputeButton_Click(null, null);
00220 try {
00221
00222 XMLData.WriteSettings(dlg.FileName);
00223 }
00224 catch (Exception ex)
00225 {
00226 MessageBox.Show("Unable to write settings to file.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
00227 }
00228 }
00229 }
00230
00231 private void DebugRandomGraph_Click(object sender, RoutedEventArgs e)
00232 {
00233 LoadRandomGraph();
00234 }
00235
00236 private void SaveGraph_Click(object sender, RoutedEventArgs e)
00237 {
00238 if (_graph == null)
00239 return;
00240
00241 Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
00242 dlg.FileName = "Graph";
00243 dlg.DefaultExt = ".xml";
00244 dlg.Filter = "XML Graph (.xml)|*.xml";
00245
00246
00247 Nullable<bool> result = dlg.ShowDialog();
00248
00249
00250 if (result == true)
00251 {
00252
00253 string filename = dlg.FileName;
00254 _graph.WriteXMLGraph(filename);
00255 }
00256
00257
00258
00259 }
00260
00261 private void Compute_Click(object sender, RoutedEventArgs e)
00262 {
00263 if (_graph == null)
00264 return;
00265
00266 _graph.Compute();
00267 gdrawer.InvalidateVisual();
00268 }
00269
00270 private void ComputeButton_Click(object sender, RoutedEventArgs e)
00271 {
00272 Graph.Settings.GridSize = (int)Math.Round(GridSizeSlider.Value);
00273 Graph.Settings.KdeRadius = KDEAngleSlider.Value;
00274 Graph.Settings.KdeThreshold = KDEThresholdSlider.Value;
00275 MergedSegment.threshold = MergeAngleSlider.Value;
00276
00277 if (_graph == null)
00278 return;
00279
00280 _graph.Compute();
00281 gdrawer.InvalidateVisual();
00282 }
00283
00284 private void OriginalGraphColor_SelectedColorChanged(Color obj)
00285 {
00286
00287 GraphDrawer.Settings.GraphColor = obj;
00288
00289 gdrawer.InvalidateVisual();
00290 }
00291
00292 private void Segments_ColorChanged(Color obj)
00293 {
00294 GraphDrawer.Settings.SegmentsColor = obj;
00295
00296 gdrawer.InvalidateVisual();
00297 }
00298
00299 private void MergedSegments_ColorChanged(Color obj)
00300 {
00301 GraphDrawer.Settings.MergedSegmentsColor = obj;
00302 gdrawer.InvalidateVisual();
00303 }
00304
00305 private void KDEPeaks_ColorChanged(Color obj)
00306 {
00307 GraphDrawer.Settings.KDEPeakColor = obj;
00308 gdrawer.InvalidateVisual();
00309 }
00310
00311 private void SegmentsNormal_ColorChanged(Color obj)
00312 {
00313 GraphDrawer.Settings.ControlMeshEdgesColor = obj;
00314 gdrawer.InvalidateVisual();
00315 }
00316
00317 private void ControlPoints_SelectedColorChanged(Color obj)
00318 {
00319 GraphDrawer.Settings.ControlPointsColor = obj;
00320 gdrawer.InvalidateVisual();
00321 }
00322
00323 private void CustomColorPicker_SelectedColorChanged(Color obj)
00324 {
00325 GraphDrawer.Settings.ControlMeshColor = obj;
00326 gdrawer.InvalidateVisual();
00327 }
00328
00329 private void CustomColorPicker_SelectedColorChanged_1(Color obj)
00330 {
00331 GraphDrawer.Settings.EnhancedGraphColor = obj;
00332 gdrawer.InvalidateVisual();
00333 }
00334 public void InitSettings()
00335 {
00336 GridSizeSlider.Value = Graph.Settings.GridSize;
00337 GraphVisibleCheckbox.IsChecked = GraphDrawer.Settings.graphEdgesVisible;
00338 OriginalGraphColor.SelectedColor = GraphDrawer.Settings.GraphColor;
00339 KDEAngleSlider.Value = Graph.Settings.KdeRadius;
00340 KDEAngleTextBlock.Text = Graph.Settings.KdeRadius + "°";
00341 GridSizeTextBlock.Text = Graph.Settings.GridSize+"";
00342
00343 KDEThresholdSlider.Value = Graph.Settings.KdeThreshold;
00344 KDEThresholdTextblock.Text = string.Format("{0:0.00}", KDEThresholdSlider.Value * 100) + "%";
00345
00346 MergeAngleSlider.Value = MergedSegment.threshold;
00347 MergeAngleTextbox.Text = string.Format("{0:0}", MergeAngleSlider.Value) + "°";
00348
00349 SegmentNormalsVisibleCheckbox.IsChecked = GraphDrawer.Settings.controlMeshEdgesVisible;
00350 KDEPeaksVisibleCheckbox.IsChecked = GraphDrawer.Settings.kernelPeaksVisible;
00351 MergedSegmentsVisibleCheckbox.IsChecked = GraphDrawer.Settings.mergedSegmentsVisible;
00352 SegmentsVisibleCheckbox.IsChecked = GraphDrawer.Settings.segmentsVisible;
00353 ControlPointsVisibleCheckbox.IsChecked = GraphDrawer.Settings.controlPointsVisible;
00354 ControlMeshVisibleCheckbox.IsChecked = GraphDrawer.Settings.controlMeshVisible;
00355 EnhancedGraphVisibleCeckbox.IsChecked = GraphDrawer.Settings.enhancedGraphVisible;
00356
00357 SettingsCanvas.InvalidateVisual();
00358 }
00359
00360 private void GraphVisibleCheckbox_Click(object sender, RoutedEventArgs e)
00361 {
00362 GraphDrawer.Settings.graphEdgesVisible = (GraphVisibleCheckbox.IsChecked == true);
00363 gdrawer.InvalidateVisual();
00364 }
00365
00366 private void SegmentsVisibleCheckbox_Click(object sender, RoutedEventArgs e)
00367 {
00368 GraphDrawer.Settings.segmentsVisible = (SegmentsVisibleCheckbox.IsChecked == true);
00369 gdrawer.InvalidateVisual();
00370 }
00371
00372 private void MergedSegmentsVisibleCheckbox_Click(object sender, RoutedEventArgs e)
00373 {
00374 GraphDrawer.Settings.mergedSegmentsVisible = (MergedSegmentsVisibleCheckbox.IsChecked == true);
00375 gdrawer.InvalidateVisual();
00376 }
00377
00378 private void KDEPeaksVisibleCheckbox_Click(object sender, RoutedEventArgs e)
00379 {
00380 GraphDrawer.Settings.kernelPeaksVisible = (KDEPeaksVisibleCheckbox.IsChecked == true);
00381 gdrawer.InvalidateVisual();
00382 }
00383
00384 private void SegmentNormalsVisibleCheckbox_Click(object sender, RoutedEventArgs e)
00385 {
00386 GraphDrawer.Settings.controlMeshEdgesVisible = (SegmentNormalsVisibleCheckbox.IsChecked == true);
00387 gdrawer.InvalidateVisual();
00388 }
00389
00390 private void ControlPointsVisibleCheckbox_Click(object sender, RoutedEventArgs e)
00391 {
00392 GraphDrawer.Settings.controlPointsVisible = (ControlPointsVisibleCheckbox.IsChecked == true);
00393 gdrawer.InvalidateVisual();
00394 }
00395
00396 private void ControlMeshVisibleCheckbox_Click(object sender, RoutedEventArgs e)
00397 {
00398 GraphDrawer.Settings.controlMeshVisible = (ControlMeshVisibleCheckbox.IsChecked == true);
00399 gdrawer.InvalidateVisual();
00400 }
00401
00402 private void EnhancedGraphVisibleCeckbox_Click(object sender, RoutedEventArgs e)
00403 {
00404 GraphDrawer.Settings.enhancedGraphVisible = (EnhancedGraphVisibleCeckbox.IsChecked == true);
00405 gdrawer.InvalidateVisual();
00406 }
00407
00408 private void GridSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
00409 {
00410 if (GridSizeTextBlock != null)
00411 GridSizeTextBlock.Text = string.Format("{0:0}", GridSizeSlider.Value);
00412 }
00413
00414 private void KDEAngleSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
00415 {
00416 if (KDEAngleTextBlock != null)
00417 {
00418 KDEAngleTextBlock.Text = string.Format("{0:0}", KDEAngleSlider.Value)+"°";
00419
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429 }
00430
00431 private void KDEThresholdSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
00432 {
00433 if (KDEThresholdTextblock != null)
00434 KDEThresholdTextblock.Text = string.Format("{0:0.00}", KDEThresholdSlider.Value*100) + "%";
00435 }
00436
00437 private void MergeAngleSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
00438 {
00439 if (MergeAngleTextbox != null)
00440 MergeAngleTextbox.Text = string.Format("{0:0}", MergeAngleSlider.Value) + "°";
00441 }
00442
00443 private void UpdateGUI()
00444 {
00445 GraphVisibleCheckbox.IsChecked = GraphDrawer.Settings.graphEdgesVisible;
00446 SegmentsVisibleCheckbox.IsChecked = GraphDrawer.Settings.segmentsVisible;
00447 SegmentNormalsVisibleCheckbox.IsChecked = GraphDrawer.Settings.graphEdgesVisible;
00448 EnhancedGraphVisibleCeckbox.IsChecked = GraphDrawer.Settings.enhancedGraphVisible;
00449 MergedSegmentsVisibleCheckbox.IsChecked = GraphDrawer.Settings.mergedSegmentsVisible;
00450 KDEPeaksVisibleCheckbox.IsChecked = GraphDrawer.Settings.kernelPeaksVisible;
00451 ControlPointsVisibleCheckbox.IsChecked = GraphDrawer.Settings.controlPointsVisible;
00452 ControlMeshVisibleCheckbox.IsChecked = GraphDrawer.Settings.controlMeshVisible;
00453
00454 GridSizeSlider.Value = Graph.Settings.GridSize;
00455 KDEAngleSlider.Value = Graph.Settings.KdeRadius;
00456 KDEThresholdSlider.Value = Graph.Settings.KdeThreshold;
00457 MergeAngleSlider.Value = MergedSegment.threshold;
00458 }
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475 }
00476 }