Skip to content

Commit

Permalink
ok I fixed exp and log up to atol=1e-3 at least; should be good enoug…
Browse files Browse the repository at this point in the history
…h. I think all remaining issues are from the different code path when the weight_norm is 0 of the incoming exp/log
  • Loading branch information
zoemcc committed Jul 16, 2023
1 parent 2198460 commit 2c28385
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
24 changes: 16 additions & 8 deletions src/element_construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ function Base.log(m::Motor3D)
b * m[1],
b * m[2],
b * m[3],
c * m[1] + b * m[5],
c * m[2] + b * m[6],
c * m[3] + b * m[7])
b * m[5] - c * m[1],
b * m[6] - c * m[2],
b * m[7] - c * m[3])
end
end

function Base.exp(axis::Line3D)
# https://arxiv.org/abs/2206.07496 section 8.2
l = axis[1] * axis[1] + axis[2] * axis[2] + axis[3] * axis[3]
l = line_vector(axis) line_vector(axis)
if l 0
return Motor3D(0, 0, 0, 1, axis[4], axis[5], axis[6], 0)
else
m = axis[1] * axis[4] + axis[2] * axis[5] + axis[3] * axis[6]
m = -(line_vector(axis) line_moment(axis))
a = sqrt(l)
c = cos(a)
s = sin(a) / a
Expand All @@ -96,13 +96,21 @@ function Base.exp(axis::Line3D)
s * axis[2],
s * axis[3],
c,
s * axis[4] + t * axis[1],
s * axis[5] + t * axis[2],
s * axis[6] + t * axis[3],
s * axis[4] - t * axis[1],
s * axis[5] - t * axis[2],
s * axis[6] - t * axis[3],
m * s)
end
end

#=
function Base.exp(axis::Line3D)
# from 3D_PGA_Cheat_sheet
l = axis[1] * axis[1] + axis[2] * axis[2] + axis[3] * axis[3]
m =
end
=#


#=
function motor_line_exp(axis::Line3D)
Expand Down
9 changes: 9 additions & 0 deletions src/motor3D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,21 @@ weight_norm(a::Motor3D) = norm(SA[a[1], a[2], a[3], a[4]])
bulk_norm(a::Motor3D) = norm(SA[a[5], a[6], a[7], a[8]])

unitize(a::Motor3D) = Motor3D(internal_vec(a) .* (1 / weight_norm(a)))
function LinearAlgebra.normalize(m::Motor3D)
mmrev = m * reverse(m)
s, t = mmrev[4], mmrev[8]
s2 = sqrt(s)
study_inv_sqrt = Motor3D(0, 0, 0, 1 / s2, 0, 0, 0, t / (2 * s2^3))
return m * study_inv_sqrt
end
#=
function LinearAlgebra.normalize(m::Motor3D)
A = 1 / weight_norm(m)
B = (m[4] * m[8] - (m[1] * m[5] + m[2] * m[6] + m[3] * m[7])) * A * A * A
Motor3D(A * m[1], A * m[2], A * m[3], A * m[4],
A * m[5] + B * m[1], A * m[6] + B * m[2], A * m[7] + B * m[3], A * m[8] - B * m[4])
end
=#

reverse(a::Motor3D) = Motor3D(-a[1], -a[2], -a[3], a[4], -a[5], -a[6], -a[7], a[8])
anti_reverse(a::Motor3D) = reverse(a)
Expand Down
6 changes: 3 additions & 3 deletions test/test_motor3D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ end
testmotornormed = normalize(testmotor)
@test isapprox(weight_norm(testmotornormed), 1.0; atol=0.0001)
#@info testmotornormed
#@test isapprox(dot(testmotornormed[1:4], testmotornormed[5:8]), 0.0; atol=0.0001)
@test isapprox(dot(testmotornormed[1:4], testmotornormed[5:8]), 0.0; atol=0.0001)
testmotornormedsq = testmotornormed * PGA3D.reverse(testmotornormed)
#@info testmotornormedsq
#@test isapprox(testmotornormedsq, identity_motor(); atol=0.0001)
#@test get_transform_matrix(testmotor) ≈ get_transform_matrix(testmotornormed)
@test isapprox(testmotornormedsq, identity_motor(); atol=0.0001)
@test get_transform_matrix(testmotor) get_transform_matrix(testmotornormed)
testmotorsq = testmotor * PGA3D.reverse(testmotor)
#@info testmotorsq
#@info testmotornormedsq
Expand Down
24 changes: 18 additions & 6 deletions test/test_transformations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,32 @@ using PGA3D, Test, SafeTestsets, Logging, PrettyPrinting, StaticArrays, Random

@safetestset "Motor log and bivector exp" begin
using PGA3D, Test, SafeTestsets, Logging, PrettyPrinting, StaticArrays, Random, LinearAlgebra
Random.seed!(1)
motor_identity = identity_motor()
for i in 1:1000
for i in 1:2000
testfrom = Point3D(randn(3)...)
testto = Point3D(randn(3)...)
testline = line_fromto(testfrom, testto)
testangle = rand()
testdisp = rand()
testmotor = normalize(motor_screw(testline, testangle, testdisp))
@test testmotor * PGA3D.reverse(testmotor) motor_identity

testbv = log(testmotor)

testmotorexp = normalize(exp(testbv))
testmotorexp = exp(testbv)
@test testmotorexp * PGA3D.reverse(testmotorexp) motor_identity
#@info testmotor
#@info testbv
#@info testmotorexp

@test testmotorexp testmotor || testmotorexp -testmotor
atol = 1e-3
@test isapprox(testmotorexp, testmotor; atol=atol) || isapprox(testmotorexp, -testmotor; atol=atol) || isapprox(testmotorexp * PGA3D.reverse(testmotor), motor_identity; atol=atol) || isapprox(testmotorexp * PGA3D.reverse(testmotor), -motor_identity; atol=atol)
if !(isapprox(testmotorexp, testmotor; atol=atol) || isapprox(testmotorexp, -testmotor; atol=atol))
@info testmotor
@info testbv
@info testmotorexp
end

testbvha = testbv * 0.5
testmotorexpha = normalize(exp(testbvha))
Expand All @@ -127,7 +139,7 @@ using PGA3D, Test, SafeTestsets, Logging, PrettyPrinting, StaticArrays, Random
#@info "testmotor: $testmotor"
#@info "testmotorexpha: $testmotorexpha"
#@info "testmotorexpha2: $testmotorexpha2"
@test isapprox(testmotorexpha2, testmotor; atol=0.1) || isapprox(testmotorexpha2, -testmotor; atol=0.1)
@test isapprox(testmotorexpha2, testmotor; atol=atol) || isapprox(testmotorexpha2, -testmotor; atol=atol) || isapprox(testmotorexpha2 * PGA3D.reverse(testmotor), motor_identity; atol=atol) || isapprox(testmotorexpha2 * PGA3D.reverse(testmotor), -motor_identity; atol=atol)

testfrom2 = Point3D(randn(3)...)
testto2 = Point3D(randn(3)...)
Expand All @@ -141,15 +153,15 @@ using PGA3D, Test, SafeTestsets, Logging, PrettyPrinting, StaticArrays, Random

#@info "testmotor2: $testmotor2"
#@info "testmotor221: $testmotor221"
#@test isapprox(testmotor221, testmotor2; atol=0.1) || isapprox(testmotor221, -testmotor2; atol=0.1)
@test testmotor221 testmotor2 || testmotor221 -testmotor2

testbv21 = log(testmotor21)
testmotor212 = normalize(exp(testbv21))
testmotor222 = normalize(testmotor212 * testmotor)

#@info "testmotor2: $testmotor2"
#@info "testmotor222: $testmotor222"
#@test isapprox(testmotor222, testmotor2; atol=0.1) || isapprox(testmotor222, -testmotor2; atol=0.1)
@test isapprox(testmotor222, testmotor2; atol=atol) || isapprox(testmotor222, -testmotor2; atol=atol)


end
Expand Down

0 comments on commit 2c28385

Please sign in to comment.