Sophie

Sophie

distrib > Mageia > 5 > i586 > by-pkgid > 82ac505190c212a37e5a9f824939c992 > files > 447

vtk-examples-6.0.0-8.mga5.i586.rpm


# 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 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)