VTK  9.6.20260221
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 : public vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT,
37 vtkArrayTypes::VTK_SOA_DATA_ARRAY>
38{
39 using GenericDataArrayType = vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT,
41
42public:
44 vtkTemplateTypeMacro(SelfType, GenericDataArrayType);
45 using typename Superclass::ArrayTypeTag;
46 using typename Superclass::DataTypeTag;
47 using typename Superclass::ValueType;
48
56
58
60
64 VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
65 {
66 auto div = std::div(valueIdx, static_cast<vtkIdType>(this->NumberOfComponents));
67 return this->GetTypedComponent(div.quot, div.rem);
68 }
69
70
72
75 void SetValue(vtkIdType valueIdx, ValueType value)
76 VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
77 {
78 auto div = std::div(valueIdx, static_cast<vtkIdType>(this->NumberOfComponents));
79 this->SetTypedComponent(div.quot, div.rem, value);
80 }
81
82
86 void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
87 VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples());
88
92 void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
93 VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
94 {
95 for (size_t cc = 0; cc < this->Data.size(); ++cc)
96 {
97 this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc];
98 }
99 }
100
104 ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
105 VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
106 VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents());
107
111 void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
112 VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
113 VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents());
114
118 void FillTypedComponent(int compIdx, ValueType value) override;
119
133 void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size, bool updateMaxId = false,
134 bool save = false, int deleteMethod = VTK_DATA_ARRAY_FREE);
135
143 void SetArrayFreeFunction(void (*callback)(void*)) override;
144
151 void SetArrayFreeFunction(int comp, void (*callback)(void*));
152
158
164#ifdef __VTK_WRAP__
166#else
168#endif // __VTK_WRAP__
169
174 void* GetVoidPointer(vtkIdType valueIdx) override;
175
180 VTK_DEPRECATED_IN_9_7_0("Use DeepCopy with an vtkAOSDataArrayTemplate array")
181 void ExportToVoidPointer(void* ptr) override;
182
189 {
190 AOS VTK_DEPRECATED_IN_9_7_0("The storage will always be SOA going forward.") = 0,
191 SOA VTK_DEPRECATED_IN_9_7_0("The storage will always be SOA going forward.") = 1
192 };
193 VTK_DEPRECATED_IN_9_7_0("The storage will always be SOA going forward.")
194 StorageTypeEnum GetStorageType() { return static_cast<StorageTypeEnum>(1); }
195
196 VTK_DEPRECATED_IN_9_7_0("Use vtk::DataArrayValueRange, or the array directly")
198 void SetNumberOfComponents(int numComps) override;
199 void ShallowCopy(vtkDataArray* other) override;
201
202 // Reimplemented for efficiency:
204 vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
205 // MSVC doesn't like 'using' here (error C2487). Just forward instead:
206 // using Superclass::InsertTuples;
207 void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
208 {
209 this->Superclass::InsertTuples(dstIds, srcIds, source);
210 }
212 vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override
213 {
214 this->Superclass::InsertTuplesStartingAt(dstStart, srcIds, source);
215 }
216
217#ifndef __VTK_WRAP__
218 // helper method for vtkDataArray.cxx DeepCopyWorker () operator
220#endif
221
222protected:
225
230 bool AllocateTuples(vtkIdType numTuples);
231
237
238 std::vector<vtkBuffer<ValueType>*> Data;
239
240private:
242 void operator=(const vtkSOADataArrayTemplate&) = delete;
243
244 friend class vtkGenericDataArray<SelfType, ValueType, ArrayTypeTag::value>;
245};
246
247// Declare vtkArrayDownCast implementations for SoA containers:
249
250VTK_ABI_NAMESPACE_END
251
252// This macro is used by the subclasses to create dummy
253// declarations for these functions such that the wrapper
254// can see them. The wrappers ignore vtkSOADataArrayTemplate.
255#define vtkCreateSOAWrappedArrayInterface(T) \
256 int GetDataType() const override; \
257 T GetDataTypeValueMin() const; \
258 T GetDataTypeValueMax() const; \
259 void GetTypedTuple(vtkIdType i, T* tuple) VTK_EXPECTS(0 <= i && i < GetNumberOfTuples()); \
260 T GetValue(vtkIdType id) const VTK_EXPECTS(0 <= id && id < GetNumberOfValues()); \
261 T* GetValueRange(int comp) VTK_SIZEHINT(2); \
262 T* GetValueRange() VTK_SIZEHINT(2); \
263 void SetTypedTuple(vtkIdType i, const T* tuple) VTK_EXPECTS(0 <= i && i < GetNumberOfTuples()); \
264 void InsertTypedTuple(vtkIdType i, const T* tuple) VTK_EXPECTS(0 <= i); \
265 vtkIdType InsertNextTypedTuple(const T* tuple); \
266 void SetValue(vtkIdType id, T value) VTK_EXPECTS(0 <= id && id < GetNumberOfValues()); \
267 bool SetNumberOfValues(vtkIdType number) override; \
268 void InsertValue(vtkIdType id, T f) VTK_EXPECTS(0 <= id); \
269 vtkIdType InsertNextValue(T f); \
270 T* GetComponentArrayPointer(int id); \
271 vtkAbstractBuffer* GetComponentBuffer(int comp); \
272 void SetArray(int comp, VTK_ZEROCOPY T* array, vtkIdType size, bool updateMaxId, bool save, \
273 int deleteMethod);
274
275#endif // header guard
276
277// This portion must be OUTSIDE the include blockers. This is used to tell
278// libraries other than vtkCommonCore that instantiations of
279// vtkSOADataArrayTemplate can be found externally. This prevents each library
280// from instantiating these on their own.
281#ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
282#define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
283 namespace vtkDataArrayPrivate \
284 { \
285 VTK_ABI_NAMESPACE_BEGIN \
286 VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<T>, double); \
287 VTK_ABI_NAMESPACE_END \
288 } \
289 VTK_ABI_NAMESPACE_BEGIN \
290 template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate<T>; \
291 VTK_ABI_NAMESPACE_END
292
293#elif defined(VTK_USE_EXTERN_TEMPLATE)
294#ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
295#define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
296#ifdef _MSC_VER
297#pragma warning(push)
298// The following is needed when the vtkSOADataArrayTemplate is declared
299// dllexport and is used from another class in vtkCommonCore
300#pragma warning(disable : 4910) // extern and dllexport incompatible
301#endif
302VTK_ABI_NAMESPACE_BEGIN
303vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
304VTK_ABI_NAMESPACE_END
305
306namespace vtkDataArrayPrivate
307{
308VTK_ABI_NAMESPACE_BEGIN
309
310// These are instantiated in vtkSOADataArrayTemplateInstantiate${i}.cxx
324
325VTK_ABI_NAMESPACE_END
326} // namespace vtkDataArrayPrivate
327
328#ifdef _MSC_VER
329#pragma warning(pop)
330#endif
331#endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
332
333// The following clause is only for MSVC 2008 and 2010
334#elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
335#pragma warning(push)
336
337// C4091: 'extern ' : ignored on left of 'int' when no variable is declared
338#pragma warning(disable : 4091)
339
340// Compiler-specific extension warning.
341#pragma warning(disable : 4231)
342
343// We need to disable warning 4910 and do an extern dllexport
344// anyway. When deriving new arrays from an
345// instantiation of this template the compiler does an explicit
346// instantiation of the base class. From outside the vtkCommon
347// library we block this using an extern dllimport instantiation.
348// For classes inside vtkCommon we should be able to just do an
349// extern instantiation, but VS 2008 complains about missing
350// definitions. We cannot do an extern dllimport inside vtkCommon
351// since the symbols are local to the dll. An extern dllexport
352// seems to be the only way to convince VS 2008 to do the right
353// thing, so we just disable the warning.
354#pragma warning(disable : 4910) // extern and dllexport incompatible
355
356// Use an "extern explicit instantiation" to give the class a DLL
357// interface. This is a compiler-specific extension.
358VTK_ABI_NAMESPACE_BEGIN
359vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
360VTK_ABI_NAMESPACE_END
361
362#pragma warning(pop)
363
364#endif
365
366// 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: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 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 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 VTK_DECLARE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType)
#define vtkExternTemplateMacro(decl)
A macro to declare extern templates for all numerical types.
Definition vtkType.h:490
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