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:
49  typedef typename Superclass::ValueType ValueType;
50 
52  {
56  };
57 
58  static vtkSOADataArrayTemplate* New();
59 
61 
64  inline ValueType GetValue(vtkIdType valueIdx) const
65  {
66  vtkIdType tupleIdx;
67  int comp;
68  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
69  return this->GetTypedComponent(tupleIdx, comp);
70  }
72 
74 
77  inline void SetValue(vtkIdType valueIdx, ValueType value)
78  {
79  vtkIdType tupleIdx;
80  int comp;
81  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
82  this->SetTypedComponent(tupleIdx, comp, value);
83  }
85 
89  inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
90  {
91  for (size_t cc=0; cc < this->Data.size(); cc++)
92  {
93  tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx];
94  }
95  }
96 
100  inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
101  {
102  for (size_t cc=0; cc < this->Data.size(); ++cc)
103  {
104  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc];
105  }
106  }
107 
111  inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
112  {
113  return this->Data[comp]->GetBuffer()[tupleIdx];
114  }
115 
119  inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
120  {
121  this->Data[comp]->GetBuffer()[tupleIdx] = value;
122  }
123 
127  void FillTypedComponent(int compIdx, ValueType value) override;
128 
142  void SetArray(int comp, VTK_ZEROCOPY ValueType* array, vtkIdType size,
143  bool updateMaxId = false, bool save=false,
144  int deleteMethod=VTK_DATA_ARRAY_FREE);
145 
150  ValueType* GetComponentArrayPointer(int comp);
151 
156  void *GetVoidPointer(vtkIdType valueIdx) override;
157 
162  void ExportToVoidPointer(void *ptr) override;
163 
164 #ifndef __VTK_WRAP__
165 
166 
174  {
175  if (source)
176  {
177  switch (source->GetArrayType())
178  {
180  if (vtkDataTypesCompare(source->GetDataType(),
182  {
183  return static_cast<vtkSOADataArrayTemplate<ValueType>*>(source);
184  }
185  break;
186  }
187  }
188  return nullptr;
189  }
191 #endif
192 
195  void SetNumberOfComponents(int numComps) override;
196  void ShallowCopy(vtkDataArray *other) override;
197 
198  // Reimplemented for efficiency:
199  void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart,
200  vtkAbstractArray* source) override;
201  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
202  // using Superclass::InsertTuples;
203  void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds,
204  vtkAbstractArray *source) override
205  { this->Superclass::InsertTuples(dstIds, srcIds, source); }
206 
207 protected:
209  ~vtkSOADataArrayTemplate() override;
210 
215  bool AllocateTuples(vtkIdType numTuples);
216 
221  bool ReallocateTuples(vtkIdType numTuples);
222 
223  std::vector<vtkBuffer<ValueType>*> Data;
225 
227 
228 private:
230  void operator=(const vtkSOADataArrayTemplate&) = delete;
231 
232  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx,
233  vtkIdType& tupleIdx, int& comp) const
234  {
235  tupleIdx = static_cast<vtkIdType>(valueIdx *
236  this->NumberOfComponentsReciprocal);
237  comp = valueIdx - (tupleIdx * this->NumberOfComponents);
238  }
239 
240  friend class vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>,
241  ValueTypeT>;
242 };
243 
244 // Declare vtkArrayDownCast implementations for SoA containers:
246 
247 #endif // header guard
248 
249 // This portion must be OUTSIDE the include blockers. This is used to tell
250 // libraries other than vtkCommonCore that instantiations of
251 // vtkSOADataArrayTemplate can be found externally. This prevents each library
252 // from instantiating these on their own.
253 #ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
254 #define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
255  template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate< T >
256 #elif defined(VTK_USE_EXTERN_TEMPLATE)
257 #ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
258 #define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
259 #ifdef _MSC_VER
260 #pragma warning (push)
261 // The following is needed when the vtkSOADataArrayTemplate is declared
262 // dllexport and is used from another class in vtkCommonCore
263 #pragma warning (disable: 4910) // extern and dllexport incompatible
264 #endif
266  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
267 #ifdef _MSC_VER
268 #pragma warning (pop)
269 #endif
270 #endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
271 
272 // The following clause is only for MSVC 2008 and 2010
273 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
274 #pragma warning (push)
275 
276 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
277 #pragma warning (disable: 4091)
278 
279 // Compiler-specific extension warning.
280 #pragma warning (disable: 4231)
281 
282 // We need to disable warning 4910 and do an extern dllexport
283 // anyway. When deriving new arrays from an
284 // instantiation of this template the compiler does an explicit
285 // instantiation of the base class. From outside the vtkCommon
286 // library we block this using an extern dllimport instantiation.
287 // For classes inside vtkCommon we should be able to just do an
288 // extern instantiation, but VS 2008 complains about missing
289 // definitions. We cannot do an extern dllimport inside vtkCommon
290 // since the symbols are local to the dll. An extern dllexport
291 // seems to be the only way to convince VS 2008 to do the right
292 // thing, so we just disable the warning.
293 #pragma warning (disable: 4910) // extern and dllexport incompatible
294 
295 // Use an "extern explicit instantiation" to give the class a DLL
296 // interface. This is a compiler-specific extension.
298  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
299 
300 #pragma warning (pop)
301 
302 #endif
303 
304 // 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 SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
Set component compIdx of the tuple at tupleIdx to value.
int vtkIdType
Definition: vtkType.h:345
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:411
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:391
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:32
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.