VTK  9.7.1
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.
35 VTK_ABI_NAMESPACE_BEGIN
36 template <class ValueTypeT>
37 class VTK_DEPRECATED_IN_9_7_0("Use vtkSOADataArrayTemplate and scale values yourself")
38  VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate
39 #ifndef __VTK_WRAP__
40  : public vtkGenericDataArray<vtkScaledSOADataArrayTemplate<ValueTypeT>, ValueTypeT,
41  /*vtkArrayTypes::VTK_SCALED_SOA_DATA_ARRAY*/ 7>
42 {
44  ValueTypeT, /*vtkArrayTypes::VTK_SCALED_SOA_DATA_ARRAY*/ 7>;
45 #else
46  : public vtkDataArray
47 {
48  using GenericDataArrayType = vtkDataArray;
49 #endif
50 
51 public:
53  vtkTemplateTypeMacro(SelfType, GenericDataArrayType);
54 #ifndef __VTK_WRAP__
55  using typename Superclass::ArrayTypeTag;
56  using typename Superclass::DataTypeTag;
57  using typename Superclass::ValueType;
58 #else
59  using ValueType = ValueTypeT;
60 #endif
61 
63  {
65  VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
66  VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
67  VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
68  };
69 
71 
73 
77  {
78  if (scale != this->Scale)
79  {
80  if (scale == 0)
81  {
82  vtkErrorMacro("Cannot set Scale to 0");
83  }
84  else
85  {
86  this->Scale = scale;
87  this->Modified();
88  }
89  }
90  }
91  ValueType GetScale() const { return this->Scale; }
93 
95 
98  ValueType GetValue(vtkIdType valueIdx) const
99  VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
100  {
101  vtkIdType tupleIdx;
102  int comp;
103  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
104  return this->GetTypedComponent(tupleIdx, comp);
105  }
107 
109 
113  VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
114  {
115  vtkIdType tupleIdx;
116  int comp;
117  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
118  this->SetTypedComponent(tupleIdx, comp, value);
119  }
121 
125  void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
126  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
127  {
128  for (size_t cc = 0; cc < this->Data.size(); cc++)
129  {
130  tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx] * this->Scale;
131  }
132  }
133 
137  void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
138  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
139  {
140  for (size_t cc = 0; cc < this->Data.size(); ++cc)
141  {
142  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc] / this->Scale;
143  }
144  }
145 
149  ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
150  VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
151  VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents())
152  {
153  return this->Data[comp]->GetBuffer()[tupleIdx] * this->Scale;
154  }
155 
159  void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
160  VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
161  VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents())
162  {
163  this->Data[comp]->GetBuffer()[tupleIdx] = value / this->Scale;
164  }
165 
169  void FillTypedComponent(int compIdx, ValueType value) override;
170 
184  void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size, bool updateMaxId = false,
185  bool save = false, int deleteMethod = VTK_DATA_ARRAY_FREE);
186 
194  void SetArrayFreeFunction(void (*callback)(void*)) override;
195 
202  void SetArrayFreeFunction(int comp, void (*callback)(void*));
203 
209  ValueType* GetComponentArrayPointer(int comp);
210 
217 #ifdef __VTK_WRAP__
218  vtkAbstractBuffer* GetComponentBuffer(int comp);
219 #else
220  vtkBuffer<ValueTypeT>* GetComponentBuffer(int comp);
221 #endif // __VTK_WRAP__
222 
227  VTK_DEPRECATED_IN_9_7_0("Use DeepCopy with an vtkAOSDataArrayTemplate array")
228  void ExportToVoidPointer(void* ptr) override;
229 
230  VTK_DEPRECATED_IN_9_7_0("Use vtk::DataArrayValueRange, or the array directly")
231  VTK_NEWINSTANCE vtkArrayIterator* NewIterator() override;
232  void SetNumberOfComponents(int numComps) override;
233  void ShallowCopy(vtkDataArray* other) override;
234  using vtkAbstractArray::ShallowCopy;
235 
236  // Reimplemented for efficiency:
237  void InsertTuples(
238  vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
239  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
240  // using Superclass::InsertTuples;
241  void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
242  {
243  this->Superclass::InsertTuples(dstIds, srcIds, source);
244  }
246  vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override
247  {
248  this->Superclass::InsertTuplesStartingAt(dstStart, srcIds, source);
249  }
250 
251 #ifdef __VTK_WRAP__
252  // Add APIs inherited from vtkGenericDataArray, which is excluded from wrapping
254 #endif
255 
256 protected:
258  ~vtkScaledSOADataArrayTemplate() override;
259 
264  VTK_DEPRECATED_IN_9_7_0("No longer needed")
265  bool AllocateTuples(vtkIdType numTuples);
266 
271  bool ReallocateTuples(vtkIdType numTuples);
272 
274 
275 private:
277  void operator=(const vtkScaledSOADataArrayTemplate&) = delete;
278 
279  void GetTupleIndexFromValueIndex(vtkIdType valueIdx, vtkIdType& tupleIdx, int& comp) const
280  {
281  tupleIdx = valueIdx / this->NumberOfComponents;
282  comp = valueIdx % this->NumberOfComponents;
283  }
284 
289  ValueType Scale;
290 };
291 
292 // Declare vtkArrayDownCast implementations for scaled SoA containers:
293 vtkArrayDownCast_TemplateFastCastMacro(vtkScaledSOADataArrayTemplate);
294 
295 VTK_ABI_NAMESPACE_END
296 
297 // This macro is used by the subclasses to create dummy
298 // declarations for these functions such that the wrapper
299 // can see them. The wrappers ignore vtkScaledSOADataArrayTemplate.
300 #define vtkCreateScaledSOAWrappedArrayInterface(T) \
301  vtkCreateWrappedArrayReadInterface(T); \
302  vtkCreateWrappedArrayWriteInterface(T); \
303  T* GetComponentArrayPointer(int id); \
304  vtkAbstractBuffer* GetComponentBuffer(int comp); \
305  void SetArray(int comp, VTK_ZEROCOPY T* array, vtkIdType size, bool updateMaxId, bool save, \
306  int deleteMethod);
307 #endif // header guard
308 
309 // This portion must be OUTSIDE the include blockers. This is used to tell
310 // libraries other than vtkCommonCore that instantiations of
311 // vtkScaledSOADataArrayTemplate can be found externally. This prevents each library
312 // from instantiating these on their own.
313 #ifdef VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
314 #define VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
315  namespace vtkDataArrayPrivate \
316  { \
317  VTK_ABI_NAMESPACE_BEGIN \
318  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkScaledSOADataArrayTemplate<T>, double); \
319  VTK_ABI_NAMESPACE_END \
320  } \
321  VTK_ABI_NAMESPACE_BEGIN \
322  template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate<T>; \
323  VTK_ABI_NAMESPACE_END
324 // We only provide these specializations for the 64-bit integer types, since
325 // other types can reuse the double-precision mechanism in
326 // vtkDataArray::GetRange without losing precision.
327 #define VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE_VALUERANGE(T) \
328  namespace vtkDataArrayPrivate \
329  { \
330  VTK_ABI_NAMESPACE_BEGIN \
331  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkScaledSOADataArrayTemplate<T>, T); \
332  VTK_ABI_NAMESPACE_END \
333  }
334 // MinGW GCC does not reliably export inline member functions (such as IsTypeOf
335 // defined by vtkTemplateTypeMacro above) when using template class __declspec(dllexport)
336 // with extern template. This guard is applied here rather than in other VTK places
337 // with #elif defined(VTK_USE_EXTERN_TEMPLATE) because only these three DataArrayTemplate
338 // classes combine extern template with vtkTemplateTypeMacro inline functions in the class
339 // body; other VTK template classes using this pattern (e.g. vtkConstantArray,
340 // vtkAffineArray) use vtkImplicitArrayTypeMacro instead and were not observed with this
341 // MinGW issue.
342 #elif defined(VTK_USE_EXTERN_TEMPLATE) && !defined(__MINGW32__)
343 #ifndef VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
344 #define VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
345 #ifdef _MSC_VER
346 #pragma warning(push)
347 // The following is needed when the vtkScaledSOADataArrayTemplate is declared
348 // dllexport and is used from another class in vtkCommonCore
349 #pragma warning(disable : 4910) // extern and dllexport incompatible
350 #endif
351 VTK_ABI_NAMESPACE_BEGIN
352 vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate);
353 VTK_ABI_NAMESPACE_END
354 
355 namespace vtkDataArrayPrivate
356 {
357 VTK_ABI_NAMESPACE_BEGIN
358 // These are instantiated in vtkGenericDataArrayValueRange${i}.cxx
364 // These are instantiated in vtkScaledSOADataArrayTemplateInstantiate${i}.cxx
378 VTK_ABI_NAMESPACE_END
379 }
380 
381 #ifdef _MSC_VER
382 #pragma warning(pop)
383 #endif
384 #endif // VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
385 
386 // The following clause is only for MSVC 2008 and 2010
387 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
388 #pragma warning(push)
389 
390 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
391 #pragma warning(disable : 4091)
392 
393 // Compiler-specific extension warning.
394 #pragma warning(disable : 4231)
395 
396 // We need to disable warning 4910 and do an extern dllexport
397 // anyway. When deriving new arrays from an
398 // instantiation of this template the compiler does an explicit
399 // instantiation of the base class. From outside the vtkCommon
400 // library we block this using an extern dllimport instantiation.
401 // For classes inside vtkCommon we should be able to just do an
402 // extern instantiation, but VS 2008 complains about missing
403 // definitions. We cannot do an extern dllimport inside vtkCommon
404 // since the symbols are local to the dll. An extern dllexport
405 // seems to be the only way to convince VS 2008 to do the right
406 // thing, so we just disable the warning.
407 #pragma warning(disable : 4910) // extern and dllexport incompatible
408 
409 // Use an "extern explicit instantiation" to give the class a DLL
410 // interface. This is a compiler-specific extension.
411 VTK_ABI_NAMESPACE_BEGIN
413  extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate);
414 
415 #pragma warning(pop)
416 
417 VTK_ABI_NAMESPACE_END
418 #endif
419 
420 // VTK-HeaderTest-Exclude: vtkScaledSOADataArrayTemplate.h
Struct-Of-Arrays implementation of vtkGenericDataArray with a scaling factor.
Struct-Of-Arrays implementation of vtkGenericDataArray.
void SetScale(ValueType scale)
Set/Get the Scale value for the object.
#define vtkCreateGenericWrappedArrayInterface(T)
int GetNumberOfComponents() const
Set/Get the dimension (n) of the components.
Abstract superclass for all arrays.
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
#define VTK_DECLARE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType)
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
int vtkIdType
Definition: vtkType.h:363
Base interface for all typed vtkDataArray subclasses.
ValueType GetScale() const
Set/Get the Scale value for the object.
vtkIdType GetNumberOfValues() const
Get the total number of values in the array.
void SetArrayFreeFunction(void(*callback)(void *)) override
Default implementation raises a runtime error.
vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkArrayIteratorTemplate)
internal storage class used by vtkSOADataArrayTemplate, vtkAOSDataArrayTemplate, and others...
Definition: vtkBuffer.h:31
void save(Archiver &ar, const std::string &str, const unsigned int version)
list of point or cell ids
Definition: vtkIdList.h:25
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
Abstract superclass to iterate over elements in an vtkAbstractArray.
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
Set component compIdx of the tuple at tupleIdx to value.
VTK_ITER_INLINE auto DataArrayValueRange(const ArrayTypePtr &array, ValueIdType start=-1, ValueIdType end=-1) -> typename detail::SelectValueRange< ArrayTypePtr, TupleSize, ForceValueTypeForVtkDataArray >::type
Generate an stl and for-range compatible range of flat AOS iterators from a vtkDataArray.
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
vtkTemplateTypeMacro(SelfType, vtkDataArray)
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
#define VTK_NEWINSTANCE
vtkArrayDownCast_TemplateFastCastMacro(vtkScaledSOADataArrayTemplate)
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
#define VTK_ZEROCOPY
Abstract base class for vtkBuffer providing buffer protocol support.
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array's tuple at tupleIdx to the values in tuple.
std::integral_constant< int, vtkArrayTypes::VTK_ABSTRACT_ARRAY > ArrayTypeTag
vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate)
virtual void FillTypedComponent(int compIdx, ValueType value)
Set component comp of all tuples to value.
void InsertTuplesStartingAt(vtkIdType dstStart, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
#define VTK_EXPECTS(x)
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
#define VTK_DEPRECATED_IN_9_7_0(reason)
void Modified() override
Removes out-of-date L2_NORM_RANGE() and L2_NORM_FINITE_RANGE() values.
ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
Get component comp of the tuple at tupleIdx.