VTK
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 "vtkCommonCoreModule.h" // For export macro
32 #include "vtkGenericDataArray.h"
33 #include "vtkBuffer.h"
34 
35 // The export macro below makes no sense, but is necessary for older compilers
36 // when we export instantiations of this class from vtkCommonCore.
37 template <class ValueTypeT>
38 class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate :
39  public vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT>
40 {
43 public:
46  typedef typename Superclass::ValueType ValueType;
47 
49  {
52  VTK_DATA_ARRAY_ALIGNED_FREE=vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
54  };
55 
56  static vtkSOADataArrayTemplate* New();
57 
59 
62  inline ValueType GetValue(vtkIdType valueIdx) const
63  {
64  vtkIdType tupleIdx;
65  int comp;
66  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
67  return this->GetTypedComponent(tupleIdx, comp);
68  }
70 
72 
75  inline void SetValue(vtkIdType valueIdx, ValueType value)
76  {
77  vtkIdType tupleIdx;
78  int comp;
79  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
80  this->SetTypedComponent(tupleIdx, comp, value);
81  }
83 
87  inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
88  {
89  for (size_t cc=0; cc < this->Data.size(); cc++)
90  {
91  tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx];
92  }
93  }
94 
98  inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
99  {
100  for (size_t cc=0; cc < this->Data.size(); ++cc)
101  {
102  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc];
103  }
104  }
105 
109  inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
110  {
111  return this->Data[comp]->GetBuffer()[tupleIdx];
112  }
113 
117  inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
118  {
119  this->Data[comp]->GetBuffer()[tupleIdx] = value;
120  }
121 
125  void FillTypedComponent(int compIdx, ValueType value) override;
126 
140  void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size,
141  bool updateMaxId = false, bool save=false,
142  int deleteMethod=VTK_DATA_ARRAY_FREE);
143 
151  void SetArrayFreeFunction(void (*callback)(void *)) override;
152 
159  void SetArrayFreeFunction(int comp, void (*callback)(void *));
160 
165  ValueType* GetComponentArrayPointer(int comp);
166 
171  void *GetVoidPointer(vtkIdType valueIdx) override;
172 
177  void ExportToVoidPointer(void *ptr) override;
178 
179 #ifndef __VTK_WRAP__
180 
181 
189  {
190  if (source)
191  {
192  switch (source->GetArrayType())
193  {
195  if (vtkDataTypesCompare(source->GetDataType(),
197  {
198  return static_cast<vtkSOADataArrayTemplate<ValueType>*>(source);
199  }
200  break;
201  }
202  }
203  return nullptr;
204  }
206 #endif
207 
210  void SetNumberOfComponents(int numComps) override;
211  void ShallowCopy(vtkDataArray *other) override;
212 
213  // Reimplemented for efficiency:
214  void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart,
215  vtkAbstractArray* source) override;
216  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
217  // using Superclass::InsertTuples;
218  void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds,
219  vtkAbstractArray *source) override
220  { this->Superclass::InsertTuples(dstIds, srcIds, source); }
221 
222 protected:
224  ~vtkSOADataArrayTemplate() override;
225 
230  bool AllocateTuples(vtkIdType numTuples);
231 
236  bool ReallocateTuples(vtkIdType numTuples);
237 
238  std::vector<vtkBuffer<ValueType>*> Data;
240 
242 
243 private:
245  void operator=(const vtkSOADataArrayTemplate&) = delete;
246 
247  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx,
248  vtkIdType& tupleIdx, int& comp) const
249  {
250  tupleIdx = static_cast<vtkIdType>(valueIdx *
251  this->NumberOfComponentsReciprocal);
252  comp = valueIdx - (tupleIdx * this->NumberOfComponents);
253  }
254 
255  friend class vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>,
256  ValueTypeT>;
257 };
258 
259 // Declare vtkArrayDownCast implementations for SoA containers:
261 
262 #endif // header guard
263 
264 // This portion must be OUTSIDE the include blockers. This is used to tell
265 // libraries other than vtkCommonCore that instantiations of
266 // vtkSOADataArrayTemplate can be found externally. This prevents each library
267 // from instantiating these on their own.
268 #ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
269 #define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
270  template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate< T >
271 #elif defined(VTK_USE_EXTERN_TEMPLATE)
272 #ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
273 #define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
274 #ifdef _MSC_VER
275 #pragma warning (push)
276 // The following is needed when the vtkSOADataArrayTemplate is declared
277 // dllexport and is used from another class in vtkCommonCore
278 #pragma warning (disable: 4910) // extern and dllexport incompatible
279 #endif
281  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
282 #ifdef _MSC_VER
283 #pragma warning (pop)
284 #endif
285 #endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
286 
287 // The following clause is only for MSVC 2008 and 2010
288 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
289 #pragma warning (push)
290 
291 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
292 #pragma warning (disable: 4091)
293 
294 // Compiler-specific extension warning.
295 #pragma warning (disable: 4231)
296 
297 // We need to disable warning 4910 and do an extern dllexport
298 // anyway. When deriving new arrays from an
299 // instantiation of this template the compiler does an explicit
300 // instantiation of the base class. From outside the vtkCommon
301 // library we block this using an extern dllimport instantiation.
302 // For classes inside vtkCommon we should be able to just do an
303 // extern instantiation, but VS 2008 complains about missing
304 // definitions. We cannot do an extern dllimport inside vtkCommon
305 // since the symbols are local to the dll. An extern dllexport
306 // seems to be the only way to convince VS 2008 to do the right
307 // thing, so we just disable the warning.
308 #pragma warning (disable: 4910) // extern and dllexport incompatible
309 
310 // Use an "extern explicit instantiation" to give the class a DLL
311 // interface. This is a compiler-specific extension.
313  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
314 
315 #pragma warning (pop)
316 
317 #endif
318 
319 // VTK-HeaderTest-Exclude: vtkSOADataArrayTemplate.h
Struct-Of-Arrays implementation of vtkGenericDataArray.
vtkBuffer< ValueType > * AoSCopy
ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
Get component comp of the tuple at tupleIdx.
virtual void ShallowCopy(vtkDataArray *other)
Create a shallow copy of other into this, if possible.
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
Abstract superclass for all arrays.
vtkTemplateTypeMacro(SelfType, vtkDataArray) enum
Compile time access to the VTK type identifier.
static vtkSOADataArrayTemplate< ValueType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
void SetArrayFreeFunction(void(*callback)(void *)) override
Default implementation raises a runtime error.
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
Set component compIdx of the tuple at tupleIdx to value.
int vtkIdType
Definition: vtkType.h:347
Base interface for all typed vtkDataArray subclasses.
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array's tuple at tupleIdx to the values in tuple.
#define vtkExternTemplateMacro(decl)
A macro to declare extern templates for all numerical types.
Definition: vtkType.h:415
list of point or cell ids
Definition: vtkIdList.h:30
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
vtkSOADataArrayTemplate< ValueTypeT > SelfType
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:48
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...
Abstract superclass to iterate over elements in an vtkAbstractArray.
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
#define VTK_NEWINSTANCE
#define vtkInstantiateTemplateMacro(decl)
A macro to instantiate a template over all numerical types.
Definition: vtkType.h:395
void * GetVoidPointer(vtkIdType valueIdx) override
Default implementation raises a runtime error.
void save(Archiver &ar, const vtkUnicodeString &str, const unsigned int vtkNotUsed(version))
vtkArrayDownCast_TemplateFastCastMacro(vtkTypedDataArray) template< class Scalar > inline typename vtkTypedDataArray< Scalar >
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
#define VTK_ZEROCOPY
std::vector< vtkBuffer< ValueType > * > Data
virtual void FillTypedComponent(int compIdx, ValueType value)
Set component comp of all tuples to value.
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...
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
void SetNumberOfComponents(int num) override
Set/Get the dimension (n) of the components.
int GetArrayType() override
Method for type-checking in FastDownCast implementations.
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:29
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
virtual void ExportToVoidPointer(void *out_ptr)
This method copies the array data to the void pointer specified by the user.