VTK  9.7.1
vtkImplicitArray.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Funded by CEA, DAM, DIF, F-91297 Arpajon, France
4 #ifndef vtkImplicitArray_h
5 #define vtkImplicitArray_h
6 
7 #include "vtkCommonCoreModule.h" // for export macro
8 #include "vtkGenericDataArray.h"
9 #include "vtkImplicitArrayTraits.h" // for traits
10 
11 #include <memory>
12 #include <type_traits>
13 
140 //-------------------------------------------------------------------------------------------------
141 // Special macro for implicit array types modifying the behavior of NewInstance to provide writable
142 // AOS arrays instead of empty implicit arrays
143 #define vtkImplicitArrayTypeMacro(thisClass, superclass) \
144  vtkAbstractTypeMacroWithNewInstanceType(thisClass, superclass, \
145  vtkAOSDataArrayTemplate<typename thisClass::ValueType>, typeid(thisClass).name()); \
146  \
147 protected: \
148  vtkObjectBase* NewInstanceInternal() const override \
149  { \
150  return vtkAOSDataArrayTemplate<typename thisClass::ValueType>::New(); \
151  } \
152  \
153 public:
154 //-------------------------------------------------------------------------------------------------
155 
156 VTK_ABI_NAMESPACE_BEGIN
157 template <class BackendT, int ArrayType = vtkArrayTypes::VTK_IMPLICIT_ARRAY>
159  : public vtkGenericDataArray<vtkImplicitArray<BackendT, ArrayType>,
160  typename vtk::detail::implicit_array_traits<BackendT>::rtype, ArrayType>
161 {
163  "Supplied backend type does not have mandatory read trait. Must implement either map() const "
164  "or operator() const.");
167 
168 public:
171  using typename Superclass::ArrayTypeTag;
172  using typename Superclass::DataTypeTag;
173  using typename Superclass::ValueType;
174  using BackendType = BackendT;
175 
176  static vtkImplicitArray* New();
177 
179 
185  ValueType GetValue(vtkIdType idx) const { return this->GetValueImpl<BackendT>(idx); }
186 
190  void SetValue(vtkIdType idx, ValueType value);
191 
195  void GetTypedTuple(vtkIdType idx, ValueType* tuple) const
196  {
197  this->GetTypedTupleImpl<BackendT>(idx, tuple);
198  }
199 
203  void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple);
204 
208  ValueType GetTypedComponent(vtkIdType idx, int comp) const
209  {
210  return this->GetTypedComponentImpl<BackendT>(idx, comp);
211  }
212 
216  void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value);
218 
220 
223  void SetBackend(std::shared_ptr<BackendT> newBackend)
224  {
225  this->Backend = newBackend;
226  this->Modified();
227  }
228  std::shared_ptr<BackendT> GetBackend() { return this->Backend; }
229  bool IsBackendConstructed() const { return this->Backend != nullptr; }
231 
235  template <typename... Params>
236  void ConstructBackend(Params&&... params)
237  {
238  this->SetBackend(std::make_shared<BackendT>(std::forward<Params>(params)...));
239  }
240 
244  void Initialize() override
245  {
246  this->Initialize<BackendT>();
247  this->Squeeze();
249  }
250 
260  unsigned long GetActualMemorySize() const override
261  {
262  return this->GetActualMemorySizeImpl<BackendT>();
263  }
264 
266 
277  template <typename OtherBackend, int OtherArrayType>
279  {
280  static_assert(std::is_same_v<BackendT, OtherBackend>,
281  "Cannot copy implicit array with one type of backend to an implicit array with a different "
282  "type of backend");
283  static_assert(ArrayTypeTag::value == OtherArrayType,
284  "Cannot copy implicit array with one array type to an implicit array with a different "
285  "array type");
287  this->SetNumberOfTuples(other->GetNumberOfTuples());
288  this->SetBackend(other->GetBackend());
289  }
291  void DeepCopy(vtkDataArray* da) override
292  {
293  if (da == nullptr || da == this)
294  {
295  return;
296  }
297  // dispatch to the templated version if possible
299  {
300  this->ImplicitDeepCopy(otherImplicit);
301  }
302  else
303  {
304  // otherwise fallback to generic implementation that will copy all values
305  this->vtkDataArray::DeepCopy(da);
306  }
307  }
309 
311 
319 
320 protected:
322  ~vtkImplicitArray() override;
323 
325 
328  VTK_DEPRECATED_IN_9_7_0("No longer needed")
329  bool AllocateTuples(vtkIdType vtkNotUsed(numTuples)) { return true; }
330  bool ReallocateTuples(vtkIdType vtkNotUsed(numTuples)) { return true; }
332 
336  std::shared_ptr<BackendT> Backend;
337 
338 private:
339  vtkImplicitArray(const vtkImplicitArray&) = delete;
340  void operator=(const vtkImplicitArray&) = delete;
341 
343 
346  template <typename U>
348  vtkIdType idx) const
349  {
350  return this->Backend->map(idx);
351  }
353 
355 
358  template <typename U>
360  vtkIdType idx) const
361  {
362  return (*this->Backend)(idx);
363  }
365 
367 
370  template <typename U>
371  typename std::enable_if<vtk::detail::implicit_array_traits<U>::default_constructible, void>::type
372  Initialize()
373  {
374  this->Backend = std::make_shared<BackendT>();
375  }
377 
379 
382  template <typename U>
383  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::default_constructible, void>::type
384  Initialize()
385  {
386  this->Backend = nullptr;
387  }
389 
391 
394  template <typename U>
395  typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_direct_read_tuple, void>::type
396  GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
397  {
398  static_assert(
400  "Tuple type should be the same as the return type of the mapTuple");
401  this->Backend->mapTuple(idx, tuple);
402  }
404 
406 
409  template <typename U>
410  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_tuple &&
412  void>::type
413  GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
414  {
415  for (vtkIdType comp = 0; comp < this->NumberOfComponents; comp++)
416  {
417  tuple[comp] = this->GetTypedComponent(idx, comp);
418  }
419  }
420 
424  template <typename U>
425  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_tuple &&
426  !vtk::detail::implicit_array_traits<U>::can_direct_read_component,
427  void>::type
428  GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
429  {
430  const vtkIdType tupIdx = idx * this->NumberOfComponents;
431  for (vtkIdType comp = 0; comp < this->NumberOfComponents; comp++)
432  {
433  tuple[comp] = this->GetValue(tupIdx + comp);
434  }
435  }
437 
439 
442  template <typename U>
443  typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_direct_read_component,
445  GetTypedComponentImpl(vtkIdType idx, int comp) const
446  {
447  static_assert(
449  "Component return type should be the same as the return type of the mapComponent");
450  return this->Backend->mapComponent(idx, comp);
451  }
453 
455 
458  template <typename U>
459  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_component,
461  GetTypedComponentImpl(vtkIdType idx, int comp) const
462  {
463  return this->GetValue(idx * this->NumberOfComponents + comp);
464  }
466 
468 
471  template <typename U>
472  typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_get_memory_size,
473  unsigned long>::type
474  GetActualMemorySizeImpl() const
475  {
476  return this->Backend->getMemorySize();
477  }
478 
483  template <typename U>
484  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_get_memory_size,
485  unsigned long>::type
486  GetActualMemorySizeImpl() const
487  {
488  return 1;
489  }
491 
493 };
494 
495 // Declare vtkArrayDownCast implementations for implicit containers:
497 
498 VTK_ABI_NAMESPACE_END
499 
500 // This macro is used by the subclasses to create dummy
501 // declarations for these functions such that the wrapper
502 // can see them. The wrappers ignore vtkImplicitArray.
503 #define vtkCreateImplicitWrappedArrayInterface(T) \
504  vtkCreateWrappedArrayReadInterface(T); \
505  bool SetNumberOfValues(vtkIdType number) override;
506 
507 #include "vtkImplicitArray.txx"
508 
509 #endif // vtkImplicitArray_h
510 
511 // VTK-HeaderTest-Exclude: vtkImplicitArray.h
A composite trait for handling all the different capabilities a "backend" to an implicit array can ha...
virtual void SetNumberOfTuples(vtkIdType numTuples)
Set the number of tuples (a component group) in the array.
unsigned long GetActualMemorySize() const override
Return the memory in kibibytes (1024 bytes) consumed by this implicit data array. ...
int GetNumberOfComponents() const
Set/Get the dimension (n) of the components.
Abstract superclass for all arrays.
virtual void Initialize()
Release storage and reset array to initial state.
vtkImplicitArrayTypeMacro(SelfType, GenericDataArrayType)
static vtkImplicitArray< BackendT, ArrayType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Will not do anything for these read only arrays!
void SetValue(vtkIdType idx, ValueType value)
Will not do anything for these read only arrays!
bool AllocateTuples(vtkIdType numTuples)
No allocation necessary.
~vtkImplicitArray() override
int vtkIdType
Definition: vtkType.h:363
Base interface for all typed vtkDataArray subclasses.
bool IsBackendConstructed() const
Setter/Getter for Backend.
void DeepCopy(vtkAbstractArray *aa) override
Deep copy of data.
std::shared_ptr< BackendT > GetBackend()
Setter/Getter for Backend.
void ImplicitDeepCopy(vtkImplicitArray< OtherBackend, OtherArrayType > *other)
Specific DeepCopy for implicit arrays.
ValueType GetValue(vtkIdType idx) const
Implementation of vtkGDAConceptMethods.
std::shared_ptr< BackendT > Backend
The backend object actually mapping the indexes.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
void Initialize() override
Reset the array to default construction.
ValueType GetTypedComponent(vtkIdType idx, int comp) const
Get component comp of the tuple at idx.
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Will not do anything for these read only arrays!
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
A read only array class that wraps an implicit function from integers to any value type supported by ...
void SetBackend(std::shared_ptr< BackendT > newBackend)
Setter/Getter for Backend.
vtkArrayDownCast_Template2FastCastMacro(vtkImplicitArray)
void DeepCopy(vtkDataArray *da) override
Specific DeepCopy for implicit arrays.
void GetTypedTuple(vtkIdType idx, ValueType *tuple) const
Copy the tuple at idx into tuple.
std::integral_constant< int, vtkArrayTypes::VTK_ABSTRACT_ARRAY > ArrayTypeTag
bool ReallocateTuples(vtkIdType numTuples)
No allocation necessary.
static vtkImplicitArray * New()
#define VTK_DEPRECATED_IN_9_7_0(reason)
void Modified() override
Removes out-of-date L2_NORM_RANGE() and L2_NORM_FINITE_RANGE() values.
void ConstructBackend(Params &&...params)
Utility method for setting backend parameterization directly.
A utility structure serving as a backend for composite arrays: an array composed of multiple arrays c...