VTK中如何获取Prop的世界坐标?
In VTK, how to get the world coordinate of a Prop?
我在 SolidWorks 中创建了一个立方体并将其导出到 STL 文件。(立方体位于原点右侧 50mm)然后我使用 VTK class vtkSTLReader 读取该文件并将其渲染到渲染 window。
screenshot
然而,当我调用函数GetPosition()时,它总是返回(0,0,0)。在我看来,它应该是(50,0,0)。 The VTK class reference说明这个功能是为了"Get the position of the Prop3D in world coordinates"。这让我很困惑。不知道问题出在哪里
import vtk
sr = vtk.vtkSTLReader()
sr.SetFileName("cube.stl")
stlMapper = vtk.vtkPolyDataMapper()
stlMapper.SetInputConnection(sr.GetOutputPort())
stlActor = vtk.vtkLODActor()
stlActor.SetMapper(stlMapper)
# Create the Renderer, RenderWindow, and RenderWindowInteractor
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Add the actors to the render; set the background and size
ren.AddActor(stlActor)
ren.SetBackground(0.1, 0.1, 0.1)
renWin.SetSize(500, 500)
print stlActor.GetPosition()
iren.Initialize()
renWin.Render()
iren.Start()
嗯,除非你改变 道具(演员) 的位置,否则它将保持在 0,0,0。这个道具代表几何体,在你的例子中是一个立方体,它的一个节点定义在 50,0,0,但这对道具的位置没有影响。事实上,没有办法将那个点(道具的位置)绑定到基础几何体的任何一个节点 - 它如何决定它是立方体的哪个节点?相反,它是一个参考点,如果它是 0,0,0,则意味着您的几何图形将在与定义几何图形的坐标完全相同的坐标处呈现(您可以通过 sr.GetOutputPort().GetPoints()
或查看您的stl文件)。基本上,GetPosition() 为您提供应用于您的点的变换矩阵的平移部分。
为了回答您的问题,正如我已经说过的,您可以从 sr.GetOutputPort().GetPoints()
获取几何体的点,假设没有应用任何变换,这些点是该几何体的世界坐标。如果您担心某些转换,您可以在演员 (Set/GetUserTransform) 上定义自己的转换或使用相机的 ModelMatrix (ren.GetActiveCamera().GetModelMatrix),然后使用 vtkTransform class 将这些变换应用到任何你想在这些变换后得到它们 "image" 的坐标。
我在 SolidWorks 中创建了一个立方体并将其导出到 STL 文件。(立方体位于原点右侧 50mm)然后我使用 VTK class vtkSTLReader 读取该文件并将其渲染到渲染 window。
screenshot
然而,当我调用函数GetPosition()时,它总是返回(0,0,0)。在我看来,它应该是(50,0,0)。 The VTK class reference说明这个功能是为了"Get the position of the Prop3D in world coordinates"。这让我很困惑。不知道问题出在哪里
import vtk
sr = vtk.vtkSTLReader()
sr.SetFileName("cube.stl")
stlMapper = vtk.vtkPolyDataMapper()
stlMapper.SetInputConnection(sr.GetOutputPort())
stlActor = vtk.vtkLODActor()
stlActor.SetMapper(stlMapper)
# Create the Renderer, RenderWindow, and RenderWindowInteractor
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Add the actors to the render; set the background and size
ren.AddActor(stlActor)
ren.SetBackground(0.1, 0.1, 0.1)
renWin.SetSize(500, 500)
print stlActor.GetPosition()
iren.Initialize()
renWin.Render()
iren.Start()
嗯,除非你改变 道具(演员) 的位置,否则它将保持在 0,0,0。这个道具代表几何体,在你的例子中是一个立方体,它的一个节点定义在 50,0,0,但这对道具的位置没有影响。事实上,没有办法将那个点(道具的位置)绑定到基础几何体的任何一个节点 - 它如何决定它是立方体的哪个节点?相反,它是一个参考点,如果它是 0,0,0,则意味着您的几何图形将在与定义几何图形的坐标完全相同的坐标处呈现(您可以通过 sr.GetOutputPort().GetPoints()
或查看您的stl文件)。基本上,GetPosition() 为您提供应用于您的点的变换矩阵的平移部分。
为了回答您的问题,正如我已经说过的,您可以从 sr.GetOutputPort().GetPoints()
获取几何体的点,假设没有应用任何变换,这些点是该几何体的世界坐标。如果您担心某些转换,您可以在演员 (Set/GetUserTransform) 上定义自己的转换或使用相机的 ModelMatrix (ren.GetActiveCamera().GetModelMatrix),然后使用 vtkTransform class 将这些变换应用到任何你想在这些变换后得到它们 "image" 的坐标。