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 "vtkCommonCoreModule.h" // For export macro
32 #include "vtkGenericDataArray.h"
33 #include "vtkBuffer.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 {
43 public:
46  typedef typename Superclass::ValueType ValueType;
47 
49  {
52  VTK_DATA_ARRAY_ALIGNED_FREE=vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
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,
141  bool updateMaxId = false, bool save=false,
142  int deleteMethod=VTK_DATA_ARRAY_FREE);
143 
151  void SetArrayFreeFunction(void (*callback)(void *)) override;
152 
159  void SetArrayFreeFunction(int comp, void (*callback)(void *));
160 
165  ValueType* GetComponentArrayPointer(int comp);
166 
171  void *GetVoidPointer(vtkIdType valueIdx) override;
172 
177  void ExportToVoidPointer(void *ptr) override;
178 
179 #ifndef __VTK_WRAP__
180 
181 
189  {
190  if (source)
191  {
192  switch (source->GetArrayType())
193  {
195  if (vtkDataTypesCompare(source->GetDataType(),
197  {
198  return static_cast<vtkSOADataArrayTemplate<ValueType>*>(source);
199  }
200  break;
201  }
202  }
203  return nullptr;
204  }
206 #endif
207 
210  void SetNumberOfComponents(int numComps) override;
211  void ShallowCopy(vtkDataArray *other) override;
212 
213  // Reimplemented for efficiency:
214  void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart,
215  vtkAbstractArray* source) override;
216  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
217  // using Superclass::InsertTuples;
218  void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds,
219  vtkAbstractArray *source) override
220  { this->Superclass::InsertTuples(dstIds, srcIds, source); }
221 
222 protected:
224  ~vtkSOADataArrayTemplate() override;
225 
230  bool AllocateTuples(vtkIdType numTuples);
231 
236  bool ReallocateTuples(vtkIdType numTuples);
237 
238  std::vector<vtkBuffer<ValueType>*> Data;
240 
242 
243 private:
245  void operator=(const vtkSOADataArrayTemplate&) = delete;
246 
247  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx,
248  vtkIdType& tupleIdx, int& comp) const
249  {
250  tupleIdx = static_cast<vtkIdType>(valueIdx *
251  this->NumberOfComponentsReciprocal);
252  comp = valueIdx - (tupleIdx * this->NumberOfComponents);
253  }
254 
255  friend class vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>,
256  ValueTypeT>;
257 };
258 
259 // Declare vtkArrayDownCast implementations for SoA containers:
261 
262 #endif // header guard
263 
264 // This portion must be OUTSIDE the include blockers. This is used to tell
265 // libraries other than vtkCommonCore that instantiations of
266 // vtkSOADataArrayTemplate can be found externally. This prevents each library
267 // from instantiating these on their own.
268 #ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
269 #define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
270  template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate< T >
271 #elif defined(VTK_USE_EXTERN_TEMPLATE)
272 #ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
273 #define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
274 #ifdef _MSC_VER
275 #pragma warning (push)
276 // The following is needed when the vtkSOADataArrayTemplate is declared
277 // dllexport and is used from another class in vtkCommonCore
278 #pragma warning (disable: 4910) // extern and dllexport incompatible
279 #endif
281  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
282 #ifdef _MSC_VER
283 #pragma warning (pop)
284 #endif
285 #endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
286 
287 // The following clause is only for MSVC 2008 and 2010
288 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
289 #pragma warning (push)
290 
291 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
292 #pragma warning (disable: 4091)
293 
294 // Compiler-specific extension warning.
295 #pragma warning (disable: 4231)
296 
297 // We need to disable warning 4910 and do an extern dllexport
298 // anyway. When deriving new arrays from an
299 // instantiation of this template the compiler does an explicit
300 // instantiation of the base class. From outside the vtkCommon
301 // library we block this using an extern dllimport instantiation.
302 // For classes inside vtkCommon we should be able to just do an
303 // extern instantiation, but VS 2008 complains about missing
304 // definitions. We cannot do an extern dllimport inside vtkCommon
305 // since the symbols are local to the dll. An extern dllexport
306 // seems to be the only way to convince VS 2008 to do the right
307 // thing, so we just disable the warning.
308 #pragma warning (disable: 4910) // extern and dllexport incompatible
309 
310 // Use an "extern explicit instantiation" to give the class a DLL
311 // interface. This is a compiler-specific extension.
313  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
314 
315 #pragma warning (pop)
316 
317 #endif
318 
319 // VTK-HeaderTest-Exclude: vtkSOADataArrayTemplate.h
vtkAbstractArray::DeleteMethod
DeleteMethod
Definition: vtkAbstractArray.h:330
VTK_ZEROCOPY
#define VTK_ZEROCOPY
Definition: vtkWrappingHints.h:40
vtkGenericDataArray::ValueType
ValueTypeT ValueType
Definition: vtkGenericDataArray.h:81
vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE
Definition: vtkAbstractArray.h:334
vtkSOADataArrayTemplate::AoSCopy
vtkBuffer< ValueType > * AoSCopy
Definition: vtkSOADataArrayTemplate.h:239
vtkGenericDataArray::GetTypedComponent
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
Definition: vtkGenericDataArray.h:152
vtkTypeTraits
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:29
vtkX3D::value
Definition: vtkX3D.h:220
vtkAbstractArray::VTK_DATA_ARRAY_DELETE
Definition: vtkAbstractArray.h:333
vtkSOADataArrayTemplate
Struct-Of-Arrays implementation of vtkGenericDataArray.
Definition: vtkSOADataArrayTemplate.h:38
vtkIdType
int vtkIdType
Definition: vtkType.h:347
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:65
vtkGenericDataArray::SetTypedComponent
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
Set component compIdx of the tuple at tupleIdx to value.
Definition: vtkGenericDataArray.h:165
vtkAbstractArray::VTK_DATA_ARRAY_FREE
Definition: vtkAbstractArray.h:332
vtkGenericDataArray
Base interface for all typed vtkDataArray subclasses.
Definition: vtkGenericDataArray.h:77
vtkDataArray
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:48
vtkAbstractArray::SoADataArrayTemplate
Definition: vtkAbstractArray.h:643
vtkSOADataArrayTemplate::FastDownCast
static vtkSOADataArrayTemplate< ValueType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
Definition: vtkSOADataArrayTemplate.h:188
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...
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:825
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...
vtkSOADataArrayTemplate::NumberOfComponentsReciprocal
double NumberOfComponentsReciprocal
Definition: vtkSOADataArrayTemplate.h:241
vtkBuffer.h
vtkGenericDataArray::AllocateTuples
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
Definition: vtkGenericDataArray.h:323
vtkExternTemplateMacro
#define vtkExternTemplateMacro(decl)
A macro to declare extern templates for all numerical types.
Definition: vtkType.h:415
vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
Definition: vtkAbstractArray.h:335
vtkSOADataArrayTemplate::SetValue
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
Definition: vtkSOADataArrayTemplate.h:75
vtkGenericDataArray::vtkTemplateTypeMacro
vtkTemplateTypeMacro(SelfType, vtkDataArray) enum
Compile time access to the VTK type identifier.
Definition: vtkGenericDataArray.h:82
vtkGenericDataArray.h
vtkIdList
list of point or cell ids
Definition: vtkIdList.h:30
vtkX3D::size
Definition: vtkX3D.h:253
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:395
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:218
vtkSOADataArrayTemplate::Data
std::vector< vtkBuffer< ValueType > * > Data
Definition: vtkSOADataArrayTemplate.h:238
vtkArrayIterator
Abstract superclass to iterate over elements in an vtkAbstractArray.
Definition: vtkArrayIterator.h:49
vtkArrayDownCast_TemplateFastCastMacro
vtkArrayDownCast_TemplateFastCastMacro(vtkTypedDataArray) template< class Scalar > inline typename vtkTypedDataArray< Scalar >
Definition: vtkTypedDataArray.h:188
vtkSOADataArrayTemplate::GetArrayType
int GetArrayType() override
Method for type-checking in FastDownCast implementations.
Definition: vtkSOADataArrayTemplate.h:208
VTK_NEWINSTANCE
#define VTK_NEWINSTANCE
Definition: vtkWrappingHints.h:39
vtkGenericDataArray::ReallocateTuples
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
Definition: vtkGenericDataArray.h:333
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:686
vtkSOADataArrayTemplate::SetTypedComponent
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
Definition: vtkSOADataArrayTemplate.h:117