2012-03-09 17:38:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-11-23 22:34:22 +00:00
|
|
|
"""
|
|
|
|
Simple examples demonstrating the use of GLMeshItem.
|
2012-03-09 17:38:15 +00:00
|
|
|
|
2012-11-23 22:34:22 +00:00
|
|
|
"""
|
2012-03-09 17:38:15 +00:00
|
|
|
|
|
|
|
## Add path to library (just for examples; you do not need this)
|
2012-12-26 22:51:52 +00:00
|
|
|
import initExample
|
2012-03-09 17:38:15 +00:00
|
|
|
|
|
|
|
from pyqtgraph.Qt import QtCore, QtGui
|
|
|
|
import pyqtgraph as pg
|
|
|
|
import pyqtgraph.opengl as gl
|
|
|
|
|
2021-01-27 18:42:38 +00:00
|
|
|
app = QtGui.QApplication([])
|
2012-03-09 17:38:15 +00:00
|
|
|
w = gl.GLViewWidget()
|
|
|
|
w.show()
|
2013-02-25 04:09:03 +00:00
|
|
|
w.setWindowTitle('pyqtgraph example: GLMeshItem')
|
2012-11-23 22:34:22 +00:00
|
|
|
w.setCameraPosition(distance=40)
|
|
|
|
|
2012-03-09 17:38:15 +00:00
|
|
|
g = gl.GLGridItem()
|
|
|
|
g.scale(2,2,1)
|
|
|
|
w.addItem(g)
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2012-11-23 22:34:22 +00:00
|
|
|
|
|
|
|
## Example 1:
|
|
|
|
## Array of vertex positions and array of vertex indexes defining faces
|
|
|
|
## Colors are specified per-face
|
|
|
|
|
|
|
|
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]
|
|
|
|
])
|
|
|
|
|
|
|
|
## Mesh item will automatically compute face normals.
|
|
|
|
m1 = gl.GLMeshItem(vertexes=verts, faces=faces, faceColors=colors, smooth=False)
|
|
|
|
m1.translate(5, 5, 0)
|
|
|
|
m1.setGLOptions('additive')
|
|
|
|
w.addItem(m1)
|
|
|
|
|
2014-03-11 03:04:10 +00:00
|
|
|
|
2012-11-23 22:34:22 +00:00
|
|
|
## Example 2:
|
|
|
|
## Array of vertex positions, three per face
|
2014-03-11 03:04:10 +00:00
|
|
|
verts = np.empty((36, 3, 3), dtype=np.float32)
|
|
|
|
theta = np.linspace(0, 2*np.pi, 37)[:-1]
|
|
|
|
verts[:,0] = np.vstack([2*np.cos(theta), 2*np.sin(theta), [0]*36]).T
|
|
|
|
verts[:,1] = np.vstack([4*np.cos(theta+0.2), 4*np.sin(theta+0.2), [-1]*36]).T
|
|
|
|
verts[:,2] = np.vstack([4*np.cos(theta-0.2), 4*np.sin(theta-0.2), [1]*36]).T
|
|
|
|
|
2012-11-23 22:34:22 +00:00
|
|
|
## Colors are specified per-vertex
|
|
|
|
colors = np.random.random(size=(verts.shape[0], 3, 4))
|
2014-03-11 03:04:10 +00:00
|
|
|
m2 = gl.GLMeshItem(vertexes=verts, vertexColors=colors, smooth=False, shader='balloon',
|
|
|
|
drawEdges=True, edgeColor=(1, 1, 0, 1))
|
2012-11-23 22:34:22 +00:00
|
|
|
m2.translate(-5, 5, 0)
|
|
|
|
w.addItem(m2)
|
|
|
|
|
|
|
|
|
2014-03-11 03:04:10 +00:00
|
|
|
|
2012-11-23 22:34:22 +00:00
|
|
|
## Example 3:
|
2014-02-02 01:46:05 +00:00
|
|
|
## sphere
|
2012-11-23 22:34:22 +00:00
|
|
|
|
|
|
|
md = gl.MeshData.sphere(rows=10, cols=20)
|
|
|
|
#colors = np.random.random(size=(md.faceCount(), 4))
|
|
|
|
#colors[:,3] = 0.3
|
|
|
|
#colors[100:] = 0.0
|
|
|
|
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')
|
|
|
|
|
2014-03-11 03:04:10 +00:00
|
|
|
m3.translate(5, -5, 0)
|
2012-11-23 22:34:22 +00:00
|
|
|
w.addItem(m3)
|
|
|
|
|
|
|
|
|
2013-05-29 18:33:14 +00:00
|
|
|
# Example 4:
|
|
|
|
# wireframe
|
|
|
|
|
|
|
|
md = gl.MeshData.sphere(rows=4, cols=8)
|
|
|
|
m4 = gl.GLMeshItem(meshdata=md, smooth=False, drawFaces=False, drawEdges=True, edgeColor=(1,1,1,1))
|
|
|
|
m4.translate(0,10,0)
|
|
|
|
w.addItem(m4)
|
|
|
|
|
2014-02-02 01:46:05 +00:00
|
|
|
# Example 5:
|
|
|
|
# cylinder
|
|
|
|
md = gl.MeshData.cylinder(rows=10, cols=20, radius=[1., 2.0], length=5.)
|
|
|
|
md2 = gl.MeshData.cylinder(rows=10, cols=20, radius=[2., 0.5], length=10.)
|
|
|
|
colors = np.ones((md.faceCount(), 4), dtype=float)
|
|
|
|
colors[::2,0] = 0
|
|
|
|
colors[:,1] = np.linspace(0, 1, colors.shape[0])
|
|
|
|
md.setFaceColors(colors)
|
|
|
|
m5 = gl.GLMeshItem(meshdata=md, smooth=True, drawEdges=True, edgeColor=(1,0,0,1), shader='balloon')
|
|
|
|
colors = np.ones((md.faceCount(), 4), dtype=float)
|
|
|
|
colors[::2,0] = 0
|
|
|
|
colors[:,1] = np.linspace(0, 1, colors.shape[0])
|
|
|
|
md2.setFaceColors(colors)
|
|
|
|
m6 = gl.GLMeshItem(meshdata=md2, smooth=True, drawEdges=False, shader='balloon')
|
|
|
|
m6.translate(0,0,7.5)
|
2013-05-29 18:33:14 +00:00
|
|
|
|
2014-02-02 01:46:05 +00:00
|
|
|
m6.rotate(0., 0, 1, 1)
|
|
|
|
#m5.translate(-3,3,0)
|
|
|
|
w.addItem(m5)
|
|
|
|
w.addItem(m6)
|
2013-05-29 18:33:14 +00:00
|
|
|
|
2012-11-23 22:34:22 +00:00
|
|
|
|
|
|
|
|
2012-03-09 17:38:15 +00:00
|
|
|
|
2012-11-23 22:34:22 +00:00
|
|
|
|
2012-03-09 17:38:15 +00:00
|
|
|
|
|
|
|
## Start Qt event loop unless running in interactive mode.
|
2012-12-05 05:25:45 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
|
|
|
|
QtGui.QApplication.instance().exec_()
|