00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #ifndef __VCG_TETRA_ALLOCATE
00050 #define __VCG_TETRA_ALLOCATE
00051 #include <vector>
00052 using namespace std;
00053 namespace vcg {
00054 namespace tetra {
00057
00063 template < class TM_TYPE >
00064 class Allocator
00065 {
00066
00067 public:
00068
00070 typedef TM_TYPE TetraMeshType;
00071
00072
00074 typedef typename TM_TYPE::VertexType VertexType;
00075
00077 typedef typename TM_TYPE::TetraType TetraType;
00078
00080 typedef typename TM_TYPE::VertexIterator VertexIterator;
00081
00083 typedef typename TM_TYPE::TetraIterator TetraIterator;
00084
00086 typedef typename TM_TYPE::const_VertexIterator const_VertexIterator;
00087
00089 typedef typename TM_TYPE::const_TetraIterator const_TetraIterator;
00090
00091
00092 public:
00093
00102 static VertexIterator AddVertices(TetraMeshType &m,int n, vector<VertexType **> &local_var)
00103 {
00104 VertexIterator oldbegin, newbegin;
00105 oldbegin = m.vert.begin();
00106 VertexIterator last=m.vert.end();
00107 if(m.vert.empty()) last=(VertexIterator)0;
00108 else --last;
00109 unsigned int siz=0;
00110 #ifdef __STL_CONFIG_H
00111 if(last!=(VertexIterator)0) distance(m.vert.begin(),last,siz);
00112 #else
00113 if(last!=(VertexIterator)0) siz=distance(m.vert.begin(),last);
00114 #endif
00115 for(unsigned int i=0; i<n; ++i)
00116 {
00117 m.vert.push_back(VertexType());
00118 m.vert.back().ClearFlags();
00119 }
00120 m.vn+=n;
00121 newbegin = m.vert.begin();
00122 if(newbegin != oldbegin)
00123 {
00124 TetraIterator f;
00125 for (f=m.tetra.begin(); f!=m.tetra.end(); ++f)
00126 if(!(*f).IsD())
00127 for(unsigned int k=0; k<4; ++k)
00128 (*f).V(k)= (*f).V(k)-&*oldbegin+&*newbegin;
00129 for(unsigned int j=0; j<local_var.size(); ++j)
00130 if((*local_var[j]) !=0 ) *local_var[j] = *local_var[j]-&*oldbegin+&*newbegin;
00131
00132
00133
00134 if(last!=(VertexIterator)0)
00135 {
00136 last = m.vert.begin();
00137 advance(last,siz+1);
00138 }
00139 else last=m.vert.begin();
00140 }
00141 else
00142 {
00143
00144 if(last==(VertexIterator)0) last = m.vert.begin();
00145 else advance(last,1);
00146 }
00147 return last;
00148 }
00149
00153 static VertexIterator AddVertices(TetraMeshType &m,int n)
00154 {
00155 vector<VertexType **> empty_var;
00156 return AddVertices(m,n,empty_var);
00157 }
00158
00159 struct InsertedVT{
00160 InsertedVT(VertexType *_v,
00161 TetraType *_t,
00162 int _z):v(_v),t(_t),z(_z){}
00163
00164 VertexType *v;
00165 TetraType *t;
00166 int z;
00167
00168 const bool operator <(const InsertedVT & o){
00169 return (v<o.v);
00170 }
00171 const bool operator ==(const InsertedVT & o){
00172 return (v==o.v);
00173 }
00174 const bool operator !=(const InsertedVT & o){
00175 return (v!=o.v);
00176 }
00177 };
00178
00179
00183 static TetraIterator AddTetra(TetraMeshType &m,int n)
00184 {
00185 unsigned int sz = m.tetra.size();
00186 m.tetra.resize(sz+n);
00187
00188 TetraIterator ti =m.tetra.begin();
00189 advance(ti,sz);
00190
00191 m.tn+=n;
00192 return ti;
00193 }
00194
00200 template <class STL_CONT >
00201 static void SubSetT(STL_CONT & subSet, TetraMeshType & m)
00202 {
00203 vector< InsertedVT > newVertices;
00204 typename STL_CONT :: iterator pfi;
00205 newVertices.clear();
00206
00207 for(pfi = subSet.begin(); pfi != subSet.end(); ++pfi)
00208 m.tetra.push_back((*pfi));
00209
00210 TetraIterator fi;
00211 for(fi = m.tetra.begin(); fi != m.tetra.end(); ++fi)
00212 {
00213 newVertices.push_back(InsertedVT( (*fi).V(0),&(*fi),0));
00214 newVertices.push_back(InsertedVT( (*fi).V(1),&(*fi),1));
00215 newVertices.push_back(InsertedVT( (*fi).V(2),&(*fi),2));
00216 newVertices.push_back(InsertedVT( (*fi).V(3),&(*fi),3));
00217 }
00218
00219 sort(newVertices.begin(),newVertices.end());
00220
00221 typename std::vector< InsertedVT >::iterator curr,next;
00222 int pos = 0;
00223 curr = next = newVertices.begin();
00224 while( next != newVertices.end())
00225 {
00226 if((*curr)!=(*next))
00227 pos++;
00228 (*next).t->V( (*next).z) = (VertexType *)pos;
00229 curr = next;
00230 next++;
00231 }
00232
00233 typename std::vector<InsertedVT >::iterator newE = unique(newVertices.begin(),newVertices.end());
00234
00235 for(curr = newVertices.begin();curr!= newE;++curr)
00236 m.vert.push_back(*((*curr).v));
00237
00238 for(fi = m.tetra.begin(); fi != m.tetra.end(); ++fi)
00239 {
00240 (*fi).V(0) = &(m.vert[(int)(*fi).V(0)]);
00241 (*fi).V(1) = &(m.vert[(int)(*fi).V(1)]);
00242 (*fi).V(2) = &(m.vert[(int)(*fi).V(2)]);
00243 (*fi).V(3) = &(m.vert[(int)(*fi).V(3)]);
00244 }
00245 m.vn = m.vert.size();
00246 m.tn = m.tetra.size();
00247 }
00248
00249 };
00250
00251
00253 }
00254 }
00255
00256
00257 #endif