-
-
Save mrabault/788f97205860390e0899766a3c264601 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" Original code: from vispy """ | |
import threading | |
import sys | |
import pyquaternion | |
import numpy as np | |
import PyQt5.QtGui as qt | |
import pyqtgraph.opengl as ogl | |
# golden ratio | |
PHI = (1.0 + np.sqrt(5.0))/2.0 | |
def icosahedron(radius): | |
""" Generate an icosahedron with a given radius """ | |
# vertices of a icosahedron | |
verts = [(-1, PHI, 0), | |
(1, PHI, 0), | |
(-1, -PHI, 0), | |
(1, -PHI, 0), | |
(0, -1, PHI), | |
(0, 1, PHI), | |
(0, -1, -PHI), | |
(0, 1, -PHI), | |
(PHI, 0, -1), | |
(PHI, 0, 1), | |
(-PHI, 0, -1), | |
(-PHI, 0, 1)] | |
# faces of the icosahedron | |
faces = [(0, 11, 5), | |
(0, 5, 1), | |
(0, 1, 7), | |
(0, 7, 10), | |
(0, 10, 11), | |
(1, 5, 9), | |
(5, 11, 4), | |
(11, 10, 2), | |
(10, 7, 6), | |
(7, 1, 8), | |
(3, 9, 4), | |
(3, 4, 2), | |
(3, 2, 6), | |
(3, 6, 8), | |
(3, 8, 9), | |
(4, 9, 5), | |
(2, 4, 11), | |
(6, 2, 10), | |
(8, 6, 7), | |
(9, 8, 1)] | |
verts = np.array(verts) | |
faces = np.array(faces) | |
# make each vertex lie on the sphere | |
lengths = np.sqrt((verts*verts).sum(axis=1)) | |
verts /= lengths[:, np.newaxis]/radius | |
return verts, faces | |
class RotThread(threading.Thread): | |
""" Reads quaternions from stdin and use them to set the cube's orientation """ | |
def __init__(self, ico): | |
super().__init__() | |
self.ico = ico | |
def run(self): | |
for line in sys.stdin: | |
try: | |
quat = pyquaternion.Quaternion(list(map(float, line.strip().split(',')))) | |
self.ico.resetTransform() | |
self.ico.rotate(quat.degrees, quat.axis[0], quat.axis[1], quat.axis[2]) | |
self.ico.update() | |
except ValueError: | |
print("Invalid input") | |
def run_sim(): | |
""" Run the simulator """ | |
verts, faces = icosahedron(100) | |
app = qt.QApplication([]) | |
widget = ogl.GLViewWidget() | |
widget.show() | |
widget.setWindowTitle('bouLED simulator') | |
widget.setCameraPosition(distance=440) | |
grid = ogl.GLGridItem(color=(1, 1, 1, 1), glOptions='opaque') | |
grid.scale(200, 200, 200) | |
widget.addItem(grid) | |
axes = ogl.GLAxisItem(glOptions='opaque') | |
axes.setSize(300, 300, 300) | |
widget.addItem(axes) | |
colors = np.random.random(size=(faces.shape[0], 4)) | |
mesh = ogl.MeshData(vertexes=verts, faces=faces, faceColors=colors) | |
ico = ogl.GLMeshItem(meshdata=mesh, smooth=False) | |
widget.addItem(ico) | |
rot_thread = RotThread(ico) | |
rot_thread.setDaemon(True) | |
rot_thread.start() | |
app.exec_() | |
if __name__ == '__main__': | |
run_sim() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment