VTK
|
00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkDataArrayTemplate.h 00005 00006 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00007 All rights reserved. 00008 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 00013 00014 =========================================================================*/ 00023 #ifndef vtkDataArrayTemplate_h 00024 #define vtkDataArrayTemplate_h 00025 00026 #include "vtkCommonCoreModule.h" // For export macro 00027 #include "vtkTypedDataArray.h" 00028 #include "vtkTypeTemplate.h" // For templated vtkObject API 00029 #include <cassert> // for assert() 00030 00031 template <class T> 00032 class vtkDataArrayTemplateLookup; 00033 00034 template <class T> 00035 class VTKCOMMONCORE_EXPORT vtkDataArrayTemplate: 00036 public vtkTypeTemplate<vtkDataArrayTemplate<T>, vtkTypedDataArray<T> > 00037 { 00038 public: 00039 typedef vtkTypedDataArray<T> Superclass; 00040 typedef typename Superclass::ValueType ValueType; 00041 friend class vtkDataArrayTemplateHelper; 00042 00043 void PrintSelf(ostream& os, vtkIndent indent); 00044 00048 typedef ValueType* Iterator; 00049 00053 Iterator Begin() { return Iterator(this->GetVoidPointer(0)); } 00054 00058 Iterator End() { return Iterator(this->GetVoidPointer(this->MaxId + 1)); } 00059 00066 static vtkDataArrayTemplate<T>* FastDownCast(vtkAbstractArray *src); 00067 00070 int Allocate(vtkIdType sz, vtkIdType ext=1000); 00071 00073 void Initialize(); 00074 00076 int GetDataTypeSize() { return static_cast<int>(sizeof(T)); } 00077 00079 void SetNumberOfTuples(vtkIdType number); 00080 00086 virtual void SetTuple(vtkIdType i, vtkIdType j, vtkAbstractArray* source); 00087 00091 virtual void InsertTuple(vtkIdType i, vtkIdType j, vtkAbstractArray* source); 00092 00094 00097 virtual void InsertTuples(vtkIdList *destIds, vtkIdList *srcIds, 00098 vtkAbstractArray *source); 00100 00102 00105 virtual void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, 00106 vtkAbstractArray* source); 00108 00112 virtual vtkIdType InsertNextTuple(vtkIdType j, vtkAbstractArray* source); 00113 00116 double* GetTuple(vtkIdType i); 00117 00119 00120 void GetTuple(vtkIdType i, double* tuple); 00121 void GetTupleValue(vtkIdType i, T* tuple); 00123 00125 00126 void SetTuple(vtkIdType i, const float* tuple); 00127 void SetTuple(vtkIdType i, const double* tuple); 00128 void SetTupleValue(vtkIdType i, const T* tuple); 00130 00132 00134 void InsertTuple(vtkIdType i, const float* tuple); 00135 void InsertTuple(vtkIdType i, const double* tuple); 00136 void InsertTupleValue(vtkIdType i, const T* tuple); 00138 00140 00142 vtkIdType InsertNextTuple(const float* tuple); 00143 vtkIdType InsertNextTuple(const double* tuple); 00144 vtkIdType InsertNextTupleValue(const T* tuple); 00146 00148 00150 void GetValueRange(T range[2], int comp) 00151 { 00152 double doubleRange[2]; 00153 this->ComputeRange(doubleRange, comp); 00154 range[0] = static_cast<T>(doubleRange[0]); 00155 range[1] = static_cast<T>(doubleRange[1]); 00156 } 00157 T *GetValueRange(int comp) 00158 { 00159 this->GetValueRange(this->ValueRange, comp); 00160 return this->ValueRange; 00161 } 00163 00165 00167 T *GetValueRange() 00168 { return this->GetValueRange(0); } 00169 void GetValueRange(T range[2]) 00170 { this->GetValueRange(range, 0); } 00172 00174 void Squeeze() { this->ResizeAndExtend (this->MaxId+1); } 00175 00177 vtkIdType Capacity() { return this->Size; } 00178 00183 virtual int Resize(vtkIdType numTuples); 00184 00186 00187 T GetValue(vtkIdType id) 00188 { assert(id >= 0 && id < this->Size); return this->Array[id]; } 00189 T& GetValueReference(vtkIdType id) 00190 { assert(id >= 0 && id < this->Size); return this->Array[id]; } 00192 00194 00196 void SetValue(vtkIdType id, T value) 00197 { assert(id >= 0 && id < this->Size); this->Array[id] = value;}; 00199 00203 void SetNumberOfValues(vtkIdType number); 00204 00206 void InsertValue(vtkIdType id, T f); 00207 00209 void SetVariantValue(vtkIdType id, vtkVariant value); 00210 00213 vtkIdType InsertNextValue(T f); 00214 00216 00219 virtual void RemoveTuple(vtkIdType id); 00220 virtual void RemoveFirstTuple(); 00221 virtual void RemoveLastTuple(); 00223 00227 double GetComponent(vtkIdType i, int j); 00228 00233 void SetComponent(vtkIdType i, int j, double c); 00234 00238 virtual void InsertComponent(vtkIdType i, int j, double c); 00239 00241 00244 T* WritePointer(vtkIdType id, vtkIdType number); 00245 virtual void* WriteVoidPointer(vtkIdType id, vtkIdType number) 00246 { return this->WritePointer(id, number); } 00248 00250 00254 T* GetPointer(vtkIdType id) { return this->Array + id; } 00255 virtual void* GetVoidPointer(vtkIdType id) { return this->GetPointer(id); } 00257 00258 //BTX 00259 enum DeleteMethod 00260 { 00261 VTK_DATA_ARRAY_FREE, 00262 VTK_DATA_ARRAY_DELETE 00263 }; 00264 //ETX 00265 00267 00276 void SetArray(T* array, vtkIdType size, int save, int deleteMethod); 00277 void SetArray(T* array, vtkIdType size, int save) 00278 { this->SetArray(array, size, save, VTK_DATA_ARRAY_FREE); } 00279 virtual void SetVoidArray(void* array, vtkIdType size, int save) 00280 { this->SetArray(static_cast<T*>(array), size, save); } 00281 virtual void SetVoidArray(void* array, 00282 vtkIdType size, 00283 int save, 00284 int deleteMethod) 00285 { 00286 this->SetArray(static_cast<T*>(array), size, save, deleteMethod); 00287 } 00289 00293 virtual void ExportToVoidPointer(void *out_ptr); 00294 00296 virtual vtkArrayIterator* NewIterator(); 00297 00299 00300 virtual vtkIdType LookupValue(vtkVariant value); 00301 virtual void LookupValue(vtkVariant value, vtkIdList* ids); 00302 vtkIdType LookupValue(T value); 00303 void LookupValue(T value, vtkIdList* ids); 00304 vtkIdType LookupTypedValue(T value) 00305 { return this->LookupValue(value); } 00306 void LookupTypedValue(T value, vtkIdList* ids) 00307 { this->LookupValue(value, ids); } 00309 00316 virtual void DataChanged(); 00317 00321 virtual void DataElementChanged(vtkIdType id); 00322 00326 virtual void ClearLookup(); 00327 00329 virtual int GetArrayType() { return vtkAbstractArray::DataArrayTemplate; } 00330 00331 protected: 00332 vtkDataArrayTemplate(); 00333 ~vtkDataArrayTemplate(); 00334 00335 T* Array; // pointer to data 00336 T ValueRange[2]; // range of the data 00337 T* ResizeAndExtend(vtkIdType sz); // function to resize data 00338 T* Realloc(vtkIdType sz); 00339 00340 int TupleSize; //used for data conversion 00341 double* Tuple; 00342 00343 int SaveUserArray; 00344 int DeleteMethod; 00345 00346 virtual bool ComputeScalarRange(double* ranges); 00347 virtual bool ComputeVectorRange(double range[2]); 00348 private: 00349 vtkDataArrayTemplate(const vtkDataArrayTemplate&); // Not implemented. 00350 void operator=(const vtkDataArrayTemplate&); // Not implemented. 00351 00352 vtkDataArrayTemplateLookup<T>* Lookup; 00353 bool RebuildLookup; 00354 void UpdateLookup(); 00355 00356 void DeleteArray(); 00357 }; 00358 00359 #if !defined(VTK_NO_EXPLICIT_TEMPLATE_INSTANTIATION) 00360 # define VTK_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \ 00361 template class VTKCOMMONCORE_EXPORT vtkDataArrayTemplate< T > 00362 #else 00363 # include "vtkDataArrayTemplateImplicit.txx" 00364 # define VTK_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) 00365 #endif 00366 00367 // This macro is used by the subclasses to create dummy 00368 // declarations for these functions such that the wrapper 00369 // can see them. The wrappers ignore vtkDataArrayTemplate. 00370 #define vtkCreateWrappedArrayInterface(T) \ 00371 int GetDataType(); \ 00372 void GetTupleValue(vtkIdType i, T* tuple); \ 00373 void SetTupleValue(vtkIdType i, const T* tuple); \ 00374 void InsertTupleValue(vtkIdType i, const T* tuple); \ 00375 vtkIdType InsertNextTupleValue(const T* tuple); \ 00376 T GetValue(vtkIdType id); \ 00377 void SetValue(vtkIdType id, T value); \ 00378 void SetNumberOfValues(vtkIdType number); \ 00379 void InsertValue(vtkIdType id, T f); \ 00380 vtkIdType InsertNextValue(T f); \ 00381 T *GetValueRange(int comp); \ 00382 T *GetValueRange(); \ 00383 T* WritePointer(vtkIdType id, vtkIdType number); \ 00384 T* GetPointer(vtkIdType id)/*; \ 00385 00386 * These methods are not wrapped to avoid wrappers exposing these 00387 * easy-to-get-wrong methods because passing in the wrong value for 'save' is 00388 * guaranteed to cause a memory issue down the line. Either the wrappers 00389 * didn't use malloc to allocate the memory or the memory isn't actually 00390 * persisted because a temporary array is used that doesn't persist like this 00391 * method expects. 00392 00393 void SetArray(T* array, vtkIdType size, int save); \ 00394 void SetArray(T* array, vtkIdType size, int save, int deleteMethod) */ 00395 00396 #endif // !defined(vtkDataArrayTemplate_h) 00397 00398 // This portion must be OUTSIDE the include blockers. Each 00399 // vtkDataArray subclass uses this to give its instantiation of this 00400 // template a DLL interface. 00401 #if defined(VTK_DATA_ARRAY_TEMPLATE_TYPE) && !defined(VTK_NO_EXPLICIT_TEMPLATE_INSTANTIATION) 00402 # if defined(VTK_BUILD_SHARED_LIBS) && defined(_MSC_VER) 00403 # pragma warning (push) 00404 # pragma warning (disable: 4091) // warning C4091: 'extern ' : 00405 // ignored on left of 'int' when no variable is declared 00406 # pragma warning (disable: 4231) // Compiler-specific extension warning. 00407 00408 // We need to disable warning 4910 and do an extern dllexport 00409 // anyway. When deriving vtkCharArray and other types from an 00410 // instantiation of this template the compiler does an explicit 00411 // instantiation of the base class. From outside the vtkCommon 00412 // library we block this using an extern dllimport instantiation. 00413 // For classes inside vtkCommon we should be able to just do an 00414 // extern instantiation, but VS 2008 complains about missing 00415 // definitions. We cannot do an extern dllimport inside vtkCommon 00416 // since the symbols are local to the dll. An extern dllexport 00417 // seems to be the only way to convince VS 2008 to do the right 00418 // thing, so we just disable the warning. 00419 # pragma warning (disable: 4910) // extern and dllexport incompatible 00420 00421 // Use an "extern explicit instantiation" to give the class a DLL 00422 // interface. This is a compiler-specific extension. 00423 extern VTK_DATA_ARRAY_TEMPLATE_INSTANTIATE(VTK_DATA_ARRAY_TEMPLATE_TYPE); 00424 # pragma warning (pop) 00425 # endif 00426 # undef VTK_DATA_ARRAY_TEMPLATE_TYPE 00427 #endif 00428 // VTK-HeaderTest-Exclude: vtkDataArrayTemplate.h