panellum_generator/panoconfig.py

146 lines
4.4 KiB
Python

import math
import argparse
#old
#input config file format: x,y,z,id,filename,angleoffset
#angleoffset is 0 when center of 360 image points to the right, positive rotation means image center in pointing more CCW. value in degrees
#new:
#input config file format: FromFile,ToFile,Pitch,Yaw,TargetYaw|i,Text
parser = argparse.ArgumentParser(
prog = 'File Edit Script',
description = 'Read content of text file and write to file',
epilog = '')
parser.add_argument('filename') # positional argument
parser.add_argument('-o', '--output') # option that takes a value
parser.add_argument('-v', '--verbose', action='store_true') # on/off flag
args = parser.parse_args()
print(args.filename, args.output, args.verbose)
def distance(x1,y1,x2,y2):
dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
return dist
print("Loading Templates")
template_panellum=None
with open("template_panellum.txt",'r') as rf:
template_panellum=rf.readlines()
template_scenes=None
with open("template_scenes.txt",'r') as rf:
template_scenes=rf.readlines()
template_hotspots=None
with open("template_hotspots.txt",'r') as rf:
template_hotspots=rf.readlines()
linesinput=None
with open(args.filename,'r') as rf:
linesinput=rf.readlines()
linesinput=[x.rstrip() for x in linesinput] #remove newline and whitespaces at end
linesmask=[True for x in linesinput]
output_scenes=[]
first_sceneId=None
for iline1,line1 in enumerate(linesinput):
if (linesmask[iline1]==False):
continue
print("Parsing: line1")
fromID,toID,pitch,yaw,targetYaw=line1.split(',')
#if (targetYaw=='i'):
# targetYaw=0
#pitch,yaw,targetYaw=(int(pitch),int(yaw),int(targetYaw))
if first_sceneId is None:
first_sceneId=fromID #take first entry as start scene
# ### Set information for scene ###
current_scene=template_scenes
current_scene=[x.replace('<id>',fromID) for x in current_scene]
current_scene=[x.replace('<title>',fromID) for x in current_scene]
current_scene=[x.replace('<filename>',fromID+".jpg") for x in current_scene]
output_hotspots=[]
for iline2,line2 in enumerate(linesinput):
if (linesmask[iline2]==False):
continue
fromID2,toID2,pitch2,yaw2,targetYaw2=line2.split(',')
if fromID==fromID2:
linesmask[iline2]=False
print()
if (targetYaw2=='i'):
targetYaw2=0
for iline3,line3 in enumerate(linesinput):
fromID3,toID3,pitch3,yaw3,targetYaw3=line3.split(',')
if (toID2==fromID3 and fromID2==toID3):
targetYaw2=int(yaw3)-180
if (targetYaw2<-180):
targetYaw2+=360
print("Calculated Target Yaw="+str(targetYaw2))
pitch2,yaw2,targetYaw2=(int(pitch2),int(yaw2),int(targetYaw2))
print("from "+str(fromID2))
print("to "+str(toID2))
# ### Set information for target/hotspot ###
current_hotspot=template_hotspots
current_hotspot=[x.replace('<id>',toID2) for x in current_hotspot]
current_hotspot=[x.replace('<yaw>',str(yaw2)) for x in current_hotspot]
current_hotspot=[x.replace('<pitch>',str(pitch2)) for x in current_hotspot]
current_hotspot=[x.replace('<targetyaw>',str(targetYaw2)) for x in current_hotspot]
output_hotspots.append(current_hotspot)
output_hotspots_string=""
for _ihotspot,_hotspot in enumerate(output_hotspots):
if _ihotspot>0:
output_hotspots_string+=",\n" #separate blocks with comma
for _hotspotline in _hotspot:
output_hotspots_string+=_hotspotline
current_scene=[x.replace('<hotspot>',output_hotspots_string) for x in current_scene]
output_scenes.append(current_scene)
output_scenes_string=""
for _iscene,_scene in enumerate(output_scenes):
if _iscene>0:
output_scenes_string+=",\n" #separate blocks with comma
for _sceneline in _scene:
output_scenes_string+=_sceneline
output=template_panellum
output=[x.replace('<firstScene>',first_sceneId) for x in output]
output=[x.replace('<scene>',output_scenes_string) for x in output]
with open(args.output, 'w') as wf:
wf.writelines(output)