Skip to content

Commit 6126d2f

Browse files
committed
test: add zero-allocation tests for core helper functions
1 parent b69c7d4 commit 6126d2f

3 files changed

Lines changed: 90 additions & 1 deletion

File tree

src/ModernRoboticsBook.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,17 @@ julia> matrix_log6([1 0 0 0; 0 0 -1 0; 0 1 0 3; 0 0 0 1])
600600
```
601601
"""
602602
function matrix_log6(T::AbstractMatrix)
603-
R, p = transform_to_rotation_position(T)
603+
R = SMatrix{3,3}(
604+
T[1, 1],
605+
T[2, 1],
606+
T[3, 1],
607+
T[1, 2],
608+
T[2, 2],
609+
T[3, 2],
610+
T[1, 3],
611+
T[2, 3],
612+
T[3, 3],
613+
)
604614
ωmat = matrix_log3(R)
605615
I3 = SMatrix{3,3,Float64}(LA.I)
606616
v = SA[T[1, 4], T[2, 4], T[3, 4]]

test/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
44
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
5+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
56
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
67

78
[compat]
89
Aqua = "0.8"
910
JSON = "1.4"
11+
StaticArrays = "1.9"

test/runtests.jl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Aqua
22
using JSON
33
using ModernRoboticsBook
4+
using StaticArrays
45
using Test
56

67
import LinearAlgebra as LA
@@ -1043,6 +1044,82 @@ Aqua.test_all(ModernRoboticsBook)
10431044
@test_throws ErrorException load_robot(:nonexistent_robot)
10441045
end
10451046

1047+
@testset "zero allocations for core helpers" begin
1048+
# Inputs as regular Matrix/Vector
1049+
so3 = [0.0 -3.0 2.0; 3.0 0.0 -1.0; -2.0 1.0 0.0]
1050+
R = [
1051+
-0.694921 0.713521 0.0892929
1052+
-0.192007 -0.303785 0.933192
1053+
0.692978 0.63135 0.348107
1054+
]
1055+
se3 = [
1056+
0.0 -3.0 2.0 4.0
1057+
3.0 0.0 -1.0 5.0
1058+
-2.0 1.0 0.0 6.0
1059+
0.0 0.0 0.0 0.0
1060+
]
1061+
T = [
1062+
1.0 0.0 0.0 0.0
1063+
0.0 0.0 -1.0 0.0
1064+
0.0 1.0 0.0 3.0
1065+
0.0 0.0 0.0 1.0
1066+
]
1067+
v3 = [1.0, 2.0, 3.0]
1068+
v6 = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
1069+
1070+
# Inputs as SMatrix/SVector
1071+
sso3 = SMatrix{3,3}(so3)
1072+
sR = SMatrix{3,3}(R)
1073+
sse3 = SMatrix{4,4}(se3)
1074+
sT = SMatrix{4,4}(T)
1075+
sv3 = SVector{3}(v3)
1076+
sv6 = SVector{6}(v6)
1077+
1078+
# Warm up all functions (trigger compilation)
1079+
vec_to_so3(v3);
1080+
vec_to_so3(sv3)
1081+
so3_to_vec(so3);
1082+
so3_to_vec(sso3)
1083+
matrix_exp3(so3);
1084+
matrix_exp3(sso3)
1085+
matrix_log3(R);
1086+
matrix_log3(sR)
1087+
vec_to_se3(v6);
1088+
vec_to_se3(sv6)
1089+
se3_to_vec(se3);
1090+
se3_to_vec(sse3)
1091+
transform_inv(T);
1092+
transform_inv(sT)
1093+
adjoint_representation(T);
1094+
adjoint_representation(sT)
1095+
matrix_exp6(se3);
1096+
matrix_exp6(sse3)
1097+
matrix_log6(T);
1098+
matrix_log6(sT)
1099+
ad(v6);
1100+
ad(sv6)
1101+
1102+
# Test zero allocations with SMatrix/SVector inputs
1103+
@test @allocated(vec_to_so3(sv3)) == 0
1104+
@test @allocated(so3_to_vec(sso3)) == 0
1105+
@test @allocated(matrix_exp3(sso3)) == 0
1106+
@test @allocated(matrix_log3(sR)) == 0
1107+
@test @allocated(vec_to_se3(sv6)) == 0
1108+
@test @allocated(se3_to_vec(sse3)) == 0
1109+
@test @allocated(transform_inv(sT)) == 0
1110+
@test @allocated(adjoint_representation(sT)) == 0
1111+
@test @allocated(matrix_exp6(sse3)) == 0
1112+
@test @allocated(matrix_log6(sT)) == 0
1113+
@test @allocated(ad(sv6)) == 0
1114+
1115+
# Test zero allocations with regular Matrix/Vector inputs
1116+
@test @allocated(vec_to_so3(v3)) == 0
1117+
@test @allocated(so3_to_vec(so3)) == 0
1118+
@test @allocated(vec_to_se3(v6)) == 0
1119+
@test @allocated(se3_to_vec(se3)) == 0
1120+
@test @allocated(ad(v6)) == 0
1121+
end
1122+
10461123
@testset "cross-validate UR5 against Pinocchio" begin
10471124
# Pinocchio reference uses gravity [0, 0, -9.81] which matches our default
10481125
robot = load_robot(:ur5)

0 commit comments

Comments
 (0)