VTK  9.1.0
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 =========================================================================*/
33 #ifndef vtkScaledSOADataArrayTemplate_h
34 #define vtkScaledSOADataArrayTemplate_h
35 
36 #include "vtkBuffer.h"
37 #include "vtkBuild.h" // For VTK_BUILD_SHARED_LIBS
38 #include "vtkCommonCoreModule.h" // For export macro
39 #include "vtkCompiler.h" // for VTK_USE_EXTERN_TEMPLATE
40 #include "vtkGenericDataArray.h"
41 
42 // The export macro below makes no sense, but is necessary for older compilers
43 // when we export instantiations of this class from vtkCommonCore.
44 template <class ValueTypeT>
45 class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate
46  : public vtkGenericDataArray<vtkScaledSOADataArrayTemplate<ValueTypeT>, ValueTypeT>
47 {
50 
51 public:
54  typedef typename Superclass::ValueType ValueType;
55 
57  {
59  VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
60  VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
61  VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
62  };
63 
65 
67 
71  {
72  if (scale != this->Scale)
73  {
74  if (scale == 0)
75  {
76  vtkErrorMacro("Cannot set Scale to 0");
77  }
78  else
79  {
80  this->Scale = scale;
81  this->Modified();
82  }
83  }
84  }
85  ValueType GetScale() const { return this->Scale; }
87 
89 
92  inline ValueType GetValue(vtkIdType valueIdx) const
93  {
94  vtkIdType tupleIdx;
95  int comp;
96  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
97  return this->GetTypedComponent(tupleIdx, comp);
98  }
100 
102 
105  inline void SetValue(vtkIdType valueIdx, ValueType value)
106  {
107  vtkIdType tupleIdx;
108  int comp;
109  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
110  this->SetTypedComponent(tupleIdx, comp, value);
111  }
113 
117  inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
118  {
119  for (size_t cc = 0; cc < this->Data.size(); cc++)
120  {
121  tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx] * this->Scale;
122  }
123  }
124 
128  inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
129  {
130  for (size_t cc = 0; cc < this->Data.size(); ++cc)
131  {
132  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc] / this->Scale;
133  }
134  }
135 
139  inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
140  {
141  return this->Data[comp]->GetBuffer()[tupleIdx] * this->Scale;
142  }
143 
147  inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
148  {
149  this->Data[comp]->GetBuffer()[tupleIdx] = value / this->Scale;
150  }
151 
155  void FillTypedComponent(int compIdx, ValueType value) override;
156 
170  void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size, bool updateMaxId = false,
171  bool save = false, int deleteMethod = VTK_DATA_ARRAY_FREE);
172 
180  void SetArrayFreeFunction(void (*callback)(void*)) override;
181 
188  void SetArrayFreeFunction(int comp, void (*callback)(void*));
189 
196 
201  void* GetVoidPointer(vtkIdType valueIdx) override;
202 
207  void ExportToVoidPointer(void* ptr) override;
208 
209 #ifndef __VTK_WRAP__
210 
218  {
219  if (source)
220  {
221  switch (source->GetArrayType())
222  {
224  if (vtkDataTypesCompare(source->GetDataType(), vtkTypeTraits<ValueType>::VTK_TYPE_ID))
225  {
226  return static_cast<vtkScaledSOADataArrayTemplate<ValueType>*>(source);
227  }
228  break;
229  }
230  }
231  return nullptr;
232  }
234 #endif
235 
238  void SetNumberOfComponents(int numComps) override;
239  void ShallowCopy(vtkDataArray* other) override;
240 
241  // Reimplemented for efficiency:
243  vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
244  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
245  // using Superclass::InsertTuples;
246  void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
247  {
248  this->Superclass::InsertTuples(dstIds, srcIds, source);
249  }
250 
251 protected:
254 
259  bool AllocateTuples(vtkIdType numTuples);
260 
265  bool ReallocateTuples(vtkIdType numTuples);
266 
267  std::vector<vtkBuffer<ValueType>*> Data;
269 
270 private:
272  void operator=(const vtkScaledSOADataArrayTemplate&) = delete;
273 
274  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx, vtkIdType& tupleIdx, int& comp) const
275  {
276  tupleIdx = valueIdx / this->NumberOfComponents;
277  comp = valueIdx % this->NumberOfComponents;
278  }
279 
280  friend class vtkGenericDataArray<vtkScaledSOADataArrayTemplate<ValueTypeT>, ValueTypeT>;
284  ValueType Scale;
285 };
286 
287 // Declare vtkArrayDownCast implementations for scale SoA containers:
289 
290 #endif // header guard
291 
292 // This portion must be OUTSIDE the include blockers. This is used to tell
293 // libraries other than vtkCommonCore that instantiations of
294 // vtkScaledSOADataArrayTemplate can be found externally. This prevents each library
295 // from instantiating these on their own.
296 #ifdef VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
297 #define VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
298  namespace vtkDataArrayPrivate \
299  { \
300  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkScaledSOADataArrayTemplate<T>, double); \
301  } \
302  template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate<T>
303 
304 #elif defined(VTK_USE_EXTERN_TEMPLATE)
305 #ifndef VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
306 #define VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
307 #ifdef _MSC_VER
308 #pragma warning(push)
309 // The following is needed when the vtkScaledSOADataArrayTemplate is declared
310 // dllexport and is used from another class in vtkCommonCore
311 #pragma warning(disable : 4910) // extern and dllexport incompatible
312 #endif
313 vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate);
314 #ifdef _MSC_VER
315 #pragma warning(pop)
316 #endif
317 #endif // VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
318 
319 // The following clause is only for MSVC 2008 and 2010
320 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
321 #pragma warning(push)
322 
323 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
324 #pragma warning(disable : 4091)
325 
326 // Compiler-specific extension warning.
327 #pragma warning(disable : 4231)
328 
329 // We need to disable warning 4910 and do an extern dllexport
330 // anyway. When deriving new arrays from an
331 // instantiation of this template the compiler does an explicit
332 // instantiation of the base class. From outside the vtkCommon
333 // library we block this using an extern dllimport instantiation.
334 // For classes inside vtkCommon we should be able to just do an
335 // extern instantiation, but VS 2008 complains about missing
336 // definitions. We cannot do an extern dllimport inside vtkCommon
337 // since the symbols are local to the dll. An extern dllexport
338 // seems to be the only way to convince VS 2008 to do the right
339 // thing, so we just disable the warning.
340 #pragma warning(disable : 4910) // extern and dllexport incompatible
341 
342 // Use an "extern explicit instantiation" to give the class a DLL
343 // interface. This is a compiler-specific extension.
345  extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate);
346 
347 #pragma warning(pop)
348 
349 #endif
350 
351 // VTK-HeaderTest-Exclude: vtkScaledSOADataArrayTemplate.h
vtkAbstractArray::DeleteMethod
DeleteMethod
Definition: vtkAbstractArray.h:322
VTK_ZEROCOPY
#define VTK_ZEROCOPY
Definition: vtkWrappingHints.h:45
vtkGenericDataArray::ValueType
ValueTypeT ValueType
Definition: vtkGenericDataArray.h:84
vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE
@ VTK_DATA_ARRAY_ALIGNED_FREE
Definition: vtkAbstractArray.h:325
vtkGenericDataArray::GetTypedComponent
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
Definition: vtkGenericDataArray.h:158
vtkTypeTraits
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:30
vtkScaledSOADataArrayTemplate::SetScale
void SetScale(ValueType scale)
Set/Get the Scale value for the object.
Definition: vtkScaledSOADataArrayTemplate.h:70
vtkX3D::scale
@ scale
Definition: vtkX3D.h:235
vtkScaledSOADataArrayTemplate::~vtkScaledSOADataArrayTemplate
~vtkScaledSOADataArrayTemplate() override
vtkX3D::value
@ value
Definition: vtkX3D.h:226
vtkAbstractArray::VTK_DATA_ARRAY_DELETE
@ VTK_DATA_ARRAY_DELETE
Definition: vtkAbstractArray.h:324
vtkIdType
int vtkIdType
Definition: vtkType.h:332
vtkScaledSOADataArrayTemplate::GetTypedComponent
ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
Get component comp of the tuple at tupleIdx.
Definition: vtkScaledSOADataArrayTemplate.h:139
vtkScaledSOADataArrayTemplate::AoSCopy
vtkBuffer< ValueType > * AoSCopy
Definition: vtkScaledSOADataArrayTemplate.h:268
vtkGenericDataArray::SetTypedComponent
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
Set component compIdx of the tuple at tupleIdx to value.
Definition: vtkGenericDataArray.h:169
vtkAbstractArray::VTK_DATA_ARRAY_FREE
@ VTK_DATA_ARRAY_FREE
Definition: vtkAbstractArray.h:323
vtkScaledSOADataArrayTemplate::GetArrayType
int GetArrayType() const override
Method for type-checking in FastDownCast implementations.
Definition: vtkScaledSOADataArrayTemplate.h:236
vtkGenericDataArray
Base interface for all typed vtkDataArray subclasses.
Definition: vtkGenericDataArray.h:80
vtkScaledSOADataArrayTemplate::vtkTemplateTypeMacro
vtkTemplateTypeMacro(SelfType, GenericDataArrayType)
vtkScaledSOADataArrayTemplate::SelfType
vtkScaledSOADataArrayTemplate< ValueTypeT > SelfType
Definition: vtkScaledSOADataArrayTemplate.h:52
vtkAbstractArray::ScaleSoADataArrayTemplate
@ ScaleSoADataArrayTemplate
Definition: vtkAbstractArray.h:641
vtkDataArray
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:159
vtkScaledSOADataArrayTemplate::GetTypedTuple
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
Definition: vtkScaledSOADataArrayTemplate.h:117
vtkScaledSOADataArrayTemplate
Struct-Of-Arrays implementation of vtkGenericDataArray with a scaling factor.
Definition: vtkScaledSOADataArrayTemplate.h:47
vtkDataArray::Modified
void Modified() override
Removes out-of-date L2_NORM_RANGE() and L2_NORM_FINITE_RANGE() values.
vtkArrayDownCast_TemplateFastCastMacro
vtkArrayDownCast_TemplateFastCastMacro(vtkScaledSOADataArrayTemplate)
vtkBuffer< ValueType >
vtkScaledSOADataArrayTemplate::ReallocateTuples
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
vtkScaledSOADataArrayTemplate::New
static vtkScaledSOADataArrayTemplate * New()
vtkScaledSOADataArrayTemplate::GetValue
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
Definition: vtkScaledSOADataArrayTemplate.h:92
vtkScaledSOADataArrayTemplate::SetValue
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
Definition: vtkScaledSOADataArrayTemplate.h:105
vtkScaledSOADataArrayTemplate::NewIterator
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
vtkBuffer.h
vtkScaledSOADataArrayTemplate::vtkScaledSOADataArrayTemplate
vtkScaledSOADataArrayTemplate()
vtkScaledSOADataArrayTemplate::SetArrayFreeFunction
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...
vtkScaledSOADataArrayTemplate::GetComponentArrayPointer
ValueType * GetComponentArrayPointer(int comp)
Return a pointer to a contiguous block of memory containing all values for a particular components (i...
vtkScaledSOADataArrayTemplate::SetArray
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.
vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
@ VTK_DATA_ARRAY_USER_DEFINED
Definition: vtkAbstractArray.h:326
save
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))
Definition: vtkVariantBoostSerialization.h:64
vtkGenericDataArray.h
vtkScaledSOADataArrayTemplate::Data
std::vector< vtkBuffer< ValueType > * > Data
Definition: vtkScaledSOADataArrayTemplate.h:267
vtkIdList
list of point or cell ids
Definition: vtkIdList.h:140
vtkScaledSOADataArrayTemplate::GetVoidPointer
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...
vtkX3D::size
@ size
Definition: vtkX3D.h:259
vtkCompiler.h
vtkAbstractArray
Abstract superclass for all arrays.
Definition: vtkAbstractArray.h:76
vtkScaledSOADataArrayTemplate::FillTypedComponent
void FillTypedComponent(int compIdx, ValueType value) override
Set component comp of all tuples to value.
vtkScaledSOADataArrayTemplate::AllocateTuples
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
vtkScaledSOADataArrayTemplate::SetTypedTuple
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array's tuple at tupleIdx to the values in tuple.
Definition: vtkScaledSOADataArrayTemplate.h:128
vtkInstantiateTemplateMacro
vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkArrayIteratorTemplate)
vtkArrayIterator
Abstract superclass to iterate over elements in an vtkAbstractArray.
Definition: vtkArrayIterator.h:50
vtkScaledSOADataArrayTemplate::InsertTuples
void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray *source) override
Copy n consecutive tuples starting at srcStart from the source array to this array,...
vtkScaledSOADataArrayTemplate::InsertTuples
void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source) override
Copy the tuples indexed in srcIds from the source array to the tuple locations indexed by dstIds in t...
Definition: vtkScaledSOADataArrayTemplate.h:246
vtkScaledSOADataArrayTemplate::SetArrayFreeFunction
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...
vtkScaledSOADataArrayTemplate::SetNumberOfComponents
void SetNumberOfComponents(int numComps) override
Set/Get the dimension (n) of the components.
vtkScaledSOADataArrayTemplate::ShallowCopy
void ShallowCopy(vtkDataArray *other) override
Create a shallow copy of other into this, if possible.
vtkScaledSOADataArrayTemplate::GetScale
ValueType GetScale() const
Set/Get the Scale value for the object.
Definition: vtkScaledSOADataArrayTemplate.h:85
vtkScaledSOADataArrayTemplate::FastDownCast
static vtkScaledSOADataArrayTemplate< ValueType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
Definition: vtkScaledSOADataArrayTemplate.h:217
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:998
VTK_NEWINSTANCE
#define VTK_NEWINSTANCE
Definition: vtkWrappingHints.h:44
vtkScaledSOADataArrayTemplate::ValueType
Superclass::ValueType ValueType
Definition: vtkScaledSOADataArrayTemplate.h:54
vtkScaledSOADataArrayTemplate::SetTypedComponent
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
Definition: vtkScaledSOADataArrayTemplate.h:147
vtkExternTemplateMacro
vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate)
vtkAbstractArray::NumberOfComponents
int NumberOfComponents
Definition: vtkAbstractArray.h:679
vtkScaledSOADataArrayTemplate::ExportToVoidPointer
void ExportToVoidPointer(void *ptr) override
Export a copy of the data in AoS ordering to the preallocated memory buffer.