VTK  9.7.1
vtkSOADataArrayTemplate.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
22 #ifndef vtkSOADataArrayTemplate_h
23 #define vtkSOADataArrayTemplate_h
24 
25 #include "vtkBuffer.h"
26 #include "vtkCommonCoreModule.h" // For export macro
27 #include "vtkCompiler.h" // for VTK_USE_EXTERN_TEMPLATE
28 #include "vtkDeprecation.h" // For VTK_DEPRECATED_IN_9_7_0
29 #include "vtkGenericDataArray.h"
30 
31 // The export macro below makes no sense, but is necessary for older compilers
32 // when we export instantiations of this class from vtkCommonCore.
33 VTK_ABI_NAMESPACE_BEGIN
34 template <class ValueTypeT>
35 class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate
36 #ifndef __VTK_WRAP__
37  : public vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT,
38  vtkArrayTypes::VTK_SOA_DATA_ARRAY>
39 {
42 #else // Fake the superclass for the wrappers.
43  : public vtkDataArray
44 {
45  using GenericDataArrayType = vtkDataArray;
46 #endif
47 
48 public:
50  vtkTemplateTypeMacro(SelfType, GenericDataArrayType);
51 #ifndef __VTK_WRAP__
52  using typename Superclass::ArrayTypeTag;
53  using typename Superclass::DataTypeTag;
54  using typename Superclass::ValueType;
55 #else
56  using ValueType = ValueTypeT;
57 #endif
58 
60  {
62  VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
63  VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
64  VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
65  };
66 
67  static vtkSOADataArrayTemplate* New();
68 
70 
73  ValueType GetValue(vtkIdType valueIdx) const
74  VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
75  {
76  auto div = std::div(valueIdx, static_cast<vtkIdType>(this->NumberOfComponents));
77  return this->GetTypedComponent(div.quot, div.rem);
78  }
80 
82 
86  VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
87  {
88  auto div = std::div(valueIdx, static_cast<vtkIdType>(this->NumberOfComponents));
89  this->SetTypedComponent(div.quot, div.rem, value);
90  }
92 
96  void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
97  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples());
98 
102  void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
103  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
104  {
105  for (size_t cc = 0; cc < this->Data.size(); ++cc)
106  {
107  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc];
108  }
109  }
110 
114  ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
115  VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
116  VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents());
117 
121  void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
122  VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
123  VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents());
124 
128  void FillTypedComponent(int compIdx, ValueType value) override;
129 
143  void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size, bool updateMaxId = false,
144  bool save = false, int deleteMethod = VTK_DATA_ARRAY_FREE);
145 
146 #ifndef __VTK_WRAP__
147 
155  void SetBuffer(int comp, vtkBuffer<ValueType>* buffer, bool updateMaxId = false);
156 #endif
157 
167  void SetBuffer(int comp, vtkAbstractBuffer* buffer, bool updateMaxId = false);
168 
176  void SetArrayFreeFunction(void (*callback)(void*)) override;
177 
184  void SetArrayFreeFunction(int comp, void (*callback)(void*));
185 
190  ValueType* GetComponentArrayPointer(int comp);
191 
197 #ifdef __VTK_WRAP__
198  vtkAbstractBuffer* GetComponentBuffer(int comp);
199 #else
200  vtkBuffer<ValueTypeT>* GetComponentBuffer(int comp);
201 #endif // __VTK_WRAP__
202 
207  void* GetVoidPointer(vtkIdType valueIdx) override;
208 
213  VTK_DEPRECATED_IN_9_7_0("Use DeepCopy with an vtkAOSDataArrayTemplate array")
214  void ExportToVoidPointer(void* ptr) override;
215 
222  {
223  AOS VTK_DEPRECATED_IN_9_7_0("The storage will always be SOA going forward.") = 0,
224  SOA VTK_DEPRECATED_IN_9_7_0("The storage will always be SOA going forward.") = 1
225  };
226  VTK_DEPRECATED_IN_9_7_0("The storage will always be SOA going forward.")
227  StorageTypeEnum GetStorageType() { return static_cast<StorageTypeEnum>(1); }
228 
229  VTK_DEPRECATED_IN_9_7_0("Use vtk::DataArrayValueRange, or the array directly")
230  VTK_NEWINSTANCE vtkArrayIterator* NewIterator() override;
231  void SetNumberOfComponents(int numComps) override;
232  void ShallowCopy(vtkDataArray* other) override;
233  using vtkAbstractArray::ShallowCopy;
234 
235  // Reimplemented for efficiency:
236  void InsertTuples(
237  vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
238  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
239  // using Superclass::InsertTuples;
240  void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
241  {
242  this->Superclass::InsertTuples(dstIds, srcIds, source);
243  }
245  vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override
246  {
247  this->Superclass::InsertTuplesStartingAt(dstStart, srcIds, source);
248  }
249 
250 #ifndef __VTK_WRAP__
251  // helper method for vtkDataArray.cxx DeepCopyWorker () operator
252  void CopyData(vtkSOADataArrayTemplate<ValueType>* src);
253 #endif
254 
255 #ifdef __VTK_WRAP__
256  // Add APIs inherited from vtkGenericDataArray, which is excluded from wrapping
258 #endif
259 
260 protected:
262  ~vtkSOADataArrayTemplate() override;
263 
268  VTK_DEPRECATED_IN_9_7_0("No longer needed")
269  bool AllocateTuples(vtkIdType numTuples);
270 
275  bool ReallocateTuples(vtkIdType numTuples);
276 
278 
279 private:
281  void operator=(const vtkSOADataArrayTemplate&) = delete;
282 
284 };
285 
286 // Declare vtkArrayDownCast implementations for SoA containers:
288 
289 VTK_ABI_NAMESPACE_END
290 
291 // This macro is used by the subclasses to create dummy
292 // declarations for these functions such that the wrapper
293 // can see them. The wrappers ignore vtkSOADataArrayTemplate.
294 #define vtkCreateSOAWrappedArrayInterface(T) \
295  vtkCreateWrappedArrayReadInterface(T); \
296  vtkCreateWrappedArrayWriteInterface(T); \
297  T* GetComponentArrayPointer(int id); \
298  vtkAbstractBuffer* GetComponentBuffer(int comp); \
299  void SetArray(int comp, VTK_ZEROCOPY T* array, vtkIdType size, bool updateMaxId, bool save, \
300  int deleteMethod); \
301  void SetBuffer(int comp, vtkAbstractBuffer* buffer, bool updateMaxId);
302 
303 #endif // header guard
304 
305 // This portion must be OUTSIDE the include blockers. This is used to tell
306 // libraries other than vtkCommonCore that instantiations of
307 // vtkSOADataArrayTemplate can be found externally. This prevents each library
308 // from instantiating these on their own.
309 #ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
310 #define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
311  namespace vtkDataArrayPrivate \
312  { \
313  VTK_ABI_NAMESPACE_BEGIN \
314  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<T>, double); \
315  VTK_ABI_NAMESPACE_END \
316  } \
317  VTK_ABI_NAMESPACE_BEGIN \
318  template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate<T>; \
319  VTK_ABI_NAMESPACE_END
320 // This portion must be OUTSIDE the include blockers. This is used to tell
321 // libraries other than vtkCommonCore that instantiations of
322 // vtkSOADataArrayTemplate can be found externally. This prevents each library
323 // from instantiating these on their own.
324 #define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE_VALUERANGE(T) \
325  namespace vtkDataArrayPrivate \
326  { \
327  VTK_ABI_NAMESPACE_BEGIN \
328  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<T>, T); \
329  VTK_ABI_NAMESPACE_END \
330  }
331 // MinGW GCC does not reliably export inline member functions (such as IsTypeOf
332 // defined by vtkTemplateTypeMacro above) when using template class __declspec(dllexport)
333 // with extern template. This guard is applied here rather than in other VTK places
334 // with #elif defined(VTK_USE_EXTERN_TEMPLATE) because only these three DataArrayTemplate
335 // classes combine extern template with vtkTemplateTypeMacro inline functions in the class
336 // body; other VTK template classes using this pattern (e.g. vtkConstantArray,
337 // vtkAffineArray) use vtkImplicitArrayTypeMacro instead and were not observed with this
338 // MinGW issue.
339 #elif defined(VTK_USE_EXTERN_TEMPLATE) && !defined(__MINGW32__)
340 #ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
341 #define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
342 #ifdef _MSC_VER
343 #pragma warning(push)
344 // The following is needed when the vtkSOADataArrayTemplate is declared
345 // dllexport and is used from another class in vtkCommonCore
346 #pragma warning(disable : 4910) // extern and dllexport incompatible
347 #endif
348 VTK_ABI_NAMESPACE_BEGIN
349 vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
350 VTK_ABI_NAMESPACE_END
351 
352 namespace vtkDataArrayPrivate
353 {
354 VTK_ABI_NAMESPACE_BEGIN
355 
356 // These are instantiated in vtkGenericDataArrayValueRange${i}.cxx
358 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<unsigned long>, unsigned long)
359 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<long long>, long long)
360 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<unsigned long long>, unsigned long long)
361 // These are instantiated in vtkSOADataArrayTemplateInstantiate${i}.cxx
362 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<float>, double)
363 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<double>, double)
364 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<char>, double)
365 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<signed char>, double)
366 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<unsigned char>, double)
367 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<short>, double)
368 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<unsigned short>, double)
369 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<int>, double)
370 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<unsigned int>, double)
371 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<long>, double)
372 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<unsigned long>, double)
373 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<long long>, double)
374 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<unsigned long long>, double)
375 
376 VTK_ABI_NAMESPACE_END
377 } // namespace vtkDataArrayPrivate
378 
379 #ifdef _MSC_VER
380 #pragma warning(pop)
381 #endif
382 #endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
383 
384 // The following clause is only for MSVC 2008 and 2010
385 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
386 #pragma warning(push)
387 
388 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
389 #pragma warning(disable : 4091)
390 
391 // Compiler-specific extension warning.
392 #pragma warning(disable : 4231)
393 
394 // We need to disable warning 4910 and do an extern dllexport
395 // anyway. When deriving new arrays from an
396 // instantiation of this template the compiler does an explicit
397 // instantiation of the base class. From outside the vtkCommon
398 // library we block this using an extern dllimport instantiation.
399 // For classes inside vtkCommon we should be able to just do an
400 // extern instantiation, but VS 2008 complains about missing
401 // definitions. We cannot do an extern dllimport inside vtkCommon
402 // since the symbols are local to the dll. An extern dllexport
403 // seems to be the only way to convince VS 2008 to do the right
404 // thing, so we just disable the warning.
405 #pragma warning(disable : 4910) // extern and dllexport incompatible
406 
407 // Use an "extern explicit instantiation" to give the class a DLL
408 // interface. This is a compiler-specific extension.
409 VTK_ABI_NAMESPACE_BEGIN
410 vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
411 VTK_ABI_NAMESPACE_END
412 
413 #pragma warning(pop)
414 
415 #endif
416 
417 // VTK-HeaderTest-Exclude: vtkSOADataArrayTemplate.h
Struct-Of-Arrays implementation of vtkGenericDataArray.
#define vtkCreateGenericWrappedArrayInterface(T)
int GetNumberOfComponents() const
Set/Get the dimension (n) of the components.
Abstract superclass for all arrays.
vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
vtkArrayDownCast_TemplateFastCastMacro(vtkSOADataArrayTemplate)
#define VTK_DECLARE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType)
int vtkIdType
Definition: vtkType.h:363
Base interface for all typed vtkDataArray subclasses.
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array's tuple at tupleIdx to the values in tuple.
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
vtkIdType GetNumberOfValues() const
Get the total number of values in the array.
void SetArrayFreeFunction(void(*callback)(void *)) override
Default implementation raises a runtime error.
vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkArrayIteratorTemplate)
void save(Archiver &ar, const std::string &str, const unsigned int version)
list of point or cell ids
Definition: vtkIdList.h:25
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
Abstract superclass to iterate over elements in an vtkAbstractArray.
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
Set component compIdx of the tuple at tupleIdx to value.
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
vtkTemplateTypeMacro(SelfType, vtkDataArray)
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
#define VTK_NEWINSTANCE
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
#define VTK_ZEROCOPY
Abstract base class for vtkBuffer providing buffer protocol support.
std::integral_constant< int, vtkArrayTypes::VTK_ABSTRACT_ARRAY > ArrayTypeTag
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
#define VTK_EXPECTS(x)
#define VTK_DEPRECATED_IN_9_7_0(reason)
void InsertTuplesStartingAt(vtkIdType dstStart, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
void * GetVoidPointer(vtkIdType valueIdx) override
Default implementation raises a runtime error.
StorageTypeEnum
Because we still need to support GetVoidPointer() for both reading from and writing to memory we may ...