How to know if specific point in world coordinates is visible within current render?

Hi all,

How to know if specific point in world coordinates is visible within current render?
I know vtkSelectVisiblePoints could handle that using z-buffer, however I cannot find source code of that part, and any examples related.
Any suggestions would be appreciated, thanks.

I tried following code to test if point on object’s surface is occluded(rotating camera 360 degrees and check if point is visible), but the returned value is always false.

    step = 10
    for i in range(36):
        renderer.GetActiveCamera().Azimuth(step)
        visPts = vtk.vtkSelectVisiblePoints()
        visPts.SetRenderer(renderer)
        visible = visPts.IsPointOccluded(pos, 0.0)
        print(f"visible {visible}")

Hello,

vtkSelectVisiblePoints’s IsPointOccluded() expects, as its second parameter, an array of foats that is the Z-Buffer data. You passed a 0.0 scalar instead. Hence the unexpected result.

Following the rationale in vtk render window GetZbufferData method - Stack Overflow, here’s a possible solution :warning: Untested C++ → Python translation:

    step = 10

    x1 = renderer.GetViewport()[0] * (renWin.GetSize()[0] - 1)
    y1 = renderer.GetViewport()[1] * (renWin.GetSize()[1] - 1)
    x2 = renderer.GetViewport()[2] * (renWin.GetSize()[0] - 1)
    y2 = renderer.GetViewport()[3] * (renWin.GetSize()[1] - 1)
    renZ = renWin.GetZbufferData(x1,y1,x2,y2)

    for i in range(36):
        renderer.GetActiveCamera().Azimuth(step)
        visPts = vtk.vtkSelectVisiblePoints()
        visPts.SetRenderer(renderer)
        visible = visPts.IsPointOccluded(pos, renZ)
        print(f"visible {visible}")

renWin is the vtkRenderWindow object of your program. Replace that with whatever name you gave to it. I hope this helps.

best,

PC

While the detailed solution provided by @Paulo_Carvalho will work, accessing the Z buffer is an expensive operation. Maybe you can achieve better performances by creating an implicit function from the camera frustum. Something similar is done here: https://examples.vtk.org/site/Cxx/Meshes/ClipFrustum/

1 Like