mirror of
http://git.xinwangdao.com/cnnc-embedded-parts-detect/detect-gui.git
synced 2025-06-25 05:24:13 +08:00
101 lines
3.4 KiB
Python
101 lines
3.4 KiB
Python
|
import sys
|
||
|
import random
|
||
|
from PyQt5.QtWidgets import QApplication, QMainWindow, QOpenGLWidget
|
||
|
from PyQt5.QtCore import Qt, pyqtSignal, QObject
|
||
|
from OpenGL.GL import *
|
||
|
from OpenGL.GLUT import *
|
||
|
from OpenGL.GLU import *
|
||
|
|
||
|
class PointCloudWidget(QOpenGLWidget):
|
||
|
def __init__(self, parent=None):
|
||
|
super(PointCloudWidget, self).__init__(parent)
|
||
|
self.point_cloud = [] # 初始化点云数据为空
|
||
|
self.point_size = 5 # 点的大小
|
||
|
|
||
|
def update_point_cloud(self, new_point_cloud):
|
||
|
"""更新点云数据并触发刷新"""
|
||
|
self.point_cloud = new_point_cloud
|
||
|
self.update() # 触发重绘
|
||
|
|
||
|
def initializeGL(self):
|
||
|
"""初始化OpenGL参数"""
|
||
|
glClearColor(0.0, 0.0, 0.0, 1.0) # 背景颜色:黑色
|
||
|
glEnable(GL_DEPTH_TEST) # 启用深度测试
|
||
|
glPointSize(self.point_size) # 设置点的大小
|
||
|
|
||
|
def paintGL(self):
|
||
|
"""绘制点云"""
|
||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # 清除颜色和深度缓冲
|
||
|
glLoadIdentity() # 重置当前矩阵
|
||
|
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0) # 设置观察视角
|
||
|
|
||
|
glBegin(GL_POINTS) # 开始绘制点
|
||
|
for point in self.point_cloud:
|
||
|
x, y, z, r, g, b = point
|
||
|
glColor3f(r, g, b) # 设置点的颜色
|
||
|
glVertex3f(x, y, z) # 设置点的位置
|
||
|
glEnd() # 结束绘制
|
||
|
|
||
|
glFlush() # 刷新绘图
|
||
|
|
||
|
def resizeGL(self, w, h):
|
||
|
"""调整窗口大小时重置视口和投影矩阵"""
|
||
|
glViewport(0, 0, w, h)
|
||
|
glMatrixMode(GL_PROJECTION)
|
||
|
glLoadIdentity()
|
||
|
gluPerspective(45.0, w / h, 1.0, 100.0)
|
||
|
glMatrixMode(GL_MODELVIEW)
|
||
|
|
||
|
|
||
|
class PointCloudManager(QObject):
|
||
|
"""负责发送点云数据的管理类"""
|
||
|
point_cloud_signal = pyqtSignal(list) # 信号,用于发送点云数据
|
||
|
|
||
|
def __init__(self):
|
||
|
super().__init__()
|
||
|
|
||
|
def generate_and_send_point_cloud(self):
|
||
|
"""模拟生成点云数据并通过信号发送"""
|
||
|
new_point_cloud = self.generate_point_cloud(1000) # 生成1000个随机点
|
||
|
self.point_cloud_signal.emit(new_point_cloud) # 发射信号
|
||
|
|
||
|
def generate_point_cloud(self, num_points):
|
||
|
"""生成随机点云数据"""
|
||
|
points = []
|
||
|
for _ in range(num_points):
|
||
|
x = random.uniform(-1.0, 1.0) # X坐标
|
||
|
y = random.uniform(-1.0, 1.0) # Y坐标
|
||
|
z = random.uniform(-1.0, 1.0) # Z坐标
|
||
|
r = random.random() # 红色通道
|
||
|
g = random.random() # 绿色通道
|
||
|
b = random.random() # 蓝色通道
|
||
|
points.append((x, y, z, r, g, b))
|
||
|
return points
|
||
|
|
||
|
|
||
|
class MainWindow(QMainWindow):
|
||
|
def __init__(self):
|
||
|
super(MainWindow, self).__init__()
|
||
|
self.setWindowTitle("PyQt5 + OpenGL 实时点云显示")
|
||
|
self.setGeometry(100, 100, 800, 600)
|
||
|
|
||
|
# 创建OpenGL显示组件
|
||
|
self.gl_widget = PointCloudWidget()
|
||
|
self.setCentralWidget(self.gl_widget)
|
||
|
|
||
|
# 创建点云管理器
|
||
|
self.point_cloud_manager = PointCloudManager()
|
||
|
|
||
|
# 将信号连接到PointCloudWidget的更新槽
|
||
|
self.point_cloud_manager.point_cloud_signal.connect(self.gl_widget.update_point_cloud)
|
||
|
|
||
|
# 模拟点云数据生成与发送
|
||
|
self.point_cloud_manager.generate_and_send_point_cloud()
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app = QApplication(sys.argv)
|
||
|
window = MainWindow()
|
||
|
window.show()
|
||
|
sys.exit(app.exec_())
|