| 1 |
# Copyright (c), ETH Zurich and UNC Chapel Hill. |
| 2 |
# All rights reserved. |
| 3 |
|
| 4 |
import dataclasses |
| 5 |
import json |
| 6 |
import math |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
import numpy as np |
| 10 |
|
| 11 |
MANIFEST_PATH = Path(__file__).with_name("tartanair_v2_manifest.json") |
| 12 |
|
| 13 |
|
| 14 |
@dataclasses.dataclass(frozen=True) |
| 15 |
class SceneSelection: |
| 16 |
category: str |
| 17 |
environment: str |
| 18 |
difficulty: str |
| 19 |
trajectory: str |
| 20 |
|
| 21 |
@property |
| 22 |
def name(self) -> str: |
| 23 |
return f"{self.environment}-{self.difficulty}-{self.trajectory}" |
| 24 |
|
| 25 |
@property |
| 26 |
def source_archive(self) -> str: |
| 27 |
return ( |
| 28 |
f"{self.environment}/Data_{self.difficulty}/image_lcam_equirect.zip" |
| 29 |
) |
| 30 |
|
| 31 |
@property |
| 32 |
def depth_source_archive(self) -> str: |
| 33 |
return ( |
| 34 |
f"{self.environment}/Data_{self.difficulty}/depth_lcam_equirect.zip" |
| 35 |
) |
| 36 |
|
| 37 |
|
| 38 |
def load_manifest() -> dict: |
| 39 |
return json.loads(MANIFEST_PATH.read_text()) |
| 40 |
|
| 41 |
|
| 42 |
def list_scenes(manifest: dict | None = None) -> list[SceneSelection]: |
| 43 |
manifest = manifest or load_manifest() |
| 44 |
scenes = [] |
| 45 |
for category, selections in manifest["selections"].items(): |
| 46 |
for selection in selections: |
| 47 |
environment, difficulty, trajectory = selection.split(":") |
| 48 |
scenes.append( |
| 49 |
SceneSelection( |
| 50 |
category=category, |
| 51 |
environment=environment, |
| 52 |
difficulty=difficulty, |
| 53 |
trajectory=trajectory, |
| 54 |
) |
| 55 |
) |
| 56 |
return scenes |
| 57 |
|
| 58 |
|
| 59 |
def shard_name(manifest: dict, shard_index: int) -> str: |
| 60 |
version = manifest["version"] |
| 61 |
return f"tartanair-v2-v{version}-shard-{shard_index:03d}.tar" |
| 62 |
|
| 63 |
|
| 64 |
def scene_shards(manifest: dict | None = None) -> list[list[SceneSelection]]: |
| 65 |
manifest = manifest or load_manifest() |
| 66 |
scenes = list_scenes(manifest) |
| 67 |
size = manifest["release"]["scenes_per_shard"] |
| 68 |
return [scenes[i : i + size] for i in range(0, len(scenes), size)] |
| 69 |
|
| 70 |
|
| 71 |
def quaternion_angular_distance_deg( |
| 72 |
quaternion1: np.ndarray, quaternion2: np.ndarray |
| 73 |
) -> float: |
| 74 |
dot = abs(float(np.dot(quaternion1, quaternion2))) |
| 75 |
return math.degrees(2.0 * math.acos(np.clip(dot, -1.0, 1.0))) |
| 76 |
|
| 77 |
|
| 78 |
def select_frame_window( |
| 79 |
poses: np.ndarray, |
| 80 |
num_frames: int, |
| 81 |
max_adjacent_translation_m: float, |
| 82 |
max_adjacent_rotation_deg: float, |
| 83 |
) -> range: |
| 84 |
"""Select a contiguous, overlapping window with maximum spatial extent.""" |
| 85 |
if len(poses) < num_frames: |
| 86 |
raise ValueError( |
| 87 |
f"Trajectory has {len(poses)} poses, fewer than {num_frames}" |
| 88 |
) |
| 89 |
|
| 90 |
best_score = None |
| 91 |
best_start = None |
| 92 |
for start in range(len(poses) - num_frames + 1): |
| 93 |
window = poses[start : start + num_frames] |
| 94 |
translations = np.linalg.norm(np.diff(window[:, :3], axis=0), axis=1) |
| 95 |
rotations = np.array( |
| 96 |
[ |
| 97 |
quaternion_angular_distance_deg(q1, q2) |
| 98 |
for q1, q2 in zip( |
| 99 |
window[:-1, 3:7], window[1:, 3:7], strict=True |
| 100 |
) |
| 101 |
] |
| 102 |
) |
| 103 |
if ( |
| 104 |
translations.max() > max_adjacent_translation_m |
| 105 |
or rotations.max() > max_adjacent_rotation_deg |
| 106 |
): |
| 107 |
continue |
| 108 |
|
| 109 |
bbox_diagonal = np.linalg.norm( |
| 110 |
window[:, :3].max(axis=0) - window[:, :3].min(axis=0) |
| 111 |
) |
| 112 |
path_length = translations.sum() |
| 113 |
score = (bbox_diagonal, path_length, -start) |
| 114 |
if best_score is None or score > best_score: |
| 115 |
best_score = score |
| 116 |
best_start = start |
| 117 |
|
| 118 |
if best_start is None: |
| 119 |
raise ValueError( |
| 120 |
"Trajectory has no frame window satisfying motion limits" |
| 121 |
) |
| 122 |
return range(best_start, best_start + num_frames) |