如何在3D pyqtgraph实现中设置GLMeshItem的绝对位置

问题描述 投票:0回答:1

我正在为某些数据构建可视化工具,并希望使用在pyqtgraphs 3D OpenGL组件中绘制的3D球体来表示在提供的数据中确定的目标。

我能够生成球体并使用GLMeshItem.translate()命令移动它们,但是,如果不先通过调用.transform()来获得所述球体的当前位置,就无法找到一种方便的方法来设置球体的坐标。 ),然后从当前位置到我希望将其移动到的新绝对坐标生成一个转换命令。可能那是完成此操作的唯一方法,我只是怀疑还有一个更直接的设置,我似乎无法识别的网格项绝对坐标。

以下代码显示了我正在做的事情的基本框架,以及我正在用来移动球体的当前方法。


from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import pyqtgraph.opengl as gl
import numpy as np

app = QtGui.QApplication([])
w = gl.GLViewWidget()
w.showMaximized()
w.setWindowTitle('pyqtgraph example: GLMeshItem')
w.setCameraPosition(distance=40)

g = gl.GLGridItem()
g.scale(2,2,1)
w.addItem(g)

verts = np.array([
    [0, 0, 0],
    [2, 0, 0],
    [1, 2, 0],
    [1, 1, 1],
])
faces = np.array([
    [0, 1, 2],
    [0, 1, 3],
    [0, 2, 3],
    [1, 2, 3]
])
colors = np.array([
    [1, 0, 0, 0.3],
    [0, 1, 0, 0.3],
    [0, 0, 1, 0.3],
    [1, 1, 0, 0.3]
])


md = gl.MeshData.sphere(rows=4, cols=4)

colors = np.ones((md.faceCount(), 4), dtype=float)
colors[::2,0] = 0
colors[:,1] = np.linspace(0, 1, colors.shape[0])
md.setFaceColors(colors)
m3 = gl.GLMeshItem(meshdata=md, smooth=False)#, shader='balloon')
w.addItem(m3)

target = gl.MeshData.sphere(4,4,10)
targetMI = gl.GLMeshItem(meshdata = target, drawFaces = True,smooth = False)
w.addItem(targetMI)
while(1):
    targetMI.translate(0.1,0,0)
    app.processEvents()



## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

在此示例中可以看到。平移适用于相对于当前位置的移动。我只是想知道是否存在一种方法可以在GLMeshItem(在这种情况下为targetMI)上进行绝对位置移动,这样我就可以使其移动到坐标而不必先获取变换然后进行计算移动到所需坐标所需的平移。

python opengl pyqtgraph
1个回答
0
投票

[一种选择是通过identity将项目的变换重置为resetTransform()变换,然后再通过resetTransform()设置绝对位置。例如:

translate()
© www.soinside.com 2019 - 2024. All rights reserved.