VTK  9.6.20260315
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; }
230
234 template <typename... Params>
235 void ConstructBackend(Params&&... params)
236 {
237 this->SetBackend(std::make_shared<BackendT>(std::forward<Params>(params)...));
238 }
239
243 void Initialize() override
244 {
245 this->Initialize<BackendT>();
246 this->Squeeze();
248 }
249
259 unsigned long GetActualMemorySize() const override
260 {
261 return this->GetActualMemorySizeImpl<BackendT>();
262 }
263
265
276 template <typename OtherBackend, int OtherArrayType>
278 {
279 static_assert(std::is_same_v<BackendT, OtherBackend>,
280 "Cannot copy implicit array with one type of backend to an implicit array with a different "
281 "type of backend");
282 static_assert(ArrayTypeTag::value == OtherArrayType,
283 "Cannot copy implicit array with one array type to an implicit array with a different "
284 "array type");
286 this->SetNumberOfTuples(other->GetNumberOfTuples());
287 this->SetBackend(other->GetBackend());
288 }
289 using vtkDataArray::DeepCopy;
290 void DeepCopy(vtkDataArray* da) override
291 {
292 if (da == nullptr || da == this)
293 {
294 return;
295 }
296 // dispatch to the templated version if possible
298 {
299 this->ImplicitDeepCopy(otherImplicit);
300 }
301 else
302 {
303 // otherwise fallback to generic implementation that will copy all values
304 this->vtkDataArray::DeepCopy(da);
305 }
306 }
307
308
310
318
319protected:
322
324
327 bool AllocateTuples(vtkIdType vtkNotUsed(numTuples)) { return true; }
328 bool ReallocateTuples(vtkIdType vtkNotUsed(numTuples)) { return true; }
330
334 std::shared_ptr<BackendT> Backend;
335
336private:
337 vtkImplicitArray(const vtkImplicitArray&) = delete;
338 void operator=(const vtkImplicitArray&) = delete;
339
341
344 template <typename U>
345 typename std::enable_if<vtk::detail::has_map_trait<U>::value, ValueType>::type GetValueImpl(
346 vtkIdType idx) const
347 {
348 return this->Backend->map(idx);
349 }
351
353
356 template <typename U>
357 typename std::enable_if<vtk::detail::is_closure_trait<U>::value, ValueType>::type GetValueImpl(
358 vtkIdType idx) const
359 {
360 return (*this->Backend)(idx);
361 }
363
365
368 template <typename U>
369 typename std::enable_if<vtk::detail::implicit_array_traits<U>::default_constructible, void>::type
370 Initialize()
371 {
372 this->Backend = std::make_shared<BackendT>();
373 }
375
377
380 template <typename U>
381 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::default_constructible, void>::type
382 Initialize()
383 {
384 this->Backend = nullptr;
385 }
387
389
392 template <typename U>
393 typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_direct_read_tuple, void>::type
394 GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
395 {
396 static_assert(
397 std::is_same<typename vtk::detail::can_map_tuple_trait<U>::rtype, ValueType>::value,
398 "Tuple type should be the same as the return type of the mapTuple");
399 this->Backend->mapTuple(idx, tuple);
400 }
402
404
407 template <typename U>
408 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_tuple &&
410 void>::type
411 GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
412 {
413 for (vtkIdType comp = 0; comp < this->NumberOfComponents; comp++)
414 {
415 tuple[comp] = this->GetTypedComponent(idx, comp);
416 }
417 }
418
422 template <typename U>
423 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_tuple &&
425 void>::type
426 GetTypedTupleImpl(vtkIdType idx, ValueType* tuple) const
427 {
428 const vtkIdType tupIdx = idx * this->NumberOfComponents;
429 for (vtkIdType comp = 0; comp < this->NumberOfComponents; comp++)
430 {
431 tuple[comp] = this->GetValue(tupIdx + comp);
432 }
433 }
435
437
440 template <typename U>
441 typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_direct_read_component,
442 ValueType>::type
443 GetTypedComponentImpl(vtkIdType idx, int comp) const
444 {
445 static_assert(
446 std::is_same<typename vtk::detail::can_map_component_trait<U>::rtype, ValueType>::value,
447 "Component return type should be the same as the return type of the mapComponent");
448 return this->Backend->mapComponent(idx, comp);
449 }
451
453
456 template <typename U>
457 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_direct_read_component,
458 ValueType>::type
459 GetTypedComponentImpl(vtkIdType idx, int comp) const
460 {
461 return this->GetValue(idx * this->NumberOfComponents + comp);
462 }
464
466
469 template <typename U>
470 typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_get_memory_size,
471 unsigned long>::type
472 GetActualMemorySizeImpl() const
473 {
474 return this->Backend->getMemorySize();
475 }
476
481 template <typename U>
482 typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_get_memory_size,
483 unsigned long>::type
484 GetActualMemorySizeImpl() const
485 {
486 return 1;
487 }
489
490 friend class vtkGenericDataArray<SelfType, ValueType, ArrayTypeTag::value>;
491};
492
493// Declare vtkArrayDownCast implementations for implicit containers:
495
496VTK_ABI_NAMESPACE_END
497
498// This macro is used by the subclasses to create dummy
499// declarations for these functions such that the wrapper
500// can see them. The wrappers ignore vtkImplicitArray.
501#define vtkCreateImplicitWrappedArrayInterface(T) \
502 vtkCreateWrappedArrayReadInterface(T); \
503 bool SetNumberOfValues(vtkIdType number) override;
504
505#include "vtkImplicitArray.txx"
506
507#endif // vtkImplicitArray_h
508
509// 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.
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!
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.
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
int vtkIdType
Definition vtkType.h:363