VTK  9.5.20251031
vtkScaledSOADataArrayTemplate.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
24#ifndef vtkScaledSOADataArrayTemplate_h
25#define vtkScaledSOADataArrayTemplate_h
26
27#include "vtkBuffer.h"
28#include "vtkBuild.h" // For VTK_BUILD_SHARED_LIBS
29#include "vtkCommonCoreModule.h" // For export macro
30#include "vtkCompiler.h" // for VTK_USE_EXTERN_TEMPLATE
31#include "vtkGenericDataArray.h"
32
33// The export macro below makes no sense, but is necessary for older compilers
34// when we export instantiations of this class from vtkCommonCore.
35VTK_ABI_NAMESPACE_BEGIN
36template <class ValueTypeT>
37class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate
38 : public vtkGenericDataArray<vtkScaledSOADataArrayTemplate<ValueTypeT>, ValueTypeT,
39 vtkArrayTypes::VTK_SCALED_SOA_DATA_ARRAY>
40{
43
44public:
47 using typename Superclass::ArrayTypeTag;
48 using typename Superclass::DataTypeTag;
49 using typename Superclass::ValueType;
50
52 {
55 VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
56 VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
57 };
58
60
62
65 void SetScale(ValueType scale)
66 {
67 if (scale != this->Scale)
68 {
69 if (scale == 0)
70 {
71 vtkErrorMacro("Cannot set Scale to 0");
72 }
73 else
74 {
75 this->Scale = scale;
76 this->Modified();
77 }
78 }
79 }
80 ValueType GetScale() const { return this->Scale; }
82
84
88 VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
89 {
90 vtkIdType tupleIdx;
91 int comp;
92 this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
93 return this->GetTypedComponent(tupleIdx, comp);
94 }
96
98
101 void SetValue(vtkIdType valueIdx, ValueType value)
102 VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
103 {
104 vtkIdType tupleIdx;
105 int comp;
106 this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
107 this->SetTypedComponent(tupleIdx, comp, value);
108 }
110
114 void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
115 VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
116 {
117 for (size_t cc = 0; cc < this->Data.size(); cc++)
118 {
119 tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx] * this->Scale;
120 }
121 }
122
126 void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
127 VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
128 {
129 for (size_t cc = 0; cc < this->Data.size(); ++cc)
130 {
131 this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc] / this->Scale;
132 }
133 }
134
138 ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
139 VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
140 VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents())
141 {
142 return this->Data[comp]->GetBuffer()[tupleIdx] * this->Scale;
143 }
144
148 void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
149 VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
150 VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents())
151 {
152 this->Data[comp]->GetBuffer()[tupleIdx] = value / this->Scale;
153 }
154
158 void FillTypedComponent(int compIdx, ValueType value) override;
159
173 void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size, bool updateMaxId = false,
174 bool save = false, int deleteMethod = VTK_DATA_ARRAY_FREE);
175
183 void SetArrayFreeFunction(void (*callback)(void*)) override;
184
191 void SetArrayFreeFunction(int comp, void (*callback)(void*));
192
199
204 void* GetVoidPointer(vtkIdType valueIdx) override;
205
210 void ExportToVoidPointer(void* ptr) override;
211
213 void SetNumberOfComponents(int numComps) override;
214 void ShallowCopy(vtkDataArray* other) override;
215
216 // Reimplemented for efficiency:
218 vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
219 // MSVC doesn't like 'using' here (error C2487). Just forward instead:
220 // using Superclass::InsertTuples;
221 void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
222 {
223 this->Superclass::InsertTuples(dstIds, srcIds, source);
224 }
226 vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override
227 {
228 this->Superclass::InsertTuplesStartingAt(dstStart, srcIds, source);
229 }
230
231protected:
234
239 bool AllocateTuples(vtkIdType numTuples);
240
246
247 std::vector<vtkBuffer<ValueType>*> Data;
249
250private:
252 void operator=(const vtkScaledSOADataArrayTemplate&) = delete;
253
254 void GetTupleIndexFromValueIndex(vtkIdType valueIdx, vtkIdType& tupleIdx, int& comp) const
255 {
256 tupleIdx = valueIdx / this->NumberOfComponents;
257 comp = valueIdx % this->NumberOfComponents;
258 }
259
260 friend class vtkGenericDataArray<SelfType, ValueType, ArrayTypeTag::value>;
264 ValueType Scale;
265};
266
267// Declare vtkArrayDownCast implementations for scaled SoA containers:
268vtkArrayDownCast_TemplateFastCastMacro(vtkScaledSOADataArrayTemplate);
269
270VTK_ABI_NAMESPACE_END
271
272// This macro is used by the subclasses to create dummy
273// declarations for these functions such that the wrapper
274// can see them. The wrappers ignore vtkScaledSOADataArrayTemplate.
275#define vtkCreateScaledSOAWrappedArrayInterface(T) \
276 int GetDataType() const override; \
277 T GetDataTypeValueMin() const; \
278 T GetDataTypeValueMax() const; \
279 void GetTypedTuple(vtkIdType i, T* tuple) VTK_EXPECTS(0 <= i && i < GetNumberOfTuples()); \
280 T GetValue(vtkIdType id) const VTK_EXPECTS(0 <= id && id < GetNumberOfValues()); \
281 T* GetValueRange(int comp) VTK_SIZEHINT(2); \
282 T* GetValueRange() VTK_SIZEHINT(2); \
283 void SetTypedTuple(vtkIdType i, const T* tuple) VTK_EXPECTS(0 <= i && i < GetNumberOfTuples()); \
284 void InsertTypedTuple(vtkIdType i, const T* tuple) VTK_EXPECTS(0 <= i); \
285 vtkIdType InsertNextTypedTuple(const T* tuple); \
286 void SetValue(vtkIdType id, T value) VTK_EXPECTS(0 <= id && id < GetNumberOfValues()); \
287 bool SetNumberOfValues(vtkIdType number) override; \
288 void InsertValue(vtkIdType id, T f) VTK_EXPECTS(0 <= id); \
289 vtkIdType InsertNextValue(T f); \
290 T* GetComponentArrayPointer(int id); \
291 void SetArray(int comp, VTK_ZEROCOPY T* array, vtkIdType size, bool updateMaxId, bool save, \
292 int deleteMethod);
293
294#endif // header guard
295
296// This portion must be OUTSIDE the include blockers. This is used to tell
297// libraries other than vtkCommonCore that instantiations of
298// vtkScaledSOADataArrayTemplate can be found externally. This prevents each library
299// from instantiating these on their own.
300#ifdef VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
301#define VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
302 namespace vtkDataArrayPrivate \
303 { \
304 VTK_ABI_NAMESPACE_BEGIN \
305 VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkScaledSOADataArrayTemplate<T>, double); \
306 VTK_ABI_NAMESPACE_END \
307 } \
308 VTK_ABI_NAMESPACE_BEGIN \
309 template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate<T>; \
310 VTK_ABI_NAMESPACE_END
311
312#elif defined(VTK_USE_EXTERN_TEMPLATE)
313#ifndef VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
314#define VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
315#ifdef _MSC_VER
316#pragma warning(push)
317// The following is needed when the vtkScaledSOADataArrayTemplate is declared
318// dllexport and is used from another class in vtkCommonCore
319#pragma warning(disable : 4910) // extern and dllexport incompatible
320#endif
321VTK_ABI_NAMESPACE_BEGIN
322vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate);
323VTK_ABI_NAMESPACE_END
324
325namespace vtkDataArrayPrivate
326{
327VTK_ABI_NAMESPACE_BEGIN
341VTK_ABI_NAMESPACE_END
342}
343
344#ifdef _MSC_VER
345#pragma warning(pop)
346#endif
347#endif // VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
348
349// The following clause is only for MSVC 2008 and 2010
350#elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
351#pragma warning(push)
352
353// C4091: 'extern ' : ignored on left of 'int' when no variable is declared
354#pragma warning(disable : 4091)
355
356// Compiler-specific extension warning.
357#pragma warning(disable : 4231)
358
359// We need to disable warning 4910 and do an extern dllexport
360// anyway. When deriving new arrays from an
361// instantiation of this template the compiler does an explicit
362// instantiation of the base class. From outside the vtkCommon
363// library we block this using an extern dllimport instantiation.
364// For classes inside vtkCommon we should be able to just do an
365// extern instantiation, but VS 2008 complains about missing
366// definitions. We cannot do an extern dllimport inside vtkCommon
367// since the symbols are local to the dll. An extern dllexport
368// seems to be the only way to convince VS 2008 to do the right
369// thing, so we just disable the warning.
370#pragma warning(disable : 4910) // extern and dllexport incompatible
371
372// Use an "extern explicit instantiation" to give the class a DLL
373// interface. This is a compiler-specific extension.
374VTK_ABI_NAMESPACE_BEGIN
376 extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate);
377
378#pragma warning(pop)
379
380VTK_ABI_NAMESPACE_END
381#endif
382
383// VTK-HeaderTest-Exclude: vtkScaledSOADataArrayTemplate.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 with a scaling factor.
void ShallowCopy(vtkDataArray *other) override
Create a shallow copy of other into this, if possible.
void SetNumberOfComponents(int numComps) override
Set/Get the dimension (n) of the components.
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
static vtkScaledSOADataArrayTemplate * New()
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(int comp, void(*callback)(void *))
This method allows the user to specify a custom free function to be called when the array is dealloca...
std::vector< vtkBuffer< ValueType > * > Data
vtkTemplateTypeMacro(SelfType, GenericDataArrayType)
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...
ValueType * GetComponentArrayPointer(int comp)
Return a pointer to a contiguous block of memory containing all values for a particular components (i...
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray *source) override
See documentation from parent class.
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
void SetScale(ValueType scale)
Set/Get the Scale value for the object.
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
~vtkScaledSOADataArrayTemplate() override
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 InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array's tuple at tupleIdx to the values in tuple.
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
void FillTypedComponent(int compIdx, ValueType value) override
Set component comp of all tuples to value.
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
ValueType GetScale() const
Set/Get the Scale value for the object.
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.
#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_SCALED_SOA_DATA_ARRAY
Definition vtkType.h:85
void save(Archiver &ar, const std::string &str, const unsigned int version)
#define VTK_EXPECTS(x)
#define VTK_ZEROCOPY
#define VTK_NEWINSTANCE