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 =========================================================================*/
69 #ifndef vtkGenericDataArray_h
70 #define vtkGenericDataArray_h
71 
72 #include "vtkDataArray.h"
73 #include "vtkSmartPointer.h"
74 #include "vtkTypeTraits.h"
76 
77 #include <cassert>
78 
79 template<class DerivedT, class ValueTypeT>
81 {
83 public:
84  typedef ValueTypeT ValueType;
86 
87 
90  enum { VTK_DATA_TYPE = vtkTypeTraits<ValueType>::VTK_TYPE_ID };
91 
96 
104  inline ValueType GetValue(vtkIdType valueIdx) const
105  {
106  return static_cast<const DerivedT*>(this)->GetValue(valueIdx);
107  }
108 
117  VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
118  {
119  static_cast<DerivedT*>(this)->SetValue(valueIdx, value);
120  }
121 
130  void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
131  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
132  {
133  static_cast<const DerivedT*>(this)->GetTypedTuple(tupleIdx, tuple);
134  }
135 
144  void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
145  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
146  {
147  static_cast<DerivedT*>(this)->SetTypedTuple(tupleIdx, tuple);
148  }
149 
155  ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
156  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
157  VTK_EXPECTS(0 <= compIdx && compIdx < GetNumberOfComponents())
158  {
159  return static_cast<const DerivedT*>(this)->GetTypedComponent(tupleIdx,
160  compIdx);
161  }
162 
168  void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
169  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
170  VTK_EXPECTS(0 <= compIdx && compIdx < GetNumberOfComponents())
171  {
172  static_cast<DerivedT*>(this)->SetTypedComponent(tupleIdx, compIdx, value);
173  }
174 
176 
180  void *GetVoidPointer(vtkIdType valueIdx) override;
181  ValueType* GetPointer(vtkIdType valueIdx);
182  void SetVoidArray(void*, vtkIdType, int) override;
183  void SetVoidArray(void*, vtkIdType, int, int) override;
184  void* WriteVoidPointer(vtkIdType valueIdx, vtkIdType numValues) override;
185  ValueType* WritePointer(vtkIdType valueIdx, vtkIdType numValues);
187 
194  void RemoveTuple(vtkIdType tupleIdx) override;
195 
200 
204  void InsertValue(vtkIdType valueIdx, ValueType value);
205 
209  void InsertTypedTuple(vtkIdType tupleIdx, const ValueType *t);
210 
215 
220  void InsertTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType val);
221 
223 
227  void GetValueRange(ValueType range[2], int comp);
228  ValueType *GetValueRange(int comp);
230 
235  ValueType *GetValueRange() { return this->GetValueRange(0); }
236  void GetValueRange(ValueType range[2]) { this->GetValueRange(range, 0); }
237 
242  vtkIdType Capacity() { return this->Size; }
243 
247  virtual void FillTypedComponent(int compIdx, ValueType value);
248 
252  virtual void FillValue(ValueType value);
253 
254  int GetDataType() override;
255  int GetDataTypeSize() override;
256  bool HasStandardMemoryLayout() override;
257  int Allocate(vtkIdType size, vtkIdType ext = 1000) override;
258  int Resize(vtkIdType numTuples) override;
259  void SetNumberOfComponents(int num) override;
260  void SetNumberOfTuples(vtkIdType number) override;
261  void Initialize() override;
262  void Squeeze() override;
263  void SetTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx,
264  vtkAbstractArray* source) override;
265  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
266  // using Superclass::SetTuple;
267  void SetTuple(vtkIdType tupleIdx, const float *tuple) override
268  { this->Superclass::SetTuple(tupleIdx, tuple); }
269  void SetTuple(vtkIdType tupleIdx, const double *tuple) override
270  { this->Superclass::SetTuple(tupleIdx, tuple); }
271 
272  void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds,
273  vtkAbstractArray *source) override;
274  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
275  // using Superclass::InsertTuples;
276  void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart,
277  vtkAbstractArray* source) override
278  { this->Superclass::InsertTuples(dstStart, n, srcStart, source); }
279 
280  void InsertTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx,
281  vtkAbstractArray *source) override;
282  void InsertTuple(vtkIdType tupleIdx, const float *source) override;
283  void InsertTuple(vtkIdType tupleIdx, const double *source) override;
284  void InsertComponent(vtkIdType tupleIdx, int compIdx,
285  double value) override;
286  vtkIdType InsertNextTuple(vtkIdType srcTupleIdx,
287  vtkAbstractArray *source) override;
288  vtkIdType InsertNextTuple(const float *tuple) override;
289  vtkIdType InsertNextTuple(const double *tuple) override;
290  void GetTuples(vtkIdList *tupleIds,
291  vtkAbstractArray *output) override;
292  void GetTuples(vtkIdType p1, vtkIdType p2,
293  vtkAbstractArray *output) override;
294  double *GetTuple(vtkIdType tupleIdx) override;
295  void GetTuple(vtkIdType tupleIdx, double * tuple) override;
296  void InterpolateTuple(vtkIdType dstTupleIdx, vtkIdList *ptIndices,
298  double* weights) override;
299  void InterpolateTuple(vtkIdType dstTupleIdx,
300  vtkIdType srcTupleIdx1, vtkAbstractArray* source1,
301  vtkIdType srcTupleIdx2, vtkAbstractArray* source2, double t) override;
302  void SetComponent(vtkIdType tupleIdx, int compIdx, double value) override;
303  double GetComponent(vtkIdType tupleIdx, int compIdx) override;
304  void SetVariantValue(vtkIdType valueIdx, vtkVariant value) override;
305  vtkVariant GetVariantValue(vtkIdType valueIdx) override;
306  void InsertVariantValue(vtkIdType valueIdx, vtkVariant value) override;
309  void LookupValue(vtkVariant value, vtkIdList* valueIds) override;
310  virtual void LookupTypedValue(ValueType value, vtkIdList* valueIds);
311  void ClearLookup() override;
312  void DataChanged() override;
313  void FillComponent(int compIdx, double value) override;
315 
316 protected:
318  ~vtkGenericDataArray() override;
319 
325  inline bool AllocateTuples(vtkIdType numTuples)
326  {
327  return static_cast<DerivedT*>(this)->AllocateTuples(numTuples);
328  }
329 
335  inline bool ReallocateTuples(vtkIdType numTuples)
336  {
337  return static_cast<DerivedT*>(this)->ReallocateTuples(numTuples);
338  }
339 
340  // This method resizes the array if needed so that the given tuple index is
341  // valid/accessible.
342  bool EnsureAccessToTuple(vtkIdType tupleIdx);
343 
344  std::vector<double> LegacyTuple;
345  std::vector<ValueType> LegacyValueRange;
346 
348 
349 private:
350  vtkGenericDataArray(const vtkGenericDataArray&) = delete;
351  void operator=(const vtkGenericDataArray&) = delete;
352 
353 };
354 
355 #include "vtkGenericDataArray.txx"
356 
357 // Adds an implementation of NewInstanceInternal() that returns an AoS
358 // (unmapped) VTK array, if possible. This allows the pipeline to copy and
359 // propagate the array when the array data is not modifiable. Use this in
360 // combination with vtkAbstractTypeMacro or vtkAbstractTemplateTypeMacro
361 // (instead of vtkTypeMacro) to avoid adding the default NewInstance
362 // implementation.
363 #define vtkAOSArrayNewInstanceMacro(thisClass) \
364  protected: \
365  vtkObjectBase *NewInstanceInternal() const override \
366  { \
367  if (vtkDataArray *da = \
368  vtkDataArray::CreateDataArray(thisClass::VTK_DATA_TYPE)) \
369  { \
370  return da; \
371  } \
372  return thisClass::New(); \
373  } \
374  public:
375 
376 #endif
377 // 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:70
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:36
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: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...
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.
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:32
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.