在 VTK 中可视化 networkx 图但没有渲染?
Visualizing networkx graph in VTK but nothing is rendering?
我在尝试 运行 这段代码时没有收到任何错误,但是没有呈现任何内容,只出现一个空白屏幕。请让我知道我哪里出错了。 node_pos 是一个字典,所有节点坐标都以节点号为键,G 是 networkx 图对象 G。此代码改编自 2005 年在其他地方找到的代码,因此不得不更新一些 VTK 属性,因为它们已经过时了。
def draw_nxvtk(G, node_pos):
# set node positions
np={}
for n in G.nodes():
np[n]=node_pos[n]
nodePoints = vtk.vtkPoints()
i=0
for (x,y,z) in np.values():
nodePoints.InsertPoint(i, x, y, z)
i=i+1
# Create a polydata to be glyphed.
inputData = vtk.vtkPolyData()
inputData.SetPoints(nodePoints)
# Use sphere as glyph source.
balls = vtk.vtkSphereSource()
balls.SetRadius(.05)
balls.SetPhiResolution(20)
balls.SetThetaResolution(20)
glyphPoints = vtk.vtkGlyph3D()
glyphPoints.SetInputData(inputData)
glyphPoints.SetSourceData(balls.GetOutput())
glyphMapper = vtk.vtkPolyDataMapper()
glyphMapper.SetInputData(glyphPoints.GetOutput())
glyph = vtk.vtkActor()
glyph.SetMapper(glyphMapper)
glyph.GetProperty().SetDiffuseColor(plum)
glyph.GetProperty().SetSpecular(.3)
glyph.GetProperty().SetSpecularPower(30)
# Generate the polyline for the spline.
points = vtk.vtkPoints()
edgeData = vtk.vtkPolyData()
# Edges
lines = vtk.vtkCellArray()
i=0
for e in G.edges:
u=e[0]
v=e[1]
lines.InsertNextCell(2)
for n in (u,v):
(x,y,z)=node_pos[n]
points.InsertPoint(i, x, y, z)
lines.InsertCellPoint(i)
i=i+1
edgeData.SetPoints(points)
edgeData.SetLines(lines)
Tubes = vtk.vtkTubeFilter()
Tubes.SetNumberOfSides(16)
Tubes.SetInputData(edgeData)
Tubes.SetRadius(.01)
profileMapper = vtk.vtkPolyDataMapper()
profileMapper.SetInputData(Tubes.GetOutput())
profile = vtk.vtkActor()
profile.SetMapper(profileMapper)
profile.GetProperty().SetDiffuseColor(banana)
profile.GetProperty().SetSpecular(.3)
profile.GetProperty().SetSpecularPower(30)
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren.AddActor(glyph)
ren.AddActor(profile)
renWin.SetSize(1000, 1000)
iren.Initialize()
renWin.Render()
iren.Start()
您错过了这些行:
balls.Update()
glyphPoints.Update()
Tubes.Update()
我在尝试 运行 这段代码时没有收到任何错误,但是没有呈现任何内容,只出现一个空白屏幕。请让我知道我哪里出错了。 node_pos 是一个字典,所有节点坐标都以节点号为键,G 是 networkx 图对象 G。此代码改编自 2005 年在其他地方找到的代码,因此不得不更新一些 VTK 属性,因为它们已经过时了。
def draw_nxvtk(G, node_pos):
# set node positions
np={}
for n in G.nodes():
np[n]=node_pos[n]
nodePoints = vtk.vtkPoints()
i=0
for (x,y,z) in np.values():
nodePoints.InsertPoint(i, x, y, z)
i=i+1
# Create a polydata to be glyphed.
inputData = vtk.vtkPolyData()
inputData.SetPoints(nodePoints)
# Use sphere as glyph source.
balls = vtk.vtkSphereSource()
balls.SetRadius(.05)
balls.SetPhiResolution(20)
balls.SetThetaResolution(20)
glyphPoints = vtk.vtkGlyph3D()
glyphPoints.SetInputData(inputData)
glyphPoints.SetSourceData(balls.GetOutput())
glyphMapper = vtk.vtkPolyDataMapper()
glyphMapper.SetInputData(glyphPoints.GetOutput())
glyph = vtk.vtkActor()
glyph.SetMapper(glyphMapper)
glyph.GetProperty().SetDiffuseColor(plum)
glyph.GetProperty().SetSpecular(.3)
glyph.GetProperty().SetSpecularPower(30)
# Generate the polyline for the spline.
points = vtk.vtkPoints()
edgeData = vtk.vtkPolyData()
# Edges
lines = vtk.vtkCellArray()
i=0
for e in G.edges:
u=e[0]
v=e[1]
lines.InsertNextCell(2)
for n in (u,v):
(x,y,z)=node_pos[n]
points.InsertPoint(i, x, y, z)
lines.InsertCellPoint(i)
i=i+1
edgeData.SetPoints(points)
edgeData.SetLines(lines)
Tubes = vtk.vtkTubeFilter()
Tubes.SetNumberOfSides(16)
Tubes.SetInputData(edgeData)
Tubes.SetRadius(.01)
profileMapper = vtk.vtkPolyDataMapper()
profileMapper.SetInputData(Tubes.GetOutput())
profile = vtk.vtkActor()
profile.SetMapper(profileMapper)
profile.GetProperty().SetDiffuseColor(banana)
profile.GetProperty().SetSpecular(.3)
profile.GetProperty().SetSpecularPower(30)
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren.AddActor(glyph)
ren.AddActor(profile)
renWin.SetSize(1000, 1000)
iren.Initialize()
renWin.Render()
iren.Start()
您错过了这些行:
balls.Update()
glyphPoints.Update()
Tubes.Update()