VTK  9.5.20251213
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
141
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::VTK_IMPLICIT_ARRAY>
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;
169 using GenericDataArrayType =
171
172public:
174 vtkImplicitArrayTypeMacro(SelfType, GenericDataArrayType);
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 }
327
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#define vtkCreateReadOnlyWrappedArrayInterface(T) \
523 int GetDataType() const override; \
524 T GetDataTypeValueMin() const; \
525 T GetDataTypeValueMax() const; \
526 void GetTypedTuple(vtkIdType i, T* tuple) VTK_EXPECTS(0 <= i && i < GetNumberOfTuples()); \
527 T GetValue(vtkIdType id) const VTK_EXPECTS(0 <= id && id < GetNumberOfValues()); \
528 T* GetValueRange(int comp) VTK_SIZEHINT(2); \
529 T* GetValueRange() VTK_SIZEHINT(2);
530
531#endif // vtkImplicitArray_h
532
533#define VTK_WRAP_TEMPLATE(...) __VA_ARGS__
534// See vtkGenericDataArray for similar section
535#ifdef VTK_USE_EXTERN_TEMPLATE
536
537#ifndef VTK_IMPLICIT_TEMPLATE_EXTERN
538#define VTK_IMPLICIT_TEMPLATE_EXTERN
539#ifdef _MSC_VER
540#pragma warning(push)
541// The following is needed when the following is declared
542// dllexport and is used from another class in vtkCommonCore
543#pragma warning(disable : 4910) // extern and dllexport incompatible
544#endif
545
546VTK_ABI_NAMESPACE_BEGIN
547template <typename ValueType>
549template <typename ValueType>
551template <typename ValueType>
553template <typename ValueType>
555template <typename ValueType>
557VTK_ABI_NAMESPACE_END
558#include <functional>
559
560namespace vtkDataArrayPrivate
561{
562VTK_ABI_NAMESPACE_BEGIN
563template <typename A, typename R, typename T>
564VTKCOMMONCORE_EXPORT bool DoComputeScalarRange(
565 A*, R*, T, const unsigned char* ghosts, unsigned char ghostsToSkip);
566template <typename A, typename R>
567VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(
568 A*, R[2], AllValues, const unsigned char* ghosts, unsigned char ghostsToSkip);
569template <typename A, typename R>
570VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(
571 A*, R[2], FiniteValues, const unsigned char* ghosts, unsigned char ghostsToSkip);
572VTK_ABI_NAMESPACE_END
573} // namespace vtkDataArrayPrivate
574
575#define VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(BackendT, ArrayTypeValue) \
576 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
577 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<float>, ArrayTypeValue>), double) \
578 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
579 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<double>, ArrayTypeValue>), double) \
580 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
581 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<char>, ArrayTypeValue>), double) \
582 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
583 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<signed char>, ArrayTypeValue>), double) \
584 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
585 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned char>, ArrayTypeValue>), double) \
586 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
587 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<short>, ArrayTypeValue>), double) \
588 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
589 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned short>, ArrayTypeValue>), double) \
590 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
591 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<int>, ArrayTypeValue>), double) \
592 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
593 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned int>, ArrayTypeValue>), double) \
594 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
595 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<long>, ArrayTypeValue>), double) \
596 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
597 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned long>, ArrayTypeValue>), double) \
598 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
599 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<long long>, ArrayTypeValue>), double) \
600 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
601 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned long long>, ArrayTypeValue>), double)
602
603namespace vtkDataArrayPrivate
604{
605VTK_ABI_NAMESPACE_BEGIN
616
620 double)
623 vtkImplicitArray<std::function<double(int)>, vtkArrayTypes::VTK_STD_FUNCTION_ARRAY>),
624 double)
628 double)
631 vtkImplicitArray<std::function<signed char(int)>, vtkArrayTypes::VTK_STD_FUNCTION_ARRAY>),
632 double)
635 vtkImplicitArray<std::function<unsigned char(int)>, vtkArrayTypes::VTK_STD_FUNCTION_ARRAY>),
636 double)
640 double)
643 vtkImplicitArray<std::function<unsigned short(int)>, vtkArrayTypes::VTK_STD_FUNCTION_ARRAY>),
644 double)
648 double)
651 vtkImplicitArray<std::function<unsigned int(int)>, vtkArrayTypes::VTK_STD_FUNCTION_ARRAY>),
652 double)
656 double)
659 vtkImplicitArray<std::function<unsigned long(int)>, vtkArrayTypes::VTK_STD_FUNCTION_ARRAY>),
660 double)
663 vtkImplicitArray<std::function<long long(int)>, vtkArrayTypes::VTK_STD_FUNCTION_ARRAY>),
664 double)
666 VTK_WRAP_TEMPLATE(vtkImplicitArray<std::function<unsigned long long(int)>,
668 double)
669VTK_ABI_NAMESPACE_END
670}
671
672#ifdef _MSC_VER
673#pragma warning(pop)
674#endif
675#endif // VTK_IMPLICIT_TEMPLATE_EXTERN
676
677#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, VTK_OPAQUE > DataTypeTag
std::integral_constant< int, vtkArrayTypes::VTK_ABSTRACT_ARRAY > ArrayTypeTag
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 ...
void ImplicitDeepCopy(vtkImplicitArray< OtherBackend, OtherArrayType > *other)
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
static vtkImplicitArray< BackendT, ArrayType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
void SetBackend(std::shared_ptr< vtkAffineImplicitBackend< T > > newBackend)
vtkImplicitArray< vtkAffineImplicitBackend< T >, ArrayType > SelfType
std::integral_constant< int, vtkArrayTypes::VTK_ABSTRACT_ARRAY > ArrayTypeTag
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.
VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(vtkAffineImplicitBackend, vtkArrayTypes::VTK_AFFINE_ARRAY) VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(vtkConstantImplicitBackend
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 vtkDataArray
#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
@ VTK_INDEXED_ARRAY
Definition vtkType.h:94
@ VTK_STD_FUNCTION_ARRAY
Definition vtkType.h:95
@ VTK_CONSTANT_ARRAY
Definition vtkType.h:93
@ VTK_AFFINE_ARRAY
Definition vtkType.h:91
@ VTK_COMPOSITE_ARRAY
Definition vtkType.h:92
@ VTK_STRUCTURED_POINT_ARRAY
Definition vtkType.h:97