VTK  9.1.20211115
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 =========================================================================*/
28 #ifndef vtkSOADataArrayTemplate_h
29 #define vtkSOADataArrayTemplate_h
30 
31 #include "vtkBuffer.h"
32 #include "vtkCommonCoreModule.h" // For export macro
33 #include "vtkCompiler.h" // for VTK_USE_EXTERN_TEMPLATE
34 #include "vtkGenericDataArray.h"
35 
36 // The export macro below makes no sense, but is necessary for older compilers
37 // when we export instantiations of this class from vtkCommonCore.
38 template <class ValueTypeT>
39 class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate
40  : public vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT>
41 {
43 
44 public:
47  typedef typename Superclass::ValueType ValueType;
48 
50  {
52  VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
53  VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
54  VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
55  };
56 
58 
60 
63  inline ValueType GetValue(vtkIdType valueIdx) const
64  {
65  vtkIdType tupleIdx;
66  int comp;
67  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
68  return this->GetTypedComponent(tupleIdx, comp);
69  }
71 
73 
76  inline void SetValue(vtkIdType valueIdx, ValueType value)
77  {
78  vtkIdType tupleIdx;
79  int comp;
80  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
81  this->SetTypedComponent(tupleIdx, comp, value);
82  }
84 
88  inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
89  {
90  for (size_t cc = 0; cc < this->Data.size(); cc++)
91  {
92  tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx];
93  }
94  }
95 
99  inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
100  {
101  for (size_t cc = 0; cc < this->Data.size(); ++cc)
102  {
103  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc];
104  }
105  }
106 
110  inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
111  {
112  return this->Data[comp]->GetBuffer()[tupleIdx];
113  }
114 
118  inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
119  {
120  this->Data[comp]->GetBuffer()[tupleIdx] = value;
121  }
122 
126  void FillTypedComponent(int compIdx, ValueType value) override;
127 
141  void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size, bool updateMaxId = false,
142  bool save = false, int deleteMethod = VTK_DATA_ARRAY_FREE);
143 
151  void SetArrayFreeFunction(void (*callback)(void*)) override;
152 
159  void SetArrayFreeFunction(int comp, void (*callback)(void*));
160 
166 
171  void* GetVoidPointer(vtkIdType valueIdx) override;
172 
177  void ExportToVoidPointer(void* ptr) override;
178 
179 #ifndef __VTK_WRAP__
181 
189 #endif
190 
191  int GetArrayType() const override { return vtkAbstractArray::SoADataArrayTemplate; }
193  void SetNumberOfComponents(int numComps) override;
194  void ShallowCopy(vtkDataArray* other) override;
195 
196  // Reimplemented for efficiency:
198  vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
199  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
200  // using Superclass::InsertTuples;
201  void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
202  {
203  this->Superclass::InsertTuples(dstIds, srcIds, source);
204  }
205  void InsertTuples(vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override
206  {
207  this->Superclass::InsertTuples(dstStart, srcIds, source);
208  }
209 
210 protected:
213 
218  bool AllocateTuples(vtkIdType numTuples);
219 
224  bool ReallocateTuples(vtkIdType numTuples);
225 
226  std::vector<vtkBuffer<ValueType>*> Data;
228 
229 private:
231  void operator=(const vtkSOADataArrayTemplate&) = delete;
232 
233  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx, vtkIdType& tupleIdx, int& comp) const
234  {
235  tupleIdx = valueIdx / this->NumberOfComponents;
236  comp = valueIdx % this->NumberOfComponents;
237  }
238 
239  friend class vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT>;
240 };
241 
242 // Declare vtkArrayDownCast implementations for SoA containers:
244 
245 #endif // header guard
246 
247 // This portion must be OUTSIDE the include blockers. This is used to tell
248 // libraries other than vtkCommonCore that instantiations of
249 // vtkSOADataArrayTemplate can be found externally. This prevents each library
250 // from instantiating these on their own.
251 #ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
252 #define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
253  namespace vtkDataArrayPrivate \
254  { \
255  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkSOADataArrayTemplate<T>, double); \
256  } \
257  template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate<T>
258 
259 #elif defined(VTK_USE_EXTERN_TEMPLATE)
260 #ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
261 #define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
262 #ifdef _MSC_VER
263 #pragma warning(push)
264 // The following is needed when the vtkSOADataArrayTemplate is declared
265 // dllexport and is used from another class in vtkCommonCore
266 #pragma warning(disable : 4910) // extern and dllexport incompatible
267 #endif
268 vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
269 #ifdef _MSC_VER
270 #pragma warning(pop)
271 #endif
272 #endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
273 
274 // The following clause is only for MSVC 2008 and 2010
275 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
276 #pragma warning(push)
277 
278 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
279 #pragma warning(disable : 4091)
280 
281 // Compiler-specific extension warning.
282 #pragma warning(disable : 4231)
283 
284 // We need to disable warning 4910 and do an extern dllexport
285 // anyway. When deriving new arrays from an
286 // instantiation of this template the compiler does an explicit
287 // instantiation of the base class. From outside the vtkCommon
288 // library we block this using an extern dllimport instantiation.
289 // For classes inside vtkCommon we should be able to just do an
290 // extern instantiation, but VS 2008 complains about missing
291 // definitions. We cannot do an extern dllimport inside vtkCommon
292 // since the symbols are local to the dll. An extern dllexport
293 // seems to be the only way to convince VS 2008 to do the right
294 // thing, so we just disable the warning.
295 #pragma warning(disable : 4910) // extern and dllexport incompatible
296 
297 // Use an "extern explicit instantiation" to give the class a DLL
298 // interface. This is a compiler-specific extension.
299 vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate);
300 
301 #pragma warning(pop)
302 
303 #endif
304 
305 // 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:159
Base interface for all typed vtkDataArray subclasses.
list of point or cell ids
Definition: vtkIdList.h:140
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
Copy the tuples indexed in srcIds from the source array to the tuple locations indexed by dstIds in t...
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
Copy n consecutive tuples starting at srcStart from the source array to this array,...
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
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
void InsertTuples(vtkIdType dstStart, vtkIdList *srcIds, vtkAbstractArray *source) override
Copy the tuples indexed in srcIds from the source array to the tuple locations starting at index dstS...
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:332
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))
#define VTK_ZEROCOPY
#define VTK_NEWINSTANCE