# Visibility, camera and mesh

**URL:** https://discourse.vtk.org/t/visibility-camera-and-mesh/11812
**Category:** Development
**Tags:** python, code
**Created:** [June 25, 2023, 7:02am UTC](https://discourse.vtk.org/t/visibility-camera-and-mesh/11812 "2023-06-25T07:02:47Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Victor](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@Victor](https://discourse.vtk.org/u/Victor)
#### Post date: [June 25, 2023, 7:02am UTC](https://discourse.vtk.org/t/visibility-camera-and-mesh/11812/1 "2023-06-25T07:02:48Z")

</div>

Hello everyone, I have a problem to be solved.  
There is a triangular mesh of STL format and 3 cameras in my scene.  
I need to get the index ID of all the mesh faces that is visible to each camera.  
Since there are close to 100w faces, I also have requirements for efficiency.  
I use python 3.8 and vtk 9.2.6. Thank you.  
I’m interested to fund this question, please reach out privately if interested.  
It seems the vtk [example](https://examples.vtk.org/site/Python/Visualization/HardwareSelector/) can be used but it should be acceleracted.

```auto
#!/usr/bin/env python

# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonDataModel import vtkDataObject
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkHardwareSelector,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)

# Callback for when selection is changed

# This is global - fix later.
ren1 = vtkRenderer()

def selectionCallback(caller, eventId):
    hsel = vtkHardwareSelector()
    hsel.SetFieldAssociation(vtkDataObject.FIELD_ASSOCIATION_CELLS)
    hsel.SetRenderer(ren1)

    x, y = caller.GetRenderWindow().GetSize()

    # Create a small area around clicked point for selector area
    hsel.SetArea(0, 0, x, y)
    res = hsel.Select()

    numNodes = res.GetNumberOfNodes()
    if (numNodes < 1):
        print("No visible cells")
    else:
        sel_node = res.GetNode(0)
        print('Visible cell IDs: ', VN.vtk_to_numpy(sel_node.GetSelectionList()).tolist())

def main():
    colors = vtkNamedColors()

    sphere = vtkSphereSource()
    sphere.SetCenter(0, 0, 0)
    sphere.SetRadius(5.0)

    sphereMapper = vtkPolyDataMapper()
    sphereMapper.SetInputConnection(sphere.GetOutputPort())

    sphereActor = vtkActor()
    sphereActor.SetMapper(sphereMapper)
    sphereActor.GetProperty().SetColor(colors.GetColor3d('Bisque'))

    ren1.AddActor(sphereActor)
    ren1.GetActiveCamera().ParallelProjectionOn()
    ren1.SetBackground(colors.GetColor3d('Navy'))

    renWin = vtkRenderWindow()
    renWin.AddRenderer(ren1)
    renWin.SetSize(300, 300)
    renWin.SetWindowName('HardwareSelector')

    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)
    iren.AddObserver("UserEvent", selectionCallback)

    style = vtkInteractorStyleTrackballCamera()
    iren.SetInteractorStyle(style)
    renWin.GetInteractor().SetInteractorStyle(style)

    ren1.ResetCamera()
    renWin.Render()

    iren.Initialize()
    iren.Start()

if __name__ == ' __main__':
    main()

```

---

<div class="post-metadata">

### Author: ![Jishnu](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jishnu/32/6221_2.png) [@Jishnu](https://discourse.vtk.org/u/Jishnu)
#### Post date: [June 26, 2023, 3:39am UTC](https://discourse.vtk.org/t/visibility-camera-and-mesh/11812/2 "2023-06-26T03:39:52Z")

</div>

Hi @Victor, what you mean by a paid question?

---

<div class="post-metadata">

### Author: ![Victor](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@Victor](https://discourse.vtk.org/u/Victor)
#### Post date: [June 26, 2023, 6:25am UTC](https://discourse.vtk.org/t/visibility-camera-and-mesh/11812/3 "2023-06-26T06:25:15Z")

</div>

This means I’m interested to fund someone to help me, please reach out privately.

---

<div class="post-metadata">

### Author: ![Jishnu](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jishnu/32/6221_2.png) [@Jishnu](https://discourse.vtk.org/u/Jishnu)
#### Post date: [June 26, 2023, 6:54am UTC](https://discourse.vtk.org/t/visibility-camera-and-mesh/11812/4 "2023-06-26T06:54:54Z")

</div>

Dear @Victor, here we help each other without expecting any rewards. I strongly believe these kind of post are not good for the community, however it is up to the mods to the decide.

---

<div class="post-metadata">

### Author: ![Francois\_Mazen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/francois_mazen/32/3290_2.png) [@Francois\_Mazen](https://discourse.vtk.org/u/Francois_Mazen)
#### Post date: [June 26, 2023, 6:56am UTC](https://discourse.vtk.org/t/visibility-camera-and-mesh/11812/5 "2023-06-26T06:56:53Z")

</div>

> [@Victor](#):
>
> This means I would pay $10 for the best solution.

You should contact Kitware directly to get fast answer via a support contract: [https://www.kitware.com/contact/](https://www.kitware.com/contact/)

---

<div class="post-metadata">

### Author: ![Victor](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@Victor](https://discourse.vtk.org/u/Victor)
#### Post date: [June 26, 2023, 7:12am UTC](https://discourse.vtk.org/t/visibility-camera-and-mesh/11812/6 "2023-06-26T07:12:40Z")

</div>

Hey, a free spirit is perfect, but knowledge is also valuable. I’m a researcher, but I don’t know much about VTK. This question has been bothering me for a long time. I have applied various methods, but nothing works.

---

<div class="post-metadata">

### Author: ![Jishnu](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jishnu/32/6221_2.png) [@Jishnu](https://discourse.vtk.org/u/Jishnu)
#### Post date: [June 26, 2023, 7:27am UTC](https://discourse.vtk.org/t/visibility-camera-and-mesh/11812/7 "2023-06-26T07:27:22Z")

</div>

No comments.

For your question, I think you are looking for ray casting. My suggestion would be to cast rays from the location of each camera to the object. You can get cell ids where these rays hit.

To get cells along a line you can use `vtkCellLocator` `vtkCellLocator.FindCellsAlongLine` method.

Based on the distance of your camera to the object and object dimension, you can customize how many rays to cast and in which all direction.

---

<div class="post-metadata">

### Author: ![Victor](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@Victor](https://discourse.vtk.org/u/Victor)
#### Post date: [June 26, 2023, 7:27am UTC](https://discourse.vtk.org/t/visibility-camera-and-mesh/11812/8 "2023-06-26T07:27:56Z")

</div>

Thank you. I would try it.

---

<div class="post-metadata">

### Author: ![Victor](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@Victor](https://discourse.vtk.org/u/Victor)
#### Post date: [June 26, 2023, 7:36am UTC](https://discourse.vtk.org/t/visibility-camera-and-mesh/11812/9 "2023-06-26T07:36:25Z")

</div>

Hey Jishnu,  
Thank you for your suggestion.  
There is no doubt that raycasting can solve this problem, but there seems to be a problem.  
Raycasting will discretize space leading to some regions could not be extracted, and this requires us to control the density of rays. Besides, raycasting can take more time if not optimized. I’m sensitive to time consumption because one iteration of my research takes a long time.

Thanks again for your reply and suggestion.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [June 28, 2023, 7:13am UTC](https://discourse.vtk.org/t/visibility-camera-and-mesh/11812/10 "2023-06-28T07:13:09Z")

</div>

@Victor , I’ve edited your message to avoid mentionning actual pricing. Please use private communication for such exchanges in the future.
