VTK  9.4.20241117
DataArrayConverters.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-FileCopyrightText: Copyright (c) Kitware, Inc.
3// SPDX-FileCopyrightText: Copyright 2012 Sandia Corporation.
4// SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-USGov
5
6#ifndef vtkmlib_DataArrayConverters_h
7#define vtkmlib_DataArrayConverters_h
8
9#include "vtkAcceleratorsVTKmCoreModule.h" //required for correct implementation
10#include "vtkmConfigCore.h" //required for general vtkm setup
11
14
15#include "vtkLogger.h"
16
17#include <vtkm/cont/ArrayExtractComponent.h>
18#include <vtkm/cont/ArrayHandleBasic.h>
19#include <vtkm/cont/ArrayHandleRecombineVec.h>
20#include <vtkm/cont/ArrayHandleRuntimeVec.h>
21#include <vtkm/cont/ArrayHandleSOA.h>
22#include <vtkm/cont/ArrayHandleStride.h>
23#include <vtkm/cont/Field.h>
24#include <vtkm/cont/UnknownArrayHandle.h>
25
26#include <type_traits> // for std::underlying_type
27#include <utility> // for std::pair
28
29namespace vtkm
30{
31namespace cont
32{
33class CoordinateSystem;
34}
35}
36
37VTK_ABI_NAMESPACE_BEGIN
38class vtkDataArray;
39class vtkPoints;
40VTK_ABI_NAMESPACE_END
41
42namespace tovtkm
43{
44VTK_ABI_NAMESPACE_BEGIN
45
49inline static const char* NoNameVTKFieldName()
50{
51 static const char* name = "NoNameVTKField";
52 return name;
53}
54
55template <typename T>
56vtkm::cont::ArrayHandleBasic<T> vtkAOSDataArrayToFlatArrayHandle(vtkAOSDataArrayTemplate<T>* input)
57{
58 // Register a reference to the input here to make sure the array cannot
59 // be deleted before the `ArrayHandle` is done with it. (Note that you
60 // will still get problems if the `vtkAOSDataArrayTemplate` gets resized.
61 input->Register(nullptr);
62
63 auto deleter = [](void* container)
64 {
65 vtkAOSDataArrayTemplate<T>* vtkArray = reinterpret_cast<vtkAOSDataArrayTemplate<T>*>(container);
66 vtkArray->UnRegister(nullptr);
67 };
68 auto reallocator =
69 [](void*& memory, void*& container, vtkm::BufferSizeType oldSize, vtkm::BufferSizeType newSize)
70 {
71 vtkAOSDataArrayTemplate<T>* vtkArray = reinterpret_cast<vtkAOSDataArrayTemplate<T>*>(container);
72 if ((vtkArray->GetVoidPointer(0) != memory) || (vtkArray->GetNumberOfValues() != oldSize))
73 {
74 vtkLog(ERROR,
75 "Dangerous inconsistency found between pointers for VTK and VTK-m. "
76 "Was the VTK array resized outside of VTK-m?");
77 }
78 vtkArray->SetNumberOfValues(newSize);
79 memory = vtkArray->GetVoidPointer(0);
80 };
81
82 return vtkm::cont::ArrayHandleBasic<T>(
83 input->GetPointer(0), input, input->GetNumberOfValues(), deleter, reallocator);
84}
85
86template <typename T>
87vtkm::cont::ArrayHandleBasic<T> vtkSOADataArrayToComponentArrayHandle(
88 vtkSOADataArrayTemplate<T>* input, int componentIndex)
89{
90 // Register for each component (as each will have the deleter call to
91 // unregister).
92 input->Register(nullptr);
93
94 using ContainerPair = std::pair<vtkSOADataArrayTemplate<T>*, int>;
95 ContainerPair* componentInput = new ContainerPair(input, componentIndex);
96
97 auto deleter = [](void* container)
98 {
99 ContainerPair* containerPair = reinterpret_cast<ContainerPair*>(container);
100 containerPair->first->UnRegister(nullptr);
101 delete containerPair;
102 };
103 auto reallocator = [](void*& memory, void*& container, vtkm::BufferSizeType vtkNotUsed(oldSize),
104 vtkm::BufferSizeType newSize)
105 {
106 ContainerPair* containerPair = reinterpret_cast<ContainerPair*>(container);
107 containerPair->first->SetNumberOfTuples(newSize);
108 memory = containerPair->first->GetComponentArrayPointer(containerPair->second);
109 };
110
111 return vtkm::cont::ArrayHandleBasic<T>(input->GetComponentArrayPointer(componentIndex),
112 componentInput, input->GetNumberOfTuples(), deleter, reallocator);
113}
114
115template <typename T>
116vtkm::cont::ArrayHandleRuntimeVec<T> vtkDataArrayToArrayHandle(vtkAOSDataArrayTemplate<T>* input)
117{
118 auto flatArray = vtkAOSDataArrayToFlatArrayHandle(input);
119 return vtkm::cont::make_ArrayHandleRuntimeVec(input->GetNumberOfComponents(), flatArray);
120}
121
122template <typename T>
123vtkm::cont::ArrayHandleRecombineVec<T> vtkDataArrayToArrayHandle(vtkSOADataArrayTemplate<T>* input)
124{
125 // Wrap each component array in a basic array handle, convert that to a
126 // strided array, and then add that as a component to the returned
127 // recombined vec.
128 vtkm::cont::ArrayHandleRecombineVec<T> output;
129
130 for (int componentIndex = 0; componentIndex < input->GetNumberOfComponents(); ++componentIndex)
131 {
132 auto componentArray = vtkSOADataArrayToComponentArrayHandle(input, componentIndex);
133 output.AppendComponentArray(
134 vtkm::cont::ArrayExtractComponent(componentArray, 0, vtkm::CopyFlag::Off));
135 }
136
137 return output;
138}
139
140template <typename DataArrayType>
141vtkm::cont::UnknownArrayHandle vtkDataArrayToUnknownArrayHandle(DataArrayType* input)
142{
143 return vtkDataArrayToArrayHandle(input);
144}
145
146template <typename DataArrayType, vtkm::IdComponent NumComponents>
148
149template <typename T, vtkm::IdComponent NumComponents>
151{
152 using ValueType =
153 typename std::conditional<NumComponents == 1, T, vtkm::Vec<T, NumComponents>>::type;
154 using StorageType = vtkm::cont::internal::Storage<ValueType, vtkm::cont::StorageTagBasic>;
155 using ArrayHandleType = vtkm::cont::ArrayHandle<ValueType, vtkm::cont::StorageTagBasic>;
156
157 VTK_DEPRECATED_IN_9_3_0("Use vtkDataArrayToArrayHandle or vtkAOSDataArrayToFlatArrayHandle.")
159 {
160 return vtkm::cont::make_ArrayHandle(reinterpret_cast<ValueType*>(input->GetPointer(0)),
161 input->GetNumberOfTuples(), vtkm::CopyFlag::Off);
162 }
163};
164
165template <typename T, vtkm::IdComponent NumComponents>
167{
168 using ValueType = vtkm::Vec<T, NumComponents>;
169 using StorageType = vtkm::cont::internal::Storage<ValueType, vtkm::cont::StorageTagSOA>;
170 using ArrayHandleType = vtkm::cont::ArrayHandle<ValueType, vtkm::cont::StorageTagSOA>;
171
172 VTK_DEPRECATED_IN_9_3_0("Use vtkDataArrayToArrayHandle or vtkSOADataArrayToComponentArrayHandle.")
174 {
175 vtkm::Id numValues = input->GetNumberOfTuples();
176 vtkm::cont::ArrayHandleSOA<ValueType> handle;
177 for (vtkm::IdComponent i = 0; i < NumComponents; ++i)
178 {
179 handle.SetArray(i,
180 vtkm::cont::make_ArrayHandle<T>(reinterpret_cast<T*>(input->GetComponentArrayPointer(i)),
181 numValues, vtkm::CopyFlag::Off));
182 }
183
184 return std::move(handle);
185 }
186};
187
188template <typename T>
190{
191 using StorageType = vtkm::cont::internal::Storage<T, vtkm::cont::StorageTagBasic>;
192 using ArrayHandleType = vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagBasic>;
193
194 VTK_DEPRECATED_IN_9_3_0("Use vtkDataArrayToArrayHandle or vtkSOADataArrayToComponentArrayHandle.")
196 {
197 return vtkm::cont::make_ArrayHandle(
198 input->GetComponentArrayPointer(0), input->GetNumberOfTuples(), vtkm::CopyFlag::Off);
199 }
200};
201
202enum class FieldsFlag
203{
204 None = 0x0,
205 Points = 0x1,
206 Cells = 0x2,
207
209};
210
211VTK_ABI_NAMESPACE_END
212}
213
214namespace fromvtkm
215{
216VTK_ABI_NAMESPACE_BEGIN
217
218VTKACCELERATORSVTKMCORE_EXPORT
219vtkDataArray* Convert(const vtkm::cont::Field& input);
220
221VTKACCELERATORSVTKMCORE_EXPORT
222vtkDataArray* Convert(const vtkm::cont::UnknownArrayHandle& input, const std::string& name);
223
224VTKACCELERATORSVTKMCORE_EXPORT
225vtkPoints* Convert(const vtkm::cont::CoordinateSystem& input);
226
227VTK_ABI_NAMESPACE_END
228}
229
230VTK_ABI_NAMESPACE_BEGIN
232{
233 using T = std::underlying_type<tovtkm::FieldsFlag>::type;
234 return static_cast<tovtkm::FieldsFlag>(static_cast<T>(a) & static_cast<T>(b));
235}
236
238{
239 using T = std::underlying_type<tovtkm::FieldsFlag>::type;
240 return static_cast<tovtkm::FieldsFlag>(static_cast<T>(a) | static_cast<T>(b));
241}
242VTK_ABI_NAMESPACE_END
243
244#endif // vtkmlib_ArrayConverters_h
245/* VTK-HeaderTest-Exclude: DataArrayConverters.h */
tovtkm::FieldsFlag operator&(const tovtkm::FieldsFlag &a, const tovtkm::FieldsFlag &b)
tovtkm::FieldsFlag operator|(const tovtkm::FieldsFlag &a, const tovtkm::FieldsFlag &b)
Array-Of-Structs implementation of vtkGenericDataArray.
ValueType * GetPointer(vtkIdType valueIdx)
Get the address of a particular data index.
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.
vtkIdType GetNumberOfValues() const
Get the total number of values in the array.
Abstract interface for N-dimensional arrays.
Definition vtkArray.h:52
abstract superclass for arrays of numeric data
virtual void UnRegister(vtkObjectBase *o)
Decrease the reference count (release by another object).
void Register(vtkObjectBase *o)
Increase the reference count (mark as used by another object).
represent and manipulate 3D points
Definition vtkPoints.h:139
Struct-Of-Arrays implementation of vtkGenericDataArray.
ValueType * GetComponentArrayPointer(int comp)
Return a pointer to a contiguous block of memory containing all values for a particular components (i...
VTKACCELERATORSVTKMCORE_EXPORT vtkDataArray * Convert(const vtkm::cont::Field &input)
vtkm::cont::ArrayHandleRuntimeVec< T > vtkDataArrayToArrayHandle(vtkAOSDataArrayTemplate< T > *input)
vtkm::cont::UnknownArrayHandle vtkDataArrayToUnknownArrayHandle(DataArrayType *input)
vtkm::cont::ArrayHandleBasic< T > vtkAOSDataArrayToFlatArrayHandle(vtkAOSDataArrayTemplate< T > *input)
vtkm::cont::ArrayHandleBasic< T > vtkSOADataArrayToComponentArrayHandle(vtkSOADataArrayTemplate< T > *input, int componentIndex)
static const char * NoNameVTKFieldName()
Temporary name for arrays converted from VTK that do not have a name.
typename std::conditional< NumComponents==1, T, vtkm::Vec< T, NumComponents > >::type ValueType
vtkm::cont::ArrayHandle< ValueType, vtkm::cont::StorageTagBasic > ArrayHandleType
vtkm::cont::internal::Storage< ValueType, vtkm::cont::StorageTagBasic > StorageType
vtkm::cont::ArrayHandle< T, vtkm::cont::StorageTagBasic > ArrayHandleType
vtkm::cont::internal::Storage< T, vtkm::cont::StorageTagBasic > StorageType
vtkm::cont::internal::Storage< ValueType, vtkm::cont::StorageTagSOA > StorageType
vtkm::cont::ArrayHandle< ValueType, vtkm::cont::StorageTagSOA > ArrayHandleType
#define VTK_DEPRECATED_IN_9_3_0(reason)
#define vtkLog(verbosity_name, x)
Add to log given the verbosity level.
Definition vtkLogger.h:513