如何在三维pyqtgraph实现中设置GLMeshItem的绝对位置



我正在为一些数据构建一个可视化工具,并希望使用Pyqtgraph 3D OpenGL组件中绘制的3D球体来表示所提供数据中识别的目标。

我能够生成球体并使用CCD_ 1命令移动它们,然而,如果不首先通过调用.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()设置绝对位置之前,通过resetTransform()将项的转换重置为身份转换。例如:

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

最新更新