VTK
dox/Common/Core/vtkDataArrayTemplate.h
Go to the documentation of this file.
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   void PrintSelf(ostream& os, vtkIndent indent);
00042 
00046   typedef ValueType* Iterator;
00047 
00051   Iterator Begin() { return Iterator(this->GetVoidPointer(0)); }
00052 
00056   Iterator End() { return Iterator(this->GetVoidPointer(this->MaxId + 1)); }
00057 
00064   static vtkDataArrayTemplate<T>* FastDownCast(vtkAbstractArray *src);
00065 
00068   int Allocate(vtkIdType sz, vtkIdType ext=1000);
00069 
00071   void Initialize();
00072 
00074   int GetDataTypeSize() { return static_cast<int>(sizeof(T)); }
00075 
00077   void SetNumberOfTuples(vtkIdType number);
00078 
00084   virtual void SetTuple(vtkIdType i, vtkIdType j, vtkAbstractArray* source);
00085 
00089   virtual void InsertTuple(vtkIdType i, vtkIdType j, vtkAbstractArray* source);
00090 
00092 
00095   virtual void InsertTuples(vtkIdList *destIds, vtkIdList *srcIds,
00096                             vtkAbstractArray *source);
00098 
00102   virtual vtkIdType InsertNextTuple(vtkIdType j, vtkAbstractArray* source);
00103 
00106   double* GetTuple(vtkIdType i);
00107 
00109 
00110   void GetTuple(vtkIdType i, double* tuple);
00111   void GetTupleValue(vtkIdType i, T* tuple);
00113 
00115 
00116   void SetTuple(vtkIdType i, const float* tuple);
00117   void SetTuple(vtkIdType i, const double* tuple);
00118   void SetTupleValue(vtkIdType i, const T* tuple);
00120 
00122 
00124   void InsertTuple(vtkIdType i, const float* tuple);
00125   void InsertTuple(vtkIdType i, const double* tuple);
00126   void InsertTupleValue(vtkIdType i, const T* tuple);
00128 
00130 
00132   vtkIdType InsertNextTuple(const float* tuple);
00133   vtkIdType InsertNextTuple(const double* tuple);
00134   vtkIdType InsertNextTupleValue(const T* tuple);
00136 
00138 
00140   void GetValueRange(T range[2], int comp)
00141     {
00142     double doubleRange[2];
00143     this->ComputeRange(doubleRange, comp);
00144     range[0] = static_cast<T>(doubleRange[0]);
00145     range[1] = static_cast<T>(doubleRange[1]);
00146     }
00147   T *GetValueRange(int comp)
00148     {
00149     this->GetValueRange(this->ValueRange, comp);
00150     return this->ValueRange;
00151     }
00153 
00155 
00157   T *GetValueRange()
00158     { return this->GetValueRange(0); }
00159   void GetValueRange(T range[2])
00160     { this->GetValueRange(range, 0); }
00162 
00164   void Squeeze() { this->ResizeAndExtend (this->MaxId+1); }
00165 
00167   vtkIdType Capacity() { return this->Size; }
00168 
00173   virtual int Resize(vtkIdType numTuples);
00174 
00176 
00177   T GetValue(vtkIdType id)
00178     { assert(id >= 0 && id < this->Size); return this->Array[id]; }
00179   T& GetValueReference(vtkIdType id)
00180     { assert(id >= 0 && id < this->Size); return this->Array[id]; }
00182 
00184 
00186   void SetValue(vtkIdType id, T value)
00187     { assert(id >= 0 && id < this->Size); this->Array[id] = value;};
00189 
00193   void SetNumberOfValues(vtkIdType number);
00194 
00196   void InsertValue(vtkIdType id, T f);
00197 
00199   void SetVariantValue(vtkIdType id, vtkVariant value);
00200 
00203   vtkIdType InsertNextValue(T f);
00204 
00206 
00209   virtual void RemoveTuple(vtkIdType id);
00210   virtual void RemoveFirstTuple();
00211   virtual void RemoveLastTuple();
00213 
00217   double GetComponent(vtkIdType i, int j);
00218 
00223   void SetComponent(vtkIdType i, int j, double c);
00224 
00228   virtual void InsertComponent(vtkIdType i, int j, double c);
00229 
00231 
00234   T* WritePointer(vtkIdType id, vtkIdType number);
00235   virtual void* WriteVoidPointer(vtkIdType id, vtkIdType number)
00236     { return this->WritePointer(id, number); }
00238 
00240 
00244   T* GetPointer(vtkIdType id) { return this->Array + id; }
00245   virtual void* GetVoidPointer(vtkIdType id) { return this->GetPointer(id); }
00247 
00248 //BTX
00249   enum DeleteMethod
00250   {
00251     VTK_DATA_ARRAY_FREE,
00252     VTK_DATA_ARRAY_DELETE
00253   };
00254 //ETX
00255 
00257 
00266   void SetArray(T* array, vtkIdType size, int save, int deleteMethod);
00267   void SetArray(T* array, vtkIdType size, int save)
00268     { this->SetArray(array, size, save, VTK_DATA_ARRAY_FREE); }
00269   virtual void SetVoidArray(void* array, vtkIdType size, int save)
00270     { this->SetArray(static_cast<T*>(array), size, save); }
00271   virtual void SetVoidArray(void* array,
00272                             vtkIdType size,
00273                             int save,
00274                             int deleteMethod)
00275     {
00276       this->SetArray(static_cast<T*>(array), size, save, deleteMethod);
00277     }
00279 
00283   virtual void ExportToVoidPointer(void *out_ptr);
00284 
00286   virtual vtkArrayIterator* NewIterator();
00287 
00289 
00290   virtual vtkIdType LookupValue(vtkVariant value);
00291   virtual void LookupValue(vtkVariant value, vtkIdList* ids);
00292   vtkIdType LookupValue(T value);
00293   void LookupValue(T value, vtkIdList* ids);
00294   vtkIdType LookupTypedValue(T value)
00295     { return this->LookupValue(value); }
00296   void LookupTypedValue(T value, vtkIdList* ids)
00297     { this->LookupValue(value, ids); }
00299 
00306   virtual void DataChanged();
00307 
00311   virtual void DataElementChanged(vtkIdType id);
00312 
00316   virtual void ClearLookup();
00317 
00319   virtual int GetArrayType() { return vtkAbstractArray::DataArrayTemplate; }
00320 
00321 protected:
00322   vtkDataArrayTemplate();
00323   ~vtkDataArrayTemplate();
00324 
00325   T* Array;   // pointer to data
00326   T ValueRange[2]; // range of the data
00327   T* ResizeAndExtend(vtkIdType sz);  // function to resize data
00328   T* Realloc(vtkIdType sz);
00329 
00330   int TupleSize; //used for data conversion
00331   double* Tuple;
00332 
00333   int SaveUserArray;
00334   int DeleteMethod;
00335 
00336   virtual void ComputeScalarRange(double range[2], int comp);
00337   virtual void ComputeVectorRange(double range[2]);
00338 private:
00339   vtkDataArrayTemplate(const vtkDataArrayTemplate&);  // Not implemented.
00340   void operator=(const vtkDataArrayTemplate&);  // Not implemented.
00341 
00342   vtkDataArrayTemplateLookup<T>* Lookup;
00343   bool RebuildLookup;
00344   void UpdateLookup();
00345 
00346   void DeleteArray();
00347 };
00348 
00349 #if !defined(VTK_NO_EXPLICIT_TEMPLATE_INSTANTIATION)
00350 # define VTK_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
00351    template class VTKCOMMONCORE_EXPORT vtkDataArrayTemplate< T >
00352 #else
00353 # include "vtkDataArrayTemplateImplicit.txx"
00354 # define VTK_DATA_ARRAY_TEMPLATE_INSTANTIATE(T)
00355 #endif
00356 
00357 // This macro is used by the subclasses to create dummy
00358 // declarations for these functions such that the wrapper
00359 // can see them. The wrappers ignore vtkDataArrayTemplate.
00360 #define vtkCreateWrappedArrayInterface(T) \
00361   int GetDataType(); \
00362   void GetTupleValue(vtkIdType i, T* tuple); \
00363   void SetTupleValue(vtkIdType i, const T* tuple); \
00364   void InsertTupleValue(vtkIdType i, const T* tuple); \
00365   vtkIdType InsertNextTupleValue(const T* tuple); \
00366   T GetValue(vtkIdType id); \
00367   void SetValue(vtkIdType id, T value); \
00368   void SetNumberOfValues(vtkIdType number); \
00369   void InsertValue(vtkIdType id, T f); \
00370   vtkIdType InsertNextValue(T f); \
00371   T *GetValueRange(int comp); \
00372   T *GetValueRange(); \
00373   T* WritePointer(vtkIdType id, vtkIdType number); \
00374   T* GetPointer(vtkIdType id)/*; \
00375 
00376   * These methods are not wrapped to avoid wrappers exposing these
00377   * easy-to-get-wrong methods because passing in the wrong value for 'save' is
00378   * guaranteed to cause a memory issue down the line. Either the wrappers
00379   * didn't use malloc to allocate the memory or the memory isn't actually
00380   * persisted because a temporary array is used that doesn't persist like this
00381   * method expects.
00382 
00383   void SetArray(T* array, vtkIdType size, int save); \
00384   void SetArray(T* array, vtkIdType size, int save, int deleteMethod) */
00385 
00386 #endif // !defined(__vtkDataArrayTemplate_h)
00387 
00388 // This portion must be OUTSIDE the include blockers.  Each
00389 // vtkDataArray subclass uses this to give its instantiation of this
00390 // template a DLL interface.
00391 #if defined(VTK_DATA_ARRAY_TEMPLATE_TYPE)
00392 # if defined(VTK_BUILD_SHARED_LIBS) && defined(_MSC_VER)
00393 #  pragma warning (push)
00394 #  pragma warning (disable: 4091) // warning C4091: 'extern ' :
00395    // ignored on left of 'int' when no variable is declared
00396 #  pragma warning (disable: 4231) // Compiler-specific extension warning.
00397 
00398    // We need to disable warning 4910 and do an extern dllexport
00399    // anyway.  When deriving vtkCharArray and other types from an
00400    // instantiation of this template the compiler does an explicit
00401    // instantiation of the base class.  From outside the vtkCommon
00402    // library we block this using an extern dllimport instantiation.
00403    // For classes inside vtkCommon we should be able to just do an
00404    // extern instantiation, but VS 2008 complains about missing
00405    // definitions.  We cannot do an extern dllimport inside vtkCommon
00406    // since the symbols are local to the dll.  An extern dllexport
00407    // seems to be the only way to convince VS 2008 to do the right
00408    // thing, so we just disable the warning.
00409 #  pragma warning (disable: 4910) // extern and dllexport incompatible
00410 
00411    // Use an "extern explicit instantiation" to give the class a DLL
00412    // interface.  This is a compiler-specific extension.
00413    extern VTK_DATA_ARRAY_TEMPLATE_INSTANTIATE(VTK_DATA_ARRAY_TEMPLATE_TYPE);
00414 #  pragma warning (pop)
00415 # endif
00416 # undef VTK_DATA_ARRAY_TEMPLATE_TYPE
00417 #endif
00418 // VTK-HeaderTest-Exclude: vtkDataArrayTemplate.h