Query system for timestamp-based pose lookup with SLERP interpolation for rotations.
| 94 | |
| 95 | |
| 96 | class PoseQueryEngine: |
| 97 | """ |
| 98 | Query system for timestamp-based pose lookup with SLERP interpolation for rotations. |
| 99 | """ |
| 100 | |
| 101 | def __init__(self): |
| 102 | self.continuous_poses: Dict[int, np.ndarray] = ( |
| 103 | {} |
| 104 | ) # timestamp_ns -> 4x4 pose matrix |
| 105 | |
| 106 | def load_continuous_poses(self, poses_file: str) -> bool: |
| 107 | """ |
| 108 | Load continuous poses from a saved file. |
| 109 | |
| 110 | Args: |
| 111 | poses_file: Path to the continuous poses .npy file |
| 112 | |
| 113 | Returns: |
| 114 | True if loaded successfully, False otherwise |
| 115 | """ |
| 116 | try: |
| 117 | loaded_poses = np.load(poses_file, allow_pickle=True).item() |
| 118 | self.continuous_poses.update(loaded_poses) |
| 119 | print(f"Loaded {len(loaded_poses)} continuous poses from {poses_file}") |
| 120 | return True |
| 121 | except Exception as e: |
| 122 | print(f"Failed to load continuous poses from {poses_file}: {e}") |
| 123 | return False |
| 124 | |
| 125 | def find_closest_poses( |
| 126 | self, target_timestamp: int, poses_dict: Dict[int, np.ndarray] |
| 127 | ) -> Tuple[Optional[Tuple[int, np.ndarray]], Optional[Tuple[int, np.ndarray]]]: |
| 128 | """ |
| 129 | Find the two closest poses (before and after) to the target timestamp. |
| 130 | |
| 131 | Args: |
| 132 | target_timestamp: Target timestamp in nanoseconds |
| 133 | poses_dict: Dictionary of timestamp -> pose matrix |
| 134 | |
| 135 | Returns: |
| 136 | Tuple of (before_pose, after_pose) where each is (timestamp, pose_matrix) or None |
| 137 | """ |
| 138 | if not poses_dict: |
| 139 | return None, None |
| 140 | |
| 141 | timestamps = sorted(poses_dict.keys()) |
| 142 | |
| 143 | # Find insertion point |
| 144 | idx = np.searchsorted(timestamps, target_timestamp) |
| 145 | |
| 146 | before_pose = None |
| 147 | after_pose = None |
| 148 | |
| 149 | if idx > 0: |
| 150 | before_ts = timestamps[idx - 1] |
| 151 | before_pose = (before_ts, poses_dict[before_ts]) |
| 152 | |
| 153 | if idx < len(timestamps): |
no outgoing calls
no test coverage detected