File: Matlab_engine_interface.py

package info (click to toggle)
paraview 5.0.0%2Bdfsg1-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 225,112 kB
  • sloc: cpp: 2,432,495; ansic: 204,338; python: 97,967; xml: 79,887; tcl: 46,936; fortran: 26,923; yacc: 4,926; java: 4,413; perl: 3,108; sh: 2,775; lex: 1,934; f90: 748; asm: 471; pascal: 228; makefile: 192; objc: 17
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))