00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using Triangulator;
00006 using System.Windows;
00007
00008 namespace EdgeClustering
00009 {
00010
00011
00012
00013
00014
00015 class ControlMesh
00016 {
00017 List<ControlMeshEdge> edges = new List<ControlMeshEdge>();
00018
00019
00020
00021
00022 public List<ControlMeshEdge> Edges
00023 {
00024 get { return edges; }
00025 }
00026
00027 List<ControlMeshVertex> vertices = new List<ControlMeshVertex>();
00028
00029
00030
00031 public List<ControlMeshVertex> Vertices
00032 {
00033 get { return vertices; }
00034 }
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 public void GenerateTestControlMesh(List<SimpleEdge> initialEdges)
00045 {
00046 edges = new List<ControlMeshEdge>();
00047 vertices = new List<ControlMeshVertex>();
00048
00049 foreach (SimpleEdge s in initialEdges)
00050 {
00051 ControlMeshVertex v1,v2;
00052 v1 = new ControlMeshVertex(s.A.X,s.A.Y);
00053 v2 = new ControlMeshVertex(s.B.X,s.B.Y);
00054
00055
00056 if (!vertices.Contains(v1))
00057 vertices.Add(v1);
00058 else
00059 v1 = vertices[vertices.IndexOf(v1)];
00060
00061
00062 if (!vertices.Contains(v2))
00063 vertices.Add(v2);
00064 else
00065 v2 = vertices[vertices.IndexOf(v2)];
00066
00067
00068 edges.Add(new ControlMeshEdge(ref v1,ref v2));
00069 }
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 public List<EnhancedEdge> Intersect(List<SimpleEdge> originalEdges)
00081 {
00082 List<EnhancedEdge> enhancedEdges = new List<EnhancedEdge>();
00083
00084 foreach (SimpleEdge s in originalEdges)
00085 enhancedEdges.Add(new EnhancedEdge(new SimpleEdge(
00086 new Point(s.A.X / Graph.gridScale, s.A.Y / Graph.gridScale),
00087 new Point(s.B.X / Graph.gridScale, s.B.Y / Graph.gridScale))));
00088
00089 for (int i = 0; i < edges.Count; i++)
00090 edges[i].Intersect(ref enhancedEdges);
00091
00092 return enhancedEdges;
00093 }
00094
00095
00096
00097
00098
00099 public void CalculateControlMesh(List<SimpleEdge> initialEdges)
00100 {
00101 vertices = new List<ControlMeshVertex>();
00102 edges = new List<ControlMeshEdge>();
00103
00104 foreach (SimpleEdge s in initialEdges)
00105 {
00106 ControlMeshVertex v1, v2;
00107 v1 = new ControlMeshVertex(s.A.X, s.A.Y);
00108 v2 = new ControlMeshVertex(s.B.X, s.B.Y);
00109
00110
00111 if (!vertices.Contains(v1))
00112 vertices.Add(v1);
00113 else
00114 v1 = vertices[vertices.IndexOf(v1)];
00115
00116
00117 if (!vertices.Contains(v2))
00118 vertices.Add(v2);
00119 else
00120 v2 = vertices[vertices.IndexOf(v2)];
00121
00122
00123 edges.Add(new ControlMeshEdge(ref v1, ref v2));
00124 }
00125
00126 List<Triangulator.Geometry.Triangle> delaunayTriangles = Triangulator.Delaunay.Triangulate(vertices);
00127
00128 if (delaunayTriangles != null)
00129 {
00130 edges = new List<ControlMeshEdge>();
00131
00132 foreach (Triangulator.Geometry.Triangle t in delaunayTriangles)
00133 {
00134 List<ControlMeshEdge> temp = new List<ControlMeshEdge>();
00135 ControlMeshVertex v1, v2, v3;
00136 v1 = vertices[t.p1];
00137 v2 = vertices[t.p2];
00138 v3 = vertices[t.p3];
00139
00140 temp.Add(new ControlMeshEdge(ref v1, ref v2));
00141 temp.Add(new ControlMeshEdge(ref v2, ref v3));
00142 temp.Add(new ControlMeshEdge(ref v3, ref v1));
00143
00144 foreach (ControlMeshEdge e in temp)
00145 {
00146 if (!edges.Contains(e))
00147 edges.Add(e);
00148 }
00149 }
00150 }
00151 }
00152
00153 }
00154 }