# Draw polygon and highlight points inside the polygon

**URL:** https://discourse.vtk.org/t/draw-polygon-and-highlight-points-inside-the-polygon/8682
**Category:** Support
**Created:** [June 8, 2022, 3:27pm UTC](https://discourse.vtk.org/t/draw-polygon-and-highlight-points-inside-the-polygon/8682 "2022-06-08T15:27:55Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![mdc93](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@mdc93](https://discourse.vtk.org/u/mdc93)
#### Post date: [June 8, 2022, 3:27pm UTC](https://discourse.vtk.org/t/draw-polygon-and-highlight-points-inside-the-polygon/8682/1 "2022-06-08T15:27:55Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mau\_igna\_06](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mau_igna_06/32/8064_2.png) [@mau\_igna\_06](https://discourse.vtk.org/u/mau_igna_06)
#### Post date: [June 8, 2022, 6:25pm UTC](https://discourse.vtk.org/t/draw-polygon-and-highlight-points-inside-the-polygon/8682/2 "2022-06-08T18:25:02Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Dave\_DeMarle](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dave_demarle/32/6_2.png) [@Dave\_DeMarle](https://discourse.vtk.org/u/Dave_DeMarle)
#### Post date: [June 8, 2022, 9:49pm UTC](https://discourse.vtk.org/t/draw-polygon-and-highlight-points-inside-the-polygon/8682/3 "2022-06-08T21:49:32Z")

</div>

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](https://discourse.vtk.org/uploads/default/original/2X/2/24010f4356dd907e36e4545b74ed34d6743bdd03.png)

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

---

<div class="post-metadata">

### Author: ![mdc93](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@mdc93](https://discourse.vtk.org/u/mdc93)
#### Post date: [June 9, 2022, 9:25am UTC](https://discourse.vtk.org/t/draw-polygon-and-highlight-points-inside-the-polygon/8682/4 "2022-06-09T09:25:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Dave\_DeMarle](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dave_demarle/32/6_2.png) [@Dave\_DeMarle](https://discourse.vtk.org/u/Dave_DeMarle)
#### Post date: [June 9, 2022, 1:03pm UTC](https://discourse.vtk.org/t/draw-polygon-and-highlight-points-inside-the-polygon/8682/5 "2022-06-09T13:03:16Z")

</div>

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

[Rendering/Core/Testing/Cxx/TestPolygonSelection.cxx · master · VTK / VTK · GitLab (kitware.com)](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Rendering/Core/Testing/Cxx/TestPolygonSelection.cxx#L122)

---

<div class="post-metadata">

### Author: ![mdc93](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@mdc93](https://discourse.vtk.org/u/mdc93)
#### Post date: [June 10, 2022, 7:21am UTC](https://discourse.vtk.org/t/draw-polygon-and-highlight-points-inside-the-polygon/8682/6 "2022-06-10T07:21:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Dave\_DeMarle](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dave_demarle/32/6_2.png) [@Dave\_DeMarle](https://discourse.vtk.org/u/Dave_DeMarle)
#### Post date: [June 10, 2022, 1:28pm UTC](https://discourse.vtk.org/t/draw-polygon-and-highlight-points-inside-the-polygon/8682/7 "2022-06-10T13:28:24Z")

</div>

I do not know.

---

<div class="post-metadata">

### Author: ![mdc93](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@mdc93](https://discourse.vtk.org/u/mdc93)
#### Post date: [June 27, 2022, 8:45am UTC](https://discourse.vtk.org/t/draw-polygon-and-highlight-points-inside-the-polygon/8682/8 "2022-06-27T08:45:08Z")

</div>

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](https://gist.github.com/mhogg/4863cf0b91e2f85a56d52b8a6981e325). 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?

---

<div class="post-metadata">

### Author: ![mdc93](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@mdc93](https://discourse.vtk.org/u/mdc93)
#### Post date: [June 28, 2022, 1:09pm UTC](https://discourse.vtk.org/t/draw-polygon-and-highlight-points-inside-the-polygon/8682/9 "2022-06-28T13:09:34Z")

</div>

Could anyone help me?
