00001 package commandline.baseMeshGeneration.delaunaypackage;
00002 import java.awt.Color;
00003
00004 import commandline.debuggingCode.Debug;
00005
00006
00007
00008
00009 public class QuadraticAlgorithm extends TriangulationAlgorithm {
00010 int s, t, u, bP;
00011 Circle bC = new Circle();
00012 final static String algName = "O(n^2)";
00013
00014 public QuadraticAlgorithm(){
00015 super();
00016 }
00017
00018 public QuadraticAlgorithm(Triangulation t, RealWindow w, int nPoints) {
00019 super(t, w, algName, nPoints);
00020 }
00021
00022 public void reset() {
00023 triCanvas.needToClear = true;
00024 super.reset();
00025 }
00026
00027 public void draw(RealWindowGraphics rWG, Triangulation tri) {
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 tri.draw(rWG, Color.black, Color.black);
00052
00053 }
00054
00055 public synchronized void triangulate(Triangulation tri) {
00056 if(Debug.DEBUG_DELAUNAY_TRIANGULATION)Debug.print("start triangulating");
00057 int seedEdge,currentEdge;
00058 int nFaces;
00059 Int s, t;
00060
00061
00062 nFaces = 0;
00063 s = new Int();
00064 t = new Int();
00065
00066
00067 findClosestNeighbours(tri.point, tri.nPoints, s, t);
00068
00069
00070 if(Debug.DEBUG_DELAUNAY_TRIANGULATION)Debug.print("begin edge van de triangulatie, de edge het dichts bij de oorsprong.");
00071 seedEdge = tri.addEdge(s.getValue(), t.getValue(),
00072 Triangulation.Undefined,
00073 Triangulation.Undefined);
00074
00075 currentEdge = 0;
00076 while (currentEdge < tri.nEdges) {
00077 if (tri.edge[currentEdge].leftTriangle == Triangulation.Undefined) {
00078 nFaces= completeFacet(currentEdge, tri, nFaces);
00079
00080 }
00081 if (tri.edge[currentEdge].rightTriangle == Triangulation.Undefined) {
00082 nFaces= completeFacet(currentEdge, tri, nFaces);
00083
00084 }
00085 currentEdge++;
00086 }
00087 tri.setnFaces(nFaces);
00088 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) tri.print(System.out);
00089 }
00090
00091
00092 public void findClosestNeighbours(RealPoint p[], int nPoints,
00093 Int u, Int v) {
00094 int i, j;
00095 float d, min;
00096 int s, t;
00097
00098 s = t = 0;
00099 min = Float.MAX_VALUE;
00100 for (i = 0; i < nPoints-1; i++)
00101 for (j = i+1; j < nPoints; j++)
00102 {
00103 d = p[i].distanceSq(p[j]);
00104 if (d < min)
00105 {
00106 s = i;
00107 t = j;
00108 min = d;
00109 }
00110 }
00111 u.setValue(s);
00112 v.setValue(t);
00113 }
00114
00115
00116
00117
00118
00119
00120
00121 public int completeFacet(int eI, Triangulation tri, int nFaces) {
00122
00123 float cP;
00124 Edge e[] = tri.edge;
00125 RealPoint p[] = tri.point;
00126
00127
00128 if (e[eI].leftTriangle == Triangulation.Undefined)
00129 {
00130 s = e[eI].beginPunt;
00131 t = e[eI].eindPunt;
00132 }
00133 else if (e[eI].rightTriangle == Triangulation.Undefined)
00134 {
00135 s = e[eI].eindPunt;
00136 t = e[eI].beginPunt;
00137 }
00138 else
00139
00140 return nFaces;
00141
00142
00143
00144 for (u = 0; u < tri.nPoints; u++)
00145 {
00146 if (u == s || u == t)
00147 continue;
00148 if (Vector.crossProduct(p[s], p[t], p[u]) > 0.0){
00149 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print("eerste punt aan de linkerkant van de edge is gevonden");
00150 break; }
00151 }
00152
00153
00154 bP = u;
00155 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print("onderzoek nu nog de overblijvende punten (kan ook zijn dat er geen gevonden zijn)");
00156 if (bP < tri.nPoints)
00157 {
00158 bC.circumCircle(p[s], p[t], p[bP]);
00159
00160
00161
00162 for (u = bP+1; u < tri.nPoints; u++)
00163 {
00164 if (u == s || u == t)
00165 continue;
00166
00167
00168
00169 cP = Vector.crossProduct(p[s], p[t], p[u]);
00170
00171 if (cP > 0.0)
00172 if (bC.inside(p[u]))
00173 {
00174
00175 bP = u;
00176 bC.circumCircle(p[s], p[t], p[u]);
00177
00178 }
00179 }
00180 }
00181
00182
00183 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print("voeg een nieuwe driehoek toe of, als de edge op de hull ligt, update de informatie");
00184 if (bP < tri.nPoints)
00185 {
00186
00187
00188
00189 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print("zorg ervoor dat de linker driehoek van (s,t) naar de nieuwe driehoek wijst");
00190 tri.updateLeftFace(eI, s, t, nFaces);
00191
00192
00193
00194 eI = tri.findEdge(bP, s);
00195 if (eI == Triangulation.Undefined){
00196
00197 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" er bestaat nog geen edge tussen het beste punt bP en s");
00198 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" de linkerdriehoek van de edge (bP,s) is de nieuwe driehoek");
00199 eI = tri.addEdge(bP, s, nFaces, Triangulation.Undefined);
00200 tri.face[nFaces].addEdge(eI);
00201 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" tri.face["+nFaces+"].addEdge("+(eI)+"); // me ");
00202 }
00203 else{
00204
00205 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" edge (bP,s) bestaat al: update alle betrokken info");
00206 tri.updateLeftFace(eI, bP, s, nFaces);
00207 }
00208
00209
00210
00211 eI = tri.findEdge(t, bP);
00212 if (eI == Triangulation.Undefined){
00213
00214 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" er bestaat nog geen edge tussen het beste punt bP en t");
00215 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" de linkerdriehoek van de edge (t,bP) is de nieuwe driehoek");
00216 eI = tri.addEdge(t, bP, nFaces, Triangulation.Undefined);
00217 tri.face[nFaces].addEdge(eI);
00218 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" tri.face["+nFaces+"].addEdge("+(eI)+"); // me ");
00219 }
00220 else{
00221
00222 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" edge (t,bP) bestaat al: update alle betrokken info");
00223 tri.updateLeftFace(eI, t, bP, nFaces);}
00224
00225 if(Debug.DEBUG_DELAUNAY_TRIANGULATION)Debug.print("maak een nieuwe driehoek (bP,s) (s,t) (t,bP)");
00226 if(Debug.DEBUG_DELAUNAY_TRIANGULATION)Debug.print("verhoog het aantal driehoeken");
00227 nFaces++;
00228 }
00229 else{
00230 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print("geen nieuwe driehoek, edge (s,t) op de hull");
00231 tri.updateLeftFace(eI, s, t, Triangulation.Universe);
00232 }
00233 return nFaces;
00234 }
00235
00236 }