Skip to content

Commit

Permalink
feat: Quaternion.from_json()
Browse files Browse the repository at this point in the history
  • Loading branch information
tklockau committed Nov 4, 2024
1 parent 2f45b00 commit cd3d7b7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
27 changes: 13 additions & 14 deletions raillabel/format/quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@

@dataclass
class Quaternion:
"""A quaternion.
Parameters
----------
x: float or int
The x component of the quaternion.
y: float or int
The y component of the quaternion.
z: float or int
The z component of the quaternion.
w: float or int
The w component of the quaternion.
"""
"""A rotation represented by a quaternion."""

x: float
"The x component of the quaternion."

y: float
"The y component of the quaternion."

z: float
"The z component of the quaternion."

w: float
"The omega component of the quaternion."

@classmethod
def from_json(cls, json: tuple[float, float, float, float]) -> Quaternion:
"""Construct an instant of this class from RailLabel JSON data."""
return Quaternion(x=json[0], y=json[1], z=json[2], w=json[3])

@classmethod
def fromdict(cls, data_dict: dict) -> Quaternion:
Expand Down
7 changes: 1 addition & 6 deletions raillabel/format/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ def from_json(cls, json: JSONTransformData) -> Transform:
"""Construct an instant of this class from RailLabel JSON data."""
return Transform(
position=Point3d.from_json(json.translation),
quaternion=Quaternion(
x=json.quaternion[0],
y=json.quaternion[1],
z=json.quaternion[2],
w=json.quaternion[3],
),
quaternion=Quaternion.from_json(json.quaternion),
)

@classmethod
Expand Down
5 changes: 5 additions & 0 deletions tests/test_raillabel/format/test_quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def quaternion() -> dict:
# == Tests ============================


def test_from_json(quaternion, quaternion_dict):
actual = Quaternion.from_json(quaternion_dict)
assert actual == quaternion


def test_fromdict():
quaternion = Quaternion.fromdict([0.75318325, -0.10270147, 0.21430262, -0.61338551])

Expand Down

0 comments on commit cd3d7b7

Please sign in to comment.