-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDemoDisplay.m
More file actions
278 lines (241 loc) · 8.93 KB
/
DemoDisplay.m
File metadata and controls
278 lines (241 loc) · 8.93 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
classdef DemoDisplay < DemoConfig & handle
% Copyright 2011 Scientific Systems Company Inc., New BSD License
properties (Constant = true, GetAccess = private)
gamma = 2; % (2) nonlinearity of trajectory transparency
bigSteps = 10; % (10) number of body frames per trajectory
subSteps = 10; % (10) number of line segments between body frames
colorBackground = [1, 1, 1]; % ([1, 1, 1]) color of figure background
colorHighlight = [1, 0, 0]; % ([1, 0, 0]) color of objects to emphasize
colorReference = [0, 1, 0]; % ([0, 1, 0]) color of reference objects
end
properties (SetAccess = private, GetAccess = private)
hfigure
haxes
index
tRef
pRef
qRef
xDefault
end
methods (Access = public, Static = true)
function this = DemoDisplay(initialTime, uri)
if(this.textOnly)
return;
end
this.hfigure = figure;
set(this.hfigure, 'Color', this.colorBackground);
set(this.hfigure, 'Units', 'pixels');
set(this.hfigure, 'Position', [0, 0, this.width, this.height]);
this.haxes = axes('Parent', this.hfigure, 'Clipping', 'off');
set(this.haxes, 'Box', 'on');
set(this.haxes, 'Projection', 'Perspective');
set(this.haxes, 'Units', 'normalized');
set(this.haxes, 'DataAspectRatio', [1, 1, 1]);
set(this.haxes, 'Position', [0, 0, 1, 1]);
set(this.haxes, 'CameraTargetMode', 'manual');
set(this.haxes, 'CameraPositionMode', 'manual');
set(this.haxes, 'XLimMode', 'manual');
set(this.haxes, 'YLimMode', 'manual');
set(this.haxes, 'ZLimMode', 'manual');
set(this.haxes, 'Visible', 'on');
set(this.haxes, 'NextPlot', 'add');
this.index = uint32(0);
this.xDefault = tom.DynamicModel.create('tom', initialTime, uri);
this.tRef = [];
this.pRef = [];
this.qRef = [];
if(nargin>0)
if(~strncmp(uri, 'hidi:', 5))
error('URI scheme not recognized');
end
container = hidi.DataContainer.create(uri(6:end), initialTime);
if(hasReferenceTrajectory(container))
xRef = getReferenceTrajectory(container);
this.tRef = this.generateSampleTimes(xRef);
poseRef = xRef.evaluate(this.tRef);
this.pRef = cat(2, poseRef.p);
this.qRef = cat(2, poseRef.q);
end
end
end
end
methods (Access = public)
% Visualize a set of trajectories with optional transparency
%
% INPUT
% x = trajectory instances, tom.Trajectory N-by-1
% c = costs, double N-by-1
function put(this, x, c)
this.index = this.index+uint32(1);
% fill missing arguments with defaults
if(nargin<3)
c=0;
end
if(nargin<2)
x = this.xDefault;
end
% handle case of no trajectories
K = numel(x);
if(K<1)
return;
end
% compute minimium cost
costBest = min(c);
kBest = find(c==costBest, 1, 'first');
alpha = this.cost2alpha(c);
% text display
fprintf('\n');
fprintf('\nindex: %d', this.index);
fprintf('\ncost(%d): %0.6f', [1:numel(c); c']);
fprintf('\nbest(%d): %0.6f', kBest, costBest);
if(this.textOnly)
return;
end
% generate sample times assuming all trajectories have the same domain
t = this.generateSampleTimes(x(1));
% clear the figure
figure(this.hfigure);
cla(this.haxes);
% evaluate the best trajectory
k = kBest;
poseBest = x(k).evaluate(t);
pBest = cat(2, poseBest.p);
qBest = cat(2, poseBest.q);
% compute scene parameters from best trajectory or ground truth if available
if(isempty(this.pRef))
[origin, avgSiz, avgPos, cameraPosition] = computeScene(pBest, this.index);
summaryText = sprintf('cost = %0.6f', costBest);
else
[origin, avgSiz, avgPos, cameraPosition] = computeScene(this.pRef, this.index);
pDif = pBest-this.pRef; % position comparison
pDif = sqrt(sum(pDif.*pDif, 1));
qDif = acos(sum(qBest.*this.qRef, 1)); % quaternion comparison
pTwoNorm = twoNorm(pDif);
pInfNorm = infNorm(pDif);
qTwoNorm = twoNorm(qDif);
qInfNorm = infNorm(qDif);
summaryText = sprintf([' costBest = %0.6f\npositionTwoNorm = %0.6f\npositionInfNorm = %0.6f', ...
'\nrotationTwoNorm = %0.6f\nrotationInfNorm = %0.6f', ...
'\n originX = %0.2f\n originY = %0.2f\n originZ = %0.2f'], ...
costBest, pTwoNorm, pInfNorm, qTwoNorm, qInfNorm, origin(1), origin(2), origin(3));
end
% plot ground truth if available in reference color
if(~isempty(this.pRef))
this.plotIndividual(origin, this.pRef, this.qRef, avgSiz, 1, this.colorReference, 'LineWidth', 1);
end
% plot best trajectory in a highlight color
this.plotIndividual(origin, pBest, qBest, avgSiz, alpha(k), this.colorHighlight, 'LineWidth', 1);
% plot other trajectories in background contrast color
if(~this.bestOnly)
for k = 1:K
if(k~=kBest)
posek = x(k).evaluate(t);
pk = cat(2, posek.p);
qk = cat(2, posek.q);
this.plotIndividual(origin, pk, qk, avgSiz, alpha(k), 1-this.colorBackground, 'LineWidth', 1);
end
end
end
% set axes properties being careful with large numbers
text(avgPos(1), avgPos(2), avgPos(3)+avgSiz, summaryText, 'FontName', 'Courier', 'FontSize', 9);
set(this.haxes, 'CameraTarget', avgPos');
set(this.haxes, 'CameraPosition', cameraPosition);
set(this.haxes, 'XLim', avgPos(1)+avgSiz*[-1, 1]);
set(this.haxes, 'YLim', avgPos(2)+avgSiz*[-1, 1]);
set(this.haxes, 'ZLim', avgPos(3)+avgSiz*[-1, 1]);
drawnow;
% save snapshot
if(this.saveFigure)
imwrite(fbuffer(this.hfigure), sprintf('%06d.png', this.index));
end
end
end
methods (Access = private)
function alpha = cost2alpha(this, c)
if(numel(c)==1)
alpha = 1;
else
cMax = max(c);
fitness = cMax-c;
alpha = (fitness/max([fitness; eps])).^this.gamma;
end
end
function t = generateSampleTimes(this, x)
assert(numel(x)==1);
interval = x.domain();
tmin = interval.first;
tmax = interval.second;
if(~isempty(this.tRef))
tmin = max(tmin, this.tRef(1));
tmax = min(tmax, this.tRef(end));
end
if(tmin==tmax)
t = repmat(tmin, [1, this.bigSteps*this.subSteps+1]);
else
tmax(isinf(tmax)) = tmin+this.infinity; % prevent NaN
t = tmin:((tmax-tmin)/this.bigSteps/this.subSteps):tmax;
end
end
function plotIndividual(this, origin, p, q, scale, alpha, color, varargin)
p = p-repmat(origin, [1, size(p, 2)]);
plot3(p(1, :), p(2, :), p(3, :), 'Color', alpha*color+(1-alpha)*ones(1, 3), 'Clipping', 'off', varargin{:});
this.plotFrame(p(:, 1), q(:, 1), scale, alpha); % plot first frame
for bs = 1:this.bigSteps
ksub = (bs-1)*this.subSteps+(1:(this.subSteps+1));
this.plotFrame(p(:, ksub(end)), q(:, ksub(end)), scale, alpha);
end
end
% Plot a triangle indicating body axes as in "the tail of an airplane"
function plotFrame(this, p, q, scale, alpha)
M = (0.05*scale)*tom.Rotation.quatToMatrix(q);
xp = p(1)+[M(1, 1); 0; -0.5*M(1, 3)];
yp = p(2)+[M(2, 1); 0; -0.5*M(2, 3)];
zp = p(3)+[M(3, 1); 0; -0.5*M(3, 3)];
patch(xp, yp, zp, this.colorHighlight, 'FaceAlpha', alpha, 'LineStyle', 'none', 'Clipping', 'off');
end
end
end
function [origin, avgSiz, avgPos, cameraPosition] = computeScene(pScene, index)
origin = pScene(:, 1);
avgPos = sum(pScene/size(pScene, 2), 2)-origin;
avgSiz = twoNorm(max(pScene, [], 2)-min(pScene, [], 2));
if(avgSiz<eps)
avgSiz = 1;
end
cameraPosition = avgPos'+avgSiz*[8*cos(double(index)/30), 8*sin(double(index)/30), 4];
end
function y = twoNorm(x)
y = sqrt(sum(x.*x)/numel(x));
end
function y = infNorm(x)
y = max(abs(x));
end
% Captures a figure via an offscreen buffer
%
% INPUT
% hfig = handle to a MATLAB figure
%
% OUTPUT
% cdata = color image in uint8, M-by-N-by-3
function cdata = fbuffer(hfig)
pos = get(hfig, 'Position');
%noanimate('save', hfig);
gldata = opengl('data');
if( strcmp(gldata.Renderer, 'None') )
mode = get(hfig, 'PaperPositionMode');
set(hfig, 'PaperPositionMode', 'auto', 'InvertHardcopy', 'off');
cdata = hardcopy(hfig, '-dzbuffer', '-r0');
set(hfig, 'PaperPositionMode', mode);
else
sppi = get(0, 'ScreenPixelsPerInch');
ppos = get(hfig, 'PaperPosition');
pos(1:2) = 0;
set(hfig, 'PaperPosition', pos./sppi);
cdata = hardcopy(hfig, '-dopengl', ['-r', num2str(round(sppi))]);
set(hfig, 'PaperPosition', ppos);
end
%noanimate('restore', hfig);
if( numel(cdata)>(pos(3)*pos(4)*3) )
cdata = cdata(1:pos(4), 1:pos(3), :);
end
end