是否可以使用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_()