Download sharpa11_20261107/scripts/sharpa11_align.py from JoeyYuzy/humanoidBench: direct link, hf CLI and curl.
- Browser
- Download file 4.59 kB
-
https://huggingface.co/datasets/JoeyYuzy/humanoidBench/resolve/main/sharpa11_20261107/scripts/sharpa11_align.py
- Command line
-
hf download hf://datasets/JoeyYuzy/humanoidBench/sharpa11_20261107/scripts/sharpa11_align.py
-
curl -L -o sharpa11_align.py https://huggingface.co/datasets/JoeyYuzy/humanoidBench/resolve/main/sharpa11_20261107/scripts/sharpa11_align.py
4.59 kB
| """Fit the main floor by triangle area and level scan geometry and cameras together.""" | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| from pxr import Gf, Usd, UsdGeom | |
| from sharpa11_build import OUT, SOURCE, save | |
| def align(directory): | |
| report = json.loads((directory / 'build.json').read_text()) | |
| if report.get('floor_plane_fit'): | |
| return | |
| result = next(SOURCE.glob(directory.name + '*/v001/result')) | |
| raw = json.loads((result / 'trajectory.json').read_text()) | |
| frames = [f for m in raw['mf_files'].values() for f in m['frames']] | |
| poses = np.array([f['cam2world_pose'] for f in frames]) | |
| stage = Usd.Stage.Open(str(directory / 'scene.usdc')) | |
| centers, normals, areas = [], [], [] | |
| for prim in stage.GetPrimAtPath('/World/Scan/Collisions').GetChildren(): | |
| geom = UsdGeom.Mesh(prim) | |
| v = np.asarray(geom.GetPointsAttr().Get()) | |
| f = np.asarray(geom.GetFaceVertexIndicesAttr().Get()).reshape(-1, 3) | |
| t = v[f] | |
| cross = np.cross(t[:, 1] - t[:, 0], t[:, 2] - t[:, 0]) | |
| area = np.linalg.norm(cross, axis=1) | |
| n = cross / np.maximum(area[:, None], 1e-20) | |
| c = t.mean(1) | |
| mask = (abs(n[:, 1]) > .94) & (c[:, 1] > np.quantile(poses[:, 1, 3], .90) + .12) | |
| centers.append(c[mask]); normals.append(n[mask]); areas.append(area[mask] / 2) | |
| c = np.concatenate(centers); n = np.concatenate(normals); a = np.concatenate(areas) | |
| assert len(c) > 100 | |
| rng = np.random.default_rng(281) | |
| ids = rng.choice(len(c), min(20000, len(c)), replace=True, p=a/a.sum()) | |
| q = c[ids] | |
| best_score = -1 | |
| best_plane = None | |
| eye = poses[:, :3, 3][::5] | |
| for index in rng.choice(len(c), 240, replace=True, p=a/a.sum()): | |
| coef = np.array([-n[index, 0]/n[index, 1], -n[index, 2]/n[index, 1], c[index] @ n[index]/n[index, 1]]) | |
| separation = eye[:, [0, 2]] @ coef[:2] + coef[2] - eye[:, 1] | |
| if np.quantile(separation, .02) < .15: | |
| continue | |
| residual = abs(q[:, 1] - (q[:, [0, 2]] @ coef[:2] + coef[2])) | |
| score = int((residual < .025).sum()) | |
| if score > best_score: | |
| best_score, best_plane = score, coef | |
| assert best_plane is not None | |
| for _ in range(3): | |
| residual = abs(c[:, 1] - (c[:, [0, 2]] @ best_plane[:2] + best_plane[2])) | |
| mask = residual < .03 | |
| A = np.c_[c[mask, 0], c[mask, 2], np.ones(mask.sum())] | |
| w = np.sqrt(a[mask]) | |
| best_plane = np.linalg.lstsq(A*w[:, None], c[mask, 1]*w, rcond=None)[0] | |
| normal = np.array([best_plane[0], -1., best_plane[1]]) | |
| length = np.linalg.norm(normal) | |
| normal /= length | |
| axis = np.array([1., 0., 0.]); axis -= normal*(axis @ normal); axis /= np.linalg.norm(axis) | |
| other = np.cross(normal, axis) | |
| matrix = np.eye(4) | |
| matrix[:3, :3] = np.c_[axis, other, normal] | |
| matrix[3, 2] = best_plane[2] / length | |
| UsdGeom.Xformable(stage.GetPrimAtPath('/World/Scan')).GetOrderedXformOps()[0].Set(Gf.Matrix4d(matrix.tolist())) | |
| views = json.loads((directory / 'candidate_cameras.json').read_text()) | |
| for view in views: | |
| pose = poses[view['trajectory_index']] | |
| source_eye = pose[:3, 3].copy() | |
| source_eye[1] = source_eye[[0, 2]] @ best_plane[:2] + best_plane[2] - 1.4*length | |
| eye_world = (np.r_[source_eye, 1] @ matrix)[:3] | |
| direction = pose[:3, 2] @ matrix[:3, :3] | |
| direction[2] = 0; direction /= np.linalg.norm(direction) | |
| target = eye_world + direction*3.; target[2] -= .22 | |
| view['eye'] = eye_world.tolist(); view['target'] = target.tolist() | |
| camera = UsdGeom.Camera(stage.GetPrimAtPath(f'/World/Cameras/View_{view["index"]:02d}')) | |
| camera.GetOrderedXformOps()[0].Set(Gf.Matrix4d().SetLookAt(Gf.Vec3d(*eye_world), Gf.Vec3d(*target), Gf.Vec3d(0, 0, 1)).GetInverse()) | |
| stage.GetRootLayer().Save() | |
| save(directory / 'candidate_cameras.json', views) | |
| report['source_to_world_matrix'] = matrix.tolist() | |
| report['floor_plane_fit'] = dict(source_y_equals_ax_plus_bz_plus_c=best_plane.tolist(), | |
| support_area_m2=float(a[mask].sum()), inlier_tolerance_m=.03, tilt_deg=float(np.degrees(np.arccos(-normal[1]))), | |
| method='area-weighted horizontal scan triangles; RANSAC with below-camera constraint, weighted least squares') | |
| save(directory / 'build.json', report) | |
| print('ALIGNED', directory.name, report['floor_plane_fit'], flush=True) | |
| if __name__ == '__main__': | |
| import sys | |
| for directory in sorted(OUT.iterdir()): | |
| if directory.is_dir() and (not sys.argv[1:] or directory.name in sys.argv[1:]): | |
| align(directory) | |