【问题标题】:PyQtGraph & OpenGL: How to create a sphere between two coordinates?PyQtGraph & OpenGL:如何在两个坐标之间创建一个球体?
【发布时间】:2020-07-19 10:45:57
【问题描述】:

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

【问题讨论】:

    标签: python opengl pyqt pyqtgraph


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 2018-02-25
      • 2016-07-16
      • 1970-01-01
      • 2015-09-02
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多