00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __VCGLIB_TRIALLOCATOR
00025 #define __VCGLIB_TRIALLOCATOR
00026
00027 #include <typeinfo>
00028 #include <vector>
00029 #include <map>
00030 #include <string>
00031 #include <set>
00032 #include <assert.h>
00033 #include <vcg/complex/trimesh/base.h>
00034 #include <vcg/container/simple_temporary_data.h>
00035
00036 namespace vcg {
00037 namespace tri {
00040 template<class MeshType>
00041 size_t Index(MeshType &m, typename MeshType::VertexType &v) {return &v-&*m.vert.begin();}
00042 template<class MeshType>
00043 size_t Index(MeshType &m, typename MeshType::FaceType &f) {return &f-&*m.face.begin();}
00044 template<class MeshType>
00045 size_t Index(MeshType &m, typename MeshType::EdgeType &e) {return &e-&*m.edge.begin();}
00046 template<class MeshType>
00047 size_t Index(MeshType &m, typename MeshType::HEdgeType &h) {return &h-&*m.hedge.begin();}
00048
00049 template<class MeshType>
00050 size_t Index(MeshType &m, const typename MeshType::VertexType *vp) {return vp-&*m.vert.begin();}
00051 template<class MeshType>
00052 size_t Index(MeshType &m, const typename MeshType::FaceType * fp) {return fp-&*m.face.begin();}
00053 template<class MeshType>
00054 size_t Index(MeshType &m, typename MeshType::EdgeType* e) {return e-&*m.edge.begin();}
00055 template<class MeshType>
00056 size_t Index(MeshType &m, typename MeshType::HEdgeType* h) {return h-&*m.hedge.begin();}
00057
00058
00059
00060
00061 template <class face_type>
00062 void ReorderFace( std::vector<size_t> & , std::vector<face_type> & )
00063 {}
00064 template <class vertex_type>
00065 void ReorderVert( std::vector<size_t> &, std::vector<vertex_type> &)
00066 {}
00067
00068 template <class MeshType, class ATTR_CONT>
00069 void ReorderAttribute(ATTR_CONT &c,std::vector<size_t> & newVertIndex, MeshType & ){
00070 typename std::set<typename MeshType::PointerToAttribute>::iterator ai;
00071 for(ai = c.begin(); ai != c.end(); ++ai)
00072 ((typename MeshType::PointerToAttribute)(*ai)).Reorder(newVertIndex);
00073 }
00074
00075 template <class MeshType, class ATTR_CONT>
00076 void ResizeAttribute(ATTR_CONT &c,const int & , MeshType &m){
00077 typename std::set<typename MeshType::PointerToAttribute>::iterator ai;
00078 for(ai =c.begin(); ai != c.end(); ++ai)
00079 ((typename MeshType::PointerToAttribute)(*ai)).Resize(m.vn);
00080 }
00081
00083
00084
00085 template <class AllocateMeshType>
00086 class Allocator
00087 {
00088
00089 public:
00090 typedef AllocateMeshType MeshType;
00091 typedef typename MeshType::VertexType VertexType;
00092 typedef typename MeshType::VertexPointer VertexPointer;
00093 typedef typename MeshType::VertexIterator VertexIterator;
00094 typedef typename MeshType::VertContainer VertContainer;
00095
00096 typedef typename MeshType::EdgeType EdgeType;
00097 typedef typename MeshType::EdgePointer EdgePointer;
00098 typedef typename MeshType::EdgeIterator EdgeIterator;
00099 typedef typename MeshType::EdgeContainer EdgeContainer;
00100
00101 typedef typename MeshType::FaceType FaceType;
00102 typedef typename MeshType::FacePointer FacePointer;
00103 typedef typename MeshType::FaceIterator FaceIterator;
00104 typedef typename MeshType::FaceContainer FaceContainer;
00105
00106 typedef typename MeshType::HEdgeType HEdgeType;
00107 typedef typename MeshType::HEdgePointer HEdgePointer;
00108 typedef typename MeshType::HEdgeIterator HEdgeIterator;
00109 typedef typename MeshType::HEdgeContainer HEdgeContainer;
00110
00111
00112 typedef typename MeshType::PointerToAttribute PointerToAttribute;
00113 typedef typename std::set<PointerToAttribute>::iterator AttrIterator;
00114 typedef typename std::set<PointerToAttribute>::const_iterator AttrConstIterator;
00115 typedef typename std::set<PointerToAttribute >::iterator PAIte;
00116
00122 template<class SimplexPointerType>
00123 class PointerUpdater
00124 {
00125 public:
00126 PointerUpdater(void) : newBase(0), oldBase(0), newEnd(0), oldEnd(0), preventUpdateFlag(false) { ; }
00127 void Clear(){newBase=oldBase=newEnd=oldEnd=0;};
00128 void Update(SimplexPointerType &vp)
00129 {
00130 if(vp>=newBase && vp<newEnd) return;
00131 assert(vp>=oldBase);
00132 assert(vp<oldEnd);
00133 vp=newBase+(vp-oldBase);
00134 }
00135 bool NeedUpdate() {if(oldBase && newBase!=oldBase && !preventUpdateFlag) return true; else return false;}
00136
00137 SimplexPointerType newBase;
00138 SimplexPointerType oldBase;
00139 SimplexPointerType newEnd;
00140 SimplexPointerType oldEnd;
00141 bool preventUpdateFlag;
00142 };
00143
00144
00152 static VertexIterator AddVertices(MeshType &m,int n, PointerUpdater<VertexPointer> &pu)
00153 {
00154 VertexIterator last;
00155 if(n == 0) return m.vert.end();
00156 pu.Clear();
00157 if(m.vert.empty()) pu.oldBase=0;
00158 else {
00159 pu.oldBase=&*m.vert.begin();
00160 pu.oldEnd=&m.vert.back()+1;
00161 }
00162
00163 m.vert.resize(m.vert.size()+n);
00164 m.vn+=n;
00165
00166 typename std::set<PointerToAttribute>::iterator ai;
00167 for(ai = m.vert_attr.begin(); ai != m.vert_attr.end(); ++ai)
00168 ((PointerToAttribute)(*ai)).Resize(m.vert.size());
00169
00170 pu.newBase = &*m.vert.begin();
00171 pu.newEnd = &m.vert.back()+1;
00172 if(pu.NeedUpdate())
00173 {
00174 FaceIterator fi;
00175 for (fi=m.face.begin(); fi!=m.face.end(); ++fi)
00176 if(!(*fi).IsD())
00177 for(int i=0; i < (*fi).VN(); ++i)
00178 if ((*fi).cV(i)!=0) pu.Update((*fi).V(i));
00179 EdgeIterator ei;
00180 for (ei=m.edge.begin(); ei!=m.edge.end(); ++ei)
00181 if(!(*ei).IsD())
00182 {
00183 if(HasEVAdjacency (m)) { pu.Update((*ei).EVp(0));pu.Update((*ei).EVp(1));}
00184
00185 }
00186 HEdgeIterator hi;
00187 for (hi=m.hedge.begin(); hi!=m.hedge.end(); ++hi)
00188 if(!(*hi).IsD())
00189 {
00190 if(HasHVAdjacency (m))
00191 {
00192 pu.Update((*hi).HVp());
00193 }
00194 }
00195
00196
00197 }
00198 unsigned int siz=(unsigned int)m.vert.size()-n;
00199
00200 last = m.vert.begin();
00201 advance(last,siz);
00202
00203 return last;
00204 }
00205
00209 static VertexIterator AddVertices(MeshType &m, int n)
00210 {
00211 PointerUpdater<VertexPointer> pu;
00212 return AddVertices(m, n,pu);
00213 }
00214
00218 static VertexIterator AddVertices(MeshType &m, int n, std::vector<VertexPointer *> &local_vec)
00219 {
00220 PointerUpdater<VertexPointer> pu;
00221 VertexIterator v_ret = AddVertices(m, n,pu);
00222
00223 typename std::vector<VertexPointer *>::iterator vi;
00224 for(vi=local_vec.begin();vi!=local_vec.end();++vi)
00225 pu.Update(**vi);
00226 return v_ret;
00227 }
00228
00229
00237 static EdgeIterator AddEdges(MeshType &m,int n, PointerUpdater<EdgePointer> &pu)
00238 {
00239 EdgeIterator last;
00240 if(n == 0) return m.edge.end();
00241 pu.Clear();
00242 if(m.edge.empty()) pu.oldBase=0;
00243 else {
00244 pu.oldBase=&*m.edge.begin();
00245 pu.oldEnd=&m.edge.back()+1;
00246 }
00247
00248 m.edge.resize(m.edge.size()+n);
00249 m.en+=n;
00250
00251 typename std::set<typename MeshType::PointerToAttribute>::iterator ai;
00252 for(ai = m.edge_attr.begin(); ai != m.edge_attr.end(); ++ai)
00253 ((typename MeshType::PointerToAttribute)(*ai)).Resize(m.edge.size());
00254
00255 pu.newBase = &*m.edge.begin();
00256 pu.newEnd = &m.edge.back()+1;
00257 if(pu.NeedUpdate())
00258 {
00259 FaceIterator fi;
00260 for (fi=m.face.begin(); fi!=m.face.end(); ++fi){
00261
00262
00263 if(!(*fi).IsD())
00264 for(int i=0; i < (*fi).VN(); ++i)
00265 if ((*fi).cFEp(i)!=0) pu.Update((*fi).FEp(i));
00266 }
00267
00268 VertexIterator vi;
00269 if(HasVEAdjacency(m))
00270 for (vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
00271 if(!(*vi).IsD())
00272 if ((*vi).cVEp()!=0) pu.Update((*vi).VEp());
00273
00274 HEdgeIterator hi;
00275 if(HasHEAdjacency(m))
00276 for (hi=m.hedge.begin(); hi!=m.hedge.end(); ++hi)
00277 if(!(*hi).IsD())
00278 if ((*hi).cHEp()!=0) pu.Update((*hi).HEp());
00279
00280
00281
00282 }
00283 unsigned int siz=(unsigned int)m.edge.size()-n;
00284
00285 last = m.edge.begin();
00286 advance(last,siz);
00287
00288 return last;
00289 }
00290
00294 static EdgeIterator AddEdges(MeshType &m, int n)
00295 {
00296 PointerUpdater<EdgePointer> pu;
00297 return AddEdges(m, n,pu);
00298 }
00299
00303 static EdgeIterator AddEdges(MeshType &m, int n, std::vector<EdgePointer*> &local_vec)
00304 {
00305 PointerUpdater<EdgePointer> pu;
00306 EdgeIterator v_ret = AddEdges(m, n,pu);
00307
00308 typename std::vector<EdgePointer *>::iterator ei;
00309 for(ei=local_vec.begin();ei!=local_vec.end();++ei)
00310 pu.Update(**ei);
00311 return v_ret;
00312 }
00313
00314
00315
00323 static HEdgeIterator AddHEdges(MeshType &m,int n, PointerUpdater<HEdgePointer> &pu)
00324 {
00325 HEdgeIterator last;
00326 if(n == 0) return m.hedge.end();
00327 pu.Clear();
00328 if(m.hedge.empty()) pu.oldBase=0;
00329 else {
00330 pu.oldBase=&*m.hedge.begin();
00331 pu.oldEnd=&m.hedge.back()+1;
00332 }
00333
00334 m.hedge.resize(m.hedge.size()+n);
00335 m.hn+=n;
00336
00337 pu.newBase = &*m.hedge.begin();
00338 pu.newEnd = &m.hedge.back()+1;
00339
00340 if(pu.NeedUpdate())
00341 {
00342 int ii = 0;
00343 FaceIterator fi;
00344 for (fi=m.face.begin(); fi!=m.face.end(); ++fi)
00345 {
00346 if(HasFHAdjacency(m))
00347 if(!(*fi).IsD() && (*fi).FHp())
00348 pu.Update((*fi).FHp());
00349 }
00350
00351 {
00352 VertexIterator vi;
00353 for (vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
00354 if(HasVHAdjacency(m))
00355 if(!(*vi).IsD())
00356 if ((*vi).cVHp()!=0)
00357 pu.Update((*vi).VHp());
00358 }
00359
00360 {
00361 EdgeIterator ei;
00362 for (ei=m.edge.begin(); ei!=m.edge.end(); ++ei)
00363 if(HasEHAdjacency(m))
00364 if(!(*ei).IsD())
00365 if ((*ei).cEHp()!=0)
00366 pu.Update((*ei).EHp());
00367 }
00368
00369 {
00370 HEdgeIterator hi = m.hedge.begin();
00371 while(ii < m.hn - n)
00372 {
00373 if(!(*hi).IsD())
00374 {
00375 if(HasHNextAdjacency(m)) pu.Update((*hi).HNp());
00376 if(HasHPrevAdjacency(m)) pu.Update((*hi).HPp());
00377 if(HasHOppAdjacency(m)) pu.Update((*hi).HOp());
00378 ++ii;
00379 }
00380
00381 ++hi;
00382 }
00383 }
00384 }
00385 unsigned int siz = (unsigned int)m.hedge.size()-n;
00386
00387 last = m.hedge.begin();
00388 advance(last,siz);
00389
00390 return last;
00391 }
00392
00396 static HEdgeIterator AddHEdges(MeshType &m, int n)
00397 {
00398 PointerUpdater<HEdgePointer> pu;
00399 return AddHEdges(m, n,pu);
00400 }
00401
00405 static HEdgeIterator AddHEdges(MeshType &m, int n, std::vector<HEdgePointer*> &local_vec)
00406 {
00407 PointerUpdater<HEdgePointer> pu;
00408 HEdgeIterator v_ret = AddHEdges(m, n,pu);
00409
00410 typename std::vector<HEdgePointer *>::iterator ei;
00411 for(ei=local_vec.begin();ei!=local_vec.end();++ei)
00412 pu.Update(**ei);
00413 return v_ret;
00414 }
00415
00416
00420 static FaceIterator AddFaces(MeshType &m, int n)
00421 {
00422 PointerUpdater<FacePointer> pu;
00423 return AddFaces(m,n,pu);
00424 }
00425
00429 static FaceIterator AddFaces(MeshType &m, int n,std::vector<FacePointer *> &local_vec)
00430 {
00431 PointerUpdater<FacePointer> pu;
00432 FaceIterator f_ret= AddFaces(m,n,pu);
00433
00434 typename std::vector<FacePointer *>::iterator fi;
00435 for(fi=local_vec.begin();fi!=local_vec.end();++fi)
00436 pu.Update(**fi);
00437 return f_ret;
00438 }
00439
00444 static FaceIterator AddFaces(MeshType &m, int n, PointerUpdater<FacePointer> &pu)
00445 {
00446 FaceIterator last, fi;
00447 if(n == 0) return m.face.end();
00448 pu.Clear();
00449 if(m.face.empty()) {
00450 pu.oldBase=0;
00451 } else {
00452 pu.oldBase=&*m.face.begin();
00453 pu.oldEnd=&m.face.back()+1;
00454 last=m.face.end();
00455 }
00456
00457 m.face.resize(m.face.size()+n);
00458 m.fn+=n;
00459
00460
00461 typename std::set<PointerToAttribute>::iterator ai;
00462 for(ai = m.face_attr.begin(); ai != m.face_attr.end(); ++ai)
00463 ((PointerToAttribute)(*ai)).Resize(m.face.size());
00464
00465 pu.newBase = &*m.face.begin();
00466 pu.newEnd = &m.face.back()+1;
00467
00468 if(pu.NeedUpdate())
00469 {
00470 int ii = 0;
00471 FaceIterator fi = m.face.begin();
00472 while(ii<m.fn-n)
00473 {
00474 if(!(*fi).IsD())
00475 {
00476 if(HasFFAdjacency(m))
00477 for(int i = 0; i < (*fi).VN(); ++i)
00478 if ((*fi).cFFp(i)!=0) pu.Update((*fi).FFp(i));
00479
00480 if(HasPerVertexVFAdjacency(m) && HasPerFaceVFAdjacency(m))
00481 for(int i = 0; i < (*fi).VN(); ++i)
00482 if ((*fi).cVFp(i)!=0) pu.Update((*fi).VFp(i));
00483 ++ii;
00484 }
00485 ++fi;
00486 }
00487 VertexIterator vi;
00488 for (vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
00489 if(!(*vi).IsD())
00490 {
00491 if(HasPerVertexVFAdjacency(m) && HasPerFaceVFAdjacency(m))
00492 if ((*vi).cVFp()!=0)
00493 pu.Update((FaceType * &)(*vi).VFp());
00494
00495
00496
00497
00498 }
00499 EdgeIterator ei;
00500 for (ei=m.edge.begin(); ei!=m.edge.end(); ++ei)
00501 if(!(*ei).IsD())
00502 {
00503 if(HasEFAdjacency(m))
00504 if ((*ei).cEFp()!=0)
00505 pu.Update((FaceType * &)(*ei).EFp());
00506
00507
00508
00509
00510 }
00511
00512 HEdgeIterator hi;
00513 for (hi=m.hedge.begin(); hi!=m.hedge.end(); ++hi)
00514 if(!(*hi).IsD())
00515 {
00516 if(HasHFAdjacency(m))
00517 if ((*hi).cHFp()!=0)
00518 pu.Update((FaceType * &)(*hi).HFp());
00519
00520
00521
00522
00523 }
00524
00525 }
00526 unsigned int siz=(unsigned int)m.face.size()-n;
00527 last = m.face.begin();
00528 advance(last,siz);
00529 return last;
00530 }
00531
00535 static void DeleteFace(MeshType &m, FaceType &f)
00536 {
00537 assert(!f.IsD());
00538 f.SetD();
00539 --m.fn;
00540 }
00541
00545 static void DeleteVertex(MeshType &m, VertexType &v)
00546 {
00547 assert(!v.IsD());
00548 v.SetD();
00549 --m.vn;
00550 }
00551
00555 static void DeleteEdge(MeshType &m, EdgeType &e)
00556 {
00557 assert(!e.IsD());
00558 e.SetD();
00559 --m.en;
00560 }
00561
00565 static void DeleteHEdge(MeshType &m, HEdgeType &h)
00566 {
00567 assert(!h.IsD());
00568 h.SetD();
00569 --m.hn;
00570 }
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581 static void PermutateVertexVector(MeshType &m, std::vector<size_t> &newVertIndex )
00582 {
00583 for(unsigned int i=0;i<m.vert.size();++i)
00584 {
00585 if(newVertIndex[i]<size_t(m.vn))
00586 {
00587 assert(!m.vert[i].IsD());
00588 m.vert[ newVertIndex[i] ].ImportData(m.vert[i]);
00589 if(HasPerVertexVFAdjacency(m) &&HasPerFaceVFAdjacency(m) )
00590 if (m.vert[i].cVFp()!=0)
00591 {
00592 m.vert[ newVertIndex[i] ].VFp() = m.vert[i].cVFp();
00593 m.vert[ newVertIndex[i] ].VFi() = m.vert[i].cVFi();
00594 }
00595 }
00596 }
00597
00598
00599
00600 ReorderVert<typename MeshType::VertexType>(newVertIndex,m.vert);
00601
00602
00603 ReorderAttribute(m.vert_attr,newVertIndex,m);
00604
00605 m.vert.resize(m.vn);
00606
00607
00608 ResizeAttribute(m.vert_attr,m.vn,m);
00609
00610 FaceIterator fi;
00611 VertexPointer vbase=&m.vert[0];
00612 for(fi=m.face.begin();fi!=m.face.end();++fi)
00613 if(!(*fi).IsD())
00614 for(unsigned int i=0;i<3;++i)
00615 {
00616 size_t oldIndex = (*fi).V(i) - vbase;
00617 assert(vbase <= (*fi).V(i) && oldIndex < newVertIndex.size());
00618 (*fi).V(i) = vbase+newVertIndex[oldIndex];
00619 }
00620 }
00621
00622
00623
00624
00625
00626
00627
00628 static void CompactVertexVector( MeshType &m )
00629 {
00630
00631 if(m.vn==(int)m.vert.size()) return;
00632
00633
00634 std::vector<size_t> newVertIndex(m.vert.size(),std::numeric_limits<size_t>::max() );
00635
00636 size_t pos=0;
00637 size_t i=0;
00638
00639 for(i=0;i<m.vert.size();++i)
00640 {
00641 if(!m.vert[i].IsD())
00642 {
00643 newVertIndex[i]=pos;
00644 ++pos;
00645 }
00646 }
00647 assert((int)pos==m.vn);
00648 PermutateVertexVector(m,newVertIndex);
00649 }
00650
00651
00652
00653
00654
00655
00656
00657 static void CompactFaceVector( MeshType &m )
00658 {
00659
00660 if(m.fn==(int)m.face.size()) return;
00661
00662
00663 std::vector<size_t> newFaceIndex(m.face.size(),std::numeric_limits<size_t>::max() );
00664
00665 size_t pos=0;
00666 size_t i=0;
00667
00668 for(i=0;i<m.face.size();++i)
00669 {
00670 if(!m.face[i].IsD())
00671 {
00672 if(pos!=i)
00673 {
00674 m.face[pos].ImportData(m.face[i]);
00675 m.face[pos].V(0) = m.face[i].V(0);
00676 m.face[pos].V(1) = m.face[i].V(1);
00677 m.face[pos].V(2) = m.face[i].V(2);
00678 if(HasPerVertexVFAdjacency(m) && HasPerFaceVFAdjacency(m))
00679 for(int j=0;j<3;++j)
00680 if (m.face[i].cVFp(j)!=0) {
00681 m.face[pos].VFp(j) = m.face[i].cVFp(j);
00682 m.face[pos].VFi(j) = m.face[i].cVFi(j);
00683 }
00684 if(HasFFAdjacency(m))
00685 for(int j=0;j<3;++j)
00686 if (m.face[i].cFFp(j)!=0) {
00687 m.face[pos].FFp(j) = m.face[i].cFFp(j);
00688 m.face[pos].FFi(j) = m.face[i].cFFi(j);
00689 }
00690 }
00691 newFaceIndex[i]=pos;
00692 ++pos;
00693 }
00694 }
00695 assert((int)pos==m.fn);
00696
00697
00698
00699 ReorderFace<typename MeshType::FaceType>(newFaceIndex,m.face);
00700
00701
00702 ReorderAttribute(m.face_attr,newFaceIndex,m);
00703
00704
00705 VertexIterator vi;
00706 FacePointer fbase=&m.face[0];
00707 for (vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
00708 if(!(*vi).IsD())
00709 {
00710 if(HasPerVertexVFAdjacency(m) &&HasPerFaceVFAdjacency(m) )
00711 if ((*vi).cVFp()!=0)
00712 {
00713 size_t oldIndex = (*vi).cVFp() - fbase;
00714 assert(fbase <= (*vi).cVFp() && oldIndex < newFaceIndex.size());
00715 (*vi).VFp() = fbase+newFaceIndex[oldIndex];
00716 }
00717 }
00718
00719
00720 m.face.resize(m.fn);
00721
00722 ResizeAttribute(m.face_attr,m.vn,m);
00723
00724 FaceIterator fi;
00725 for(fi=m.face.begin();fi!=m.face.end();++fi)
00726 if(!(*fi).IsD())
00727 {
00728 if(HasPerVertexVFAdjacency(m) &&HasPerFaceVFAdjacency(m) )
00729 for(i=0;i<3;++i)
00730 if ((*fi).cVFp(i)!=0)
00731 {
00732 size_t oldIndex = (*fi).VFp(i) - fbase;
00733 assert(fbase <= (*fi).VFp(i) && oldIndex < newFaceIndex.size());
00734 (*fi).VFp(i) = fbase+newFaceIndex[oldIndex];
00735 }
00736 if(HasFFAdjacency(m))
00737 for(i=0;i<3;++i)
00738 if ((*fi).cFFp(i)!=0)
00739 {
00740 size_t oldIndex = (*fi).FFp(i) - fbase;
00741 assert(fbase <= (*fi).FFp(i) && oldIndex < newFaceIndex.size());
00742 (*fi).FFp(i) = fbase+newFaceIndex[oldIndex];
00743 }
00744 }
00745 }
00746
00747 public:
00748
00750 template <class ATTR_TYPE>
00751 static
00752 bool IsValidHandle( MeshType & m, const typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE> & a){
00753 if(a._handle == NULL) return false;
00754 for(AttrIterator i = m.vert_attr.begin(); i!=m.vert_attr.end();++i)
00755 if ( (*i).n_attr == a.n_attr ) return true;
00756 return false;
00757 }
00758
00759 template <class ATTR_TYPE>
00760 static
00761 typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>
00762 AddPerVertexAttribute( MeshType & m, std::string name){
00763 PAIte i;
00764 PointerToAttribute h;
00765 h._name = name;
00766 if(!name.empty()){
00767 i = m.vert_attr.find(h);
00768 assert(i ==m.vert_attr.end() );
00769 }
00770 h._typename = typeid(ATTR_TYPE).name();
00771 h._sizeof = sizeof(ATTR_TYPE);
00772 h._padding = 0;
00773 h._handle = (void*) new SimpleTempData<VertContainer,ATTR_TYPE>(m.vert);
00774 m.attrn++;
00775 h.n_attr = m.attrn;
00776 std::pair < AttrIterator , bool> res = m.vert_attr.insert(h);
00777 return typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>(res.first->_handle,res.first->n_attr );
00778 }
00779
00780 template <class ATTR_TYPE>
00781 static
00782 typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>
00783 AddPerVertexAttribute( MeshType & m){
00784 return AddPerVertexAttribute<ATTR_TYPE>(m,std::string(""));
00785 }
00786
00787 template <class ATTR_TYPE>
00788 static
00789 typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>
00790 GetPerVertexAttribute( MeshType & m, const std::string & name){
00791 assert(!name.empty());
00792 PointerToAttribute h1; h1._name = name;
00793 typename std::set<PointerToAttribute > :: iterator i;
00794
00795 i =m.vert_attr.find(h1);
00796 if(i!=m.vert_attr.end())
00797 if((*i)._typename == typeid(ATTR_TYPE).name() ){
00798 if( (*i)._padding != 0 ){
00799 PointerToAttribute attr = (*i);
00800 m.vert_attr.erase(i);
00801 FixPaddedPerVertexAttribute<ATTR_TYPE>(m,attr);
00802 std::pair<AttrIterator,bool> new_i = m.vert_attr.insert(attr);
00803 assert(new_i.second);
00804 i = new_i.first;
00805 }
00806
00807 return typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr);
00808 }
00809
00810 return typename MeshType:: template PerVertexAttributeHandle<ATTR_TYPE>(NULL,0);
00811
00812 }
00813
00814 template <class ATTR_TYPE>
00815 static void GetAllPerVertexAttribute(const MeshType & m, std::vector<std::string> &all){
00816 typename std::set<PointerToAttribute > ::const_iterator i;
00817 for(i = m.vert_attr.begin(); i != m.vert_attr.end(); ++i )
00818 if((*i)._typename == typeid(ATTR_TYPE).name())
00819 all.push_back((*i)._name);
00820 }
00821
00822 template <class ATTR_TYPE>
00823 static
00824 void
00825 DeletePerVertexAttribute( MeshType & m,typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE> & h){
00826 typename std::set<PointerToAttribute > ::iterator i;
00827 for( i = m.vert_attr.begin(); i != m.vert_attr.end(); ++i)
00828 if( (*i)._handle == h._handle ){
00829 delete ((SimpleTempData<VertContainer,ATTR_TYPE>*)(*i)._handle);
00830 m.vert_attr.erase(i);
00831 return;}
00832 assert(0);
00833 }
00834
00835 static
00836 void DeletePerVertexAttribute( MeshType & m, std::string name){
00837 AttrIterator i;
00838 PointerToAttribute h1; h1._name = name;
00839 i = m.vert_attr.find(h1);
00840 assert(i!=m.vert_attr.end());
00841 delete ((SimpleTempDataBase<VertContainer>*)(*i)._handle);
00842 m.vert_attr.erase(i);
00843 }
00844
00845
00846
00848 template <class ATTR_TYPE>
00849 static
00850 bool IsValidHandle( MeshType & m, const typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE> & a){
00851 if(a._handle == NULL) return false;
00852 for(AttrIterator i = m.edge_attr.begin(); i!=m.edge_attr.end();++i)
00853 if ( (*i).n_attr == a.n_attr ) return true;
00854 return false;
00855 }
00856
00857 template <class ATTR_TYPE>
00858 static
00859 typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE>
00860 AddPerEdgeAttribute( MeshType & m, std::string name){
00861 PAIte i;
00862 PointerToAttribute h;
00863 h._name = name;
00864 if(!name.empty()){
00865 i = m.edge_attr.find(h);
00866 assert(i ==m.edge_attr.end() );
00867 }
00868 h._sizeof = sizeof(ATTR_TYPE);
00869 h._typename = typeid(ATTR_TYPE).name();
00870 h._handle = (void*) new SimpleTempData<EdgeContainer,ATTR_TYPE>(m.edge);
00871 m.attrn++;
00872 h.n_attr = m.attrn;
00873 std::pair < AttrIterator , bool> res = m.edge_attr.insert(h);
00874 return typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE>(res.first->_handle,res.first->n_attr);
00875 }
00876
00877 template <class ATTR_TYPE>
00878 static
00879 typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>
00880 AddPerEdgeAttribute( MeshType & m){
00881 return AddPerEdgeAttribute<ATTR_TYPE>(m,std::string(""));
00882 }
00883
00884 template <class ATTR_TYPE>
00885 static
00886 typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE>
00887 GetPerEdgeAttribute( const MeshType & m, const std::string & name){
00888 assert(!name.empty());
00889 PointerToAttribute h1; h1._name = name;
00890 typename std::set<PointerToAttribute > ::const_iterator i;
00891
00892 i =m.edge_attr.find(h1);
00893
00894 if(i!=m.edge_attr.end())
00895 if((*i)._typename == typeid(ATTR_TYPE).name() )
00896 return typename MeshType:: template PerVertexAttributeHandle<ATTR_TYPE>(NULL,0);
00897
00898 return typename MeshType:: template PerFaceAttributeHandle<ATTR_TYPE>(NULL,0);
00899 }
00900
00901 template <class ATTR_TYPE>
00902 static void GetAllPerEdgeAttribute(const MeshType & m, std::vector<std::string> &all){
00903 typename std::set<PointerToAttribute > :: iterator i;
00904 for(i = m.edge_attr.begin(); i != m.edge_attr.end(); ++i )
00905 if((*i)._typename == typeid(ATTR_TYPE).name())
00906 all.push_back((*i)._name);
00907 }
00908
00909 template <class ATTR_TYPE>
00910 static
00911 void
00912 DeletePerEdgeAttribute( MeshType & m,typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE> & h){
00913 typename std::set<PointerToAttribute > ::iterator i;
00914 for( i = m.edge_attr.begin(); i != m.edge_attr.end(); ++i)
00915 if( (*i)._handle == h._handle ){
00916 delete ((SimpleTempData<FaceContainer,ATTR_TYPE>*)(*i)._handle);
00917 m.edge_attr.erase(i);
00918 return;}
00919 assert(0);
00920 }
00921
00922 static
00923 void DeletePerEdgeAttribute( MeshType & m, std::string name){
00924 AttrIterator i;
00925 PointerToAttribute h1; h1._name = name;
00926 i = m.edge_attr.find(h1);
00927 assert(i!=m.edge_attr.end());
00928 delete ((SimpleTempDataBase<EdgeContainer>*)(*i)._handle);
00929 m.edge_attr.erase(i);
00930 }
00931
00933 template <class ATTR_TYPE>
00934 static
00935 bool IsValidHandle( MeshType & m, const typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE> & a){
00936 if(a._handle == NULL) return false;
00937 for(AttrIterator i = m.face_attr.begin(); i!=m.face_attr.end();++i)
00938 if ( (*i).n_attr == a.n_attr ) return true;
00939 return false;
00940 }
00941
00942 template <class ATTR_TYPE>
00943 static
00944 typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>
00945 AddPerFaceAttribute( MeshType & m, std::string name){
00946 PAIte i;
00947 PointerToAttribute h;
00948 h._name = name;
00949 if(!name.empty()){
00950 i = m.face_attr.find(h);
00951 assert(i ==m.face_attr.end() );
00952 }
00953 h._typename = typeid(ATTR_TYPE).name();
00954 h._sizeof = sizeof(ATTR_TYPE);
00955 h._handle = (void*) new SimpleTempData<FaceContainer,ATTR_TYPE>(m.face);
00956 m.attrn++;
00957 h.n_attr = m.attrn;
00958 std::pair < AttrIterator , bool> res = m.face_attr.insert(h);
00959 return typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>(res.first->_handle,res.first->n_attr);
00960 }
00961
00962 template <class ATTR_TYPE>
00963 static
00964 typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>
00965 AddPerFaceAttribute( MeshType & m){
00966 return AddPerFaceAttribute<ATTR_TYPE>(m,std::string(""));
00967 }
00968
00969 template <class ATTR_TYPE>
00970 static
00971 typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>
00972 GetPerFaceAttribute( MeshType & m, const std::string & name){
00973 assert(!name.empty());
00974 PointerToAttribute h1; h1._name = name;
00975 typename std::set<PointerToAttribute > ::iterator i;
00976
00977 i =m.face_attr.find(h1);
00978 if(i!=m.face_attr.end())
00979 if((*i)._typename == typeid(ATTR_TYPE).name() ){
00980 if( (*i)._padding != 0 ){
00981 PointerToAttribute attr = (*i);
00982 m.face_attr.erase(i);
00983 FixPaddedPerFaceAttribute<ATTR_TYPE>(m,attr);
00984 std::pair<AttrIterator,bool> new_i = m.face_attr.insert(attr);
00985 assert(new_i.second);
00986 i = new_i.first;
00987 }
00988 return typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr);
00989 }
00990 return typename MeshType:: template PerFaceAttributeHandle<ATTR_TYPE>(NULL,0);
00991 }
00992
00993 template <class ATTR_TYPE>
00994 static void GetAllPerFaceAttribute(const MeshType & m, std::vector<std::string> &all){
00995 typename std::set<PointerToAttribute > :: const_iterator i;
00996 for(i = m.face_attr.begin(); i != m.face_attr.end(); ++i )
00997 if((*i)._typename == typeid(ATTR_TYPE).name())
00998 all.push_back((*i)._name);
00999 }
01000
01001 template <class ATTR_TYPE>
01002 static
01003 void
01004 DeletePerFaceAttribute( MeshType & m,typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE> & h){
01005 typename std::set<PointerToAttribute > ::iterator i;
01006 for( i = m.face_attr.begin(); i != m.face_attr.end(); ++i)
01007 if( (*i)._handle == h._handle ){
01008 delete ((SimpleTempData<FaceContainer,ATTR_TYPE>*)(*i)._handle);
01009 m.face_attr.erase(i);
01010 return;}
01011 assert(0);
01012 }
01013
01014 static
01015 void DeletePerFaceAttribute( MeshType & m, std::string name){
01016 AttrIterator i;
01017 PointerToAttribute h1; h1._name = name;
01018 i = m.face_attr.find(h1);
01019 assert(i!=m.face_attr.end());
01020 delete ((SimpleTempDataBase<FaceContainer>*)(*i)._handle);
01021 m.face_attr.erase(i);
01022 }
01023
01025 template <class ATTR_TYPE>
01026 static
01027 bool IsValidHandle( MeshType & m, const typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE> & a){
01028 if(a._handle == NULL) return false;
01029 for(AttrIterator i = m.mesh_attr.begin(); i!=m.mesh_attr.end();++i)
01030 if ( (*i).n_attr == a.n_attr ) return true;
01031 return false;
01032 }
01033
01034 template <class ATTR_TYPE>
01035 static
01036 typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>
01037 AddPerMeshAttribute( MeshType & m, std::string name){
01038 PAIte i;
01039 PointerToAttribute h;
01040 h._name = name;
01041 if(!name.empty()){
01042 i = m.mesh_attr.find(h);
01043 assert(i ==m.mesh_attr.end() );
01044 }
01045 h._typename = typeid(ATTR_TYPE).name();
01046 h._sizeof = sizeof(ATTR_TYPE);
01047 h._handle = (void*) new Attribute<ATTR_TYPE>();
01048 m.attrn++;
01049 h.n_attr = m.attrn;
01050 std::pair < AttrIterator , bool> res = m.mesh_attr.insert(h);
01051 return typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>(res.first->_handle,res.first->n_attr);
01052 }
01053
01054 template <class ATTR_TYPE>
01055 static
01056 typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>
01057 GetPerMeshAttribute( MeshType & m, const std::string & name){
01058 assert(!name.empty());
01059 PointerToAttribute h1; h1._name = name;
01060 typename std::set<PointerToAttribute > ::iterator i;
01061
01062 i =m.mesh_attr.find(h1);
01063 if(i!=m.mesh_attr.end())
01064 if((*i)._typename == typeid(ATTR_TYPE).name() ){
01065 if( (*i)._padding != 0 ){
01066 PointerToAttribute attr = (*i);
01067 m.mesh_attr.erase(i);
01068 FixPaddedPerMeshAttribute<ATTR_TYPE>(m,attr);
01069 std::pair<AttrIterator,bool> new_i = m.mesh_attr.insert(attr);
01070 assert(new_i.second);
01071 i = new_i.first;
01072 }
01073
01074 return typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr);
01075 }
01076
01077 return typename MeshType:: template PerMeshAttributeHandle<ATTR_TYPE>(NULL,0);
01078 }
01079
01080 template <class ATTR_TYPE>
01081 static void GetAllPerMeshAttribute(const MeshType & m, std::vector<std::string> &all){
01082 typename std::set<PointerToAttribute > :: iterator i;
01083 for(i = m.mesh_attr.begin(); i != m.mesh_attr.end(); ++i )
01084 if((*i)._typename == typeid(ATTR_TYPE).name())
01085 all.push_back((*i)._name);
01086 }
01087
01088 template <class ATTR_TYPE>
01089 static
01090 void
01091 DeletePerMeshAttribute( MeshType & m,typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE> & h){
01092 typename std::set<PointerToAttribute > ::iterator i;
01093 for( i = m.mesh_attr.begin(); i != m.mesh_attr.end(); ++i)
01094 if( (*i)._handle == h._handle ){
01095 delete (( Attribute<ATTR_TYPE> *)(*i)._handle);
01096 m.mesh_attr.erase(i);
01097 return;}
01098 assert(0);
01099 }
01100
01101 static
01102 void DeletePerMeshAttribute( MeshType & m, std::string name){
01103 AttrIterator i;
01104 PointerToAttribute h1; h1._name = name;
01105 i = m.mesh_attr.find(h1);
01106 assert(i!=m.mesh_attr.end());
01107 delete ((AttributeBase *)(*i)._handle);
01108 m.mesh_attr.erase(i);
01109 }
01110
01111 template <class ATTR_TYPE>
01112 static
01113 void FixPaddedPerVertexAttribute ( MeshType & m,PointerToAttribute & pa){
01114
01115
01116 SimpleTempData<VertContainer,ATTR_TYPE>* _handle = new SimpleTempData<VertContainer,ATTR_TYPE>(m.vert);
01117
01118
01119 _handle->Resize(m.vert.size());
01120 for(unsigned int i = 0; i < m.vert.size(); ++i){
01121 ATTR_TYPE * dest = &(*_handle)[i];
01122 char * ptr = (char*)( ((SimpleTempDataBase<VertContainer> *)pa._handle)->DataBegin());
01123 memcpy((void*)dest ,
01124 (void*) &(ptr[i * pa._sizeof ]) ,sizeof(ATTR_TYPE));
01125 }
01126
01127
01128 delete ((SimpleTempDataBase<VertContainer>*) pa._handle);
01129
01130
01131 pa._sizeof = sizeof(ATTR_TYPE);
01132
01133
01134 pa._handle = _handle;
01135
01136
01137 pa._padding = 0;
01138 }
01139
01140 template <class ATTR_TYPE>
01141 static
01142 void FixPaddedPerFaceAttribute ( MeshType & m,PointerToAttribute & pa){
01143
01144
01145 SimpleTempData<FaceContainer,ATTR_TYPE>* _handle = new SimpleTempData<FaceContainer,ATTR_TYPE>(m.face);
01146
01147
01148 _handle->Resize(m.face.size());
01149 for(unsigned int i = 0; i < m.face.size(); ++i){
01150 ATTR_TYPE * dest = &(*_handle)[i];
01151 char * ptr = (char*)( ((SimpleTempDataBase<FaceContainer> *)pa._handle)->DataBegin());
01152 memcpy((void*)dest ,
01153 (void*) &(ptr[i * pa._sizeof ]) ,sizeof(ATTR_TYPE));
01154 }
01155
01156
01157 delete ((SimpleTempDataBase<FaceContainer>*) pa._handle);
01158
01159
01160 pa._sizeof = sizeof(ATTR_TYPE);
01161
01162
01163 pa._handle = _handle;
01164
01165
01166 pa._padding = 0;
01167 }
01168
01169
01170 template <class ATTR_TYPE>
01171 static
01172 void FixPaddedPerMeshAttribute ( MeshType & m,PointerToAttribute & pa){
01173
01174
01175 Attribute<ATTR_TYPE> * _handle = new Attribute<ATTR_TYPE>();
01176
01177
01178 char * ptr = (char*)( ((Attribute<ATTR_TYPE> *)pa._handle)->DataBegin());
01179 memcpy((void*)_handle->attribute ,(void*) &(ptr[0]) ,sizeof(ATTR_TYPE));
01180
01181
01182 delete ( (Attribute<ATTR_TYPE> *) pa._handle);
01183
01184
01185 pa._sizeof = sizeof(ATTR_TYPE);
01186
01187
01188 pa._handle = _handle;
01189
01190
01191 pa._padding = 0;
01192 }
01193
01194
01195
01196
01197
01198
01199
01200 struct NameTypeBound_Base{
01201 virtual std::string Name() = 0;
01202 virtual std::string TypeID() = 0;
01203
01204 virtual void AddPerVertexAttribute(MeshType & m) = 0;
01205 virtual void AddPerFaceAttribute(MeshType & m) = 0;
01206 virtual void AddPerEdgeAttribute(MeshType & m) = 0;
01207 virtual void AddPerMeshAttribute(MeshType & m) = 0;
01208
01209 };
01210
01211
01212 typedef typename std::map<std::string,NameTypeBound_Base*>::iterator BindersIterator;
01213 typedef typename std::map<std::string,NameTypeBound_Base*>::const_iterator CBindersIterator;
01214 typedef std::pair<std::string,NameTypeBound_Base*> TypeBound;
01215 typedef std::map<std::string,NameTypeBound_Base*> NameTypeScope;
01216
01217
01218
01219 template <class TYPE>
01220 struct NameTypeBound: public NameTypeBound_Base{
01221 NameTypeBound(){}
01222 NameTypeBound(std::string name){_name = name ;}
01223 std::string Name() {return _name;}
01224 bool operator ==(const NameTypeBound & o ) const {return Name()==o.Name();}
01225
01226 std::string TypeID(){ return typeid(TYPE).name();}
01227
01228 void AddPerVertexAttribute(MeshType & m){Allocator::template AddPerVertexAttribute<TYPE> (m,_name);}
01229 void AddPerFaceAttribute(MeshType & m) {Allocator::template AddPerFaceAttribute<TYPE> (m,_name);}
01230 void AddPerEdgeAttribute(MeshType & m) {Allocator::template AddPerEdgeAttribute<TYPE> (m,_name);}
01231 void AddPerMeshAttribute(MeshType & m) {Allocator::template AddPerMeshAttribute<TYPE> (m,_name);}
01232 private:
01233 std::string _name;
01234 };
01235
01236 static bool CheckNameIsBound(const NameTypeScope & binders,std::string name){ return (binders.find(name)!=binders.end()); }
01237
01238 template <class TYPE>
01239 static void AddNameTypeBound(NameTypeScope & binders,std::string name){
01240 assert(!name.empty());
01241 BindersIterator bi = binders.find(name);
01242 if(bi!=binders.end())
01243 assert(typeid(TYPE).name() == ((*bi).second)->TypeID());
01244 else{
01245 NameTypeBound<TYPE> * newbound = new NameTypeBound<TYPE>(name);
01246 binders.insert( TypeBound(name,newbound));
01247 }
01248 }
01249
01250 static void RemoveTypeBound( NameTypeScope& binders,std::string name){
01251 BindersIterator bi = binders.find(name);
01252 if(bi!=binders.end()) {delete(*bi).second; binders.erase(bi);}
01253 }
01254
01255
01256 template <typename TYPE>
01257 static std::vector<std::string> NamesWithType(const NameTypeScope & binders){
01258 std::vector<std::string> res;
01259 CBindersIterator bi;
01260 for(bi = binders.begin(); bi != binders.end(); ++bi)
01261 if (typeid(TYPE).name() == ((*bi).second->TypeID()))
01262 res.push_back( (*bi).second->Name());
01263 return res;
01264 }
01265
01266 static void AddPerVertexAttribute(const NameTypeScope & binders, MeshType & m, std::string name){
01267 BindersIterator bi = binders.find(name);
01268 assert(bi != binders.end() );
01269 (*bi).second->AddPerVertexAttribute(m);
01270 }
01271
01272 static void AddPerEdgeAttribute(const NameTypeScope & binders, MeshType & m, std::string name){
01273 BindersIterator bi = binders.find(name);
01274 assert(bi != binders.end() );
01275 (*bi).second->AddPerEdgeAttribute(m);
01276 }
01277
01278 static void AddPerFaceAttribute(const NameTypeScope & binders,MeshType & m, std::string name){
01279 BindersIterator bi = binders.find(name);
01280 assert(bi != binders.end() );
01281 (*bi).second->AddPerFaceAttribute(m);
01282 }
01283
01284 static void AddPerMeshAttribute( const NameTypeScope & binders,MeshType & m, std::string name){
01285 CBindersIterator bi = binders.find(name);
01286 assert(bi != binders.end() );
01287 (*bi).second->AddPerMeshAttribute(m);
01288 }
01289
01290
01291
01292
01293 };
01294
01295
01297 }
01298 }
01299
01300 #endif