VTK  9.6.20260408
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 "Supplied backend type does not have mandatory read trait. Must implement either map() const "
164 "or operator() const.");
167
168public:
170 vtkImplicitArrayTypeMacro(SelfType, GenericDataArrayType);
171 using typename Superclass::ArrayTypeTag;
172 using typename Superclass::DataTypeTag;
173 using typename Superclass::ValueType;
174 using BackendType = BackendT;
175
177
179
185 ValueType GetValue(vtkIdType idx) const { return this->GetValueImpl<BackendT>(idx); }
186
190 void SetValue(vtkIdType idx, ValueType value);
191
195 void GetTypedTuple(vtkIdType idx, ValueType* tuple) const
196 {
197 this->GetTypedTupleImpl<BackendT>(idx, tuple);
198 }
199
203 void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple);
204
209 {
210 return this->GetTypedComponentImpl<BackendT>(idx, comp);
211 }
212
216 void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value);
218
220
223 void SetBackend(std::shared_ptr<BackendT> newBackend)
224 {
225 this->Backend = newBackend;
226 this->Modified();
227 }
228 std::shared_ptr<BackendT> GetBackend() { return this->Backend; }
229 bool IsBackendConstructed() const { return this->Backend != nullptr; }
231
235 template <typename... Params>
236 void ConstructBackend(Params&&... params)
237 {
238 this->SetBackend(std::make_shared<BackendT>(std::forward<Params>(params)...));
239 }
240
244 void Initialize() override
245 {
246 this->Initialize<BackendT>();
247 this->Squeeze();
249 }
250
260 unsigned long GetActualMemorySize() const override
261 {
262 return this->GetActualMemorySizeImpl<BackendT>();
263 }
264
266
277 template <typename OtherBackend, int OtherArrayType>
279 {
280 static_assert(std::is_same_v<BackendT, OtherBackend>,
281 "Cannot copy implicit array with one type of backend to an implicit array with a different "
282 "type of backend");
283 static_assert(ArrayTypeTag::value == OtherArrayType,
284 "Cannot copy implicit array with one array type to an implicit array with a different "
285 "array type");
287 this->SetNumberOfTuples(other->GetNumberOfTuples());
288 this->SetBackend(other->GetBackend());
289 }
290 using vtkDataArray::DeepCopy;
291 void DeepCopy(vtkDataArray* da) override
292 {
293 if (da == nullptr || da == this)
294 {
295 return;
296 }
297 // dispatch to the templated version if possible
299 {
300 this->ImplicitDeepCopy(otherImplicit);
301 }
302 else
303 {
304 // otherwise fallback to generic implementation that will copy all values
305 this->vtkDataArray::DeepCopy(da);
306 }
307 }
308
309
311
319
320protected:
323
325
328 VTK_DEPRECATED_IN_9_7_0("No longer needed")
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:
497
498VTK_ABI_NAMESPACE_END
499
500// This macro is used by the subclasses to create dummy
501// declarations for these functions such that the wrapper
502// can see them. The wrappers ignore vtkImplicitArray.
503#define vtkCreateImplicitWrappedArrayInterface(T) \
504 vtkCreateWrappedArrayReadInterface(T); \
505 bool SetNumberOfValues(vtkIdType number) override;
506
507#include "vtkImplicitArray.txx"
508
509#endif // vtkImplicitArray_h
510
511// VTK-HeaderTest-Exclude: vtkImplicitArray.h
Abstract superclass for all arrays.
virtual void SetNumberOfTuples(vtkIdType numTuples)
Set the number of tuples (a component group) in the array.
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
virtual void Initialize()
Release storage and reset array to initial state.
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.
bool IsBackendConstructed() const
Setter/Getter for Backend.
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 SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Will not do anything for these read only arrays!
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.
void Initialize() override
Reset the array to default construction.
vtkImplicitArray< vtkAffineImplicitBackend< ValueTypeT >, ArrayType > SelfType
std::integral_constant< int, vtkArrayTypes::VTK_ABSTRACT_ARRAY > ArrayTypeTag
void DeepCopy(vtkDataArray *da) override
Specific DeepCopy for implicit arrays.
void GetTypedTuple(vtkIdType idx, ValueType *tuple) const
Copy the tuple at idx into tuple.
~vtkImplicitArray() override
#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_DEPRECATED_IN_9_7_0(reason)
int vtkIdType
Definition vtkType.h:363