Skip to content

Commit

Permalink
Add magnitude function
Browse files Browse the repository at this point in the history
  • Loading branch information
cmower committed Jul 18, 2024
1 parent f3c6976 commit f0f0ae9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions spatial_casadi/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ def inv(self):
normalize=not isinstance(self._quat, casadi.SX),
)

def magnitude(self):
"""! Get the magnitude of the rotation."""
quat = self._quat
return 2. * casadi.arctan2(casadi.norm_fro(quat[:3]), casadi.fabs(quat[3]))

#
# From methods
#
Expand Down Expand Up @@ -597,6 +602,10 @@ def symbolic():
t = casadi.SX.sym("t", 3)
return Translation(t)

def magnitude(self):
"""! Get the magnitude of the translation."""
return casadi.norm_fro(self._t)

@staticmethod
def from_vector(t):
"""! Initialize from translation vector.
Expand Down
13 changes: 12 additions & 1 deletion test/test_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,18 @@ def test_Rotation_inv():
for _ in range(NUM_RANDOM):
r = Rotation.random()
R = Rot.from_quat(r.as_quat().toarray().flatten())
assert np.allclose(r.inv().as_quat().toarray().flatten(), R.inv().as_quat())
lhs = r.inv().as_quat().toarray().flatten()
rhs = R.inv().as_quat()
assert np.allclose(lhs, rhs) or np.allclose(lhs, -rhs)


def test_Rotation_magnitiude():
for _ in range(NUM_RANDOM):
r = Rotation.random()
R = Rot.from_quat(r.as_quat().toarray().flatten())
lhs = r.magnitude()
rhs = R.magnitude()
assert np.allclose(lhs, rhs)


def test_Rotation_from_matrix():
Expand Down
9 changes: 9 additions & 0 deletions test/test_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@ def test_Translation_as_matrix():
T[:3, :3] = Rotation.identity().as_matrix().toarray()
t = Translation.from_matrix(T)
assert np.allclose(t.as_matrix().toarray(), T)


def test_Translation_magnitude():
for _ in range(NUM_RANDOM):
t = Translation.random()
t_np = t.as_vector().toarray().flatten()
magn_np = np.linalg.norm(t_np)
magn_sc = t.magnitude().toarray()
assert np.allclose(magn_np, magn_sc)

0 comments on commit f0f0ae9

Please sign in to comment.