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__
211 
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
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.
ValueType GetScale() const
Set/Get the Scale value for the object.
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
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:332
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))
#define VTK_ZEROCOPY
#define VTK_NEWINSTANCE