VTK: Common/Core/vtkSOADataArrayTemplate.h Source File

| VTK 9.3.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 "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.
32 VTK_ABI_NAMESPACE_BEGIN
33 template <class ValueTypeT>
34 class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate
35 : public vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT>
36 {
37 typedef vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT> GenericDataArrayType;
38
39 public:
40 typedef vtkSOADataArrayTemplate<ValueTypeT> SelfType;
41 vtkTemplateTypeMacro(SelfType, GenericDataArrayType);
42 typedef typename Superclass::ValueType ValueType;
43
44 enum DeleteMethod
45 {
46 VTK_DATA_ARRAY_FREE = vtkAbstractArray::VTK_DATA_ARRAY_FREE,
47 VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
48 VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
49 VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
50 };
51
52 static vtkSOADataArrayTemplate* New();
53
55
58 inline ValueType GetValue(vtkIdType valueIdx) const
59 {
60 vtkIdType tupleIdx;
61 int comp;
62 this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
63 return this->GetTypedComponent(tupleIdx, comp);
64 }
66
68
71 inline void SetValue(vtkIdType valueIdx, ValueType value)
72 {
73 vtkIdType tupleIdx;
74 int comp;
75 this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
76 this->SetTypedComponent(tupleIdx, comp, value);
77 }
79
83 inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
84 {
85 if (this->StorageType == StorageTypeEnum::SOA)
86 {
87 for (size_t cc = 0; cc < this->Data.size(); cc++)
88 {
89 tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx];
90 }
91 }
92 else
93 {
94 ValueType* buffer = this->AoSData->GetBuffer();
95 std::copy(buffer + tupleIdx * this->GetNumberOfComponents(),
96 buffer + (tupleIdx + 1) * this->GetNumberOfComponents(), tuple);
97 }
98 }
99
103 inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
104 {
105 if (this->StorageType == StorageTypeEnum::SOA)
106 {
107 for (size_t cc = 0; cc < this->Data.size(); ++cc)
108 {
109 this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc];
110 }
111 }
112 else
113 {
114 ValueType* buffer = this->AoSData->GetBuffer();
115 std::copy(tuple, tuple + this->GetNumberOfComponents(),
116 buffer + tupleIdx * this->GetNumberOfComponents());
117 }
118 }
119
123 inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
124 {
125 if (this->StorageType == StorageTypeEnum::SOA)
126 {
127 return this->Data[comp]->GetBuffer()[tupleIdx];
128 }
129 return this->AoSData->GetBuffer()[tupleIdx * this->GetNumberOfComponents() + comp];
130 }
131
135 inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
136 {
137 if (this->StorageType == StorageTypeEnum::SOA)
138 {
139 this->Data[comp]->GetBuffer()[tupleIdx] = value;
140 }
141 else
142 {
143 this->AoSData->GetBuffer()[tupleIdx * this->GetNumberOfComponents() + comp] = value;
144 }
145 }
146
150 void FillTypedComponent(int compIdx, ValueType value) override;
151
165 void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size, bool updateMaxId = false,
166 bool save = false, int deleteMethod = VTK_DATA_ARRAY_FREE);
167
175 void SetArrayFreeFunction(void (*callback)(void*)) override;
176
183 void SetArrayFreeFunction(int comp, void (*callback)(void*));
184
189 ValueType* GetComponentArrayPointer(int comp);
190
195 void* GetVoidPointer(vtkIdType valueIdx) override;
196
201 void ExportToVoidPointer(void* ptr) override;
202
203 #ifndef __VTK_WRAP__
205
211 static vtkSOADataArrayTemplate<ValueType>* FastDownCast(vtkAbstractArray* source);
213 #endif
214
215 int GetArrayType() const override { return vtkAbstractArray::SoADataArrayTemplate; }
216 VTK_NEWINSTANCE vtkArrayIterator* NewIterator() override;
217 void SetNumberOfComponents(int numComps) override;
218 void ShallowCopy(vtkDataArray* other) override;
219
220 // Reimplemented for efficiency:
221 void InsertTuples(
222 vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
223 // MSVC doesn't like 'using' here (error C2487). Just forward instead:
224 // using Superclass::InsertTuples;
225 void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
226 {
227 this->Superclass::InsertTuples(dstIds, srcIds, source);
228 }
229 void InsertTuplesStartingAt(
230 vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override
231 {
232 this->Superclass::InsertTuplesStartingAt(dstStart, srcIds, source);
233 }
234
235 #ifndef __VTK_WRAP__
236 // helper method for vtkDataArray.cxx DeepCopyWorker () operator
237 void CopyData(vtkSOADataArrayTemplate<ValueType>* src);
238 #endif
239
240 protected:
241 vtkSOADataArrayTemplate();
242 ~vtkSOADataArrayTemplate() override;
243
248 bool AllocateTuples(vtkIdType numTuples);
249
254 bool ReallocateTuples(vtkIdType numTuples);
255
256 std::vector<vtkBuffer<ValueType>*> Data;
257 vtkBuffer<ValueType>* AoSData;
258
264 enum StorageTypeEnum
265 {
266 AOS,
267 SOA
268 };
269 StorageTypeEnum StorageType;
270
271 void ClearSOAData();
272
273 private:
274 vtkSOADataArrayTemplate(const vtkSOADataArrayTemplate&) = delete;
275 void operator=(const vtkSOADataArrayTemplate&) = delete;
276
277 inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx, vtkIdType& tupleIdx, int& comp) const
278 {
279 tupleIdx = valueIdx / this->NumberOfComponents;
280 comp = valueIdx % this->NumberOfComponents;
281 }
282
283 friend class vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT>;
284 };
285
286 // Declare vtkArrayDownCast implementations for SoA containers:
287 vtkArrayDownCast_TemplateFastCastMacro(vtkSOADataArrayTemplate);
288
289 VTK_ABI_NAMESPACE_END
290 #endif // header guard
291
292 // This portion must be OUTSIDE the include blockers. This is used to tell
293 // libraries other than vtkCommonCore that instantiations of
294 // vtkSOADataArrayTemplate can be found externally. This prevents each library
295 // from instantiating these on their own.
296 #ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
297 #define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
298 namespace vtkDataArrayPrivate \
299 { \
300 VTK_ABI_NAMESPACE_BEGIN \
301 VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<T>, double); \
302 VTK_ABI_NAMESPACE_END \
303 } \
304 VTK_ABI_NAMESPACE_BEGIN \
305 template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate<T>; \
306 VTK_ABI_NAMESPACE_END
307
308 #elif defined(VTK_USE_EXTERN_TEMPLATE)
309 #ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
310 #define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
311 #ifdef _MSC_VER
312 #pragma warning(push)
313 // The following is needed when the vtkSOADataArrayTemplate is declared
314 // dllexport and is used from another class in vtkCommonCore
315 #pragma warning(disable : 4910) // extern and dllexport incompatible
316 #endif
317 VTK_ABI_NAMESPACE_BEGIN
318 vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
319 #ifdef _MSC_VER
320 #pragma warning(pop)
321 #endif
322 VTK_ABI_NAMESPACE_END
323 #endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
324
325 // The following clause is only for MSVC 2008 and 2010
326 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
327 #pragma warning(push)
328
329 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
330 #pragma warning(disable : 4091)
331
332 // Compiler-specific extension warning.
333 #pragma warning(disable : 4231)
334
335 // We need to disable warning 4910 and do an extern dllexport
336 // anyway. When deriving new arrays from an
337 // instantiation of this template the compiler does an explicit
338 // instantiation of the base class. From outside the vtkCommon
339 // library we block this using an extern dllimport instantiation.
340 // For classes inside vtkCommon we should be able to just do an
341 // extern instantiation, but VS 2008 complains about missing
342 // definitions. We cannot do an extern dllimport inside vtkCommon
343 // since the symbols are local to the dll. An extern dllexport
344 // seems to be the only way to convince VS 2008 to do the right
345 // thing, so we just disable the warning.
346 #pragma warning(disable : 4910) // extern and dllexport incompatible
347
348 // Use an "extern explicit instantiation" to give the class a DLL
349 // interface. This is a compiler-specific extension.
350 VTK_ABI_NAMESPACE_BEGIN
351 vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
352
353 #pragma warning(pop)
354
355 VTK_ABI_NAMESPACE_END
356 #endif
357
358 // VTK-HeaderTest-Exclude: vtkSOADataArrayTemplate.h
vtkAbstractArray
Abstract superclass for all arrays.
Definition: vtkAbstractArray.h:76
vtkAbstractArray::GetNumberOfComponents
int GetNumberOfComponents() const
Set/Get the dimension (n) of the components.
Definition: vtkAbstractArray.h:127
vtkAbstractArray::DeleteMethod
DeleteMethod
Definition: vtkAbstractArray.h:330
vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
@ VTK_DATA_ARRAY_USER_DEFINED
Definition: vtkAbstractArray.h:334
vtkAbstractArray::VTK_DATA_ARRAY_FREE
@ VTK_DATA_ARRAY_FREE
Definition: vtkAbstractArray.h:331
vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE
@ VTK_DATA_ARRAY_ALIGNED_FREE
Definition: vtkAbstractArray.h:333
vtkAbstractArray::VTK_DATA_ARRAY_DELETE
@ VTK_DATA_ARRAY_DELETE
Definition: vtkAbstractArray.h:332
vtkAbstractArray::NumberOfComponents
int NumberOfComponents
Definition: vtkAbstractArray.h:697
vtkAbstractArray::SoADataArrayTemplate
@ SoADataArrayTemplate
Definition: vtkAbstractArray.h:650
vtkArrayIterator
Abstract superclass to iterate over elements in an vtkAbstractArray.
Definition: vtkArrayIterator.h:42
vtkBuffer< ValueType >
vtkDataArray
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:54
vtkGenericDataArray
Base interface for all typed vtkDataArray subclasses.
Definition: vtkGenericDataArray.h:81
vtkIdList
list of point or cell ids
Definition: vtkIdList.h:32
vtkSOADataArrayTemplate
Struct-Of-Arrays implementation of vtkGenericDataArray.
Definition: vtkSOADataArrayTemplate.h:36
vtkSOADataArrayTemplate::ShallowCopy
void ShallowCopy(vtkDataArray *other) override
Create a shallow copy of other into this, if possible.
vtkSOADataArrayTemplate::ExportToVoidPointer
void ExportToVoidPointer(void *ptr) override
Export a copy of the data in AoS ordering to the preallocated memory buffer.
vtkSOADataArrayTemplate::ValueType
Superclass::ValueType ValueType
Definition: vtkSOADataArrayTemplate.h:42
vtkSOADataArrayTemplate::GetVoidPointer
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...
vtkSOADataArrayTemplate::NewIterator
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
vtkSOADataArrayTemplate::GetTypedComponent
ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
Get component comp of the tuple at tupleIdx.
Definition: vtkSOADataArrayTemplate.h:123
vtkSOADataArrayTemplate::ReallocateTuples
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
vtkSOADataArrayTemplate::SetValue
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
Definition: vtkSOADataArrayTemplate.h:71
vtkSOADataArrayTemplate::vtkTemplateTypeMacro
vtkTemplateTypeMacro(SelfType, GenericDataArrayType)
vtkSOADataArrayTemplate::InsertTuples
void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
Definition: vtkSOADataArrayTemplate.h:225
vtkSOADataArrayTemplate::FillTypedComponent
void FillTypedComponent(int compIdx, ValueType value) override
Set component comp of all tuples to value.
vtkSOADataArrayTemplate::InsertTuples
void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray *source) override
See documentation from parent class.
vtkSOADataArrayTemplate::SetArrayFreeFunction
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...
vtkSOADataArrayTemplate::SetTypedComponent
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
Definition: vtkSOADataArrayTemplate.h:135
vtkSOADataArrayTemplate::GetValue
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
Definition: vtkSOADataArrayTemplate.h:58
vtkSOADataArrayTemplate::~vtkSOADataArrayTemplate
~vtkSOADataArrayTemplate() override
vtkSOADataArrayTemplate::InsertTuplesStartingAt
void InsertTuplesStartingAt(vtkIdType dstStart, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
Definition: vtkSOADataArrayTemplate.h:229
vtkSOADataArrayTemplate::AoSData
vtkBuffer< ValueType > * AoSData
Definition: vtkSOADataArrayTemplate.h:257
vtkSOADataArrayTemplate::vtkSOADataArrayTemplate
vtkSOADataArrayTemplate()
vtkSOADataArrayTemplate::AllocateTuples
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
vtkSOADataArrayTemplate::SelfType
vtkSOADataArrayTemplate< ValueTypeT > SelfType
Definition: vtkSOADataArrayTemplate.h:40
vtkSOADataArrayTemplate::StorageTypeEnum
StorageTypeEnum
Because we still need to support GetVoidPointer() for both reading from and writing to memory we may ...
Definition: vtkSOADataArrayTemplate.h:265
vtkSOADataArrayTemplate::AOS
@ AOS
Definition: vtkSOADataArrayTemplate.h:266
vtkSOADataArrayTemplate::FastDownCast
static vtkSOADataArrayTemplate< ValueType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
vtkSOADataArrayTemplate::GetTypedTuple
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
Definition: vtkSOADataArrayTemplate.h:83
vtkSOADataArrayTemplate::ClearSOAData
void ClearSOAData()
vtkSOADataArrayTemplate::Data
std::vector< vtkBuffer< ValueType > * > Data
Definition: vtkSOADataArrayTemplate.h:256
vtkSOADataArrayTemplate::SetArray
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.
vtkSOADataArrayTemplate::New
static vtkSOADataArrayTemplate * New()
vtkSOADataArrayTemplate::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...
vtkSOADataArrayTemplate::SetTypedTuple
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array's tuple at tupleIdx to the values in tuple.
Definition: vtkSOADataArrayTemplate.h:103
vtkSOADataArrayTemplate::StorageType
StorageTypeEnum StorageType
Definition: vtkSOADataArrayTemplate.h:269
vtkSOADataArrayTemplate::GetComponentArrayPointer
ValueType * GetComponentArrayPointer(int comp)
Return a pointer to a contiguous block of memory containing all values for a particular components (i...
vtkSOADataArrayTemplate::CopyData
void CopyData(vtkSOADataArrayTemplate< ValueType > *src)
vtkSOADataArrayTemplate::GetArrayType
int GetArrayType() const override
Method for type-checking in FastDownCast implementations.
Definition: vtkSOADataArrayTemplate.h:215
vtkSOADataArrayTemplate::SetNumberOfComponents
void SetNumberOfComponents(int numComps) override
Set/Get the dimension (n) of the components.
vtkGenericDataArray::SetTypedComponent
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
Set component compIdx of the tuple at tupleIdx to value.
Definition: vtkGenericDataArray.h:170
vtkGenericDataArray::GetTypedComponent
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
Definition: vtkGenericDataArray.h:159
vtkX3D::value
@ value
Definition: vtkX3D.h:220
vtkX3D::size
@ size
Definition: vtkX3D.h:253
vtkInstantiateTemplateMacro
vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkArrayIteratorTemplate)
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:964
vtkBuffer.h
vtkCompiler.h
vtkGenericDataArray.h
vtkExternTemplateMacro
vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
vtkArrayDownCast_TemplateFastCastMacro
vtkArrayDownCast_TemplateFastCastMacro(vtkSOADataArrayTemplate)
vtkIdType
int vtkIdType
Definition: vtkType.h:315
save
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))
Definition: vtkVariantBoostSerialization.h:46
VTK_ZEROCOPY
#define VTK_ZEROCOPY
Definition: vtkWrappingHints.h:33
VTK_NEWINSTANCE
#define VTK_NEWINSTANCE
Definition: vtkWrappingHints.h:32