在大型数据集中单独翻译所有VTK字形

时间:2017-11-30 14:12:08

标签: python vtk

我在2d网格点上成像单位3矢量数据。向量是笛卡儿。我希望将这些矢量显示为以箭头总长度的中点为中心的3d箭头。我的想法是创建另一个翻译矩阵,等于 - (vector / 2),然后移动字形(或更改每个矢量原点或任何可行的)!因此,我想将这些新的矢量逐点应用为字形的翻译。我失败了,需要帮助。

翻译需要保留向量的大小。是否有其他方法可以做到这一点。我注意到Mayavi2可以使用复选按钮执行此操作,但没有我需要的其他功能。

我应该提一下,我本周才开始使用Python和VTK,所以请在代码中指出任何白痴。工作代码:

import vtk
import math
import sys #Just to break flow

# The source file
file_name = "Stable.vts"

# Read the source file.
reader = vtk.vtkXMLStructuredGridReader()
reader.SetFileName(file_name)
reader.Update()
output = reader.GetOutput()
numPoints = output.GetNumberOfPoints()
dimensions = output.GetDimensions()

#Tuple to hold spherical polar coords
mpolar = vtk.vtkFloatArray()
mpolar.SetNumberOfTuples(numPoints)
mpolar.SetNumberOfComponents(3)
mpolar.SetNumberOfValues(3*numPoints)
mpolar.SetName("mpolar")

#Tuple to hold translations
mtrans = vtk.vtkFloatArray()
mtrans.SetNumberOfTuples(numPoints)
mtrans.SetNumberOfComponents(3)
mtrans.SetNumberOfValues(3*numPoints)
mtrans.SetName("mtrans")

#Tuple copy of cartesian data
mxyz = vtk.vtkFloatArray()
mxyz = output.GetPointData().GetAbstractArray(0)
mxyz.SetName("mxyz")

#Loop through input data and convert to spherical polar
#Also set points equal to input points
#Also create vector transform for each vector
for x in range(numPoints):
    azi = math.atan2(output.GetPointData().GetAbstractArray(0).GetTuple(x)[0],output.GetPointData().GetAbstractArray(0).GetTuple(x)[1])
    pol = math.acos(output.GetPointData().GetAbstractArray(0).GetTuple(x)[2])
    rad = 1
    mpolar.SetTuple3(x, azi, pol, rad)
    mtrans.SetTuple3(x, -output.GetPointData().GetAbstractArray(0).GetTuple(x)[0]/2, \
        -output.GetPointData().GetAbstractArray(0).GetTuple(x)[1]/2, \
        -output.GetPointData().GetAbstractArray(0).GetTuple(x)[2]/2)

#Define new structured grid
skrGrid = vtk.vtkStructuredGrid()
skrGrid.SetDimensions(dimensions)
skrGrid.SetPoints(output.GetPoints())
skrGrid.GetPointData().AddArray(mpolar)
skrGrid.GetPointData().AddArray(mxyz)
skrGrid.GetPointData().AddArray(mtrans)

#Write new structured grid
#writer = vtk.vtkXMLStructuredGridWriter()
#writer.SetFileName("skrGrid.vts")
#writer.SetInputData(skrGrid)
#writer.Write()

#Subsample grid
subs = 11
extract = vtk.vtkExtractGrid()
extract.SetInputData(skrGrid)
extract.SetSampleRate(subs, subs, 1)
extract.Update()
#extract.IncludeBoundaryOn() #Get boundary even if not sampled

#Cast data into PolyData format for Glyphs
pd = vtk.vtkPolyData()
pd.SetPoints(extract.GetOutput().GetPoints())
pd.GetPointData().SetVectors(extract.GetOutput().GetPointData().GetAbstractArray(1))

arrowSource = vtk.vtkArrowSource()

glyph3D = vtk.vtkGlyph3D()
glyph3D.SetSourceConnection(arrowSource.GetOutputPort())
glyph3D.SetVectorModeToUseVector()
glyph3D.SetInputData(pd)
glyph3D.SetScaleFactor(1e-07)
glyph3D.Update()

# Create a mapper and actor
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(glyph3D.GetOutputPort())

actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Visualize
camera = vtk.vtkCamera()
camera.SetPosition(5e-07, -15e-07, 1e-06)
camera.SetFocalPoint(5e-07, 5e-07, 0)

# Create a renderer, render window, and interactor
renderer = vtk.vtkRenderer()
renderer.SetActiveCamera(camera)

renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

# Add the actor to the scene
renderer.AddActor(actor)
renderer.SetBackground(1, 1, 1)  # Background color white
renderer.ResetCameraClippingRange() # Call because without it you need window input to make render display!

# enable user interface interactor
# Render and interact
renderWindow.SetSize(800,600)
renderWindowInteractor.Initialize()
renderWindow.Render()
renderWindowInteractor.Start()     

1 个答案:

答案 0 :(得分:0)

这里需要的是Warp Vector。由于位移已经计算为mtrans,因此可以将这些位移应用于点集,以便将其扭曲到新配置。点集是字形的原点。下面是子样本网格下面的一段代码。

#Cast data into PolyData format for warping
pd = vtk.vtkPolyData()
pd.SetPoints(extract.GetOutput().GetPoints())
pd.GetPointData().SetVectors(extract.GetOutput().GetPointData().GetAbstractArray(2))

#Warp vector origins
warpVector = vtk.vtkWarpVector()
warpVector.SetInputData(pd)
warpVector.SetScaleFactor(1e-07)
warpVector.Update()

#Cast warped points and original vectors into PolyData for rendering
warped = vtk.vtkPolyData()
warped.SetPoints(warpVector.GetOutput().GetPoints())
warped.GetPointData().SetVectors(extract.GetOutput().GetPointData().GetAbstractArray(1))

arrowSource = vtk.vtkArrowSource()

glyph3D = vtk.vtkGlyph3D()
glyph3D.SetSourceConnection(arrowSource.GetOutputPort())
glyph3D.SetVectorModeToUseVector()
glyph3D.SetInputData(warped)
glyph3D.SetScaleFactor(1e-07)
glyph3D.Update()