LJ/clitools/generators/tunnel.py

177 lines
5.5 KiB
Python

#!/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
import math
import random
import sys
import time
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)
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)
argsparser.add_argument("-m","--max-size",help="maximum size for objects",default=400,type=int)
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)
argsparser.add_argument("-v","--verbose",action="store_true",help="Verbose output")
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)
args = argsparser.parse_args()
centerX = args.centerX
centerY = args.centerY
color = args.color
fps = args.fps
interval = args.interval
max_size = args.max_size
randomize = args.randomize
speed = args.speed
verbose = args.verbose
origSpeed = speed
optimal_looptime = 1 / fps
square = [
[-1,1],
[1,1],
[1,-1],
[-1,-1],
[-1,1]
]
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 ) )
class polylineGenerator( object ):
def __init__( self ):
self.polylineList = [[0,[currentCenter[0],currentCenter[1]]]]
self.buf = []
def init(self):
finished = False
while not finished:
finished = self.increment()
debug(name,"init done:{}".format(self.polylineList))
def draw( self ):
self.buf = []
for it_pl, infoList in enumerate(self.polylineList):
size = infoList[0]
center = infoList[1]
for it_sqr, point in enumerate(shape):
x = int( center[0] + point[0]*size )
y = int( center[1] + point[1]*size )
# Add an invisible point in first location
if 0 == it_sqr:
self.buf.append([x,y,0])
self.buf.append([x,y,color])
#debug( name, "buf size:", str(len(self.buf)) )
return self.buf
def increment(self):
global speed
self.buffer = []
min_size = 9999
delList = []
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
#debug(name, "currentCenter:{} speed:{}".format(currentCenter,speed))
for i, shapeInfo in enumerate(self.polylineList):
size = shapeInfo[0]
# 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)
if size < min_size : min_size = size
if size > max_size : delList.append(i)
self.polylineList[i][0] = size
for i in delList:
del self.polylineList[i]
#debug(name, "polyline:",self.polylineList)
if min_size >= interval:
debug(name, "new shape")
self.polylineList.append([0,[currentCenter[0],currentCenter[1]]])
# Return True if we delete a shape
if len(delList):
return True
return False
pgen = polylineGenerator()
pgen.init()
while True:
start = time.time()
# Generate
pgen.increment()
# send
pl = pgen.draw()
print(pl, flush=True)
#debug(name,"output:{}".format(pl))
looptime = time.time() - start
if( looptime < optimal_looptime ):
time.sleep( optimal_looptime - looptime)
#debug(name+" micro sleep:"+str( optimal_looptime - looptime))