PyQtGraph & OpenGL:如何在两个坐标之间创建一个球体?

PyQtGraph & OpenGL: How to create a sphere between two coordinates?

是否可以使用 PyQtGraph 和 OpenGL 在两个坐标之间创建一个球体?在我的示例代码中,我制作了一个球体,但位置和大小仅由 "rows" 和 "columns" 决定。我想在 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() 方法 returns 与一定半径(默认为 1)并以 (0, 0, 0) 为中心的球体相关联的数据,因此使用 point1 和 point2 的信息你可以获得半径和圆心,要建立圆心必须使用translate()方法移动item:

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_()