I want to display a surface which is textured. I want the triangles boundaries to be visible over the surface in a different color (lets say red). I have found the following code from the vtk code samples but it does not display the triangle boundaries but the filled triangles.
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()
Can anybody please let me know that how to display triangle only with the b开发者_运维问答oundaries with a specific color.
I ideally i want to display triangles on a textured surface. My data consist of triangles. It might also be possible that the vertices of the triangles which are given to vtk can be made visible.
I am coding in python.
Thanks a lot
You need to extract the edges from your vtkPolyData
object:
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()
First you must extract the edges via a vtkExtractEdges
filter. You map the results of that filter to a vtkPolyData
object and construct an actor for that data. We then set the color of the mesh to red by modifying the actor directly.
The call to vtk.vtkPolyDataMapper().SetResolveCoincidentTopologyToPolygonOffset()
prevents the edges from fighting with the surfaces (the two geometric objects are coincident and tear through each other due to z-buffer precision issues).
For completeness sake, here's the whole code:
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()
精彩评论