Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix implementation of Transform.inv() #316

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions brax/v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ def to_local(self, t: 'Transform') -> 'Transform':
rot = math.quat_mul(math.quat_inv(t.rot), self.rot)
return Transform(pos=pos, rot=rot)

def inv(self):
def inv(self) -> 'Transform':
"""Invert the transform."""
return Transform(pos=-1.0 * self.pos, rot=math.quat_inv(self.rot))
inv_rot = math.quat_inv(self.rot)
return Transform(pos=math.rotate(-self.pos, inv_rot), rot=inv_rot)

@classmethod
def create(
Expand Down
22 changes: 21 additions & 1 deletion brax/v2/math_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from absl.testing import absltest
from absl.testing import parameterized
from brax.v2 import math
from brax.v2 import base, math
import jax
from jax import numpy as jp
import numpy as np
Expand Down Expand Up @@ -81,5 +81,25 @@ def test_orthogonals(self, i):
self.assertAlmostEqual(np.abs(a.dot(c)), 0, 6)


class TransformInvTest(parameterized.TestCase):
"""Tests Transform.inv()."""

@parameterized.parameters(range(100))
def test_transform_inv(self, i):
np.random.seed(i)
pos = np.random.randn(3)
rot = np.random.randn(4)
rot /= np.linalg.norm(rot)

a = base.Transform(pos, rot)
b = a.inv().do(a)
c = a.do(a.inv())

np.testing.assert_array_almost_equal(b.pos, np.zeros(3))
np.testing.assert_array_almost_equal(b.rot, np.array([1.0, 0.0, 0.0, 0.0]))
np.testing.assert_array_almost_equal(c.pos, np.zeros(3))
np.testing.assert_array_almost_equal(c.rot, np.array([1.0, 0.0, 0.0, 0.0]))


if __name__ == '__main__':
absltest.main()