Qiang Zhang
Qiang Zhang

Reputation: 952

Visualization failed when display two polygon in the same time

I want to display two polygons in one renderer. And the result is:

enter image description here

The green and red color indicates two polygons.

However, when I rotate it, some part of green color disappear:

enter image description here

enter image description here

enter image description here

My environment is:

win 10
python 3.7.13
vtk: 9.2.4

I can 100% reproduce this phenomenon, and the code to reproduce my problem is:

import vtkmodules.all as vtk


def buildPolygon(points):
    polydata = vtk.vtkPolyData()

    vps = vtk.vtkPoints()
    polygon = vtk.vtkPolygon()
    polygon.GetPointIds().SetNumberOfIds(len(points))

    for i in range(len(points)):
        vps.InsertNextPoint(points[i][0], points[i][1], points[i][2])
        polygon.GetPointIds().SetId(i, i)

    polygons = vtk.vtkCellArray()
    polygons.InsertNextCell(polygon)
    polydata.SetPoints(vps)
    polydata.SetPolys(polygons)
    return polydata

polydata1 = buildPolygon([
    [0, 0, 0],
    [10, 0, 0],
    [10, 10, 0],
    [0, 10, 0]
])
map1 = vtk.vtkPolyDataMapper()
map1.SetInputData(polydata1)
actor1 = vtk.vtkActor()
actor1.SetMapper(map1)
actor1.GetProperty().SetColor(1, 0, 0)

polydata2 = buildPolygon([
    [0, 0, 0],
    [5, 0, 0],
    [5, 5, 0],
    [0, 5, 0]
])
map2 = vtk.vtkPolyDataMapper()
map2.SetInputData(polydata2)
actor2 = vtk.vtkActor()
actor2.SetMapper(map2)
actor2.GetProperty().SetColor(0, 1, 0)

render = vtk.vtkRenderer()
render.AddActor(actor1)
render.AddActor(actor2)

renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(render)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
iren.Initialize()
iren.Start()

Upvotes: 0

Views: 43

Answers (1)

mmusy
mmusy

Reputation: 1337

As they lie exactly on the same plane z=0 there is no way to know which should be drawn on top of the other. Just add a small tolerance:

polydata2 = buildPolygon([
    [0, 0, 0.001],
    [5, 0, 0.001],
    [5, 5, 0.001],
    [0, 5, 0.001]
])

Upvotes: 1

Related Questions