Files
2026-05-10 05:02:33 +02:00

138 lines
3.5 KiB
Python

from math import ceil
from PIL.ImageChops import offset
if "clip005.avi".endswith(".png"):
raise RuntimeError;
import math
from algorithms import *
from classes import *
import os
import cv2
import random
import numpy as np
FRAME_MULTIPLIER_VIDEO = 10
def renderTomp4(removeImageSource = False):
img_list_unfiltered = os.listdir(OUTPUT_DIR)
img_list =[]
for x in img_list_unfiltered:
if (""+x).endswith(".png"):
img_list.append(x)
# for x in img_list:
# if not x.endswith(".png"):
# print("removed: "+x)
# img_list.remove(x)
# else:
# print("keept: "+x)
#
# print(img_list)
#
img_list.sort()
img_array = []
size = (100,100)
frameindex = 0
for filename in img_list:
frameindex += 1
img = cv2.imread(OUTPUT_DIR + filename)
if(img is None):
print("loadingError: "+filename)
return
height, width, layers = img.shape
size = (width, height)
img_array+=[img]*FRAME_MULTIPLIER_VIDEO
clipIndex = 0
while( os.path.exists(OUTPUT_DIR+f"clip{clipIndex:03d}.avi")):
clipIndex +=1
out = cv2.VideoWriter(OUTPUT_DIR+f"clip{clipIndex:03d}.avi", cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
if not removeImageSource: return
for x in img_list:
os.remove(OUTPUT_DIR + x)
def createRandomSubspace(points:set,size):
pointslist = list(points)
random.shuffle(pointslist)
return set(pointslist[:size])
def createRandomsListSequence(points:set):#, garages:set
pointslist = list(points)*5
if len(pointslist) < 50:
pointslist = pointslist * int(ceil(50/len(pointslist))+1)
random.shuffle(pointslist)
cars = pointslist[:50]
#gar = list(garages)
#garageOrder =[]
#for x in range(len(cars)):
# random.shuffle(gar)
# garageOrder.append(gar[0])
return cars #(cars,garageOrder)
def main():
# seeded randomness
random.seed(25)
space = MetricSpace.getPopulatedSpace(10,10)
ammountToKeep = 100#random.randint(50,95)
points = createRandomSubspace(space.points,ammountToKeep)
func = space.distancefunction
space = MetricSpace(points,func)
# generate garages
garagesCount = 3#random.randint(5, 10)
garages_locations = createRandomSubspace(points, garagesCount)
capactiyPerGarage = math.ceil(50.0/garagesCount)
#create Garage capacity
garages: List[Garage] = []
for x in garages_locations:
garages.append(Garage(x,capactiyPerGarage))
garages = [Garage(Point(0,0),1),
Garage(Point(5,9),1),
Garage(Point(8,5),1)]
gMngr = GarageManager(garages)
# initiate solving Algorithm
algo : SolvingAlgorithm = SolvingAlgorithm(space,gMngr)
#algo : SolvingAlgorithm = AlgorithmA(space,gMngr)
# sequence = createRandomsListSequence(points.difference(garages),garages) squence without garages
sequence = createRandomsListSequence(points)
#sequence = createRandomsListSequence(garages) # For algo A
sequence = [Car(Point(5,5),0),
Car(Point(9,0),1),
Car(Point(1,1),2)]
fmg = frameGenerator(space,gMngr)
fmg.renderFrame()
for car in sequence:
fmg.spawnCar(car)
g = algo.getGarageForCar(car)
fmg.add_transition(g)
renderTomp4()
print("Traveled: "+str(algo.amountTraveled))
main()