VTK  9.3.0
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 
173 
175 
181  inline ValueType GetValue(vtkIdType idx) const { return this->GetValueImpl<BackendT>(idx); }
182 
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 &&
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 CmmonCore
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 bool DoComputeScalarRange(A*, R*, T, const unsigned char* ghosts, unsigned char ghostsToSkip);
517 template <typename A, typename R>
519  A*, R[2], AllValues, const unsigned char* ghosts, unsigned char ghostsToSkip);
520 template <typename A, typename R>
522  A*, R[2], FiniteValues, const unsigned char* ghosts, unsigned char ghostsToSkip);
523 VTK_ABI_NAMESPACE_END
524 } // namespace vtkDataArrayPrivate
525 
526 #define VTK_DECLARE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType) \
527  extern template VTKCOMMONCORE_EXPORT bool DoComputeScalarRange( \
528  ArrayType*, ValueType*, vtkDataArrayPrivate::AllValues, const unsigned char*, unsigned char); \
529  extern template VTKCOMMONCORE_EXPORT bool DoComputeScalarRange(ArrayType*, ValueType*, \
530  vtkDataArrayPrivate::FiniteValues, const unsigned char*, unsigned char); \
531  extern template VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(ArrayType*, ValueType[2], \
532  vtkDataArrayPrivate::AllValues, const unsigned char*, unsigned char); \
533  extern template VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(ArrayType*, ValueType[2], \
534  vtkDataArrayPrivate::FiniteValues, const unsigned char*, unsigned char);
535 
536 #define VTK_DECLARE_VALUERANGE_VALUETYPE(ValueType) \
537  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
538  vtkImplicitArray<vtkAffineImplicitBackend<ValueType>>, ValueType) \
539  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
540  vtkImplicitArray<vtkCompositeImplicitBackend<ValueType>>, ValueType) \
541  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
542  vtkImplicitArray<vtkConstantImplicitBackend<ValueType>>, ValueType) \
543  VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
544  vtkImplicitArray<vtkIndexedImplicitBackend<ValueType>>, ValueType) \
545  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<ValueType(int)>>, ValueType)
546 
547 #define VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(BackendT) \
548  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<float>>, double) \
549  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<double>>, double) \
550  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<char>>, double) \
551  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<signed char>>, double) \
552  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned char>>, double) \
553  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<short>>, double) \
554  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned short>>, double) \
555  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<int>>, double) \
556  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned int>>, double) \
557  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<long>>, double) \
558  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned long>>, double) \
559  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<long long>>, double) \
560  VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<BackendT<unsigned long long>>, double)
561 
562 namespace vtkDataArrayPrivate
563 {
564 VTK_ABI_NAMESPACE_BEGIN
569 
573 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<signed char>(int)>, double)
582 VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<unsigned long long(int)>>, double)
583 VTK_ABI_NAMESPACE_END
584 }
585 
586 #undef VTK_DECLARE_VALUERANGE_ARRAYTYPE
587 #undef VTK_DECLARE_VALUERANGE_VALUETYPE
588 
589 #ifdef _MSC_VER
590 #pragma warning(pop)
591 #endif
592 #endif // VTK_IMPLICIT_TEMPLATE_EXTERN
593 
594 #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.
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,...
bool DoComputeScalarRange(A *, R *, T, const unsigned char *ghosts, unsigned char ghostsToSkip)
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