VTK
vtkSOADataArrayTemplate.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkSOADataArrayTemplate.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
28 #ifndef vtkSOADataArrayTemplate_h
29 #define vtkSOADataArrayTemplate_h
30 
31 #include "vtkBuffer.h"
32 #include "vtkCommonCoreModule.h" // For export macro
33 #include "vtkGenericDataArray.h"
34 
35 // The export macro below makes no sense, but is necessary for older compilers
36 // when we export instantiations of this class from vtkCommonCore.
37 template <class ValueTypeT>
38 class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate
39  : public vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT>
40 {
42 
43 public:
46  typedef typename Superclass::ValueType ValueType;
47 
49  {
51  VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
52  VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
53  VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
54  };
55 
56  static vtkSOADataArrayTemplate* New();
57 
59 
62  inline ValueType GetValue(vtkIdType valueIdx) const
63  {
64  vtkIdType tupleIdx;
65  int comp;
66  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
67  return this->GetTypedComponent(tupleIdx, comp);
68  }
70 
72 
75  inline void SetValue(vtkIdType valueIdx, ValueType value)
76  {
77  vtkIdType tupleIdx;
78  int comp;
79  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
80  this->SetTypedComponent(tupleIdx, comp, value);
81  }
83 
87  inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
88  {
89  for (size_t cc = 0; cc < this->Data.size(); cc++)
90  {
91  tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx];
92  }
93  }
94 
98  inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
99  {
100  for (size_t cc = 0; cc < this->Data.size(); ++cc)
101  {
102  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc];
103  }
104  }
105 
109  inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
110  {
111  return this->Data[comp]->GetBuffer()[tupleIdx];
112  }
113 
117  inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
118  {
119  this->Data[comp]->GetBuffer()[tupleIdx] = value;
120  }
121 
125  void FillTypedComponent(int compIdx, ValueType value) override;
126 
140  void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size, bool updateMaxId = false,
141  bool save = false, int deleteMethod = VTK_DATA_ARRAY_FREE);
142 
150  void SetArrayFreeFunction(void (*callback)(void*)) override;
151 
158  void SetArrayFreeFunction(int comp, void (*callback)(void*));
159 
164  ValueType* GetComponentArrayPointer(int comp);
165 
170  void* GetVoidPointer(vtkIdType valueIdx) override;
171 
176  void ExportToVoidPointer(void* ptr) override;
177 
178 #ifndef __VTK_WRAP__
179 
180 
187  {
188  if (source)
189  {
190  switch (source->GetArrayType())
191  {
193  if (vtkDataTypesCompare(source->GetDataType(), vtkTypeTraits<ValueType>::VTK_TYPE_ID))
194  {
195  return static_cast<vtkSOADataArrayTemplate<ValueType>*>(source);
196  }
197  break;
198  }
199  }
200  return nullptr;
201  }
203 #endif
204 
205  int GetArrayType() const override { return vtkAbstractArray::SoADataArrayTemplate; }
207  void SetNumberOfComponents(int numComps) override;
208  void ShallowCopy(vtkDataArray* other) override;
209 
210  // Reimplemented for efficiency:
211  void InsertTuples(
212  vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
213  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
214  // using Superclass::InsertTuples;
215  void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
216  {
217  this->Superclass::InsertTuples(dstIds, srcIds, source);
218  }
219 
220 protected:
222  ~vtkSOADataArrayTemplate() override;
223 
228  bool AllocateTuples(vtkIdType numTuples);
229 
234  bool ReallocateTuples(vtkIdType numTuples);
235 
236  std::vector<vtkBuffer<ValueType>*> Data;
238 
239 private:
241  void operator=(const vtkSOADataArrayTemplate&) = delete;
242 
243  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx, vtkIdType& tupleIdx, int& comp) const
244  {
245  tupleIdx = valueIdx / this->NumberOfComponents;
246  comp = valueIdx % this->NumberOfComponents;
247  }
248 
249  friend class vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT>;
250 };
251 
252 // Declare vtkArrayDownCast implementations for SoA containers:
254 
255 #endif // header guard
256 
257 // This portion must be OUTSIDE the include blockers. This is used to tell
258 // libraries other than vtkCommonCore that instantiations of
259 // vtkSOADataArrayTemplate can be found externally. This prevents each library
260 // from instantiating these on their own.
261 #ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
262 #define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
263  template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate<T>
264 #elif defined(VTK_USE_EXTERN_TEMPLATE)
265 #ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
266 #define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
267 #ifdef _MSC_VER
268 #pragma warning(push)
269 // The following is needed when the vtkSOADataArrayTemplate is declared
270 // dllexport and is used from another class in vtkCommonCore
271 #pragma warning(disable : 4910) // extern and dllexport incompatible
272 #endif
273 vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
274 #ifdef _MSC_VER
275 #pragma warning(pop)
276 #endif
277 #endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
278 
279 // The following clause is only for MSVC 2008 and 2010
280 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
281 #pragma warning(push)
282 
283 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
284 #pragma warning(disable : 4091)
285 
286 // Compiler-specific extension warning.
287 #pragma warning(disable : 4231)
288 
289 // We need to disable warning 4910 and do an extern dllexport
290 // anyway. When deriving new arrays from an
291 // instantiation of this template the compiler does an explicit
292 // instantiation of the base class. From outside the vtkCommon
293 // library we block this using an extern dllimport instantiation.
294 // For classes inside vtkCommon we should be able to just do an
295 // extern instantiation, but VS 2008 complains about missing
296 // definitions. We cannot do an extern dllimport inside vtkCommon
297 // since the symbols are local to the dll. An extern dllexport
298 // seems to be the only way to convince VS 2008 to do the right
299 // thing, so we just disable the warning.
300 #pragma warning(disable : 4910) // extern and dllexport incompatible
301 
302 // Use an "extern explicit instantiation" to give the class a DLL
303 // interface. This is a compiler-specific extension.
304 vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
305 
306 #pragma warning(pop)
307 
308 #endif
309 
310 // VTK-HeaderTest-Exclude: vtkSOADataArrayTemplate.h
vtkAbstractArray::DeleteMethod
DeleteMethod
Definition: vtkAbstractArray.h:321
VTK_ZEROCOPY
#define VTK_ZEROCOPY
Definition: vtkWrappingHints.h:43
vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE
Definition: vtkAbstractArray.h:325
vtkSOADataArrayTemplate::GetArrayType
int GetArrayType() const override
Method for type-checking in FastDownCast implementations.
Definition: vtkSOADataArrayTemplate.h:205
vtkSOADataArrayTemplate::AoSCopy
vtkBuffer< ValueType > * AoSCopy
Definition: vtkSOADataArrayTemplate.h:237
vtkGenericDataArray::GetTypedComponent
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
Definition: vtkGenericDataArray.h:158
vtkTypeTraits
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:30
vtkGenericDataArray::vtkTemplateTypeMacro
vtkTemplateTypeMacro(SelfType, vtkDataArray)
vtkX3D::value
Definition: vtkX3D.h:226
vtkAbstractArray::VTK_DATA_ARRAY_DELETE
Definition: vtkAbstractArray.h:324
vtkSOADataArrayTemplate
Struct-Of-Arrays implementation of vtkGenericDataArray.
Definition: vtkSOADataArrayTemplate.h:38
vtkIdType
int vtkIdType
Definition: vtkType.h:343
vtkGenericDataArray::GetVoidPointer
void * GetVoidPointer(vtkIdType valueIdx) override
Default implementation raises a runtime error.
vtkObject::New
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
vtkSOADataArrayTemplate::SetTypedTuple
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array's tuple at tupleIdx to the values in tuple.
Definition: vtkSOADataArrayTemplate.h:98
vtkSOADataArrayTemplate::GetTypedComponent
ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
Get component comp of the tuple at tupleIdx.
Definition: vtkSOADataArrayTemplate.h:109
save
void save(Archiver &ar, const vtkUnicodeString &str, const unsigned int vtkNotUsed(version))
Definition: vtkVariantBoostSerialization.h:64
vtkGenericDataArray::SetTypedComponent
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
Set component compIdx of the tuple at tupleIdx to value.
Definition: vtkGenericDataArray.h:169
vtkAbstractArray::VTK_DATA_ARRAY_FREE
Definition: vtkAbstractArray.h:323
vtkGenericDataArray
Base interface for all typed vtkDataArray subclasses.
Definition: vtkGenericDataArray.h:79
vtkArrayDownCast_TemplateFastCastMacro
vtkArrayDownCast_TemplateFastCastMacro(vtkSOADataArrayTemplate)
vtkDataArray
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:49
vtkAbstractArray::SoADataArrayTemplate
Definition: vtkAbstractArray.h:638
vtkSOADataArrayTemplate::FastDownCast
static vtkSOADataArrayTemplate< ValueType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
Definition: vtkSOADataArrayTemplate.h:186
vtkSOADataArrayTemplate::SelfType
vtkSOADataArrayTemplate< ValueTypeT > SelfType
Definition: vtkSOADataArrayTemplate.h:44
vtkGenericDataArray::SetArrayFreeFunction
void SetArrayFreeFunction(void(*callback)(void *)) override
This method allows the user to specify a custom free function to be called when the array is dealloca...
vtkBuffer< ValueType >
vtkGenericDataArray::FillTypedComponent
virtual void FillTypedComponent(int compIdx, ValueType value)
Set component comp of all tuples to value.
vtkGenericDataArray::InsertTuples
void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source) override
Copy the tuples indexed in srcIds from the source array to the tuple locations indexed by dstIds in t...
vtkBuffer.h
vtkGenericDataArray::AllocateTuples
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
Definition: vtkGenericDataArray.h:335
vtkExternTemplateMacro
#define vtkExternTemplateMacro(decl)
A macro to declare extern templates for all numerical types.
Definition: vtkType.h:408
vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
Definition: vtkAbstractArray.h:326
vtkSOADataArrayTemplate::SetValue
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
Definition: vtkSOADataArrayTemplate.h:75
vtkGenericDataArray.h
vtkIdList
list of point or cell ids
Definition: vtkIdList.h:30
vtkX3D::size
Definition: vtkX3D.h:259
vtkDataArray::ShallowCopy
virtual void ShallowCopy(vtkDataArray *other)
Create a shallow copy of other into this, if possible.
vtkAbstractArray
Abstract superclass for all arrays.
Definition: vtkAbstractArray.h:75
vtkInstantiateTemplateMacro
#define vtkInstantiateTemplateMacro(decl)
A macro to instantiate a template over all numerical types.
Definition: vtkType.h:389
vtkSOADataArrayTemplate::GetTypedTuple
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
Definition: vtkSOADataArrayTemplate.h:87
vtkGenericDataArray::NewIterator
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
vtkAbstractArray::ExportToVoidPointer
virtual void ExportToVoidPointer(void *out_ptr)
This method copies the array data to the void pointer specified by the user.
vtkSOADataArrayTemplate::InsertTuples
void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source) override
Copy the tuples indexed in srcIds from the source array to the tuple locations indexed by dstIds in t...
Definition: vtkSOADataArrayTemplate.h:215
vtkSOADataArrayTemplate::Data
std::vector< vtkBuffer< ValueType > * > Data
Definition: vtkSOADataArrayTemplate.h:236
vtkArrayIterator
Abstract superclass to iterate over elements in an vtkAbstractArray.
Definition: vtkArrayIterator.h:49
vtkSOADataArrayTemplate::ValueType
Superclass::ValueType ValueType
Definition: vtkSOADataArrayTemplate.h:46
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:959
VTK_NEWINSTANCE
#define VTK_NEWINSTANCE
Definition: vtkWrappingHints.h:42
vtkGenericDataArray::ReallocateTuples
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
Definition: vtkGenericDataArray.h:345
vtkGenericDataArray::SetNumberOfComponents
void SetNumberOfComponents(int num) override
Set/Get the dimension (n) of the components.
vtkSOADataArrayTemplate::GetValue
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
Definition: vtkSOADataArrayTemplate.h:62
vtkAbstractArray::NumberOfComponents
int NumberOfComponents
Definition: vtkAbstractArray.h:679
vtkSOADataArrayTemplate::SetTypedComponent
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
Definition: vtkSOADataArrayTemplate.h:117