VTK  9.6.20260406
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
21
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.
33VTK_ABI_NAMESPACE_BEGIN
34template <class ValueTypeT>
35class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate
36#ifndef __VTK_WRAP__
37 : public vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT,
38 vtkArrayTypes::VTK_SOA_DATA_ARRAY>
39{
40 using GenericDataArrayType = vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT,
42#else // Fake the superclass for the wrappers.
43 : public vtkDataArray
44{
45 using GenericDataArrayType = vtkDataArray;
46#endif
47
48public:
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
66
68
70
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 }
79
80
82
85 void SetValue(vtkIdType valueIdx, ValueType value)
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 }
91
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__
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
191
197#ifdef __VTK_WRAP__
199#else
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")
231 void SetNumberOfComponents(int numComps) override;
232 void ShallowCopy(vtkDataArray* other) override;
234
235 // Reimplemented for efficiency:
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
253#endif
254
255#ifdef __VTK_WRAP__
256 // Add APIs inherited from vtkGenericDataArray, which is excluded from wrapping
258#endif
259
260protected:
263
268 VTK_DEPRECATED_IN_9_7_0("No longer needed")
269 bool AllocateTuples(vtkIdType numTuples);
270
276
278
279private:
281 void operator=(const vtkSOADataArrayTemplate&) = delete;
282
283 friend class vtkGenericDataArray<SelfType, ValueType, ArrayTypeTag::value>;
284};
285
286// Declare vtkArrayDownCast implementations for SoA containers:
288
289VTK_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#elif defined(VTK_USE_EXTERN_TEMPLATE)
332#ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
333#define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
334#ifdef _MSC_VER
335#pragma warning(push)
336// The following is needed when the vtkSOADataArrayTemplate is declared
337// dllexport and is used from another class in vtkCommonCore
338#pragma warning(disable : 4910) // extern and dllexport incompatible
339#endif
340VTK_ABI_NAMESPACE_BEGIN
341vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
342VTK_ABI_NAMESPACE_END
343
344namespace vtkDataArrayPrivate
345{
346VTK_ABI_NAMESPACE_BEGIN
347
348// These are instantiated in vtkGenericDataArrayValueRange${i}.cxx
353// These are instantiated in vtkSOADataArrayTemplateInstantiate${i}.cxx
367
368VTK_ABI_NAMESPACE_END
369} // namespace vtkDataArrayPrivate
370
371#ifdef _MSC_VER
372#pragma warning(pop)
373#endif
374#endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
375
376// The following clause is only for MSVC 2008 and 2010
377#elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
378#pragma warning(push)
379
380// C4091: 'extern ' : ignored on left of 'int' when no variable is declared
381#pragma warning(disable : 4091)
382
383// Compiler-specific extension warning.
384#pragma warning(disable : 4231)
385
386// We need to disable warning 4910 and do an extern dllexport
387// anyway. When deriving new arrays from an
388// instantiation of this template the compiler does an explicit
389// instantiation of the base class. From outside the vtkCommon
390// library we block this using an extern dllimport instantiation.
391// For classes inside vtkCommon we should be able to just do an
392// extern instantiation, but VS 2008 complains about missing
393// definitions. We cannot do an extern dllimport inside vtkCommon
394// since the symbols are local to the dll. An extern dllexport
395// seems to be the only way to convince VS 2008 to do the right
396// thing, so we just disable the warning.
397#pragma warning(disable : 4910) // extern and dllexport incompatible
398
399// Use an "extern explicit instantiation" to give the class a DLL
400// interface. This is a compiler-specific extension.
401VTK_ABI_NAMESPACE_BEGIN
402vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
403VTK_ABI_NAMESPACE_END
404
405#pragma warning(pop)
406
407#endif
408
409// VTK-HeaderTest-Exclude: vtkSOADataArrayTemplate.h
int GetNumberOfComponents() const
Set/Get the dimension (n) of the components.
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
virtual void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source)=0
Copy the tuples indexed in srcIds from the source array to the tuple locations indexed by dstIds in t...
virtual void InsertTuplesStartingAt(vtkIdType dstStart, vtkIdList *srcIds, vtkAbstractArray *source)=0
Copy the tuples indexed in srcIds from the source array to the tuple locations starting at index dstS...
std::integral_constant< int, VTK_OPAQUE > DataTypeTag
std::integral_constant< int, vtkArrayTypes::VTK_ABSTRACT_ARRAY > ArrayTypeTag
vtkIdType GetNumberOfValues() const
Get the total number of values in the array.
Abstract base class for vtkBuffer providing buffer protocol support.
Abstract superclass to iterate over elements in an vtkAbstractArray.
internal storage class used by vtkSOADataArrayTemplate, vtkAOSDataArrayTemplate, and others.
Definition vtkBuffer.h:32
list of point or cell ids
Definition vtkIdList.h:135
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.
void SetBuffer(int comp, vtkAbstractBuffer *buffer, bool updateMaxId=false)
Use this API to pass an existing vtkAbstractBuffer to this instance.
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 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 SetBuffer(int comp, vtkBuffer< ValueType > *buffer, bool updateMaxId=false)
Use this API to pass an existing vtkBuffer to this instance.
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< ValueTypeT > * GetComponentBuffer(int comp)
Return the underlying buffer object for a particular component.
static vtkSOADataArrayTemplate * New()
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
std::integral_constant< int, vtkArrayTypes::VTK_ABSTRACT_ARRAY > ArrayTypeTag
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
vtkSOADataArrayTemplate< ValueTypeT > SelfType
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_DEPRECATED_IN_9_7_0(reason)
#define vtkCreateGenericWrappedArrayInterface(T)
#define VTK_DECLARE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType)
#define vtkExternTemplateMacro(decl)
A macro to declare extern templates for all numerical types.
Definition vtkType.h:458
int vtkIdType
Definition vtkType.h:363
#define vtkInstantiateTemplateMacro(decl)
A macro to instantiate a template over all numerical types.
Definition vtkType.h:411
@ 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