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  {
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) VTK_OVERRIDE;
128 
142  void SetArray(int comp, 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) VTK_OVERRIDE;
157 
162  void ExportToVoidPointer(void *ptr) VTK_OVERRIDE;
163 
165 
173  {
174  if (source)
175  {
176  switch (source->GetArrayType())
177  {
179  if (vtkDataTypesCompare(source->GetDataType(),
181  {
182  return static_cast<vtkSOADataArrayTemplate<ValueType>*>(source);
183  }
184  break;
185  }
186  }
187  return NULL;
188  }
190 
193  void SetNumberOfComponents(int numComps) VTK_OVERRIDE;
194  void ShallowCopy(vtkDataArray *other) VTK_OVERRIDE;
195 
196  // Reimplemented for efficiency:
197  void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart,
198  vtkAbstractArray* source) VTK_OVERRIDE;
199  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
200  // using Superclass::InsertTuples;
201  void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds,
202  vtkAbstractArray *source) VTK_OVERRIDE
203  { this->Superclass::InsertTuples(dstIds, srcIds, source); }
204 
205 protected:
207  ~vtkSOADataArrayTemplate() VTK_OVERRIDE;
208 
213  bool AllocateTuples(vtkIdType numTuples);
214 
219  bool ReallocateTuples(vtkIdType numTuples);
220 
221  std::vector<vtkBuffer<ValueType>*> Data;
223 
225 
226 private:
227  vtkSOADataArrayTemplate(const vtkSOADataArrayTemplate&) VTK_DELETE_FUNCTION;
228  void operator=(const vtkSOADataArrayTemplate&) VTK_DELETE_FUNCTION;
229 
230  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx,
231  vtkIdType& tupleIdx, int& comp) const
232  {
233  tupleIdx = static_cast<vtkIdType>(valueIdx *
234  this->NumberOfComponentsReciprocal);
235  comp = valueIdx - (tupleIdx * this->NumberOfComponents);
236  }
237 
238  friend class vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>,
239  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  template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate< T >
254 #elif defined(VTK_USE_EXTERN_TEMPLATE)
255 #ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
256 #define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
257 #ifdef _MSC_VER
258 #pragma warning (push)
259 // The following is needed when the vtkSOADataArrayTemplate is declared
260 // dllexport and is used from another class in vtkCommonCore
261 #pragma warning (disable: 4910) // extern and dllexport incompatible
262 #endif
264  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
265 #ifdef _MSC_VER
266 #pragma warning (pop)
267 #endif
268 #endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
269 
270 // The following clause is only for MSVC 2008 and 2010
271 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
272 #pragma warning (push)
273 
274 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
275 #pragma warning (disable: 4091)
276 
277 // Compiler-specific extension warning.
278 #pragma warning (disable: 4231)
279 
280 // We need to disable warning 4910 and do an extern dllexport
281 // anyway. When deriving new arrays from an
282 // instantiation of this template the compiler does an explicit
283 // instantiation of the base class. From outside the vtkCommon
284 // library we block this using an extern dllimport instantiation.
285 // For classes inside vtkCommon we should be able to just do an
286 // extern instantiation, but VS 2008 complains about missing
287 // definitions. We cannot do an extern dllimport inside vtkCommon
288 // since the symbols are local to the dll. An extern dllexport
289 // seems to be the only way to convince VS 2008 to do the right
290 // thing, so we just disable the warning.
291 #pragma warning (disable: 4910) // extern and dllexport incompatible
292 
293 // Use an "extern explicit instantiation" to give the class a DLL
294 // interface. This is a compiler-specific extension.
296  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
297 
298 #pragma warning (pop)
299 
300 #endif
301 
302 // 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.
VTK_NEWINSTANCE vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
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:345
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.
#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.
#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 *)
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.