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 =========================================================================*/
31 #ifndef vtkSOADataArrayTemplate_h
32 #define vtkSOADataArrayTemplate_h
33 
34 #include "vtkCommonCoreModule.h" // For export macro
35 #include "vtkGenericDataArray.h"
36 #include "vtkBuffer.h"
37 
38 // The export macro below makes no sense, but is necessary for older compilers
39 // when we export instantiations of this class from vtkCommonCore.
40 template <class ValueTypeT>
41 class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate :
42  public vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT>
43 {
46 public:
49  typedef typename Superclass::ValueType ValueType;
50 
52  {
55  VTK_DATA_ARRAY_ALIGNED_FREE=vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
57  };
58 
59  static vtkSOADataArrayTemplate* New();
60 
62 
65  inline ValueType GetValue(vtkIdType valueIdx) const
66  {
67  vtkIdType tupleIdx;
68  int comp;
69  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
70  return this->GetTypedComponent(tupleIdx, comp);
71  }
73 
75 
78  inline void SetValue(vtkIdType valueIdx, ValueType value)
79  {
80  vtkIdType tupleIdx;
81  int comp;
82  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
83  this->SetTypedComponent(tupleIdx, comp, value);
84  }
86 
90  inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
91  {
92  for (size_t cc=0; cc < this->Data.size(); cc++)
93  {
94  tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx];
95  }
96  }
97 
101  inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
102  {
103  for (size_t cc=0; cc < this->Data.size(); ++cc)
104  {
105  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc];
106  }
107  }
108 
112  inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
113  {
114  return this->Data[comp]->GetBuffer()[tupleIdx];
115  }
116 
120  inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
121  {
122  this->Data[comp]->GetBuffer()[tupleIdx] = value;
123  }
124 
128  void FillTypedComponent(int compIdx, ValueType value) override;
129 
143  void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size,
144  bool updateMaxId = false, bool save=false,
145  int deleteMethod=VTK_DATA_ARRAY_FREE);
146 
154  void SetArrayFreeFunction(void (*callback)(void *)) override;
155 
162  void SetArrayFreeFunction(int comp, void (*callback)(void *));
163 
168  ValueType* GetComponentArrayPointer(int comp);
169 
174  void *GetVoidPointer(vtkIdType valueIdx) override;
175 
180  void ExportToVoidPointer(void *ptr) override;
181 
182 #ifndef __VTK_WRAP__
183 
184 
192  {
193  if (source)
194  {
195  switch (source->GetArrayType())
196  {
198  if (vtkDataTypesCompare(source->GetDataType(),
200  {
201  return static_cast<vtkSOADataArrayTemplate<ValueType>*>(source);
202  }
203  break;
204  }
205  }
206  return nullptr;
207  }
209 #endif
210 
213  void SetNumberOfComponents(int numComps) override;
214  void ShallowCopy(vtkDataArray *other) override;
215 
216  // Reimplemented for efficiency:
217  void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart,
218  vtkAbstractArray* source) override;
219  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
220  // using Superclass::InsertTuples;
221  void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds,
222  vtkAbstractArray *source) override
223  { this->Superclass::InsertTuples(dstIds, srcIds, source); }
224 
225 protected:
227  ~vtkSOADataArrayTemplate() override;
228 
233  bool AllocateTuples(vtkIdType numTuples);
234 
239  bool ReallocateTuples(vtkIdType numTuples);
240 
241  std::vector<vtkBuffer<ValueType>*> Data;
243 
245 
246 private:
248  void operator=(const vtkSOADataArrayTemplate&) = delete;
249 
250  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx,
251  vtkIdType& tupleIdx, int& comp) const
252  {
253  tupleIdx = static_cast<vtkIdType>(valueIdx *
254  this->NumberOfComponentsReciprocal);
255  comp = valueIdx - (tupleIdx * this->NumberOfComponents);
256  }
257 
258  friend class vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>,
259  ValueTypeT>;
260 };
261 
262 // Declare vtkArrayDownCast implementations for SoA containers:
264 
265 #endif // header guard
266 
267 // This portion must be OUTSIDE the include blockers. This is used to tell
268 // libraries other than vtkCommonCore that instantiations of
269 // vtkSOADataArrayTemplate can be found externally. This prevents each library
270 // from instantiating these on their own.
271 #ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
272 #define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
273  template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate< T >
274 #elif defined(VTK_USE_EXTERN_TEMPLATE)
275 #ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
276 #define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
277 #ifdef _MSC_VER
278 #pragma warning (push)
279 // The following is needed when the vtkSOADataArrayTemplate is declared
280 // dllexport and is used from another class in vtkCommonCore
281 #pragma warning (disable: 4910) // extern and dllexport incompatible
282 #endif
284  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
285 #ifdef _MSC_VER
286 #pragma warning (pop)
287 #endif
288 #endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
289 
290 // The following clause is only for MSVC 2008 and 2010
291 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
292 #pragma warning (push)
293 
294 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
295 #pragma warning (disable: 4091)
296 
297 // Compiler-specific extension warning.
298 #pragma warning (disable: 4231)
299 
300 // We need to disable warning 4910 and do an extern dllexport
301 // anyway. When deriving new arrays from an
302 // instantiation of this template the compiler does an explicit
303 // instantiation of the base class. From outside the vtkCommon
304 // library we block this using an extern dllimport instantiation.
305 // For classes inside vtkCommon we should be able to just do an
306 // extern instantiation, but VS 2008 complains about missing
307 // definitions. We cannot do an extern dllimport inside vtkCommon
308 // since the symbols are local to the dll. An extern dllexport
309 // seems to be the only way to convince VS 2008 to do the right
310 // thing, so we just disable the warning.
311 #pragma warning (disable: 4910) // extern and dllexport incompatible
312 
313 // Use an "extern explicit instantiation" to give the class a DLL
314 // interface. This is a compiler-specific extension.
316  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
317 
318 #pragma warning (pop)
319 
320 #endif
321 
322 // VTK-HeaderTest-Exclude: vtkSOADataArrayTemplate.h
vtkAbstractArray::DeleteMethod
DeleteMethod
Definition: vtkAbstractArray.h:333
VTK_ZEROCOPY
#define VTK_ZEROCOPY
Definition: vtkWrappingHints.h:40
vtkGenericDataArray::ValueType
ValueTypeT ValueType
Definition: vtkGenericDataArray.h:84
vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE
Definition: vtkAbstractArray.h:337
vtkSOADataArrayTemplate::AoSCopy
vtkBuffer< ValueType > * AoSCopy
Definition: vtkSOADataArrayTemplate.h:242
vtkGenericDataArray::GetTypedComponent
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
Definition: vtkGenericDataArray.h:155
vtkTypeTraits
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:32
vtkX3D::value
Definition: vtkX3D.h:220
vtkAbstractArray::VTK_DATA_ARRAY_DELETE
Definition: vtkAbstractArray.h:336
vtkSOADataArrayTemplate
Struct-Of-Arrays implementation of vtkGenericDataArray.
Definition: vtkSOADataArrayTemplate.h:41
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:101
vtkSOADataArrayTemplate::GetTypedComponent
ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
Get component comp of the tuple at tupleIdx.
Definition: vtkSOADataArrayTemplate.h:112
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:168
vtkAbstractArray::VTK_DATA_ARRAY_FREE
Definition: vtkAbstractArray.h:335
vtkGenericDataArray
Base interface for all typed vtkDataArray subclasses.
Definition: vtkGenericDataArray.h:80
vtkDataArray
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:54
vtkAbstractArray::SoADataArrayTemplate
Definition: vtkAbstractArray.h:646
vtkSOADataArrayTemplate::FastDownCast
static vtkSOADataArrayTemplate< ValueType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
Definition: vtkSOADataArrayTemplate.h:191
vtkSOADataArrayTemplate::SelfType
vtkSOADataArrayTemplate< ValueTypeT > SelfType
Definition: vtkSOADataArrayTemplate.h:47
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:244
vtkBuffer.h
vtkGenericDataArray::AllocateTuples
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
Definition: vtkGenericDataArray.h:326
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:338
vtkSOADataArrayTemplate::SetValue
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
Definition: vtkSOADataArrayTemplate.h:78
vtkGenericDataArray::vtkTemplateTypeMacro
vtkTemplateTypeMacro(SelfType, vtkDataArray) enum
Compile time access to the VTK type identifier.
Definition: vtkGenericDataArray.h:85
vtkGenericDataArray.h
vtkIdList
list of point or cell ids
Definition: vtkIdList.h:36
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:78
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:90
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:221
vtkSOADataArrayTemplate::Data
std::vector< vtkBuffer< ValueType > * > Data
Definition: vtkSOADataArrayTemplate.h:241
vtkArrayIterator
Abstract superclass to iterate over elements in an vtkAbstractArray.
Definition: vtkArrayIterator.h:52
vtkArrayDownCast_TemplateFastCastMacro
vtkArrayDownCast_TemplateFastCastMacro(vtkTypedDataArray) template< class Scalar > inline typename vtkTypedDataArray< Scalar >
Definition: vtkTypedDataArray.h:191
vtkSOADataArrayTemplate::GetArrayType
int GetArrayType() override
Method for type-checking in FastDownCast implementations.
Definition: vtkSOADataArrayTemplate.h:211
VTK_NEWINSTANCE
#define VTK_NEWINSTANCE
Definition: vtkWrappingHints.h:39
vtkGenericDataArray::ReallocateTuples
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
Definition: vtkGenericDataArray.h:336
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:65
vtkAbstractArray::NumberOfComponents
int NumberOfComponents
Definition: vtkAbstractArray.h:689
vtkSOADataArrayTemplate::SetTypedComponent
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
Definition: vtkSOADataArrayTemplate.h:120