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

How to set absolute position of GLMeshItem in 3D pyqtgraph implementation

我正在为一些数据构建可视化工具,并希望使用在 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_()

在这个例子中可以看出。 translate 可以很好地相对于当前位置移动。我很好奇是否有一种方法可以在 GLMeshItem 上进行绝对位置移动(在本例中为 targetMI),这样我就可以让它移动到一个坐标而不必先获取变换,然后计算移动到所需坐标所需的平移。

一个选项是在通过 translate() 设置绝对位置之前将项目的转换重置为 identity transformation by resetTransform()。例如:

targetMI.resetTransform()
targetMI.translate(10, 0, 0)