VTK  9.0.1
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 "vtkCommonCoreModule.h" // For export macro
38 #include "vtkGenericDataArray.h"
39 
40 // The export macro below makes no sense, but is necessary for older compilers
41 // when we export instantiations of this class from vtkCommonCore.
42 template <class ValueTypeT>
43 class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate
44  : public vtkGenericDataArray<vtkScaledSOADataArrayTemplate<ValueTypeT>, ValueTypeT>
45 {
48 
49 public:
52  typedef typename Superclass::ValueType ValueType;
53 
55  {
57  VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
58  VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
59  VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
60  };
61 
63 
65 
69  {
70  if (scale != this->Scale)
71  {
72  if (scale == 0)
73  {
74  vtkErrorMacro("Cannot set Scale to 0");
75  }
76  else
77  {
78  this->Scale = scale;
79  this->Modified();
80  }
81  }
82  }
83  ValueType GetScale() const { return this->Scale; }
85 
87 
90  inline ValueType GetValue(vtkIdType valueIdx) const
91  {
92  vtkIdType tupleIdx;
93  int comp;
94  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
95  return this->GetTypedComponent(tupleIdx, comp);
96  }
98 
100 
103  inline void SetValue(vtkIdType valueIdx, ValueType value)
104  {
105  vtkIdType tupleIdx;
106  int comp;
107  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
108  this->SetTypedComponent(tupleIdx, comp, value);
109  }
111 
115  inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
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  inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
127  {
128  for (size_t cc = 0; cc < this->Data.size(); ++cc)
129  {
130  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc] / this->Scale;
131  }
132  }
133 
137  inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
138  {
139  return this->Data[comp]->GetBuffer()[tupleIdx] * this->Scale;
140  }
141 
145  inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
146  {
147  this->Data[comp]->GetBuffer()[tupleIdx] = value / this->Scale;
148  }
149 
153  void FillTypedComponent(int compIdx, ValueType value) override;
154 
168  void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size, bool updateMaxId = false,
169  bool save = false, int deleteMethod = VTK_DATA_ARRAY_FREE);
170 
178  void SetArrayFreeFunction(void (*callback)(void*)) override;
179 
186  void SetArrayFreeFunction(int comp, void (*callback)(void*));
187 
194 
199  void* GetVoidPointer(vtkIdType valueIdx) override;
200 
205  void ExportToVoidPointer(void* ptr) override;
206 
207 #ifndef __VTK_WRAP__
209 
216  {
217  if (source)
218  {
219  switch (source->GetArrayType())
220  {
222  if (vtkDataTypesCompare(source->GetDataType(), vtkTypeTraits<ValueType>::VTK_TYPE_ID))
223  {
224  return static_cast<vtkScaledSOADataArrayTemplate<ValueType>*>(source);
225  }
226  break;
227  }
228  }
229  return nullptr;
230  }
232 #endif
233 
236  void SetNumberOfComponents(int numComps) override;
237  void ShallowCopy(vtkDataArray* other) override;
238 
239  // Reimplemented for efficiency:
241  vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
242  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
243  // using Superclass::InsertTuples;
244  void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
245  {
246  this->Superclass::InsertTuples(dstIds, srcIds, source);
247  }
248 
249 protected:
252 
257  bool AllocateTuples(vtkIdType numTuples);
258 
263  bool ReallocateTuples(vtkIdType numTuples);
264 
265  std::vector<vtkBuffer<ValueType>*> Data;
267 
268 private:
270  void operator=(const vtkScaledSOADataArrayTemplate&) = delete;
271 
272  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx, vtkIdType& tupleIdx, int& comp) const
273  {
274  tupleIdx = valueIdx / this->NumberOfComponents;
275  comp = valueIdx % this->NumberOfComponents;
276  }
277 
278  friend class vtkGenericDataArray<vtkScaledSOADataArrayTemplate<ValueTypeT>, ValueTypeT>;
282  ValueType Scale;
283 };
284 
285 // Declare vtkArrayDownCast implementations for scale SoA containers:
287 
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  template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate<T>
297 #elif defined(VTK_USE_EXTERN_TEMPLATE)
298 #ifndef VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
299 #define VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
300 #ifdef _MSC_VER
301 #pragma warning(push)
302 // The following is needed when the vtkScaledSOADataArrayTemplate is declared
303 // dllexport and is used from another class in vtkCommonCore
304 #pragma warning(disable : 4910) // extern and dllexport incompatible
305 #endif
306 vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate);
307 #ifdef _MSC_VER
308 #pragma warning(pop)
309 #endif
310 #endif // VTK_SCALED_SOA_DATA_ARRAY_TEMPLATE_EXTERN
311 
312 // The following clause is only for MSVC 2008 and 2010
313 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
314 #pragma warning(push)
315 
316 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
317 #pragma warning(disable : 4091)
318 
319 // Compiler-specific extension warning.
320 #pragma warning(disable : 4231)
321 
322 // We need to disable warning 4910 and do an extern dllexport
323 // anyway. When deriving new arrays from an
324 // instantiation of this template the compiler does an explicit
325 // instantiation of the base class. From outside the vtkCommon
326 // library we block this using an extern dllimport instantiation.
327 // For classes inside vtkCommon we should be able to just do an
328 // extern instantiation, but VS 2008 complains about missing
329 // definitions. We cannot do an extern dllimport inside vtkCommon
330 // since the symbols are local to the dll. An extern dllexport
331 // seems to be the only way to convince VS 2008 to do the right
332 // thing, so we just disable the warning.
333 #pragma warning(disable : 4910) // extern and dllexport incompatible
334 
335 // Use an "extern explicit instantiation" to give the class a DLL
336 // interface. This is a compiler-specific extension.
338  extern template class VTKCOMMONCORE_EXPORT vtkScaledSOADataArrayTemplate);
339 
340 #pragma warning(pop)
341 
342 #endif
343 
344 // 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:50
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:31
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
Copy n consecutive tuples starting at srcStart from the source array to this array,...
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
Copy the tuples indexed in srcIds from the source array to the tuple locations indexed by dstIds in t...
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.
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:226
@ scale
Definition: vtkX3D.h:235
@ size
Definition: vtkX3D.h:259
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:30
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
vtkArrayDownCast_TemplateFastCastMacro(vtkScaledSOADataArrayTemplate)
#define vtkExternTemplateMacro(decl)
A macro to declare extern templates for all numerical types.
Definition: vtkType.h:403
int vtkIdType
Definition: vtkType.h:338
#define vtkInstantiateTemplateMacro(decl)
A macro to instantiate a template over all numerical types.
Definition: vtkType.h:384
void save(Archiver &ar, const vtkUnicodeString &str, const unsigned int vtkNotUsed(version))
#define VTK_ZEROCOPY
#define VTK_NEWINSTANCE