VTK
vtkGenericDataArray.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkGenericDataArray.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 =========================================================================*/
66 #ifndef vtkGenericDataArray_h
67 #define vtkGenericDataArray_h
68 
69 #include "vtkDataArray.h"
70 #include "vtkSmartPointer.h"
71 #include "vtkTypeTraits.h"
73 
74 #include <cassert>
75 
76 template<class DerivedT, class ValueTypeT>
78 {
80 public:
81  typedef ValueTypeT ValueType;
83 
84 
87  enum { VTK_DATA_TYPE = vtkTypeTraits<ValueType>::VTK_TYPE_ID };
88 
93 
101  inline ValueType GetValue(vtkIdType valueIdx) const
102  {
103  return static_cast<const DerivedT*>(this)->GetValue(valueIdx);
104  }
105 
114  VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
115  {
116  static_cast<DerivedT*>(this)->SetValue(valueIdx, value);
117  }
118 
127  void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
128  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
129  {
130  static_cast<const DerivedT*>(this)->GetTypedTuple(tupleIdx, tuple);
131  }
132 
141  void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
142  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
143  {
144  static_cast<DerivedT*>(this)->SetTypedTuple(tupleIdx, tuple);
145  }
146 
152  ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
153  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
154  VTK_EXPECTS(0 <= compIdx && compIdx < GetNumberOfComponents())
155  {
156  return static_cast<const DerivedT*>(this)->GetTypedComponent(tupleIdx,
157  compIdx);
158  }
159 
165  void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
166  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
167  VTK_EXPECTS(0 <= compIdx && compIdx < GetNumberOfComponents())
168  {
169  static_cast<DerivedT*>(this)->SetTypedComponent(tupleIdx, compIdx, value);
170  }
171 
173 
177  void *GetVoidPointer(vtkIdType valueIdx) override;
178  ValueType* GetPointer(vtkIdType valueIdx);
179  void SetVoidArray(void*, vtkIdType, int) override;
180  void SetVoidArray(void*, vtkIdType, int, int) override;
181  void* WriteVoidPointer(vtkIdType valueIdx, vtkIdType numValues) override;
182  ValueType* WritePointer(vtkIdType valueIdx, vtkIdType numValues);
184 
191  void RemoveTuple(vtkIdType tupleIdx) override;
192 
197 
201  void InsertValue(vtkIdType valueIdx, ValueType value);
202 
206  void InsertTypedTuple(vtkIdType tupleIdx, const ValueType *t);
207 
212 
217  void InsertTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType val);
218 
220 
224  void GetValueRange(ValueType range[2], int comp);
225  ValueType *GetValueRange(int comp) VTK_SIZEHINT(2);
227 
233  void GetValueRange(ValueType range[2]) { this->GetValueRange(range, 0); }
234 
239  vtkIdType Capacity() { return this->Size; }
240 
244  virtual void FillTypedComponent(int compIdx, ValueType value);
245 
249  virtual void FillValue(ValueType value);
250 
251  int GetDataType() override;
252  int GetDataTypeSize() override;
253  bool HasStandardMemoryLayout() override;
254  int Allocate(vtkIdType size, vtkIdType ext = 1000) override;
255  int Resize(vtkIdType numTuples) override;
256  void SetNumberOfComponents(int num) override;
257  void SetNumberOfTuples(vtkIdType number) override;
258  void Initialize() override;
259  void Squeeze() override;
260  void SetTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx,
261  vtkAbstractArray* source) override;
262  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
263  // using Superclass::SetTuple;
264  void SetTuple(vtkIdType tupleIdx, const float *tuple) override
265  { this->Superclass::SetTuple(tupleIdx, tuple); }
266  void SetTuple(vtkIdType tupleIdx, const double *tuple) override
267  { this->Superclass::SetTuple(tupleIdx, tuple); }
268 
269  void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds,
270  vtkAbstractArray *source) override;
271  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
272  // using Superclass::InsertTuples;
273  void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart,
274  vtkAbstractArray* source) override
275  { this->Superclass::InsertTuples(dstStart, n, srcStart, source); }
276 
277  void InsertTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx,
278  vtkAbstractArray *source) override;
279  void InsertTuple(vtkIdType tupleIdx, const float *source) override;
280  void InsertTuple(vtkIdType tupleIdx, const double *source) override;
281  void InsertComponent(vtkIdType tupleIdx, int compIdx,
282  double value) override;
283  vtkIdType InsertNextTuple(vtkIdType srcTupleIdx,
284  vtkAbstractArray *source) override;
285  vtkIdType InsertNextTuple(const float *tuple) override;
286  vtkIdType InsertNextTuple(const double *tuple) override;
287  void GetTuples(vtkIdList *tupleIds,
288  vtkAbstractArray *output) override;
289  void GetTuples(vtkIdType p1, vtkIdType p2,
290  vtkAbstractArray *output) override;
291  double *GetTuple(vtkIdType tupleIdx) override;
292  void GetTuple(vtkIdType tupleIdx, double * tuple) override;
293  void InterpolateTuple(vtkIdType dstTupleIdx, vtkIdList *ptIndices,
295  double* weights) override;
296  void InterpolateTuple(vtkIdType dstTupleIdx,
297  vtkIdType srcTupleIdx1, vtkAbstractArray* source1,
298  vtkIdType srcTupleIdx2, vtkAbstractArray* source2, double t) override;
299  void SetComponent(vtkIdType tupleIdx, int compIdx, double value) override;
300  double GetComponent(vtkIdType tupleIdx, int compIdx) override;
301  void SetVariantValue(vtkIdType valueIdx, vtkVariant value) override;
302  vtkVariant GetVariantValue(vtkIdType valueIdx) override;
303  void InsertVariantValue(vtkIdType valueIdx, vtkVariant value) override;
306  void LookupValue(vtkVariant value, vtkIdList* valueIds) override;
307  virtual void LookupTypedValue(ValueType value, vtkIdList* valueIds);
308  void ClearLookup() override;
309  void DataChanged() override;
310  void FillComponent(int compIdx, double value) override;
312 
313 protected:
315  ~vtkGenericDataArray() override;
316 
322  inline bool AllocateTuples(vtkIdType numTuples)
323  {
324  return static_cast<DerivedT*>(this)->AllocateTuples(numTuples);
325  }
326 
332  inline bool ReallocateTuples(vtkIdType numTuples)
333  {
334  return static_cast<DerivedT*>(this)->ReallocateTuples(numTuples);
335  }
336 
337  // This method resizes the array if needed so that the given tuple index is
338  // valid/accessible.
339  bool EnsureAccessToTuple(vtkIdType tupleIdx);
340 
341  std::vector<double> LegacyTuple;
342  std::vector<ValueType> LegacyValueRange;
343 
345 
346 private:
347  vtkGenericDataArray(const vtkGenericDataArray&) = delete;
348  void operator=(const vtkGenericDataArray&) = delete;
349 
350 };
351 
352 #include "vtkGenericDataArray.txx"
353 
354 // Adds an implementation of NewInstanceInternal() that returns an AoS
355 // (unmapped) VTK array, if possible. This allows the pipeline to copy and
356 // propagate the array when the array data is not modifiable. Use this in
357 // combination with vtkAbstractTypeMacro or vtkAbstractTemplateTypeMacro
358 // (instead of vtkTypeMacro) to avoid adding the default NewInstance
359 // implementation.
360 #define vtkAOSArrayNewInstanceMacro(thisClass) \
361  protected: \
362  vtkObjectBase *NewInstanceInternal() const override \
363  { \
364  if (vtkDataArray *da = \
365  vtkDataArray::CreateDataArray(thisClass::VTK_DATA_TYPE)) \
366  { \
367  return da; \
368  } \
369  return thisClass::New(); \
370  } \
371  public:
372 
373 #endif
374 // VTK-HeaderTest-Exclude: vtkGenericDataArray.h
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
bool HasStandardMemoryLayout() override
Returns true if this array uses the standard memory layout defined in the VTK user guide,...
int Resize(vtkIdType numTuples) override
Resize the array to the requested number of tuples and preserve data.
void SetTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray *source) override
Set the tuple at dstTupleIdx in this array to the tuple at srcTupleIdx in the source array.
int Allocate(vtkIdType size, vtkIdType ext=1000) override
Allocate memory for this array.
void FillComponent(int compIdx, double value) override
Fill a component of a data array with a specified value.
vtkIdType InsertNextTypedTuple(const ValueType *t)
Insert (memory allocation performed) the tuple onto the end of the array.
void Squeeze() override
Free any unnecessary memory.
vtkIdType GetNumberOfValues() const
Get the total number of values in the array.
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Get component compIdx of the tuple at tupleIdx.
void InsertValue(vtkIdType valueIdx, ValueType value)
Insert data at a specified position in the array.
vtkIdType GetNumberOfTuples()
Get the number of complete tuples (a component group) in the array.
vtkIdType InsertNextTuple(vtkIdType srcTupleIdx, vtkAbstractArray *source) override
Insert the tuple from srcTupleIdx in the source array at the end of this array.
internal class used by vtkGenericDataArray to support LookupValue.
Abstract superclass for all arrays.
ValueType * GetPointer(vtkIdType valueIdx)
Default implementation raises a runtime error.
vtkGenericDataArrayLookupHelper< SelfType > Lookup
void SetVariantValue(vtkIdType valueIdx, vtkVariant value) override
Set a value in the array from a variant.
vtkTemplateTypeMacro(SelfType, vtkDataArray) enum
Compile time access to the VTK type identifier.
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
void SetComponent(vtkIdType tupleIdx, int compIdx, double value) override
Set the data component at the location specified by tupleIdx and compIdx to value.
void Initialize() override
Release storage and reset array to initial state.
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.
int GetNumberOfComponents()
Set/Get the dimension (n) of the components.
void SetVoidArray(void *, vtkIdType, int) override
Default implementation raises a runtime error.
void SetNumberOfTuples(vtkIdType number) override
Set the number of tuples (a component group) in the array.
void SetTuple(vtkIdType tupleIdx, const float *tuple) override
Set the data tuple at tupleIdx.
A atomic type representing the union of many types.
Definition: vtkVariant.h:65
void SetTuple(vtkIdType tupleIdx, const double *tuple) override
Set the data tuple at tupleIdx.
void InsertTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray *source) override
Insert the tuple at srcTupleIdx in the source array into this array at dstTupleIdx.
void InsertVariantValue(vtkIdType valueIdx, vtkVariant value) override
Insert a value into the array from a variant.
double * GetTuple(vtkIdType tupleIdx) override
Get the data tuple at tupleIdx.
vtkIdType Capacity()
Return the capacity in typeof T units of the current array.
ValueType * GetValueRange()
Get the range of array values for the 0th component in the native data type.
vtkIdType InsertNextValue(ValueType value)
Insert data at the end of the array.
list of point or cell ids
Definition: vtkIdList.h:30
vtkVariant GetVariantValue(vtkIdType valueIdx) override
Retrieve value from the array as a variant.
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
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...
int GetDataTypeSize() override
Return the size of the underlying data type.
virtual void FillValue(ValueType value)
Set all the values in array to value.
void * WriteVoidPointer(vtkIdType valueIdx, vtkIdType numValues) override
Default implementation raises a runtime error.
ValueType * WritePointer(vtkIdType valueIdx, vtkIdType numValues)
Default implementation raises a runtime error.
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
Abstract superclass to iterate over elements in an vtkAbstractArray.
void ClearLookup() override
Delete the associated fast lookup data structure on this array, if it exists.
#define VTK_SIZEHINT(...)
void GetTuples(vtkIdList *tupleIds, vtkAbstractArray *output) override
Given a list of tuple ids, return an array of tuples.
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
void RemoveTuple(vtkIdType tupleIdx) override
Removes a tuple at the given index.
void GetValueRange(ValueType range[2])
~vtkGenericDataArray() override
void DataChanged() override
Tell the array explicitly that the data has changed.
#define VTK_NEWINSTANCE
void * GetVoidPointer(vtkIdType valueIdx) override
Default implementation raises a runtime error.
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array's tuple at tupleIdx to the values in tuple.
std::vector< ValueType > LegacyValueRange
double GetComponent(vtkIdType tupleIdx, int compIdx) override
Return the data component at the location specified by tupleIdx and compIdx.
vtkIdType LookupValue(vtkVariant value) override
Return the value indices where a specific value appears.
std::vector< double > LegacyTuple
virtual void FillTypedComponent(int compIdx, ValueType value)
Set component comp of all tuples to value.
void InsertTypedTuple(vtkIdType tupleIdx, const ValueType *t)
Insert (memory allocation performed) the tuple t at tupleIdx.
void InsertComponent(vtkIdType tupleIdx, int compIdx, double value) override
Insert value at the location specified by tupleIdx and compIdx.
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
void InterpolateTuple(vtkIdType dstTupleIdx, vtkIdList *ptIndices, vtkAbstractArray *source, double *weights) override
Set the tuple at dstTupleIdx in this array to the interpolated tuple value, given the ptIndices in th...
virtual vtkIdType LookupTypedValue(ValueType value)
#define VTK_EXPECTS(x)
void SetNumberOfComponents(int num) override
Set/Get the dimension (n) of the components.
int GetDataType() override
Return the underlying data type.
void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray *source) override
Copy n consecutive tuples starting at srcStart from the source array to this array,...
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:29
bool EnsureAccessToTuple(vtkIdType tupleIdx)
void InsertTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType val)
Insert (memory allocation performed) the value at the specified tuple and component location.