LJnano/clitools/generators/tunnel.py
2020-10-25 20:18:56 +01:00

195 lines
6.1 KiB
Python
Executable File

#!/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]
]
circle = [[1,0],
[0.9238795325112867,0.3826834323650898],
[0.7071067811865476,0.7071067811865475],
[0.38268343236508984,0.9238795325112867],
[0,1.0],
[-0.3826834323650897,0.9238795325112867],
[-0.7071067811865475,0.7071067811865476],
[-0.9238795325112867,0.3826834323650899],
[-1.0,0],
[-0.9238795325112868,-0.38268343236508967],
[-0.7071067811865477,-0.7071067811865475],
[-0.38268343236509034,-0.9238795325112865],
[0,-1.0],
[0.38268343236509,-0.9238795325112866],
[0.707106781186548,-0.707106781186547],
[0.9238795325112872,-0.3826834323650887],
[1,0]]
shape = circle
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))