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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00132 #ifndef __VCG_FACE_POS
00133 #define __VCG_FACE_POS
00134
00135 #include <assert.h>
00136
00137 namespace vcg {
00138 namespace face {
00139
00142
00143
00144 template <class FaceType>
00145 bool IsBorder(FaceType const & f, const int j );
00146 template <class FaceType>
00147 bool IsManifold(FaceType const & f, const int j );
00148
00157 template <class FaceType>
00158 class Pos
00159 {
00160 public:
00161
00163 typedef typename FaceType::VertexType VertexType;
00165 typedef Pos<FaceType> PosType;
00167 typedef typename VertexType::ScalarType ScalarType;
00168
00170 typename FaceType::FaceType *f;
00172 int z;
00174 VertexType *v;
00175
00177 Pos(){}
00179 Pos(FaceType * const fp, int const zp, VertexType * const vp){f=fp; z=zp; v=vp;}
00180 Pos(FaceType * const fp, int const zp){f=fp; z=zp; v=f->V(zp);}
00181 Pos(FaceType * const fp, VertexType * const vp)
00182 {
00183 f = fp;
00184 v = vp;
00185 for(int i = 0; i < f->VN(); ++i)
00186 if (f->V(i) == v) { z = f->Prev(i); break;}
00187 }
00188
00189
00190 VertexType *& V(){ return v; }
00191 int & E(){ return z; }
00192 FaceType *& F(){ return f; }
00193
00194
00195
00196
00197
00198 int VInd()
00199 {
00200 for(int i = 0; i < f->VN(); ++i) if(v==f->V(i)) return i;
00201 assert(0);
00202 return -1;
00203 }
00204
00205
00207 inline bool operator == ( PosType const & p ) const {
00208 return (f==p.f && z==p.z && v==p.v);
00209 }
00210
00212 inline bool operator != ( PosType const & p ) const {
00213 return (f!=p.f || z!=p.z || v!=p.v);
00214 }
00216 inline bool operator <= ( PosType const & p) const {
00217 return (f!=p.f)?(f<f.p):
00218 (z!=p.z)?(z<p.z):
00219 (v<=p.v);
00220 }
00221
00223 inline FaceType & operator = ( const FaceType & h ){
00224 f=h.f;
00225 z=h.z;
00226 v=h.v;
00227 return *this;
00228 }
00230 void SetNull(){
00231 f=0;
00232 v=0;
00233 z=-1;
00234 }
00236 bool IsNull() const {
00237 return f==0 || v==0 || z<0;
00238 }
00239
00240
00241
00243 void NextF()
00244 {
00245 FaceType * t = f;
00246 f = t->FFp(z);
00247 z = t->FFi(z);
00248 }
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00260 void NextE()
00261 {
00262 assert( f->V(z)==v || f->V(f->Next(z))==v );
00263 FlipE();
00264 FlipF();
00265 assert( f->V(z)==v || f->V(f->Next(z))==v );
00266 }
00267
00269 void FlipE()
00270 {
00271 assert(f->V(f->Prev(z))!=v && (f->V(f->Next(z))==v || f->V((z+0)%f->VN())==v));
00272 if(f->V(f->Next(z))==v) z=f->Next(z);
00273 else z= f->Prev(z);
00274 assert(f->V(f->Prev(z))!=v && (f->V(f->Next(z))==v || f->V((z))==v));
00275 }
00276
00277
00278
00279
00280
00281
00282
00284 void FlipF()
00285 {
00286 assert( f->FFp(z)->FFp(f->FFi(z))==f );
00287 assert(f->V(f->Prev(z))!=v && (f->V(f->Next(z))==v || f->V((z))==v));
00288 FaceType *nf=f->FFp(z);
00289 int nz=f->FFi(z);
00290 assert(nf->V(f->Prev(nz))!=v && (nf->V(f->Next(nz))==v || nf->V((nz))==v));
00291 f=nf;
00292 z=nz;
00293 assert(f->V(f->Prev(z))!=v && (f->V(f->Next(z))==v || f->V(z)==v));
00294 }
00295
00297 void FlipV()
00298 {
00299 assert(f->V(f->Prev(z))!=v && (f->V(f->Next(z))==v || f->V(z)==v));
00300
00301 if(f->V(f->Next(z))==v)
00302 v=f->V(z);
00303 else
00304 v=f->V(f->Next(z));
00305
00306 assert(f->V(f->Prev(z))!=v && (f->V(f->Next(z))==v || f->V(z)==v));
00307 }
00308
00309
00310 VertexType *VFlip()
00311 {
00312 assert(f->V(f->Prev(z))!=v && (f->V(f->Next(z))==v || f->V(z)==v));
00313 if(f->V(f->Next(z))==v) return f->V(z);
00314 else return f->V(f->Next(z));
00315 }
00316
00317
00318 const VertexType *VFlip() const
00319 {
00320 assert(f->cV(f->Prev(z))!=v && (f->cV(f->Next(z))==v || f->cV(z)==v));
00321 if(f->cV(f->Next(z))==v) return f->cV(z);
00322 else return f->cV(f->Next(z));
00323 }
00324
00325
00326 const FaceType *FFlip() const
00327 {
00328 assert( f->FFp(z)->FFp(f->FFi(z))==f );
00329 assert(f->V(f->Prev(z))!=v && (f->V(f->Next(z))==v || f->V((z+0)%f->VN())==v));
00330 FaceType *nf=f->FFp(z);
00331 return nf;
00332 }
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00348 void NextB( )
00349 {
00350 assert(f->V(f->Prev(z))!=v && (f->V(f->Next(z))==v || f->V(z)==v));
00351 assert(f->FFp(z)==f);
00352
00353
00354 do
00355 NextE();
00356 while(!IsBorder());
00357
00358
00359 assert(IsBorder() &&( f->V(z)==v || f->V(f->Next(z))==v ));
00360
00361 FlipV();
00362 assert(f->V(f->Prev(z))!=v && (f->V(f->Next(z))==v || f->V(z)==v));
00363 assert(f->FFp(z)==f);
00364 }
00365
00367 bool IsBorder()
00368 {
00369 return face::IsBorder(*f,z);
00370 }
00371
00372 bool IsManifold()
00373 {
00374 return face::IsManifold(*f,z);
00375 }
00376
00380 int NumberOfIncidentVertices()
00381 {
00382 int count = 0;
00383 bool on_border = false;
00384 CheckIncidentFaces(count, on_border);
00385 if(on_border) return (count/2)+1;
00386 else return count;
00387 }
00388
00392 int NumberOfIncidentFaces()
00393 {
00394 int count = 0;
00395 bool on_border = false;
00396 CheckIncidentFaces(count, on_border);
00397 if(on_border) return count/2;
00398 else return count;
00399 }
00400
00406 void Set(FaceType * const fp, int const zp, VertexType * const vp)
00407 {
00408 f=fp;z=zp;v=vp;
00409 assert(f->V(f->Prev(z))!=v && (f->V(f->Next(z))==v || f->V(z)==v));
00410 }
00411
00412 void Set(FaceType * const pFace, VertexType * const pVertex)
00413 {
00414 f = pFace;
00415 v = pVertex;
00416 for(int i = 0; i < f->VN(); ++i) if(f->V(i) == v ) {z = f->Prev(i);break;}
00417 }
00418
00419 void Assert()
00420 #ifdef _DEBUG
00421 {
00422 FaceType ht=*this;
00423 ht.FlipF();
00424 ht.FlipF();
00425 assert(ht==*this);
00426
00427 ht.FlipE();
00428 ht.FlipE();
00429 assert(ht==*this);
00430
00431 ht.FlipV();
00432 ht.FlipV();
00433 assert(ht==*this);
00434 }
00435 #else
00436 {}
00437 #endif
00438
00439
00440 protected:
00441 void CheckIncidentFaces(int & count, bool & on_border)
00442 {
00443 PosType ht = *this;
00444 do
00445 {
00446 ++count;
00447 ht.NextE();
00448 if(ht.IsBorder()) on_border=true;
00449 } while (ht != *this);
00450 }
00451 };
00452
00453 template <class FaceType>
00458 class PosN : public Pos<FaceType>
00459 {
00460 public:
00461 typedef typename FaceType::CoordType CoordType;
00462
00463 CoordType normal;
00464 };
00465
00466
00493 template <typename FaceType>
00494 class VFIterator
00495 {
00496 public:
00497
00499 typedef typename FaceType::VertexType VertexType;
00501 typedef FaceType VFIFaceType;
00503 typedef typename VertexType::CoordType CoordType;
00505 typedef typename VertexType::ScalarType ScalarType;
00506
00508 FaceType *f;
00510 int z;
00511
00513 VFIterator(){}
00515 VFIterator(FaceType * _f, const int & _z){f = _f; z = _z;}
00516
00518 VFIterator(VertexType * _v){f = _v->VFp(); z = _v->VFi();}
00519
00520 VFIFaceType *& F() { return f;}
00521 int & I() { return z;}
00522
00523
00524
00525 inline VertexType *V() const { return f->V(z);}
00526
00527 inline VertexType * const & V0() const { return f->V0(z);}
00528 inline VertexType * const & V1() const { return f->V1(z);}
00529 inline VertexType * const & V2() const { return f->V2(z);}
00530
00531 bool End() const {return f==0;}
00532 VFIFaceType *operator++() {
00533 FaceType* t = f;
00534 f = f->VFp(z);
00535 z = t->VFi(z);
00536 return f;
00537 }
00538
00539 };
00540
00542 }
00543 }
00544 #endif