Hello all,
How to add two vtkDataArray objects of same type in vtk python?
Is there any vtk-class to do this?
Thanks.
Hello,
Add to what?
best,
PC
If you mean da1 + da2
, this isn’t possible (and is unlikely). I’m sure there’s something though.
You can use vtk.util.numpy_support
import vtk
da1 = vtk.vtkDoubleArray()
da1.number_of_tuples = 4
da1.Fill(1)
da2 = vtk.vtkDoubleArray()
da2.number_of_tuples = 4
da2.Fill(2)
from vtk.util.numpy_support import numpy_to_vtk as n2v
from vtk.util.numpy_support import vtk_to_numpy as v2n
da3 = n2v(v2n(da1) + v2n(da2))
for i in range(da3.GetNumberOfValues()):
print(da1.GetValue(i), '+', da2.GetValue(i), '=', da3.GetValue(i))
Ah, element-wise addition? I was thinking of concatenating the data arrays together (like [1] + [2]
in Python).
Ah. I thought they meant element-wise addition. Any case, you can concatenate with numpy.
da4 = n2v(np.concatenate((v2n(da1), v2n(da2))))
print(da4.number_of_tuples) # prints 8
It worked exactly as I wanted
Thanks a lot.