1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#!/usr/bin/env python
# Python example script that uses the vtkMatlabEngineInterface to perform
# a calculation (sin(x)^2 + cos(x)^2 = 1) on VTK array data, and pass the
# result back to VTK.
# VTK must be built with VTK_USE_MATLAB_MEX turned on for this example to work!
from __future__ import print_function
from vtk import *
import math
if __name__ == "__main__":
# Create an instance of the Matlab Engine. Note, menginterface is not a VTK pipeline object.
menginterface = vtkMatlabEngineInterface()
# Create two arrays of doubles in VTK. y contains sin(x)^2
x = vtkDoubleArray()
y = vtkDoubleArray()
for d in range(0, 100):
x.InsertNextValue(d);
y.InsertNextValue(math.sin(d)**2)
# Copy the x and y to Matlab with the same variable names
menginterface.PutVtkDataArray("x", x)
menginterface.PutVtkDataArray("y", y)
# Calculate cos(x)^2 + sin(x)^2 = 1 in Matlab.
menginterface.EvalString("y = y + cos(x).^2")
# Copy y back to VTK as variable result
result = menginterface.GetVtkDataArray("y")
# Display contents of result, should be all ones.
print("\n\nContents of result array copied to VTK from Matlab\n\n")
for i in range(result.GetNumberOfTuples()):
t = result.GetTuple1(i)
print('result[%d] = %6.4f' % (i,t))
|