File size: 4,962 Bytes
347d9a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Continuous minimum-jerk motion, swept footprints and query ambiguity checks."""
from __future__ import annotations
import copy,math
import numpy as np
from .common import SR,active,geometry,grid,position
from dynamic_demo.render import visible


def points_clear(points,room,margin=.25):
    points=np.asarray(points);w,h,d=room['room_size_m']
    if not ((np.abs(points[...,0])<w/2-margin)&(np.abs(points[...,2])<d/2-margin)&(points[...,1]>.2)&(points[...,1]<h-.2)).all():return False
    for box in room['surfaces'][6:]:
        if np.any(np.all(np.abs(points[...,[0,2]]-np.asarray(box['location'])[[0,2]])<np.asarray(box['size'])[[0,2]]/2+margin,axis=-1)):return False
    return True


def initial_positions(entities,room,rng,force=False):
    """Keep parent positions when valid; otherwise sample legal body footprints."""
    w,_,d=room['room_size_m'];placed=[];reused=True
    for e in entities:
        point=np.asarray(e['initial_xyz_m']);others=placed
        valid=not force and points_clear(point,room,.35) and all(np.linalg.norm((point-q)[[0,2]])>=.9 for q in others)
        if not valid:
            reused=False
            for _ in range(3000):
                point=np.array([rng.uniform(-w/2+.5,w/2-.5),point[1],rng.uniform(-d/2+.5,d/2-.5)])
                if points_clear(point,room,.35) and all(np.linalg.norm((point-q)[[0,2]])>=.9 for q in others):break
            else:raise ValueError('Cannot place entities in this room')
        e['initial_xyz_m']=point.tolist();placed.append(point)
    return reused


def range_queries(scene):
    queries=[]
    for human in scene['humans']:
        anchor=next(s/SR for s in grid(scene) if active(human,s))
        for receiver in scene['robots']:
            az=geometry(human,receiver,anchor)['doa_deg'][0];lo=int(math.floor(az/30))*30;hi=lo+30
            rounded=copy.deepcopy(receiver);rounded['initial_xyz_m']=[round(float(x),1) for x in position(receiver,anchor)]
            rounded['displacement_m']=[0.,0.,0.];rounded['yaw_deg']=round(receiver['yaw_deg'])
            azq=geometry(human,rounded,anchor)['doa_deg'][0]
            if not (lo+1<az<hi-1 and lo+1<azq<hi-1):return None
            selected=[h['entity_id'] for h in scene['humans'] if active(h,round(anchor*SR)) and lo<=geometry(h,receiver,anchor)['doa_deg'][0]<hi]
            if selected!=[human['entity_id']]:return None
            queries.append(dict(source_id=human['entity_id'],receiver_id=receiver['entity_id'],
                selection_time_s=anchor,cue_time_range_s=[anchor,round(anchor+.2,9)],initial_sector_deg=[lo,hi]))
    return queries


def audit(scene,room,full_visibility=True):
    cues=range_queries(scene)
    if cues is None:return None
    ts=np.arange(0,scene['duration_samples']/SR,.025);entities=scene['humans']+scene['robots']
    pts=[position(e,ts) for e in entities];minimum=float('inf')
    for e,p in zip(entities,pts):
        if not points_clear(p,room):return None
        length=float(np.linalg.norm(e['displacement_m']));peak=1.875*length/e['motion_duration_s']
        if length and (length<=2. or peak>.8000001):return None
        if e.get('motion_start_s',0)+e['motion_duration_s']>scene['duration_samples']/SR:return None
    for i,p in enumerate(pts):
        for q in pts[i+1:]:
            minimum=min(minimum,float(np.linalg.norm((p-q)[:,[0,2]],axis=1).min()))
            if minimum<.6:return None
    # Reception points move; use their current positions for every visibility check.
    if full_visibility:
        for human in scene['humans']:
            for t in ts[::4]:
                if not any(visible(position(human,t),position(r,t),room) for r in scene['robots']):return None
    observability=[]
    for h in scene['humans']:
        active_ts=[s/SR for s in grid(scene) if active(h,s)]
        for r in scene['robots']:
            az=np.unwrap(np.radians([geometry(h,r,t)['doa_deg'][0] for t in active_ts]));observability.append(dict(source_id=h['entity_id'],receiver_id=r['entity_id'],active_azimuth_span_deg=float(np.degrees(np.ptp(az)))))
    if scene['branch']=='easy_human':
        first=scene['humans'][0]['entity_id'];r0=scene['robots'][0]['entity_id']
        if next(x['active_azimuth_span_deg'] for x in observability if x['source_id']==first and x['receiver_id']==r0)<30:return None
    elif scene['branch']=='easy_robot':
        r0=scene['moving_entity_ids'][0]
        if max(x['active_azimuth_span_deg'] for x in observability if x['receiver_id']==r0)<30:return None
    else:
        if any(max(x['active_azimuth_span_deg'] for x in observability if x['source_id']==h['entity_id'])<15 for h in scene['humans']):return None
    return dict(minimum_horizontal_clearance_m=minimum,observability=observability,range_queries=cues,
        displacement_m={e['entity_id']:float(np.linalg.norm(e['displacement_m'])) for e in entities},
        maximum_speed_mps=max(1.875*np.linalg.norm(e['displacement_m'])/e['motion_duration_s'] for e in entities))