00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using System.Windows;
00006 using System.Windows.Shapes;
00007 using System.Windows.Media;
00008 using System.Windows.Media.Imaging;
00009 using System.Collections;
00010 using System.Globalization;
00011
00012 namespace EdgeClustering
00013 {
00014
00015
00016
00017
00018
00019
00020 class GraphDrawer:FrameworkElement
00021 {
00022
00023
00024
00025
00026
00027 public struct Settings
00028 {
00029 public static bool graphEdgesVisible = true;
00030 public static bool segmentsVisible = false;
00031 public static bool mergedSegmentsVisible = false;
00032 public static bool kernelPeaksVisible = false;
00033 public static bool controlMeshEdgesVisible = true;
00034 public static bool controlPointsVisible = false;
00035
00036 public static bool controlMeshVisible = true;
00037 public static bool enhancedGraphVisible = true;
00038
00039 public static Color GraphColor = Colors.Black;
00040 public static Color SegmentsColor = Colors.Red;
00041 public static Color MergedSegmentsColor = Colors.BlueViolet;
00042 public static Color KDEPeakColor = Colors.Green;
00043 public static Color ControlMeshEdgesColor = Colors.Aquamarine;
00044 public static Color ControlPointsColor = Colors.HotPink;
00045 public static Color ControlMeshColor = Colors.Green;
00046 public static Color EnhancedGraphColor = Colors.GreenYellow;
00047 }
00048
00049
00050
00051 private Graph graph;
00052 public Graph Graph
00053 {
00054 set { graph = value; }
00055 }
00056
00057 private int windowSize;
00058
00059
00060
00061
00062
00063 public GraphDrawer(int windowSize)
00064 {
00065 this.Width = windowSize;
00066 this.Height = windowSize;
00067
00068 this.Clip = new RectangleGeometry(new Rect(0, 0, windowSize, windowSize));
00069 this.ClipToBounds = true;
00070
00071 this.windowSize = windowSize;
00072 }
00073
00074
00075
00076
00077
00078
00079 protected override void OnRender(System.Windows.Media.DrawingContext dc)
00080 {
00081 if (graph == null)
00082 return;
00083
00084
00085 DrawGrid(dc, Colors.Gray);
00086 if((Settings.enhancedGraphVisible && graph.ComputingStage >=6) || Settings.graphEdgesVisible)
00087 DrawGraphPoints(dc, graph.Points);
00088
00089 if (Settings.graphEdgesVisible)
00090 DrawGraphEdges(dc, graph.SimpleEdges, Settings.GraphColor);
00091
00092 if (graph.ComputingStage >= 1 && Settings.segmentsVisible)
00093 DrawSegmentAngles(dc, Settings.SegmentsColor,graph.Grid);
00094
00095 if (graph.ComputingStage >= 2 && Settings.kernelPeaksVisible)
00096 DrawKernelPeak(dc, Settings.KDEPeakColor, graph.KernelPeaks);
00097
00098 if (graph.ComputingStage >= 3 && Settings.mergedSegmentsVisible)
00099 DrawMergedSegments(dc,graph.MergedSegments, Settings.MergedSegmentsColor);
00100
00101 if (graph.ComputingStage >= 4 && Settings.controlMeshEdgesVisible)
00102 DrawMeshEdges(dc, Settings.ControlMeshEdgesColor, graph.MeshEdges);
00103
00104 if (graph.ComputingStage >= 5 && Settings.controlMeshVisible)
00105 DrawControlMesh(dc, Settings.ControlMeshColor, graph.ControlMesh.Edges, graph.ControlMesh.Vertices);
00106
00107 if (graph.ComputingStage >= 6 && Settings.controlPointsVisible)
00108 DrawControlPoints(dc, Settings.ControlPointsColor, graph.EnhancedEdges);
00109
00110
00111
00112 if (graph.ComputingStage >= 6 && Settings.enhancedGraphVisible)
00113 DrawEnhancedGraph(dc, Settings.EnhancedGraphColor, graph.EnhancedEdges);
00114
00115
00116
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 private void DrawEnhancedGraph(DrawingContext dc, Color c, List<EnhancedEdge> edges)
00128 {
00129 SolidColorBrush Brush = new SolidColorBrush(c);
00130
00131 Pen Pen = new Pen(Brush, 4);
00132
00133 double delta = (double)windowSize / Graph.Settings.GridSize;
00134
00135 foreach (EnhancedEdge ee in edges)
00136 {
00137
00138
00139 List<Point> points = new List<Point>();
00140
00141 ControlMeshVertex vA,vB;
00142 vA = new ControlMeshVertex(ee.A.X,ee.A.Y);
00143 vB = new ControlMeshVertex(ee.B.X,ee.B.Y);
00144
00145 Point end;
00146
00147
00148 if (vA.CompareTo(vB) < 0)
00149 {
00150 ee.SortControlPoints(vA.Point);
00151 points.Add(new Point(vA.X*delta,vA.Y*delta));
00152 end = vB.Point;
00153 }
00154 else
00155 {
00156 ee.SortControlPoints(vB.Point);
00157 points.Add(new Point(vB.X*delta,vB.Y*delta));
00158 end = vA.Point;
00159 }
00160
00161
00162
00163 foreach (ControlMeshVertex v in ee.ControlPoints)
00164 {
00165 points.Add(new Point(v.X*delta,v.Y*delta));
00166 }
00167 points.Add(new Point(end.X*delta,end.Y*delta));
00168
00169 dc.DrawGeometry(null, Pen, CardinalSpline.CalculateCardinalSplinePath(points.ToArray(), 0.5));
00170 }
00171 }
00172
00173
00174
00175
00176
00177
00178
00179 private void DrawControlPoints(DrawingContext dc, Color c, List<EnhancedEdge> edges)
00180 {
00181 SolidColorBrush pointBrush = new SolidColorBrush(c);
00182 Pen pointsPen = new Pen(pointBrush, 4);
00183
00184 double delta = (double)windowSize / Graph.Settings.GridSize;
00185
00186 foreach (EnhancedEdge ee in edges)
00187 foreach (ControlMeshVertex p in ee.ControlPoints)
00188 dc.DrawEllipse(null, pointsPen, new Point(p.X * delta, p.Y * delta), 4, 4);
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198 private void DrawControlMesh(DrawingContext dc, Color c, List<ControlMeshEdge> edges, List<ControlMeshVertex> vertices)
00199 {
00200 SolidColorBrush edgeBrush = new SolidColorBrush(c);
00201
00202 Pen pen = new Pen(edgeBrush, 3);
00203
00204 double delta = (double)windowSize / Graph.Settings.GridSize;
00205
00206 foreach(ControlMeshEdge e in edges)
00207 dc.DrawLine(pen,new Point(e.V1.X*delta,e.V1.Y*delta),new Point(e.V2.X*delta,e.V2.Y*delta));
00208
00209 foreach(ControlMeshVertex v in vertices)
00210 dc.DrawRectangle(null,pen,new Rect(v.X*delta -2,v.Y*delta -2,4,4));
00211 }
00212
00213
00214
00215
00216
00217
00218 private void DrawMeshEdges(DrawingContext dc, Color c,List<SimpleEdge> meshEdges)
00219 {
00220 SolidColorBrush edgeBrush = new SolidColorBrush(c);
00221
00222 Pen pen = new Pen(edgeBrush, 4);
00223 Point p1, p2;
00224
00225 double delta = (double)windowSize / Graph.Settings.GridSize;
00226
00227 foreach (SimpleEdge se in meshEdges)
00228 {
00229 p1 = new Point(se.A.X * delta, se.A.Y * delta);
00230 p2 = new Point(se.B.X * delta, se.B.Y * delta);
00231 dc.DrawLine(pen, p1, p2);
00232 }
00233 }
00234
00235
00236
00237
00238
00239
00240 private void DrawMergedSegments(DrawingContext dc, List<MergedSegment> mergedSegments, Color color)
00241 {
00242
00243 SolidColorBrush brush = new SolidColorBrush(color);
00244
00245 Pen pen = new Pen(brush, 2);
00246
00247 double delta = (double)windowSize / Graph.Settings.GridSize;
00248
00249 for (int i = 0; i < mergedSegments.Count; i++)
00250 {
00251 foreach (MergedSegment.Segment s in mergedSegments[i].segments)
00252 {
00253
00254 FormattedText label = new FormattedText(
00255 i + "",
00256 CultureInfo.GetCultureInfo("de"),
00257 FlowDirection.LeftToRight,
00258 new Typeface("Verdana"),
00259 8,
00260 Brushes.Black);
00261
00262 dc.DrawText(label, new Point((s.x + 0.5) * delta, (s.y + 0.5) * delta));
00263
00264
00265 FormattedText averageAngle = new FormattedText(
00266 String.Format("{0:0.0}", mergedSegments[i].averageAngle),
00267 CultureInfo.GetCultureInfo("de"),
00268 FlowDirection.LeftToRight,
00269 new Typeface("Verdana"),
00270 4,
00271 Brushes.Gray);
00272
00273 dc.DrawText(averageAngle, new Point((s.x) * delta, (s.y) * delta));
00274
00275 DrawAngle(dc, pen, s.x, s.y, mergedSegments[i].averageAngle);
00276 }
00277 }
00278 }
00279
00280
00281
00282
00283
00284
00285 private void DrawKernelPeak(DrawingContext dc, Color color, double[,] kernelPeaks)
00286 {
00287 SolidColorBrush brush = new SolidColorBrush(color);
00288
00289 Pen pen = new Pen(brush, 2);
00290
00291 double delta = (double)windowSize / Graph.Settings.GridSize;
00292
00293 for (int x = 0; x < Graph.Settings.GridSize; x++)
00294 {
00295 for (int y = 0; y < Graph.Settings.GridSize; y++)
00296 {
00297 if (kernelPeaks[x, y] != -1)
00298 DrawAngle(dc, pen, x, y, kernelPeaks[x, y]);
00299 }
00300 }
00301 }
00302
00303
00304
00305
00306
00307
00308
00309 private void DrawSegmentAngles(DrawingContext dc, Color color, ArrayList[,] grid)
00310 {
00311
00312 SolidColorBrush brush = new SolidColorBrush(color);
00313
00314 Pen pen = new Pen(brush, 4);
00315
00316 double delta = (double)windowSize / Graph.Settings.GridSize;
00317
00318
00319 for (int x = 0; x < Graph.Settings.GridSize; x++)
00320 {
00321 for (int y = 0; y < Graph.Settings.GridSize; y++)
00322 {
00323 if (grid[x, y] != null)
00324 foreach (double d in grid[x, y])
00325 DrawAngle(dc, pen, x, y, d);
00326 }
00327 }
00328 }
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 private void DrawAngle(DrawingContext dc, Pen pen, int x, int y, double angle)
00339 {
00340 double delta = (double)windowSize / Graph.Settings.GridSize;
00341
00342 Point p1 = new Point((x + 0.5 + Math.Cos(angle * Math.PI / 180.0) * 0.5) * delta, (y + 0.5 + Math.Sin(angle * Math.PI / 180.0) * 0.5) * delta);
00343 Point p2 = new Point((x + 0.5 - Math.Cos(angle * Math.PI / 180.0) * 0.5) * delta, (y + 0.5 - Math.Sin(angle * Math.PI / 180.0) * 0.5) * delta);
00344 dc.DrawLine(pen, p1, p2);
00345 }
00346
00347
00348
00349
00350
00351
00352 private void DrawGrid(System.Windows.Media.DrawingContext dc, Color color)
00353 {
00354 SolidColorBrush gridBrush = new SolidColorBrush(color);
00355 Pen gridPen = new Pen(gridBrush, 0.5);
00356
00357 Point a, b;
00358 double delta = (double)windowSize / Graph.Settings.GridSize;
00359
00360 a = new Point(0, 0);
00361 b = new Point(0, windowSize);
00362
00363
00364 for (int i = 0; i <= Graph.Settings.GridSize; i++)
00365 {
00366 a.X = i * delta;
00367 b.X = i * delta;
00368 dc.DrawLine(gridPen, a, b);
00369 }
00370
00371 a.X = 0;
00372 b.X = windowSize;
00373
00374
00375 for (int i = 0; i <= Graph.Settings.GridSize; i++)
00376 {
00377 a.Y = i * delta;
00378 b.Y = i * delta;
00379 dc.DrawLine(gridPen, a, b);
00380 }
00381 }
00382
00383
00384
00385
00386
00387
00388 private void DrawGraphPoints(DrawingContext dc, PointCollection points)
00389 {
00390 double scale = (double)windowSize / graph.MaxPointValue;
00391
00392 SolidColorBrush pointBrush = new SolidColorBrush(Colors.LightGreen);
00393 Pen pointsPen = new Pen(pointBrush, 4);
00394
00395 foreach (Point p in points)
00396 dc.DrawEllipse(null, pointsPen, new Point(p.X*scale,p.Y*scale), 4, 4);
00397 }
00398
00399
00400
00401
00402
00403
00404 private void DrawGraphEdges(DrawingContext dc, List<SimpleEdge> edges, Color color)
00405 {
00406 double scale = (double)windowSize / graph.MaxPointValue;
00407
00408 SolidColorBrush edgeBrush = new SolidColorBrush(color);
00409
00410 color.A = 0xFF;
00411 Pen pen = new Pen(edgeBrush, 4);
00412
00413 foreach (SimpleEdge e in edges)
00414 dc.DrawLine(pen, new Point(e.A.X * scale, e.A.Y * scale), new Point(e.B.X * scale, e.B.Y * scale));
00415 }
00416
00417
00418
00419
00420
00421
00422
00423 public void DrawStreamGeometry(DrawingContext dc)
00424 {
00425 SolidColorBrush edgeBrush = new SolidColorBrush(Colors.Black);
00426
00427 Pen penLine = new Pen(edgeBrush, 4);
00428
00429 SolidColorBrush pointBrush = new SolidColorBrush(Colors.LightGreen);
00430 Pen penPoints = new Pen(pointBrush, 4);
00431
00432 Point[] pts = new Point[] { new Point(100, 50), new Point(50, 200), new Point(200, 200), new Point(250, 250), new Point(250, 300) };
00433
00434 dc.DrawGeometry(null, penLine, CardinalSpline.CalculateCardinalSplinePath(pts, 0.5));
00435
00436 foreach (Point p in pts)
00437 {
00438 dc.DrawEllipse(null, penPoints, p, 4, 4);
00439 }
00440 }
00441 }
00442 }