我正在为某些数据构建可视化工具,并希望使用在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
),这样我就可以使它移动到坐标而不必先获取变换,然后计算移动到所需的平移所需的坐标。
一种选择是,在通过设置绝对位置之前,通过将项的变换重置为恒等变换。例如:resetTransform()
translate()
targetMI.resetTransform()
targetMI.translate(10, 0, 0)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句