VTK  9.4.20250130
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
146enum class FieldsFlag
147{
148 None = 0x0,
149 Points = 0x1,
150 Cells = 0x2,
151
153};
154
155VTK_ABI_NAMESPACE_END
156}
157
158namespace fromvtkm
159{
160VTK_ABI_NAMESPACE_BEGIN
161
162VTKACCELERATORSVTKMCORE_EXPORT
163vtkDataArray* Convert(const vtkm::cont::Field& input);
164
165VTKACCELERATORSVTKMCORE_EXPORT
166vtkDataArray* Convert(const vtkm::cont::UnknownArrayHandle& input, const std::string& name);
167
168VTKACCELERATORSVTKMCORE_EXPORT
169vtkPoints* Convert(const vtkm::cont::CoordinateSystem& input);
170
171VTK_ABI_NAMESPACE_END
172}
173
174VTK_ABI_NAMESPACE_BEGIN
176{
177 using T = std::underlying_type<tovtkm::FieldsFlag>::type;
178 return static_cast<tovtkm::FieldsFlag>(static_cast<T>(a) & static_cast<T>(b));
179}
180
182{
183 using T = std::underlying_type<tovtkm::FieldsFlag>::type;
184 return static_cast<tovtkm::FieldsFlag>(static_cast<T>(a) | static_cast<T>(b));
185}
186VTK_ABI_NAMESPACE_END
187
188#endif // vtkmlib_ArrayConverters_h
189/* 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.
#define vtkLog(verbosity_name, x)
Add to log given the verbosity level.
Definition vtkLogger.h:513