-
Notifications
You must be signed in to change notification settings - Fork 1
/
vzpVisualizer.m
1266 lines (1093 loc) · 47.5 KB
/
vzpVisualizer.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function vzpVisualizer()
% function vzpVisualizer()
%
% Jonas Lins, December 2019.
%
% Visualization tool for motion capture data from PTI motion trackers. No
% arguments required. Allows streaming from trackers in realtime as well as
% loading vzp or mat file (as obtained from function loadVzpFile).
%
% Note that MEX-files obtained from PTI are required (see startup message).
% NOTE: To debug streaming without a tracker connection uncomment the
% VzGetDat function replacement at the bottom of this file, which will
% "stream" random data points.
% TODO: Make figure user data and store there things like speed, speeds,
% take etc. to make it accessible to callbacks. Then move all logic
% including these to callbacks. This should tidy up this code.
% Set up GUI etc.
secsPerDegreeRotation = 0.01; % rotation speed
histLen = 500; % initial history length in frames (plotted lines)
speeds = [0.1, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 2, 4, 8, 16, 32];
markerBaseLineWidth = 0.5;
waitSecsForBufferUpdate = 0.1; % how long to wait for buffer update when
% getting data. After that the old data is
% returned. (and a flag is set).
% Check existence of PTI functions required for streaming and loading vzp
% files. Warn if not existent.
reqMex = mexext;
sysType = computer;
if strfind(sysType, 'PCWIN')
os = 'MS Windows';
elseif strfind(sysType, 'GLNX')
os = 'Linux';
else
os = 'unknown';
end
if strfind(sysType, '64')
bit = '64 bits';
else
bit = '32 bits';
end
sysNote = ['Note that the current system is ', os, ' ', bit, '. MEX-files for this ', ...
'system have the extension *.', reqMex, '.'];
% required mex-function names
reqFuns = ...
{'VzGetDat', ...
'VzpGetTakeCount', ...
'vzpGetTakeMarkers', ...
'vzpGetTakeFrames', ...
'vzpGetMarkerName', ...
'vzpgetmarkerID', ...
'vzpGetFrameData', ...
'vzpGetTakeFrameRate', ...
'vzpGetTakeCRF'};
reqFunsExt = ...
cellfun(@(nameStr) [nameStr, '.', reqMex], reqFuns, 'uniformoutput', 0);
% Check existence of functions
funFound = [];
for funName = reqFunsExt
funFound(end+1) = exist(funName{1});
end
funFound = funFound == 3;
% Construct strings
fnfStr = {'not found', 'found'};
streamStr = ...
['To stream data from the trackers you need the following MEX-file ', ...
'on your MATLAB path (letter case intentional):\n\n', ...
reqFuns{1}, ' (', fnfStr{funFound(1)+1}, ')', '\n\n'];
loadVzpStr = ...
['To load data from a *.vzp file you need the following MEX-files ', ...
'on your MATLAB path (letter case intentional):\n\n', ...
cell2mat(cellfun(@(n,f) cell2mat([n, ' (', fnfStr(f), ')\n']), ...
reqFuns(2:end), num2cell(funFound(2:end)+1), 'uniformoutput', 0)), '\n'];
% Warn if any missing
introStr = ['Some of the functions required for online-streaming or for loading *.vzp ', ...
'files are missing. These can be obtained directly from Phoenix ', ...
'Technology. Without these functions, you will still be able to ', ...
'load tracking data from *.mat files.\n\n'];
if any(~funFound)
waitfor(...
warndlg(sprintf([introStr, streamStr, loadVzpStr, sysNote]), ...
'Required functions not found'));
end
% set marker colors (in order as they appear in data). Use strings or
% RGB vectors. Can be commented out to cycle through color palette.
% colors = {'g','g','g','g','r','r','r','b','b','b'};
hFig = figure('SizeChangedFcn', @fig_size_cb, 'visible', 'off', ...
'position', [300, 200, 1200, 750], ...
'windowbuttonupfcn', @fig_buttonUp_cb, 'UserData', ...
struct('buttonUp',0), 'name', 'Vzp Data Visualizer', 'numbertitle', ...
'off');
% UI control elements
hBorder = 20;
vBorder = 80;
vSliderBorder = 20;
sep = 5;
height = 20;
fullWidth = (hFig.Position(3) - hBorder*2);
width = (fullWidth-7*sep) / 8;
% use this to get positions for the elements
% h is horizontal grid pos, left to right
% v is vertical grid pos, bottom to top
% hb is horizontal border used (added once to move left from left fig border)
% hb is horizontal border used (added once to move up from lower fig border)
uiPos = @(h,v,hb,vb) [hb+width*(h-1)+sep*(h-1), ...
vb+height*(v-1)+sep*(v-1), ...
width, height];
hFrameSlider = ...
uicontrol(hFig, 'style','slider','units','pixels', ...
'position', uiPos(1,1,hBorder,vSliderBorder), ...
'min', 1, 'max', 1000, 'value', 1, 'tag', 'frameSlider');
addlistener(hFrameSlider,'Value','PreSet',@frameSlider_preSet_cb);
hFrameText = ...
uicontrol(hFig, 'style', 'text', 'units', 'pixels', ...
'position', [hBorder, vSliderBorder+height, fullWidth, height], ...
'string', ...
'Frame n/a of n/a | n/a of n/a s | n/a fps total | n/a fps last sec.', ...
'horizontalAlignment', 'center', 'tooltip', ...
'fps as given by loaded file or as mean over all streamed data');
hFrameText.Units = 'normalized';
hPauseButton = ...
uicontrol(hFig, 'style', 'pushbutton', 'units', 'pixels', ...
'position', uiPos(1,1,hBorder,vSliderBorder+height+sep), ...
'string', 'Pause', 'callback', @pause_cb, 'UserData', 0, 'tag', ...
'pauseButton', 'Enable', 'off');
hHistLenEdit = ...
uicontrol(hFig, 'style', 'edit', 'units', 'pixels', ...
'position', uiPos(1,3,hBorder,vBorder)./[1 1 3 1], ...
'string', ...
num2str(histLen));
hHistLenText = ...
uicontrol(hFig, 'style', 'text', 'units', 'pixels', ...
'position', uiPos(2,3,hBorder,vBorder)./[3 1 1.5 1]+[sep*3 0 0 0], ...
'string', 'History steps', 'horizontalAlignment', 'left');
hSpeedMenu = ...
uicontrol(hFig, 'style', 'popupmenu', 'units', 'pixels', ...
'position', uiPos(1,4,hBorder,vBorder)./[1 1 3 1], ...
'string', strsplit(num2str(speeds)), 'Value', find(speeds==1), 'tag', ...
'speedMenu');
hSpeedText = ...
uicontrol(hFig, 'style', 'text', 'units', 'pixels', ...
'position', uiPos(2,4,hBorder,vBorder)./[3 1 1.5 1]+[sep*3 0 0 0], ...
'string', 'Speed', 'horizontalAlignment', 'left');
hLabelCheckbox = ...
uicontrol(hFig, 'style', 'checkbox', 'units', 'pixels', ...
'position', uiPos(1,6,hBorder,vBorder), 'String', 'Show labels');
hRotateCheckbox = ...
uicontrol(hFig, 'style', 'checkbox', 'units', 'pixels', ...
'position', uiPos(1,7,hBorder,vBorder), 'String', 'Rotation');
hFitAxesToMarkersCheckbox = ...
uicontrol(hFig, 'style', 'checkbox', 'units', 'pixels', ...
'position', uiPos(1,9,hBorder,vBorder), 'String', 'Always fit to frame ', ...
'callback', @fitAxesToMarkersCheckbox_cb, 'tag', 'fitToMarkersCheckbox', ...
'tooltip', ['Continuously adjust axes limits to show all marker ', ...
'positions in current frame (disregarding zero data)']);
hFitAxesToMarkersButton = ...
uicontrol(hFig, 'style', 'pushbutton', 'units', 'pixels', ...
'position', uiPos(1,10,hBorder,vBorder), ...
'string', 'Fit axes to frame', 'UserData', struct('clicked', 0), ...
'callback', @fitAxesToMarkersButton_cb, 'tooltip', ...
['Adjust axes limits to show all marker ', ...
'positions in current frame (disregarding zero data)']);
hFitAxesToHistCheckbox = ...
uicontrol(hFig, 'style', 'checkbox', 'units', 'pixels', ...
'position', uiPos(1,11,hBorder,vBorder), 'String', 'Always fit to history', ...
'callback', @fitAxesToHistCheckbox_cb, 'tag', 'fitToHistCheckbox', ...
'tooltip', ...
['Continuously adjust axes limits to show data in all history frames ', ...
' (disregarding zero data)']);
hFitAxesToHistButton = ...
uicontrol(hFig, 'style', 'pushbutton', 'units', 'pixels', ...
'position', uiPos(1,12,hBorder,vBorder), ...
'string', 'Fit axes to history', 'UserData', struct('clicked', 0), ...
'callback', @fitAxesToHistButton_cb, ...
'tooltip', ...
['Adjust axes limits to show data in all history frames ', ...
' (disregarding zero data)']);
hFitAxesToTakeDataButton = ...
uicontrol(hFig, 'style', 'pushbutton', 'units', 'pixels', ...
'position', uiPos(1,13,hBorder,vBorder), ...
'string', 'Fit axes to all data in take', 'UserData', struct('clicked', 0), ...
'callback', @fitAxesToTakeDataButton_cb, ...
'tooltip', ...
['Adjust axes limits to include all data points in the take ', ...
' (disregarding zero data)']);
hExcludeOutliersCheckbox = ...
uicontrol(hFig, 'style', 'checkbox', 'units', 'pixels', ...
'position', uiPos(1,14,hBorder,vBorder), 'String', 'Omit outliers for fit', ...
'callback', @fitAxesToHistCheckbox_cb, 'tooltip', ...
['When fitting axes limits to take or history data, use only absolute values ', ...
'below 99th percentile.']);
hTakeMenu = ...
uicontrol(hFig, 'style', 'popupmenu', 'units', 'pixels', ...
'position', uiPos(1,16,hBorder,vBorder)./[1 1 3 1], ...
'string', ' ', 'tag', 'takeMenu');
hTakeText = ...
uicontrol(hFig, 'style', 'text', 'units', 'pixels', ...
'position', uiPos(2,16,hBorder,vBorder)./[3 1 1.5 1]+[sep*3 0 0 0], ...
'string', 'Take', 'horizontalAlignment', 'left');
hLoadButton = ...
uicontrol(hFig, 'style', 'pushbutton', 'units', 'pixels', ...
'position', uiPos(1,17,hBorder,vBorder), ...
'string', 'Load data file', 'callback', @load_cb, 'tooltip', ...
['Load *.vzp or *.mat file (the latter must contain data ', ...
'in the format returned by loadVzpFile()).'], 'tag', ...
'loadButton');
hLoadButton.UserData.data = [];
hLoadButton.UserData.newFileLoaded = 0;
hSaveStreamingButton = ...
uicontrol(hFig, 'style', 'pushbutton', 'units', 'pixels', ...
'position', uiPos(1,18,hBorder,vBorder), ...
'string', 'Save to *.mat', 'callback', @streamingSave_cb, ...
'UserData', struct('wasClicked', 0), 'tag', ...
'hSaveStreamingButton', 'Enable', 'off', 'tooltip', ...
'Save data to *.mat file.');
hStreamingDiscardButton = ...
uicontrol(hFig, 'style', 'pushbutton', 'units', 'pixels', ...
'position', uiPos(1,20,hBorder,vBorder), ...
'string', 'Restart recording', 'callback', @streamingDiscard_cb, ...
'UserData', struct('wasClicked', 0), 'tag', ...
'StreamingDiscardButton', 'Enable', 'off' , 'tooltip', ...
'Discard data streamed so far and start new data recording.');
hStreamingUpdateButton = ...
uicontrol(hFig, 'style', 'pushbutton', 'units', 'pixels', ...
'position', uiPos(1,21,hBorder,vBorder), ...
'string', 'Update streamed data', 'callback', @streamingUpdate_cb, ...
'UserData', struct('wasClicked', 0), 'tag', 'StreamingUpdateButton', ...
'Enable', 'off', 'tooltip', ['Do not resume the live plotting, but ', ...
'add data captured in the meantime to the plot.']);
hStreamingPauseButton = ...
uicontrol(hFig, 'style', 'pushbutton', 'units', 'pixels', ...
'position', uiPos(1,22,hBorder,vBorder), ...
'string', 'Pause live plot', 'callback', @streamingPause_cb, 'UserData', ...
struct('active',0,'justEnabled',0,'justDisabled',0), 'tag', ...
'StreamingPauseButton', 'Enable', 'off', 'tooltip', ...
['Stop the live plot, enabling to inspect data captured thus far. ', ...
'Data recording resumes in background but won''t update plots.' ]);
hStreamCheckbox = ...
uicontrol(hFig, 'style', 'checkbox', 'units', 'pixels', ...
'position', uiPos(1,23,hBorder,vBorder), 'String', 'Stream from trackers', ...
'UserData', struct('wasChecked',0,'wasUnchecked',0), ...
'callback', @streamCheckbox_cb);
hQuitButton = ...
uicontrol(hFig, 'style', 'pushbutton', 'units', 'pixels', ...
'position', uiPos(1,25,hBorder,vBorder), ...
'string', 'Quit', 'callback', @quit_cb);
hQuitButton.UserData.wantQuit = 0;
% Set up 3d axes
axWidth = 930;
axHeight = 611;
hAx = axes('view', [-45 30], 'ButtonDownFcn', @hAx_ButtonDownFcn, ...
'UserData', struct('buttonDown',0),'tag','3dAxes', 'units', 'pixels', ...
'position', [width/4, (height*3+vSliderBorder)*1.5, axWidth, axHeight]);
axis equal
axis vis3d
xlabel('x')
ylabel('y')
zlabel('z')
hold on
% Warning text for static data
axc = hAx.Position(1:2)+hAx.Position(3:4)./2;
txtWidth = 300;
txtHeight = 30;
hIdenticalDataText = ...
uicontrol('style', 'text', 'string', ['Warning: Streamed data ', ...
'unchanging. Unchanged frames won''t be recorded. Capture process ', ...
'not running in VZSoft?'],...
'position', ...
[axc(1)-txtWidth/2, axc(2)-txtHeight/2, txtWidth, txtHeight], ...
'foregroundcolor', 'r', 'visible', 'off', ...
'tag', 'identicalDataText', 'backgroundColor', 'k');
% UI data table
tXpos = hAx.Position(1)+hAx.Position(3)-150;
tYpos = vBorder;
tWidth = hFig.Position(3) - hBorder - tXpos;
tHeight = hFig.Position(4) - tYpos - 40;
hDataTable = uitable(hFig,'Data',nan(7,7), 'units', 'pixels', ...
'Position',[tXpos tYpos tWidth tHeight], 'ColumnName', ...
{'TCM','LED','x','y','z','t','good'}, ...
'ColumnWidth', num2cell(repmat(floor((tWidth-30)/7),1,7)));
% Set units to normalize for scaling
hDataTable.Units = 'normalized';
hAx.Units = 'normalized';
hFig.Visible = 'on';
hIdenticalDataText.Units = 'Normalized';
try
% Note: There are two main loops here, the outer one just below, which
% determines what data are used, and an inner one, which iterates over the
% frames of the current data.
%
% The code in the outer loop is (re-)executed:
%
% - on startup and as long as no data has been loaded or recorded
% - when a new file was loaded
% - when the user selected a new take in the take dropdown menu
% - when the streaming checkbox is checked or unchecked
% - when the "restart recording" button was clicked (discard data so far)
speedSelected = hSpeedMenu.Value;
speed = speeds(speedSelected);
dataIsStreamedData = 0;
while 1
% request to restart online streaming
if hStreamingDiscardButton.UserData.wasClicked
hStreamingDiscardButton.UserData.wasClicked = 0;
hStreamCheckbox.Value = 1;
streamCheckbox_cb(hStreamCheckbox,[]);
end
% when streaming first enabled or restart of streaming requested, get
% some sample data and construct data struct from that
if hStreamCheckbox.UserData.wasChecked
% if streaming pause active -> disable
if hStreamingPauseButton.UserData.active == 1
streamingPause_cb(hStreamingPauseButton);
streamingPauseWasActive = 1;
else
streamingPauseWasActive = 0;
end
% reset click state of and checkbox
hStreamCheckbox.UserData.wasChecked = 0;
% Get some sample data (one sec, but at least five frames)
f = 0; sampleTime = 1; sampleStart = tic;
tmpDat = [];
while toc(sampleStart) < sampleTime || size(tmpDat,3) < 5
f = f + 1;
[tmpDat(:,:,f), waitSecsForBufferUpdateExc] = getDat(3);
% if data is static (VzSoft not capturing?), cancel streaming
if waitSecsForBufferUpdateExc
break;
end
end
if waitSecsForBufferUpdateExc % streaming failed
waitfor(...
msgbox(['Data obtained from trackers is not changing. Maybe ', ...
'data capture is inactive in VzSoft? I stopped ', ...
'streaming for now.'],'Static data'));
% if streaming pause was active before -> re-enable
% else (=streaming was not active at all) -> disable streaming
if streamingPauseWasActive
streamingPause_cb(hStreamingPauseButton);
else
hStreamCheckbox.Value = 0;
streamCheckbox_cb(hStreamCheckbox,[]);
end
else
dataIsStreamedData = 1;
% reset variable to which data will be recorded
data = struct();
% compute needed values from sample data
data(1).nFrames = f;
data(1).frameRate = f/sampleTime;
data(1).nMarkers = size(tmpDat,1);
data(1).markerNames = cellstr(num2str(tmpDat(:,1:2,1)));
data(1).markerIDs = tmpDat(:,1:2,1);
data(1).crf = nan;
% Set some UI variables
take = 1;
speed = 1;
hSpeedMenu.Value = find((speeds == 1));
% First few data points
data(1).frames(:,:,1:5) = tmpDat(:,:,1:5);
end
end
% Load file
if hLoadButton.UserData.newFileLoaded
hLoadButton.UserData.newFileLoaded = 0;
data = hLoadButton.UserData.data;
if isfield(data,'data') % for *.mat (already contain struct 'data')
data = data.data;
end
take = 1;
hTakeMenu.Value = take;
hFrameSlider.Max = data(1).nFrames;
hFrameSlider.Value = 1;
hFrameSlider.SliderStep = [1/data(1).nFrames 1/data(1).nFrames];
hTakeMenu.String = num2cell(1:size(data,2));
dataIsStreamedData = 0;
end
% Calculations based on data only when data has been loaded or recorded
if exist('data', 'var')
% Store take and speed to be able to detect changes
previousTake = take;
previousSpeedSelected = speedSelected;
% Put data from struct into variables used in the following
frames = data(take).frames;
nFrames = data(take).nFrames;
frameRate = data(take).frameRate;
nMarkers = data(take).nMarkers;
lastGoodPos = nan(nMarkers, 3);
% Get time stamps relative to first frame
T = relativeTimeStamps(frames, frameRate);
takeDuration = max(T);
% Set frame slider properties according to data
hFrameSlider.Max = nFrames;
hFrameSlider.SliderStep = [1/nFrames, 1/nFrames];
% Set axes limit based on take data extent (happens in frame loop)
hFitAxesToTakeDataButton.UserData.clicked = 1;
end
% % Stream data save button only operable when streamed data present
% if dataIsStreamedData
% hSaveStreamingButton.Enable = 'on';
% else
% hSaveStreamingButton.Enable = 'off';
% end
% Make sure pausea and save buttons not operable when no data present,
% otherwise enable and set correct string for pause.
if ~exist('data', 'var') || isempty(data)
hPauseButton.Enable = 'off';
hSaveStreamingButton.Enable = 'off';
else
hSaveStreamingButton.Enable = 'on';
if hPauseButton.UserData == 1
hPauseButton.String = 'Play';
elseif hPauseButton.UserData == 0
hPauseButton.String = 'Pause';
end
end
% Iterate over frames
tElapsed = 0;
curFrame = 0;
initializeDots = 1;
initializeLines = 1;
previousFrame = 0;
hFrameSlider.UserData.setElapsedTime = 0;
forceReplot = 0;
rotationTic = tic;
nWaitTimeExceeded = 0;
tic
while 1
% Initialize plots if streaming was enabled or restart
% of streaming take requested
if hStreamCheckbox.UserData.wasChecked || ...
hStreamingDiscardButton.UserData.wasClicked
delete(hAx.Children)
break;
end
% in case streaming is active
if hStreamCheckbox.Value
% Get frame from trackers (record even when paused) BUT
% if no new data within wait time, a frame will be obtained
% anyway, but it will not differ from before (even the time
% stamps may be identical). If so, replace timestamps
% artificially and warn. This is just so the script can't get
% stuck due to bufferUpdateCheck within getDat().
[obtainedFrame, waitSecsForBufferUpdateExc] = getDat(waitSecsForBufferUpdate);
if ~waitSecsForBufferUpdateExc
data(1).frames(:,:,end+1) = obtainedFrame;
hIdenticalDataText.Visible = 'off';
nWaitTimeExceeded = 0;
else
% show warning only after some successive failures to
% prevent it from popping up intermittently when buffer
% update takes too long for other reasons.
nWaitTimeExceeded = nWaitTimeExceeded + 1;
if nWaitTimeExceeded > 3
hIdenticalDataText.Visible = 'on';
end
end
data(1).nFrames = size(data(1).frames,3);
% If live plot is not paused or update button was pressed.
% Update plotting variables / UI with new data, update time to
% last streamed frame
if ~hStreamingPauseButton.UserData.active || ...
hStreamingUpdateButton.UserData.wasClicked
hStreamingUpdateButton.UserData.wasClicked = 0;
nFrames = data(1).nFrames;
frames = data(1).frames;
T = relativeTimeStamps(frames, frameRate);
takeDuration = max(T);
frameRate = nFrames/takeDuration;
hFrameSlider.Max = nFrames;
hFrameSlider.SliderStep = [1/nFrames, 1/nFrames];
tElapsed = T(end);
end
% When streaming pause just clicked: Enabled-> go to start of
% recorded frames and set speed to 1. When Disabled-> reset
% speed.
if hStreamingPauseButton.UserData.justEnabled
hStreamingPauseButton.UserData.justEnabled = 0;
tElapsed = 0;
oldSpeed = speed;
oldSpeedMenuValue = hSpeedMenu.Value;
speed = 1;
hSpeedMenu.Value = find((speeds == 1));
elseif hStreamingPauseButton.UserData.justDisabled
hStreamingPauseButton.UserData.justDisabled = 0;
speed = oldSpeed;
hSpeedMenu.Value = oldSpeedMenuValue;
end
end
% Saving streamed data
if hSaveStreamingButton.UserData.wasClicked
hSaveStreamingButton.UserData.wasClicked = 0;
% save only streamed data updated in UI (not that recorded in
% background)
saveStreamedData(data, frames, nFrames, frameRate)
end
% For quitters
if hQuitButton.UserData.wantQuit
if exist('data', 'var') && ~isempty(data)
resp = ...
questdlg(['Save data to mat-file before quitting?'], ...
'Save data', 'Yes', 'No', 'Yes');
if strcmp(resp, 'Yes')
saveStreamedData(data, frames, nFrames, frameRate)
end
end
close(hFig);
return
end
reload = 0; % flag used in frame loop to get back here
% skip bulk of this loop if no data loaded/recorded yet
if exist('data', 'var')
% If pause not active, increment elapsed time since last plot
if ~hPauseButton.UserData
tElapsed = tElapsed + toc * speed;
end
tic
% if user moved slider, update tElapsed accordingly; else adjust
% slider to current elapsed time
if hFrameSlider.UserData.setElapsedTime
tElapsed = T(round(hFrameSlider.Value));
hFrameSlider.UserData.setElapsedTime = 0;
else
hFrameSlider.UserData.settingFromCode = 1;
hFrameSlider.Value = max(1,curFrame);
hFrameSlider.UserData.settingFromCode = 0;
end
% change frame number based on elapsed time
[~, newFrame] = min(abs(T- tElapsed));
% curFrame becomes newFrame BUT
% during streaming, new frame should not exceed available
% frames and tElapsed should not exceed total recording time;
% except when pause activated.
if hStreamCheckbox.Value && ~hPauseButton.UserData
if tElapsed > T(end) && ...
~hStreamingPauseButton.UserData.active
tElapsed = T(end);
end
if ~(newFrame > nFrames)
curFrame = newFrame;
end
else
curFrame = newFrame;
end
% Compute running fps
fpsWindow = 1;
[~,indStart] = min(abs(T-(T(curFrame)-fpsWindow)));
runningFps = (curFrame-indStart)/(T(curFrame)-T(indStart));
% Update frame number etc.
hFrameText.String = ['Frame ', num2str(curFrame), ' of ', ...
num2str(nFrames), ' | ', num2str(tElapsed), ' of ',...
num2str(takeDuration) ' s', ...
' | ', num2str(frameRate), ' fps total', ...
' | ', num2str(runningFps), ' fps last sec.'];
% roll around when take duration exceeded
if tElapsed > T(end)
tElapsed = 0;
curFrame = 1;
tic
end
% if new take selected delete plots and leave loop
if hTakeMenu.Value ~= previousTake
take = hTakeMenu.Value;
reload = 1;
end
% if new speed selected, change speed
if hSpeedMenu.Value ~= previousSpeedSelected
speed = speeds(hSpeedMenu.Value);
end
% set history length
histLen_old = histLen;
histLen = round(str2num(hHistLenEdit.String));
if isempty(histLen)
histLen = histLen_old;
end
if histLen ~= histLen_old
forceReplot = 1;
end
hHistLenEdit.String = num2str(histLen);
% zoom to data if button clicked (onto mean of data with axes
% extension based on data span.
if hFitAxesToMarkersCheckbox.Value || ...
hFitAxesToMarkersButton.UserData.clicked
hFitAxesToMarkersButton.UserData.clicked = 0;
posTmp = frames(:,[3,4,5],curFrame);
posTmp = posTmp(~all(posTmp==0,2),:);
if ~isempty(posTmp)
center = mean(posTmp,1);
maxDist = ...
max(1, max(max(abs(posTmp - repmat(center,size(posTmp,1),1)))));
axLims = [center-(maxDist+maxDist*0.1); ...
center+(maxDist+maxDist*0.1)];
set(hAx, 'XLim', axLims(:,1), 'YLim', axLims(:,2), 'ZLim', axLims(:,3));
end
end
% zoom to history or all data (this is done in the frame loop
% as the history frames are needed which are computed there)
if hFitAxesToHistButton.UserData.clicked || ...
hFitAxesToTakeDataButton.UserData.clicked
forceReplot = 1;
end
% Axes rotation
if hRotateCheckbox.Value && ~hAx.UserData.buttonDown
if toc(rotationTic) > secsPerDegreeRotation
oldView = hAx.View;
hAx.View = oldView + [1 0];
rotationTic = tic;
end
end
if curFrame ~= previousFrame || forceReplot
forceReplot = 0;
% Get current frame data to plot dots (and determine which
% rows are marked bad data in column seven and which
% contain only zero position data)
frame = frames(:,:,curFrame);
if size(frame,2) == 7
goodDataCol = frame(:,7);
else % streamed data has no column 7
goodDataCol = ones(size(frame,1),1);
end
rowsMarkedGood = find(goodDataCol)';
[~,zeroRows] = zeroDataCheck(frame);
rowsNotZero = setdiff(1:nMarkers, zeroRows);
% store last good position (not zero, not bad data)
rowsUsable = unique([rowsMarkedGood, rowsNotZero]);
lastGoodPos(rowsUsable, :) = frame(rowsUsable,3:5);
% use current data for markers where data is marked good
% and not zero data, otherwise use last good frame position
rowsUnusable = setdiff(1:nMarkers, rowsUsable);
tmpFrame = frame;
tmpFrame(rowsUnusable, 3:5) = lastGoodPos(rowsUnusable,:);
x = tmpFrame(:,3)';
y = tmpFrame(:,4)';
z = tmpFrame(:,5)';
t = tmpFrame(:,6)';
% for later use during plotting
rowsMarkedGood = toBoolMask(rowsMarkedGood, nMarkers);
rowsNotZero = toBoolMask(rowsNotZero, nMarkers);
rowsUsable = toBoolMask(rowsUsable, nMarkers);
% Update table
tblData = [num2cell(frame(:,1:2)), ...
arrayfun(@(x) sprintf('%.4f\n',x), frame(:,3:6),'un',0)];
% last (good data column depends on whether loaded or streamed)
if size(frame,2) == 7
tblData = [tblData, num2cell(frame(:,7))];
else
tblData = [tblData, num2cell(nan(size(frame,2),1))];
end
hDataTable.Data = tblData;
% Get history over last steps, for lines; replace zero
% position values with nan so that these are not drawn in
% history lines
histFrames = frames(:,:,max(1,curFrame-histLen):curFrame);
xHist = squeeze(histFrames(:,3,:))';
yHist = squeeze(histFrames(:,4,:))';
zHist = squeeze(histFrames(:,5,:))';
xHist(xHist == 0) = nan;
yHist(yHist == 0) = nan;
zHist(zHist == 0) = nan;
% Initialize dots (current marker positions)
if initializeDots == 1
initializeDots = 0;
hDots = plot3(hAx, [x;x], [y;y], [z;z], 'o', ...
'markersize', 8, 'linewidth', markerBaseLineWidth);
set(hDots, 'markerEdgeColor', 'k');
for j = 1:numel(hDots)
% adjust marker color
dt = hDots(j);
col = dt.Color+0.25;
col(col>1) = 1;
dt.MarkerFaceColor = col;
% add marker labels
hAx;
hLabels(j) = text(...
dt.XData(1), dt.YData(1), dt.ZData(1), ...
[' ', num2str(frame(j,1)), '|', num2str(frame(j,2))]);
end
try
set(hLabels, 'color', [.3 .3 .3], 'fontsize', 10);
end
if exist('colors', 'var')
for i = 1:numel(hDots)
hDots(i).MarkerFaceColor = colors{i};
end
end
grid(hAx, 'on');
end
% Initialize lines (marker histories)
if initializeLines && curFrame > 1
hLines = plot3(hAx, xHist, yHist, zHist);
for c = 1:numel(hLines)
hLines(c).Color = hDots(c).Color;
end
initializeLines = 0;
end
% Update plots
hLabels = hLabels(isgraphics(hLabels));
if ~hLabelCheckbox.Value
set(hLabels,'Visible','off');
else
set(hLabels,'Visible','on');
end
set(hDots, 'MarkerEdgeColor', 'k', ... % (Reset dot colors)
'linewidth', markerBaseLineWidth);
if exist('hDotsCross', 'var') % remove bad data indicators
delete(hDotsCross)
end
hDotsCross = gobjects();
% Go through dots and lines, update their data, also add
% data quality indicators for each marker
for numLine = 1:numel(hDots)
% Update lines
if ~initializeLines
hLines(numLine).XData = xHist(:,numLine)';
hLines(numLine).YData = yHist(:,numLine)';
hLines(numLine).ZData = zHist(:,numLine)';
end
% Update dots
hDots(numLine).XData = x([numLine, numLine]);
hDots(numLine).YData = y([numLine, numLine]);
hDots(numLine).ZData = z([numLine, numLine]);
% Add indicators for bad data
if ~rowsUsable(numLine)
% zero data -> red outline
if ~rowsNotZero(numLine)
hDots(numLine).MarkerEdgeColor = 'r';
hDots(numLine).LineWidth = markerBaseLineWidth + 1;
end
% marked bad -> additional red cross
if ~rowsMarkedGood(numLine)
hDotsCross(end+1) = copyobj(hDots(numLine),hAx) ;
hDotsCross(end).Marker = 'x';
hDotsCross(end).MarkerEdgeColor = 'r';
hDotsCross(end).LineWidth = markerBaseLineWidth;
%uistack(hDotsCross(numLine),'top')
end
end
% Update labels
hLabels(numLine).Position = ...
[x(numLine) y(numLine) z(numLine)];
end
% fit axes limits to extent of history data if desired
if hFitAxesToHistCheckbox.Value || ...
hFitAxesToHistButton.UserData.clicked || ...
hFitAxesToTakeDataButton.UserData.clicked
% if fit to all data requested, use all data, not hist
% (last frame excluded as it sometimes holds erroneous
% data)
if hFitAxesToTakeDataButton.UserData.clicked
hFitAxesToTakeDataButton.UserData.clicked = 0;
xHist = squeeze(frames(:,3,1:end-1))';
yHist = squeeze(frames(:,4,1:end-1))';
zHist = squeeze(frames(:,5,1:end-1))';
end
hFitAxesToHistButton.UserData.clicked = 0;
% Get history and exclude zeros
hist = [xHist(:), yHist(:), zHist(:)];
hist(hist==0) = nan;
% If desired, use only values below 99th percentile
if hExcludeOutliersCheckbox.Value
hist(abs(hist)>=repmat(prctile(hist,99), size(hist,1), 1)) = nan;
end
% compute and apply axes limits
if size(hist,1) > 1 && all(sum(~isnan(hist))>1)
extrema = [min(hist); max(hist)];
span = diff(extrema);
center = extrema(1,:) + span/2;
maxSpan = max(span);
oneSide = (maxSpan/2 + maxSpan*0.025);
axLims = [center - oneSide; center + oneSide];
set(hAx, 'XLim', axLims(:,1), 'YLim', axLims(:,2), 'ZLim', axLims(:,3));
end
end
previousFrame = curFrame;
end
end
drawnow
% For changing take or loading new file
if reload || hLoadButton.UserData.newFileLoaded
delete(hAx.Children)
break
end
end
end
% in case an error occurs and streaming mode is running, allow user to save
% data before closing.
catch
if dataIsStreamedData
resp = ...
questdlg(['Something went wrong or you closed the window instead ', ...
'of hitting quit. Would you like to save ', ...
'your streamed data before the application closes?'], ...
'Error', 'Yes', 'No', 'Yes');
if strcmp(resp, 'Yes')
saveStreamedData(data, frames, nFrames, frameRate)
end
end
try
error('An error occurred. Sorry.')
close(hFig);
end
end
end
%%%% Callbacks
function quit_cb(h,~)
h.UserData.wantQuit = 1;
end
function load_cb(h,~)
loadError = 0;
[f,p] = uigetfile({'*.vzp';'*.mat'});
filename = [p,f];
fl = length(filename);
if ~ischar(filename)
loadError = 1;
elseif strcmp(filename(fl-3:end),'.mat')
data = load(filename);
if ~(isfield(data,'data') && isstruct(data.data))
loadError = 1;
msgbox(['Loaded mat-file must contain data in a struct ''data'', ', ...
'as output by function loadVzpFile.'],'Invalid data format')
else
disp('Loading data...')
end
elseif strcmp(filename(fl-3:end),'.vzp')
try
data = loadVzpFile(filename, false);
disp('Loading data...')
catch
loadError = 1;
msgbox(sprintf(['Error loading vzp-file.\n\nMake sure the following functions ', ...
'are on your MATLAB path (letter cases intentional): \n\n', ...
'VzpGetTakeCount\n', ...
'vzpGetTakeMarkers\n', ...
'vzpGetTakeFrames\n', ...
'vzpGetMarkerName\n', ...
'vzpgetmarkerID\n', ...
'vzpGetFrameData\n', ...
'vzpGetTakeFrameRate\n', ...
'vzpGetTakeCRF']))
end
end
if ~loadError
h.UserData.data = data;
h.UserData.newFileLoaded = 1;
set(findobj(h.Parent, 'tag', 'pauseButton'), 'enable', 'on');
disp('Done')
else
h.UserData.newFileLoaded = 0;
disp('File could not be loaded.')
end
end
function fig_buttonUp_cb(h,~)
% Use figure button up to reset button down state of 3d axis.
hAx = findobj(h, 'tag', '3dAxes');
if hAx.UserData.buttonDown == 1
hAx.UserData.buttonDown = 0;
end
end
function hAx_ButtonDownFcn(h,~)
h.UserData.buttonDown = 1;
end