VTK  9.5.20251008
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
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 \
149protected: \
150 vtkObjectBase* NewInstanceInternal() const override \
151 { \
152 return vtkAOSDataArrayTemplate<typename thisClass::ValueType>::New(); \
153 } \
154 \
155public:
156//-------------------------------------------------------------------------------------------------
157
158VTK_ABI_NAMESPACE_BEGIN
159template <class BackendT, int ArrayType = vtkArrayTypes::ImplicitArray>
161 : public vtkGenericDataArray<vtkImplicitArray<BackendT, ArrayType>,
162 typename vtk::detail::implicit_array_traits<BackendT>::rtype, ArrayType>
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;
171
172public:
175 using typename Superclass::ArrayTypeTag;
176 using typename Superclass::DataTypeTag;
177 using typename Superclass::ValueType;
178 using BackendType = BackendT;
179
181
183
189 ValueType GetValue(vtkIdType idx) const { return this->GetValueImpl<BackendT>(idx); }
190
194 void SetValue(vtkIdType idx, ValueType value);
195
199 void GetTypedTuple(vtkIdType idx, ValueType* tuple) const
200 {
201 this->GetTypedTupleImpl<BackendT>(idx, tuple);
202 }
203
207 void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple);
208
213 {
214 return this->GetTypedComponentImpl<BackendT>(idx, comp);
215 }
216
220 void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value);
222
224
227 void SetBackend(std::shared_ptr<BackendT> newBackend)
228 {
229 this->Backend = newBackend;
230 this->Modified();
231 }
232 std::shared_ptr<BackendT> GetBackend() { return this->Backend; }
234
238 template <typename... Params>
239 void ConstructBackend(Params&&... params)
240 {
241 this->SetBackend(std::make_shared<BackendT>(std::forward<Params>(params)...));
242 }
243
253 void* GetVoidPointer(vtkIdType valueIdx) override;
254
258 void Squeeze() override;
259
263 void Initialize() override
264 {
265 this->Initialize<BackendT>();
266 this->Squeeze();
268 }
269
279 unsigned long GetActualMemorySize() const override
280 {
281 return this->GetActualMemorySizeImpl<BackendT>();
282 }
283
285
296 template <typename OtherBackend, int OtherArrayType>
298 {
299 static_assert(std::is_same_v<BackendT, OtherBackend>,
300 "Cannot copy implicit array with one type of backend to an implicit array with a different "
301 "type of backend");
302 static_assert(ArrayTypeTag::value == OtherArrayType,
303 "Cannot copy implicit array with one array type to an implicit array with a different "
304 "array type");
306 this->SetNumberOfTuples(other->GetNumberOfTuples());
307 this->SetBackend(other->GetBackend());
308 }
309 using vtkDataArray::DeepCopy;
310 void DeepCopy(vtkDataArray* da) override
311 {
312 if (da == nullptr || da == this)
313 {
314 return;
315 }
316 // dispatch to the templated version if possible
318 {
319 this->ImplicitDeepCopy(otherImplicit);
320 }
321 else
322 {
323 // otherwise fallback to generic implementation that will copy all values
324 this->vtkDataArray::DeepCopy(da);
325 }
326 }
328
330
338
339protected:
342
344
347 bool AllocateTuples(vtkIdType vtkNotUsed(numTuples)) { return true; }
348 bool ReallocateTuples(vtkIdType vtkNotUsed(numTuples)) { return true; }
350
351 struct vtkInternals;
352 std::unique_ptr<vtkInternals> Internals;
353
357 std::shared_ptr<BackendT> Backend;
358
359private:
360 vtkImplicitArray(const vtkImplicitArray&) = delete;
361 void operator=(const vtkImplicitArray&) = delete;
362
364
367 template <typename U>
368 typename std::enable_if<vtk::detail::has_map_trait<U>::value, ValueType>::type GetValueImpl(
369 vtkIdType idx) const
370 {
371 return this->Backend->map(idx);
372 }
374
376
379 template <typename U>
380 typename std::enable_if<vtk::detail::is_closure_trait<U>::value, ValueType>::type GetValueImpl(
381 vtkIdType idx) const
382 {
383 return (*this->Backend)(idx);
384 }
386
388
391 template <typename U>
392 typename std::enable_if<vtk::detail::implicit_array_traits<U>::default_constructible, void>::type
393 Initialize()
394 {
395 this->Backend = std::make_shared<BackendT>();
396 }
398
400
403 template <typename U>
404 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::default_constructible, void>::type
405 Initialize()
406 {
407 this->Backend = nullptr;
408 }
410
412
415 template <typename U>
416 typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_direct_read_tuple, void>::type
417 GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
418 {
419 static_assert(
420 std::is_same<typename vtk::detail::can_map_tuple_trait<U>::rtype, ValueType>::value,
421 "Tuple type should be the same as the return type of the mapTuple");
422 this->Backend->mapTuple(idx, tuple);
423 }
425
427
430 template <typename U>
431 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_tuple &&
433 void>::type
434 GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
435 {
436 for (vtkIdType comp = 0; comp < this->NumberOfComponents; comp++)
437 {
438 tuple[comp] = this->GetTypedComponent(idx, comp);
439 }
440 }
441
445 template <typename U>
446 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_tuple &&
448 void>::type
449 GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
450 {
451 const vtkIdType tupIdx = idx * this->NumberOfComponents;
452 for (vtkIdType comp = 0; comp < this->NumberOfComponents; comp++)
453 {
454 tuple[comp] = this->GetValue(tupIdx + comp);
455 }
456 }
458
460
463 template <typename U>
464 typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_direct_read_component,
465 ValueType>::type
466 GetTypedComponentImpl(vtkIdType idx, int comp) const
467 {
468 static_assert(
469 std::is_same<typename vtk::detail::can_map_component_trait<U>::rtype, ValueType>::value,
470 "Component return type should be the same as the return type of the mapComponent");
471 return this->Backend->mapComponent(idx, comp);
472 }
474
476
479 template <typename U>
480 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_component,
481 ValueType>::type
482 GetTypedComponentImpl(vtkIdType idx, int comp) const
483 {
484 return this->GetValue(idx * this->NumberOfComponents + comp);
485 }
487
489
492 template <typename U>
493 typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_get_memory_size,
494 unsigned long>::type
495 GetActualMemorySizeImpl() const
496 {
497 return this->Backend->getMemorySize();
498 }
499
504 template <typename U>
505 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_get_memory_size,
506 unsigned long>::type
507 GetActualMemorySizeImpl() const
508 {
509 return 1;
510 }
512
513 friend class vtkGenericDataArray<SelfType, ValueType, ArrayTypeTag::value>;
514};
515
516// Declare vtkArrayDownCast implementations for implicit containers:
518VTK_ABI_NAMESPACE_END
519
520#include "vtkImplicitArray.txx"
521
522#endif // vtkImplicitArray_h
523
524#define VTK_WRAP_TEMPLATE(...) __VA_ARGS__
525// See vtkGenericDataArray for similar section
526#ifdef VTK_USE_EXTERN_TEMPLATE
527
528#ifndef VTK_IMPLICIT_TEMPLATE_EXTERN
529#define VTK_IMPLICIT_TEMPLATE_EXTERN
530#ifdef _MSC_VER
531#pragma warning(push)
532// The following is needed when the following is declared
533// dllexport and is used from another class in vtkCommonCore
534#pragma warning(disable : 4910) // extern and dllexport incompatible
535#endif
536
537VTK_ABI_NAMESPACE_BEGIN
538template <typename ValueType>
540template <typename ValueType>
542template <typename ValueType>
544template <typename ValueType>
546template <typename ValueType>
548VTK_ABI_NAMESPACE_END
549#include <functional>
550
551namespace vtkDataArrayPrivate
552{
553VTK_ABI_NAMESPACE_BEGIN
554template <typename A, typename R, typename T>
555VTKCOMMONCORE_EXPORT bool DoComputeScalarRange(
556 A*, R*, T, const unsigned char* ghosts, unsigned char ghostsToSkip);
557template <typename A, typename R>
558VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(
559 A*, R[2], AllValues, const unsigned char* ghosts, unsigned char ghostsToSkip);
560template <typename A, typename R>
561VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(
562 A*, R[2], FiniteValues, const unsigned char* ghosts, unsigned char ghostsToSkip);
563VTK_ABI_NAMESPACE_END
564} // namespace vtkDataArrayPrivate
565
566#define VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(BackendT, ArrayTypeValue) \
567 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
568 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<float>, ArrayTypeValue>), double) \
569 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
570 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<double>, ArrayTypeValue>), double) \
571 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
572 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<char>, ArrayTypeValue>), double) \
573 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
574 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<signed char>, ArrayTypeValue>), double) \
575 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
576 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned char>, ArrayTypeValue>), double) \
577 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
578 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<short>, ArrayTypeValue>), double) \
579 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
580 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned short>, ArrayTypeValue>), double) \
581 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
582 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<int>, ArrayTypeValue>), double) \
583 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
584 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned int>, ArrayTypeValue>), double) \
585 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
586 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<long>, ArrayTypeValue>), double) \
587 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
588 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned long>, ArrayTypeValue>), double) \
589 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
590 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<long long>, ArrayTypeValue>), double) \
591 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
592 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned long long>, ArrayTypeValue>), double)
593
594namespace vtkDataArrayPrivate
595{
596VTK_ABI_NAMESPACE_BEGIN
605
608 double)
611 double)
614 double)
617 vtkImplicitArray<std::function<signed char(int)>, vtkArrayTypes::StdFunctionArray>),
618 double)
621 vtkImplicitArray<std::function<unsigned char(int)>, vtkArrayTypes::StdFunctionArray>),
622 double)
625 double)
628 vtkImplicitArray<std::function<unsigned short(int)>, vtkArrayTypes::StdFunctionArray>),
629 double)
632 double)
635 vtkImplicitArray<std::function<unsigned int(int)>, vtkArrayTypes::StdFunctionArray>),
636 double)
639 double)
642 vtkImplicitArray<std::function<unsigned long(int)>, vtkArrayTypes::StdFunctionArray>),
643 double)
646 vtkImplicitArray<std::function<long long(int)>, vtkArrayTypes::StdFunctionArray>),
647 double)
650 vtkImplicitArray<std::function<unsigned long long(int)>, vtkArrayTypes::StdFunctionArray>),
651 double)
652VTK_ABI_NAMESPACE_END
653}
654
655#ifdef _MSC_VER
656#pragma warning(pop)
657#endif
658#endif // VTK_IMPLICIT_TEMPLATE_EXTERN
659
660#endif // VTK_USE_EXTERN_TEMPLATE
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.
std::integral_constant< int, vtkArrayTypes::AbstractArray > ArrayTypeTag
std::integral_constant< int, VTK_OPAQUE > DataTypeTag
A utility structure serving as a backend for composite arrays: an array composed of multiple arrays c...
abstract superclass for arrays of numeric data
void DeepCopy(vtkAbstractArray *aa) override
Deep copy of data.
void Modified() override
Removes out-of-date L2_NORM_RANGE() and L2_NORM_FINITE_RANGE() values.
Base interface for all typed vtkDataArray subclasses.
void Initialize() override
Release storage and reset array to initial state.
void SetNumberOfTuples(vtkIdType number) override
Set the number of tuples (a component group) in the array.
void SetNumberOfComponents(int num) override
Set/Get the dimension (n) of the components.
A read only array class that wraps an implicit function from integers to any value type supported by ...
vtkImplicitArrayTypeMacro(SelfType, GenericDataArrayType)
void ImplicitDeepCopy(vtkImplicitArray< OtherBackend, OtherArrayType > *other)
Specific DeepCopy for implicit 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
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Will not do anything for these read only arrays!
std::unique_ptr< vtkInternals > Internals
bool AllocateTuples(vtkIdType numTuples)
No allocation necessary.
void ConstructBackend(Params &&... params)
Utility method for setting backend parameterization directly.
bool ReallocateTuples(vtkIdType numTuples)
No allocation necessary.
std::shared_ptr< BackendT > GetBackend()
Setter/Getter for Backend.
static vtkImplicitArray< BackendT, ArrayType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
static vtkImplicitArray * New()
unsigned long GetActualMemorySize() const override
Return the memory in kibibytes (1024 bytes) consumed by this implicit data array.
void SetBackend(std::shared_ptr< BackendT > newBackend)
Setter/Getter for Backend.
void SetValue(vtkIdType idx, ValueType value)
Will not do anything for these read only arrays!
ValueType GetValue(vtkIdType idx) const
Implementation of vtkGDAConceptMethods.
ValueType GetTypedComponent(vtkIdType idx, int comp) const
Get component comp of the tuple at idx.
std::integral_constant< int, vtkArrayTypes::AbstractArray > ArrayTypeTag
void Initialize() override
Reset the array to default construction.
vtkImplicitArray< BackendT, ArrayType > SelfType
void * GetVoidPointer(vtkIdType valueIdx) override
Use of this method is discouraged, it creates a memory copy of the data into a contiguous AoS-ordered...
void DeepCopy(vtkDataArray *da) override
Specific DeepCopy for implicit arrays.
std::shared_ptr< BackendT > Backend
The backend object actually mapping the indexes.
void GetTypedTuple(vtkIdType idx, ValueType *tuple) const
Copy the tuple at idx into tuple.
~vtkImplicitArray() override
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.
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...
#define vtkArrayDownCast_Template2FastCastMacro(ArrayT)
Same as vtkArrayDownCast_FastCastMacro, but treats ArrayT as a two-parameter template (the parameter ...
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
#define VTK_DECLARE_VALUERANGE_ARRAYTYPE(ArrayType, ValueType)
#define VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(BackendT, ArrayTypeValue)
#define VTK_WRAP_TEMPLATE(...)
int vtkIdType
Definition vtkType.h:367
vtkArrayTypes
Definition vtkType.h:71
@ CompositeArray
Definition vtkType.h:92
@ IndexedArray
Definition vtkType.h:94
@ StructuredPointArray
Definition vtkType.h:97
@ AffineArray
Definition vtkType.h:91
@ StdFunctionArray
Definition vtkType.h:95
@ ConstantArray
Definition vtkType.h:93