VCG Library
allocate.h
1 /***************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 #ifndef __VCGLIB_TRIALLOCATOR
24 #define __VCGLIB_TRIALLOCATOR
25 
26 #ifndef __VCG_MESH
27 #error "This file should not be included alone. It is automatically included by complex.h"
28 #endif
29 
30 namespace vcg {
31 namespace tri {
36 template<class MeshType>
37 size_t Index(MeshType &m, const typename MeshType::VertexType &v) {return &v-&*m.vert.begin();}
38 template<class MeshType>
39 size_t Index(MeshType &m, const typename MeshType::FaceType &f) {return &f-&*m.face.begin();}
40 template<class MeshType>
41 size_t Index(MeshType &m, const typename MeshType::EdgeType &e) {return &e-&*m.edge.begin();}
42 template<class MeshType>
43 size_t Index(MeshType &m, const typename MeshType::HEdgeType &h) {return &h-&*m.hedge.begin();}
44 template <class MeshType>
45 size_t Index(MeshType &m, const typename MeshType::TetraType &t) { return &t - &*m.tetra.begin(); }
46 
47 
48 template<class MeshType>
49 size_t Index(MeshType &m, const typename MeshType::VertexType *vp) {return vp-&*m.vert.begin();}
50 template<class MeshType>
51 size_t Index(MeshType &m, const typename MeshType::FaceType * fp) {return fp-&*m.face.begin();}
52 template<class MeshType>
53 size_t Index(MeshType &m, const typename MeshType::EdgeType* e) {return e-&*m.edge.begin();}
54 template<class MeshType>
55 size_t Index(MeshType &m, const typename MeshType::HEdgeType* h) {return h-&*m.hedge.begin();}
56 template <class MeshType>
57 size_t Index(MeshType &m, const typename MeshType::TetraType *t) { return t - &*m.tetra.begin(); }
58 
59 
60 template<class MeshType>
61 bool IsValidPointer( MeshType & m, const typename MeshType::VertexType *vp) { return ( m.vert.size() > 0 && (vp >= &*m.vert.begin()) && (vp <= &m.vert.back()) ); }
62 template<class MeshType>
63 bool IsValidPointer(MeshType & m, const typename MeshType::EdgeType *ep) { return ( m.edge.size() > 0 && (ep >= &*m.edge.begin()) && (ep <= &m.edge.back())); }
64 template<class MeshType>
65 bool IsValidPointer(MeshType & m, const typename MeshType::FaceType *fp) { return ( m.face.size() > 0 && (fp >= &*m.face.begin()) && (fp <= &m.face.back())); }
66 template<class MeshType>
67 bool IsValidPointer(MeshType & m, const typename MeshType::HEdgeType *hp) { return ( m.hedge.size() > 0 && (hp >= &*m.hedge.begin()) && (hp <= &m.hedge.back())); }
68 template <class MeshType>
69 bool IsValidPointer(MeshType &m, const typename MeshType::TetraType *tp) { return (m.tetra.size() > 0 && (tp >= &*m.tetra.begin()) && (tp <= &m.tetra.back())); }
70 
71 template <class MeshType, class ATTR_CONT>
72 void ReorderAttribute(ATTR_CONT &c, std::vector<size_t> & newVertIndex, MeshType & /* m */){
73  typename std::set<typename MeshType::PointerToAttribute>::iterator ai;
74  for(ai = c.begin(); ai != c.end(); ++ai)
75  ((typename MeshType::PointerToAttribute)(*ai)).Reorder(newVertIndex);
76 }
77 
78 template <class MeshType, class ATTR_CONT>
79 void ResizeAttribute(ATTR_CONT &c, size_t sz, MeshType &/*m*/){
80  typename std::set<typename MeshType::PointerToAttribute>::iterator ai;
81  for(ai =c.begin(); ai != c.end(); ++ai)
82  ((typename MeshType::PointerToAttribute)(*ai)).Resize(sz);
83 }
84 
92 template <class MeshType>
93 class Allocator
94 {
95 
96 public:
97  typedef typename MeshType::VertexType VertexType;
98  typedef typename MeshType::VertexPointer VertexPointer;
99  typedef typename MeshType::VertexIterator VertexIterator;
100  typedef typename MeshType::VertContainer VertContainer;
101 
102  typedef typename MeshType::EdgeType EdgeType;
103  typedef typename MeshType::EdgePointer EdgePointer;
104  typedef typename MeshType::EdgeIterator EdgeIterator;
105  typedef typename MeshType::EdgeContainer EdgeContainer;
106 
107  typedef typename MeshType::FaceType FaceType;
108  typedef typename MeshType::FacePointer FacePointer;
109  typedef typename MeshType::FaceIterator FaceIterator;
110  typedef typename MeshType::FaceContainer FaceContainer;
111 
112  typedef typename MeshType::HEdgeType HEdgeType;
113  typedef typename MeshType::HEdgePointer HEdgePointer;
114  typedef typename MeshType::HEdgeIterator HEdgeIterator;
115  typedef typename MeshType::HEdgeContainer HEdgeContainer;
116 
117  typedef typename MeshType::TetraType TetraType;
118  typedef typename MeshType::TetraPointer TetraPointer;
119  typedef typename MeshType::TetraIterator TetraIterator;
120  typedef typename MeshType::TetraContainer TetraContainer;
121 
122  typedef typename MeshType::CoordType CoordType;
123 
124  typedef typename MeshType::PointerToAttribute PointerToAttribute;
125  typedef typename std::set<PointerToAttribute>::iterator AttrIterator;
126  typedef typename std::set<PointerToAttribute>::const_iterator AttrConstIterator;
127  typedef typename std::set<PointerToAttribute >::iterator PAIte;
128 
140  template<class SimplexPointerType>
142  {
143  public:
144  PointerUpdater(void) : newBase(0), oldBase(0), newEnd(0), oldEnd(0), preventUpdateFlag(false) { ; }
145  void Clear(){newBase=oldBase=newEnd=oldEnd=0; remap.clear();}
150  void Update(SimplexPointerType &vp)
151  {
152  //if(vp>=newBase && vp<newEnd) return;
153  if(vp<oldBase || vp>oldEnd) return;
154  assert(vp>=oldBase);
155  assert(vp<oldEnd);
156  vp=newBase+(vp-oldBase);
157  if(!remap.empty())
158  vp = newBase + remap[vp-newBase];
159  }
163  bool NeedUpdate() {if((oldBase && newBase!=oldBase && !preventUpdateFlag) || !remap.empty()) return true; else return false;}
164 
165  SimplexPointerType newBase;
166  SimplexPointerType oldBase;
167  SimplexPointerType newEnd;
168  SimplexPointerType oldEnd;
169  std::vector<size_t> remap; // this vector keep the new position of an element. Uninitialized elements have max_int value to denote an element that has not to be remapped.
170 
171  bool preventUpdateFlag;
172  };
173 
174  /* +++++++++++++++ Add Vertices ++++++++++++++++ */
175 
186  static VertexIterator AddVertices(MeshType &m, size_t n, PointerUpdater<VertexPointer> &pu)
187  {
188  VertexIterator last;
189  if(n == 0)
190  return m.vert.end();
191  pu.Clear();
192 
193  if(m.vert.empty())
194  pu.oldBase=0; // if the vector is empty we cannot find the last valid element
195  else {
196  pu.oldBase=&*m.vert.begin();
197  pu.oldEnd=&m.vert.back()+1;
198  }
199 
200  m.vert.resize(m.vert.size()+n);
201  m.vn+=int(n);
202 
203  typename std::set<PointerToAttribute>::iterator ai;
204  for(ai = m.vert_attr.begin(); ai != m.vert_attr.end(); ++ai)
205  ((PointerToAttribute)(*ai)).Resize(m.vert.size());
206 
207  pu.newBase = &*m.vert.begin();
208  pu.newEnd = &m.vert.back()+1;
209  if(pu.NeedUpdate())
210  {
211  for (FaceIterator fi=m.face.begin(); fi!=m.face.end(); ++fi)
212  if(!(*fi).IsD())
213  for(int i=0; i < (*fi).VN(); ++i)
214  if ((*fi).cV(i)!=0) pu.Update((*fi).V(i));
215 
216  for (EdgeIterator ei=m.edge.begin(); ei!=m.edge.end(); ++ei)
217  if(!(*ei).IsD())
218  {
219  // if(HasEVAdjacency (m))
220  pu.Update((*ei).V(0));
221  pu.Update((*ei).V(1));
222  // if(HasEVAdjacency(m)) pu.Update((*ei).EVp());
223  }
224 
225  HEdgeIterator hi;
226  for (hi=m.hedge.begin(); hi!=m.hedge.end(); ++hi)
227  if(!(*hi).IsD())
228  {
229  if(HasHVAdjacency (m))
230  {
231  pu.Update((*hi).HVp());
232  }
233  }
234 
235  for (TetraIterator ti = m.tetra.begin(); ti != m.tetra.end(); ++ti)
236  if (!(*ti).IsD())
237  for (int i = 0; i < 4; ++i)
238  if ((*ti).cV(i) != 0)
239  pu.Update((*ti).V(i));
240 
241  // e poiche' lo spazio e' cambiato si ricalcola anche last da zero
242  }
243  size_t siz=(size_t)(m.vert.size()-n);
244 
245  last = m.vert.begin();
246  advance(last,siz);
247 
248  return last;// deve restituire l'iteratore alla prima faccia aggiunta;
249  }
250 
253  static VertexIterator AddVertices(MeshType &m, size_t n)
254  {
256  return AddVertices(m, n,pu);
257  }
258 
261  static VertexIterator AddVertices(MeshType &m, size_t n, std::vector<VertexPointer *> &local_vec)
262  {
264  VertexIterator v_ret = AddVertices(m, n,pu);
265 
266  typename std::vector<VertexPointer *>::iterator vi;
267  for(vi=local_vec.begin();vi!=local_vec.end();++vi)
268  pu.Update(**vi);
269  return v_ret;
270  }
271 
274  static VertexIterator AddVertex(MeshType &m, const CoordType &p)
275  {
276  VertexIterator v_ret = AddVertices(m, 1);
277  v_ret->P()=p;
278  return v_ret;
279  }
280 
283  static VertexIterator AddVertex(MeshType &m, const CoordType &p, const CoordType &n)
284  {
285  VertexIterator v_ret = AddVertices(m, 1);
286  v_ret->P()=p;
287  v_ret->N()=n;
288  return v_ret;
289  }
290 
293  static VertexIterator AddVertex(MeshType &m, const CoordType &p, const Color4b &c)
294  {
295  VertexIterator v_ret = AddVertices(m, 1);
296  v_ret->P()=p;
297  v_ret->C()=c;
298  return v_ret;
299  }
300 
301  /* +++++++++++++++ Add Edges ++++++++++++++++ */
302 
312  static EdgeIterator AddEdges(MeshType &m, size_t n, PointerUpdater<EdgePointer> &pu)
313  {
314  if(n == 0) return m.edge.end();
315  pu.Clear();
316  if(m.edge.empty()) pu.oldBase=0; // if the vector is empty we cannot find the last valid element
317  else {
318  pu.oldBase=&*m.edge.begin();
319  pu.oldEnd=&m.edge.back()+1;
320  }
321 
322  m.edge.resize(m.edge.size()+n);
323  m.en+=int(n);
324  size_t siz=(size_t)(m.edge.size()-n);
325  EdgeIterator firstNewEdge = m.edge.begin();
326  advance(firstNewEdge,siz);
327 
328  typename std::set<typename MeshType::PointerToAttribute>::iterator ai;
329  for(ai = m.edge_attr.begin(); ai != m.edge_attr.end(); ++ai)
330  ((typename MeshType::PointerToAttribute)(*ai)).Resize(m.edge.size());
331 
332  pu.newBase = &*m.edge.begin();
333  pu.newEnd = &m.edge.back()+1;
334  if(pu.NeedUpdate())
335  {
336  if(HasFEAdjacency(m))
337  for (FaceIterator fi=m.face.begin(); fi!=m.face.end(); ++fi){
338  if(!(*fi).IsD())
339  for(int i=0; i < (*fi).VN(); ++i)
340  if ((*fi).cFEp(i)!=0) pu.Update((*fi).FEp(i));
341  }
342 
343  if(HasVEAdjacency(m)){
344  for (VertexIterator vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
345  if(!(*vi).IsD())
346  if ((*vi).cVEp()!=0) pu.Update((*vi).VEp());
347  for(EdgeIterator ei=m.edge.begin();ei!=firstNewEdge;++ei)
348  if(!(*ei).IsD())
349  {
350  if ((*ei).cVEp(0)!=0) pu.Update((*ei).VEp(0));
351  if ((*ei).cVEp(1)!=0) pu.Update((*ei).VEp(1));
352  }
353  }
354 
355  if(HasHEAdjacency(m))
356  for (HEdgeIterator hi=m.hedge.begin(); hi!=m.hedge.end(); ++hi)
357  if(!(*hi).IsD())
358  if ((*hi).cHEp()!=0) pu.Update((*hi).HEp());
359  }
360 
361  return firstNewEdge;// deve restituire l'iteratore alla prima faccia aggiunta;
362  }
363 
366  static EdgeIterator AddEdge(MeshType &m, VertexPointer v0, VertexPointer v1)
367  {
368  EdgeIterator ei= AddEdges(m, 1);
369  ei->V(0)=v0;
370  ei->V(1)=v1;
371  return ei;
372  }
373 
376  static EdgeIterator AddEdge(MeshType &m, size_t v0, size_t v1)
377  {
378  assert(v0!=v1);
379  assert(v0>=0 && v0<m.vert.size());
380  assert(v1>=0 && v1<m.vert.size());
381  return AddEdge(m,&(m.vert[v0]),&(m.vert[v1]));
382  }
383 
384 
387  static EdgeIterator AddEdge(MeshType &m, CoordType p0, CoordType p1)
388  {
389  VertexIterator vi = AddVertices(m,2);
390  EdgeIterator ei = AddEdges(m,1);
391  vi->P()=p0;
392  ei->V(0)=&*vi++;
393  vi->P()=p1;
394  ei->V(1)=&*vi++;
395  return ei;
396  }
397 
401  static EdgeIterator AddEdges(MeshType &m, size_t n)
402  {
404  return AddEdges(m, n,pu);
405  }
406 
410  static EdgeIterator AddEdges(MeshType &m, size_t n, std::vector<EdgePointer*> &local_vec)
411  {
413  EdgeIterator v_ret = AddEdges(m, n,pu);
414 
415  typename std::vector<EdgePointer *>::iterator ei;
416  for(ei=local_vec.begin();ei!=local_vec.end();++ei)
417  pu.Update(**ei);
418  return v_ret;
419  }
420 
421  /* +++++++++++++++ Add HalfEdges ++++++++++++++++ */
422 
432  static HEdgeIterator AddHEdges(MeshType &m, size_t n, PointerUpdater<HEdgePointer> &pu)
433  {
434  HEdgeIterator last;
435  if(n == 0) return m.hedge.end();
436  pu.Clear();
437  if(m.hedge.empty()) pu.oldBase=0; // if the vector is empty we cannot find the last valid element
438  else {
439  pu.oldBase=&*m.hedge.begin();
440  pu.oldEnd=&m.hedge.back()+1;
441  }
442 
443  m.hedge.resize(m.hedge.size()+n);
444  m.hn+=int(n);
445 
446  pu.newBase = &*m.hedge.begin();
447  pu.newEnd = &m.hedge.back()+1;
448 
449  if(pu.NeedUpdate())
450  {
451  if(HasFHAdjacency(m)) {
452  for (FaceIterator fi=m.face.begin(); fi!=m.face.end(); ++fi)
453  {
454  if(!(*fi).IsD() && (*fi).FHp())
455  pu.Update((*fi).FHp());
456  }
457  }
458  if(HasVHAdjacency(m)) {
459  for (VertexIterator vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
460  if(!(*vi).IsD() && (*vi).cVHp()!=0)
461  pu.Update((*vi).VHp());
462  }
463  if(HasEHAdjacency(m)) {
464  for (EdgeIterator ei=m.edge.begin(); ei!=m.edge.end(); ++ei)
465  if(!(*ei).IsD() && (*ei).cEHp()!=0)
466  pu.Update((*ei).EHp());
467  }
468 
469  int ii = 0;
470  HEdgeIterator hi = m.hedge.begin();
471  while(ii < m.hn - int(n))// cycle on all the faces except the new ones
472  {
473  if(!(*hi).IsD())
474  {
475  if(HasHNextAdjacency(m)) pu.Update((*hi).HNp());
476  if(HasHPrevAdjacency(m)) pu.Update((*hi).HPp());
477  if(HasHOppAdjacency(m)) pu.Update((*hi).HOp());
478  ++ii;
479  }
480  ++hi;
481  }
482  }
483  size_t siz = (size_t)(m.hedge.size()-n);
484 
485  last = m.hedge.begin();
486  advance(last,siz);
487 
488  return last;// deve restituire l'iteratore alla prima faccia aggiunta;
489  }
490 
494  static HEdgeIterator AddHEdges(MeshType &m, size_t n)
495  {
497  return AddHEdges(m, n,pu);
498  }
499 
503  static HEdgeIterator AddHEdges(MeshType &m, size_t n, std::vector<HEdgePointer*> &local_vec)
504  {
506  HEdgeIterator v_ret = AddHEdges(m, n,pu);
507 
508  typename std::vector<HEdgePointer *>::iterator ei;
509  for(ei=local_vec.begin();ei!=local_vec.end();++ei)
510  pu.Update(**ei);
511  return v_ret;
512  }
513 
514  /* +++++++++++++++ Add Faces ++++++++++++++++ */
515 
518  static FaceIterator AddFace(MeshType &m, VertexPointer v0, VertexPointer v1, VertexPointer v2)
519  {
520  assert(m.vert.size()>0);
521  assert((v0!=v1) && (v1!=v2) && (v0!=v2));
522  assert(v0>=&m.vert.front() && v0<=&m.vert.back());
523  assert(v1>=&m.vert.front() && v1<=&m.vert.back());
524  assert(v2>=&m.vert.front() && v2<=&m.vert.back());
526  FaceIterator fi = AddFaces(m,1,pu);
527  fi->Alloc(3);
528  fi->V(0)=v0;
529  fi->V(1)=v1;
530  fi->V(2)=v2;
531  return fi;
532  }
533 
536  static FaceIterator AddFace(MeshType &m, size_t v0, size_t v1, size_t v2)
537  {
538  assert((v0!=v1) && (v1!=v2) && (v0!=v2));
539  assert(v0>=0 && v0<m.vert.size());
540  assert(v1>=0 && v1<m.vert.size());
541  assert(v2>=0 && v2<m.vert.size());
542  return AddFace(m,&(m.vert[v0]),&(m.vert[v1]),&(m.vert[v2]));
543  }
546  static FaceIterator AddFace(MeshType &m, CoordType p0, CoordType p1, CoordType p2)
547  {
548  VertexIterator vi = AddVertices(m,3);
549  FaceIterator fi = AddFaces(m,1);
550  fi->Alloc(3);
551  vi->P()=p0;
552  fi->V(0)=&*vi++;
553  vi->P()=p1;
554  fi->V(1)=&*vi++;
555  vi->P()=p2;
556  fi->V(2)=&*vi;
557  return fi;
558  }
559 
564  static FaceIterator AddQuadFace(MeshType &m, VertexPointer v0, VertexPointer v1, VertexPointer v2, VertexPointer v3)
565  {
566  assert(m.vert.size()>0);
567  assert(v0>=&m.vert.front() && v0<=&m.vert.back());
568  assert(v1>=&m.vert.front() && v1<=&m.vert.back());
569  assert(v2>=&m.vert.front() && v2<=&m.vert.back());
570  assert(v3>=&m.vert.front() && v3<=&m.vert.back());
572  if(FaceType::HasPolyInfo())
573  {
574  FaceIterator fi = AddFaces(m,1,pu);
575  fi->Alloc(4);
576  fi->V(0)=v0; fi->V(1)=v1;
577  fi->V(2)=v2; fi->V(3)=v3;
578  return fi;
579  }
580  else
581  {
582  FaceIterator fi = AddFaces(m,2,pu);
583  fi->Alloc(3); fi->V(0)=v0; fi->V(1)=v1; fi->V(2)=v2;
584  fi->SetF(2);
585  ++fi;
586  fi->Alloc(3); fi->V(0)=v0; fi->V(1)=v2; fi->V(2)=v3;
587  fi->SetF(0);
588  return fi;
589  }
590  }
594  static FaceIterator AddFaces(MeshType &m, size_t n)
595  {
597  return AddFaces(m,n,pu);
598  }
599 
603  static FaceIterator AddFaces(MeshType &m, size_t n,std::vector<FacePointer *> &local_vec)
604  {
606  FaceIterator f_ret= AddFaces(m,n,pu);
607 
608  typename std::vector<FacePointer *>::iterator fi;
609  for(fi=local_vec.begin();fi!=local_vec.end();++fi)
610  pu.Update(**fi);
611  return f_ret;
612  }
613 
626  static FaceIterator AddFaces(MeshType &m, size_t n, PointerUpdater<FacePointer> &pu)
627  {
628  pu.Clear();
629  if(n == 0) return m.face.end();
630  if(!m.face.empty()) // if the vector is empty we cannot find the last valid element
631  {
632  pu.oldBase=&*m.face.begin();
633  pu.oldEnd=&m.face.back()+1;
634  }
635  // The actual resize
636  m.face.resize(m.face.size()+n);
637  m.fn+=int(n);
638 
639  size_t siz=(size_t)(m.face.size()-n);
640  FaceIterator firstNewFace = m.face.begin();
641  advance(firstNewFace,siz);
642 
643  typename std::set<PointerToAttribute>::iterator ai;
644  for(ai = m.face_attr.begin(); ai != m.face_attr.end(); ++ai)
645  ((PointerToAttribute)(*ai)).Resize(m.face.size());
646 
647  pu.newBase = &*m.face.begin();
648  pu.newEnd = &m.face.back()+1;
649 
650  if(pu.NeedUpdate())
651  {
652  if(HasFFAdjacency(m))
653  { // cycle on all the faces except the new ones
654  for(FaceIterator fi=m.face.begin();fi!=firstNewFace;++fi)
655  if(!(*fi).IsD())
656  for(int i = 0; i < (*fi).VN(); ++i)
657  if ((*fi).cFFp(i)!=0) pu.Update((*fi).FFp(i));
658  }
659 
660  if(HasPerVertexVFAdjacency(m) && HasPerFaceVFAdjacency(m))
661  { // cycle on all the faces except the new ones
662  for(FaceIterator fi=m.face.begin();fi!=firstNewFace;++fi)
663  if(!(*fi).IsD())
664  for(int i = 0; i < (*fi).VN(); ++i)
665  if ((*fi).cVFp(i)!=0) pu.Update((*fi).VFp(i));
666 
667  for (VertexIterator vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
668  if(!(*vi).IsD() && (*vi).cVFp()!=0)
669  pu.Update((*vi).VFp());
670  }
671 
672  if(HasEFAdjacency(m))
673  {
674  for (EdgeIterator ei=m.edge.begin(); ei!=m.edge.end(); ++ei)
675  if(!(*ei).IsD() && (*ei).cEFp()!=0)
676  pu.Update((*ei).EFp());
677  }
678 
679  if(HasHFAdjacency(m))
680  {
681  for (HEdgeIterator hi=m.hedge.begin(); hi!=m.hedge.end(); ++hi)
682  if(!(*hi).IsD() && (*hi).cHFp()!=0)
683  pu.Update((*hi).HFp());
684  }
685  }
686  return firstNewFace;
687  }
688 
689  //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
690  //:::::::::::::::::TETRAS ADDER FUNCTIONS:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
691 
704  static TetraIterator AddTetras(MeshType &m, size_t n, PointerUpdater<TetraPointer> &pu)
705  {
706  //nothing to do
707  if (n == 0)
708  return m.tetra.end();
709 
710  //prepare the pointerupdater info
711  pu.Clear();
712  if (m.tetra.empty())
713  pu.oldBase = 0;
714  else
715  {
716  pu.oldBase = &*m.tetra.begin();
717  pu.oldEnd = &m.tetra.back() + 1;
718  }
719 
720  //resize the tetra list and update tetra count
721  m.tetra.resize(m.tetra.size() + n);
722  m.tn += n;
723 
724  //get the old size and advance to the first new tetrahedron position
725  size_t oldSize = (size_t)(m.tetra.size() - n);
726 
727  TetraIterator firstNewTetra = m.tetra.begin();
728  advance(firstNewTetra, oldSize);
729 
730  //for each attribute make adapt the list size
731  typename std::set<typename MeshType::PointerToAttribute>::iterator ai;
732  for (ai = m.tetra_attr.begin(); ai != m.tetra_attr.end(); ++ai)
733  ((typename MeshType::PointerToAttribute)(*ai)).Resize(m.tetra.size());
734 
735  //do the update
736  pu.newBase = &*m.tetra.begin();
737  pu.newEnd = &m.tetra.back() + 1;
738  if (pu.NeedUpdate())
739  {
740  if (HasVTAdjacency(m))
741  {
742  for (VertexIterator vi = m.vert.begin(); vi != m.vert.end(); ++vi)
743  if (!vi->IsD())
744  pu.Update(vi->VTp());
745 
746  for (TetraIterator ti = m.tetra.begin(); ti != m.tetra.end(); ++ti)
747  if (!ti->IsD())
748  {
749  pu.Update(ti->VTp(0));
750  pu.Update(ti->VTp(1));
751  pu.Update(ti->VTp(2));
752  pu.Update(ti->VTp(3));
753  }
754  }
755 
756  //do edge and face adjacency
757  if (HasTTAdjacency(m))
758  for (TetraIterator ti = m.tetra.begin(); ti != m.tetra.end(); ++ti)
759  if (!ti->IsD())
760  {
761  pu.Update(ti->TTp(0));
762  pu.Update(ti->TTp(1));
763  pu.Update(ti->TTp(2));
764  pu.Update(ti->TTp(3));
765  }
766  }
767 
768  return firstNewTetra;
769  }
770 
771 //TODO: ADD 4 FACES then add tetra
774  static TetraIterator AddTetra(MeshType &m, VertexPointer v0, VertexPointer v1, VertexPointer v2, VertexPointer v3)
775  {
776  assert(m.vert.size() > 0);
777  assert((v0 != v1) && (v0 != v2) && (v0 != v3) && (v1 != v2) && (v1 != v3) && (v2 != v3));
778  assert(v0 >= &m.vert.front() && v0 <= &m.vert.back());
779  assert(v1 >= &m.vert.front() && v1 <= &m.vert.back());
780  assert(v2 >= &m.vert.front() && v2 <= &m.vert.back());
781  assert(v3 >= &m.vert.front() && v3 <= &m.vert.back());
782 
783 // AddFace(m, v0, v1, v2);
784 // AddFace(m, v0, v3, v1);
785 // AddFace(m, v0, v2, v3);
786 // AddFace(m, v1, v3, v2);
787 
789  TetraIterator ti = AddTetras(m, 1, pu);
790  ti->V(0) = v0;
791  ti->V(1) = v1;
792  ti->V(2) = v2;
793  ti->V(3) = v3;
794  return ti;
795  }
796 
799  static TetraIterator AddTetra(MeshType &m, const size_t v0, const size_t v1, const size_t v2, const size_t v3)
800  {
801  assert(m.vert.size() > 0);
802  assert((v0 != v1) && (v0 != v2) && (v0 != v3) && (v1 != v2) && (v1 != v3) && (v2 != v3));
803  assert(v0 >= 0 && v0 < m.vert.size());
804  assert(v1 >= 0 && v1 < m.vert.size());
805  assert(v2 >= 0 && v2 < m.vert.size());
806  assert(v3 >= 0 && v3 < m.vert.size());
807 
808  return AddTetra(m, &(m.vert[v0]), &(m.vert[v1]), &(m.vert[v2]), &(m.vert[v3]));
809  }
812  static TetraIterator AddTetra(MeshType &m, const CoordType & p0, const CoordType & p1, const CoordType & p2, const CoordType & p3)
813  {
814  VertexIterator vi = AddVertices(m, 4);
815 
816  VertexPointer v0 = &*vi++;
817  VertexPointer v1 = &*vi++;
818  VertexPointer v2 = &*vi++;
819  VertexPointer v3 = &*vi++;
820 
821  v0->P() = p0;
822  v1->P() = p1;
823  v2->P() = p2;
824  v3->P() = p3;
825 
826  return AddTetra(m, v0, v1, v2, v3);
827  }
828 
829 // //requires no duplicate vertices on faces you use
830 // static TetraIterator AddTetra(MeshType &m, const FaceType & f0, const FaceType & f1, const FaceType & f2, const FaceType & f3)
831 // {
832 // assert(m.face.size() > 0);
833 // assert((f0 != f1) && (f0 != f2) && (f0 != f3) && (f1 != f2) && (f1 != f3) && (f2 != f3));
834 // assert(f1 >= 0 && f1 < m.face.size());
835 // assert(f2 >= 0 && f2 < m.face.size());
836 // assert(f3 >= 0 && f3 < m.face.size());
837 // assert(f0 >= 0 && f0 < m.face.size());
838 // //TODO: decide if you want to address this like this
839 // //ERROR: can't use position...so..could force to have no dup verts..and use pointers or avoid this kind of thing
840 // assert(f0.V(0) == f1.V(0) && f0.V(0) == f2.V(0) && //v0
841 // f0.V(1) == f1.V(2) && f0.V(1) == f3.V(0) && //v1
842 // f0.V(2) == f2.V(1) && f0.V(2) == f3.V(2) && //v2
843 // f1.V(1) == f2.V(2) && f1.V(1) == f3.V(1) ) //v3
844 
845 // //add a tetra...and set vertices correctly
846 // PointerUpdater<TetraPointer> pu;
847 // TetraIterator ti = AddTetras(m, 1, pu);
848 // ti->V(0) = f0.V(0);
849 // ti->V(1) = f0.V(1);
850 // ti->V(2) = f0.V(2);
851 // ti->V(3) = f1.V(1);
852 
853 // return ti;
854 // }
855 
859  static TetraIterator AddTetras(MeshType &m, size_t n)
860  {
862  return AddTetras(m, n, pu);
863  }
864 
868  static TetraIterator AddTetras(MeshType &m, size_t n, std::vector<TetraPointer *> &local_vec)
869  {
871  TetraIterator t_ret = AddTetras(m, n, pu);
872 
873  typename std::vector<TetraPointer *>::iterator ti;
874  for (ti = local_vec.begin(); ti != local_vec.end(); ++ti)
875  pu.Update(**ti);
876  return t_ret;
877  }
878 
879  /* +++++++++++++++ Deleting ++++++++++++++++ */
880 
884  static void DeleteFace(MeshType &m, FaceType &f)
885  {
886  assert(&f >= &m.face.front() && &f <= &m.face.back());
887  assert(!f.IsD());
888  f.Dealloc();
889  f.SetD();
890  --m.fn;
891  }
892 
896  static void DeleteVertex(MeshType &m, VertexType &v)
897  {
898  assert(&v >= &m.vert.front() && &v <= &m.vert.back());
899  assert(!v.IsD());
900  v.SetD();
901  --m.vn;
902  }
903 
907  static void DeleteEdge(MeshType &m, EdgeType &e)
908  {
909  assert(&e >= &m.edge.front() && &e <= &m.edge.back());
910  assert(!e.IsD());
911  e.SetD();
912  --m.en;
913  }
914 
918  static void DeleteHEdge(MeshType &m, HEdgeType &h)
919  {
920  assert(&h >= &m.hedge.front() && &h <= &m.hedge.back());
921  assert(!h.IsD());
922  h.SetD();
923  --m.hn;
924  }
925 
929  static void DeleteTetra(MeshType &m, TetraType &t)
930  {
931  assert(&t >= &m.tetra.front() && &t <= &m.tetra.back());
932  assert(!t.IsD());
933  t.SetD();
934  --m.tn;
935  }
936 
937  /*
938  Function to rearrange the vertex vector according to a given index permutation
939  the permutation is vector such that after calling this function
940 
941  m.vert[ newVertIndex[i] ] = m.vert[i];
942 
943  e.g. newVertIndex[i] is the new index of the vertex i
944 
945  */
946  static void PermutateVertexVector(MeshType &m, PointerUpdater<VertexPointer> &pu)
947  {
948  if(m.vert.empty()) return;
949  for(size_t i=0;i<m.vert.size();++i)
950  {
951  if(pu.remap[i]<size_t(m.vn))
952  {
953  assert(!m.vert[i].IsD());
954  m.vert[ pu.remap [i] ].ImportData(m.vert[i]);
955  if(HasVFAdjacency(m))
956  {
957  if (m.vert[i].IsVFInitialized())
958  {
959  m.vert[ pu.remap[i] ].VFp() = m.vert[i].cVFp();
960  m.vert[ pu.remap[i] ].VFi() = m.vert[i].cVFi();
961  }
962  else m.vert [ pu.remap[i] ].VFClear();
963  }
964  if(HasVEAdjacency(m))
965  {
966  if (m.vert[i].IsVEInitialized())
967  {
968  m.vert[ pu.remap[i] ].VEp() = m.vert[i].cVEp();
969  m.vert[ pu.remap[i] ].VEi() = m.vert[i].cVEi();
970  }
971  else m.vert [ pu.remap[i] ].VEClear();
972  }
973  if (HasVTAdjacency(m))
974  {
975  if (m.vert[i].IsVTInitialized())
976  {
977  m.vert[ pu.remap[i] ].VTp() = m.vert[i].cVTp();
978  m.vert[ pu.remap[i] ].VTi() = m.vert[i].cVTi();
979  }
980  else m.vert[ pu.remap[i] ].VTClear();
981  }
982  }
983  }
984 
985  // reorder the optional atttributes in m.vert_attr to reflect the changes
986  ReorderAttribute(m.vert_attr,pu.remap,m);
987 
988  // setup the pointer updater
989  pu.oldBase = &m.vert[0];
990  pu.oldEnd = &m.vert.back()+1;
991 
992  // resize
993  m.vert.resize(m.vn);
994 
995  // setup the pointer updater
996  pu.newBase = (m.vert.empty())?0:&m.vert[0];
997  pu.newEnd = (m.vert.empty())?0:&m.vert.back()+1;
998 
999  // resize the optional atttributes in m.vert_attr to reflect the changes
1000  ResizeAttribute(m.vert_attr,m.vn,m);
1001 
1002  // Loop on the face to update the pointers FV relation (vertex refs)
1003  for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
1004  if(!(*fi).IsD())
1005  for(int i=0;i<fi->VN();++i)
1006  {
1007  size_t oldIndex = (*fi).V(i) - pu.oldBase;
1008  assert(pu.oldBase <= (*fi).V(i) && oldIndex < pu.remap.size());
1009  (*fi).V(i) = pu.newBase+pu.remap[oldIndex];
1010  }
1011  // Loop on the tetras to update the pointers TV relation (vertex refs)
1012  for(TetraIterator ti = m.tetra.begin(); ti != m.tetra.end(); ++ti)
1013  if(!(*ti).IsD())
1014  for(int i = 0; i < 4; ++i)
1015  {
1016  size_t oldIndex = (*ti).V(i) - pu.oldBase;
1017  assert(pu.oldBase <= (*ti).V(i) && oldIndex < pu.remap.size());
1018  (*ti).V(i) = pu.newBase+pu.remap[oldIndex];
1019  }
1020  // Loop on the edges to update the pointers EV relation (vertex refs)
1021  // if(HasEVAdjacency(m))
1022  for(EdgeIterator ei=m.edge.begin();ei!=m.edge.end();++ei)
1023  if(!(*ei).IsD())
1024  {
1025  pu.Update((*ei).V(0));
1026  pu.Update((*ei).V(1));
1027  }
1028  }
1029 
1030  static void CompactEveryVector(MeshType &m)
1031  {
1032  CompactVertexVector(m);
1033  CompactEdgeVector(m);
1034  CompactFaceVector(m);
1035  CompactTetraVector(m);
1036  }
1037 
1045  static void CompactVertexVector( MeshType &m, PointerUpdater<VertexPointer> &pu )
1046  {
1047  // If already compacted fast return please!
1048  if(m.vn==(int)m.vert.size()) return;
1049 
1050  // newVertIndex [ <old_vert_position> ] gives you the new position of the vertex in the vector;
1051  pu.remap.resize( m.vert.size(),std::numeric_limits<size_t>::max() );
1052 
1053  size_t pos=0;
1054  size_t i=0;
1055 
1056  for(i=0;i<m.vert.size();++i)
1057  {
1058  if(!m.vert[i].IsD())
1059  {
1060  pu.remap[i]=pos;
1061  ++pos;
1062  }
1063  }
1064  assert((int)pos==m.vn);
1065 
1066  PermutateVertexVector(m, pu);
1067  }
1068 
1070  static void CompactVertexVector( MeshType &m ) {
1072  CompactVertexVector(m,pu);
1073  }
1074 
1083  static void CompactEdgeVector( MeshType &m, PointerUpdater<EdgePointer> &pu )
1084  {
1085  // If already compacted fast return please!
1086  if(m.en==(int)m.edge.size()) return;
1087 
1088  // remap [ <old_edge_position> ] gives you the new position of the edge in the vector;
1089  pu.remap.resize( m.edge.size(),std::numeric_limits<size_t>::max() );
1090 
1091  size_t pos=0;
1092  size_t i=0;
1093 
1094  for(i=0;i<m.edge.size();++i)
1095  {
1096  if(!m.edge[i].IsD())
1097  {
1098  pu.remap[i]=pos;
1099  ++pos;
1100  }
1101  }
1102  assert((int)pos==m.en);
1103 
1104  // the actual copying of the data.
1105  for(size_t i=0;i<m.edge.size();++i)
1106  {
1107  if(pu.remap[i]<size_t(m.en)) // uninitialized entries in the remap vector has max_int value;
1108  {
1109  assert(!m.edge[i].IsD());
1110  m.edge[ pu.remap [i] ].ImportData(m.edge[i]);
1111  // copy the vertex reference (they are not data!)
1112  m.edge[ pu.remap[i] ].V(0) = m.edge[i].cV(0);
1113  m.edge[ pu.remap[i] ].V(1) = m.edge[i].cV(1);
1114  // Now just copy the adjacency pointers (without changing them, to be done later)
1115  if(HasVEAdjacency(m))
1116  //if (m.edge[i].cVEp(0)!=0)
1117  {
1118  m.edge[ pu.remap[i] ].VEp(0) = m.edge[i].cVEp(0);
1119  m.edge[ pu.remap[i] ].VEi(0) = m.edge[i].cVEi(0);
1120  m.edge[ pu.remap[i] ].VEp(1) = m.edge[i].cVEp(1);
1121  m.edge[ pu.remap[i] ].VEi(1) = m.edge[i].cVEi(1);
1122  }
1123  if(HasEEAdjacency(m))
1124 // if (m.edge[i].cEEp(0)!=0)
1125  {
1126  m.edge[ pu.remap[i] ].EEp(0) = m.edge[i].cEEp(0);
1127  m.edge[ pu.remap[i] ].EEi(0) = m.edge[i].cEEi(0);
1128  m.edge[ pu.remap[i] ].EEp(1) = m.edge[i].cEEp(1);
1129  m.edge[ pu.remap[i] ].EEi(1) = m.edge[i].cEEi(1);
1130  }
1131  if(HasEFAdjacency(m))
1132 // if (m.edge[i].cEEp(0)!=0)
1133  {
1134  m.edge[ pu.remap[i] ].EFp() = m.edge[i].cEFp();
1135  m.edge[ pu.remap[i] ].EFi() = m.edge[i].cEFi();
1136  m.edge[ pu.remap[i] ].EFp() = m.edge[i].cEFp();
1137  m.edge[ pu.remap[i] ].EFi() = m.edge[i].cEFi();
1138  }
1139 
1140  }
1141  }
1142 
1143  // reorder the optional attributes in m.vert_attr to reflect the changes
1144  ReorderAttribute(m.edge_attr, pu.remap,m);
1145 
1146  // setup the pointer updater
1147  pu.oldBase = &m.edge[0];
1148  pu.oldEnd = &m.edge.back()+1;
1149 
1150  // THE resize
1151  m.edge.resize(m.en);
1152 
1153  // setup the pointer updater
1154  pu.newBase = (m.edge.empty())?0:&m.edge[0];
1155  pu.newEnd = (m.edge.empty())?0:&m.edge.back()+1;
1156 
1157  // resize the optional atttributes in m.vert_attr to reflect the changes
1158  ResizeAttribute(m.edge_attr,m.en,m);
1159 
1160  // Loop on the vertices to update the pointers of VE relation
1161  if(HasVEAdjacency(m))
1162  for (VertexIterator vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
1163  if(!(*vi).IsD()) pu.Update((*vi).VEp());
1164 
1165  // Loop on the edges to update the pointers EE VE relation
1166  for(EdgeIterator ei=m.edge.begin();ei!=m.edge.end();++ei)
1167  for(unsigned int i=0;i<2;++i)
1168  {
1169  if(HasVEAdjacency(m))
1170  pu.Update((*ei).VEp(i));
1171  if(HasEEAdjacency(m))
1172  pu.Update((*ei).EEp(i));
1173 // if(HasEFAdjacency(m))
1174 // pu.Update((*ei).EFp());
1175  }
1176  }
1177 
1179  static void CompactEdgeVector( MeshType &m ) {
1181  CompactEdgeVector(m,pu);
1182  }
1183 
1192  static void CompactFaceVector( MeshType &m, PointerUpdater<FacePointer> &pu )
1193  {
1194  // If already compacted fast return please!
1195  if(m.fn==(int)m.face.size()) return;
1196 
1197  // newFaceIndex [ <old_face_position> ] gives you the new position of the face in the vector;
1198  pu.remap.resize( m.face.size(),std::numeric_limits<size_t>::max() );
1199 
1200  size_t pos=0;
1201  for(size_t i=0;i<m.face.size();++i)
1202  {
1203  if(!m.face[i].IsD())
1204  {
1205  if(pos!=i)
1206  {
1207  m.face[pos].ImportData(m.face[i]);
1208  if(FaceType::HasPolyInfo())
1209  {
1210  m.face[pos].Dealloc();
1211  m.face[pos].Alloc(m.face[i].VN());
1212  }
1213  for(int j=0;j<m.face[i].VN();++j)
1214  m.face[pos].V(j) = m.face[i].V(j);
1215 
1216  if(HasVFAdjacency(m))
1217  for(int j=0;j<m.face[i].VN();++j)
1218  {
1219  if (m.face[i].IsVFInitialized(j)) {
1220  m.face[pos].VFp(j) = m.face[i].cVFp(j);
1221  m.face[pos].VFi(j) = m.face[i].cVFi(j);
1222  }
1223  else m.face[pos].VFClear(j);
1224  }
1225  if(HasFFAdjacency(m))
1226  for(int j=0;j<m.face[i].VN();++j)
1227  {
1228  m.face[pos].FFp(j) = m.face[i].cFFp(j);
1229  m.face[pos].FFi(j) = m.face[i].cFFi(j);
1230  }
1231  }
1232  pu.remap[i]=pos;
1233  ++pos;
1234  }
1235  }
1236  assert((int)pos==m.fn);
1237 
1238  // reorder the optional atttributes in m.face_attr to reflect the changes
1239  ReorderAttribute(m.face_attr,pu.remap,m);
1240 
1241  FacePointer fbase=&m.face[0];
1242 
1243  // Loop on the vertices to correct VF relation
1244  if(HasVFAdjacency(m))
1245  {
1246  for (VertexIterator vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
1247  if(!(*vi).IsD())
1248  {
1249  if ((*vi).IsVFInitialized() && (*vi).VFp()!=0 )
1250  {
1251  size_t oldIndex = (*vi).cVFp() - fbase;
1252  assert(fbase <= (*vi).cVFp() && oldIndex < pu.remap.size());
1253  (*vi).VFp() = fbase+pu.remap[oldIndex];
1254  }
1255  }
1256  }
1257 
1258  // Loop on the faces to correct VF and FF relations
1259  pu.oldBase = &m.face[0];
1260  pu.oldEnd = &m.face.back()+1;
1261  for(size_t i=m.fn;i<m.face.size();++i)
1262  m.face[i].Dealloc();
1263  m.face.resize(m.fn);
1264  pu.newBase = (m.face.empty())?0:&m.face[0];
1265  pu.newEnd = (m.face.empty())?0:&m.face.back()+1;
1266 
1267 
1268  // resize the optional atttributes in m.face_attr to reflect the changes
1269  ResizeAttribute(m.face_attr,m.fn,m);
1270 
1271  // now we update the various (not null) face pointers (inside VF and FF relations)
1272  for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
1273  if(!(*fi).IsD())
1274  {
1275  if(HasVFAdjacency(m))
1276  for(int i=0;i<(*fi).VN();++i)
1277  if ((*fi).IsVFInitialized(i) && (*fi).VFp(i)!=0 )
1278  {
1279  size_t oldIndex = (*fi).VFp(i) - fbase;
1280  assert(fbase <= (*fi).VFp(i) && oldIndex < pu.remap.size());
1281  (*fi).VFp(i) = fbase+pu.remap[oldIndex];
1282  }
1283  if(HasFFAdjacency(m))
1284  for(int i=0;i<(*fi).VN();++i)
1285  if ((*fi).cFFp(i)!=0)
1286  {
1287  size_t oldIndex = (*fi).FFp(i) - fbase;
1288  assert(fbase <= (*fi).FFp(i) && oldIndex < pu.remap.size());
1289  (*fi).FFp(i) = fbase+pu.remap[oldIndex];
1290  }
1291  }
1292 
1293 
1294 
1295  }
1296 
1298  static void CompactFaceVector( MeshType &m ) {
1300  CompactFaceVector(m,pu);
1301  }
1302 
1311  static void CompactTetraVector(MeshType & m, PointerUpdater<TetraPointer> & pu)
1312  {
1313  //nothing to do
1314  if (size_t(m.tn) == m.tetra.size())
1315  return;
1316 
1317  //init the remap
1318  pu.remap.resize(m.tetra.size(), std::numeric_limits<size_t>::max());
1319 
1320  //cycle over all the tetras, pos is the last not D() position, I is the index
1321  //when pos != i and !tetra[i].IsD() => we need to compact and update adj
1322  size_t pos = 0;
1323  for (size_t i = 0; i < m.tetra.size(); ++i)
1324  {
1325  if (!m.tetra[i].IsD())
1326  {
1327  if (pos != i)
1328  {
1329  //import data
1330  m.tetra[pos].ImportData(m.tetra[i]);
1331  //import vertex refs
1332  for (int j = 0; j < 4; ++j)
1333  m.tetra[pos].V(j) = m.tetra[i].cV(j);
1334  //import VT adj
1335  if (HasVTAdjacency(m))
1336  for (int j = 0; j < 4; ++j)
1337  {
1338  if (m.tetra[i].IsVTInitialized(j))
1339  {
1340  m.tetra[pos].VTp(j) = m.tetra[i].VTp(j);
1341  m.tetra[pos].VTi(j) = m.tetra[i].VTi(j);
1342  }
1343  else
1344  m.tetra[pos].VTClear(j);
1345  }
1346  //import TT adj
1347  if (HasTTAdjacency(m))
1348  for (int j = 0; j < 4; ++j)
1349  {
1350  m.tetra[pos].TTp(j) = m.tetra[i].cTTp(j);
1351  m.tetra[pos].TTi(j) = m.tetra[i].cTTi(j);
1352  }
1353  }
1354  //update remapping and advance pos
1355  pu.remap[i] = pos;
1356  ++pos;
1357  }
1358  }
1359 
1360  assert(size_t(m.tn) == pos);
1361  //reorder the optional attributes in m.tetra_attr
1362  ReorderAttribute(m.tetra_attr, pu.remap, m);
1363  // resize the optional atttributes in m.tetra_attr to reflect the changes
1364  ResizeAttribute(m.tetra_attr, m.tn, m);
1365 
1366  // Loop on the tetras to correct VT and TT relations
1367  pu.oldBase = &m.tetra[0];
1368  pu.oldEnd = &m.tetra.back() + 1;
1369 
1370  m.tetra.resize(m.tn);
1371  pu.newBase = (m.tetra.empty()) ? 0 : &m.tetra[0];
1372  pu.newEnd = (m.tetra.empty()) ? 0 : &m.tetra.back() + 1;
1373 
1374  TetraPointer tbase = &m.tetra[0];
1375 
1376  //Loop on the vertices to correct VT relation (since we moved things around)
1377  if (HasVTAdjacency(m))
1378  {
1379  for (VertexIterator vi = m.vert.begin(); vi != m.vert.end(); ++vi)
1380  if (!(*vi).IsD())
1381  {
1382  if ((*vi).IsVTInitialized() && (*vi).VTp() != 0)
1383  {
1384  size_t oldIndex = (*vi).cVTp() - tbase;
1385  assert(tbase <= (*vi).cVTp() && oldIndex < pu.remap.size());
1386  (*vi).VTp() = tbase + pu.remap[oldIndex];
1387  }
1388  }
1389  }
1390 
1391  // Loop on the tetras to correct the VT and TT relations
1392  for (TetraIterator ti = m.tetra.begin(); ti != m.tetra.end(); ++ti)
1393  if (!(*ti).IsD())
1394  {
1395  //VT
1396  if (HasVTAdjacency(m))
1397  for (int i = 0; i < 4; ++i)
1398  if ((*ti).IsVTInitialized(i) && (*ti).VTp(i) != 0)
1399  {
1400  size_t oldIndex = (*ti).VTp(i) - tbase;
1401  assert(tbase <= (*ti).VTp(i) && oldIndex < pu.remap.size());
1402  (*ti).VTp(i) = tbase + pu.remap[oldIndex];
1403  }
1404  //TT
1405  if (HasTTAdjacency(m))
1406  for (int i = 0; i < 4; ++i)
1407  if ((*ti).cTTp(i) != 0)
1408  {
1409  size_t oldIndex = (*ti).TTp(i) - tbase;
1410  assert(tbase <= (*ti).TTp(i) && oldIndex < pu.remap.size());
1411  (*ti).TTp(i) = tbase + pu.remap[oldIndex];
1412  }
1413  }
1414  }
1415 
1417  static void CompactTetraVector(MeshType &m)
1418  {
1420  CompactTetraVector(m, pu);
1421  }
1422 
1423 
1424 public:
1425 
1428  template <class ATTR_TYPE>
1429  static
1430  bool IsValidHandle( MeshType & m, const typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE> & a){
1431  if(a._handle == NULL) return false;
1432  for(AttrIterator i = m.vert_attr.begin(); i!=m.vert_attr.end();++i)
1433  if ( (*i).n_attr == a.n_attr ) return true;
1434  return false;
1435  }
1436 
1441  template <class ATTR_TYPE>
1442  static
1443  typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>
1444  AddPerVertexAttribute( MeshType & m, std::string name){
1445  PAIte i;
1446  PointerToAttribute h;
1447  h._name = name;
1448  if(!name.empty()){
1449  i = m.vert_attr.find(h);
1450  assert(i ==m.vert_attr.end() );// an attribute with this name exists
1451  }
1452 
1453  h._sizeof = sizeof(ATTR_TYPE);
1454  h._padding = 0;
1455  h._handle = new SimpleTempData<VertContainer,ATTR_TYPE>(m.vert);
1456  h._type = typeid(ATTR_TYPE);
1457  m.attrn++;
1458  h.n_attr = m.attrn;
1459  std::pair < AttrIterator , bool> res = m.vert_attr.insert(h);
1460  return typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>(res.first->_handle,res.first->n_attr );
1461  }
1462 
1463  template <class ATTR_TYPE>
1464  static typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>
1465  AddPerVertexAttribute( MeshType & m){
1466  return AddPerVertexAttribute<ATTR_TYPE>(m,std::string(""));
1467  }
1468 
1473  template <class ATTR_TYPE>
1474  static
1475  typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>
1476  GetPerVertexAttribute( MeshType & m, std::string name = std::string("")){
1477  typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE> h;
1478  if(!name.empty()){
1479  h = FindPerVertexAttribute<ATTR_TYPE>(m,name);
1480  if(IsValidHandle(m,h))
1481  return h;
1482  }
1483  return AddPerVertexAttribute<ATTR_TYPE>(m,name);
1484  }
1485 
1489  template <class ATTR_TYPE>
1490  static typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>
1491  FindPerVertexAttribute( MeshType & m, const std::string & name)
1492  {
1493  assert(!name.empty());
1494  PointerToAttribute h1; h1._name = name;
1495  typename std::set<PointerToAttribute > :: iterator i;
1496 
1497  i =m.vert_attr.find(h1);
1498  if(i!=m.vert_attr.end())
1499  if((*i)._sizeof == sizeof(ATTR_TYPE) ){
1500  if( (*i)._padding != 0 ){
1501  PointerToAttribute attr = (*i); // copy the PointerToAttribute
1502  m.vert_attr.erase(i); // remove it from the set
1503  FixPaddedPerVertexAttribute<ATTR_TYPE>(m,attr);
1504  std::pair<AttrIterator,bool> new_i = m.vert_attr.insert(attr); // insert the modified PointerToAttribute
1505  assert(new_i.second);
1506  i = new_i.first;
1507  }
1508  return typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr);
1509  }
1510  return typename MeshType:: template PerVertexAttributeHandle<ATTR_TYPE>(NULL,0);
1511  }
1512 
1516  template <class ATTR_TYPE>
1517  static void GetAllPerVertexAttribute(MeshType & m, std::vector<std::string> &all){
1518  all.clear();
1519  typename std::set<PointerToAttribute > ::const_iterator i;
1520  for(i = m.vert_attr.begin(); i != m.vert_attr.end(); ++i )
1521  if(!(*i)._name.empty())
1522  {
1523  typename MeshType:: template PerVertexAttributeHandle<ATTR_TYPE> hh;
1524  hh = Allocator<MeshType>:: template FindPerVertexAttribute <ATTR_TYPE>(m,(*i)._name);
1525  if(IsValidHandle<ATTR_TYPE>(m,hh))
1526  all.push_back((*i)._name);
1527  }
1528  }
1529 
1530  template <class ATTR_TYPE>
1531  static
1532  void
1533  ClearPerVertexAttribute( MeshType & m,typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE> & h, const ATTR_TYPE & initVal = ATTR_TYPE()){
1534  typename std::set<PointerToAttribute > ::iterator i;
1535  for( i = m.vert_attr.begin(); i != m.vert_attr.end(); ++i)
1536  if( (*i)._handle == h._handle ){
1537  for(typename MeshType::VertexIterator vi = m.vert.begin(); vi != m.vert.end(); ++vi)
1538  h[vi] = initVal;
1539  return;}
1540  assert(0);
1541  }
1542 
1545  template <class ATTR_TYPE>
1546  static
1547  void
1548  DeletePerVertexAttribute( MeshType & m,typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE> & h){
1549  typename std::set<PointerToAttribute > ::iterator i;
1550  for( i = m.vert_attr.begin(); i != m.vert_attr.end(); ++i)
1551  if( (*i)._handle == h._handle ){
1552  delete ((SimpleTempData<VertContainer,ATTR_TYPE>*)(*i)._handle);
1553  m.vert_attr.erase(i);
1554  return;}
1555  }
1556 
1557  // Generic DeleteAttribute.
1558  // It must not crash if you try to delete a non existing attribute,
1559  // because you do not have a way of asking for a handle of an attribute for which you do not know the type.
1560  static
1561  bool DeletePerVertexAttribute( MeshType & m, std::string name){
1562  AttrIterator i;
1563  PointerToAttribute h1; h1._name = name;
1564  i = m.vert_attr.find(h1);
1565  if(i==m.vert_attr.end()) return false;
1566  delete ((SimpleTempDataBase*)(*i)._handle);
1567  m.vert_attr.erase(i);
1568  return true;
1569  }
1570 
1571 
1572 
1574  template <class ATTR_TYPE>
1575  static
1576  bool IsValidHandle( MeshType & m, const typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE> & a){
1577  if(a._handle == NULL) return false;
1578  for(AttrIterator i = m.edge_attr.begin(); i!=m.edge_attr.end();++i)
1579  if ( (*i).n_attr == a.n_attr ) return true;
1580  return false;
1581  }
1582 
1583  template <class ATTR_TYPE>
1584  static
1585  typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE>
1586  AddPerEdgeAttribute( MeshType & m, std::string name){
1587  PAIte i;
1588  PointerToAttribute h;
1589  h._name = name;
1590  if(!name.empty()){
1591  i = m.edge_attr.find(h);
1592  assert(i ==m.edge_attr.end() );// an attribute with this name exists
1593  }
1594  h._sizeof = sizeof(ATTR_TYPE);
1595  h._padding = 0;
1596  // h._typename = typeid(ATTR_TYPE).name();
1597  h._handle = new SimpleTempData<EdgeContainer,ATTR_TYPE>(m.edge);
1598  h._type = typeid(ATTR_TYPE);
1599  m.attrn++;
1600  h.n_attr = m.attrn;
1601  std::pair < AttrIterator , bool> res = m.edge_attr.insert(h);
1602  return typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE>(res.first->_handle,res.first->n_attr);
1603  }
1604 
1605  template <class ATTR_TYPE>
1606  static
1607  typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE>
1608  AddPerEdgeAttribute( MeshType & m){
1609  return AddPerEdgeAttribute<ATTR_TYPE>(m,std::string(""));
1610  }
1611 
1616  template <class ATTR_TYPE>
1617  static
1618  typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE>
1619  GetPerEdgeAttribute( MeshType & m, std::string name = std::string("")){
1620  typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE> h;
1621  if(!name.empty()){
1622  h = FindPerEdgeAttribute<ATTR_TYPE>(m,name);
1623  if(IsValidHandle(m,h))
1624  return h;
1625  }
1626  return AddPerEdgeAttribute<ATTR_TYPE>(m,name);
1627  }
1628 
1629 
1630  template <class ATTR_TYPE>
1631  static
1632  typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE>
1633  FindPerEdgeAttribute( MeshType & m, const std::string & name){
1634  assert(!name.empty());
1635  PointerToAttribute h1; h1._name = name;
1636  typename std::set<PointerToAttribute > ::const_iterator i;
1637 
1638  i =m.edge_attr.find(h1);
1639  if(i!=m.edge_attr.end())
1640  if((*i)._sizeof == sizeof(ATTR_TYPE) ){
1641  if( (*i)._padding != 0 ){
1642  PointerToAttribute attr = (*i); // copy the PointerToAttribute
1643  m.edge_attr.erase(i); // remove it from the set
1644  FixPaddedPerEdgeAttribute<ATTR_TYPE>(m,attr);
1645  std::pair<AttrIterator,bool> new_i = m.edge_attr.insert(attr); // insert the modified PointerToAttribute
1646  assert(new_i.second);
1647  i = new_i.first;
1648  }
1649  return typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr);
1650  }
1651 
1652  return typename MeshType:: template PerEdgeAttributeHandle<ATTR_TYPE>(NULL,0);
1653  }
1654 
1655  template <class ATTR_TYPE>
1656  static void GetAllPerEdgeAttribute(const MeshType & m, std::vector<std::string> &all){
1657  all.clear();
1658  typename std::set<PointerToAttribute > :: const_iterator i;
1659  for(i = m.edge_attr.begin(); i != m.edge_attr.end(); ++i )
1660  if(!(*i)._name.empty())
1661  {
1662  typename MeshType:: template PerEdgeAttributeHandle<ATTR_TYPE> hh;
1663  hh = Allocator<MeshType>:: template FindPerEdgeAttribute <ATTR_TYPE>(m,(*i)._name);
1664  if(IsValidHandle<ATTR_TYPE>(m,hh))
1665  all.push_back((*i)._name);
1666  }
1667  }
1668 
1671  template <class ATTR_TYPE>
1672  static
1673  void
1674  DeletePerEdgeAttribute( MeshType & m,typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE> & h){
1675  typename std::set<PointerToAttribute > ::iterator i;
1676  for( i = m.edge_attr.begin(); i != m.edge_attr.end(); ++i)
1677  if( (*i)._handle == h._handle ){
1678  delete ((SimpleTempData<FaceContainer,ATTR_TYPE>*)(*i)._handle);
1679  m.edge_attr.erase(i);
1680  return;}
1681  }
1682 
1683  // Generic DeleteAttribute.
1684  // It must not crash if you try to delete a non existing attribute,
1685  // because you do not have a way of asking for a handle of an attribute for which you do not know the type.
1686  static
1687  bool DeletePerEdgeAttribute( MeshType & m, std::string name){
1688  AttrIterator i;
1689  PointerToAttribute h1; h1._name = name;
1690  i = m.edge_attr.find(h1);
1691  if(i==m.edge_attr.end()) return false;
1692  delete ((SimpleTempDataBase*)(*i)._handle);
1693  m.edge_attr.erase(i);
1694  return true;
1695  }
1696 
1698  template <class ATTR_TYPE>
1699  static
1700  bool IsValidHandle( MeshType & m, const typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE> & a){
1701  if(a._handle == NULL) return false;
1702  for(AttrIterator i = m.face_attr.begin(); i!=m.face_attr.end();++i)
1703  if ( (*i).n_attr == a.n_attr ) return true;
1704  return false;
1705  }
1706 
1707  template <class ATTR_TYPE>
1708  static
1709  typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>
1710  AddPerFaceAttribute( MeshType & m, std::string name){
1711  PAIte i;
1712  PointerToAttribute h;
1713  h._name = name;
1714  if(!name.empty()){
1715  i = m.face_attr.find(h);
1716  assert(i ==m.face_attr.end() );// an attribute with this name exists
1717  }
1718 
1719  h._sizeof = sizeof(ATTR_TYPE);
1720  h._padding = 0;
1721  h._handle = new SimpleTempData<FaceContainer,ATTR_TYPE>(m.face);
1722  h._type = typeid(ATTR_TYPE);
1723  m.attrn++;
1724  h.n_attr = m.attrn;
1725  std::pair < AttrIterator , bool> res = m.face_attr.insert(h);
1726  return typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>(res.first->_handle,res.first->n_attr);
1727  }
1728 
1729  template <class ATTR_TYPE>
1730  static
1731  typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>
1732  AddPerFaceAttribute( MeshType & m){
1733  return AddPerFaceAttribute<ATTR_TYPE>(m,std::string(""));
1734  }
1735 
1740  template <class ATTR_TYPE>
1741  static
1742  typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>
1743  GetPerFaceAttribute( MeshType & m, std::string name = std::string("")){
1744  typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE> h;
1745  if(!name.empty()){
1746  h = FindPerFaceAttribute<ATTR_TYPE>(m,name);
1747  if(IsValidHandle(m,h))
1748  return h;
1749  }
1750  return AddPerFaceAttribute<ATTR_TYPE>(m,name);
1751  }
1752 
1753  template <class ATTR_TYPE>
1754  static
1755  typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>
1756  FindPerFaceAttribute( MeshType & m, const std::string & name){
1757  assert(!name.empty());
1758  PointerToAttribute h1; h1._name = name;
1759  typename std::set<PointerToAttribute > ::iterator i;
1760 
1761  i =m.face_attr.find(h1);
1762  if(i!=m.face_attr.end())
1763  if((*i)._sizeof == sizeof(ATTR_TYPE) ){
1764  if( (*i)._padding != 0 ){
1765  PointerToAttribute attr = (*i); // copy the PointerToAttribute
1766  m.face_attr.erase(i); // remove it from the set
1767  FixPaddedPerFaceAttribute<ATTR_TYPE>(m,attr);
1768  std::pair<AttrIterator,bool> new_i = m.face_attr.insert(attr); // insert the modified PointerToAttribute
1769  assert(new_i.second);
1770  i = new_i.first;
1771  }
1772  return typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr);
1773  }
1774  return typename MeshType:: template PerFaceAttributeHandle<ATTR_TYPE>(NULL,0);
1775  }
1776 
1777  template <class ATTR_TYPE>
1778  static void GetAllPerFaceAttribute(MeshType & m, std::vector<std::string> &all){
1779  all.clear();
1780  typename std::set<PointerToAttribute > :: const_iterator i;
1781  for(i = m.face_attr.begin(); i != m.face_attr.end(); ++i )
1782  if(!(*i)._name.empty())
1783  {
1784  typename MeshType:: template PerFaceAttributeHandle<ATTR_TYPE> hh;
1785  hh = Allocator<MeshType>:: template FindPerFaceAttribute <ATTR_TYPE>(m,(*i)._name);
1786  if(IsValidHandle<ATTR_TYPE>(m,hh))
1787  all.push_back((*i)._name);
1788  }
1789  }
1790 
1793  template <class ATTR_TYPE>
1794  static void DeletePerFaceAttribute( MeshType & m,typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE> & h){
1795  typename std::set<PointerToAttribute > ::iterator i;
1796  for( i = m.face_attr.begin(); i != m.face_attr.end(); ++i)
1797  if( (*i)._handle == h._handle ){
1798  delete ((SimpleTempData<FaceContainer,ATTR_TYPE>*)(*i)._handle);
1799  m.face_attr.erase(i);
1800  return;}
1801 
1802  }
1803 
1804  // Generic DeleteAttribute.
1805  // It must not crash if you try to delete a non existing attribute,
1806  // because you do not have a way of asking for a handle of an attribute for which you do not know the type.
1807  static bool DeletePerFaceAttribute( MeshType & m, std::string name){
1808  AttrIterator i;
1809  PointerToAttribute h1; h1._name = name;
1810  i = m.face_attr.find(h1);
1811  if(i==m.face_attr.end()) return false;
1812  delete ((SimpleTempDataBase*)(*i)._handle);
1813  m.face_attr.erase(i);
1814  return true;
1815  }
1816 
1818  template <class ATTR_TYPE>
1819  static bool IsValidHandle(MeshType & m, const typename MeshType::template PerTetraAttributeHandle<ATTR_TYPE> & a)
1820  {
1821  if (a._handle == NULL)
1822  return false;
1823  for (AttrIterator i = m.tetra_attr.begin(); i != m.tetra_attr.end(); ++i)
1824  if ((*i).n_attr == a.n_attr)
1825  return true;
1826  return false;
1827  }
1828 
1829  template <class ATTR_TYPE>
1830  static typename MeshType::template PerTetraAttributeHandle<ATTR_TYPE> AddPerTetraAttribute(MeshType & m, std::string name)
1831  {
1832  PAIte i;
1833  PointerToAttribute h;
1834  h._name = name;
1835  if (!name.empty())
1836  {
1837  i = m.tetra_attr.find(h);
1838  assert(i == m.tetra_attr.end());
1839  }
1840 
1841  h._sizeof = sizeof(ATTR_TYPE);
1842  h._padding = 0;
1843  h._handle = new SimpleTempData<TetraContainer, ATTR_TYPE>(m.tetra);
1844  h._type = typeid(ATTR_TYPE);
1845  m.attrn++;
1846  h.n_attr = m.attrn;
1847  std::pair<AttrIterator, bool> res = m.tetra_attr.insert(h);
1848  return typename MeshType::template PerTetraAttributeHandle<ATTR_TYPE>(res.first->_handle, res.first->n_attr);
1849  }
1850 
1851  template <class ATTR_TYPE>
1852  static typename MeshType::template PerTetraAttributeHandle<ATTR_TYPE> AddPerTetraAttribute(MeshType &m)
1853  {
1854  return AddPerTetraAttribute<ATTR_TYPE>(m, std::string(""));
1855  }
1856 
1861  template <class ATTR_TYPE>
1862  static typename MeshType::template PerTetraAttributeHandle<ATTR_TYPE> GetPerTetraAttribute(MeshType &m, std::string name = std::string(""))
1863  {
1864  typename MeshType::template PerTetraAttributeHandle<ATTR_TYPE> h;
1865  if (!name.empty())
1866  {
1867  h = FindPerTetraAttribute<ATTR_TYPE>(m, name);
1868  if (IsValidHandle(m, h))
1869  return h;
1870  }
1871  return AddPerTetraAttribute<ATTR_TYPE>(m, name);
1872  }
1873 
1874  template <class ATTR_TYPE>
1875  static typename MeshType::template PerTetraAttributeHandle<ATTR_TYPE> FindPerTetraAttribute(MeshType &m, const std::string &name)
1876  {
1877  assert(!name.empty());
1878  PointerToAttribute h1;
1879  h1._name = name;
1880  typename std::set<PointerToAttribute>::iterator i;
1881 
1882  i = m.tetra_attr.find(h1);
1883  if (i != m.tetra_attr.end())
1884  if ((*i)._sizeof == sizeof(ATTR_TYPE))
1885  {
1886  if ((*i)._padding != 0)
1887  {
1888  PointerToAttribute attr = (*i); // copy the PointerToAttribute
1889  m.tetra_attr.erase(i); // remove it from the set
1890  FixPaddedPerTetraAttribute<ATTR_TYPE>(m, attr);
1891  std::pair<AttrIterator, bool> new_i = m.tetra_attr.insert(attr); // insert the modified PointerToAttribute
1892  assert(new_i.second);
1893  i = new_i.first;
1894  }
1895  return typename MeshType::template PerTetraAttributeHandle<ATTR_TYPE>((*i)._handle, (*i).n_attr);
1896  }
1897  return typename MeshType::template PerTetraAttributeHandle<ATTR_TYPE>(NULL, 0);
1898  }
1899 
1900  template <class ATTR_TYPE>
1901  static void GetAllPerTetraAttribute(MeshType &m, std::vector<std::string> &all)
1902  {
1903  all.clear();
1904  typename std::set<PointerToAttribute>::const_iterator i;
1905  for (i = m.tetra_attr.begin(); i != m.tetra_attr.end(); ++i)
1906  if (!(*i)._name.empty())
1907  {
1908  typename MeshType::template PerTetraAttributeHandle<ATTR_TYPE> hh;
1909  hh = Allocator<MeshType>::template FindPerTetraAttribute<ATTR_TYPE>(m, (*i)._name);
1910  if (IsValidHandle<ATTR_TYPE>(m, hh))
1911  all.push_back((*i)._name);
1912  }
1913  }
1914 
1917  template <class ATTR_TYPE>
1918  static void DeletePerTetraAttribute(MeshType &m, typename MeshType::template PerTetraAttributeHandle<ATTR_TYPE> &h)
1919  {
1920  typename std::set<PointerToAttribute>::iterator i;
1921  for (i = m.tetra_attr.begin(); i != m.tetra_attr.end(); ++i)
1922  if ((*i)._handle == h._handle)
1923  {
1924  delete ((SimpleTempData<TetraContainer, ATTR_TYPE> *)(*i)._handle);
1925  m.tetra_attr.erase(i);
1926  return;
1927  }
1928  }
1929 
1930  // Generic DeleteAttribute.
1931  // It must not crash if you try to delete a non existing attribute,
1932  // because you do not have a way of asking for a handle of an attribute for which you do not know the type.
1933  static bool DeletePerTetraAttribute(MeshType &m, std::string name)
1934  {
1935  AttrIterator i;
1936  PointerToAttribute h1;
1937  h1._name = name;
1938  i = m.tetra_attr.find(h1);
1939  if (i == m.tetra_attr.end())
1940  return false;
1941  delete ((SimpleTempDataBase *)(*i)._handle);
1942  m.tetra_attr.erase(i);
1943  return true;
1944  }
1945 
1946 
1948  template <class ATTR_TYPE>
1949  static
1950  bool IsValidHandle( MeshType & m, const typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE> & a){
1951  if(a._handle == NULL) return false;
1952  for(AttrIterator i = m.mesh_attr.begin(); i!=m.mesh_attr.end();++i)
1953  if ( (*i).n_attr == a.n_attr ) return true;
1954  return false;
1955  }
1956 
1957  template <class ATTR_TYPE>
1958  static
1959  typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>
1960  AddPerMeshAttribute( MeshType & m, std::string name){
1961  PAIte i;
1962  PointerToAttribute h;
1963  h._name = name;
1964  if(!name.empty()){
1965  i = m.mesh_attr.find(h);
1966  assert(i ==m.mesh_attr.end() );// an attribute with this name exists
1967  }
1968  h._sizeof = sizeof(ATTR_TYPE);
1969  h._padding = 0;
1970  h._handle = new Attribute<ATTR_TYPE>();
1971  h._type = typeid(ATTR_TYPE);
1972  m.attrn++;
1973  h.n_attr = m.attrn;
1974  std::pair < AttrIterator , bool> res = m.mesh_attr.insert(h);
1975  return typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>(res.first->_handle,res.first->n_attr);
1976  }
1977 
1982  template <class ATTR_TYPE>
1983  static
1984  typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>
1985  GetPerMeshAttribute( MeshType & m, std::string name = std::string("")){
1986  typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE> h;
1987  if(!name.empty()){
1988  h = FindPerMeshAttribute<ATTR_TYPE>(m,name);
1989  if(IsValidHandle(m,h))
1990  return h;
1991  }
1992  return AddPerMeshAttribute<ATTR_TYPE>(m,name);
1993  }
1994 
1995  template <class ATTR_TYPE>
1996  static
1997  typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>
1998  FindPerMeshAttribute( MeshType & m, const std::string & name){
1999  assert(!name.empty());
2000  PointerToAttribute h1; h1._name = name;
2001  typename std::set<PointerToAttribute > ::iterator i;
2002 
2003  i =m.mesh_attr.find(h1);
2004  if(i!=m.mesh_attr.end())
2005  if((*i)._sizeof == sizeof(ATTR_TYPE) ){
2006  if( (*i)._padding != 0 ){
2007  PointerToAttribute attr = (*i); // copy the PointerToAttribute
2008  m.mesh_attr.erase(i); // remove it from the set
2009  FixPaddedPerMeshAttribute<ATTR_TYPE>(m,attr);
2010  std::pair<AttrIterator,bool> new_i = m.mesh_attr.insert(attr); // insert the modified PointerToAttribute
2011  assert(new_i.second);
2012  i = new_i.first;
2013  }
2014 
2015  return typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr);
2016  }
2017 
2018  return typename MeshType:: template PerMeshAttributeHandle<ATTR_TYPE>(NULL,0);
2019  }
2020 
2021  template <class ATTR_TYPE>
2022  static void GetAllPerMeshAttribute(const MeshType & m, std::vector<std::string> &all){
2023  typename std::set<PointerToAttribute > :: iterator i;
2024  for(i = m.mesh_attr.begin(); i != m.mesh_attr.end(); ++i )
2025  if((*i)._sizeof == sizeof(ATTR_TYPE))
2026  all.push_back((*i)._name);
2027  }
2028 
2031  template <class ATTR_TYPE>
2032  static void DeletePerMeshAttribute( MeshType & m,typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE> & h){
2033  typename std::set<PointerToAttribute > ::iterator i;
2034  for( i = m.mesh_attr.begin(); i != m.mesh_attr.end(); ++i)
2035  if( (*i)._handle == h._handle ){
2036  delete (( Attribute<ATTR_TYPE> *)(*i)._handle);
2037  m.mesh_attr.erase(i);
2038  return;}
2039  }
2040 
2041  // Generic DeleteAttribute.
2042  // It must not crash if you try to delete a non existing attribute,
2043  // because you do not have a way of asking for a handle of an attribute for which you do not know the type.
2044  static bool DeletePerMeshAttribute( MeshType & m, std::string name){
2045  AttrIterator i;
2046  PointerToAttribute h1; h1._name = name;
2047  i = m.mesh_attr.find(h1);
2048  if (i==m.mesh_attr.end())
2049  return false;
2050  delete ((SimpleTempDataBase *)(*i)._handle);
2051  m.mesh_attr.erase(i);
2052  return true;
2053  }
2054 
2055  template <class ATTR_TYPE>
2056  static void FixPaddedPerVertexAttribute (MeshType & m, PointerToAttribute & pa){
2057 
2058  // create the container of the right type
2059  SimpleTempData<VertContainer,ATTR_TYPE>* _handle = new SimpleTempData<VertContainer,ATTR_TYPE>(m.vert);
2060 
2061  // copy the padded container in the new one
2062  _handle->Resize(m.vert.size());
2063  for(size_t i = 0; i < m.vert.size(); ++i){
2064  ATTR_TYPE * dest = &(*_handle)[i];
2065  char * ptr = (char*)( ((SimpleTempDataBase *)pa._handle)->DataBegin());
2066  memcpy((void*)dest ,
2067  (void*) &(ptr[i * pa._sizeof ]) ,sizeof(ATTR_TYPE));
2068  }
2069 
2070  // remove the padded container
2071  delete ((SimpleTempDataBase*) pa._handle);
2072 
2073  // update the pointer to data
2074  pa._sizeof = sizeof(ATTR_TYPE);
2075 
2076  // update the pointer to data
2077  pa._handle = _handle;
2078 
2079  // zero the padding
2080  pa._padding = 0;
2081  }
2082  template <class ATTR_TYPE>
2083  static void FixPaddedPerEdgeAttribute (MeshType & m, PointerToAttribute & pa){
2084 
2085  // create the container of the right type
2086  SimpleTempData<EdgeContainer,ATTR_TYPE>* _handle = new SimpleTempData<EdgeContainer,ATTR_TYPE>(m.edge);
2087 
2088  // copy the padded container in the new one
2089  _handle->Resize(m.edge.size());
2090  for(size_t i = 0; i < m.edge.size(); ++i){
2091  ATTR_TYPE * dest = &(*_handle)[i];
2092  char * ptr = (char*)( ((SimpleTempDataBase *)pa._handle)->DataBegin());
2093  memcpy((void*)dest ,
2094  (void*) &(ptr[i * pa._sizeof ]) ,sizeof(ATTR_TYPE));
2095  }
2096 
2097  // remove the padded container
2098  delete ((SimpleTempDataBase*) pa._handle);
2099 
2100  // update the pointer to data
2101  pa._sizeof = sizeof(ATTR_TYPE);
2102 
2103  // update the pointer to data
2104  pa._handle = _handle;
2105 
2106  // zero the padding
2107  pa._padding = 0;
2108  }
2109 
2110  template <class ATTR_TYPE>
2111  static void FixPaddedPerFaceAttribute ( MeshType & m,PointerToAttribute & pa){
2112 
2113  // create the container of the right type
2114  SimpleTempData<FaceContainer,ATTR_TYPE>* _handle = new SimpleTempData<FaceContainer,ATTR_TYPE>(m.face);
2115 
2116  // copy the padded container in the new one
2117  _handle->Resize(m.face.size());
2118  for(size_t i = 0; i < m.face.size(); ++i){
2119  ATTR_TYPE * dest = &(*_handle)[i];
2120  char * ptr = (char*)( ((SimpleTempDataBase *)pa._handle)->DataBegin());
2121  memcpy((void*)dest ,
2122  (void*) &(ptr[i * pa._sizeof ]) ,sizeof(ATTR_TYPE));
2123  }
2124 
2125  // remove the padded container
2126  delete ((SimpleTempDataBase*) pa._handle);
2127 
2128  // update the pointer to data
2129  pa._sizeof = sizeof(ATTR_TYPE);
2130 
2131  // update the pointer to data
2132  pa._handle = _handle;
2133 
2134  // zero the padding
2135  pa._padding = 0;
2136  }
2137 
2138  template <class ATTR_TYPE>
2139  static void FixPaddedPerTetraAttribute(MeshType &m, PointerToAttribute &pa)
2140  {
2141 
2142  // create the container of the right type
2143  SimpleTempData<TetraContainer, ATTR_TYPE> *_handle = new SimpleTempData<TetraContainer, ATTR_TYPE>(m.tetra);
2144 
2145  // copy the padded container in the new one
2146  _handle->Resize(m.tetra.size());
2147  for (size_t i = 0; i < m.tetra.size(); ++i)
2148  {
2149  ATTR_TYPE *dest = &(*_handle)[i];
2150  char *ptr = (char *)(((SimpleTempDataBase *)pa._handle)->DataBegin());
2151  memcpy((void *)dest,
2152  (void *)&(ptr[i * pa._sizeof]), sizeof(ATTR_TYPE));
2153  }
2154 
2155  // remove the padded container
2156  delete ((SimpleTempDataBase *)pa._handle);
2157 
2158  // update the pointer to data
2159  pa._sizeof = sizeof(ATTR_TYPE);
2160 
2161  // update the pointer to data
2162  pa._handle = _handle;
2163 
2164  // zero the padding
2165  pa._padding = 0;
2166  }
2167 
2168  template <class ATTR_TYPE>
2169  static void FixPaddedPerMeshAttribute ( MeshType & /* m */,PointerToAttribute & pa){
2170 
2171  // create the container of the right type
2172  Attribute<ATTR_TYPE> * _handle = new Attribute<ATTR_TYPE>();
2173 
2174  // copy the padded container in the new one
2175  char * ptr = (char*)( ((Attribute<ATTR_TYPE> *)pa._handle)->DataBegin());
2176  memcpy((void*)_handle->attribute ,(void*) &(ptr[0]) ,sizeof(ATTR_TYPE));
2177 
2178  // remove the padded container
2179  delete ( (Attribute<ATTR_TYPE> *) pa._handle);
2180 
2181  // update the pointer to data
2182  pa._sizeof = sizeof(ATTR_TYPE);
2183 
2184  // update the pointer to data
2185  pa._handle = _handle;
2186 
2187  // zero the padding
2188  pa._padding = 0;
2189  }
2190 
2191 }; // end Allocator class
2192 
2193  // end doxygen group trimesh
2195 } // end namespace tri
2196 } // end namespace vcg
2197 
2198 #endif