VTK  9.5.20250810
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 viskores setup
11
14
15#include "vtkLogger.h"
16
17#include <viskores/cont/ArrayExtractComponent.h>
18#include <viskores/cont/ArrayHandleBasic.h>
19#include <viskores/cont/ArrayHandleRecombineVec.h>
20#include <viskores/cont/ArrayHandleRuntimeVec.h>
21#include <viskores/cont/ArrayHandleSOA.h>
22#include <viskores/cont/ArrayHandleStride.h>
23#include <viskores/cont/Field.h>
24#include <viskores/cont/UnknownArrayHandle.h>
25
26#include <type_traits> // for std::underlying_type
27#include <utility> // for std::pair
28
29namespace viskores
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>
56viskores::cont::ArrayHandleBasic<T> vtkAOSDataArrayToFlatArrayHandle(
58{
59 // Register a reference to the input here to make sure the array cannot
60 // be deleted before the `ArrayHandle` is done with it. (Note that you
61 // will still get problems if the `vtkAOSDataArrayTemplate` gets resized.
62 input->Register(nullptr);
63
64 auto deleter = [](void* container)
65 {
66 vtkAOSDataArrayTemplate<T>* vtkArray = reinterpret_cast<vtkAOSDataArrayTemplate<T>*>(container);
67 vtkArray->UnRegister(nullptr);
68 };
69 auto reallocator = [](void*& memory, void*& container, viskores::BufferSizeType oldSize,
70 viskores::BufferSizeType newSize)
71 {
72 vtkAOSDataArrayTemplate<T>* vtkArray = reinterpret_cast<vtkAOSDataArrayTemplate<T>*>(container);
73 if ((vtkArray->GetVoidPointer(0) != memory) || (vtkArray->GetNumberOfValues() != oldSize))
74 {
75 vtkLog(ERROR,
76 "Dangerous inconsistency found between pointers for VTK and Viskores. "
77 "Was the VTK array resized outside of Viskores?");
78 }
79 vtkArray->SetNumberOfValues(newSize);
80 memory = vtkArray->GetVoidPointer(0);
81 };
82
83 return viskores::cont::ArrayHandleBasic<T>(
84 input->GetPointer(0), input, input->GetNumberOfValues(), deleter, reallocator);
85}
86
87template <typename T>
88viskores::cont::ArrayHandleBasic<T> vtkSOADataArrayToComponentArrayHandle(
89 vtkSOADataArrayTemplate<T>* input, int componentIndex)
90{
91 // Register for each component (as each will have the deleter call to
92 // unregister).
93 input->Register(nullptr);
94
95 using ContainerPair = std::pair<vtkSOADataArrayTemplate<T>*, int>;
96 ContainerPair* componentInput = new ContainerPair(input, componentIndex);
97
98 auto deleter = [](void* container)
99 {
100 ContainerPair* containerPair = reinterpret_cast<ContainerPair*>(container);
101 containerPair->first->UnRegister(nullptr);
102 delete containerPair;
103 };
104 auto reallocator = [](void*& memory, void*& container,
105 viskores::BufferSizeType vtkNotUsed(oldSize),
106 viskores::BufferSizeType newSize)
107 {
108 ContainerPair* containerPair = reinterpret_cast<ContainerPair*>(container);
109 containerPair->first->SetNumberOfTuples(newSize);
110 memory = containerPair->first->GetComponentArrayPointer(containerPair->second);
111 };
112
113 return viskores::cont::ArrayHandleBasic<T>(input->GetComponentArrayPointer(componentIndex),
114 componentInput, input->GetNumberOfTuples(), deleter, reallocator);
115}
116
117template <typename T>
118viskores::cont::ArrayHandleRuntimeVec<T> vtkDataArrayToArrayHandle(
120{
121 auto flatArray = vtkAOSDataArrayToFlatArrayHandle(input);
122 return viskores::cont::make_ArrayHandleRuntimeVec(input->GetNumberOfComponents(), flatArray);
123}
124
125template <typename T>
126viskores::cont::ArrayHandleRecombineVec<T> vtkDataArrayToArrayHandle(
128{
129 // Wrap each component array in a basic array handle, convert that to a
130 // strided array, and then add that as a component to the returned
131 // recombined vec.
132 viskores::cont::ArrayHandleRecombineVec<T> output;
133
134 for (int componentIndex = 0; componentIndex < input->GetNumberOfComponents(); ++componentIndex)
135 {
136 auto componentArray = vtkSOADataArrayToComponentArrayHandle(input, componentIndex);
137 output.AppendComponentArray(
138 viskores::cont::ArrayExtractComponent(componentArray, 0, viskores::CopyFlag::Off));
139 }
140
141 return output;
142}
143
144template <typename DataArrayType>
145viskores::cont::UnknownArrayHandle vtkDataArrayToUnknownArrayHandle(DataArrayType* input)
146{
147 return vtkDataArrayToArrayHandle(input);
148}
149
150enum class FieldsFlag
151{
152 None = 0x0,
153 Points = 0x1,
154 Cells = 0x2,
155
157};
158
159VTK_ABI_NAMESPACE_END
160}
161
162namespace fromvtkm
163{
164VTK_ABI_NAMESPACE_BEGIN
165
166VTKACCELERATORSVTKMCORE_EXPORT
167vtkDataArray* Convert(const viskores::cont::Field& input);
168
169VTKACCELERATORSVTKMCORE_EXPORT
170vtkDataArray* Convert(const viskores::cont::UnknownArrayHandle& input, const std::string& name);
171
172VTKACCELERATORSVTKMCORE_EXPORT
173vtkPoints* Convert(const viskores::cont::CoordinateSystem& input);
174
175VTK_ABI_NAMESPACE_END
176}
177
178VTK_ABI_NAMESPACE_BEGIN
180{
181 using T = std::underlying_type<tovtkm::FieldsFlag>::type;
182 return static_cast<tovtkm::FieldsFlag>(static_cast<T>(a) & static_cast<T>(b));
183}
184
186{
187 using T = std::underlying_type<tovtkm::FieldsFlag>::type;
188 return static_cast<tovtkm::FieldsFlag>(static_cast<T>(a) | static_cast<T>(b));
189}
190VTK_ABI_NAMESPACE_END
191
192#endif // vtkmlib_ArrayConverters_h
193/* 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 viskores::cont::Field &input)
viskores::cont::ArrayHandleBasic< T > vtkAOSDataArrayToFlatArrayHandle(vtkAOSDataArrayTemplate< T > *input)
viskores::cont::ArrayHandleRuntimeVec< T > vtkDataArrayToArrayHandle(vtkAOSDataArrayTemplate< T > *input)
viskores::cont::UnknownArrayHandle vtkDataArrayToUnknownArrayHandle(DataArrayType *input)
static const char * NoNameVTKFieldName()
Temporary name for arrays converted from VTK that do not have a name.
viskores::cont::ArrayHandleBasic< T > vtkSOADataArrayToComponentArrayHandle(vtkSOADataArrayTemplate< T > *input, int componentIndex)
#define vtkLog(verbosity_name, x)
Add to log given the verbosity level.
Definition vtkLogger.h:513