从 vtkActor 更新 vtkPolyData
Updating vtkPolyData from vtkActor
我是 VTK 的新手。我正在尝试在 python 中使用 VTK 来使用 vtkBoxWidget
编辑 .stl
文件。我正在读取 .stl
文件,通过 window 交互器中的 mouse/drag 使用 vtkBoxWidget
对其进行编辑,最后我想将新数据写入新 .stl
按下某个键时。
下面是演示问题的代码:
#!/usr/bin/env python
import vtk
import sys
import os
filename = "file.stl"
# render the data
reader = vtk.vtkSTLReader()
reader.SetFileName(filename)
stlMapper = vtk.vtkPolyDataMapper()
stlMapper.SetInputConnection(reader.GetOutputPort())
# create an actor for our scene
stlActor = vtk.vtkActor()
stlActor.SetMapper(stlMapper)
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# The box widget observes the events invoked by the render window
# interactor. These events come from user interaction in the render
# window.
boxWidget = vtk.vtkBoxWidget()
boxWidget.SetInteractor(iren)
boxWidget.SetPlaceFactor(1.25)
# Add the actors to the renderer, set the background and window size.
ren.AddActor(stlActor)
ren.SetBackground(0.1, 0.2, 0.4)
renWin.SetSize(300, 300)
# As the box widget is interacted with, it produces a transformation
# matrix that is set on the actor.
t = vtk.vtkTransform()
def TransformActor(obj, event):
global t, stlActor
obj.GetTransform(t)
stlActor.SetUserTransform(t)
def onKeyPressEvent(self, renderer):
key = self.GetKeyCode()
if(key=='l'):
print stlActor.GetBounds()
if(os.path.isfile("done.stl")==1):
os.remove("done.stl")
print stlActor.GetMapper().GetInput().GetBounds()
stlPolyData = stlActor.GetMapper().GetInput()
writer = vtk.vtkSTLWriter()
writer.SetFileName("done.stl")
writer.AddInputDataObject(stlPolyData)
writer.Write()
# Place the interactor initially. The actor is used to place and scale
# the interactor. An observer is added to the box widget to watch for
# interaction events. This event is captured and used to set the
# transformation matrix of the actor.
boxWidget.SetProp3D(stlActor)
boxWidget.PlaceWidget()
boxWidget.AddObserver("InteractionEvent", TransformActor)
iren.AddObserver('KeyPressEvent', onKeyPressEvent)
iren.Initialize()
renWin.Render()
iren.Start()
问题是我不知道如何更新 vtkPolyData
或将我应用于 vtkActor
的转换传递给 vtkPolyData
。在上面的代码中,我在两个打印函数中使用 .GetBounds() 对此进行了测试。最初对象有边界:
(-2.0, 2.0, 0.0, 1.625, -1.5, 1.8125)
更新后,actor Bounds 改变了,但我无法将所有这些属性传递给 vtkPolyData。
(-2.1331110248939638, 2.246371570191821, -1.5743578447908013, 2.5817210301577385, -2.1311284932847796, 2.0695034726591715)
(-2.0, 2.0, 0.0, 1.625, -1.5, 1.8125)
谢谢!
我想我明白了:
def transformPolyData(actor):
polyData = vtk.vtkPolyData()
polyData.DeepCopy(actor.GetMapper().GetInput())
transform = vtk.vtkTransform()
transform.SetMatrix(actor.GetMatrix())
fil = vtk.vtkTransformPolyDataFilter()
fil.SetTransform(transform)
fil.SetInputDataObject(polyData)
fil.Update()
polyData.DeepCopy(fil.GetOutput())
return polyData;
这会将 polyData 转换为我在保存之前需要的内容。
我是 VTK 的新手。我正在尝试在 python 中使用 VTK 来使用 vtkBoxWidget
编辑 .stl
文件。我正在读取 .stl
文件,通过 window 交互器中的 mouse/drag 使用 vtkBoxWidget
对其进行编辑,最后我想将新数据写入新 .stl
按下某个键时。
下面是演示问题的代码:
#!/usr/bin/env python
import vtk
import sys
import os
filename = "file.stl"
# render the data
reader = vtk.vtkSTLReader()
reader.SetFileName(filename)
stlMapper = vtk.vtkPolyDataMapper()
stlMapper.SetInputConnection(reader.GetOutputPort())
# create an actor for our scene
stlActor = vtk.vtkActor()
stlActor.SetMapper(stlMapper)
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# The box widget observes the events invoked by the render window
# interactor. These events come from user interaction in the render
# window.
boxWidget = vtk.vtkBoxWidget()
boxWidget.SetInteractor(iren)
boxWidget.SetPlaceFactor(1.25)
# Add the actors to the renderer, set the background and window size.
ren.AddActor(stlActor)
ren.SetBackground(0.1, 0.2, 0.4)
renWin.SetSize(300, 300)
# As the box widget is interacted with, it produces a transformation
# matrix that is set on the actor.
t = vtk.vtkTransform()
def TransformActor(obj, event):
global t, stlActor
obj.GetTransform(t)
stlActor.SetUserTransform(t)
def onKeyPressEvent(self, renderer):
key = self.GetKeyCode()
if(key=='l'):
print stlActor.GetBounds()
if(os.path.isfile("done.stl")==1):
os.remove("done.stl")
print stlActor.GetMapper().GetInput().GetBounds()
stlPolyData = stlActor.GetMapper().GetInput()
writer = vtk.vtkSTLWriter()
writer.SetFileName("done.stl")
writer.AddInputDataObject(stlPolyData)
writer.Write()
# Place the interactor initially. The actor is used to place and scale
# the interactor. An observer is added to the box widget to watch for
# interaction events. This event is captured and used to set the
# transformation matrix of the actor.
boxWidget.SetProp3D(stlActor)
boxWidget.PlaceWidget()
boxWidget.AddObserver("InteractionEvent", TransformActor)
iren.AddObserver('KeyPressEvent', onKeyPressEvent)
iren.Initialize()
renWin.Render()
iren.Start()
问题是我不知道如何更新 vtkPolyData
或将我应用于 vtkActor
的转换传递给 vtkPolyData
。在上面的代码中,我在两个打印函数中使用 .GetBounds() 对此进行了测试。最初对象有边界:
(-2.0, 2.0, 0.0, 1.625, -1.5, 1.8125)
更新后,actor Bounds 改变了,但我无法将所有这些属性传递给 vtkPolyData。
(-2.1331110248939638, 2.246371570191821, -1.5743578447908013, 2.5817210301577385, -2.1311284932847796, 2.0695034726591715)
(-2.0, 2.0, 0.0, 1.625, -1.5, 1.8125)
谢谢!
我想我明白了:
def transformPolyData(actor):
polyData = vtk.vtkPolyData()
polyData.DeepCopy(actor.GetMapper().GetInput())
transform = vtk.vtkTransform()
transform.SetMatrix(actor.GetMatrix())
fil = vtk.vtkTransformPolyDataFilter()
fil.SetTransform(transform)
fil.SetInputDataObject(polyData)
fil.Update()
polyData.DeepCopy(fil.GetOutput())
return polyData;
这会将 polyData 转换为我在保存之前需要的内容。