2020-09-27 08:37:52 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# -*- mode: Python -*-
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
Woooh! I'm progressing in a tunnel !
|
|
|
|
v0.1.0
|
|
|
|
|
|
|
|
Use it to test your filters and outputs
|
|
|
|
|
|
|
|
LICENCE : CC
|
|
|
|
|
|
|
|
by cocoa
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import argparse
|
2020-09-30 17:52:31 +00:00
|
|
|
import math
|
|
|
|
import random
|
2020-09-27 08:37:52 +00:00
|
|
|
import sys
|
2020-09-30 17:52:31 +00:00
|
|
|
import time
|
2020-09-27 08:37:52 +00:00
|
|
|
name="generator::tunnel"
|
|
|
|
|
|
|
|
def debug(*args, **kwargs):
|
|
|
|
if( verbose == False ):
|
|
|
|
return
|
|
|
|
print(*args, file=sys.stderr, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
argsparser = argparse.ArgumentParser(description="tunnel generator")
|
|
|
|
argsparser.add_argument("-c","--color",help="Color",default=65280,type=int)
|
2020-09-30 17:52:31 +00:00
|
|
|
argsparser.add_argument("-f","--fps",help="Frame Per Second",default=30,type=int)
|
|
|
|
argsparser.add_argument("-i","--interval",help="point per shape interval",default=30,type=int)
|
2020-10-03 15:50:28 +00:00
|
|
|
argsparser.add_argument("-m","--max-size",help="maximum size for objects",default=400,type=int)
|
2020-09-30 17:52:31 +00:00
|
|
|
argsparser.add_argument("-r","--randomize",help="center randomization",default=5,type=int)
|
|
|
|
argsparser.add_argument("-s","--speed",help="point per frame progress",default=3,type=int)
|
2020-09-27 08:37:52 +00:00
|
|
|
argsparser.add_argument("-v","--verbose",action="store_true",help="Verbose output")
|
2020-09-30 17:52:31 +00:00
|
|
|
argsparser.add_argument("-x","--centerX",help="geometrical center X position",default=400,type=int)
|
|
|
|
argsparser.add_argument("-y","--centerY",help="geometrical center Y position",default=400,type=int)
|
2020-09-27 08:37:52 +00:00
|
|
|
|
|
|
|
args = argsparser.parse_args()
|
|
|
|
centerX = args.centerX
|
|
|
|
centerY = args.centerY
|
2020-09-30 17:52:31 +00:00
|
|
|
color = args.color
|
|
|
|
fps = args.fps
|
2020-09-27 08:37:52 +00:00
|
|
|
interval = args.interval
|
|
|
|
max_size = args.max_size
|
2020-09-30 17:52:31 +00:00
|
|
|
randomize = args.randomize
|
2020-09-27 08:37:52 +00:00
|
|
|
speed = args.speed
|
|
|
|
verbose = args.verbose
|
|
|
|
|
2020-09-30 17:52:31 +00:00
|
|
|
origSpeed = speed
|
2020-09-27 08:37:52 +00:00
|
|
|
optimal_looptime = 1 / fps
|
|
|
|
square = [
|
|
|
|
[-1,1],
|
|
|
|
[1,1],
|
|
|
|
[1,-1],
|
|
|
|
[-1,-1],
|
|
|
|
[-1,1]
|
|
|
|
]
|
|
|
|
|
2020-09-30 17:52:31 +00:00
|
|
|
shape = square
|
|
|
|
currentCenter = [centerX, centerY]
|
|
|
|
centerVector= [0,0]
|
|
|
|
# tweak random basis
|
|
|
|
if randomize % 2 == 1:
|
|
|
|
randomize += 1
|
|
|
|
debug(name,"randomize:{}".format(randomize))
|
|
|
|
centerRand = int(math.sqrt(randomize) / 4 ) + 1
|
|
|
|
debug( name, "centerRand:{}".format(centerRand ) )
|
2020-09-27 08:37:52 +00:00
|
|
|
class polylineGenerator( object ):
|
|
|
|
|
|
|
|
def __init__( self ):
|
2020-09-30 17:52:31 +00:00
|
|
|
self.polylineList = [[0,[currentCenter[0],currentCenter[1]]]]
|
2020-09-27 08:37:52 +00:00
|
|
|
self.buf = []
|
|
|
|
|
2020-10-03 15:50:28 +00:00
|
|
|
def init(self):
|
|
|
|
finished = False
|
|
|
|
while not finished:
|
|
|
|
finished = self.increment()
|
|
|
|
debug(name,"init done:{}".format(self.polylineList))
|
2020-09-27 08:37:52 +00:00
|
|
|
def draw( self ):
|
|
|
|
self.buf = []
|
2020-09-30 17:52:31 +00:00
|
|
|
for it_pl, infoList in enumerate(self.polylineList):
|
|
|
|
size = infoList[0]
|
|
|
|
center = infoList[1]
|
|
|
|
for it_sqr, point in enumerate(shape):
|
2020-10-03 15:50:28 +00:00
|
|
|
x = int( center[0] + point[0]*size )
|
|
|
|
y = int( center[1] + point[1]*size )
|
2020-09-27 08:37:52 +00:00
|
|
|
# Add an invisible point in first location
|
|
|
|
if 0 == it_sqr:
|
|
|
|
self.buf.append([x,y,0])
|
|
|
|
self.buf.append([x,y,color])
|
2020-09-30 17:52:31 +00:00
|
|
|
#debug( name, "buf size:", str(len(self.buf)) )
|
2020-09-27 08:37:52 +00:00
|
|
|
return self.buf
|
|
|
|
|
|
|
|
def increment(self):
|
2020-09-30 17:52:31 +00:00
|
|
|
global speed
|
2020-09-27 08:37:52 +00:00
|
|
|
self.buffer = []
|
|
|
|
min_size = 9999
|
|
|
|
delList = []
|
2020-09-30 17:52:31 +00:00
|
|
|
if randomize :
|
|
|
|
# Change the vector
|
|
|
|
centerVector[0] += random.randrange( -centerRand,centerRand )
|
|
|
|
centerVector[1] += random.randrange( -centerRand,centerRand )
|
|
|
|
# Modify the vector if it is over the limit
|
|
|
|
if currentCenter[0] + centerVector[0] >= centerX + randomize or currentCenter[0] + centerVector[0] <= centerX - randomize:
|
|
|
|
centerVector[0] = 0
|
|
|
|
if currentCenter[1] + centerVector[1] >= centerY + randomize or currentCenter[1] +centerVector[1] <= centerY - randomize:
|
|
|
|
centerVector[1] = 0
|
|
|
|
currentCenter[0] += centerVector[0]
|
|
|
|
currentCenter[1] += centerVector[1]
|
|
|
|
# Change speed
|
|
|
|
speed += int( random.randrange( int(-origSpeed),origSpeed ) )
|
|
|
|
if speed < origSpeed :
|
|
|
|
speed = origSpeed
|
|
|
|
elif speed > (origSpeed + randomize / 2) :
|
|
|
|
speed = origSpeed + randomize / 2
|
2020-10-03 15:50:28 +00:00
|
|
|
#debug(name, "currentCenter:{} speed:{}".format(currentCenter,speed))
|
2020-09-30 17:52:31 +00:00
|
|
|
|
|
|
|
for i, shapeInfo in enumerate(self.polylineList):
|
|
|
|
size = shapeInfo[0]
|
2020-10-03 15:50:28 +00:00
|
|
|
# Augment speed with size
|
|
|
|
"""
|
|
|
|
size = 0 : += sqrt(speed)
|
|
|
|
size = half max size : +=speed
|
|
|
|
|
|
|
|
"""
|
|
|
|
if size < max_size / 4:
|
|
|
|
size += math.pow(speed, 0.1)
|
|
|
|
elif size < max_size / 3:
|
|
|
|
size += math.pow(speed, 0.25)
|
|
|
|
elif size < max_size / 2:
|
|
|
|
size += math.pow(speed, 0.5)
|
|
|
|
else:
|
|
|
|
size += math.pow(speed, 1.25)
|
2020-09-27 08:37:52 +00:00
|
|
|
if size < min_size : min_size = size
|
|
|
|
if size > max_size : delList.append(i)
|
2020-09-30 17:52:31 +00:00
|
|
|
self.polylineList[i][0] = size
|
2020-09-27 08:37:52 +00:00
|
|
|
for i in delList:
|
|
|
|
del self.polylineList[i]
|
2020-09-30 17:52:31 +00:00
|
|
|
#debug(name, "polyline:",self.polylineList)
|
2020-10-03 15:50:28 +00:00
|
|
|
if min_size >= interval:
|
|
|
|
debug(name, "new shape")
|
|
|
|
self.polylineList.append([0,[currentCenter[0],currentCenter[1]]])
|
2020-09-30 17:52:31 +00:00
|
|
|
|
2020-10-03 15:50:28 +00:00
|
|
|
# Return True if we delete a shape
|
|
|
|
|
|
|
|
if len(delList):
|
|
|
|
return True
|
|
|
|
return False
|
2020-09-27 08:37:52 +00:00
|
|
|
|
|
|
|
|
2020-10-03 15:50:28 +00:00
|
|
|
pgen = polylineGenerator()
|
|
|
|
pgen.init()
|
2020-09-27 08:37:52 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
start = time.time()
|
|
|
|
|
|
|
|
# Generate
|
|
|
|
pgen.increment()
|
|
|
|
|
|
|
|
# send
|
2020-09-30 17:52:31 +00:00
|
|
|
pl = pgen.draw()
|
|
|
|
print(pl, flush=True)
|
2020-10-03 15:50:28 +00:00
|
|
|
#debug(name,"output:{}".format(pl))
|
2020-09-27 08:37:52 +00:00
|
|
|
|
|
|
|
looptime = time.time() - start
|
|
|
|
if( looptime < optimal_looptime ):
|
|
|
|
time.sleep( optimal_looptime - looptime)
|
2020-09-30 17:52:31 +00:00
|
|
|
#debug(name+" micro sleep:"+str( optimal_looptime - looptime))
|
2020-09-27 08:37:52 +00:00
|
|
|
|
|
|
|
|