Skip to content

Commit 95a5ae5

Browse files
andreasstoltAndreas Stolt
and
Andreas Stolt
authored
Make logR work also when the total rotation is pi rad (#31)
* Add tests for logR that currently fails * Add special version of logR for the case when the rotation is close to pi rad Co-authored-by: Andreas Stolt <andreas.stolt@cognibotics.org>
1 parent e6b5775 commit 95a5ae5

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/utils.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ end
117117
function logR(R)
118118
@assert isrot(R)
119119
phi = acos((min(tr(R),3)-1)/2)
120+
if abs(phi-π) < 1e-5
121+
return logR_special(R)
122+
end
120123
if abs(phi) < 1e-10
121124
ω = (R-R')/2
122125
else
@@ -125,6 +128,51 @@ function logR(R)
125128
return ω
126129
end
127130

131+
"""Special variant of logR that handles the case when the rotation is π rad"""
132+
function logR_special(R)
133+
qs = sqrt(abs(tr(R) + 1))/2
134+
kx = R[3, 2] - R[2, 3]
135+
ky = R[1, 3] - R[3, 1]
136+
kz = R[2, 1] - R[1, 2]
137+
138+
if R[1, 1] >= R[2, 2] && R[1, 1] >= R[3, 3]
139+
kx1 = R[1, 1] - R[2, 2] - R[3, 3] + 1
140+
ky1 = R[2, 1] + R[1, 2]
141+
kz1 = R[3, 1] + R[1, 3]
142+
add = kx >= 0
143+
elseif R[2, 2] >= R[3, 3]
144+
kx1 = R[2, 1] + R[1, 2]
145+
ky1 = R[2, 2] - R[1, 1] - R[3, 3] + 1
146+
kz1 = R[3, 2] + R[2, 3]
147+
add = (ky >= 0)
148+
else
149+
kx1 = R[3, 1] + R[1, 3]
150+
ky1 = R[3, 2] + R[2, 3]
151+
kz1 = R[3, 3] - R[1, 1] - R[2, 2] + 1
152+
add = (kz >= 0)
153+
end
154+
155+
if add
156+
kx += kx1
157+
ky += ky1
158+
kz += kz1
159+
else
160+
kx -= kx1
161+
ky -= ky1
162+
kz -= kz1
163+
end
164+
165+
v_tmp = SA[kx, ky, kz]
166+
v = @SVector zeros(eltype(R), 3)
167+
divider = norm(v_tmp)
168+
if divider > 1e-8
169+
v = v_tmp ./ divider
170+
end
171+
theta = 2*acos(qs)
172+
ret = skew(v*theta)
173+
return ret
174+
end
175+
128176
"""Calculates the adjoint of a transformation matrix"""
129177
function ad(T)
130178
# Computes the adjoint of transformation matrix T

test/runtests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ end
8989
r,p,y = R2rpy(R, conv="xyz")
9090
@test rpy2R(r,p,y, "xyz") R
9191

92+
r1 = [-1.0 0.0 0.0
93+
0.0 0.0 1.0
94+
0.0 1.0 0.0]
95+
log_r1 = logR(r1)
96+
log_r1_true = [0.0 -π/sqrt(2) π/sqrt(2)
97+
π/sqrt(2) 0.0 0.0
98+
-π/sqrt(2) 0.0 0.0]
99+
@test isapprox(log_r1, log_r1_true)
100+
101+
r2 = [-1.0 0.0 0.0
102+
0.0 -1.0 0.0
103+
0.0 0.0 1.0]
104+
log_r2 = logR(r2)
105+
log_r2_true = [0.0 -π 0.0
106+
π 0.0 0.0
107+
0.0 0.0 0.0]
108+
@test isapprox(log_r2, log_r2_true)
92109
end
93110

94111

0 commit comments

Comments
 (0)