00001 package commandline.baseMeshGeneration.delaunaypackage;
00002 import java.awt.Color;
00003 import java.io.PrintStream;
00004
00005 import commandline.CodingUtils.MyRandom;
00006 import commandline.debuggingCode.Debug;
00007
00008
00009
00010
00011
00012
00013
00014 public class Triangulation {
00015 public static final int Undefined = -1;
00016 public static final int Universe = -2;
00017 int nPoints;
00018 RealPoint point[];
00019 int nEdges;
00020 int nFaces;
00021 int maxEdges;
00022 int maxFaces;
00023 Edge edge[];
00024 Face face[];
00025
00030 public Triangulation(RealPoint[] points){
00031 setPoints(points);
00032 init();
00033 }
00034
00040 public Triangulation(RealWindow w,int nPoints) {
00041 randomPoints(w,nPoints);
00042 init();
00043 }
00044
00045
00046 private void init(){
00047
00048 maxEdges = 3 * nPoints - 6;
00049 edge = new Edge[maxEdges];
00050 for (int i = 0; i < maxEdges; i++)
00051 edge[i] = new Edge();
00052 nEdges = 0;
00053
00054
00055
00056 maxFaces= 2*nPoints-1;
00057 face=new Face[maxFaces];
00058 for (int i=0; i < maxFaces;i++){
00059 face[i]=new Face();
00060 }
00061
00062 }
00063
00064 public void setnFaces(int nFaces){
00065 this.nFaces=nFaces;
00066 }
00067
00068
00069
00070
00071
00072
00073
00074 public void setNPoints(int nPoints) {
00075
00076 Edge tmpEdge[] = edge;
00077 int tmpMaxEdges = maxEdges;
00078 maxEdges = 3 * nPoints - 6;
00079 edge = new Edge[maxEdges];
00080
00081
00082 int minMaxEdges;
00083 if (tmpMaxEdges < maxEdges)
00084 minMaxEdges = tmpMaxEdges;
00085 else
00086 minMaxEdges = maxEdges;
00087
00088
00089 for (int i = 0; i < minMaxEdges; i++)
00090 this.edge[i] = tmpEdge[i];
00091
00092
00093 for (int i = minMaxEdges; i < maxEdges; i++)
00094 this.edge[i] = new Edge();
00095
00096
00097 RealPoint tmpPoint[] = point;
00098 point = new RealPoint[nPoints];
00099
00100
00101 int minPoints;
00102 if (nPoints < this.nPoints)
00103 minPoints = nPoints;
00104 else
00105 minPoints = this.nPoints;
00106
00107
00108 for (int i = 0; i < minPoints; i++)
00109 this.point[i] = tmpPoint[i];
00110
00111
00112 for (int i = minPoints; i < nPoints; i++)
00113 this.point[i] = new RealPoint();
00114
00115 this.nPoints = nPoints;
00116 }
00117
00118
00119
00120
00121 private void randomPoints(RealWindow w, int nPoints) {
00122 this.nPoints = nPoints;
00123 point=new RealPoint[nPoints];
00124 point[0]=new RealPoint(0,0);
00125 point[1]=new RealPoint(w.xMax()-1,w.yMax()-1);
00126 point[2]=new RealPoint(w.xMax()-1,0);
00127 point[3]=new RealPoint(0,w.yMax()-1);
00128
00129 for (int i = 4; i < nPoints; i++)
00130 {
00131 point[i]=new RealPoint();
00132 point[i].x = (float)(MyRandom.random() * w.xMax());
00133 point[i].y = (float)(MyRandom.random() * w.yMax());
00134 }
00135 nEdges = 0;
00136 }
00137
00138
00139
00140
00141 private void setPoints(RealPoint[] points){
00142 this.nPoints=points.length;
00143 point=new RealPoint[nPoints];
00144 for (int i = 0; i < nPoints; i++)
00145 {
00146 point[i]=new RealPoint();
00147 point[i].x = (float)points[i].x;
00148 point[i].y = (float)points[i].y;
00149 }
00150 nEdges = 0;
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160 public static RealPoint[] copyPoints(Triangulation t) {
00161 RealPoint[] points=new RealPoint[t.getPoints().length];
00162
00163 for(int i=0;i!=points.length;i++){
00164 points[i]=new RealPoint(t.getPoints()[i].x,t.getPoints()[i].y);
00165 }
00166 return points;
00167 }
00168
00169 void addTriangle(int s, int t, int u) {
00170 addEdge(s, t);
00171 addEdge(t, u);
00172 addEdge(u, s);
00173 }
00174
00175 public int addEdge(int s, int t) {
00176 Debug.print("hier zou een nieuwe driehoek moeten gevormd worden.");
00177 return addEdge(s, t, Undefined, Undefined);
00178 }
00179
00180
00181
00182
00183
00184 public int addEdge(int s, int t, int l, int r) {
00185
00186
00187
00188
00189
00190
00191 if (s < t)
00192 {
00193 edge[nEdges].beginPunt = s;
00194 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" point["+s+"].setEdge_index("+nEdges+");");
00195 point[s].setEdge_index(nEdges);
00196 edge[nEdges].eindPunt = t;
00197 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" point["+t+"].setEdge_index("+nEdges+");");
00198 point[t].setEdge_index(nEdges);
00199 edge[nEdges].leftTriangle = l;
00200 edge[nEdges].rightTriangle = r;
00201 return nEdges++;
00202 }
00203 else
00204 {
00205 edge[nEdges].beginPunt = t;
00206 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" point["+t+"].setEdge_index("+nEdges+");");
00207 point[t].setEdge_index(nEdges);
00208 edge[nEdges].eindPunt = s;
00209 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" point["+s+"].setEdge_index("+nEdges+");");
00210 point[s].setEdge_index(nEdges);
00211 edge[nEdges].leftTriangle = r;
00212 edge[nEdges].rightTriangle = l;
00213 return nEdges++;
00214 }
00215
00216
00217
00218 }
00219
00220 public int findEdge(int s, int t) {
00221 boolean edgeExists = false;
00222 int i;
00223
00224 for (i = 0; i < nEdges; i++)
00225 if (edge[i].beginPunt == s && edge[i].eindPunt == t ||
00226 edge[i].beginPunt == t && edge[i].eindPunt == s) {
00227 edgeExists = true;
00228 break;
00229 }
00230
00231 if (edgeExists)
00232 return i;
00233 else
00234 return Undefined;
00235 }
00236
00237
00238
00239
00240 public void updateLeftFace(int eI, int s, int t, int f) {
00241 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print("-> updateLeftFace(f)" + f);
00242 if (!((edge[eI].beginPunt == s && edge[eI].eindPunt == t) ||
00243 (edge[eI].beginPunt == t && edge[eI].eindPunt == s)))
00244 Panic.panic("updateLeftFace: adj. matrix and edge table mismatch");
00245 if(Debug.DEBUG_DELAUNAY_TRIANGULATION)Debug.print("eens kijken of de richting van bestaande edge overeenkomt met (s,t)");
00246 if (edge[eI].beginPunt == s && edge[eI].leftTriangle == Triangulation.Undefined){
00247 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" LeftTriangle is nog niet gedefinieerd.");
00248 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" edge["+eI+"].leftTriangle = "+ f);
00249
00250 if(f!=Universe){
00251 this.face[f].addEdge(eI);
00252 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" this.face["+f+"].addEdge("+eI+");");
00253 }
00254 edge[eI].leftTriangle = f;}
00255 else if (edge[eI].eindPunt == s && edge[eI].rightTriangle == Triangulation.Undefined){
00256 if(Debug.DEBUG_DELAUNAY_TRIANGULATION)Debug.print(" RightTriangle is nog niet gedefinieerd.");
00257 if(Debug.DEBUG_DELAUNAY_TRIANGULATION)Debug.print(" edge["+eI+"].rightTriangle ="+ f+";");
00258
00259 if(f!=Universe){
00260 this.face[f].addEdge(eI);
00261 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" this.face["+f+"].addEdge("+eI+");");
00262 }
00263 edge[eI].rightTriangle = f;
00264 }
00265 else {if(!Debug.DISTIBUTION) System.out.print("attempt to overwrite edge info");
00266 Panic.panic("updateLeftFace: attempt to overwrite edge info");
00267 }
00268 }
00269
00270 public void draw(RealWindowGraphics rWG, Color pC, Color eC) {
00271 drawPoints(rWG, pC);
00272 drawEdges(rWG, eC);
00273 }
00274
00275 public void drawPoints(RealWindowGraphics rWG, Color c) {
00276 for (int i = 0; i < nPoints; i++)
00277 rWG.drawPoint(point[i], c);
00278 }
00279
00280 public void drawEdges(RealWindowGraphics rWG, Color c) {
00281 for (int i = 0; i < nEdges; i++)
00282 drawEdge(rWG, edge[i], c);
00283 }
00284
00285 public void drawEdge(RealWindowGraphics rWG, Edge e, Color c) {
00286 rWG.drawLine(point[e.beginPunt], point[e.eindPunt], c);
00287 }
00288
00289 public void print(PrintStream p) {
00290 print(p , "");
00291 }
00292
00293 public void print(PrintStream p , String pre){
00294 printPoints(p,pre);
00295 printEdges(p,pre);
00296 printFaces(p,pre);
00297 }
00298
00299 public void printPoints(PrintStream p,String pre) {
00300 p.println(pre+"points=[");
00301 for (int i = 0; i < nPoints; i++)
00302 p.println("["+ String.valueOf(point[i].x) + " " + String.valueOf(point[i].y)+"]");
00303
00304 p.println("];");
00305 }
00306
00307 public void printEdges(PrintStream p,String pre) {
00308 p.println(pre+"edges=[");
00309 for (int i = 0; i < nEdges; i++)
00310 p.println( "["+String.valueOf(edge[i].beginPunt) + " " + String.valueOf(edge[i].eindPunt)+"]");
00311
00312 p.println("];");
00313 }
00314
00315 public void printFaces(PrintStream p,String pre){
00316 p.println(pre+"faces=[");
00317 for(int i =0;i < nFaces;i++){
00318 p.println("["+face[i].getEdge_index(0) + " " + face[i].getEdge_index(1) + " "
00319 +face[i].getEdge_index(2)+"]");
00320 }
00321 p.println("];");
00322 }
00323
00324 public RealPoint[] getPoints() {
00325 return point;
00326 }
00327
00328 public RealPoint getPoint(int i) {
00329 return point[i];
00330 }
00331
00332
00333
00334 public Edge[] getEdges() {
00335 return edge;
00336 }
00337
00338 public Edge getEdge(int i){
00339 return edge[i];
00340 }
00341
00342 public Face[] getFaces() {
00343 return face;
00344 }
00345
00346 public Face getFace(int i) {
00347 return face[i];
00348 }
00349
00350 public int getNFaces() {
00351 return nFaces;
00352 }
00353
00354 public int getNPoints(){
00355 return this.point.length;
00356 }
00357 public int getNEdges() {
00358 return nEdges;
00359 }
00360
00361 public void setEdge(Edge[] edge) {
00362 this.nEdges=edge.length;
00363 this.edge = edge;
00364 }
00365
00366 public void setFace(Face[] face) {
00367 this.nFaces=face.length;
00368 this.face = face;
00369 }
00370 }