Skip to content

Commit 244b10c

Browse files
committed
refactor: change the arguments of "set_rotation" in sensor and transform
1 parent db4bdc5 commit 244b10c

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

tensorbay/geometry/tests/test_transform.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,16 @@ def test_set_translation(self):
115115
def test_set_rotation(self):
116116
transform = Transform3D()
117117

118-
transform.set_rotation([0, 1, 0, 0])
118+
transform.set_rotation(0, 1, 0, 0)
119119
assert transform.rotation == quaternion(0, 1, 0, 0)
120120

121121
quaternion_1 = quaternion(0, 1, 0, 0)
122-
transform.set_rotation(quaternion_1)
122+
transform.set_rotation(rotation=quaternion_1)
123123
assert transform.rotation == quaternion_1
124124

125+
with pytest.raises(TypeError):
126+
transform.set_rotation([0, 1, 0, 0])
127+
125128
def test_as_matrix(self):
126129
matrix = np.array([[1, 0, 0, 1], [0, -1, 0, 2], [0, 0, -1, 3], [0, 0, 0, 1]])
127130
transform = Transform3D([1, 2, 3], [0, 1, 0, 0])

tensorbay/geometry/transform.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,26 +264,38 @@ def set_translation(self, x: float, y: float, z: float) -> None:
264264
"""
265265
self._translation = Vector3D(x, y, z)
266266

267-
def set_rotation(self, rotation: RotationType) -> None:
267+
def set_rotation(
268+
self,
269+
w: Optional[float] = None,
270+
x: Optional[float] = None,
271+
y: Optional[float] = None,
272+
z: Optional[float] = None,
273+
*,
274+
rotation: Optional[quaternion] = None,
275+
) -> None:
268276
"""Set the rotation of the transform.
269277
270278
Arguments:
271-
rotation: Rotation in a sequence of [w, x, y, z] or numpy quaternion.
279+
w: The w componet of the roation quaternion.
280+
x: The x componet of the roation quaternion.
281+
y: The y componet of the roation quaternion.
282+
z: The z componet of the roation quaternion.
283+
rotation: Rotation in numpy quaternion.
272284
273285
Examples:
274286
>>> transform = Transform3D([1, 1, 1], [1, 0, 0, 0])
275-
>>> transform.set_rotation([0, 1, 0, 0])
287+
>>> transform.set_rotation(0, 1, 0, 0)
276288
>>> transform
277289
Transform3D(
278290
(translation): Vector3D(1, 1, 1),
279291
(rotation): quaternion(0, 1, 0, 0)
280292
)
281293
282294
"""
283-
if isinstance(rotation, quaternion):
295+
if rotation:
284296
self._rotation = quaternion(rotation)
285-
else:
286-
self._rotation = quaternion(*rotation)
297+
return
298+
self._rotation = quaternion(w, x, y, z)
287299

288300
def as_matrix(self) -> np.ndarray:
289301
"""Return the transform as a 4x4 transform matrix.

tensorbay/sensor/sensor.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
3131
"""
3232

33+
import warnings
3334
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, TypeVar, Union
3435

3536
from ..geometry import Transform3D
@@ -45,6 +46,10 @@
4546
)
4647
from .intrinsics import CameraIntrinsics
4748

49+
with warnings.catch_warnings():
50+
warnings.simplefilter("ignore")
51+
from quaternion import quaternion
52+
4853
_T = TypeVar("_T", bound="Sensor")
4954

5055

@@ -234,14 +239,26 @@ def set_translation(self, x: float, y: float, z: float) -> None:
234239
self.extrinsics = Transform3D()
235240
self.extrinsics.set_translation(x, y, z)
236241

237-
def set_rotation(self, rotation: Transform3D.RotationType) -> None:
242+
def set_rotation(
243+
self,
244+
w: Optional[float] = None,
245+
x: Optional[float] = None,
246+
y: Optional[float] = None,
247+
z: Optional[float] = None,
248+
*,
249+
rotation: Optional[quaternion] = None,
250+
) -> None:
238251
"""Set the rotation of the sensor.
239252
240253
Arguments:
241-
rotation: Rotation in a sequence of [w, x, y, z] or numpy quaternion.
254+
w: The w componet of the roation quaternion.
255+
x: The x componet of the roation quaternion.
256+
y: The y componet of the roation quaternion.
257+
z: The z componet of the roation quaternion.
258+
rotation: Rotation in numpy quaternion.
242259
243260
Examples:
244-
>>> sensor.set_rotation([2, 3, 4, 5])
261+
>>> sensor.set_rotation(2, 3, 4, 5)
245262
>>> sensor
246263
Lidar("Lidar1")(
247264
(extrinsics): Transform3D(
@@ -253,7 +270,7 @@ def set_rotation(self, rotation: Transform3D.RotationType) -> None:
253270
"""
254271
if not hasattr(self, "extrinsics"):
255272
self.extrinsics = Transform3D()
256-
self.extrinsics.set_rotation(rotation)
273+
self.extrinsics.set_rotation(w, x, y, z, rotation=rotation)
257274

258275

259276
@TypeRegister(SensorType.LIDAR)

tensorbay/sensor/tests/test_sensor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_set_translation(self):
110110

111111
def test_set_rotation(self):
112112
lidar = Lidar("test")
113-
lidar.set_rotation([1, 2, 3, 4])
113+
lidar.set_rotation(1, 2, 3, 4)
114114
assert lidar.extrinsics.rotation == _ROTATION
115115

116116
def test_dumps(self):
@@ -141,7 +141,7 @@ def test_set_translation(self):
141141

142142
def test_set_rotation(self):
143143
radar = Radar("test")
144-
radar.set_rotation([1, 2, 3, 4])
144+
radar.set_rotation(1, 2, 3, 4)
145145
assert radar.extrinsics.rotation == _ROTATION
146146

147147
def test_dumps(self):
@@ -172,7 +172,7 @@ def test_set_translation(self):
172172

173173
def test_set_rotation(self):
174174
camera = Camera("test")
175-
camera.set_rotation([1, 2, 3, 4])
175+
camera.set_rotation(1, 2, 3, 4)
176176
assert camera.extrinsics.rotation == _ROTATION
177177

178178
def test_set_camera_matrix(self):
@@ -221,7 +221,7 @@ def test_set_translation(self):
221221

222222
def test_set_rotation(self):
223223
fisheye_camera = FisheyeCamera("test")
224-
fisheye_camera.set_rotation([1, 2, 3, 4])
224+
fisheye_camera.set_rotation(1, 2, 3, 4)
225225
assert fisheye_camera.extrinsics.rotation == _ROTATION
226226

227227
def test_dumps(self):

0 commit comments

Comments
 (0)