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 #ifndef __VCG_TETRA_TRI_COLLAPSE
00042 #define __VCG_TETRA_TRI_COLLAPSE
00043
00044
00045 #include<vcg/simplex/face/pos.h>
00046 #include<vcg/simplex/face/topology.h>
00047 #include<vcg/complex/trimesh/allocate.h>
00048
00049 namespace vcg{
00050 namespace tri{
00051
00059 template <class TRI_MESH_TYPE>
00060 class EdgeCollapse
00061 {
00062 public:
00064 typedef TRI_MESH_TYPE TriMeshType;
00066 typedef typename TriMeshType::FaceType FaceType;
00068 typedef typename FaceType::VertexType VertexType;
00069 typedef typename FaceType::VertexPointer VertexPointer;
00071 typedef typename TriMeshType::VertexIterator VertexIterator;
00073 typedef typename TriMeshType::FaceIterator FaceIterator;
00075 typedef typename FaceType::VertexType::CoordType CoordType;
00077 typedef typename TriMeshType::VertexType::ScalarType ScalarType;
00079 typedef typename TriMeshType::FaceContainer FaceContainer;
00081 typedef typename TriMeshType::VertContainer VertContainer;
00083 typedef typename TriMeshType::FaceType::EdgeType EdgeType;
00085 typedef typename std::vector<EdgeType> EdgeVec;
00087 typedef typename vcg::face::VFIterator<FaceType> VFI;
00089 typedef typename std::vector<vcg::face::VFIterator<FaceType> > VFIVec;
00090
00091
00092
00094 EdgeCollapse()
00095 {
00096 };
00097
00098 ~EdgeCollapse()
00099 {
00100 };
00101
00102 static VFIVec & AV0(){static VFIVec av0; return av0;}
00103 static VFIVec & AV1(){static VFIVec av1; return av1;}
00104 static VFIVec & AV01(){static VFIVec av01; return av01;}
00105
00106
00107 void FindSets(EdgeType &p)
00108 {
00109 VertexType * v0 = p.V(0);
00110 VertexType * v1 = p.V(1);
00111
00112 AV0().clear();
00113 AV1().clear();
00114 AV01().clear();
00115
00116 VFI x;
00117
00118 for( x.f = v0->VFp(), x.z = v0->VFi(); x.f!=0; ++x)
00119 {
00120 int zv1 = -1;
00121
00122 for(int j=0;j<3;++j)
00123 if( x.f->V(j)==&*v1 ) {
00124 zv1 = j;
00125 break;
00126 }
00127 if(zv1==-1) AV0().push_back( x );
00128 else AV01().push_back( x );
00129 }
00130
00131 for( x.f = v1->VFp(), x.z = v1->VFi(); x.f!=0; ++x )
00132 {
00133 int zv0 = -1;
00134
00135 for(int j=0;j<3;++j)
00136 if( x.f->V(j)==&*v0 ) {
00137 zv0 = j;
00138 break;
00139 }
00140 if(zv0==-1) AV1().push_back( x );
00141 }
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 bool LinkConditions(EdgeType pos)
00167 {
00168 typedef typename vcg::face::VFIterator<FaceType> VFIterator;
00169
00170
00171 std::map<VertexPointer,int> VertCnt;
00172 std::map<std::pair<VertexPointer,VertexPointer>,int> EdgeCnt;
00173
00174
00175 std::vector<VertexPointer> BoundaryVertexVec[2];
00176
00177
00178 VFIterator vfi;
00179 for(int i=0;i<2;++i)
00180 {
00181 vfi = VFIterator(pos.V(i));
00182 for( ;!vfi.End();++vfi)
00183 {
00184 ++ VertCnt[vfi.V1()];
00185 ++ VertCnt[vfi.V2()];
00186 if(vfi.V1()<vfi.V2()) ++EdgeCnt[std::make_pair(vfi.V1(),vfi.V2())];
00187 else ++EdgeCnt[std::make_pair(vfi.V2(),vfi.V1())];
00188 }
00189
00190
00191 typename std::map<VertexPointer,int>::iterator vcmit;
00192 for(vcmit=VertCnt.begin();vcmit!=VertCnt.end();++vcmit)
00193 {
00194 if((*vcmit).second==1)
00195 BoundaryVertexVec[i].push_back((*vcmit).first);
00196 }
00197 if(BoundaryVertexVec[i].size()==2)
00198 {
00199
00200 VertCnt[0]+=2;
00201 ++ EdgeCnt[std::make_pair(VertexPointer(0),BoundaryVertexVec[i][0]) ] ;
00202 ++ EdgeCnt[std::make_pair(VertexPointer(0),BoundaryVertexVec[i][1]) ] ;
00203
00204 ++VertCnt[BoundaryVertexVec[i][0]];
00205 ++VertCnt[BoundaryVertexVec[i][1]];
00206 }
00207 }
00208
00209
00210
00211 std::vector<VertexPointer> LkEdge;
00212
00213 for( vfi = VFIterator(pos.V(0)); !vfi.End(); ++vfi)
00214 {
00215 if(vfi.V1() == pos.V(1) ) LkEdge.push_back(pos.V(2));
00216 if(vfi.V2() == pos.V(1) ) LkEdge.push_back(pos.V(1));
00217 }
00218
00219
00220
00221 if(LkEdge.size()==1)
00222 {
00223 LkEdge.push_back(0);
00224 }
00225
00226
00227 size_t SharedEdgeCnt=0;
00228 typename std::map<std::pair<VertexPointer,VertexPointer>, int>::iterator eci;
00229 for(eci=EdgeCnt.begin();eci!=EdgeCnt.end();++eci)
00230 if((*eci).second == 2) SharedEdgeCnt ++;
00231
00232 if(SharedEdgeCnt>0) return false;
00233 size_t SharedVertCnt=0;
00234 typename std::map<VertexPointer,int>::iterator vci;
00235 for(vci=VertCnt.begin();vci!=VertCnt.end();++vci)
00236 if((*vci).second == 4) SharedVertCnt++;
00237
00238 if(SharedVertCnt != LkEdge.size() ) return false;
00239
00240 return true;
00241 }
00242
00243
00244
00245 bool LinkConditionsOld(EdgeType pos){
00246
00247 const int ADJ_1 = TriMeshType::VertexType::NewBitFlag();
00248 const int ADJ_E = TriMeshType::VertexType::NewBitFlag();
00249
00250
00251
00252 const int NOTALLADJ = ~(ADJ_1 | ADJ_E | TriMeshType::VertexType::VISITED);
00253 const int NOTALLADJ1 = ~(ADJ_E | TriMeshType::VertexType::VISITED);
00254
00255
00256 typename vcg::face::VFIterator<FaceType> x;
00257
00258 for(x.f = pos.V(0)->VFp(), x.z = pos.V(0)->VFi(); x.f!=0; ++x ) {
00259 x.f->V1(x.z)->Flags() &= NOTALLADJ;
00260 x.f->V2(x.z)->Flags() &= NOTALLADJ;
00261 }
00262
00263 for(x.f = pos.V(1)->VFp(), x.z = pos.V(1)->VFi(); x.f!=0; ++x ) {
00264 x.f->V1(x.z)->Flags() &= NOTALLADJ1;
00265 x.f->V2(x.z)->Flags() &= NOTALLADJ1;
00266 }
00267
00268 for(x.f = pos.V(1)->VFp(), x.z = pos.V(1)->VFi(); x.f!=0; ++x ) {
00269 if(x.f->V1(x.z)==pos.V(0)) x.f->V2(x.z)->Flags() |= ADJ_E | ADJ_1;
00270 else x.f->V2(x.z)->Flags() |= ADJ_1;
00271 if(x.f->V2(x.z)==pos.V(0)) x.f->V1(x.z)->Flags() |= ADJ_E | ADJ_1;
00272 else x.f->V1(x.z)->Flags() |= ADJ_1;
00273 }
00274
00275
00276 int adj01=0;
00277 int adje=0;
00278 for(x.f = pos.V(0)->VFp(), x.z = pos.V(0)->VFi(); x.f!=0; ++x ) {
00279 if(!x.f->V1(x.z)->IsV()) {
00280 x.f->V1(x.z)->SetV();
00281 if(x.f->V1(x.z)->Flags()&ADJ_1) ++adj01;
00282 if(x.f->V1(x.z)->Flags()&ADJ_E) ++adje;
00283 }
00284 if(!x.f->V2(x.z)->IsV()) {
00285 x.f->V2(x.z)->SetV();
00286 if(x.f->V2(x.z)->Flags()&ADJ_1) ++adj01;
00287 if(x.f->V2(x.z)->Flags()&ADJ_E) ++adje;
00288 }
00289 }
00290
00291
00292
00293 TriMeshType::VertexType::DeleteBitFlag(ADJ_E);
00294 TriMeshType::VertexType::DeleteBitFlag(ADJ_1);
00295
00296 return (adj01==adje);
00297 }
00298
00299
00300
00301 int DoCollapse(TriMeshType &m, EdgeType & c, const Point3<ScalarType> &p)
00302 {
00303 FindSets(c);
00304 typename VFIVec::iterator i;
00305 int n_face_del =0 ;
00306
00307
00308 if (TriMeshType::HasFFTopology())
00309 {
00310
00311
00312
00313
00314
00315
00317
00318
00319
00320
00321
00323
00324
00325
00326
00327
00329
00330
00331
00332
00333
00334
00336
00337
00338
00339
00340 }
00341
00342 for(i=AV01().begin();i!=AV01().end();++i)
00343 {
00344 FaceType & f = *((*i).f);
00345 assert(f.V((*i).z) == c.V(0));
00346 vcg::face::VFDetach(f,((*i).z+1)%3);
00347 vcg::face::VFDetach(f,((*i).z+2)%3);
00348 Allocator<TriMeshType>::DeleteFace(m,f);
00349
00350 }
00351
00352
00353 for(i=AV0().begin();i!=AV0().end();++i)
00354 {
00355 (*i).f->V((*i).z) = c.V(1);
00356 (*i).f->VFp((*i).z) = (*i).f->V((*i).z)->VFp();
00357 (*i).f->VFi((*i).z) = (*i).f->V((*i).z)->VFi();
00358 (*i).f->V((*i).z)->VFp() = (*i).f;
00359 (*i).f->V((*i).z)->VFi() = (*i).z;
00360 }
00361
00362 Allocator<TriMeshType>::DeleteVertex(m,*(c.V(0)));
00363
00364 c.V(1)->P()=p;
00365 return n_face_del;
00366 }
00367
00368 };
00369
00370 }
00371 }
00372 #endif