VTK  9.2.20220705
vtkSOADataArrayTemplate.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkSOADataArrayTemplate.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 =========================================================================*/
31 #ifndef vtkSOADataArrayTemplate_h
32 #define vtkSOADataArrayTemplate_h
33 
34 #include "vtkBuffer.h"
35 #include "vtkCommonCoreModule.h" // For export macro
36 #include "vtkCompiler.h" // for VTK_USE_EXTERN_TEMPLATE
37 #include "vtkGenericDataArray.h"
38 
39 // The export macro below makes no sense, but is necessary for older compilers
40 // when we export instantiations of this class from vtkCommonCore.
41 template <class ValueTypeT>
42 class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate
43  : public vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT>
44 {
46 
47 public:
50  typedef typename Superclass::ValueType ValueType;
51 
53  {
55  VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
56  VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
57  VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
58  };
59 
61 
63 
66  inline ValueType GetValue(vtkIdType valueIdx) const
67  {
68  vtkIdType tupleIdx;
69  int comp;
70  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
71  return this->GetTypedComponent(tupleIdx, comp);
72  }
74 
76 
79  inline void SetValue(vtkIdType valueIdx, ValueType value)
80  {
81  vtkIdType tupleIdx;
82  int comp;
83  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
84  this->SetTypedComponent(tupleIdx, comp, value);
85  }
87 
91  inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
92  {
93  for (size_t cc = 0; cc < this->Data.size(); cc++)
94  {
95  tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx];
96  }
97  }
98 
102  inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
103  {
104  for (size_t cc = 0; cc < this->Data.size(); ++cc)
105  {
106  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc];
107  }
108  }
109 
113  inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
114  {
115  return this->Data[comp]->GetBuffer()[tupleIdx];
116  }
117 
121  inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
122  {
123  this->Data[comp]->GetBuffer()[tupleIdx] = value;
124  }
125 
129  void FillTypedComponent(int compIdx, ValueType value) override;
130 
144  void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size, bool updateMaxId = false,
145  bool save = false, int deleteMethod = VTK_DATA_ARRAY_FREE);
146 
154  void SetArrayFreeFunction(void (*callback)(void*)) override;
155 
162  void SetArrayFreeFunction(int comp, void (*callback)(void*));
163 
169 
174  void* GetVoidPointer(vtkIdType valueIdx) override;
175 
180  void ExportToVoidPointer(void* ptr) override;
181 
182 #ifndef __VTK_WRAP__
184 
192 #endif
193 
194  int GetArrayType() const override { return vtkAbstractArray::SoADataArrayTemplate; }
196  void SetNumberOfComponents(int numComps) override;
197  void ShallowCopy(vtkDataArray* other) override;
198 
199  // Reimplemented for efficiency:
201  vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
202  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
203  // using Superclass::InsertTuples;
204  void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
205  {
206  this->Superclass::InsertTuples(dstIds, srcIds, source);
207  }
209  vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override
210  {
211  this->Superclass::InsertTuplesStartingAt(dstStart, srcIds, source);
212  }
213 
214 protected:
217 
222  bool AllocateTuples(vtkIdType numTuples);
223 
228  bool ReallocateTuples(vtkIdType numTuples);
229 
230  std::vector<vtkBuffer<ValueType>*> Data;
232 
233 private:
235  void operator=(const vtkSOADataArrayTemplate&) = delete;
236 
237  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx, vtkIdType& tupleIdx, int& comp) const
238  {
239  tupleIdx = valueIdx / this->NumberOfComponents;
240  comp = valueIdx % this->NumberOfComponents;
241  }
242 
243  friend class vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT>;
244 };
245 
246 // Declare vtkArrayDownCast implementations for SoA containers:
248 
249 #endif // header guard
250 
251 // This portion must be OUTSIDE the include blockers. This is used to tell
252 // libraries other than vtkCommonCore that instantiations of
253 // vtkSOADataArrayTemplate can be found externally. This prevents each library
254 // from instantiating these on their own.
255 #ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
256 #define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
257  namespace vtkDataArrayPrivate \
258  { \
259  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<T>, double); \
260  } \
261  template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate<T>
262 
263 #elif defined(VTK_USE_EXTERN_TEMPLATE)
264 #ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
265 #define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
266 #ifdef _MSC_VER
267 #pragma warning(push)
268 // The following is needed when the vtkSOADataArrayTemplate is declared
269 // dllexport and is used from another class in vtkCommonCore
270 #pragma warning(disable : 4910) // extern and dllexport incompatible
271 #endif
272 vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
273 #ifdef _MSC_VER
274 #pragma warning(pop)
275 #endif
276 #endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
277 
278 // The following clause is only for MSVC 2008 and 2010
279 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
280 #pragma warning(push)
281 
282 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
283 #pragma warning(disable : 4091)
284 
285 // Compiler-specific extension warning.
286 #pragma warning(disable : 4231)
287 
288 // We need to disable warning 4910 and do an extern dllexport
289 // anyway. When deriving new arrays from an
290 // instantiation of this template the compiler does an explicit
291 // instantiation of the base class. From outside the vtkCommon
292 // library we block this using an extern dllimport instantiation.
293 // For classes inside vtkCommon we should be able to just do an
294 // extern instantiation, but VS 2008 complains about missing
295 // definitions. We cannot do an extern dllimport inside vtkCommon
296 // since the symbols are local to the dll. An extern dllexport
297 // seems to be the only way to convince VS 2008 to do the right
298 // thing, so we just disable the warning.
299 #pragma warning(disable : 4910) // extern and dllexport incompatible
300 
301 // Use an "extern explicit instantiation" to give the class a DLL
302 // interface. This is a compiler-specific extension.
303 vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
304 
305 #pragma warning(pop)
306 
307 #endif
308 
309 // VTK-HeaderTest-Exclude: vtkSOADataArrayTemplate.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:165
Base interface for all typed vtkDataArray subclasses.
list of point or cell ids
Definition: vtkIdList.h:143
Struct-Of-Arrays implementation of vtkGenericDataArray.
void ShallowCopy(vtkDataArray *other) override
Create a shallow copy of other into this, if possible.
void ExportToVoidPointer(void *ptr) override
Export a copy of the data in AoS ordering to the preallocated memory buffer.
Superclass::ValueType ValueType
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...
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
Get component comp of the tuple at tupleIdx.
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
vtkTemplateTypeMacro(SelfType, GenericDataArrayType)
void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
void FillTypedComponent(int compIdx, ValueType value) override
Set component comp of all tuples to value.
void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray *source) override
See documentation from parent class.
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...
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
~vtkSOADataArrayTemplate() override
vtkBuffer< ValueType > * AoSCopy
void InsertTuplesStartingAt(vtkIdType dstStart, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
vtkSOADataArrayTemplate< ValueTypeT > SelfType
static vtkSOADataArrayTemplate< ValueType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
std::vector< vtkBuffer< ValueType > * > Data
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.
static vtkSOADataArrayTemplate * New()
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...
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array's tuple at tupleIdx to the values in tuple.
ValueType * GetComponentArrayPointer(int comp)
Return a pointer to a contiguous block of memory containing all values for a particular components (i...
int GetArrayType() const override
Method for type-checking in FastDownCast implementations.
void SetNumberOfComponents(int numComps) override
Set/Get the dimension (n) of the components.
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
@ size
Definition: vtkX3D.h:259
vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkArrayIteratorTemplate)
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
vtkArrayDownCast_TemplateFastCastMacro(vtkSOADataArrayTemplate)
int vtkIdType
Definition: vtkType.h:325
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))
#define VTK_ZEROCOPY
#define VTK_NEWINSTANCE