00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #define PFLN {printf("DEBUG PRINT FILE %s LINE %d\n",__FILE__,__LINE__);}
00048 #include<math.h>
00049 #include "preh.h"
00050 Delaunay dttmp;
00051 bool getPlaneEqn(int x0,int y0,int z0,int x1,int y1,int z1,int x2,int y2,int z2,double *a,double *b)
00052 {
00053
00054
00055
00056
00057
00058
00059 if(x0==x1)
00060 {
00061 if(x0==x2)
00062 {
00063 *a=99999;
00064 *b=99999;
00065 return false;
00066 }
00067 }
00068 if(y0==y1)
00069 {
00070 if(y0==y2)
00071 {
00072 *a=99999;
00073 *b=99999;
00074 return false;
00075 }
00076 }
00077 double d0[3],d1[3],n[3],s,c,d,a1,b1;
00078 d0[0] = x1 - x0;
00079 d0[1] = y1 - y0;
00080 d0[2] = z1 - z0;
00081 d1[0] = x2 - x0;
00082 d1[1] = y2 - y0;
00083 d1[2] = z2 - z0;
00084 n[0] = d1[1] * d0[2] - d1[2] * d0[1];
00085 n[1] = d1[2] * d0[0] - d1[0] * d0[2];
00086 n[2] = d1[0] * d0[1] - d1[1] * d0[0];
00087 if((n[0] * n[0] + n[1] * n[1] + n[2] * n[2] )==0)
00088 {
00089 *a=99999;
00090 *b=99999;
00091 return false;
00092 }
00093 s = 1.0f / sqrt( n[0] * n[0] + n[1] * n[1] + n[2] * n[2] );
00094 a1 = n[0] * s;
00095 b1 = n[1] * s;
00096 c = n[2] * s;
00097
00098 *a=-a1/c;
00099 *b=-b1/c;
00100 return true;
00101 }
00102
00103 double getCost(double a1,double b1,double a2,double b2)
00104 {
00105 double cost=sqrt(a1*a1+b1*b1)*sqrt(a2*a2+b2*b2)-(a1*a2+b1*b2);
00106 return cost;
00107 }
00108
00109
00110
00111 bool checkConvex(Point v1,Point v2,Point v3,Point v4)
00112 {
00113 GPoint pts[]={GPoint(v1.x(),v1.y()),GPoint(v2.x(),v2.y()),GPoint(v3.x(),v3.y()),GPoint(v4.x(),v4.y())};
00114 Polygon_2 pgn(pts, pts+4);
00115 return pgn.is_convex();
00116 }
00117
00118 bool processForCost(Face_handle f1,int i,Point_value_map values,Delaunay *dt)
00119 {
00120 double z1,z2,z3,z4,za,zb,zc,zd,a1,a2,b1,b2,aa,ba,ab,bb,ac,bc,ad,bd;
00121
00122 double c1,c2,c3,c4,c5;
00123 double s1,s2,s3,s4,s5,s6;
00124
00125
00126 Vertex_handle vih=f1->vertex(i);
00127 Point pi=vih->point();
00128
00129 Point pccwi=f1->vertex(f1->ccw(i))->point();
00130
00131 Point pcwi=f1->vertex(f1->cw(i))->point();
00132
00133 z1=values[pi];
00134 z2=values[pccwi];
00135 z3=values[pcwi];
00136
00137
00138
00139 if(!getPlaneEqn(pi.x(),pi.y(),z1,pccwi.x(),pccwi.y(),z2,pcwi.x(),pcwi.y(),z3,&a1,&b1))
00140 return false;
00141
00142
00143
00144 Face_handle f2=f1->neighbor(i);
00145 if (!f1->has_neighbor(f2))
00146 return false;
00147
00148
00149
00150
00151 int vihn=f2->index(f1->vertex(f1->ccw(i)));
00152 Vertex_handle vin=f2->vertex(f2->ccw(vihn));
00153 Point pin=f2->vertex(f2->ccw(vihn))->point();
00154 z4=values[pin];
00155
00156
00157 if(!getPlaneEqn(pin.x(),pin.y(),z4,pcwi.x(),pcwi.y(),z3,pccwi.x(),pccwi.y(),z2,&a2,&b2))
00158 return false;
00159
00160
00161
00162
00163 c1=getCost(a1,b1,a2,b2);
00164
00165
00166
00167
00169 Point a,b,c,d;
00170 Point pd1,pd2;
00171 c2=0;
00172 Face_handle fd=f1->neighbor(f1->ccw(i));
00173 Face_handle fd1,fd2;
00174
00175 int id,id1,id2;
00176
00177 if(f1->has_neighbor(fd))
00178 {
00179
00180 id=fd->index(vih);
00181
00182 d=fd->vertex(fd->cw(id))->point();
00183
00184 if(!checkConvex(pi,pccwi,pcwi,d)) return false;
00185 zd=values[d];
00186
00187
00188 if(!getPlaneEqn(pi.x(),pi.y(),z1,pcwi.x(),pcwi.y(),z3,d.x(),d.y(),zd,&ad,&bd))
00189 return false;
00190 c2=getCost(a1,b1,ad,bd);
00191
00192
00193 fd1=fd->neighbor(fd->ccw(id));
00194
00195 fd2=fd->neighbor(id);
00196
00197 id1= fd1->index(vih);
00198
00199 pd1=fd1->vertex(fd1->cw(id1))->point();
00200
00201
00202 id2= fd2->index(fd->vertex(fd->cw(id)));
00203
00204 pd2=fd2->vertex(fd2->cw(id2))->point();
00205
00206
00207 }
00208 else return false;
00209
00210
00211 c3=0;
00212
00213 Face_handle fa=f1->neighbor(f1->cw(i));
00214
00215 Face_handle fa1,fa2;
00216
00217 Point pa1,pa2;
00218 int ia,ia1,ia2;
00219 if(f1->has_neighbor(fa))
00220 {
00221
00222 ia=fa->index(vih);
00223
00224 a=fa->vertex(fa->ccw(ia))->point();
00225
00226 if(!checkConvex(pi,a,pccwi,pcwi)) return false;
00227
00228 za=values[a];
00229
00230 if(!getPlaneEqn(pi.x(),pi.y(),z1,a.x(),a.y(),za,pccwi.x(),pccwi.y(),z2,&aa,&ba))
00231 return false;
00232 c3=getCost(a1,b1,aa,ba);
00233
00234
00235 fa2=fa->neighbor(ia);
00236 fa1=fa->neighbor(fa->cw(ia));
00237
00238 ia1=fa1->index(fa->vertex(fa->ccw(ia)));
00239 pa1=fa1->vertex(fa1->cw(ia1))->point();
00240
00241 ia2=fa2->index(fa->vertex(fa->ccw(ia)));
00242 pa2=fa2->vertex(fa2->ccw(ia2))->point();
00243
00244 }
00245 else return false;
00246
00247
00248 c4=0;
00249
00250 Face_handle fc=f2->neighbor(vihn);
00251
00252 Face_handle fc1,fc2;
00253
00254 Point pc1,pc2;
00255 int ic,ic1,ic2;
00256 if(f2->has_neighbor(fc))
00257 {
00258 ic=fc->index(vin);
00259 c=fc->vertex(fc->ccw(ic))->point();
00260 if(!checkConvex(pccwi,pcwi,c,pin)) return false;
00261 zc=values[c];
00262
00263 if(!getPlaneEqn(pin.x(),pin.y(),z4,c.x(),c.y(),zc,pcwi.x(),pcwi.y(),z3,&ac,&bc))
00264 return false;
00265 c4=getCost(a2,b2,ac,bc);
00266
00267 fc1=fc->neighbor(ic);
00268 fc2=fc->neighbor(fc->cw(ic));
00269
00270 ic1=fc1->index(fc->vertex(fc->ccw(ic)));
00271 pc1=fc1->vertex(fc1->ccw(ic1))->point();
00272
00273 ic2=fc2->index(fc->vertex(fc->ccw(ic)));
00274 pc2=fc2->vertex(fc2->cw(ic2))->point();
00275 }
00276 else return false;
00277
00278
00279 c5=0;
00280 Face_handle fb=f2->neighbor(f2->cw(vihn));
00281 Face_handle fb1,fb2;
00282 Point pb1,pb2;
00283 int ib,ib1,ib2;
00284 if(f2->has_neighbor(fb))
00285 {
00286 ib=fb->index(vin);
00287 b=fb->vertex(fb->cw(ib))->point();
00288 if(!checkConvex(pccwi,pcwi,pin,b)) return false;
00289 zb=values[b];
00290
00291 if(!getPlaneEqn(pin.x(),pin.y(),z4,pccwi.x(),pccwi.y(),z2,b.x(),b.y(),zb,&ab,&bb))
00292 return false;
00293 c5=getCost(a2,b2,ab,bb);
00294
00295 fb1=fb->neighbor(ib);
00296 fb2=fb->neighbor(fb->ccw(ib));
00297
00298 ib1=fb1->index(fb->vertex(fb->cw(ib)));
00299 pb1=fb1->vertex(fb1->cw(ib1))->point();
00300 ib2=fb2->index(fb->vertex(fb->cw(ib)));
00301 pb2=fb2->vertex(fb2->ccw(ib2))->point();
00302
00303 }
00304 else return false;
00305
00306 s1=c1+c2+c3+c4+c5;
00307
00308
00309
00310
00311
00312 if(!f1->has_neighbor(fd)) return false;
00313 if(!f1->has_neighbor(fa)) return false;
00314 if(!f2->has_neighbor(fb)) return false;
00315 if(!f2->has_neighbor(fc)) return false;
00316
00317
00318 if(!checkConvex(pi,pccwi,pin,pcwi)) return false;
00319
00320
00321
00322 if(!getPlaneEqn(pi.x(),pi.y(),z1,pin.x(),pin.y(),z4,pcwi.x(),pcwi.y(),z3,&a1,&b1))
00323 return false;
00324 if(!getPlaneEqn(pi.x(),pi.y(),z1,pccwi.x(),pccwi.y(),z2,pin.x(),pin.y(),z4,&a2,&b2))
00325 return false;
00326
00327 c1=getCost(a1,b1,a2,b2);
00328
00329
00330 c2=0;
00331 c2=getCost(a1,b1,ad,bd);
00332
00333 c3=0;
00334 c3=getCost(a2,b2,aa,ba);
00335
00336 c4=0;
00337 c4=getCost(a1,b1,ac,bc);
00338
00339 c5=0;
00340 c5=getCost(a2,b2,ab,bb);
00341
00342
00343 s2=c1+c2+c3+c4+c5;
00344
00345
00346
00347 if(s2<s1)
00348 {
00349 dttmp=*dt;
00350 dttmp.flip(f1,i);
00351
00352 if(dttmp.is_valid())
00353 {
00354
00355
00356
00357
00358 return true;
00359 }
00360
00361
00362 }
00363
00364
00365
00366
00367
00368
00369
00371
00372
00373
00374 if(!fd->has_neighbor(fd1)) return false;
00375 if(!checkConvex(pi,pcwi,d,pd1)) return false;
00376 if(!fd->has_neighbor(fd2)) return false;
00377 if(!checkConvex(pi,d,pd2,pcwi)) return false;
00378
00379 if(!fa->has_neighbor(fa1)) return false;
00380 if(!checkConvex(pi,pa1,a,pccwi)) return false;
00381 if(!fa->has_neighbor(fa2)) return false;
00382 if(!checkConvex(pi,a,pa2,pccwi)) return false;
00383
00384 if(!fb->has_neighbor(fb1)) return false;
00385 if(!checkConvex(pccwi,pb1,b,pin)) return false;
00386 if(!fb->has_neighbor(fb2)) return false;
00387 if(!checkConvex(pccwi,b,pb2,pin)) return false;
00388
00389 if(!fc->has_neighbor(fc1)) return false;
00390 if(!checkConvex(pin,c,pc1,pcwi)) return false;
00391 if(!fc->has_neighbor(fc2)) return false;
00392 if(!checkConvex(pin,pc2,c,pcwi)) return false;
00393
00394
00395
00396
00397 double c6,c7,c8,c9,c10,c11,c12,c13;
00398 double aa1,ba1,aa2,ba2,ab1,bb1,ab2,bb2,ac1,bc1,ac2,bc2,ad1,bd1,ad2,bd2;
00399 double za1=values[pa1];
00400 double za2=values[pa2];
00401
00402 double zb1=values[pb1];
00403 double zb2=values[pb2];
00404
00405 double zc1=values[pc1];
00406 double zc2=values[pc2];
00407
00408 double zd1=values[pd1];
00409 double zd2=values[pd2];
00410
00411 if(!getPlaneEqn(pa1.x(),pa1.y(),za1,a.x(),a.y(),za,pi.x(),pi.y(),z1,&aa1,&ba1)) return false;
00412 if(!getPlaneEqn(pi.x(),pi.y(),z4,a.x(),a.y(),za,pccwi.x(),pccwi.y(),z2,&aa2,&ba2)) return false;
00413
00414 if(!getPlaneEqn(pccwi.x(),pccwi.y(),z2,pb1.x(),pb1.y(),zb1,b.x(),b.y(),zb,&ab1,&bb1)) return false;
00415 if(!getPlaneEqn(b.x(),b.y(),zb,pb2.x(),pb2.y(),zb2,pin.x(),pin.y(),z4,&ab2,&bb2)) return false;
00416
00417
00418 if(!getPlaneEqn(pin.x(),pin.y(),z4,pc2.x(),pc2.y(),zc2,c.x(),c.y(),zc,&ac1,&bc1)) return false;
00419 if(!getPlaneEqn(c.x(),c.y(),zc,pc1.x(),pc1.y(),zc1,pcwi.x(),pcwi.y(),z3,&ac2,&bc2)) return false;
00420
00421 if(!getPlaneEqn(pd1.x(),pd1.y(),zd1,pi.x(),pi.y(),z1,d.x(),d.y(),zd,&ad1,&bd1)) return false;
00422 if(!getPlaneEqn(d.x(),d.y(),zd,pcwi.x(),pcwi.y(),z3,pd2.x(),pd2.y(),zd2,&ad2,&bd2)) return false;
00423
00424 int tryset;
00425
00426
00427
00428
00429 tryset=1;
00430 c2=0;
00431 c1=0;
00432 c13=0;
00433 c12=0;
00434 c4=0;
00435 double at1,bt1,at2,bt2;
00436 if(!checkConvex(pi,pin,pcwi,d)) tryset=0;
00437
00438 {
00439 if(!getPlaneEqn(pi.x(),pi.y(),z1,pin.x(),pin.y(),z4,d.x(),d.y(),zd,&at1,&bt1))
00440 {
00441 tryset=0;
00442
00443 }
00444 c2=getCost(a2,b2,at1,bt1);
00445 c13=getCost(ad1,bd1,at1,bt1);
00446
00447 if(!getPlaneEqn(pin.x(),pin.y(),z4,pcwi.x(),pcwi.y(),z3,d.x(),d.y(),zd,&at2,&bt2))
00448 {
00449 tryset=0;
00450
00451 }
00452 c1=getCost(at2,bt2,at1,bt1);
00453
00454 c12=getCost(at2,bt2,ad2,bd2);
00455
00456
00457 c4=getCost(at2,bt2,ac,bc);
00458
00459 }
00460
00461 c6=0;
00462
00463 c6=getCost(aa,ba,aa1,ba1);
00464
00465 c7=0;
00466
00467 c7=getCost(aa,ba,aa2,ba2);
00468
00469 c8=0;
00470
00471 c8=getCost(ab,bb,ab1,bb1);
00472 c9=0;
00473
00474 c9=getCost(ab,bb,ab2,bb2);
00475
00476
00477 c10=0;
00478
00479 c10=getCost(ac,bc,ac2,bc2);
00480 c11=0;
00481
00482 c11=getCost(ac,bc,ac1,bc1);
00483 if(tryset)
00484 {
00485 s3=c1+c2+c3+c4+c5+c6+c7+c8+c9+c10+c11+c12+c13;
00486 if(s3<s2)
00487 {
00488
00489
00490
00491
00492 {dt->flip(f1,i);
00493 dt->flip(fd,fd->index(fd->vertex(fd->cw(id)) ) );
00494 return true;
00495 }
00496 }
00497 }
00498
00499
00500
00501 tryset=1;
00502 c1=0;
00503 c2=0;
00504 c4=0;
00505 c10=0;
00506 c11=0;
00507 c12=0;
00508 c13=0;
00509 if(!checkConvex(pi,pin,c,pcwi)) tryset=0;
00510
00511 {
00512 if(!getPlaneEqn(pi.x(),pi.y(),z1,pin.x(),pin.y(),z4,c.x(),c.y(),zc,&at1,&bt1))
00513 {
00514 tryset=0;
00515
00516 }
00517 if(!getPlaneEqn(pi.x(),pi.y(),z1,c.x(),c.y(),zc,pcwi.x(),pcwi.y(),z3,&at2,&bt2))
00518 {
00519 tryset=0;
00520
00521 }
00522 c1=getCost(at1,bt1,at2,bt2);
00523 c2=getCost(at1,bt1,a2,b2);
00524
00525 c4=getCost(at1,bt1,ac2,bc2);
00526
00527 c10=getCost(at2,bt2,ac1,bc1);
00528
00529 {
00530 c11=getCost(at2,bt2,ad,bd);
00531
00532 c13=getCost(ad,bd,ad1,bd1);
00533
00534 c12=getCost(ad,bd,ad2,bd2);
00535 }
00536
00537 }
00538 if(tryset)
00539 {
00540 s4=c1+c2+c3+c4+c5+c6+c7+c8+c9+c10+c11+c12+c13;
00541 if(s4<s2)
00542 {
00543
00544
00545
00546
00547 {dt->flip(f1,i);
00548 dt->flip(fc,fc->index(fc->vertex(fc->ccw(ic) )) );
00549 return true;
00550 }
00551 }
00552 }
00553
00555 tryset=1;
00556 c5=0;c6=0;c10=0;c11=0;c12=0;c13=0;
00557 if(!checkConvex(pi,a,pccwi,pin)) tryset=0;
00558
00559 {
00560 if(!getPlaneEqn(a.x(),a.y(),za,pin.x(),pin.y(),z4,pi.x(),pi.y(),z1,&at1,&bt1))
00561 {
00562 tryset=0;
00563
00564 }
00565 if(!getPlaneEqn(a.x(),a.y(),za,pccwi.x(),pccwi.y(),z2,pin.x(),pin.y(),z4,&at2,&bt2))
00566 {
00567 tryset=0;
00568
00569 }
00570 c5=getCost(aa1,ba1,at1,bt1);
00571 c6=getCost(aa2,ba2,at2,bt2);
00572 c9=getCost(a1,b1,at1,bt1);
00573 c10=getCost(at1,bt1,at2,bt2);
00574 c11=getCost(ab,bb,at2,bt2);
00575 c12=getCost(ab1,bb1,ab,bb);
00576 c13=getCost(ab2,bb2,ab,bb);
00577
00578
00579 }
00580 c1=0;
00581 c7=0;
00582 c2=0;
00583
00584 {
00585 c7=getCost(ad,bd,a1,b1);
00586
00587 c1=getCost(ad,bd,ad1,bd1);
00588
00589 c2=getCost(ad,bd,ad2,bd2);
00590
00591 }
00592 c4=0;c3=0;c8=0;
00593
00594 {
00595 c8=getCost(a1,b1,ac,bc);
00596
00597 c3=getCost(ac,bc,ac1,bc1);
00598
00599 c4=getCost(ac,bc,ac2,bc2);
00600 }
00601 if(tryset)
00602 {
00603 s5=c1+c2+c3+c4+c5+c6+c7+c8+c9+c10+c11+c12+c13;
00604 if(s5<s2)
00605 {
00606
00607
00608
00609
00610
00611 {
00612 dt->flip(f1,i);
00613 dt->flip(fa,fa->index(fa->vertex(fa->ccw(ia) )) );
00614
00615 return true;
00616 }
00617 }
00618
00619 }
00620
00621
00622
00623
00624
00625
00626
00627
00628 if(!checkConvex(pi,pccwi,b,pin)) return false;;
00629 if(!getPlaneEqn(pi.x(),pi.y(),z1,b.x(),b.y(),zb,pin.x(),pin.y(),z4,&at1,&bt1))
00630 return false;
00631
00632 if(!getPlaneEqn(pi.x(),pi.y(),z1,pccwi.x(),pccwi.y(),z2,b.x(),b.y(),zb,&at2,&bt2))
00633 return false;
00634
00635 c11=getCost(a1,b1,at1,bt1);
00636 c12=getCost(at1,bt1,at2,bt2);
00637 c5=getCost(at1,bt1,ab2,bb2);
00638 c6=getCost(at2,bt2,ab1,bb1);
00639 c9=getCost(at2,bt2,aa,ba);
00640 c10=getCost(aa1,ba1,aa,ba);
00641 c13=getCost(aa,ba,aa2,ba2);
00642 s6=c1+c2+c3+c4+c5+c6+c7+c8+c9+c10+c11+c12+c13;
00643 if(s6<s2)
00644 {
00645
00646
00647
00648
00649
00650 {dt->flip(f1,i);
00651 dt->flip(fb,fb->index(fb->vertex(fb->cw(ib) )) );
00652 return true;
00653 }
00654 }
00655 return false;
00656 }