Skip to content

Commit c7a8cd8

Browse files
committed
✨ Add function inv_rotation
The function `inv_rotation` can be used to return the inverse rotation depending on the type of the argument (DCM or Quaternion).
1 parent 9f2fd01 commit c7a8cd8

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

src/ReferenceFrameRotations.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ include("compose_rotations.jl")
7777
include("DCM.jl")
7878
include("euler_angle_axis.jl")
7979
include("euler_angles.jl")
80+
include("inv_rotations.jl")
8081
include("quaternion.jl")
8182

8283
end # module

src/inv_rotations.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export inv_rotation
2+
3+
################################################################################
4+
# Inverse Rotations
5+
################################################################################
6+
7+
"""
8+
### @inline function inv_rotation(R)
9+
10+
Compute the inverse rotation of `R`, which can be a Direction Cosine Matrix or
11+
Quaternion.
12+
13+
The output will have the same type as `R` (DCM or quaternion).
14+
15+
##### Args
16+
17+
* R: Rotation that will be inversed.
18+
19+
##### Returns
20+
21+
The inverse rotation.
22+
23+
##### Remarks
24+
25+
If `R` is a DCM, than its transpose is computed instead of its inverse to reduce
26+
the computational burden. The both are equal if the DCM has unit norm. This must
27+
be verified by the used.
28+
29+
If `R` is a quaternion, than its conjugate is computed instead of its inverse to
30+
reduce the computational burden. The both are equal if the quaternion has unit
31+
norm. This must be verified by the used.
32+
33+
"""
34+
@inline inv_rotation(D::DCM) = D'
35+
@inline inv_rotation(q::Quaternion) = conj(q)

test/test_dcm.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@ for k = 1:samples
8080
dcm3 = dcm2*dcm1
8181

8282
# Check if they are orthonormal.
83-
@test norm(dcm3'-inv(dcm1)*inv(dcm2)) < 1e-10
83+
@test norm(inv_rotation(dcm3)-inv(dcm1)*inv(dcm2)) < 5e-10
8484
end

test/test_quaternions.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ for i = 1:samples
1919
v = [0;randn();randn()]
2020

2121
# Rotate the reference using the quaternion.
22-
v_r = vect(inv(q)*v*q)
22+
v_r = vect(inv_rotation(q)*v*q)
2323

2424
# Get the sine of the angle between the representations.
2525
sin_ang = (cross(v_r, v)/norm(v)^2)[1]
@@ -49,7 +49,7 @@ for i = 1:samples
4949
v = [randn();0;randn()]
5050

5151
# Rotate the reference using the quaternion.
52-
v_r = vect(inv(q)*v*q)
52+
v_r = vect(inv_rotation(q)*v*q)
5353

5454
# Get the sine of the angle between the representations.
5555
sin_ang = (cross(v_r, v)/norm(v)^2)[2]
@@ -80,7 +80,7 @@ for i = 1:samples
8080
v = [randn();randn();0]
8181

8282
# Rotate the reference using the quaternion.
83-
v_r = vect(inv(q)*v*q)
83+
v_r = vect(inv_rotation(q)*v*q)
8484

8585
# Get the sine of the angle between the representations.
8686
sin_ang = (cross(v_r, v)/norm(v)^2)[3]
@@ -173,3 +173,7 @@ qc = conj(q)
173173
@test qc.q1 == -6
174174
@test qc.q2 == -9
175175
@test qc.q3 == -12
176+
177+
q = Quaternion(1.0,1.0,1.0,1.0)
178+
@test inv_rotation(q) != inv(q)
179+
@test (q*inv(q))[:] [1;0;0;0]

0 commit comments

Comments
 (0)