00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __GB_AttributeData_h__
00021 #define __GB_AttributeData_h__
00022
00023 #include "GB_API.h"
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <SYS/SYS_Types.h>
00027 #include <SYS/SYS_Math.h>
00028 #include "GB_AttributeRef.h"
00029 #include "GB_AttributeBuffer.h"
00030
00031 class GB_Attribute;
00032 class GB_FloatOffsets;
00033 class GB_VectorOffsets;
00034
00035 #include <UT/UT_VectorTypes.h>
00036
00037 class GB_API GB_AttributeData
00038 {
00039 public:
00040 GB_AttributeData();
00041 ~GB_AttributeData();
00042
00043
00044
00045
00046 void SYS_DEPRECATED *getData(int n) const { return (char *)myData + n; }
00047 void SYS_DEPRECATED *getData() const { return myData; }
00048
00049 template<typename T>
00050 T *castAttribData(const GB_AttributeRef &n)
00051 { return (T *)((char *)myData+n.myHandle); }
00052 template<typename T>
00053 const T *castAttribData(const GB_AttributeRef &n) const
00054 { return (T *)((const char *)myData+n.myHandle); }
00055
00056
00057 void changeSize(int s) { myData = realloc(myData, (unsigned)s); }
00058 void deleteData() { if (myData) free(myData); myData = 0; }
00059
00060 void SYS_DEPRECATED *operator[](unsigned n) { return (void *)((char *)myData + n); }
00061 void SYS_DEPRECATED *operator[](unsigned n) const
00062 {
00063 return (void *)((char *)myData + n);
00064 }
00065
00066
00067
00068
00069
00070
00071
00072 void sumData(const GB_AttributeData &from,
00073 const GB_Attribute *table, fpreal scale);
00074
00075 void sumData(const GB_AttributeData &from,
00076 const GB_Attribute *table);
00077
00078 void scaleData(const GB_Attribute *table, fpreal s);
00079
00080
00081 void setData(const GB_AttributeData &from, int s)
00082 {
00083 memcpy(myData, from.myData, (unsigned)s);
00084 }
00085
00086 void swapData(GB_AttributeData &d)
00087 {
00088 void *tmp = myData; myData = d.myData; d.myData = tmp;
00089 }
00090
00091
00092
00093
00094 bool compareData(const GB_AttributeData &cmp,
00095 const GB_Attribute *table,
00096 fpreal tol = FP32_TOLERANCE) const;
00097
00098
00099
00100 void setf(fpreal val, const GB_FloatOffsets &foffsets);
00101 void addf(fpreal val, const GB_FloatOffsets &foffsets);
00102 void subf(fpreal val, const GB_FloatOffsets &foffsets);
00103 void mulf(fpreal val, const GB_FloatOffsets &foffsets);
00104 void divf(fpreal val, const GB_FloatOffsets &foffsets);
00105
00106
00107
00108 void mulv(const UT_Matrix3 &val, const GB_VectorOffsets &foffsets,
00109 int keepLength = 1);
00110
00111 void setData(const GB_AttributeData &from,
00112 const GB_FloatOffsets &foffsets);
00113
00114 void addData(const GB_AttributeData &from,
00115 const GB_FloatOffsets &foffsets);
00116
00117
00118
00119 void addScaleData(const GB_AttributeData &from,
00120 const GB_FloatOffsets &foffsets, fpreal scale);
00121
00122
00123 void interpolateData(const GB_AttributeData &a,
00124 const GB_AttributeData &b,
00125 const GB_FloatOffsets &offsets,
00126 fpreal t);
00127
00128
00129 int operator==(const GB_AttributeData &d) const
00130 {
00131 return this->myData == d.myData;
00132 }
00133
00134 void steal(GB_AttributeData &v)
00135 {
00136 myData = v.myData;
00137 v.myData = 0;
00138 }
00139
00140 bool hasData() const { return myData != 0; }
00141
00142 GB_AttributeData &operator=(const GB_AttributeData &) { return *this; }
00143
00144 private:
00145
00146
00147
00148
00149 template <typename T> class NativeBasicAccessors
00150 {
00151 public:
00152 static const T *getPointer(const GB_AttributeData &adata,
00153 const GB_AttributeRef &h, T *buffer,
00154 int count)
00155 { return adata.castAttribData<T>(h); }
00156 static const T *getPointer(const GB_AttributeData &adata,
00157 const GB_AttributeRef &h,
00158 GB_AttributeBuffer &buffer, int count)
00159 { return adata.castAttribData<T>(h); }
00160 static T get(const GB_AttributeData &adata,
00161 const GB_AttributeRef &h, int index)
00162 { return adata.castAttribData<T>(h)[index]; }
00163 static void get(const GB_AttributeData &adata,
00164 const GB_AttributeRef &h, T &val, int index)
00165 { val = adata.castAttribData<T>(h)[index]; }
00166 static void set(GB_AttributeData &adata,
00167 const GB_AttributeRef &h, const T &val, int index)
00168 { adata.castAttribData<T>(h)[index] = val; }
00169 static void get(const GB_AttributeData &adata,
00170 const GB_AttributeRef &h, T *val, int count)
00171 { adata.read<T, T>(h, val, count); }
00172 static void set(GB_AttributeData &adata,
00173 const GB_AttributeRef &h, const T *val, int count)
00174 { adata.write<T, T>(h, val, count); }
00175 };
00176
00177
00178
00179 template <typename T, typename INTERNAL_T> class NonNativeBasicAccessors
00180 {
00181 public:
00182 static const T *getPointer(const GB_AttributeData &adata,
00183 const GB_AttributeRef &h, T *buffer,
00184 int count)
00185 {
00186 adata.read<INTERNAL_T, T>(h, buffer, count);
00187 return buffer;
00188 }
00189 static const T *getPointer(const GB_AttributeData &adata,
00190 const GB_AttributeRef &h,
00191 GB_AttributeBuffer &buffer, int count)
00192 {
00193 return getPointer(adata, h,
00194 #if defined(GCC3) && __GNUC_MINOR__ <= 3
00195 buffer.template asType<T>(count),
00196 #else
00197 buffer.asType<T>(count),
00198 #endif
00199 count);
00200 }
00201 static T get(const GB_AttributeData &adata,
00202 const GB_AttributeRef &h, int index)
00203 { return
00204 adata.castAttribData<INTERNAL_T>(h)[index]; }
00205 static void get(const GB_AttributeData &adata,
00206 const GB_AttributeRef &h, T &val, int index)
00207 { val=adata.castAttribData<INTERNAL_T>(h)[index]; }
00208 static void set(GB_AttributeData &adata,
00209 const GB_AttributeRef &h, const T &val, int index)
00210 { adata.castAttribData<INTERNAL_T>(h)[index]=val; }
00211 static void get(const GB_AttributeData &adata,
00212 const GB_AttributeRef &h, T *val, int count)
00213 { adata.read<INTERNAL_T, T>(h, val, count); }
00214 static void set(GB_AttributeData &adata,
00215 const GB_AttributeRef &h, const T *val, int count)
00216 { adata.write<T, INTERNAL_T>(h, val, count); }
00217 };
00218
00219
00220
00221 template <typename T, typename BASIC_T> class NativePODCompoundAccessors
00222 {
00223 public:
00224 static const T *getPointer(const GB_AttributeData &adata,
00225 const GB_AttributeRef &h, T *buffer,
00226 int count)
00227 { return adata.castAttribData<T>(h); }
00228 static const T *getPointer(const GB_AttributeData &adata,
00229 const GB_AttributeRef &h,
00230 GB_AttributeBuffer &buffer, int count)
00231 { return adata.castAttribData<T>(h); }
00232 static T get(const GB_AttributeData &adata,
00233 const GB_AttributeRef &h, int index)
00234 { return adata.castAttribData<T>(h)[index]; }
00235 static void get(const GB_AttributeData &adata,
00236 const GB_AttributeRef &h, T &val, int index)
00237 { val = adata.castAttribData<T>(h)[index]; }
00238 static void set(GB_AttributeData &adata,
00239 const GB_AttributeRef &h, const T &val, int index)
00240 { adata.castAttribData<T>(h)[index] = val; }
00241 static void get(const GB_AttributeData &adata,
00242 const GB_AttributeRef &h, T *val, int count)
00243 { adata.read<BASIC_T, BASIC_T>(h, (BASIC_T *)val,
00244 count * sizeof(T) / sizeof(BASIC_T)); }
00245 static void set(GB_AttributeData &adata,
00246 const GB_AttributeRef &h, const T *val, int count)
00247 { adata.write<BASIC_T, BASIC_T>(h,
00248 (const BASIC_T *)val,
00249 count * sizeof(T) / sizeof(BASIC_T)); }
00250 };
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264 template <typename T, int FAKE> class TypeInfo
00265 {
00266 };
00267
00268
00269
00270 template <int FAKE> class TypeInfo<fpreal32, FAKE>
00271 { public: typedef NativeBasicAccessors<fpreal32> Accessors; };
00272 template <int FAKE> class TypeInfo<int32, FAKE>
00273 { public: typedef NativeBasicAccessors<int32> Accessors; };
00274
00275 template <int FAKE> class TypeInfo<fpreal64, FAKE>
00276 { public: typedef NonNativeBasicAccessors<fpreal64, fpreal32> Accessors; };
00277 template <int FAKE> class TypeInfo<int64, FAKE>
00278 { public: typedef NonNativeBasicAccessors<int64, int32> Accessors; };
00279
00280 template <int FAKE> class TypeInfo<UT_Vector2, FAKE>
00281 { public: typedef NativePODCompoundAccessors<UT_Vector2, fpreal32> Accessors; };
00282 template <int FAKE> class TypeInfo<UT_Vector3, FAKE>
00283 { public: typedef NativePODCompoundAccessors<UT_Vector3, fpreal32> Accessors; };
00284 template <int FAKE> class TypeInfo<UT_Vector4, FAKE>
00285 { public: typedef NativePODCompoundAccessors<UT_Vector4, fpreal32> Accessors; };
00286 template <int FAKE> class TypeInfo<UT_Quaternion, FAKE>
00287 { public: typedef NativePODCompoundAccessors<UT_Quaternion, fpreal32> Accessors; };
00288 template <int FAKE> class TypeInfo<UT_Matrix3, FAKE>
00289 { public: typedef NativePODCompoundAccessors<UT_Matrix3, fpreal32> Accessors; };
00290 template <int FAKE> class TypeInfo<UT_Matrix4, FAKE>
00291 { public: typedef NativePODCompoundAccessors<UT_Matrix4, fpreal32> Accessors; };
00292
00293
00294 public:
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 template <typename T>
00311 const T *getPointer(const GB_AttributeRef &h, T *buffer, int cnt) const
00312 { return TypeInfo<T,0>::Accessors::getPointer(
00313 *this, h, buffer, cnt); }
00314 template <typename T>
00315 const T *getPointer(const GB_AttributeRef &h,
00316 GB_AttributeBuffer &buffer, int cnt) const
00317 { return TypeInfo<T,0>::Accessors::getPointer(
00318 *this, h, buffer, cnt); }
00319 template <typename T>
00320 T getValue(const GB_AttributeRef &handle, int index = 0) const
00321 { return
00322 TypeInfo<T,0>::Accessors::get(*this, handle,index); }
00323 template <typename T>
00324 void get(const GB_AttributeRef &handle, T &val, int index = 0) const
00325 { TypeInfo<T,0>::Accessors::get(*this, handle, val,index); }
00326 template <typename T>
00327 void set(const GB_AttributeRef &handle, const T &val, int index = 0)
00328 { TypeInfo<T,0>::Accessors::set(*this, handle, val,index); }
00329 template <typename T>
00330 void get(const GB_AttributeRef &handle, T *val, int count) const
00331 { TypeInfo<T,0>::Accessors::get(*this, handle, val,count); }
00332 template <typename T>
00333 void set(const GB_AttributeRef &handle, const T *val, int count)
00334 { TypeInfo<T,0>::Accessors::set(*this, handle, val,count); }
00335
00336
00337 private:
00338
00339
00340 template <typename DST_T, typename SRC_T> struct CopyImpl
00341 {
00342 static void copy(DST_T *dest, const SRC_T *src, int count)
00343 {
00344 for (int i = 0; i < count; i++)
00345 dest[i] = (DST_T)(src[i]);
00346 }
00347 };
00348 template <typename T> struct CopyImpl<T,T>
00349 {
00350 static void copy(T *dest, const T *src, int count)
00351 {
00352 memcpy(dest, src, count*sizeof(T));
00353 }
00354 };
00355
00356 template <typename SRC_T, typename DST_T>
00357 void read(const GB_AttributeRef &handle, DST_T *dest,
00358 int count) const
00359 {
00360 CopyImpl<DST_T, SRC_T>::copy(
00361 dest,castAttribData<SRC_T>(handle),count);
00362 }
00363 template <typename SRC_T, typename DST_T>
00364 void write(const GB_AttributeRef &handle, const SRC_T *src,
00365 int count)
00366 {
00367 CopyImpl<DST_T, SRC_T>::copy(
00368 castAttribData<DST_T>(handle), src, count);
00369 }
00370
00371 void *myData;
00372 };
00373
00374 #endif