-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathprinttform.m
181 lines (166 loc) · 5.34 KB
/
printtform.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
%PRINTTFORM Compact display of 3D rotation or pose
%
% PRINTTFORM(T) displays pose in a compact single-line format. Pose is
% given as 4x4 homogoneous transform or as an se3, rigidtform3d or Twist
% object. If T is a sequence, a 4x4xN matrix or a vector of objects, then
% each element is printed on a separate line.
%
% PRINTTFORM(R) as above but displays a 3D rotation expressed as a 3x3
% rotation matrix, or as an so3 or quaternion object.
%
% S = PRINTTFORM(...) as above but returns the string for a single pose
% only.
%
% Options:
%
% mode - Display mode for rotational component as one of "rpy | "xyz" |
% "zyx" | "yxz" | "eul" | "euler" | "axang"
% unit - Rotational units "rad" (default) or "deg"
% fmt - Format string for all numbers, (default "%.2g" or "%8.2g" for
% the sequence case)
% label - Label text to display to the left of the pose. If not given,
% and a variable is passed then the variable name is used, use
% label="" to suppress.
% fid - File identifier to write string to (defaults to stdout)
%
% Examples:
% >> T = se3(rotmx(0.3),[1 2 3])
% >> printtform(T)
% t = (1, 2, 3), RPY/zyx = (0, 0, 0.3) rad
%
% >> printtform(T, label="A")
% A: t = (1, 2, 3), RPY/zyx = (0, 0, 0.3) rad
%
% >> printtform(T, mode="axang")
% t = (1, 2, 3), R = (0.3rad | 1, 0, 0)
%
%
% See also ROTM2EUL, ROTM2AXANG, so3, quaternion, Twist.
% Copyright 2022-2023 Peter Corke, Witold Jachimczyk, Remo Pillat
function out = printtform(X, options)
arguments
X
options.mode (1,1) string = "rpy";
options.fmt (1,1) string = ""
options.unit (1,1) string {mustBeMember(options.unit, ["rad", "deg"])} = "rad"
options.fid (1,1) {mustBeInteger} = 1
options.label (1,1) string = ""
end
switch options.mode
case "rpy"
options.mode = "zyx";
options.orientation = "RPY/" + options.mode;
case {"eul", "euler"}
options.mode = "zyz";
options.orientation = "EUL";
case {"xyz", "zyx", "yxz"}
options.orientation = "RPY/" + options.mode;
case "axang"
otherwise
error("RVC3:printtform:badarg", "bad orientation specified")
end
% convert various object instances to a native matrix: 4x4 or 3x3
if istform(X)
T = X;
elseif isrotm(X)
T = X;
elseif isa(X, "so3")
T = X.rotm;
elseif isa(X, "quaternion")
T = X.rotmat("point");
elseif isa(X, "se3")
T = X.tform;
elseif isa(X, "Twist")
T = X.tform;
elseif isa(X, "rigidtform3d")
T = X.T();
else
error("RVC3:printtform:badarg", "unknown type of pose")
end
% Print the label for this pose
% label not given, use variable name if available
% label="", show no label
% label="label", show the given label
if ~isfield(options, "label")
options.label = string(inputname(1));
end
if size(T,3) == 1
% scalar value
if options.fmt == ""
options.fmt = "%.3g";
end
s = tr2s(T, options);
if nargout == 0
fprintf(options.fid, s + "\n");
else
out = s;
end
else
% a sequence
if options.fmt == ""
options.fmt = "%8.2g";
end
for i=1:size(T,3)
% for each SE(3) matrix in a 3D array
s = tr2s(T(:,:,i), options);
if nargout == 0
fprintf(options.fid, s+"\n");
end
end
end
end
function s = tr2s(T, options)
% print the label is required
if options.label ~= ""
s = sprintf("%8s: ", options.label);
else
s = "";
end
if all(size(T) == [4 4])
% print the translational part if it exists
s = s + sprintf("t = (%s), ", vec2s(options.fmt, tform2trvec(T)));
R = tform2rotm(T);
else
R = T;
end
% print the angular part in various representations
switch (options.mode)
case "axang"
% as a vector and angle
aa = rotm2axang(R);
th = aa(4); v = aa(1:3);
if th == 0
s = s + sprintf(" R = nil");
elseif options.unit == "rad"
s = s + sprintf(" R = (%srad | %s)", ...
sprintf(options.fmt, th), vec2s(options.fmt, v));
elseif options.unit == "deg"
s = s + sprintf(" R = (%sdeg | %s)", ...
sprintf(options.fmt, rad2deg(th)), vec2s(options.fmt,v));
end
otherwise
% angle as a 3-vector
ang = rotm2eul(R, options.mode);
if options.mode == "zyz"
% put RPY angles in RPY order
ang = fliplr(ang);
end
if options.unit == "rad"
s = s + sprintf("%s = (%s) rad", options.orientation, vec2s(options.fmt, ang));
elseif options.unit == "deg"
s = s + sprintf("%s = (%s) deg", options.orientation, vec2s(options.fmt, rad2deg(ang)));
end
end
end
function s = vec2s(fmt, v)
% stringify a vector with specified format string and comma separated
% values
s = [];
for i=1:length(v)
if abs(v(i)) < 1e-12
v(i) = 0;
end
s = [s sprintf(fmt, v(i))]; %#ok<AGROW>
end
s = join(s, ", ");
end