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