VTK  9.0.1
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
Struct-Of-Arrays implementation of vtkGenericDataArray.
vtkBuffer< ValueType > * AoSCopy
ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
Get component comp of the tuple at tupleIdx.
virtual void ShallowCopy(vtkDataArray *other)
Create a shallow copy of other into this, if possible.
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
Abstract superclass for all arrays.
static vtkSOADataArrayTemplate< ValueType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
void SetArrayFreeFunction(void(*callback)(void *)) override
Default implementation raises a runtime error.
int GetArrayType() const override
Method for type-checking in FastDownCast implementations.
vtkArrayDownCast_TemplateFastCastMacro(vtkSOADataArrayTemplate)
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
Set component compIdx of the tuple at tupleIdx to value.
int vtkIdType
Definition: vtkType.h:338
Base interface for all typed vtkDataArray subclasses.
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array&#39;s tuple at tupleIdx to the values in tuple.
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph *>::edge_descriptor e, vtkGraph *)
#define vtkExternTemplateMacro(decl)
A macro to declare extern templates for all numerical types.
Definition: vtkType.h:403
list of point or cell ids
Definition: vtkIdList.h:30
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
vtkSOADataArrayTemplate< ValueTypeT > SelfType
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:49
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...
vtkTemplateTypeMacro(SelfType, vtkDataArray)
Abstract superclass to iterate over elements in an vtkAbstractArray.
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
#define VTK_NEWINSTANCE
#define vtkInstantiateTemplateMacro(decl)
A macro to instantiate a template over all numerical types.
Definition: vtkType.h:384
void * GetVoidPointer(vtkIdType valueIdx) override
Default implementation raises a runtime error.
void save(Archiver &ar, const vtkUnicodeString &str, const unsigned int vtkNotUsed(version))
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
Superclass::ValueType ValueType
#define VTK_ZEROCOPY
std::vector< vtkBuffer< ValueType > * > Data
virtual void FillTypedComponent(int compIdx, ValueType value)
Set component comp of all tuples to value.
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...
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
void SetNumberOfComponents(int num) override
Set/Get the dimension (n) of the components.
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:30
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
virtual void ExportToVoidPointer(void *out_ptr)
This method copies the array data to the void pointer specified by the user.