|
31 | 31 | VisualMaterialCfg, |
32 | 32 | ) |
33 | 33 | from embodichain.utils.string import resolve_matching_names |
34 | | -from embodichain.utils.math import sample_uniform |
| 34 | +from embodichain.utils.math import ( |
| 35 | + sample_uniform, |
| 36 | + quat_from_euler_xyz, |
| 37 | + euler_xyz_from_quat, |
| 38 | +) |
35 | 39 | from embodichain.utils import logger |
36 | 40 | from embodichain.data import get_data_path |
37 | 41 |
|
|
40 | 44 |
|
41 | 45 |
|
42 | 46 | __all__ = [ |
| 47 | + "randomize_camera_extrinsics", |
43 | 48 | "randomize_light", |
44 | 49 | "randomize_camera_intrinsics", |
45 | 50 | "randomize_visual_material", |
46 | 51 | ] |
47 | 52 |
|
48 | 53 |
|
| 54 | +def randomize_camera_extrinsics( |
| 55 | + env: EmbodiedEnv, |
| 56 | + env_ids: Union[torch.Tensor, None], |
| 57 | + entity_cfg: SceneEntityCfg, |
| 58 | + pos_range: Optional[tuple[list[float], list[float]]] = None, |
| 59 | + euler_range: Optional[tuple[list[float], list[float]]] = None, |
| 60 | + eye_range: Optional[tuple[list[float], list[float]]] = None, |
| 61 | + target_range: Optional[tuple[list[float], list[float]]] = None, |
| 62 | + up_range: Optional[tuple[list[float], list[float]]] = None, |
| 63 | +) -> None: |
| 64 | + """ |
| 65 | + Randomize camera extrinsic properties (position and orientation). |
| 66 | +
|
| 67 | + Behavior: |
| 68 | + - If extrinsics config has a parent field (attach mode), pos_range/euler_range are used to perturb the initial pose (pos, quat), |
| 69 | + and set_local_pose is called to attach the camera to the parent node. In this case, pose is related to parent. |
| 70 | + - If extrinsics config uses eye/target/up (no parent), eye_range/target_range/up_range are used to perturb the initial eye, target, up vectors, |
| 71 | + and look_at is called to set the camera orientation. |
| 72 | +
|
| 73 | + Args: |
| 74 | + env: The environment instance. |
| 75 | + env_ids: The environment IDs to apply the randomization. |
| 76 | + entity_cfg (SceneEntityCfg): The configuration of the scene entity to randomize. |
| 77 | + pos_range: Position perturbation range (attach mode). |
| 78 | + euler_range: Euler angle perturbation range (attach mode). |
| 79 | + eye_range: Eye position perturbation range (look_at mode). |
| 80 | + target_range: Target position perturbation range (look_at mode). |
| 81 | + up_range: Up vector perturbation range (look_at mode). |
| 82 | + """ |
| 83 | + camera: Union[Camera, StereoCamera] = env.sim.get_sensor(entity_cfg.uid) |
| 84 | + num_instance = len(env_ids) |
| 85 | + |
| 86 | + extrinsics = camera.cfg.extrinsics |
| 87 | + |
| 88 | + if extrinsics.parent is not None: |
| 89 | + # If extrinsics has a parent field, use pos/euler perturbation and attach camera to parent node |
| 90 | + init_pos = getattr(extrinsics, "pos", [0.0, 0.0, 0.0]) |
| 91 | + init_quat = getattr(extrinsics, "quat", [0.0, 0.0, 0.0, 1.0]) |
| 92 | + new_pose = torch.tensor( |
| 93 | + [init_pos + init_quat], dtype=torch.float32, device=env.device |
| 94 | + ).repeat(num_instance, 1) |
| 95 | + if pos_range: |
| 96 | + random_value = sample_uniform( |
| 97 | + lower=torch.tensor(pos_range[0]), |
| 98 | + upper=torch.tensor(pos_range[1]), |
| 99 | + size=(num_instance, 3), |
| 100 | + ) |
| 101 | + new_pose[:, :3] += random_value |
| 102 | + if euler_range: |
| 103 | + # 1. quat -> euler |
| 104 | + init_quat_np = ( |
| 105 | + torch.tensor(init_quat, dtype=torch.float32, device=env.device) |
| 106 | + .unsqueeze_(0) |
| 107 | + .repeat(num_instance, 1) |
| 108 | + ) |
| 109 | + init_euler = euler_xyz_from_quat(init_quat_np) |
| 110 | + # 2. Sample perturbation for euler angles |
| 111 | + random_value = sample_uniform( |
| 112 | + lower=torch.tensor(euler_range[0]), |
| 113 | + upper=torch.tensor(euler_range[1]), |
| 114 | + size=(num_instance, 3), |
| 115 | + ) |
| 116 | + # 3. Add perturbation to each environment and convert back to quaternion |
| 117 | + new_quat = quat_from_euler_xyz(init_euler + random_value) |
| 118 | + new_pose[:, 3:7] = new_quat |
| 119 | + |
| 120 | + camera.set_local_pose(new_pose, env_ids=env_ids) |
| 121 | + |
| 122 | + elif extrinsics.eye is not None: |
| 123 | + # If extrinsics uses eye/target/up, use perturbation for look_at mode |
| 124 | + init_eye = ( |
| 125 | + torch.tensor(extrinsics.eye, dtype=torch.float32, device=env.device) |
| 126 | + .unsqueeze(0) |
| 127 | + .repeat(num_instance, 1) |
| 128 | + ) |
| 129 | + init_target = ( |
| 130 | + torch.tensor(extrinsics.target, dtype=torch.float32, device=env.device) |
| 131 | + .unsqueeze(0) |
| 132 | + .repeat(num_instance, 1) |
| 133 | + ) |
| 134 | + init_up = ( |
| 135 | + torch.tensor(extrinsics.up, dtype=torch.float32, device=env.device) |
| 136 | + .unsqueeze(0) |
| 137 | + .repeat(num_instance, 1) |
| 138 | + ) |
| 139 | + |
| 140 | + if eye_range: |
| 141 | + eye_delta = sample_uniform( |
| 142 | + lower=torch.tensor(eye_range[0]), |
| 143 | + upper=torch.tensor(eye_range[1]), |
| 144 | + size=(num_instance, 3), |
| 145 | + ) |
| 146 | + new_eye = init_eye + eye_delta |
| 147 | + else: |
| 148 | + new_eye = init_eye |
| 149 | + |
| 150 | + if target_range: |
| 151 | + target_delta = sample_uniform( |
| 152 | + lower=torch.tensor(target_range[0]), |
| 153 | + upper=torch.tensor(target_range[1]), |
| 154 | + size=(num_instance, 3), |
| 155 | + ) |
| 156 | + new_target = init_target + target_delta |
| 157 | + else: |
| 158 | + new_target = init_target |
| 159 | + |
| 160 | + if up_range: |
| 161 | + up_delta = sample_uniform( |
| 162 | + lower=torch.tensor(up_range[0]), |
| 163 | + upper=torch.tensor(up_range[1]), |
| 164 | + size=(num_instance, 3), |
| 165 | + ) |
| 166 | + new_up = init_up + up_delta |
| 167 | + else: |
| 168 | + new_up = init_up |
| 169 | + |
| 170 | + camera.look_at(new_eye, new_target, new_up, env_ids=env_ids) |
| 171 | + |
| 172 | + else: |
| 173 | + logger.log_error("Unsupported extrinsics format for camera randomization.") |
| 174 | + |
| 175 | + |
49 | 176 | def randomize_light( |
50 | 177 | env: EmbodiedEnv, |
51 | 178 | env_ids: Union[torch.Tensor, None], |
|
0 commit comments