VTK  9.6.20260222
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
139
140//-------------------------------------------------------------------------------------------------
141// Special macro for implicit array types modifying the behavior of NewInstance to provide writable
142// AOS arrays instead of empty implicit arrays
143#define vtkImplicitArrayTypeMacro(thisClass, superclass) \
144 vtkAbstractTypeMacroWithNewInstanceType(thisClass, superclass, \
145 vtkAOSDataArrayTemplate<typename thisClass::ValueType>, typeid(thisClass).name()); \
146 \
147protected: \
148 vtkObjectBase* NewInstanceInternal() const override \
149 { \
150 return vtkAOSDataArrayTemplate<typename thisClass::ValueType>::New(); \
151 } \
152 \
153public:
154//-------------------------------------------------------------------------------------------------
155
156VTK_ABI_NAMESPACE_BEGIN
157template <class BackendT, int ArrayType = vtkArrayTypes::VTK_IMPLICIT_ARRAY>
159 : public vtkGenericDataArray<vtkImplicitArray<BackendT, ArrayType>,
160 typename vtk::detail::implicit_array_traits<BackendT>::rtype, ArrayType>
161{
163 static_assert(trait::can_read,
164 "Supplied backend type does not have mandatory read trait. Must implement either map() const "
165 "or operator() const.");
166 using ValueTypeT = typename trait::rtype;
167 using GenericDataArrayType =
169
170public:
172 vtkImplicitArrayTypeMacro(SelfType, GenericDataArrayType);
173 using typename Superclass::ArrayTypeTag;
174 using typename Superclass::DataTypeTag;
175 using typename Superclass::ValueType;
176 using BackendType = BackendT;
177
179
181
187 ValueType GetValue(vtkIdType idx) const { return this->GetValueImpl<BackendT>(idx); }
188
192 void SetValue(vtkIdType idx, ValueType value);
193
197 void GetTypedTuple(vtkIdType idx, ValueType* tuple) const
198 {
199 this->GetTypedTupleImpl<BackendT>(idx, tuple);
200 }
201
205 void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple);
206
211 {
212 return this->GetTypedComponentImpl<BackendT>(idx, comp);
213 }
214
218 void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value);
220
222
225 void SetBackend(std::shared_ptr<BackendT> newBackend)
226 {
227 this->Backend = newBackend;
228 this->Modified();
229 }
230 std::shared_ptr<BackendT> GetBackend() { return this->Backend; }
232
236 template <typename... Params>
237 void ConstructBackend(Params&&... params)
238 {
239 this->SetBackend(std::make_shared<BackendT>(std::forward<Params>(params)...));
240 }
241
245 void Initialize() override
246 {
247 this->Initialize<BackendT>();
248 this->Squeeze();
250 }
251
261 unsigned long GetActualMemorySize() const override
262 {
263 return this->GetActualMemorySizeImpl<BackendT>();
264 }
265
267
278 template <typename OtherBackend, int OtherArrayType>
280 {
281 static_assert(std::is_same_v<BackendT, OtherBackend>,
282 "Cannot copy implicit array with one type of backend to an implicit array with a different "
283 "type of backend");
284 static_assert(ArrayTypeTag::value == OtherArrayType,
285 "Cannot copy implicit array with one array type to an implicit array with a different "
286 "array type");
288 this->SetNumberOfTuples(other->GetNumberOfTuples());
289 this->SetBackend(other->GetBackend());
290 }
291 using vtkDataArray::DeepCopy;
292 void DeepCopy(vtkDataArray* da) override
293 {
294 if (da == nullptr || da == this)
295 {
296 return;
297 }
298 // dispatch to the templated version if possible
300 {
301 this->ImplicitDeepCopy(otherImplicit);
302 }
303 else
304 {
305 // otherwise fallback to generic implementation that will copy all values
306 this->vtkDataArray::DeepCopy(da);
307 }
308 }
309
310
312
320
321protected:
324
326
329 bool AllocateTuples(vtkIdType vtkNotUsed(numTuples)) { return true; }
330 bool ReallocateTuples(vtkIdType vtkNotUsed(numTuples)) { return true; }
332
336 std::shared_ptr<BackendT> Backend;
337
338private:
339 vtkImplicitArray(const vtkImplicitArray&) = delete;
340 void operator=(const vtkImplicitArray&) = delete;
341
343
346 template <typename U>
347 typename std::enable_if<vtk::detail::has_map_trait<U>::value, ValueType>::type GetValueImpl(
348 vtkIdType idx) const
349 {
350 return this->Backend->map(idx);
351 }
353
355
358 template <typename U>
359 typename std::enable_if<vtk::detail::is_closure_trait<U>::value, ValueType>::type GetValueImpl(
360 vtkIdType idx) const
361 {
362 return (*this->Backend)(idx);
363 }
365
367
370 template <typename U>
371 typename std::enable_if<vtk::detail::implicit_array_traits<U>::default_constructible, void>::type
372 Initialize()
373 {
374 this->Backend = std::make_shared<BackendT>();
375 }
377
379
382 template <typename U>
383 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::default_constructible, void>::type
384 Initialize()
385 {
386 this->Backend = nullptr;
387 }
389
391
394 template <typename U>
395 typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_direct_read_tuple, void>::type
396 GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
397 {
398 static_assert(
399 std::is_same<typename vtk::detail::can_map_tuple_trait<U>::rtype, ValueType>::value,
400 "Tuple type should be the same as the return type of the mapTuple");
401 this->Backend->mapTuple(idx, tuple);
402 }
404
406
409 template <typename U>
410 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_tuple &&
412 void>::type
413 GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
414 {
415 for (vtkIdType comp = 0; comp < this->NumberOfComponents; comp++)
416 {
417 tuple[comp] = this->GetTypedComponent(idx, comp);
418 }
419 }
420
424 template <typename U>
425 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_tuple &&
427 void>::type
428 GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
429 {
430 const vtkIdType tupIdx = idx * this->NumberOfComponents;
431 for (vtkIdType comp = 0; comp < this->NumberOfComponents; comp++)
432 {
433 tuple[comp] = this->GetValue(tupIdx + comp);
434 }
435 }
437
439
442 template <typename U>
443 typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_direct_read_component,
444 ValueType>::type
445 GetTypedComponentImpl(vtkIdType idx, int comp) const
446 {
447 static_assert(
448 std::is_same<typename vtk::detail::can_map_component_trait<U>::rtype, ValueType>::value,
449 "Component return type should be the same as the return type of the mapComponent");
450 return this->Backend->mapComponent(idx, comp);
451 }
453
455
458 template <typename U>
459 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_component,
460 ValueType>::type
461 GetTypedComponentImpl(vtkIdType idx, int comp) const
462 {
463 return this->GetValue(idx * this->NumberOfComponents + comp);
464 }
466
468
471 template <typename U>
472 typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_get_memory_size,
473 unsigned long>::type
474 GetActualMemorySizeImpl() const
475 {
476 return this->Backend->getMemorySize();
477 }
478
483 template <typename U>
484 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_get_memory_size,
485 unsigned long>::type
486 GetActualMemorySizeImpl() const
487 {
488 return 1;
489 }
491
492 friend class vtkGenericDataArray<SelfType, ValueType, ArrayTypeTag::value>;
493};
494
495// Declare vtkArrayDownCast implementations for implicit containers:
497VTK_ABI_NAMESPACE_END
498
499#include "vtkImplicitArray.txx"
500
501#define vtkCreateReadOnlyWrappedArrayInterface(T) \
502 int GetDataType() const override; \
503 T GetDataTypeValueMin() const; \
504 T GetDataTypeValueMax() const; \
505 void GetTypedTuple(vtkIdType i, T* tuple) VTK_EXPECTS(0 <= i && i < GetNumberOfTuples()); \
506 T GetValue(vtkIdType id) const VTK_EXPECTS(0 <= id && id < GetNumberOfValues()); \
507 T* GetValueRange(int comp) VTK_SIZEHINT(2); \
508 T* GetValueRange() VTK_SIZEHINT(2);
509
510#endif // vtkImplicitArray_h
511
512#define VTK_WRAP_TEMPLATE(...) __VA_ARGS__
513// See vtkGenericDataArray for similar section
514#ifdef VTK_USE_EXTERN_TEMPLATE
515
516#ifndef VTK_IMPLICIT_TEMPLATE_EXTERN
517#define VTK_IMPLICIT_TEMPLATE_EXTERN
518#ifdef _MSC_VER
519#pragma warning(push)
520// The following is needed when the following is declared
521// dllexport and is used from another class in vtkCommonCore
522#pragma warning(disable : 4910) // extern and dllexport incompatible
523#endif
524
525VTK_ABI_NAMESPACE_BEGIN
526template <typename ValueType>
528template <typename ValueType>
530template <typename ValueType>
532template <typename ValueType>
534template <typename ValueType>
536VTK_ABI_NAMESPACE_END
537#include <functional>
538
539namespace vtkDataArrayPrivate
540{
541VTK_ABI_NAMESPACE_BEGIN
542template <typename A, typename R, typename T>
543VTKCOMMONCORE_EXPORT bool DoComputeScalarRange(
544 A*, R*, T, const unsigned char* ghosts, unsigned char ghostsToSkip);
545template <typename A, typename R>
546VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(
547 A*, R[2], AllValues, const unsigned char* ghosts, unsigned char ghostsToSkip);
548template <typename A, typename R>
549VTKCOMMONCORE_EXPORT bool DoComputeVectorRange(
550 A*, R[2], FiniteValues, const unsigned char* ghosts, unsigned char ghostsToSkip);
551VTK_ABI_NAMESPACE_END
552} // namespace vtkDataArrayPrivate
553
554#define VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(BackendT, ArrayTypeValue) \
555 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
556 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<float>, ArrayTypeValue>), double) \
557 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
558 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<double>, ArrayTypeValue>), double) \
559 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
560 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<char>, ArrayTypeValue>), double) \
561 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
562 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<signed char>, ArrayTypeValue>), double) \
563 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
564 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned char>, ArrayTypeValue>), double) \
565 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
566 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<short>, ArrayTypeValue>), double) \
567 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
568 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned short>, ArrayTypeValue>), double) \
569 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
570 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<int>, ArrayTypeValue>), double) \
571 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
572 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned int>, ArrayTypeValue>), double) \
573 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
574 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<long>, ArrayTypeValue>), double) \
575 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
576 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned long>, ArrayTypeValue>), double) \
577 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
578 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<long long>, ArrayTypeValue>), double) \
579 VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
580 VTK_WRAP_TEMPLATE(vtkImplicitArray<BackendT<unsigned long long>, ArrayTypeValue>), double)
581
582namespace vtkDataArrayPrivate
583{
584VTK_ABI_NAMESPACE_BEGIN
595
598 vtkImplicitArray<std::function<float(int)>, /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
599 double)
602 vtkImplicitArray<std::function<double(int)>, /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
603 double)
606 vtkImplicitArray<std::function<char(int)>, /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
607 double)
609 /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
610 double)
612 VTK_WRAP_TEMPLATE(vtkImplicitArray<std::function<unsigned char(int)>,
613 /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
614 double)
617 vtkImplicitArray<std::function<short(int)>, /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
618 double)
620 VTK_WRAP_TEMPLATE(vtkImplicitArray<std::function<unsigned short(int)>,
621 /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
622 double)
625 vtkImplicitArray<std::function<int(int)>, /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
626 double)
628 VTK_WRAP_TEMPLATE(vtkImplicitArray<std::function<unsigned int(int)>,
629 /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
630 double)
633 vtkImplicitArray<std::function<long(int)>, /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
634 double)
636 VTK_WRAP_TEMPLATE(vtkImplicitArray<std::function<unsigned long(int)>,
637 /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
638 double)
640 /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
641 double)
643 VTK_WRAP_TEMPLATE(vtkImplicitArray<std::function<unsigned long long(int)>,
644 /* vtkArrayTypes::VTK_STD_FUNCTION_ARRAY */ 15>),
645 double)
646VTK_ABI_NAMESPACE_END
647}
648
649#ifdef _MSC_VER
650#pragma warning(pop)
651#endif
652#endif // VTK_IMPLICIT_TEMPLATE_EXTERN
653
654#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:363
vtkArrayTypes
Definition vtkType.h:71
@ VTK_INDEXED_ARRAY
Definition vtkType.h:94
@ 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