python - PyQtGraph & OpenGL:如何在两个坐标之间创建一个球体?
问题描述
是否可以使用 PyQtGraph 和 OpenGL 在两个坐标之间创建一个球体?在我的示例代码中,我制作了一个球体,但位置和大小仅由“行”和“列”确定。我想在point1和point2之间连接球体的端点。你能帮助我吗?
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import pyqtgraph.opengl as gl
import numpy as np
import sys
app = QtGui.QApplication([])
w = gl.GLViewWidget()
w.show()
w.setCameraPosition(distance=15, azimuth=-90)
g = gl.GLGridItem()
g.scale(2, 2, 1)
w.addItem(g)
# coordinates
point1 = np.array([0, 0, 0])
point2 = np.array([0, 5, 0])
md = gl.MeshData.sphere(rows=10, cols=20)
m1 = gl.GLMeshItem(meshdata=md, smooth=True, color=(1, 0, 0, 0.2), shader='balloon', glOptions='additive')
m1.scale(1, 1, 2)
w.addItem(m1)
if __name__ == '__main__':
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
解决方案
sphere() 方法返回一个与某个半径(默认为 1)的球体相关联的数据,并且以 (0, 0, 0) 为中心,因此使用 point1 和 point2 的信息可以获取半径和中心,以建立中心必须使用 translate() 方法移动项目:
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import pyqtgraph.opengl as gl
import numpy as np
import sys
app = QtGui.QApplication([])
w = gl.GLViewWidget()
w.show()
w.setCameraPosition(distance=15, azimuth=-90)
g = gl.GLGridItem()
w.addItem(g)
# coordinates
point1 = np.array([0, 0, 0])
point2 = np.array([0, 5, 0])
center = (point1 + point2) / 2
radius = np.linalg.norm(point2 - point1) / 2
md = gl.MeshData.sphere(rows=10, cols=20, radius=radius)
m1 = gl.GLMeshItem(
meshdata=md,
smooth=True,
color=(1, 0, 0, 0.2),
shader="balloon",
glOptions="additive",
)
m1.translate(*center)
w.addItem(m1)
if __name__ == "__main__":
if (sys.flags.interactive != 1) or not hasattr(QtCore, "PYQT_VERSION"):
QtGui.QApplication.instance().exec_()
推荐阅读
- html - 使用 HTML 和 CSS,是否可以创建如下所示的行
- performance - Powershell使用公共属性合并两个数组的最快方法?
- homebrew - gcloud init 错误:gcloud 崩溃(AttributeError):“凭据”对象没有属性“quota_project_id”
- google-chrome - chrome 浏览器中的网站审核显示不同设备的不同结果
- sublimetext3 - Sublime 3 高亮问题
- vue.js - 如何使用 Vue 检测浏览器后退按钮?
- sql - Azure Synapse - 检索插入的行标识值
- react-native - 活动指示器不包含 React-Native 上的标头
- python - Python Tkinter,如何同步多个闪烁的小部件?
- python - 如果使用 jinja 匹配,则从列表创建列表