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 =========================================================================*/
31 #ifndef vtkSOADataArrayTemplate_h
32 #define vtkSOADataArrayTemplate_h
33 
34 #include "vtkCommonCoreModule.h" // For export macro
35 #include "vtkGenericDataArray.h"
36 #include "vtkBuffer.h"
37 
38 // The export macro below makes no sense, but is necessary for older compilers
39 // when we export instantiations of this class from vtkCommonCore.
40 template <class ValueTypeT>
41 class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate :
42  public vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>, ValueTypeT>
43 {
46 public:
48  vtkTemplateTypeMacro(SelfType, GenericDataArrayType)
49  typedef typename Superclass::ValueType ValueType;
50 
52  {
55  VTK_DATA_ARRAY_ALIGNED_FREE=vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
57  };
58 
59  static vtkSOADataArrayTemplate* New();
60 
62 
65  inline ValueType GetValue(vtkIdType valueIdx) const
66  {
67  vtkIdType tupleIdx;
68  int comp;
69  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
70  return this->GetTypedComponent(tupleIdx, comp);
71  }
73 
75 
78  inline void SetValue(vtkIdType valueIdx, ValueType value)
79  {
80  vtkIdType tupleIdx;
81  int comp;
82  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
83  this->SetTypedComponent(tupleIdx, comp, value);
84  }
86 
90  inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
91  {
92  for (size_t cc=0; cc < this->Data.size(); cc++)
93  {
94  tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx];
95  }
96  }
97 
101  inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
102  {
103  for (size_t cc=0; cc < this->Data.size(); ++cc)
104  {
105  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc];
106  }
107  }
108 
112  inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
113  {
114  return this->Data[comp]->GetBuffer()[tupleIdx];
115  }
116 
120  inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
121  {
122  this->Data[comp]->GetBuffer()[tupleIdx] = value;
123  }
124 
128  void FillTypedComponent(int compIdx, ValueType value) override;
129 
143  void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size,
144  bool updateMaxId = false, bool save=false,
145  int deleteMethod=VTK_DATA_ARRAY_FREE);
146 
154  void SetArrayFreeFunction(void (*callback)(void *)) override;
155 
162  void SetArrayFreeFunction(int comp, void (*callback)(void *));
163 
168  ValueType* GetComponentArrayPointer(int comp);
169 
174  void *GetVoidPointer(vtkIdType valueIdx) override;
175 
180  void ExportToVoidPointer(void *ptr) override;
181 
182 #ifndef __VTK_WRAP__
183 
184 
192  {
193  if (source)
194  {
195  switch (source->GetArrayType())
196  {
198  if (vtkDataTypesCompare(source->GetDataType(),
200  {
201  return static_cast<vtkSOADataArrayTemplate<ValueType>*>(source);
202  }
203  break;
204  }
205  }
206  return nullptr;
207  }
209 #endif
210 
213  void SetNumberOfComponents(int numComps) override;
214  void ShallowCopy(vtkDataArray *other) override;
215 
216  // Reimplemented for efficiency:
217  void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart,
218  vtkAbstractArray* source) override;
219  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
220  // using Superclass::InsertTuples;
221  void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds,
222  vtkAbstractArray *source) override
223  { this->Superclass::InsertTuples(dstIds, srcIds, source); }
224 
225 protected:
227  ~vtkSOADataArrayTemplate() override;
228 
233  bool AllocateTuples(vtkIdType numTuples);
234 
239  bool ReallocateTuples(vtkIdType numTuples);
240 
241  std::vector<vtkBuffer<ValueType>*> Data;
243 
245 
246 private:
248  void operator=(const vtkSOADataArrayTemplate&) = delete;
249 
250  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx,
251  vtkIdType& tupleIdx, int& comp) const
252  {
253  tupleIdx = static_cast<vtkIdType>(valueIdx *
254  this->NumberOfComponentsReciprocal);
255  comp = valueIdx - (tupleIdx * this->NumberOfComponents);
256  }
257 
258  friend class vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>,
259  ValueTypeT>;
260 };
261 
262 // Declare vtkArrayDownCast implementations for SoA containers:
264 
265 #endif // header guard
266 
267 // This portion must be OUTSIDE the include blockers. This is used to tell
268 // libraries other than vtkCommonCore that instantiations of
269 // vtkSOADataArrayTemplate can be found externally. This prevents each library
270 // from instantiating these on their own.
271 #ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
272 #define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
273  template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate< T >
274 #elif defined(VTK_USE_EXTERN_TEMPLATE)
275 #ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
276 #define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
277 #ifdef _MSC_VER
278 #pragma warning (push)
279 // The following is needed when the vtkSOADataArrayTemplate is declared
280 // dllexport and is used from another class in vtkCommonCore
281 #pragma warning (disable: 4910) // extern and dllexport incompatible
282 #endif
284  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
285 #ifdef _MSC_VER
286 #pragma warning (pop)
287 #endif
288 #endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
289 
290 // The following clause is only for MSVC 2008 and 2010
291 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
292 #pragma warning (push)
293 
294 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
295 #pragma warning (disable: 4091)
296 
297 // Compiler-specific extension warning.
298 #pragma warning (disable: 4231)
299 
300 // We need to disable warning 4910 and do an extern dllexport
301 // anyway. When deriving new arrays from an
302 // instantiation of this template the compiler does an explicit
303 // instantiation of the base class. From outside the vtkCommon
304 // library we block this using an extern dllimport instantiation.
305 // For classes inside vtkCommon we should be able to just do an
306 // extern instantiation, but VS 2008 complains about missing
307 // definitions. We cannot do an extern dllimport inside vtkCommon
308 // since the symbols are local to the dll. An extern dllexport
309 // seems to be the only way to convince VS 2008 to do the right
310 // thing, so we just disable the warning.
311 #pragma warning (disable: 4910) // extern and dllexport incompatible
312 
313 // Use an "extern explicit instantiation" to give the class a DLL
314 // interface. This is a compiler-specific extension.
316  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
317 
318 #pragma warning (pop)
319 
320 #endif
321 
322 // VTK-HeaderTest-Exclude: vtkSOADataArrayTemplate.h
Struct-Of-Arrays implementation of vtkGenericDataArray.
vtkBuffer< ValueType > * AoSCopy
abstract base class for most VTK objects
Definition: vtkObject.h:59
virtual void ShallowCopy(vtkDataArray *other)
Create a shallow copy of other into this, if possible.
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.
virtual int GetDataType()=0
Return the underlying data type.
virtual int GetArrayType()
Method for type-checking in FastDownCast implementations.
int vtkIdType
Definition: vtkType.h:347
Base interface for all typed vtkDataArray subclasses.
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array&#39;s tuple at tupleIdx to the values in tuple.
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into 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:36
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:54
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 >
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.
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
void SetNumberOfComponents(int num) override
Set/Get the dimension (n) of the components.
ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
Get component comp of the tuple at tupleIdx.
int GetArrayType() override
Method for type-checking in FastDownCast implementations.
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:32
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
virtual void ExportToVoidPointer(void *out_ptr)
This method copies the array data to the void pointer specified by the user.