55
66from __future__ import annotations
77
8- import math
98import torch
109from typing import TYPE_CHECKING , Any
1110
11+ from autodata_utils import pose_math
12+
1213if TYPE_CHECKING :
1314 from isaaclab .envs import ManagerBasedEnv
1415 from isaaclab .managers import SceneEntityCfg
@@ -90,24 +91,33 @@ def _randomize_extrinsics(
9091 from isaaclab .sim .utils import standardize_xform_ops
9192
9293 offset = sensor .cfg .offset
93- nominal_position = tuple (float (component ) for component in offset .pos )
94- nominal_rotation = _convert_rotation (tuple (offset .rot ), offset .convention , "ros" )
94+ nominal_position = torch .tensor (offset .pos , dtype = torch .float64 )
95+ nominal_rotation = pose_math .convert_camera_frame_orientation_convention (
96+ torch .tensor (offset .rot , dtype = torch .float64 ), offset .convention , "ros"
97+ )
9598 camera_prims = _find_camera_prims_by_env (env , env_ids , sensor )
9699
97100 applied = {}
98101 for env_id in env_ids :
99- position_offset = _sample_uniform_vector (position_xyz_min , position_xyz_max , env .device )
100- rotation_offset_deg = _sample_uniform_vector (rotation_rpy_min_deg , rotation_rpy_max_deg , env .device )
101- position = tuple (nominal + sampled for nominal , sampled in zip (nominal_position , position_offset ))
102- rotation = _quat_multiply (nominal_rotation , _quat_from_rpy_deg (rotation_offset_deg ))
102+ position_offset = pose_math .sample_uniform_vector (position_xyz_min , position_xyz_max , env .device )
103+ rotation_offset_deg = pose_math .sample_uniform_vector (rotation_rpy_min_deg , rotation_rpy_max_deg , env .device )
104+ position = nominal_position + position_offset
105+ rotation = pose_math .quat_mul (
106+ nominal_rotation , pose_math .quat_from_euler_xyz (torch .deg2rad (rotation_offset_deg ))
107+ )
108+ position_xyz = tuple (position .tolist ())
103109 standardize_xform_ops (
104110 camera_prims [env_id ],
105- translation = position ,
106- orientation = _convert_rotation (rotation , "ros" , "opengl" ),
111+ translation = position_xyz ,
112+ orientation = tuple (
113+ pose_math .convert_camera_frame_orientation_convention (rotation , "ros" , "opengl" ).tolist ()
114+ ),
107115 )
108116 applied [env_id ] = {
109- "position_xyz" : position ,
110- "rotation_xyzw" : _convert_rotation (rotation , "ros" , offset .convention ),
117+ "position_xyz" : position_xyz ,
118+ "rotation_xyzw" : tuple (
119+ pose_math .convert_camera_frame_orientation_convention (rotation , "ros" , offset .convention ).tolist ()
120+ ),
111121 }
112122 return applied
113123
@@ -129,7 +139,7 @@ def _randomize_intrinsics(
129139 height , width = sensor .image_shape
130140 nominal_focal_length_px = width * focal_length / horizontal_aperture
131141 scales = torch .tensor (
132- [_sample_uniform (focal_length_scale_range , env .device ) for _ in env_ids ],
142+ [pose_math . sample_uniform_scalar (focal_length_scale_range , env .device ) for _ in env_ids ],
133143 dtype = torch .float32 ,
134144 device = env .device ,
135145 )
@@ -140,23 +150,26 @@ def _randomize_intrinsics(
140150 matrices [:, 0 , 2 ] = 0.5 * width
141151 matrices [:, 1 , 2 ] = 0.5 * height
142152 matrices [:, 2 , 2 ] = 1.0
143- prims = _find_camera_prims_by_env ( env , env_ids , sensor )
144- nominal = [ prims [ env_id ]. GetCustomDataByKey ( "isaaclab_arena:nominal_opencv_intrinsics" ) for env_id in env_ids ]
145- if any ( value is not None for value in nominal ):
146- assert all ( value is not None for value in nominal ), "Camera clones must share the same lens model"
153+ distortion = getattr ( spawn_cfg , "distortion" , None )
154+ if distortion is not None :
155+ assert distortion . model == "opencvPinhole" , "Camera DR supports OpenCV pinhole calibration only."
156+ prims = _find_camera_prims_by_env ( env , env_ids , sensor )
147157 for index , env_id in enumerate (env_ids ):
148- fx , fy , cx , cy = nominal [ index ]
158+ fx , fy , cx , cy = distortion . fx , distortion . fy , distortion . cx , distortion . cy
149159 scale = float (scales [index ])
150160 matrices [index , 0 , 0 ], matrices [index , 1 , 1 ] = fx * scale , fy * scale
151161 matrices [index , 0 , 2 ], matrices [index , 1 , 2 ] = cx , cy
152162 prim = prims [env_id ]
163+ assert (
164+ prim .GetAttribute ("omni:lensdistortion:model" ).Get () == "opencvPinhole"
165+ ), "Camera DR requires the renderer to author the configured OpenCV pinhole lens model."
153166 prefix = "omni:lensdistortion:opencvPinhole:"
154- prim . GetAttribute ( prefix + "fx" ). Set ( fx * scale )
155- prim .GetAttribute (prefix + "fy" ).Set (fy * scale )
167+ for name , value in (( "fx" , fx * scale ), ( "fy" , fy * scale ), ( "cx" , cx ), ( "cy" , cy )):
168+ prim .GetAttribute (prefix + name ).Set (value )
156169 prim .GetAttribute ("focalLength" ).Set (focal_length * scale )
157- # set_intrinsic_matrices would discard cx/cy and average fx/fy. RTX reads
158- # the native lens attributes; publish the same K in the sensor metadata .
159- sensor ._data . intrinsic_matrices . torch [ env_ids ] = matrices
170+ # Lab's setter skips cameras with an authored OpenCV model. Edit the lens
171+ # attributes and use its readback to keep the sensor calibration consistent .
172+ sensor ._update_intrinsic_matrices ( env_ids )
160173 else :
161174 sensor .set_intrinsic_matrices (matrices , focal_length = focal_length , env_ids = env_ids )
162175 return {env_id : float (matrices [index , 0 , 0 ]) for index , env_id in enumerate (env_ids )}
@@ -172,16 +185,17 @@ def _sync_camera_mounts_to_fabric(env: ManagerBasedEnv, env_ids: list[int], sens
172185 import usdrt
173186 from isaaclab .sim .utils .stage import get_current_stage_id
174187 from pxr import UsdGeom
188+ from usdrt import hierarchy
175189
176190 stage = usdrt .Usd .Stage .Attach (get_current_stage_id ())
177- hierarchy = usdrt . hierarchy .IFabricHierarchy ().get_fabric_hierarchy (
191+ fabric_hierarchy = hierarchy .IFabricHierarchy ().get_fabric_hierarchy (
178192 stage .GetFabricId (), stage .GetStageIdAsStageId ()
179193 )
180194 for prim in _find_camera_prims_by_env (env , env_ids , sensor ).values ():
181195 local = UsdGeom .Xformable (prim ).GetLocalTransformation ()
182196 matrix = usdrt .Gf .Matrix4d (* [value for row in local for value in row ])
183- hierarchy .set_local_xform (usdrt .Sdf .Path (str (prim .GetPath ())), matrix )
184- hierarchy .update_world_xforms ()
197+ fabric_hierarchy .set_local_xform (usdrt .Sdf .Path (str (prim .GetPath ())), matrix )
198+ fabric_hierarchy .update_world_xforms ()
185199
186200
187201def _find_camera_prims_by_env (env : ManagerBasedEnv , env_ids : list [int ], sensor : Camera ) -> dict [int , Any ]:
@@ -198,57 +212,3 @@ def _find_camera_prims_by_env(env: ManagerBasedEnv, env_ids: list[int], sensor:
198212 ), f"Expected exactly one camera prim under { env_path !r} for { sensor .cfg .prim_path !r} , found { len (matches )} ."
199213 prims_by_env [env_id ] = matches [0 ]
200214 return prims_by_env
201-
202-
203- def _convert_rotation (
204- rotation_xyzw : tuple [float , float , float , float ],
205- origin : str ,
206- target : str ,
207- ) -> tuple [float , float , float , float ]:
208- """Convert camera orientation conventions without changing the physical mount."""
209- from isaaclab .utils .math import convert_camera_frame_orientation_convention
210-
211- rotation = torch .tensor ([rotation_xyzw ], dtype = torch .float32 )
212- converted = convert_camera_frame_orientation_convention (rotation , origin = origin , target = target )
213- return tuple (float (component ) for component in converted [0 ])
214-
215-
216- def _quat_from_rpy_deg (rpy_deg : tuple [float , float , float ]) -> tuple [float , float , float , float ]:
217- """Build an (x, y, z, w) quaternion from intrinsic x-y-z rotations [deg]."""
218- quat = (0.0 , 0.0 , 0.0 , 1.0 )
219- for axis , angle_deg in enumerate (rpy_deg ):
220- half_angle = 0.5 * math .radians (angle_deg )
221- axis_quat = [0.0 , 0.0 , 0.0 , math .cos (half_angle )]
222- axis_quat [axis ] = math .sin (half_angle )
223- quat = _quat_multiply (quat , tuple (axis_quat ))
224- return quat
225-
226-
227- def _quat_multiply (
228- first_xyzw : tuple [float , float , float , float ],
229- second_xyzw : tuple [float , float , float , float ],
230- ) -> tuple [float , float , float , float ]:
231- """Compose two (x, y, z, w) quaternions, applying ``second_xyzw`` in the body frame."""
232- x_1 , y_1 , z_1 , w_1 = first_xyzw
233- x_2 , y_2 , z_2 , w_2 = second_xyzw
234- return (
235- w_1 * x_2 + x_1 * w_2 + y_1 * z_2 - z_1 * y_2 ,
236- w_1 * y_2 - x_1 * z_2 + y_1 * w_2 + z_1 * x_2 ,
237- w_1 * z_2 + x_1 * y_2 - y_1 * x_2 + z_1 * w_2 ,
238- w_1 * w_2 - x_1 * x_2 - y_1 * y_2 - z_1 * z_2 ,
239- )
240-
241-
242- def _sample_uniform_vector (
243- minimum : tuple [float , float , float ],
244- maximum : tuple [float , float , float ],
245- device : str ,
246- ) -> tuple [float , float , float ]:
247- return tuple (_sample_uniform ((low , high ), device ) for low , high in zip (minimum , maximum ))
248-
249-
250- def _sample_uniform (value_range : tuple [float , float ], device : str ) -> float :
251- low , high = value_range
252- if low == high :
253- return float (low )
254- return float (low + (high - low ) * torch .rand ((), device = device ).item ())
0 commit comments