VTK 渲染在 PyQt 中无法按预期工作 [英] VTK rendering not working as expected inside PyQt

查看:90
本文介绍了VTK 渲染在 PyQt 中无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 PyQt 应用程序中集成 VTK 可视化.但是,当我将模型放入 QVTKRenderWindowInteractor 时,显示会显示一些不想要的透明效果(见下图).无论我尝试加载什么,这都会发生在表面或点云上.

有没有办法在 QVTKRenderWindowInteractor 中实现正确的表示?

第一张图片是来自 vtk.vtkConeSource() 的圆锥体.

第二张图是

左: 没有 QVTKRenderWindowInteractor.正确:使用 QVTKRenderWindowInteractor

我附上问题的示例代码以供重现.这是没有Qt的代码:

#!/usr/bin/env python导入 vtk从 vtk.util.colors 导入番茄"""这个简单的例子展示了如何进行基本的渲染和管道创建."""锥体 = vtk.vtkConeSource()锥体.SetResolution(8)锥体映射器 = vtk.vtkPolyDataMapper()锥体映射器.SetInputConnection(cone.GetOutputPort())coneActor = vtk.vtkActor()coneActor.SetMapper(coneMapper)coneActor.GetProperty().SetColor(tomato)锥体Actor.RotateX(30.0)锥体Actor.RotateY(-45.0)ren = vtk.vtkRenderer()renWin = vtk.vtkRenderWindow()renWin.AddRenderer(ren)iren = vtk.vtkRenderWindowInteractor()iren.SetRenderWindow(renWin)ren.AddActor(coneActor)ren.SetBackground(0.1, 0.2, 0.4)iren.Initialize()ren.ResetCamera()ren.GetActiveCamera().Zoom(1.5)renWin.Render()iren.Start()

这是在 QVTKRenderWindowInteractor 中的等效显示:

#!/usr/bin/env python导入 vtk从 PyQt5.QtWidgets 导入 QApplication从 vtk.qt.QVTKRenderWindowInteractor 导入 QVTKRenderWindowInteractor从 vtk.util.colors 导入番茄"""使用 QVTKRenderWindowInteractor 类的简单示例."""app = QApplication(['QVTKRenderWindowInteractor'])锥体 = vtk.vtkConeSource()锥体.SetResolution(8)锥体映射器 = vtk.vtkPolyDataMapper()锥体映射器.SetInputConnection(cone.GetOutputPort())coneActor = vtk.vtkActor()coneActor.SetMapper(coneMapper)coneActor.GetProperty().SetColor(tomato)锥体.RotateX(30.0)锥体Actor.RotateY(-45.0)ren = vtk.vtkRenderer()小部件 = QVTKRenderWindowInteractor()widget.GetRenderWindow().AddRenderer(ren)ren.AddActor(coneActor)ren.SetBackground(0.1,0.2,0.4)小部件.Initialize()ren.ResetCamera()ren.GetActiveCamera().Zoom(1.5)widget.GetRenderWindow().Render()小部件.开始()小部件.show()app.exec_()

解决方案

在尝试了几件事之后,我得到了解决方案,阅读以下内容 合并请求.

我使用 QGLWidget 作为 QVTKRenderWindowInteractor 的基类,而不是 QWidget.这个改动是因为据说QWidget有时会导致渲染问题.

为此,我在导入 QVTKRenderWindowInteractor 之前添加了以下代码:

导入vtk.qtvtk.qt.QVTKRWIBase = "QGLWidget"从 vtk.qt.QVTKRenderWindowInteractor 导入 QVTKRenderWindowInteractor

为了使用 QGLWidget,我必须安装以下软件包:

<块引用>

sudo apt-get install python3-pyqt5.qtopengl

I need to integrate a VTK visualization inside a PyQt application. However, when I put a model inside a QVTKRenderWindowInteractor the display shows some undesired transparency effect (see picture below). This happens for surfaces or point clouds, whatever I try to load.

Is there any way to achieve the proper representation inside a QVTKRenderWindowInteractor?

First picture is a cone from vtk.vtkConeSource().

Second picture is a cturtle.pcd pointcloud from PCL Tests.

Left: Without QVTKRenderWindowInteractor. Right: With QVTKRenderWindowInteractor

I attach an example code of the problem for reproduction. This is the code without Qt:

#!/usr/bin/env python

import vtk
from vtk.util.colors import tomato

"""This simple example shows how to do basic rendering and pipeline creation."""

cone = vtk.vtkConeSource()
cone.SetResolution(8)

coneMapper = vtk.vtkPolyDataMapper()
coneMapper.SetInputConnection(cone.GetOutputPort())

coneActor = vtk.vtkActor()
coneActor.SetMapper(coneMapper)
coneActor.GetProperty().SetColor(tomato)
coneActor.RotateX(30.0)
coneActor.RotateY(-45.0)

ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)


ren.AddActor(coneActor)
ren.SetBackground(0.1, 0.2, 0.4)

iren.Initialize()

ren.ResetCamera()
ren.GetActiveCamera().Zoom(1.5)
renWin.Render()

iren.Start()

And this is the equivalent display inside a QVTKRenderWindowInteractor:

#!/usr/bin/env python

import vtk
from PyQt5.QtWidgets import QApplication
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from vtk.util.colors import tomato

"""A simple example that uses the QVTKRenderWindowInteractor class."""


app = QApplication(['QVTKRenderWindowInteractor'])

cone = vtk.vtkConeSource()
cone.SetResolution(8)

coneMapper = vtk.vtkPolyDataMapper()
coneMapper.SetInputConnection(cone.GetOutputPort())

coneActor = vtk.vtkActor()
coneActor.SetMapper(coneMapper)
coneActor.GetProperty().SetColor(tomato)
coneActor.RotateX(30.0)
coneActor.RotateY(-45.0)

ren = vtk.vtkRenderer()
widget = QVTKRenderWindowInteractor()
widget.GetRenderWindow().AddRenderer(ren)

ren.AddActor(coneActor)
ren.SetBackground(0.1,0.2,0.4)

widget.Initialize()

ren.ResetCamera()
ren.GetActiveCamera().Zoom(1.5)
widget.GetRenderWindow().Render()

widget.Start()

widget.show()
app.exec_()

解决方案

After trying several things, I got the solution reading the following Merge Request.

I used QGLWidget as the base class of the QVTKRenderWindowInteractor, instead of QWidget. This change is because it is reported that sometimes QWidget can cause rendering problems.

To do this I put the following code before importing QVTKRenderWindowInteractor:

import vtk.qt
vtk.qt.QVTKRWIBase = "QGLWidget"

from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor

In order to use QGLWidget I had to install the following package:

sudo apt-get install python3-pyqt5.qtopengl

这篇关于VTK 渲染在 PyQt 中无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆