panellum_generator/panoconfig.py

147 lines
4.6 KiB
Python
Raw Normal View History

2023-02-01 11:13:40 +00:00
import math
import argparse
#old
2023-02-01 11:13:40 +00:00
#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
2023-02-01 11:13:40 +00:00
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]
2023-02-01 11:13:40 +00:00
output_scenes=[]
first_sceneId=None
for iline1,line1 in enumerate(linesinput):
if (linesmask[iline1]==False):
continue
2023-02-01 11:13:40 +00:00
print("Parsing: line1")
fromID,toID,pitch,yaw,targetYaw=line1.split(',')
#if (targetYaw=='i'):
# targetYaw=0
#pitch,yaw,targetYaw=(int(pitch),int(yaw),int(targetYaw))
2023-02-01 11:13:40 +00:00
if first_sceneId is None:
first_sceneId=fromID #take first entry as start scene
2023-02-01 11:13:40 +00:00
# ### 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]
2023-02-01 11:13:40 +00:00
output_hotspots=[]
for iline2,line2 in enumerate(linesinput):
if (linesmask[iline2]==False):
continue
2023-02-01 11:13:40 +00:00
fromID2,toID2,pitch2,yaw2,targetYaw2=line2.split(',')
if fromID==fromID2:
linesmask[iline2]=False
2023-02-01 11:13:40 +00:00
print()
if (targetYaw2=='i'):
targetYaw2=0
for iline3,line3 in enumerate(linesinput):
2024-09-03 17:49:19 +00:00
assert len(line3.split(','))==5, "not enough values in line "+str(iline3+1)+" : "+str(line3.split(','))
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))
2023-02-01 11:13:40 +00:00
# ### 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]
2023-02-01 11:13:40 +00:00
output_hotspots.append(current_hotspot)
2023-02-01 11:13:40 +00:00
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)