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:
45  vtkTemplateTypeMacro(SelfType, GenericDataArrayType)
46  typedef typename Superclass::ValueType ValueType;
47 
49  {
53  };
54 
55  static vtkSOADataArrayTemplate* New();
56 
58 
61  inline ValueType GetValue(vtkIdType valueIdx) const
62  {
63  vtkIdType tupleIdx;
64  int comp;
65  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
66  return this->GetTypedComponent(tupleIdx, comp);
67  }
69 
71 
74  inline void SetValue(vtkIdType valueIdx, ValueType value)
75  {
76  vtkIdType tupleIdx;
77  int comp;
78  this->GetTupleIndexFromValueIndex(valueIdx, tupleIdx, comp);
79  this->SetTypedComponent(tupleIdx, comp, value);
80  }
82 
86  inline void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
87  {
88  for (size_t cc=0; cc < this->Data.size(); cc++)
89  {
90  tuple[cc] = this->Data[cc]->GetBuffer()[tupleIdx];
91  }
92  }
93 
97  inline void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
98  {
99  for (size_t cc=0; cc < this->Data.size(); ++cc)
100  {
101  this->Data[cc]->GetBuffer()[tupleIdx] = tuple[cc];
102  }
103  }
104 
108  inline ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
109  {
110  return this->Data[comp]->GetBuffer()[tupleIdx];
111  }
112 
116  inline void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
117  {
118  this->Data[comp]->GetBuffer()[tupleIdx] = value;
119  }
120 
124  void FillTypedComponent(int compIdx, ValueType value) VTK_OVERRIDE;
125 
139  void SetArray(int comp, ValueType* array, vtkIdType size,
140  bool updateMaxId = false, bool save=false,
141  int deleteMethod=VTK_DATA_ARRAY_FREE);
142 
147  ValueType* GetComponentArrayPointer(int comp);
148 
153  void *GetVoidPointer(vtkIdType valueIdx) VTK_OVERRIDE;
154 
159  void ExportToVoidPointer(void *ptr) VTK_OVERRIDE;
160 
162 
170  {
171  if (source)
172  {
173  switch (source->GetArrayType())
174  {
176  if (vtkDataTypesCompare(source->GetDataType(),
178  {
179  return static_cast<vtkSOADataArrayTemplate<ValueType>*>(source);
180  }
181  break;
182  }
183  }
184  return NULL;
185  }
187 
190  void SetNumberOfComponents(int numComps) VTK_OVERRIDE;
191  void ShallowCopy(vtkDataArray *other) VTK_OVERRIDE;
192 
193  // Reimplemented for efficiency:
194  void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart,
195  vtkAbstractArray* source) VTK_OVERRIDE;
196  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
197  // using Superclass::InsertTuples;
198  void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds,
199  vtkAbstractArray *source) VTK_OVERRIDE
200  { this->Superclass::InsertTuples(dstIds, srcIds, source); }
201 
202 protected:
204  ~vtkSOADataArrayTemplate() VTK_OVERRIDE;
205 
210  bool AllocateTuples(vtkIdType numTuples);
211 
216  bool ReallocateTuples(vtkIdType numTuples);
217 
218  std::vector<vtkBuffer<ValueType>*> Data;
219  vtkBuffer<ValueType> *AoSCopy;
220 
221  double NumberOfComponentsReciprocal;
222 
223 private:
224  vtkSOADataArrayTemplate(const vtkSOADataArrayTemplate&) VTK_DELETE_FUNCTION;
225  void operator=(const vtkSOADataArrayTemplate&) VTK_DELETE_FUNCTION;
226 
227  inline void GetTupleIndexFromValueIndex(vtkIdType valueIdx,
228  vtkIdType& tupleIdx, int& comp) const
229  {
230  tupleIdx = static_cast<vtkIdType>(valueIdx *
231  this->NumberOfComponentsReciprocal);
232  comp = valueIdx - (tupleIdx * this->NumberOfComponents);
233  }
234 
235  friend class vtkGenericDataArray<vtkSOADataArrayTemplate<ValueTypeT>,
236  ValueTypeT>;
237 };
238 
239 // Declare vtkArrayDownCast implementations for SoA containers:
241 
242 #endif // header guard
243 
244 // This portion must be OUTSIDE the include blockers. This is used to tell
245 // libraries other than vtkCommonCore that instantiations of
246 // vtkSOADataArrayTemplate can be found externally. This prevents each library
247 // from instantiating these on their own.
248 #ifdef VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATING
249 #define VTK_SOA_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
250  template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate< T >
251 #elif defined(VTK_USE_EXTERN_TEMPLATE)
252 #ifndef VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
253 #define VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
254 #ifdef _MSC_VER
255 #pragma warning (push)
256 // The following is needed when the vtkSOADataArrayTemplate is declared
257 // dllexport and is used from another class in vtkCommonCore
258 #pragma warning (disable: 4910) // extern and dllexport incompatible
259 #endif
261  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
262 #ifdef _MSC_VER
263 #pragma warning (pop)
264 #endif
265 #endif // VTK_SOA_DATA_ARRAY_TEMPLATE_EXTERN
266 
267 // The following clause is only for MSVC 2008 and 2010
268 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
269 #pragma warning (push)
270 
271 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
272 #pragma warning (disable: 4091)
273 
274 // Compiler-specific extension warning.
275 #pragma warning (disable: 4231)
276 
277 // We need to disable warning 4910 and do an extern dllexport
278 // anyway. When deriving new arrays from an
279 // instantiation of this template the compiler does an explicit
280 // instantiation of the base class. From outside the vtkCommon
281 // library we block this using an extern dllimport instantiation.
282 // For classes inside vtkCommon we should be able to just do an
283 // extern instantiation, but VS 2008 complains about missing
284 // definitions. We cannot do an extern dllimport inside vtkCommon
285 // since the symbols are local to the dll. An extern dllexport
286 // seems to be the only way to convince VS 2008 to do the right
287 // thing, so we just disable the warning.
288 #pragma warning (disable: 4910) // extern and dllexport incompatible
289 
290 // Use an "extern explicit instantiation" to give the class a DLL
291 // interface. This is a compiler-specific extension.
293  extern template class VTKCOMMONCORE_EXPORT vtkSOADataArrayTemplate)
294 
295 #pragma warning (pop)
296 
297 #endif
298 
299 // VTK-HeaderTest-Exclude: vtkSOADataArrayTemplate.h
Struct-Of-Arrays implementation of vtkGenericDataArray.
abstract base class for most VTK objects
Definition: vtkObject.h:53
Abstract superclass for all arrays.
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.
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
internal storage class used by vtkSOADataArrayTemplate, vtkAOSDataArrayTemplate, and others...
Definition: vtkBuffer.h:32
#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:30
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
#define vtkArrayDownCast_TemplateFastCastMacro(ArrayT)
Same as vtkArrayDownCast_FastCastMacro, but treats ArrayT as a single-parameter template (the paramet...
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))
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Superclass::ValueType ValueType
virtual void FillTypedComponent(int compIdx, ValueType value)
Set component comp of all tuples to value.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
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:29
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.