VTK  9.3.0
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 VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate
38  : public vtkGenericDataArray<vtkScaledSOADataArrayTemplate<ValueTypeT>, ValueTypeT>
39 {
42 
43 public:
46  typedef typename Superclass::ValueType ValueType;
47 
49  {
51  VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
52  VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
53  VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
54  };
55 
57 
59 
63  {
64  if (scale != this->Scale)
65  {
66  if (scale == 0)
67  {
68  vtkErrorMacro("Cannot set Scale to 0");
69  }
70  else
71  {
72  this->Scale = scale;
73  this->Modified();
74  }
75  }
76  }
77  ValueType GetScale() const { return this->Scale; }
79 
81 
84  inline ValueType GetValue(vtkIdType valueIdx) const
85  {
86  vtkIdType tupleIdx;
87  int comp;
88  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
89  return this->GetTypedComponent(tupleIdx, comp);
90  }
92 
94 
97  inline void SetValue(vtkIdType valueIdx, ValueType value)
98  {
99  vtkIdType tupleIdx;
100  int comp;
101  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
102  this->SetTypedComponent(tupleIdx, comp, value);
103  }
105 
109  inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
110  {
111  for (size_t cc = 0; cc < this->Data.size(); cc++)
112  {
113  tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx] * this->Scale;
114  }
115  }
116 
120  inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
121  {
122  for (size_t cc = 0; cc < this->Data.size(); ++cc)
123  {
124  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc] / this->Scale;
125  }
126  }
127 
131  inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
132  {
133  return this->Data[comp]->GetBuffer()[tupleIdx] * this->Scale;
134  }
135 
139  inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
140  {
141  this->Data[comp]->GetBuffer()[tupleIdx] = value / this->Scale;
142  }
143 
147  void FillTypedComponent(int compIdx, ValueType value) override;
148 
162  void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size, bool updateMaxId = false,
163  bool save = false, int deleteMethod = VTK_DATA_ARRAY_FREE);
164 
172  void SetArrayFreeFunction(void (*callback)(void*)) override;
173 
180  void SetArrayFreeFunction(int comp, void (*callback)(void*));
181 
188 
193  void* GetVoidPointer(vtkIdType valueIdx) override;
194 
199  void ExportToVoidPointer(void* ptr) override;
200 
201 #ifndef __VTK_WRAP__
203 
210  {
211  if (source)
212  {
213  switch (source->GetArrayType())
214  {
216  if (vtkDataTypesCompare(source->GetDataType(), vtkTypeTraits<ValueType>::VTK_TYPE_ID))
217  {
218  return static_cast<vtkScaledSOADataArrayTemplate<ValueType>*>(source);
219  }
220  break;
221  }
222  }
223  return nullptr;
224  }
226 #endif
227 
230  void SetNumberOfComponents(int numComps) override;
231  void ShallowCopy(vtkDataArray* other) override;
232 
233  // Reimplemented for efficiency:
235  vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
236  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
237  // using Superclass::InsertTuples;
238  void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
239  {
240  this->Superclass::InsertTuples(dstIds, srcIds, source);
241  }
243  vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override
244  {
245  this->Superclass::InsertTuplesStartingAt(dstStart, srcIds, source);
246  }
247 
248 protected:
251 
256  bool AllocateTuples(vtkIdType numTuples);
257 
262  bool ReallocateTuples(vtkIdType numTuples);
263 
264  std::vector<vtkBuffer<ValueType>*> Data;
266 
267 private:
269  void operator=(const vtkScaledSOADataArrayTemplate&) = delete;
270 
271  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx, vtkIdType& tupleIdx, int& comp) const
272  {
273  tupleIdx = valueIdx / this->NumberOfComponents;
274  comp = valueIdx % this->NumberOfComponents;
275  }
276 
277  friend class vtkGenericDataArray<vtkScaledSOADataArrayTemplate<ValueTypeT>, ValueTypeT>;
281  ValueType Scale;
282 };
283 
284 // Declare vtkArrayDownCast implementations for scale SoA containers:
286 
287 VTK_ABI_NAMESPACE_END
288 #endif // header guard
289 
290 // This portion must be OUTSIDE the include blockers. This is used to tell
291 // libraries other than vtkCommonCore that instantiations of
292 // vtkScaledSOADataArrayTemplate can be found externally. This prevents each library
293 // from instantiating these on their own.
294 #ifdef VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
295 #define VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
296  namespace vtkDataArrayPrivate \
297  { \
298  VTK_ABI_NAMESPACE_BEGIN \
299  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkScaledSOADataArrayTemplate<T>, double); \
300  VTK_ABI_NAMESPACE_END \
301  } \
302  VTK_ABI_NAMESPACE_BEGIN \
303  template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate<T>; \
304  VTK_ABI_NAMESPACE_END
305 
306 #elif defined(VTK_USE_EXTERN_TEMPLATE)
307 #ifndef VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
308 #define VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
309 #ifdef _MSC_VER
310 #pragma warning(push)
311 // The following is needed when the vtkScaledSOADataArrayTemplate is declared
312 // dllexport and is used from another class in vtkCommonCore
313 #pragma warning(disable : 4910) // extern and dllexport incompatible
314 #endif
315 VTK_ABI_NAMESPACE_BEGIN
316 vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate);
317 #ifdef _MSC_VER
318 #pragma warning(pop)
319 #endif
320 VTK_ABI_NAMESPACE_END
321 #endif // VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
322 
323 // The following clause is only for MSVC 2008 and 2010
324 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
325 #pragma warning(push)
326 
327 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
328 #pragma warning(disable : 4091)
329 
330 // Compiler-specific extension warning.
331 #pragma warning(disable : 4231)
332 
333 // We need to disable warning 4910 and do an extern dllexport
334 // anyway. When deriving new arrays from an
335 // instantiation of this template the compiler does an explicit
336 // instantiation of the base class. From outside the vtkCommon
337 // library we block this using an extern dllimport instantiation.
338 // For classes inside vtkCommon we should be able to just do an
339 // extern instantiation, but VS 2008 complains about missing
340 // definitions. We cannot do an extern dllimport inside vtkCommon
341 // since the symbols are local to the dll. An extern dllexport
342 // seems to be the only way to convince VS 2008 to do the right
343 // thing, so we just disable the warning.
344 #pragma warning(disable : 4910) // extern and dllexport incompatible
345 
346 // Use an "extern explicit instantiation" to give the class a DLL
347 // interface. This is a compiler-specific extension.
348 VTK_ABI_NAMESPACE_BEGIN
350  extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate);
351 
352 #pragma warning(pop)
353 
354 VTK_ABI_NAMESPACE_END
355 #endif
356 
357 // VTK-HeaderTest-Exclude: vtkScaledSOADataArrayTemplate.h
Abstract superclass for all arrays.
Abstract superclass to iterate over elements in an vtkAbstractArray.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:54
void Modified() override
Removes out-of-date L2_NORM_RANGE() and L2_NORM_FINITE_RANGE() values.
Base interface for all typed vtkDataArray subclasses.
list of point or cell ids
Definition: vtkIdList.h:32
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 * 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 SetNumberOfComponents(int numComps) override
Set/Get the dimension (n) of the components.
static vtkScaledSOADataArrayTemplate< ValueType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
ValueType * GetComponentArrayPointer(int comp)
Return a pointer to a contiguous block of memory containing all values for a particular components (i...
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...
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.
int GetArrayType() const override
Method for type-checking in FastDownCast implementations.
vtkScaledSOADataArrayTemplate< ValueTypeT > SelfType
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.
static vtkScaledSOADataArrayTemplate * New()
void FillTypedComponent(int compIdx, ValueType value) override
Set component comp of all tuples to value.
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
ValueType GetScale() const
Set/Get the Scale value for the object.
void InsertTuplesStartingAt(vtkIdType dstStart, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
Set component compIdx of the tuple at tupleIdx to value.
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
@ value
Definition: vtkX3D.h:220
@ scale
Definition: vtkX3D.h:229
@ size
Definition: vtkX3D.h:253
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:23
vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkArrayIteratorTemplate)
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
vtkArrayDownCast_TemplateFastCastMacro(vtkScaledSOADataArrayTemplate)
vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate)
int vtkIdType
Definition: vtkType.h:315
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))
#define VTK_ZEROCOPY
#define VTK_NEWINSTANCE