00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using System.Windows;
00006
00007 namespace EdgeClustering
00008 {
00009
00010
00011
00012
00013
00014 class ControlMeshEdge
00015 {
00016 private ControlMeshVertex v1;
00017
00018
00019
00020 public ControlMeshVertex V1
00021 {
00022 get { return v1; }
00023 set { v1 = value; UpdateBoundingBox(); }
00024 }
00025
00026 private ControlMeshVertex v2;
00027
00028
00029
00030 public ControlMeshVertex V2
00031 {
00032 get { return v2; }
00033 set { v2 = value; UpdateBoundingBox(); }
00034 }
00035
00036
00037
00038 private int[,] bbox;
00039
00040
00041
00042
00043
00044
00045 public ControlMeshEdge(ref ControlMeshVertex v1, ref ControlMeshVertex v2)
00046 {
00047 this.v1 = v1;
00048 this.v2 = v2;
00049 UpdateBoundingBox();
00050 }
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 public void Intersect(ref List<EnhancedEdge> edges)
00062 {
00063 int[,] otherBBox;
00064 List<EnhancedEdge> myEdges = new List<EnhancedEdge>();
00065 List<Vector> points = new List<Vector>();
00066
00067 Vector U1 = new Vector(v1.X, v1.Y);
00068 Vector U2 = new Vector(v2.X, v2.Y);
00069 Vector u = U2 - U1;
00070 double frac = u.Y / u.X;
00071
00072 for (int i = 0; i < edges.Count; i++)
00073 {
00074
00075 otherBBox = CalculateBoundingBoxFor(edges[i]);
00076 if (bbox[0, 0] > otherBBox[0, 1] || bbox[0, 1] < otherBBox[0, 0] ||
00077 bbox[1, 0] > otherBBox[1, 1] || bbox[1, 1] < otherBBox[1, 0])
00078 continue;
00079
00080
00081 Vector V1 = new Vector(edges[i].A.X, edges[i].A.Y);
00082 Vector V2 = new Vector(edges[i].B.X, edges[i].B.Y);
00083 Vector v = V2 - V1;
00084 Vector p;
00085
00086 if (u.X != 0)
00087 {
00088 double innerfrac = (v.Y - v.X * frac);
00089 if (innerfrac == 0) continue;
00090
00091 double t = (U1.Y + frac * V1.X - frac * U1.X - V1.Y) / innerfrac;
00092 p = V1 + v * t;
00093 }
00094 else
00095 {
00096 if (v.X != 0)
00097 {
00098 double t = (U1.X - V1.X) / v.X;
00099 double y = V1.Y + v.Y / v.X * (U1.X - V1.X);
00100 p = new Vector(U1.X, y);
00101 }
00102 else
00103 {
00104 continue;
00105 }
00106 }
00107
00108
00109 if (!( ((p.X >= V1.X && p.X <= V2.X) || (p.X >= V2.X && p.X <= V1.X)) &&
00110 ((p.Y >= V1.Y && p.Y <= V2.Y) || (p.Y >= V2.Y && p.Y <= V1.Y)) &&
00111 ((p.X >= U1.X && p.X <= U2.X) || (p.X >= U2.X && p.X <= U1.X)) &&
00112 ((p.Y >= U1.Y && p.Y <= U2.Y) || (p.Y >= U2.Y && p.Y <= U1.Y)) ))
00113 continue;
00114
00115
00116 myEdges.Add(edges[i]);
00117
00118 points.Add(p);
00119 }
00120
00121
00122 if (points.Count == 0) return;
00123
00124
00125 Vector mean = new Vector();
00126 foreach (Vector p in points)
00127 {
00128 mean += p;
00129 }
00130
00131 mean /= points.Count;
00132
00133 Point controlPoint = new Point(mean.X, mean.Y);
00134
00135
00136 foreach (EnhancedEdge ee in myEdges)
00137 {
00138 ee.AddControlPoint(controlPoint);
00139 }
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 private int[,] CalculateBoundingBoxFor(EnhancedEdge ee)
00157 {
00158 int[,] bb = new int[2, 2];
00159 bb[0, 0] = int.MaxValue;
00160 bb[1, 0] = int.MaxValue;
00161
00162
00163 if (ee.A.X > bb[0, 1]) bb[0, 1] = (int)ee.A.X + 1;
00164 if (ee.A.X < bb[0, 0]) bb[0, 0] = (int)ee.A.X;
00165 if (ee.A.Y > bb[1, 1]) bb[1, 1] = (int)ee.A.Y + 1;
00166 if (ee.A.Y < bb[1, 0]) bb[1, 0] = (int)ee.A.Y;
00167
00168 if (ee.B.X > bb[0, 1]) bb[0, 1] = (int)ee.B.X + 1;
00169 if (ee.B.X < bb[0, 0]) bb[0, 0] = (int)ee.B.X;
00170 if (ee.B.Y > bb[1, 1]) bb[1, 1] = (int)ee.B.Y + 1;
00171 if (ee.B.Y < bb[1, 0]) bb[1, 0] = (int)ee.B.Y;
00172
00173 return bb;
00174 }
00175
00176
00177
00178
00179 private void UpdateBoundingBox()
00180 {
00181 if (bbox == null)
00182 {
00183 bbox = new int[2, 2];
00184 bbox[0, 0] = int.MaxValue;
00185 bbox[1, 0] = int.MaxValue;
00186
00187 }
00188
00189 if (v1.X > bbox[0, 1]) bbox[0, 1] = (int)v1.X + 1;
00190 if (v1.X < bbox[0, 0]) bbox[0, 0] = (int)v1.X;
00191 if (v1.Y > bbox[1, 1]) bbox[1, 1] = (int)v1.Y + 1;
00192 if (v1.Y < bbox[1, 0]) bbox[1, 0] = (int)v1.Y;
00193
00194 if (v2.X > bbox[0, 1]) bbox[0, 1] = (int)v2.X + 1;
00195 if (v2.X < bbox[0, 0]) bbox[0, 0] = (int)v2.X;
00196 if (v2.Y > bbox[1, 1]) bbox[1, 1] = (int)v2.Y + 1;
00197 if (v2.Y < bbox[1, 0]) bbox[1, 0] = (int)v2.Y;
00198 }
00199 }
00200 }