我想要显示纹理的表面。我希望三角形边界在表面上以不同的颜色可见(比如红色)。我从vtk代码示例中找到了以下代码,但它不显示三角形边界,而是显示填充的三角形。
import vtk
# create a rendering window and renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# create points
points = vtk.vtkPoints()
points.InsertNextPoint(1.0,0.0,0.0)
points.InsertNextPoint(0.0,0.0,0.0)
points.InsertNextPoint(0.0,1.0,0.0)
triangle = vtk.vtkTriangle()
triangle.GetPointIds().SetId(0,0)
triangle.GetPointIds().SetId(1,1)
triangle.GetPointIds().SetId(2,2)
triangles = vtk.vtkCellArray()
triangles.InsertNextCell(triangle)
# polydata object
trianglePolyData = vtk.vtkPolyData()
trianglePolyData.SetPoints( points )
trianglePolyData.SetPolys( triangles )
# mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(trianglePolyData)
# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
# assign actor to the renderer
ren.AddActor(actor)
# enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()
任何人都可以告诉我如何只显示具有特定颜色边界的三角形。
理想情况下,我想在纹理表面上显示三角形。我的数据由三角形组成。也可能使得给予vtk的三角形的顶点可见。
我在python中编码。
非常感谢
答案 0 :(得分:4)
您需要从vtkPolyData
对象中提取边缘:
edges = vtk.vtkExtractEdges()
edges.SetInput(trianglePolyData)
edge_mapper = vtk.vtkPolyDataMapper()
edge_mapper.SetInput(edges.GetOutput())
edge_actor = vtk.vtkActor()
edge_actor.SetMapper(edge_mapper)
edge_actor.GetProperty().SetColor(1,0,0)
ren.AddActor(edge_actor)
vtk.vtkPolyDataMapper().SetResolveCoincidentTopologyToPolygonOffset()
首先,您必须通过vtkExtractEdges
过滤器提取边缘。您将该过滤器的结果映射到vtkPolyData
对象,并为该数据构造一个actor。然后我们通过直接修改actor将网格的颜色设置为红色。
对vtk.vtkPolyDataMapper().SetResolveCoincidentTopologyToPolygonOffset()
的调用可以防止边缘与曲面发生冲突(由于z缓冲区精度问题,两个几何对象重合并且撕裂相互穿过)。
为了完整起见,这是完整的代码:
import vtk
# create a rendering window and renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# create points
points = vtk.vtkPoints()
points.InsertNextPoint(1.0,0.0,0.0)
points.InsertNextPoint(0.0,0.0,0.0)
points.InsertNextPoint(0.0,1.0,0.0)
triangle = vtk.vtkTriangle()
triangle.GetPointIds().SetId(0,0)
triangle.GetPointIds().SetId(1,1)
triangle.GetPointIds().SetId(2,2)
triangles = vtk.vtkCellArray()
triangles.InsertNextCell(triangle)
# polydata object
trianglePolyData = vtk.vtkPolyData()
trianglePolyData.SetPoints( points )
trianglePolyData.SetPolys( triangles )
# mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(trianglePolyData)
# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
# assign actor to the renderer
ren.AddActor(actor)
#++++++++++++++++++++++++++++++++++++++++++++++++
# Get the edges from the mesh
edges = vtk.vtkExtractEdges()
edges.SetInput(trianglePolyData)
edge_mapper = vtk.vtkPolyDataMapper()
edge_mapper.SetInput(edges.GetOutput())
# Make an actor for those edges
edge_actor = vtk.vtkActor()
edge_actor.SetMapper(edge_mapper)
# Make the actor red (there are other ways of doing this also)
edge_actor.GetProperty().SetColor(1,0,0)
ren.AddActor(edge_actor)
# Avoid z-buffer fighting
vtk.vtkPolyDataMapper().SetResolveCoincidentTopologyToPolygonOffset()
#------------------------------------------------
# enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()