VTK (Python) rendering artifacts
Minimal working example
Consider the following minimal working example:
RENDER_SELECTION = True
# mesh:
#
# (6) - (7) - (8)
# | 2 | 3 |
# (3) - (4) - (5)
# | 0 | 1 |
# (0) - (1) - (2)
#
pointCoordinates = (
(0.0, 0.0, 0.0),
(1.0, 0.0, 0.0),
(2.0, 0.0, 0.0),
(0.0, 1.0, 0.0),
(1.0, 1.0, 0.0),
(2.0, 1.0, 0.0),
(0.0, 2.0, 0.0),
(1.0, 2.0, 0.0),
(2.0, 2.0, 0.0)
)
cellConnectivity = (
(0, 1, 4, 3),
(1, 2, 5, 4),
(3, 4, 7, 6),
(4, 5, 8, 7)
)
selectedCells = (0, 3)
import vtkmodules.vtkRenderingOpenGL2 # type: ignore
import vtkmodules.vtkInteractionStyle # type: ignore
from vtkmodules.vtkCommonCore import vtkPoints, vtkIdTypeArray
from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid, vtkSelectionNode, vtkSelection, VTK_QUAD
from vtkmodules.vtkFiltersExtraction import vtkExtractSelection
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
from vtkmodules.vtkRenderingCore import (
vtkDataSetMapper, vtkActor, vtkRenderer, vtkRenderWindow, vtkRenderWindowInteractor
)
# renderer
renderer = vtkRenderer()
# interactor & interactor style
interactorStyle = vtkInteractorStyleTrackballCamera()
interactor = vtkRenderWindowInteractor()
interactor.SetInteractorStyle(interactorStyle)
# render window
renderWindow = vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindow.SetInteractor(interactor)
# instantiate the data set object
dataSet = vtkUnstructuredGrid()
# set point coordinates
points = vtkPoints()
points.SetNumberOfPoints(len(pointCoordinates))
for i, coordinates in enumerate(pointCoordinates):
points.SetPoint(i, coordinates)
dataSet.SetPoints(points) # type: ignore
# set cell connectivity
dataSet.AllocateExact(len(cellConnectivity), 4)
for nodeIndices in cellConnectivity:
dataSet.InsertNextCell(VTK_QUAD, 4, nodeIndices) # type: ignore
dataSet.Squeeze()
# mapper
mapper = vtkDataSetMapper()
mapper.SetInputData(dataSet) # type: ignore
mapper.Update() # type: ignore
# actor
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().EdgeVisibilityOn()
actor.GetProperty().SetColor(1.0, 1.0, 0.8)
renderer.AddActor(actor)
# add selection
if RENDER_SELECTION:
# indices
indices = vtkIdTypeArray()
indices.SetNumberOfValues(len(selectedCells))
for i, index in enumerate(selectedCells):
indices.SetValue(i, index)
# selection node
selectionNode = vtkSelectionNode()
selectionNode.SetFieldType(vtkSelectionNode.CELL)
selectionNode.SetContentType(vtkSelectionNode.INDICES)
selectionNode.SetSelectionList(indices) # type: ignore
# selection
selection = vtkSelection()
selection.AddNode(selectionNode)
# extraction filter
extractionFilter = vtkExtractSelection()
extractionFilter.SetInputData(0, dataSet) # type: ignore
extractionFilter.SetInputData(1, selection) # type: ignore
extractionFilter.Update() # type: ignore
# mapper
selectionMapper = vtkDataSetMapper()
selectionMapper.SetInputData(extractionFilter.GetOutput()) # type: ignore
selectionMapper.Update() # type: ignore
# actor
selectionActor = vtkActor()
selectionActor.SetMapper(selectionMapper)
selectionActor.GetProperty().SetColor(1.0, 0.0, 0.0)
# add to scene
renderer.AddActor(selectionActor)
# start the event loop
interactor.Initialize()
interactor.Start()
The first issue
If I run this example in Python 3.11.1 with VTK 9.2.5 with RENDER_SELECTION = False
, I get the following output:
It is hard to see but I get these unwanted triangulation lines, I highlighted them in red for clarity:
The second issue
If I run with RENDER_SELECTION = True
, I get the following output:
I expected the two red quads to be correctly painted red on selection.
I can add that both these issues were not present in VTK via ActiViz (version 5.8.0).
Edited by Carlos Souto