"""Lightweight H* compatible panorama interface, no Habitat dependency. The panorama is private environment state. Only rendered perspective images and public instructions/pose should be sent to a policy. Angles are degrees. """ import math, re import cv2 import numpy as np HFOV=100.0 SYSTEM = ('You perform visual search from a fixed position. You see only the current ' 'perspective image. Rotate to gather evidence, then center the requested object ' 'or face the requested traversable path. Take one action per turn: ' 'rotate(delta_yaw,delta_pitch) or ' 'submit(current_yaw,current_pitch). Use integer degrees. ' 'Positive yaw turns right; positive pitch looks up. Submit the CURRENT pose. ' 'Do not infer that an unseen target is already visible.') def circular_delta(target, current): return (target-current+180)%360-180 def perspective(pano, yaw, pitch, height=360, fov=HFOV, crosshair=True): """Same spherical convention as Hstar: center longitude zero at W/2.""" width=height//9*16 focal=width/(2*math.tan(math.radians(fov)/2)) v,u=np.indices((height,width),dtype=np.float32) rays=np.stack(((u-width/2)/focal,(v-height/2)/focal,np.ones_like(u)),axis=-1) rays/=np.linalg.norm(rays,axis=-1,keepdims=True) a,b=math.radians(yaw),math.radians(pitch) x,y,z=rays[...,0],rays[...,1],rays[...,2] yp=y*math.cos(b)-z*math.sin(b);zp=y*math.sin(b)+z*math.cos(b) xp=x*math.cos(a)+zp*math.sin(a);zp=-x*math.sin(a)+zp*math.cos(a) longitude=np.arctan2(xp,zp);latitude=np.arcsin(np.clip(yp,-1,1)) ph,pw=pano.shape[:2] mx=((longitude/(2*np.pi)+.5)*pw).astype(np.float32) my=((latitude/np.pi+.5)*ph).astype(np.float32) # wrap only longitude; avoid pole-to-pole interpolation across vertical edge. result=cv2.remap(pano,mx%pw,np.clip(my,0,ph-1),cv2.INTER_LINEAR,borderMode=cv2.BORDER_WRAP) if crosshair: cx,cy=width//2,height//2 cv2.line(result,(cx-8,cy),(cx+8,cy),(0,255,0),1) cv2.line(result,(cx,cy-8),(cx,cy+8),(0,255,0),1) return result def bbox_angles(bbox,width,height): x1,y1,x2,y2=map(float,bbox) if not (0<=x10)&(np.abs(cx)<=cz*tanx)&(np.abs(cy)<=cz*tany) weights=np.cos(lat) return float(weights[inside].sum()/weights.sum()) def in_interval(value,interval,circular=False,tolerance=0): lo,hi=interval if circular: span=(hi-lo)%360;center=(lo+span/2)%360 return abs(circular_delta(value,center))<=max(span/2,tolerance)+1e-6 return abs(value-(lo+hi)/2)<=max((hi-lo)/2,tolerance)+1e-6 class SearchEnv: def __init__(self,pano,annotation,start_yaw=0,height=360): self.pano=pano;self.annotation=annotation;self.yaw=float(start_yaw)%360 self.pitch=0.;self.height=height;self.done=False;self.steps=0 def observe(self): return perspective(self.pano,self.yaw,self.pitch,self.height) def public_text(self): return ('\nTask: '+self.annotation['task']+ f'\nCurrent direction (yaw,pitch): ({self.yaw:g},{self.pitch:g}). Decide your next action.') def step(self,text): if self.done:raise ValueError('episode already ended') match=re.fullmatch(r'\s*(rotate|submit)\((-?\d+),\s*(-?\d+)\)\s*',text) if not match:raise ValueError('one integer rotate or submit action is required') op,a,b=match[1],int(match[2]),int(match[3]);self.steps+=1 if op=='rotate': self.yaw=(self.yaw+a)%360;self.pitch=max(-90,min(90,self.pitch+b)) return {'done':False,'pose':[self.yaw,self.pitch]} if abs(circular_delta(a,self.yaw))>.51 or abs(b-self.pitch)>.51: raise ValueError('submit must name the actual current pose; teleport submission rejected') if 'yaw' not in self.annotation: self.done=True return {'done':True,'success':None,'evaluated':False,'pose':[self.yaw,self.pitch]} is_hos='pitch' in self.annotation success=in_interval(self.yaw,self.annotation['yaw'],True,30 if is_hos else 10) if is_hos:success=success and in_interval(self.pitch,self.annotation['pitch'],False,20) self.done=True return {'done':True,'success':bool(success),'pose':[self.yaw,self.pitch]}