VTK  9.2.6
vtkScaledSOADataArrayTemplate.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkScaledSOADataArrayTemplate.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
36 #ifndef vtkScaledSOADataArrayTemplate_h
37 #define vtkScaledSOADataArrayTemplate_h
38 
39 #include "vtkBuffer.h"
40 #include "vtkBuild.h" // For VTK_BUILD_SHARED_LIBS
41 #include "vtkCommonCoreModule.h" // For export macro
42 #include "vtkCompiler.h" // for VTK_USE_EXTERN_TEMPLATE
43 #include "vtkGenericDataArray.h"
44 
45 // The export macro below makes no sense, but is necessary for older compilers
46 // when we export instantiations of this class from vtkCommonCore.
47 template <class ValueTypeT>
48 class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate
49  : public vtkGenericDataArray<vtkScaledSOADataArrayTemplate<ValueTypeT>, ValueTypeT>
50 {
53 
54 public:
56  vtkTemplateTypeMacro(SelfType, GenericDataArrayType);
57  typedef typename Superclass::ValueType ValueType;
58 
60  {
62  VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
63  VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
64  VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
65  };
66 
68 
70 
73  void SetScale(ValueType scale)
74  {
75  if (scale != this->Scale)
76  {
77  if (scale == 0)
78  {
79  vtkErrorMacro("Cannot set Scale to 0");
80  }
81  else
82  {
83  this->Scale = scale;
84  this->Modified();
85  }
86  }
87  }
88  ValueType GetScale() const { return this->Scale; }
90 
92 
95  inline ValueType GetValue(vtkIdType valueIdx) const
96  {
97  vtkIdType tupleIdx;
98  int comp;
99  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
100  return this->GetTypedComponent(tupleIdx, comp);
101  }
103 
105 
108  inline void SetValue(vtkIdType valueIdx, ValueType value)
109  {
110  vtkIdType tupleIdx;
111  int comp;
112  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
113  this->SetTypedComponent(tupleIdx, comp, value);
114  }
116 
120  inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
121  {
122  for (size_t cc = 0; cc < this->Data.size(); cc++)
123  {
124  tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx] * this->Scale;
125  }
126  }
127 
131  inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
132  {
133  for (size_t cc = 0; cc < this->Data.size(); ++cc)
134  {
135  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc] / this->Scale;
136  }
137  }
138 
142  inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
143  {
144  return this->Data[comp]->GetBuffer()[tupleIdx] * this->Scale;
145  }
146 
150  inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
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 
198  ValueType* GetComponentArrayPointer(int comp);
199 
204  void* GetVoidPointer(vtkIdType valueIdx) override;
205 
210  void ExportToVoidPointer(void* ptr) override;
211 
212 #ifndef __VTK_WRAP__
213 
221  {
222  if (source)
223  {
224  switch (source->GetArrayType())
225  {
227  if (vtkDataTypesCompare(source->GetDataType(), vtkTypeTraits<ValueType>::VTK_TYPE_ID))
228  {
229  return static_cast<vtkScaledSOADataArrayTemplate<ValueType>*>(source);
230  }
231  break;
232  }
233  }
234  return nullptr;
235  }
237 #endif
238 
241  void SetNumberOfComponents(int numComps) override;
242  void ShallowCopy(vtkDataArray* other) override;
243 
244  // Reimplemented for efficiency:
245  void InsertTuples(
246  vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
247  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
248  // using Superclass::InsertTuples;
249  void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
250  {
251  this->Superclass::InsertTuples(dstIds, srcIds, source);
252  }
254  vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override
255  {
256  this->Superclass::InsertTuplesStartingAt(dstStart, srcIds, source);
257  }
258 
259 protected:
261  ~vtkScaledSOADataArrayTemplate() override;
262 
267  bool AllocateTuples(vtkIdType numTuples);
268 
273  bool ReallocateTuples(vtkIdType numTuples);
274 
275  std::vector<vtkBuffer<ValueType>*> Data;
277 
278 private:
280  void operator=(const vtkScaledSOADataArrayTemplate&) = delete;
281 
282  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx, vtkIdType& tupleIdx, int& comp) const
283  {
284  tupleIdx = valueIdx / this->NumberOfComponents;
285  comp = valueIdx % this->NumberOfComponents;
286  }
287 
288  friend class vtkGenericDataArray<vtkScaledSOADataArrayTemplate<ValueTypeT>, ValueTypeT>;
292  ValueType Scale;
293 };
294 
295 // Declare vtkArrayDownCast implementations for scale SoA containers:
297 
298 #endif // header guard
299 
300 // This portion must be OUTSIDE the include blockers. This is used to tell
301 // libraries other than vtkCommonCore that instantiations of
302 // vtkScaledSOADataArrayTemplate can be found externally. This prevents each library
303 // from instantiating these on their own.
304 #ifdef VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
305 #define VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
306  namespace vtkDataArrayPrivate \
307  { \
308  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkScaledSOADataArrayTemplate<T>, double); \
309  } \
310  template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate<T>
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
321 vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate);
322 #ifdef _MSC_VER
323 #pragma warning(pop)
324 #endif
325 #endif // VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
326 
327 // The following clause is only for MSVC 2008 and 2010
328 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
329 #pragma warning(push)
330 
331 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
332 #pragma warning(disable : 4091)
333 
334 // Compiler-specific extension warning.
335 #pragma warning(disable : 4231)
336 
337 // We need to disable warning 4910 and do an extern dllexport
338 // anyway. When deriving new arrays from an
339 // instantiation of this template the compiler does an explicit
340 // instantiation of the base class. From outside the vtkCommon
341 // library we block this using an extern dllimport instantiation.
342 // For classes inside vtkCommon we should be able to just do an
343 // extern instantiation, but VS 2008 complains about missing
344 // definitions. We cannot do an extern dllimport inside vtkCommon
345 // since the symbols are local to the dll. An extern dllexport
346 // seems to be the only way to convince VS 2008 to do the right
347 // thing, so we just disable the warning.
348 #pragma warning(disable : 4910) // extern and dllexport incompatible
349 
350 // Use an "extern explicit instantiation" to give the class a DLL
351 // interface. This is a compiler-specific extension.
353  extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate);
354 
355 #pragma warning(pop)
356 
357 #endif
358 
359 // VTK-HeaderTest-Exclude: vtkScaledSOADataArrayTemplate.h
Struct-Of-Arrays implementation of vtkGenericDataArray with a scaling factor.
void SetScale(ValueType scale)
Set/Get the Scale value for the object.
virtual void ShallowCopy(vtkDataArray *other)
Create a shallow copy of other into this, if possible.
Abstract superclass for all arrays.
void SetArrayFreeFunction(void(*callback)(void *)) override
Default implementation raises a runtime error.
int GetArrayType() const override
Method for type-checking in FastDownCast implementations.
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
virtual int GetDataType() const =0
Return the underlying data type.
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
Set component compIdx of the tuple at tupleIdx to value.
int vtkIdType
Definition: vtkType.h:332
Base interface for all typed vtkDataArray subclasses.
vtkScaledSOADataArrayTemplate< ValueTypeT > SelfType
ValueType GetScale() const
Set/Get the Scale value for the object.
vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkArrayIteratorTemplate)
std::vector< vtkBuffer< ValueType > * > Data
list of point or cell ids
Definition: vtkIdList.h:33
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:55
void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
vtkTemplateTypeMacro(SelfType, vtkDataArray)
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))
Abstract superclass to iterate over elements in an vtkAbstractArray.
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
virtual int GetArrayType() const
Method for type-checking in FastDownCast implementations.
#define VTK_NEWINSTANCE
void * GetVoidPointer(vtkIdType valueIdx) override
Default implementation raises a runtime error.
vtkArrayDownCast_TemplateFastCastMacro(vtkScaledSOADataArrayTemplate)
void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
#define VTK_ZEROCOPY
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.
virtual void FillTypedComponent(int compIdx, ValueType value)
Set component comp of all tuples to value.
vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate)
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...
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
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.
void SetNumberOfComponents(int num) override
Set/Get the dimension (n) of the components.
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.
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:33
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
virtual void ExportToVoidPointer(void *out_ptr)
This method copies the array data to the void pointer specified by the user.