File: Matlab_engine_interface.py

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (39 lines) | stat: -rw-r--r-- 1,277 bytes parent folder | download | duplicates (5)
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))