VTK  9.3.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 
137 //-------------------------------------------------------------------------------------------------
138 // Special macro for implicit array types modifying the behavior of NewInstance to provide writable
139 // AOS arrays instead of empty implicit arrays
140 #define vtkImplicitArrayTypeMacro(thisClass, superclass) \
141  vtkAbstractTypeMacroWithNewInstanceType(thisClass, superclass, \
142  vtkAOSDataArrayTemplate<thisClass::ValueTypeT>, typeid(thisClass).name()); \
143  \
144 protected: \
145  vtkObjectBase* NewInstanceInternal() const override \
146  { \
147  return vtkAOSDataArrayTemplate<thisClass::ValueTypeT>::New(); \
148  } \
149  \
150 public:
151 //-------------------------------------------------------------------------------------------------
152 
153 VTK_ABI_NAMESPACE_BEGIN
154 template <class BackendT>
156  : public vtkGenericDataArray<vtkImplicitArray<BackendT>,
157  typename vtk::detail::implicit_array_traits<BackendT>::rtype>
158 {
160  static_assert(trait::can_read,
161  "Supplied backend type does not have mandatory read trait. Must implement either map() const "
162  "or operator() const.");
163  using ValueTypeT = typename trait::rtype;
165 
166 public:
170  using BackendType = BackendT;
171 
172  static vtkImplicitArray* New();
173 
175 
181  inline ValueType GetValue(vtkIdType idx) const { return this->GetValueImpl<BackendT>(idx); }
182 
186  void SetValue(vtkIdType idx, ValueType value);
187 
191  void GetTypedTuple(vtkIdType idx, ValueType* tuple) const
192  {
193  this->GetTypedTupleImpl<BackendT>(idx, tuple);
194  }
195 
199  void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple);
200 
204  inline ValueType GetTypedComponent(vtkIdType idx, int comp) const
205  {
206  return this->GetTypedComponentImpl<BackendT>(idx, comp);
207  }
208 
212  void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value);
214 
216 
219  void SetBackend(std::shared_ptr<BackendT> newBackend)
220  {
221  this->Backend = newBackend;
222  this->Modified();
223  }
224  std::shared_ptr<BackendT> GetBackend() { return this->Backend; }
226 
230  template <typename... Params>
231  void ConstructBackend(Params&&... params)
232  {
233  this->SetBackend(std::make_shared<BackendT>(std::forward<Params>(params)...));
234  }
235 
240  void* GetVoidPointer(vtkIdType valueIdx) override;
241 
245  void Squeeze() override;
246 
250  int GetArrayType() const override { return vtkAbstractArray::ImplicitArray; }
251 
255  void Initialize() override
256  {
257  this->Initialize<BackendT>();
258  this->Squeeze();
259  }
260 
272  template <typename OtherBackend>
274  {
276  "Cannot copy implicit array with one type of backend to an implicit array with a different "
277  "type of backend");
279  this->SetNumberOfTuples(other->GetNumberOfTuples());
280  this->SetBackend(other->GetBackend());
281  }
282 
284 
292 
293 protected:
295  ~vtkImplicitArray() override;
296 
298 
301  bool AllocateTuples(vtkIdType vtkNotUsed(numTuples)) { return true; }
302  bool ReallocateTuples(vtkIdType vtkNotUsed(numTuples)) { return true; }
304 
305  struct vtkInternals;
306  std::unique_ptr<vtkInternals> Internals;
307 
311  std::shared_ptr<BackendT> Backend;
312 
313 private:
314  vtkImplicitArray(const vtkImplicitArray&) = delete;
315  void operator=(const vtkImplicitArray&) = delete;
316 
318 
321  template <typename U>
323  vtkIdType idx) const
324  {
325  return this->Backend->map(idx);
326  }
328 
330 
333  template <typename U>
335  vtkIdType idx) const
336  {
337  return (*this->Backend)(idx);
338  }
340 
342 
345  template <typename U>
346  typename std::enable_if<vtk::detail::implicit_array_traits<U>::default_constructible, void>::type
347  Initialize()
348  {
349  this->Backend = std::make_shared<BackendT>();
350  }
352 
354 
357  template <typename U>
358  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::default_constructible, void>::type
359  Initialize()
360  {
361  this->Backend = nullptr;
362  }
364 
366 
369  template <typename U>
370  typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_direct_read_tuple, void>::type
371  GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
372  {
373  static_assert(
375  "Tuple type should be the same as the return type of the mapTuple");
376  this->Backend->mapTuple(idx, tuple);
377  }
379 
381 
384  template <typename U>
385  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_tuple &&
387  void>::type
388  GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
389  {
390  for (vtkIdType comp = 0; comp < this->NumberOfComponents; comp++)
391  {
392  tuple[comp] = this->GetTypedComponent(idx, comp);
393  }
394  }
395 
399  template <typename U>
400  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_tuple &&
401  !vtk::detail::implicit_array_traits<U>::can_direct_read_component,
402  void>::type
403  GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
404  {
405  const vtkIdType tupIdx = idx * this->NumberOfComponents;
406  for (vtkIdType comp = 0; comp < this->NumberOfComponents; comp++)
407  {
408  tuple[comp] = this->GetValue(tupIdx + comp);
409  }
410  }
412 
414 
417  template <typename U>
418  typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_direct_read_component,
420  GetTypedComponentImpl(vtkIdType idx, int comp) const
421  {
422  static_assert(
424  "Component return type should be the same as the return type of the mapComponent");
425  return this->Backend->mapComponent(idx, comp);
426  }
428 
430 
433  template <typename U>
434  typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_component,
436  GetTypedComponentImpl(vtkIdType idx, int comp) const
437  {
438  return this->GetValue(idx * this->NumberOfComponents + comp);
439  }
441 
442  friend class vtkGenericDataArray<vtkImplicitArray<BackendT>, ValueTypeT>;
443 };
444 
445 // Declare vtkArrayDownCast implementations for implicit containers:
447 VTK_ABI_NAMESPACE_END
448 
449 #include "vtkImplicitArray.txx"
450 
451 #endif // vtkImplicitArray_h
452 
453 // See vtkGenericDataArray for similar section
454 #ifdef VTK_IMPLICIT_VALUERANGE_INSTANTIATING
455 VTK_ABI_NAMESPACE_BEGIN
456 template <typename ValueType>
458 template <typename ValueType>
460 template <typename ValueType>
462 template <typename ValueType>
464 VTK_ABI_NAMESPACE_END
465 #include <functional>
466 
467 // Needed to export for this module and not CommonCore
468 #define VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType) \
469  template VTKCOMMONCORE_EXPORT bool DoComputeScalarRange( \
470  ArrayType*, ValueType*, vtkDataArrayPrivate::AllValues, const unsigned char*, unsigned char); \
471  template VTKCOMMONCORE_EXPORT bool DoComputeScalarRange(ArrayType*, ValueType*, \
472  vtkDataArrayPrivate::FiniteValues, const unsigned char*, unsigned char); \
473  template VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(ArrayType*, ValueType[2], \
474  vtkDataArrayPrivate::AllValues, const unsigned char*, unsigned char); \
475  template VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(ArrayType*, ValueType[2], \
476  vtkDataArrayPrivate::FiniteValues, const unsigned char*, unsigned char);
477 
478 #define VTK_INSTANTIATE_VALUERANGE_VALUETYPE(ValueType) \
479  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE( \
480  vtkImplicitArray<vtkAffineImplicitBackend<ValueType>>, ValueType) \
481  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE( \
482  vtkImplicitArray<vtkCompositeImplicitBackend<ValueType>>, ValueType) \
483  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE( \
484  vtkImplicitArray<vtkConstantImplicitBackend<ValueType>>, ValueType) \
485  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE( \
486  vtkImplicitArray<vtkIndexedImplicitBackend<ValueType>>, ValueType) \
487  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<ValueType(int)>>, ValueType)
488 
489 #elif defined(VTK_USE_EXTERN_TEMPLATE) // VTK_IMPLICIT_VALUERANGE_INSTANTIATING
490 
491 #ifndef VTK_IMPLICIT_TEMPLATE_EXTERN
492 #define VTK_IMPLICIT_TEMPLATE_EXTERN
493 #ifdef _MSC_VER
494 #pragma warning(push)
495 // The following is needed when the following is declared
496 // dllexport and is used from another class in vtkCommonCore
497 #pragma warning(disable : 4910) // extern and dllexport incompatible
498 #endif
499 
500 VTK_ABI_NAMESPACE_BEGIN
501 template <typename ValueType>
503 template <typename ValueType>
505 template <typename ValueType>
507 template <typename ValueType>
509 VTK_ABI_NAMESPACE_END
510 #include <functional>
511 
512 namespace vtkDataArrayPrivate
513 {
514 VTK_ABI_NAMESPACE_BEGIN
515 template <typename A, typename R, typename T>
516 VTKCOMMONCORE_EXPORT bool DoComputeScalarRange(
517  A*, R*, T, const unsigned char* ghosts, unsigned char ghostsToSkip);
518 template <typename A, typename R>
519 VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(
520  A*, R[2], AllValues, const unsigned char* ghosts, unsigned char ghostsToSkip);
521 template <typename A, typename R>
522 VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(
523  A*, R[2], FiniteValues, const unsigned char* ghosts, unsigned char ghostsToSkip);
524 VTK_ABI_NAMESPACE_END
525 } // namespace vtkDataArrayPrivate
526 
527 #define VTK_DECLARE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType) \
528  extern template VTKCOMMONCORE_EXPORT bool DoComputeScalarRange( \
529  ArrayType*, ValueType*, vtkDataArrayPrivate::AllValues, const unsigned char*, unsigned char); \
530  extern template VTKCOMMONCORE_EXPORT bool DoComputeScalarRange(ArrayType*, ValueType*, \
531  vtkDataArrayPrivate::FiniteValues, const unsigned char*, unsigned char); \
532  extern template VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(ArrayType*, ValueType[2], \
533  vtkDataArrayPrivate::AllValues, const unsigned char*, unsigned char); \
534  extern template VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(ArrayType*, ValueType[2], \
535  vtkDataArrayPrivate::FiniteValues, const unsigned char*, unsigned char);
536 
537 #define VTK_DECLARE_VALUERANGE_VALUETYPE(ValueType) \
538  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
539  vtkImplicitArray<vtkAffineImplicitBackend<ValueType>>, ValueType) \
540  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
541  vtkImplicitArray<vtkCompositeImplicitBackend<ValueType>>, ValueType) \
542  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
543  vtkImplicitArray<vtkConstantImplicitBackend<ValueType>>, ValueType) \
544  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
545  vtkImplicitArray<vtkIndexedImplicitBackend<ValueType>>, ValueType) \
546  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<ValueType(int)>>, ValueType)
547 
548 #define VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(BackendT) \
549  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<float>>, double) \
550  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<double>>, double) \
551  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<char>>, double) \
552  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<signed char>>, double) \
553  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned char>>, double) \
554  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<short>>, double) \
555  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned short>>, double) \
556  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<int>>, double) \
557  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned int>>, double) \
558  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<long>>, double) \
559  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned long>>, double) \
560  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<long long>>, double) \
561  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned long long>>, double)
562 
563 namespace vtkDataArrayPrivate
564 {
565 VTK_ABI_NAMESPACE_BEGIN
570 
574 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<signed char>(int)>, double)
583 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<unsigned long long(int)>>, double)
584 VTK_ABI_NAMESPACE_END
585 }
586 
587 #undef VTK_DECLARE_VALUERANGE_ARRAYTYPE
588 #undef VTK_DECLARE_VALUERANGE_VALUETYPE
589 
590 #ifdef _MSC_VER
591 #pragma warning(pop)
592 #endif
593 #endif // VTK_IMPLICIT_TEMPLATE_EXTERN
594 
595 #endif // VTK_IMPLICIT_VALUERANGE_INSTANTIATING
A backend for the vtkImplicitArray framework allowing one to use a subset of a given data array...
A composite trait for handling all the different capabilities a "backend" to an implicit array can ha...
std::shared_ptr< BackendT > Backend
The backend object actually mapping the indexes.
ValueType GetTypedComponent(vtkIdType idx, int comp) const
Get component comp of the tuple at idx.
int GetNumberOfComponents() const
Set/Get the dimension (n) of the components.
Abstract superclass for all arrays.
VTKCOMMONCORE_EXPORT bool DoComputeScalarRange(A *, R *, T, const unsigned char *ghosts, unsigned char ghostsToSkip)
void ImplicitDeepCopy(vtkImplicitArray< OtherBackend > *other)
Specific DeepCopy for implicit arrays.
VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(A *, R[2], AllValues, const unsigned char *ghosts, unsigned char ghostsToSkip)
vtkArrayDownCast_TemplateFastCastMacro(vtkImplicitArray)
void ConstructBackend(Params &&...params)
Utility method for setting backend parameterization directly.
void GetTypedTuple(vtkIdType idx, ValueType *tuple) const
Copy the tuple at idx into tuple.
int vtkIdType
Definition: vtkType.h:315
Base interface for all typed vtkDataArray subclasses.
#define VTK_DECLARE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType)
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Will not do anything for these read only arrays!
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Will not do anything for these read only arrays!
void Squeeze() override
Release all extraneous internal memory including the void pointer used by GetVoidPointer ...
bool AllocateTuples(vtkIdType vtkNotUsed(numTuples))
No allocation necessary.
vtkImplicitArrayTypeMacro(SelfType, GenericDataArrayType)
ValueType GetValue(vtkIdType idx) const
Implementation of vtkGDAConceptMethods.
A utility structure serving as a backend for constant implicit arrays.
std::unique_ptr< vtkInternals > Internals
void SetBackend(std::shared_ptr< BackendT > newBackend)
Setter/Getter for Backend.
std::shared_ptr< BackendT > GetBackend()
Setter/Getter for Backend.
static vtkImplicitArray< BackendT > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
typename GenericDataArrayType::ValueType ValueType
void SetValue(vtkIdType idx, ValueType value)
Will not do anything for these read only arrays!
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
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 ...
static vtkImplicitArray * New()
#define VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(BackendT)
~vtkImplicitArray() override
bool ReallocateTuples(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 Modified() override
Removes out-of-date L2_NORM_RANGE() and L2_NORM_FINITE_RANGE() values.
A utility structure serving as a backend for affine (as a function of the index) implicit arrays...
void Initialize() override
Reset the array to default construction.
A utility structure serving as a backend for composite arrays: an array composed of multiple arrays c...