VTK is new to me, When I try to mark the triangle picked by the mouse as red, Triangles do not render properly.
Here is a video:
Environment:
OS version: Windows 10 21H2
Python version: 3.8
Vtk version: 9.1.0
Here is a minimal example that reproduces the issue. Can you help me to solve it?
#!/usr/bin/env python
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkIOPLY import vtkPLYReader
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import vtkIdTypeArray
from vtkmodules.vtkIOLegacy import vtkPolyDataReader
from vtkmodules.vtkCommonDataModel import (
vtkSelection,
vtkSelectionNode,
vtkUnstructuredGrid,
)
from vtkmodules.vtkFiltersExtraction import vtkExtractSelection
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkCellPicker,
vtkDataSetMapper,
vtkPolyDataMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
# Catch mouse events
class MouseInteractorStyle(vtkInteractorStyleTrackballCamera):
def __init__(self, data):
self.AddObserver('LeftButtonPressEvent', self.left_button_press_event)
self.data = data
self.selected_mapper = vtkDataSetMapper()
self.selected_actor = vtkActor()
def left_button_press_event(self, obj, event):
colors = vtkNamedColors()
# Get the location of the click (in window coordinates)
pos = self.GetInteractor().GetEventPosition()
picker = vtkCellPicker()
picker.SetTolerance(0.0005)
# Pick from this location.
picker.Pick(pos[0], pos[1], 0, self.GetDefaultRenderer())
world_position = picker.GetPickPosition()
print(f'Cell id is: {picker.GetCellId()}')
if picker.GetCellId() != -1:
print(f'Pick position is: ({world_position[0]:.6g}, {world_position[1]:.6g}, {world_position[2]:.6g})')
ids = vtkIdTypeArray()
ids.SetNumberOfComponents(1)
ids.InsertNextValue(picker.GetCellId())
selection_node = vtkSelectionNode()
selection_node.SetFieldType(vtkSelectionNode.CELL)
selection_node.SetContentType(vtkSelectionNode.INDICES)
selection_node.SetSelectionList(ids)
selection = vtkSelection()
selection.AddNode(selection_node)
extract_selection = vtkExtractSelection()
extract_selection.SetInputData(0, self.data)
extract_selection.SetInputData(1, selection)
extract_selection.Update()
# In selection
selected = vtkUnstructuredGrid()
selected.ShallowCopy(extract_selection.GetOutput())
print(f'Number of points in the selection: {selected.GetNumberOfPoints()}')
print(f'Number of cells in the selection : {selected.GetNumberOfCells()}')
self.selected_mapper.SetInputData(selected)
self.selected_actor.SetMapper(self.selected_mapper)
self.selected_actor.GetProperty().EdgeVisibilityOn()
self.selected_actor.GetProperty().SetColor(colors.GetColor3d('Tomato'))
# self.selected_actor.GetProperty().SetLineWidth(3)
self.GetInteractor().GetRenderWindow().GetRenderers().GetFirstRenderer().AddActor(self.selected_actor)
# Forward events
self.OnLeftButtonDown()
def main():
pd_fn = get_program_parameters()
colors = vtkNamedColors()
polyData = ReadPolyData(pd_fn)
mapper = vtkPolyDataMapper()
mapper.SetInputData(polyData)
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetDiffuseColor(colors.GetColor3d('Crimson'))
actor.GetProperty().EdgeVisibilityOn()
actor.GetProperty().SetColor(colors.GetColor3d('AliceBlue'))
actor.GetProperty().SetEdgeColor(colors.GetColor3d('SteelBlue'))
renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindow.SetWindowName('Test cell pick')
renderWindowInteractor = vtkRenderWindowInteractor()
renderer.AddActor(actor)
renderer.UseHiddenLineRemovalOff()
renderer.SetBackground(colors.GetColor3d('Silver'))
picker = vtkCellPicker()
renderWindowInteractor.SetPicker(picker)
# Add the custom style.
style = MouseInteractorStyle(polyData)
style.SetDefaultRenderer(renderer)
renderWindowInteractor.SetInteractorStyle(style)
# Interact to change camera.
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindow.Render()
renderWindowInteractor.Start()
def get_program_parameters():
import argparse
description = 'Test cell pick'
epilogue = '''
'''
parser = argparse.ArgumentParser(description=description, epilog=epilogue,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('data_file', help='A polydata file e.g. honolulu.vtk or Armadillo.ply. in folder Testing\Data')
args = parser.parse_args()
return args.data_file
def ReadPolyData(file_name):
import os
path, extension = os.path.splitext(file_name)
extension = extension.lower()
if extension == '.ply':
reader = vtkPLYReader()
reader.SetFileName(file_name)
reader.Update()
poly_data = reader.GetOutput()
elif extension == '.vtk':
reader = vtkPolyDataReader()
reader.SetFileName(file_name)
reader.Update()
poly_data = reader.GetOutput()
return poly_data
if __name__ == '__main__':
main()