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 #ifndef __VCGLIB_SIMPLE__
00049 #define __VCGLIB_SIMPLE__
00050
00051 #include <limits>
00052 #include <vector>
00053
00054 namespace vcg {
00055
00056 template <class STL_CONT>
00057 class SimpleTempDataBase{
00058 public:
00059 virtual ~SimpleTempDataBase() {};
00060 SimpleTempDataBase() {};
00061 virtual void Resize(const int & sz) = 0;
00062 virtual void Reorder(std::vector<size_t> & newVertIndex)=0;
00063 virtual int SizeOf() const = 0;
00064 virtual void * DataBegin() = 0;
00065
00066
00067 };
00068
00069 template <class TYPE>
00070 struct VectorNBW: public std::vector<TYPE> {};
00071
00072 template <>
00073 class VectorNBW<bool>{
00074 public:
00075 VectorNBW():data(0),datasize(0),datareserve(0){}
00076 bool * data ;
00077
00078 void reserve (const int & sz) {
00079 if(sz<=datareserve) return;
00080 bool * newdataLoc = new bool[ sz ];
00081 if(datasize!=0) memcpy(newdataLoc,data,sizeof(datasize));
00082 std::swap(data,newdataLoc);
00083 if(newdataLoc != 0) delete newdataLoc;
00084 datareserve = sz;
00085 }
00086
00087 void resize (const int & sz) {
00088 int oldDatasize = datasize;
00089 if(sz <= oldDatasize) return;
00090 if(sz > datareserve)
00091 reserve(sz);
00092 datasize = sz;
00093 memset(&data[oldDatasize],0,datasize-oldDatasize);
00094 }
00095 void push_back(const bool & v) { resize(datasize+1); data[datasize] = v;}
00096
00097 void clear(){ datasize = 0;}
00098
00099 unsigned int size() const { return datasize;}
00100
00101 bool empty() const {return datasize==0;}
00102
00103 bool * begin() const {return data;}
00104
00105 bool & operator [](const int & i){return data[i];}
00106
00107 private:
00108 int datasize;
00109 int datareserve;
00110 };
00111
00112 template <class STL_CONT, class ATTR_TYPE>
00113 class SimpleTempData:public SimpleTempDataBase<STL_CONT>{
00114
00115 public:
00116 typedef SimpleTempData<STL_CONT,ATTR_TYPE> SimpTempDataType;
00117 typedef ATTR_TYPE AttrType;
00118
00119 STL_CONT& c;
00120 VectorNBW<ATTR_TYPE> data;
00121 int padding;
00122
00123 SimpleTempData(STL_CONT &_c):c(_c),padding(0){data.reserve(c.capacity());data.resize(c.size());};
00124 SimpleTempData(STL_CONT &_c, const ATTR_TYPE &val):c(_c){
00125 data.reserve(c.capacity());data.resize(c.size());
00126 Init(val);
00127 };
00128
00129 ~SimpleTempData(){data.clear();}
00130
00131 void Init(const ATTR_TYPE &val)
00132 {
00133 std::fill(data.begin(),data.end(),val);
00134 }
00135
00136 ATTR_TYPE & operator[](const typename STL_CONT::value_type & v){return data[&v-&*c.begin()];}
00137 ATTR_TYPE & operator[](const typename STL_CONT::value_type * v){return data[v-&*c.begin()];}
00138 ATTR_TYPE & operator[](const typename STL_CONT::iterator & cont){return data[&(*cont)-&*c.begin()];}
00139 ATTR_TYPE & operator[](const int & i){return data[i];}
00140
00141
00142 bool UpdateSize(){
00143 if(data.size() != c.size())
00144 {
00145 data.resize(c.size());
00146 return false;
00147 }
00148 return true;
00149 }
00150
00151 void Resize(const int & sz){
00152 data.resize(sz);
00153 }
00154
00155 void Reorder(std::vector<size_t> & newVertIndex){
00156 for(unsigned int i = 0 ; i < data.size(); ++i){
00157 if( newVertIndex[i] != std::numeric_limits<size_t>::max())
00158 data[newVertIndex[i]] = data[i];
00159 }
00160 }
00161
00162 int SizeOf() const {return sizeof(ATTR_TYPE);}
00163 void * DataBegin() {return data.empty()?NULL:&(*data.begin());}
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 };
00176
00177 class AttributeBase{
00178 public:
00179 virtual ~AttributeBase() {};
00180 AttributeBase() {};
00181 virtual int SizeOf()const = 0;
00182 virtual void * DataBegin() = 0;
00183
00184 };
00185
00186 template <class ATTR_TYPE>
00187 class Attribute: public AttributeBase {
00188 public:
00189 typedef ATTR_TYPE AttrType;
00190 AttrType * attribute;
00191 Attribute(){attribute = new ATTR_TYPE();}
00192 ~Attribute(){delete attribute;}
00193 int SizeOf()const {return sizeof(ATTR_TYPE);}
00194 void * DataBegin(){return attribute;}
00195 };
00196
00197 };
00198
00199 #endif