Aluode/PerceptionLabPortable
0
1"""2Simple examples demonstrating the use of GLMeshItem.3"""4import sys5 6import pyqtgraph as pg7from pyqtgraph.Qt import QtGui8import pyqtgraph.opengl as gl9 10if 'darwin' in sys.platform:11 fmt = QtGui.QSurfaceFormat()12 fmt.setRenderableType(fmt.RenderableType.OpenGL)13 fmt.setProfile(fmt.OpenGLContextProfile.CoreProfile)14 fmt.setVersion(4, 1)15 QtGui.QSurfaceFormat.setDefaultFormat(fmt)16 17app = pg.mkQApp("GLMeshItem Example")18w = gl.GLViewWidget()19w.show()20w.setWindowTitle('pyqtgraph example: GLMeshItem')21w.setCameraPosition(distance=40)22 23g = gl.GLGridItem()24g.scale(2,2,1)25w.addItem(g)26 27import numpy as np28 29## Example 1:30## Array of vertex positions and array of vertex indexes defining faces31## Colors are specified per-face32 33verts = np.array([34 [0, 0, 0],35 [2, 0, 0],36 [1, 2, 0],37 [1, 1, 1],38])39faces = np.array([40 [0, 1, 2],41 [0, 1, 3],42 [0, 2, 3],43 [1, 2, 3]44])45colors = np.array([46 [1, 0, 0, 0.3],47 [0, 1, 0, 0.3],48 [0, 0, 1, 0.3],49 [1, 1, 0, 0.3]50])51 52## Mesh item will automatically compute face normals.53m1 = gl.GLMeshItem(vertexes=verts, faces=faces, faceColors=colors, smooth=False)54m1.translate(5, 5, 0)55m1.setGLOptions('additive')56w.addItem(m1)57 58 59## Example 2:60## Array of vertex positions, three per face61verts = np.empty((36, 3, 3), dtype=np.float32)62theta = np.linspace(0, 2*np.pi, 37)[:-1]63verts[:,0] = np.vstack([2*np.cos(theta), 2*np.sin(theta), [0]*36]).T64verts[:,1] = np.vstack([4*np.cos(theta+0.2), 4*np.sin(theta+0.2), [-1]*36]).T65verts[:,2] = np.vstack([4*np.cos(theta-0.2), 4*np.sin(theta-0.2), [1]*36]).T66 67## Colors are specified per-vertex68colors = np.random.random(size=(verts.shape[0], 3, 4))69m2 = gl.GLMeshItem(vertexes=verts, vertexColors=colors, smooth=False, shader='balloon', 70 drawEdges=True, edgeColor=(1, 1, 0, 1))71m2.translate(-5, 5, 0)72w.addItem(m2)73 74 75 76## Example 3:77## sphere78 79md = gl.MeshData.sphere(rows=10, cols=20)80#colors = np.random.random(size=(md.faceCount(), 4))81#colors[:,3] = 0.382#colors[100:] = 0.083colors = np.ones((md.faceCount(), 4), dtype=np.float32)84colors[::2,0] = 085colors[:,1] = np.linspace(0, 1, colors.shape[0])86md.setFaceColors(colors)87m3 = gl.GLMeshItem(meshdata=md, smooth=False)88 89m3.translate(5, -5, 0)90w.addItem(m3)91 92 93# Example 4:94# wireframe95 96md = gl.MeshData.sphere(rows=4, cols=8)97m4 = gl.GLMeshItem(meshdata=md, smooth=False, drawFaces=False, drawEdges=True, edgeColor=(1,1,1,1))98m4.translate(0,10,0)99w.addItem(m4)100 101# Example 5:102# cylinder103md = gl.MeshData.cylinder(rows=10, cols=20, radius=[1., 2.0], length=5.)104md2 = gl.MeshData.cylinder(rows=10, cols=20, radius=[2., 0.5], length=10.)105colors = np.ones((len(md.vertexes()), 4), dtype=np.float32)106colors[::2,0] = 0107colors[:,1] = np.linspace(0, 1, colors.shape[0])108md.setVertexColors(colors)109m5 = gl.GLMeshItem(meshdata=md, smooth=True, drawEdges=True, edgeColor=(1,0,0,1))110colors = np.ones((len(md2.vertexes()), 4), dtype=np.float32)111colors[::2,0] = 0112colors[:,1] = np.linspace(0, 1, colors.shape[0])113md2.setVertexColors(colors)114m6 = gl.GLMeshItem(meshdata=md2, smooth=True, drawEdges=False)115m6.translate(0,0,7.5)116 117m6.rotate(0., 0, 1, 1)118#m5.translate(-3,3,0)119w.addItem(m5)120w.addItem(m6)121 122if __name__ == '__main__':123 pg.exec()124 