LJ/examples/python/sphere.py

231 lines
5.2 KiB
Python

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
'''
Points around a sphere
v0.1.0
Basic run
- to lasers :
python3 generators/sphere.py | python3 exports/toRedis.py -v
- to LJ nano simulator :
python3 generators/sphere.py | python3 exports/tonano.py
Open/reload www/simulocal.html in a browser.
Licensed under GNU GPLv3
by cocoa, Sam Neurohack
'''
import sys
import traceback
import argparse
import time
import numpy as np
import math, random
import redis
from operator import itemgetter
argsparser = argparse.ArgumentParser(description="3D sphere")
argsparser.add_argument("-f","--fps",help="Frame Per Second (30)",default=30,type=int)
argsparser.add_argument("-s","--speed",help="point per frame progress",default=3,type=int)
argsparser.add_argument("-v","--verbose",action="store_true",default="True",help="Verbose output")
argsparser.add_argument("-i","--ip",help="IP address of the Redis server (127.0.0.1)",default="127.0.0.1",type=str)
argsparser.add_argument("-p","--port",help="Port of the Redis server (6379)",default="6379",type=str)
argsparser.add_argument("-k","--key",help="Redis key to update, default (/pl/0/0)",default="/pl/0/0",type=str)
args = argsparser.parse_args()
fps=args.fps
looptime = 1 / fps
verbose=args.verbose
ip = args.ip
port = args.port
key = args.key
# lhc variables Line style
lhcincspeed = 10
lhccurrentspeed = 25
lhclightspeed = 300
# lhc variables Circle style
lhcincspeed = 0.2
lhccurrentspeed = 11
lhclightspeed = 21
lhccirclesteps = 15
lhcradiusL = 250
lhcradiusR = 300
width = 600
height = 600
centerX = width / 2
centerY = height / 2
# 3D to 2D projection parameters
fov = 256
viewer_distance = 2.2
def rgb2int(rgb):
return int('0x%02x%02x%02x' % tuple(rgb),0)
# Useful variables init.
white = rgb2int((255,255,255))
red = rgb2int((255,0,0))
blue = rgb2int((0,0,255))
color = 65280
L = [[100.0, 150.0, 0], [120.0, 150.0, 5], [140.0, 150.0, 10]]
R = [[460.0, 240.0, 350], [480.0, 240.0, 355], [500.0, 240.0, 360]]
particles = [L[0], L[1], L[2], R[0], R[1], R[2], R[2], R[1], R[0], L[2], L[1], L[0]]
circlepart = [[0.,lhcradiusL,color,0.,0.],[5.,lhcradiusL,color,0.,0.],[ 10.,lhcradiusL,color,0.,0.],[40. ,lhcradiusL,0,0.,0.], [80.,lhcradiusL,0,0.,0.],[120., lhcradiusL, 0,0.,0.],[160. ,lhcradiusL, 0,0.,0.],[200.,lhcradiusL,0,0.,0.],[240.,lhcradiusL,0,0.,0.],[280.,lhcradiusL,0,0.,0.],[320.,lhcradiusL,0,0.,0.],[350,lhcradiusR,color,0.,0.],[355,lhcradiusR,color,0.,0.],[360.,lhcradiusR,color,0.,0.] ]
def startFrame():
return time.time()
def endFrame(timer):
if not looptime :
debug( "No looptime provided at init.")
return
elapsed = time.time() - timer
if( elapsed < looptime ):
delta = looptime - elapsed
time.sleep( delta )
def msNow():
return time.time()
def debug(text):
if verbose:
print(text)
# use rad
def Proj3D(coord,angleX=0,angleY=0, angleZ=0):
#cli.debug(coord)
x = coord[0] #+ transx
y = coord[1] #+ transy
z = coord[2] + transz
cosa = math.cos(angleX)
sina = math.sin(angleX)
y2 = y
y = y2 * cosa - z * sina
z = y2 * sina + z * cosa
cosa = math.cos(angleY)
sina = math.sin(angleY)
z2 = z
z = z2 * cosa - x * sina
x = z2 * sina + x * cosa
cosa = math.cos(angleZ)
sina = math.sin(angleZ)
x2 = x
x = x2 * cosa - y * sina
y = x2 * sina + y * cosa
return x,y
def Circle(radius,angle):
rad = angle * math.pi / 180
x = radius * math.cos(rad)
y = radius * math.sin(rad)
return x,y
def phase_lhc():
global lhccurrentspeed
# cli.debug(L,R)
# Circle edition
for l in L:
l[2] += lhccurrentspeed
if l[2] > 360:
l[2] = 0
lhccurrentspeed += lhcincspeed
l[0],l[1]= Circle(lhcradiusL,l[2])
#cli.debug(l)
# decrement R points
for r in R:
r[2] -= lhccurrentspeed
if r[2] < 0:
r[2] = 360
lhccurrentspeed += lhcincspeed
r[0],r[1]= Circle(lhcradiusR,r[2])
# Optimized Circle edition
for p in circlepart:
# proton point
if p[2] != 0:
# turning CW lhcradiusL
if p[1] == lhcradiusL:
p[0] += lhccurrentspeed
if p[0] > 360:
p[0] = 0
lhccurrentspeed += lhcincspeed
#cli.debug(l)
# turning CCW lhcradiusR
if p[1] == lhcradiusR:
p[0] -= lhccurrentspeed
if p[0] < 0:
p[0] = 360
lhccurrentspeed += lhcincspeed
p[3], p[4] = Circle(p[1],p[0])
circlepl = sorted(circlepart,key=itemgetter(0))
#cli.debug(circlepl)
pl = []
for p in circlepl:
pl.append((p[3]+1, p[4]+1, 0))
pl.append((p[3]+1, p[4]+1, p[2]))
pl.append((p[3], p[4], p[2]))
return pl
if __name__ == "__main__":
r=redis.StrictRedis(host=ip, port=port, db=0)
try:
while True:
timer = startFrame()
pts = str(phase_lhc())
debug(pts)
if r.set(key,pts)==True:
debug("redis set("+pts+") to "+pts)
endFrame(timer)
except Exception:
debug(traceback.print_exc())
except KeyboardInterrupt:
sys.exit(0)
finally:
debug("End")
sys.exit(0)