Draw polygon and highlight points inside the polygon

Hello everybody,
I would like to have some suggestions on how to implement the following:

I have a STL file and I would like to allow the user to draw a closed polygon and pick points of the mesh inside the closed contour.
How have I to approach the problem? I couldn’t find any example online.
Best

I think it is doable on 2d and 3d (like blender) but you’ll need to put some work into it. Maybe check the contourFilter class

I would go with selection.

For a quick preview open up a .stl file in ParaView and click either of these buttons at the top of the RenderView window, then mouse drag in the view.
image

VTK’s selection is the basis for that. You should be able to find examples and tests in the VTK books and online sources.

Thanks for the reply,
what do you mean for “selection”?

See tests/examples and the book that involve vtkHardwareSelector, for example:

Rendering/Core/Testing/Cxx/TestPolygonSelection.cxx · master · VTK / VTK · GitLab (kitware.com)

Thanks for the reply. I forgot to add that I would like to achieve this using Python.
Is there any example involving python?

I do not know.

As suggested, I looked at HardwareSelector example. In the example linked ( TestPolygonSelection.cxx) the draw-polygon interactor style is involved. As concerns the Python wrapper the method “GetPolygonPoints” has not been implemented because it returns a vtkVector2i, which is difficult to wrap. So I found this re-write of the vtk class on github HERE. Just to test the class I wrote this piece of code:

 def loadSTL(filenameSTL):
      readerSTL = vtk.vtkSTLReader()
      readerSTL.SetFileName(filenameSTL)
      # 'update' the reader i.e. read the .stl file
      readerSTL.Update()
  
      polydata = readerSTL.GetOutput()
      
      print ("Number of Cells:",  polydata.GetNumberOfCells())
      print ("Number of Points:", polydata.GetNumberOfPoints())
      
      # If there are no points in 'vtkPolyData' something went wrong
      if polydata.GetNumberOfPoints() == 0:
          raise ValueError("No point data could be loaded from " + filenameSTL)
          return None
          
      return polydata



colors = vtk.vtkNamedColors()

mesh_path = path of the STL file

mesh = loadSTL(mesh_path)

mapper = vtk.vtkPolyDataMapper()

mapper.SetInputData(mesh)         # maps polygonal data to graphics primitives

actor = vtk.vtkActor()

actor.SetMapper(mapper)

actor.GetProperty().EdgeVisibilityOn()

actor.GetProperty().SetLineWidth(0.3)

ren1 = vtk.vtkRenderer()

ren1.AddActor(actor)

ren1.SetBackground(colors.GetColor3d('Navy'))

renWin = vtk.vtkRenderWindow()

renWin.AddRenderer(ren1)

renWin.SetSize(600, 600)

renWin.SetWindowName('HardwareSelector')

iren = vtk.vtkRenderWindowInteractor()

iren.SetRenderWindow(renWin)

#interactor style-->draw polygon

polystyle = InteractorStyleDrawPolygon()

polystyle.SetDefaultRenderer(ren1)

iren.SetInteractorStyle(polystyle)


iren.Initialize()

iren.Start()

The STL file is loaded correctly but the interactor seems not working because anything happens when I clicked the mouse left button. I added–> print (“pressed”) inside the callback OnLeftButtonUp and it is executed so that it seems that self.interactor is None. How can I fix it?

Could anyone help me?