VTK  9.3.20240419
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 
142 //-------------------------------------------------------------------------------------------------
143 // Special macro for implicit array types modifying the behavior of NewInstance to provide writable
144 // AOS arrays instead of empty implicit arrays
145 #define vtkImplicitArrayTypeMacro(thisClass, superclass) \
146  vtkAbstractTypeMacroWithNewInstanceType(thisClass, superclass, \
147  vtkAOSDataArrayTemplate<typename thisClass::ValueType>, typeid(thisClass).name()); \
148  \
149 protected: \
150  vtkObjectBase* NewInstanceInternal() const override \
151  { \
152  return vtkAOSDataArrayTemplate<typename thisClass::ValueType>::New(); \
153  } \
154  \
155 public:
156 //-------------------------------------------------------------------------------------------------
157 
158 VTK_ABI_NAMESPACE_BEGIN
159 template <class BackendT>
161  : public vtkGenericDataArray<vtkImplicitArray<BackendT>,
162  typename vtk::detail::implicit_array_traits<BackendT>::rtype>
163 {
165  static_assert(trait::can_read,
166  "Supplied backend type does not have mandatory read trait. Must implement either map() const "
167  "or operator() const.");
168  using ValueTypeT = typename trait::rtype;
170 
171 public:
175  using BackendType = BackendT;
176 
178 
180 
186  inline ValueType GetValue(vtkIdType idx) const { return this->GetValueImpl<BackendT>(idx); }
187 
192 
196  void GetTypedTuple(vtkIdType idx, ValueType* tuple) const
197  {
198  this->GetTypedTupleImpl<BackendT>(idx, tuple);
199  }
200 
204  void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple);
205 
209  inline ValueType GetTypedComponent(vtkIdType idx, int comp) const
210  {
211  return this->GetTypedComponentImpl<BackendT>(idx, comp);
212  }
213 
217  void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value);
219 
221 
224  void SetBackend(std::shared_ptr<BackendT> newBackend)
225  {
226  this->Backend = newBackend;
227  this->Modified();
228  }
229  std::shared_ptr<BackendT> GetBackend() { return this->Backend; }
231 
235  template <typename... Params>
236  void ConstructBackend(Params&&... params)
237  {
238  this->SetBackend(std::make_shared<BackendT>(std::forward<Params>(params)...));
239  }
240 
250  void* GetVoidPointer(vtkIdType valueIdx) override;
251 
255  void Squeeze() override;
256 
260  int GetArrayType() const override { return vtkAbstractArray::ImplicitArray; }
261 
265  void Initialize() override
266  {
267  this->Initialize<BackendT>();
268  this->Squeeze();
269  }
270 
280  inline unsigned long GetActualMemorySize() const override
281  {
282  return this->GetActualMemorySizeImpl<BackendT>();
283  }
284 
296  template <typename OtherBackend>
298  {
300  "Cannot copy implicit array with one type of backend to an implicit array with a different "
301  "type of backend");
303  this->SetNumberOfTuples(other->GetNumberOfTuples());
304  this->SetBackend(other->GetBackend());
305  }
306 
308 
316 
317 protected:
319  ~vtkImplicitArray() override;
320 
322 
325  bool AllocateTuples(vtkIdType vtkNotUsed(numTuples)) { return true; }
326  bool ReallocateTuples(vtkIdType vtkNotUsed(numTuples)) { return true; }
328 
329  struct vtkInternals;
330  std::unique_ptr<vtkInternals> Internals;
331 
335  std::shared_ptr<BackendT> Backend;
336 
337 private:
338  vtkImplicitArray(const vtkImplicitArray&) = delete;
339  void operator=(const vtkImplicitArray&) = delete;
340 
342 
345  template <typename U>
347  vtkIdType idx) const
348  {
349  return this->Backend->map(idx);
350  }
352 
354 
357  template <typename U>
359  vtkIdType idx) const
360  {
361  return (*this->Backend)(idx);
362  }
364 
366 
369  template <typename U>
370  typename std::enable_if<vtk::detail::implicit_array_traits<U>::default_constructible, void>::type
371  Initialize()
372  {
373  this->Backend = std::make_shared<BackendT>();
374  }
376 
378 
381  template <typename U>
382  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::default_constructible, void>::type
383  Initialize()
384  {
385  this->Backend = nullptr;
386  }
388 
390 
393  template <typename U>
394  typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_direct_read_tuple, void>::type
395  GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
396  {
397  static_assert(
399  "Tuple type should be the same as the return type of the mapTuple");
400  this->Backend->mapTuple(idx, tuple);
401  }
403 
405 
408  template <typename U>
409  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_tuple &&
411  void>::type
412  GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
413  {
414  for (vtkIdType comp = 0; comp < this->NumberOfComponents; comp++)
415  {
416  tuple[comp] = this->GetTypedComponent(idx, comp);
417  }
418  }
419 
423  template <typename U>
424  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_tuple &&
426  void>::type
427  GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
428  {
429  const vtkIdType tupIdx = idx * this->NumberOfComponents;
430  for (vtkIdType comp = 0; comp < this->NumberOfComponents; comp++)
431  {
432  tuple[comp] = this->GetValue(tupIdx + comp);
433  }
434  }
436 
438 
441  template <typename U>
442  typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_direct_read_component,
444  GetTypedComponentImpl(vtkIdType idx, int comp) const
445  {
446  static_assert(
448  "Component return type should be the same as the return type of the mapComponent");
449  return this->Backend->mapComponent(idx, comp);
450  }
452 
454 
457  template <typename U>
458  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_component,
460  GetTypedComponentImpl(vtkIdType idx, int comp) const
461  {
462  return this->GetValue(idx * this->NumberOfComponents + comp);
463  }
465 
467 
470  template <typename U>
471  typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_get_memory_size,
472  unsigned long>::type
473  GetActualMemorySizeImpl() const
474  {
475  return this->Backend->getMemorySize();
476  }
477 
482  template <typename U>
483  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_get_memory_size,
484  unsigned long>::type
485  GetActualMemorySizeImpl() const
486  {
487  return 1;
488  }
490 
491  friend class vtkGenericDataArray<vtkImplicitArray<BackendT>, ValueTypeT>;
492 };
493 
494 // Declare vtkArrayDownCast implementations for implicit containers:
496 VTK_ABI_NAMESPACE_END
497 
498 #include "vtkImplicitArray.txx"
499 
500 #endif // vtkImplicitArray_h
501 
502 // See vtkGenericDataArray for similar section
503 #ifdef VTK_IMPLICIT_VALUERANGE_INSTANTIATING
504 VTK_ABI_NAMESPACE_BEGIN
505 template <typename ValueType>
507 template <typename ValueType>
509 template <typename ValueType>
511 template <typename ValueType>
513 template <typename ValueType>
515 VTK_ABI_NAMESPACE_END
516 #include <functional>
517 
518 // Needed to export for this module and not CommonCore
519 #define VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType) \
520  template VTKCOMMONCORE_EXPORT bool DoComputeScalarRange( \
521  ArrayType*, ValueType*, vtkDataArrayPrivate::AllValues, const unsigned char*, unsigned char); \
522  template VTKCOMMONCORE_EXPORT bool DoComputeScalarRange(ArrayType*, ValueType*, \
523  vtkDataArrayPrivate::FiniteValues, const unsigned char*, unsigned char); \
524  template VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(ArrayType*, ValueType[2], \
525  vtkDataArrayPrivate::AllValues, const unsigned char*, unsigned char); \
526  template VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(ArrayType*, ValueType[2], \
527  vtkDataArrayPrivate::FiniteValues, const unsigned char*, unsigned char);
528 
529 #define VTK_INSTANTIATE_VALUERANGE_VALUETYPE(ValueType) \
530  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE( \
531  vtkImplicitArray<vtkAffineImplicitBackend<ValueType>>, ValueType) \
532  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE( \
533  vtkImplicitArray<vtkCompositeImplicitBackend<ValueType>>, ValueType) \
534  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE( \
535  vtkImplicitArray<vtkConstantImplicitBackend<ValueType>>, ValueType) \
536  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE( \
537  vtkImplicitArray<vtkStructuredPointBackend<ValueType>>, ValueType) \
538  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE( \
539  vtkImplicitArray<vtkIndexedImplicitBackend<ValueType>>, ValueType) \
540  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<ValueType(int)>>, ValueType)
541 
542 #elif defined(VTK_USE_EXTERN_TEMPLATE) // VTK_IMPLICIT_VALUERANGE_INSTANTIATING
543 
544 #ifndef VTK_IMPLICIT_TEMPLATE_EXTERN
545 #define VTK_IMPLICIT_TEMPLATE_EXTERN
546 #ifdef _MSC_VER
547 #pragma warning(push)
548 // The following is needed when the following is declared
549 // dllexport and is used from another class in vtkCommonCore
550 #pragma warning(disable : 4910) // extern and dllexport incompatible
551 #endif
552 
553 VTK_ABI_NAMESPACE_BEGIN
554 template <typename ValueType>
556 template <typename ValueType>
558 template <typename ValueType>
560 template <typename ValueType>
562 template <typename ValueType>
564 VTK_ABI_NAMESPACE_END
565 #include <functional>
566 
567 namespace vtkDataArrayPrivate
568 {
569 VTK_ABI_NAMESPACE_BEGIN
570 template <typename A, typename R, typename T>
571 VTKCOMMONCORE_EXPORT bool DoComputeScalarRange(
572  A*, R*, T, const unsigned char* ghosts, unsigned char ghostsToSkip);
573 template <typename A, typename R>
574 VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(
575  A*, R[2], AllValues, const unsigned char* ghosts, unsigned char ghostsToSkip);
576 template <typename A, typename R>
577 VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(
578  A*, R[2], FiniteValues, const unsigned char* ghosts, unsigned char ghostsToSkip);
579 VTK_ABI_NAMESPACE_END
580 } // namespace vtkDataArrayPrivate
581 
582 #define VTK_DECLARE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType) \
583  extern template VTKCOMMONCORE_EXPORT bool DoComputeScalarRange( \
584  ArrayType*, ValueType*, vtkDataArrayPrivate::AllValues, const unsigned char*, unsigned char); \
585  extern template VTKCOMMONCORE_EXPORT bool DoComputeScalarRange(ArrayType*, ValueType*, \
586  vtkDataArrayPrivate::FiniteValues, const unsigned char*, unsigned char); \
587  extern template VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(ArrayType*, ValueType[2], \
588  vtkDataArrayPrivate::AllValues, const unsigned char*, unsigned char); \
589  extern template VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(ArrayType*, ValueType[2], \
590  vtkDataArrayPrivate::FiniteValues, const unsigned char*, unsigned char);
591 
592 #define VTK_DECLARE_VALUERANGE_VALUETYPE(ValueType) \
593  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
594  vtkImplicitArray<vtkAffineImplicitBackend<ValueType>>, ValueType) \
595  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
596  vtkImplicitArray<vtkCompositeImplicitBackend<ValueType>>, ValueType) \
597  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
598  vtkImplicitArray<vtkConstantImplicitBackend<ValueType>>, ValueType) \
599  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
600  vtkImplicitArray<vtkStructuredPointBackend<ValueType>>, ValueType) \
601  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
602  vtkImplicitArray<vtkIndexedImplicitBackend<ValueType>>, ValueType) \
603  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<ValueType(int)>>, ValueType)
604 
605 #define VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(BackendT) \
606  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<float>>, double) \
607  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<double>>, double) \
608  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<char>>, double) \
609  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<signed char>>, double) \
610  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned char>>, double) \
611  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<short>>, double) \
612  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned short>>, double) \
613  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<int>>, double) \
614  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned int>>, double) \
615  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<long>>, double) \
616  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned long>>, double) \
617  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<long long>>, double) \
618  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned long long>>, double)
619 
620 namespace vtkDataArrayPrivate
621 {
622 VTK_ABI_NAMESPACE_BEGIN
628 
632 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<signed char>(int)>, double)
641 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<unsigned long long(int)>>, double)
642 VTK_ABI_NAMESPACE_END
643 }
644 
645 #undef VTK_DECLARE_VALUERANGE_ARRAYTYPE
646 #undef VTK_DECLARE_VALUERANGE_VALUETYPE
647 
648 #ifdef _MSC_VER
649 #pragma warning(pop)
650 #endif
651 #endif // VTK_IMPLICIT_TEMPLATE_EXTERN
652 
653 #endif // VTK_IMPLICIT_VALUERANGE_INSTANTIATING
Abstract superclass for all arrays.
int GetNumberOfComponents() const
Set/Get the dimension (n) of the components.
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
A utility structure serving as a backend for composite arrays: an array composed of multiple arrays c...
void Modified() override
Removes out-of-date L2_NORM_RANGE() and L2_NORM_FINITE_RANGE() values.
Base interface for all typed vtkDataArray subclasses.
A read only array class that wraps an implicit function from integers to any value type supported by ...
ValueType GetTypedComponent(vtkIdType idx, int comp) const
Get component comp of the tuple at idx.
typename GenericDataArrayType::ValueType ValueType
void SetBackend(std::shared_ptr< BackendT > newBackend)
Setter/Getter for Backend.
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Will not do anything for these read only arrays!
vtkImplicitArrayTypeMacro(SelfType, GenericDataArrayType)
std::shared_ptr< BackendT > Backend
The backend object actually mapping the indexes.
unsigned long GetActualMemorySize() const override
Return the memory in kibibytes (1024 bytes) consumed by this implicit data array.
static vtkImplicitArray< BackendT > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
std::unique_ptr< vtkInternals > Internals
bool ReallocateTuples(vtkIdType vtkNotUsed(numTuples))
No allocation necessary.
void Initialize() override
Reset the array to default construction.
void ConstructBackend(Params &&... params)
Utility method for setting backend parameterization directly.
~vtkImplicitArray() override
bool AllocateTuples(vtkIdType vtkNotUsed(numTuples))
No allocation necessary.
void * GetVoidPointer(vtkIdType valueIdx) override
Use of this method is discouraged, it creates a memory copy of the data into a contiguous AoS-ordered...
int GetArrayType() const override
Get the type of array this is when down casting.
void GetTypedTuple(vtkIdType idx, ValueType *tuple) const
Copy the tuple at idx into tuple.
void ImplicitDeepCopy(vtkImplicitArray< OtherBackend > *other)
Specific DeepCopy for implicit arrays.
ValueType GetValue(vtkIdType idx) const
Implementation of vtkGDAConceptMethods.
static vtkImplicitArray * New()
void SetValue(vtkIdType idx, ValueType value)
Will not do anything for these read only arrays!
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Will not do anything for these read only arrays!
std::shared_ptr< BackendT > GetBackend()
Setter/Getter for Backend.
void Squeeze() override
Release all extraneous internal memory including the void pointer used by GetVoidPointer
A backend for the vtkImplicitArray framework allowing one to use a subset of a given data array,...
A backend for the vtkImplicitArray to query structured points efficiently.
VTKCOMMONCORE_EXPORT bool DoComputeScalarRange(A *, R *, T, const unsigned char *ghosts, unsigned char ghostsToSkip)
VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(A *, R[2], AllValues, const unsigned char *ghosts, unsigned char ghostsToSkip)
@ function
Definition: vtkX3D.h:249
@ value
Definition: vtkX3D.h:220
@ type
Definition: vtkX3D.h:516
A utility structure serving as a backend for affine (as a function of the index) implicit arrays.
A utility structure serving as a backend for constant implicit arrays.
A composite trait for handling all the different capabilities a "backend" to an implicit array can ha...
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
vtkArrayDownCast_TemplateFastCastMacro(vtkImplicitArray)
#define VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(BackendT)
#define VTK_DECLARE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType)
int vtkIdType
Definition: vtkType.h:315