yuzhewu207's picture
ww_index: window list, captions, lags and scripts to rebuild the WildWorld windows from upstream
a54e6a7 verified
Raw History Blame Contribute Delete
4.73 kB
"""Physical-plausibility and framing checks for a shot. Returns a report dict; `problems` lists what an agent should fix."""
import numpy as np
from .skeleton import LINE_RATIO
FOOT_NAMES = ("foot",)
def _segments_distance(p1, q1, p2, q2):
"""Minimum distance between segments p1q1 and p2q2 (vectorised over leading dims)."""
d1, d2, r = q1 - p1, q2 - p2, p1 - p2
a = (d1 * d1).sum(-1); e = (d2 * d2).sum(-1); f = (d2 * r).sum(-1)
c = (d1 * r).sum(-1); b = (d1 * d2).sum(-1); den = a * e - b * b
s = np.clip(np.where(den > 1e-9, (b * f - c * e) / np.maximum(den, 1e-9), 0.0), 0, 1)
t = np.where(e > 1e-9, (b * s + f) / np.maximum(e, 1e-9), 0.0)
s = np.where(t < 0, np.clip(-c / np.maximum(a, 1e-9), 0, 1), np.where(t > 1, np.clip((b - c) / np.maximum(a, 1e-9), 0, 1), s))
t = np.clip(t, 0, 1)
return np.linalg.norm(p1 + d1 * s[..., None] - (p2 + d2 * t[..., None]), axis=-1)
def check(scene, floor_z=0.0, min_clearance=-5.0, max_bone_drift=0.01, min_gap=5.0, max_foot_slide=6.0, min_on_screen=0.6):
report = {"entities": {}, "problems": []}
T = scene.frames
for name, ent in scene.entities.items():
tr, sk = ent["track"], ent["skeleton"]
P = tr.poses
r = {}
if not np.isfinite(P).all() or (tr.weapon is not None and not np.isfinite(tr.weapon).all()):
report["problems"].append(f"{name}: NaN/inf in pose"); report["entities"][name] = r; continue
L0 = sk.bone_lengths[1:]
L = np.linalg.norm(P[:, 1:] - P[:, sk.parent_idx[1:]], axis=-1)
drift = float(np.max(np.abs(L - L0) / np.maximum(L0, 1e-6))); r["bone_drift"] = drift
if drift > max_bone_drift:
report["problems"].append(f"{name}: bone length drift {drift:.1%}")
low = float((P[..., 2].min(axis=1) - sk.height * LINE_RATIO).min() - floor_z); r["lowest_joint_cm"] = low
if low < min_clearance:
t = int(np.argmin(P[..., 2].min(axis=1)))
report["problems"].append(f"{name}: body goes {-low:.0f} cm below the floor (frame {t})")
if tr.weapon is not None:
wl = np.linalg.norm(tr.weapon[:, 1] - tr.weapon[:, 0], axis=1)
r["weapon_length_range"] = [float(wl.min()), float(wl.max())]
wlow = float(tr.weapon[..., 2].min() - floor_z); r["weapon_lowest_cm"] = wlow
if wlow < min_clearance - sk.weapon.radius:
report["problems"].append(f"{name}: weapon goes {-wlow:.0f} cm below the floor (frame {int(np.argmin(tr.weapon[..., 2].min(axis=1)))})")
feet = [i for i, n in enumerate(sk.joints) if any(f in n for f in FOOT_NAMES)]
if feet:
F = P[:, feet]; grounded = F[1:, :, 2] < 8.0
slide = np.linalg.norm(F[1:, :, :2] - F[:-1, :, :2], axis=-1)
slide = slide[grounded & (F[:-1, :, 2] < 8.0)]
r["foot_slide_p95_cm_per_frame"] = float(np.percentile(slide, 95)) if slide.size else 0.0
if slide.size and r["foot_slide_p95_cm_per_frame"] > max_foot_slide:
report["problems"].append(f"{name}: grounded feet slide {r['foot_slide_p95_cm_per_frame']:.1f} cm/frame (p95)")
vis = np.array([scene.camera_at(t).in_view(P[t]).mean() for t in range(T)])
r["on_screen_mean"] = float(vis.mean()); r["on_screen_min"] = float(vis.min())
if vis.mean() < min_on_screen:
report["problems"].append(f"{name}: only {vis.mean():.0%} of joints on screen on average")
report["entities"][name] = r
names = list(scene.entities)
for i in range(len(names)):
for k in range(i + 1, len(names)):
a, b = scene.entities[names[i]], scene.entities[names[k]]
gap = np.inf; worst_t = 0
for t in range(0, T, 2):
sa = _segs(a, t); sb = _segs(b, t)
d = _segments_distance(sa[0][:, None], sa[1][:, None], sb[0][None], sb[1][None]) - sa[2][:, None] - sb[2][None]
if d.min() < gap:
gap, worst_t = float(d.min()), t
report[f"gap_{names[i]}_{names[k]}_cm"] = gap
if gap < min_gap:
report["problems"].append(f"{names[i]} and {names[k]} interpenetrate by {-gap:.0f} cm (frame {worst_t})" if gap < 0 else
f"{names[i]} and {names[k]} come within {gap:.0f} cm (frame {worst_t})")
return report
def _segs(ent, t):
sk, tr = ent["skeleton"], ent["track"]
P = tr.poses[t]
a = P[sk.parent_idx[1:]]; b = P[1:]; r = np.full(len(b), sk.height * LINE_RATIO)
if tr.weapon is not None and sk.weapon is not None:
a = np.vstack([a, tr.weapon[t, :1]]); b = np.vstack([b, tr.weapon[t, 1:]]); r = np.r_[r, sk.weapon.radius]
return a, b, r