VTK  9.5.20251103
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 "vtkGenericDataArray.h"
29
30// The export macro below makes no sense, but is necessary for older compilers
31// when we export instantiations of this class from vtkCommonCore.
32VTK_ABI_NAMESPACE_BEGIN
33template <class ValueTypeT>
34class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate
35 : public vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT,
36 vtkArrayTypes::VTK_SOA_DATA_ARRAY>
37{
40
41public:
44 using typename Superclass::ArrayTypeTag;
45 using typename Superclass::DataTypeTag;
46 using typename Superclass::ValueType;
47
49 {
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
57
59
63 VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
64 {
65 vtkIdType tupleIdx;
66 int comp;
67 this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
68 return this->GetTypedComponent(tupleIdx, comp);
69 }
71
73
76 void SetValue(vtkIdType valueIdx, ValueType value)
77 VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
78 {
79 vtkIdType tupleIdx;
80 int comp;
81 this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
82 this->SetTypedComponent(tupleIdx, comp, value);
83 }
85
89 void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
90 VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
91 {
92 if (this->StorageType == StorageTypeEnum::SOA)
93 {
94 for (size_t cc = 0; cc < this->Data.size(); cc++)
95 {
96 tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx];
97 }
98 }
99 else
100 {
101 ValueType* buffer = this->AoSData->GetBuffer();
102 std::copy(buffer + tupleIdx * this->GetNumberOfComponents(),
103 buffer + (tupleIdx + 1) * this->GetNumberOfComponents(), tuple);
104 }
105 }
106
110 void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
111 VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
112 {
113 if (this->StorageType == StorageTypeEnum::SOA)
114 {
115 for (size_t cc = 0; cc < this->Data.size(); ++cc)
116 {
117 this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc];
118 }
119 }
120 else
121 {
122 ValueType* buffer = this->AoSData->GetBuffer();
123 std::copy(tuple, tuple + this->GetNumberOfComponents(),
124 buffer + tupleIdx * this->GetNumberOfComponents());
125 }
126 }
127
131 ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
132 VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
133 VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents())
134 {
135 if (this->StorageType == StorageTypeEnum::SOA)
136 {
137 return this->Data[comp]->GetBuffer()[tupleIdx];
138 }
139 return this->AoSData->GetBuffer()[tupleIdx * this->GetNumberOfComponents() + comp];
140 }
141
145 void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
146 VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
147 VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents())
148 {
149 if (this->StorageType == StorageTypeEnum::SOA)
150 {
151 this->Data[comp]->GetBuffer()[tupleIdx] = value;
152 }
153 else
154 {
155 this->AoSData->GetBuffer()[tupleIdx * this->GetNumberOfComponents() + comp] = value;
156 }
157 }
158
162 void FillTypedComponent(int compIdx, ValueType value) override;
163
177 void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size, bool updateMaxId = false,
178 bool save = false, int deleteMethod = VTK_DATA_ARRAY_FREE);
179
187 void SetArrayFreeFunction(void (*callback)(void*)) override;
188
195 void SetArrayFreeFunction(int comp, void (*callback)(void*));
196
202
207 void* GetVoidPointer(vtkIdType valueIdx) override;
208
213 void ExportToVoidPointer(void* ptr) override;
214
221 {
223 SOA
224 };
225 StorageTypeEnum GetStorageType() { return this->StorageType; }
226
228 void SetNumberOfComponents(int numComps) override;
229 void ShallowCopy(vtkDataArray* other) override;
230
231 // Reimplemented for efficiency:
233 vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
234 // MSVC doesn't like 'using' here (error C2487). Just forward instead:
235 // using Superclass::InsertTuples;
236 void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
237 {
238 this->Superclass::InsertTuples(dstIds, srcIds, source);
239 }
241 vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override
242 {
243 this->Superclass::InsertTuplesStartingAt(dstStart, srcIds, source);
244 }
245
246#ifndef __VTK_WRAP__
247 // helper method for vtkDataArray.cxx DeepCopyWorker () operator
249#endif
250
251protected:
254
259 bool AllocateTuples(vtkIdType numTuples);
260
266
267 std::vector<vtkBuffer<ValueType>*> Data;
269
271
273
274private:
276 void operator=(const vtkSOADataArrayTemplate&) = delete;
277
278 void GetTupleIndexFromValueIndex(vtkIdType valueIdx, vtkIdType& tupleIdx, int& comp) const
279 {
280 tupleIdx = valueIdx / this->NumberOfComponents;
281 comp = valueIdx % this->NumberOfComponents;
282 }
283
284 friend class vtkGenericDataArray<SelfType, ValueType, ArrayTypeTag::value>;
285};
286
287// Declare vtkArrayDownCast implementations for SoA containers:
289
290VTK_ABI_NAMESPACE_END
291
292// This macro is used by the subclasses to create dummy
293// declarations for these functions such that the wrapper
294// can see them. The wrappers ignore vtkSOADataArrayTemplate.
295#define vtkCreateSOAWrappedArrayInterface(T) \
296 int GetDataType() const override; \
297 T GetDataTypeValueMin() const; \
298 T GetDataTypeValueMax() const; \
299 void GetTypedTuple(vtkIdType i, T* tuple) VTK_EXPECTS(0 <= i && i < GetNumberOfTuples()); \
300 T GetValue(vtkIdType id) const VTK_EXPECTS(0 <= id && id < GetNumberOfValues()); \
301 T* GetValueRange(int comp) VTK_SIZEHINT(2); \
302 T* GetValueRange() VTK_SIZEHINT(2); \
303 void SetTypedTuple(vtkIdType i, const T* tuple) VTK_EXPECTS(0 <= i && i < GetNumberOfTuples()); \
304 void InsertTypedTuple(vtkIdType i, const T* tuple) VTK_EXPECTS(0 <= i); \
305 vtkIdType InsertNextTypedTuple(const T* tuple); \
306 void SetValue(vtkIdType id, T value) VTK_EXPECTS(0 <= id && id < GetNumberOfValues()); \
307 bool SetNumberOfValues(vtkIdType number) override; \
308 void InsertValue(vtkIdType id, T f) VTK_EXPECTS(0 <= id); \
309 vtkIdType InsertNextValue(T f); \
310 T* GetComponentArrayPointer(int id); \
311 void SetArray(int comp, VTK_ZEROCOPY T* array, vtkIdType size, bool updateMaxId, bool save, \
312 int deleteMethod);
313
314#endif // header guard
315
316// This portion must be OUTSIDE the include blockers. This is used to tell
317// libraries other than vtkCommonCore that instantiations of
318// vtkSOADataArrayTemplate can be found externally. This prevents each library
319// from instantiating these on their own.
320#ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
321#define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
322 namespace vtkDataArrayPrivate \
323 { \
324 VTK_ABI_NAMESPACE_BEGIN \
325 VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<T>, double); \
326 VTK_ABI_NAMESPACE_END \
327 } \
328 VTK_ABI_NAMESPACE_BEGIN \
329 template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate<T>; \
330 VTK_ABI_NAMESPACE_END
331
332#elif defined(VTK_USE_EXTERN_TEMPLATE)
333#ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
334#define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
335#ifdef _MSC_VER
336#pragma warning(push)
337// The following is needed when the vtkSOADataArrayTemplate is declared
338// dllexport and is used from another class in vtkCommonCore
339#pragma warning(disable : 4910) // extern and dllexport incompatible
340#endif
341VTK_ABI_NAMESPACE_BEGIN
342vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
343VTK_ABI_NAMESPACE_END
344
345namespace vtkDataArrayPrivate
346{
347VTK_ABI_NAMESPACE_BEGIN
348
349// These are instantiated in vtkSOADataArrayTemplateInstantiate${i}.cxx
363
364VTK_ABI_NAMESPACE_END
365} // namespace vtkDataArrayPrivate
366
367#ifdef _MSC_VER
368#pragma warning(pop)
369#endif
370#endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
371
372// The following clause is only for MSVC 2008 and 2010
373#elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
374#pragma warning(push)
375
376// C4091: 'extern ' : ignored on left of 'int' when no variable is declared
377#pragma warning(disable : 4091)
378
379// Compiler-specific extension warning.
380#pragma warning(disable : 4231)
381
382// We need to disable warning 4910 and do an extern dllexport
383// anyway. When deriving new arrays from an
384// instantiation of this template the compiler does an explicit
385// instantiation of the base class. From outside the vtkCommon
386// library we block this using an extern dllimport instantiation.
387// For classes inside vtkCommon we should be able to just do an
388// extern instantiation, but VS 2008 complains about missing
389// definitions. We cannot do an extern dllimport inside vtkCommon
390// since the symbols are local to the dll. An extern dllexport
391// seems to be the only way to convince VS 2008 to do the right
392// thing, so we just disable the warning.
393#pragma warning(disable : 4910) // extern and dllexport incompatible
394
395// Use an "extern explicit instantiation" to give the class a DLL
396// interface. This is a compiler-specific extension.
397VTK_ABI_NAMESPACE_BEGIN
398vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
399VTK_ABI_NAMESPACE_END
400
401#pragma warning(pop)
402
403#endif
404
405// VTK-HeaderTest-Exclude: vtkSOADataArrayTemplate.h
Abstract superclass for all arrays.
std::integral_constant< int, VTK_OPAQUE > DataTypeTag
std::integral_constant< int, vtkArrayTypes::VTK_ABSTRACT_ARRAY > ArrayTypeTag
Abstract superclass to iterate over elements in an vtkAbstractArray.
internal storage class used by vtkSOADataArrayTemplate, vtkAOSDataArrayTemplate, and others.
Definition vtkBuffer.h:30
abstract superclass for arrays of numeric data
Base interface for all typed vtkDataArray subclasses.
list of point or cell ids
Definition vtkIdList.h:133
Struct-Of-Arrays implementation of vtkGenericDataArray.
void ShallowCopy(vtkDataArray *other) override
Create a shallow copy of other into this, if possible.
void ExportToVoidPointer(void *ptr) override
Export a copy of the data in AoS ordering to the preallocated memory buffer.
ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
Get component comp of the tuple at tupleIdx.
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
vtkTemplateTypeMacro(SelfType, GenericDataArrayType)
void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
void FillTypedComponent(int compIdx, ValueType value) override
Set component comp of all tuples to value.
void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray *source) override
See documentation from parent class.
void SetArrayFreeFunction(int comp, void(*callback)(void *))
This method allows the user to specify a custom free function to be called when the array is dealloca...
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
~vtkSOADataArrayTemplate() override
ValueType * GetComponentArrayPointer(int comp)
Return a pointer to a contiguous block of memory containing all values for a particular components (i...
void * GetVoidPointer(vtkIdType valueIdx) override
Use of this method is discouraged, it creates a deep copy of the data into a contiguous AoS-ordered b...
void InsertTuplesStartingAt(vtkIdType dstStart, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
vtkBuffer< ValueType > * AoSData
static vtkSOADataArrayTemplate * New()
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
StorageTypeEnum
Because we still need to support GetVoidPointer() for both reading from and writing to memory we may ...
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
std::vector< vtkBuffer< ValueType > * > Data
void SetArray(int comp, ValueType *array, vtkIdType size, bool updateMaxId=false, bool save=false, int deleteMethod=VTK_DATA_ARRAY_FREE)
Use this API to pass externally allocated memory to this instance.
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...
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array's tuple at tupleIdx to the values in tuple.
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
void CopyData(vtkSOADataArrayTemplate< ValueType > *src)
void SetNumberOfComponents(int numComps) override
Set/Get the dimension (n) of the components.
#define vtkArrayDownCast_TemplateFastCastMacro(ArrayT)
Same as vtkArrayDownCast_FastCastMacro, but treats ArrayT as a single-parameter template (the paramet...
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
#define VTK_DECLARE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType)
#define vtkExternTemplateMacro(decl)
A macro to declare extern templates for all numerical types.
Definition vtkType.h:492
int vtkIdType
Definition vtkType.h:367
#define vtkInstantiateTemplateMacro(decl)
A macro to instantiate a template over all numerical types.
Definition vtkType.h:415
@ VTK_SOA_DATA_ARRAY
Definition vtkType.h:84
void save(Archiver &ar, const std::string &str, const unsigned int version)
#define VTK_EXPECTS(x)
#define VTK_ZEROCOPY
#define VTK_NEWINSTANCE