Skip to content

Commit 36483ff

Browse files
authored
refactor: remove habitat_sim dependency in motor_policies.py (#79)
1 parent 6b7eec9 commit 36483ff

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

src/tbp/monty/frameworks/models/motor_policies.py

+15-23
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import os
1616
from typing import Any, Callable, Dict, List, Tuple, Type, Union, cast
1717

18-
import habitat_sim.utils as hab_utils
1918
import numpy as np
19+
import quaternion as qt
2020
import scipy.ndimage
2121
from scipy.spatial.transform import Rotation as rot # noqa: N813
2222

@@ -1150,7 +1150,7 @@ def tangential_direction(self) -> VectorXYZ:
11501150
self.tangential_angle * (1 - self.alpha) + new_target_direction * self.alpha
11511151
)
11521152

1153-
direction = hab_utils.quat_rotate_vector(
1153+
direction = qt.rotate_vectors(
11541154
self.state[self.agent_id]["rotation"],
11551155
[
11561156
np.cos(self.tangential_angle - np.pi / 2),
@@ -1220,19 +1220,14 @@ def get_inverse_agent_rot(self):
12201220
identity pose, which will be aquired by transforming the original pose by the
12211221
inverse
12221222
1223-
NB Magnum appears to be a library used by Habitat; i.e. it is still grounded
1224-
in quaternions
1225-
12261223
Returns:
12271224
Inverse quaternion rotation.
12281225
"""
1229-
inverse_magnum_rotation = hab_utils.common.quat_to_magnum(
1230-
self.state["agent_id_0"]["rotation"]
1231-
).inverted()
1232-
inverse_quaternion_rotation = hab_utils.common.quat_from_magnum(
1233-
inverse_magnum_rotation
1234-
)
1235-
return inverse_quaternion_rotation
1226+
# Note that quaternion format is [w, x, y, z]
1227+
[w, x, y, z] = qt.as_float_array(self.state[self.agent_id]["rotation"])
1228+
# Note that scipy.spatial.transform.Rotation (v1.10.0) format is [x, y, z, w]
1229+
[x, y, z, w] = rot.from_quat([x, y, z, w]).inv().as_quat()
1230+
return qt.quaternion(w, x, y, z)
12361231

12371232
def orienting_angle_from_normal(self, orienting: str) -> float:
12381233
"""Compute turn angle to face the object.
@@ -1250,7 +1245,7 @@ def orienting_angle_from_normal(self, orienting: str) -> float:
12501245

12511246
inverse_quaternion_rotation = self.get_inverse_agent_rot()
12521247

1253-
rotated_point_normal = hab_utils.quat_rotate_vector(
1248+
rotated_point_normal = qt.rotate_vectors(
12541249
inverse_quaternion_rotation, original_point_normal
12551250
)
12561251
x, y, z = rotated_point_normal
@@ -1603,9 +1598,7 @@ def perform_pc_guided_step(self):
16031598
# Rotate the tangential vector to be in the coordinate frame of the sensory
16041599
# agent (rather than the global reference frame of the environment)
16051600
inverse_quaternion_rotation = self.get_inverse_agent_rot()
1606-
rotated_form = hab_utils.quat_rotate_vector(
1607-
inverse_quaternion_rotation, selected_pc_dir
1608-
)
1601+
rotated_form = qt.rotate_vectors(inverse_quaternion_rotation, selected_pc_dir)
16091602

16101603
# Before updating the representation and removing z-axis direction, check
16111604
# for movements defined in the z-axis
@@ -1654,7 +1647,7 @@ def perform_pc_guided_step(self):
16541647
self.reset_pc_buffers()
16551648
self.following_heading_counter = 0
16561649

1657-
return hab_utils.quat_rotate_vector(
1650+
return qt.rotate_vectors(
16581651
self.state["agent_id_0"]["rotation"],
16591652
self.tangential_vec,
16601653
)
@@ -1675,7 +1668,7 @@ def perform_pc_guided_step(self):
16751668
self.following_pc_counter += 1
16761669
self.continuous_pc_steps += 1
16771670

1678-
return hab_utils.quat_rotate_vector(
1671+
return qt.rotate_vectors(
16791672
self.state["agent_id_0"]["rotation"],
16801673
self.tangential_vec,
16811674
)
@@ -1739,7 +1732,7 @@ def perform_standard_tang_step(self):
17391732

17401733
self.following_heading_counter += 1
17411734

1742-
return hab_utils.quat_rotate_vector(
1735+
return qt.rotate_vectors(
17431736
self.state["agent_id_0"]["rotation"],
17441737
self.tangential_vec,
17451738
)
@@ -1895,10 +1888,9 @@ def avoid_revisiting_locations(
18951888
# locations to the headings (also in the reference frame of the agent) that
18961889
# we might take
18971890
# TODO could vectorize this
1898-
rotated_locs = [
1899-
hab_utils.quat_rotate_vector(inverse_quaternion_rotation, point)
1900-
for point in adjusted_prev_locs
1901-
]
1891+
rotated_locs = qt.rotate_vectors(
1892+
inverse_quaternion_rotation, adjusted_prev_locs
1893+
)
19021894

19031895
# Until we have not found a direction that we can guarentee is
19041896
# in a new heading, continue to attempt new directions

0 commit comments

Comments
 (0)