SixDofAnimation.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. % Note that this animation code was stolen from:
  2. % https://github.com/xioTechnologies/Gait-Tracking-With-x-IMU
  3. function fig = SixDOFanimation(varargin)
  4. %% Create local variables
  5. % Required arguments
  6. p = varargin{1}; % position of body
  7. R = varargin{2}; % rotation matrix of body
  8. [numSamples dummy] = size(p);
  9. % Default values of optional arguments
  10. SamplePlotFreq = 1;
  11. Trail = 'Off';
  12. LimitRatio = 1;
  13. Position = [];
  14. FullScreen = false;
  15. View = [30 20];
  16. AxisLength = 1;
  17. ShowArrowHead = 'on';
  18. Xlabel = 'X';
  19. Ylabel = 'Y';
  20. Zlabel = 'Z';
  21. Title = '6DOF Animation';
  22. ShowLegend = true;
  23. write_video = false;
  24. for i = 3:2:nargin
  25. if strcmp(varargin{i}, 'SamplePlotFreq'), SamplePlotFreq = varargin{i+1};
  26. elseif strcmp(varargin{i}, 'Trail')
  27. Trail = varargin{i+1};
  28. if(~strcmp(Trail, 'Off') && ~strcmp(Trail, 'DotsOnly') && ~strcmp(Trail, 'All'))
  29. error('Invalid argument. Trail must be ''Off'', ''DotsOnly'' or ''All''.');
  30. end
  31. elseif strcmp(varargin{i}, 'LimitRatio'), LimitRatio = varargin{i+1};
  32. elseif strcmp(varargin{i}, 'Position'), Position = varargin{i+1};
  33. elseif strcmp(varargin{i}, 'FullScreen'), FullScreen = varargin{i+1};
  34. elseif strcmp(varargin{i}, 'View'), View = varargin{i+1};
  35. elseif strcmp(varargin{i}, 'AxisLength'), AxisLength = varargin{i+1};
  36. elseif strcmp(varargin{i}, 'ShowArrowHead'), ShowArrowHead = varargin{i+1};
  37. elseif strcmp(varargin{i}, 'Xlabel'), Xlabel = varargin{i+1};
  38. elseif strcmp(varargin{i}, 'Ylabel'), Ylabel = varargin{i+1};
  39. elseif strcmp(varargin{i}, 'Zlabel'), Zlabel = varargin{i+1};
  40. elseif strcmp(varargin{i}, 'Title'), Title = varargin{i+1};
  41. elseif strcmp(varargin{i}, 'ShowLegend'), ShowLegend = varargin{i+1};
  42. elseif strcmp(varargin{i}, 'CreateVideo'), write_video = varargin{i+1};
  43. else error('Invalid argument.');
  44. end
  45. end;
  46. %% Reduce data to samples to plot only
  47. p = p(1:SamplePlotFreq:numSamples, :);
  48. R = R(:, :, 1:SamplePlotFreq:numSamples) * AxisLength;
  49. if(numel(View) > 2)
  50. View = View(1:SamplePlotFreq:numSamples, :);
  51. end
  52. [numPlotSamples dummy] = size(p);
  53. %% Setup AVI file
  54. % % % write video
  55. if write_video
  56. writeObj = VideoWriter('results');
  57. writeObj.FrameRate = 60;
  58. open(writeObj);
  59. end
  60. %% Setup figure and plot
  61. % Create figure
  62. fig = figure();
  63. if(FullScreen)
  64. screenSize = get(0, 'ScreenSize');
  65. set(fig, 'Position', [0 0 screenSize(3) screenSize(4)]);
  66. elseif(~isempty(Position))
  67. set(fig, 'Position', Position);
  68. end
  69. set(gca, 'drawmode', 'fast');
  70. lighting phong;
  71. set(gcf, 'Renderer', 'zbuffer');
  72. hold on;
  73. axis equal;
  74. grid on;
  75. view(View(1, 1), View(1, 2));
  76. title(i);
  77. xlabel(Xlabel);
  78. ylabel(Ylabel);
  79. zlabel(Zlabel);
  80. % Create plot data arrays
  81. if(strcmp(Trail, 'DotsOnly') || strcmp(Trail, 'All'))
  82. x = zeros(numPlotSamples, 1);
  83. y = zeros(numPlotSamples, 1);
  84. z = zeros(numPlotSamples, 1);
  85. end
  86. if(strcmp(Trail, 'All'))
  87. ox = zeros(numPlotSamples, 1);
  88. oy = zeros(numPlotSamples, 1);
  89. oz = zeros(numPlotSamples, 1);
  90. ux = zeros(numPlotSamples, 1);
  91. vx = zeros(numPlotSamples, 1);
  92. wx = zeros(numPlotSamples, 1);
  93. uy = zeros(numPlotSamples, 1);
  94. vy = zeros(numPlotSamples, 1);
  95. wy = zeros(numPlotSamples, 1);
  96. uz = zeros(numPlotSamples, 1);
  97. vz = zeros(numPlotSamples, 1);
  98. wz = zeros(numPlotSamples, 1);
  99. end
  100. x(1) = p(1,1);
  101. y(1) = p(1,2);
  102. z(1) = p(1,3);
  103. ox(1) = x(1);
  104. oy(1) = y(1);
  105. oz(1) = z(1);
  106. ux(1) = R(1,1,1:1);
  107. vx(1) = R(2,1,1:1);
  108. wx(1) = R(3,1,1:1);
  109. uy(1) = R(1,2,1:1);
  110. vy(1) = R(2,2,1:1);
  111. wy(1) = R(3,2,1:1);
  112. uz(1) = R(1,3,1:1);
  113. vz(1) = R(2,3,1:1);
  114. wz(1) = R(3,3,1:1);
  115. % Create graphics handles
  116. orgHandle = plot3(x, y, z, 'k.');
  117. if(ShowArrowHead)
  118. ShowArrowHeadStr = 'on';
  119. else
  120. ShowArrowHeadStr = 'off';
  121. end
  122. quivXhandle = quiver3(ox, oy, oz, ux, vx, wx, 'r', 'ShowArrowHead', ShowArrowHeadStr, 'MaxHeadSize', 0.999999, 'AutoScale', 'off');
  123. quivYhandle = quiver3(ox, oy, oz, uy, vy, wy, 'g', 'ShowArrowHead', ShowArrowHeadStr, 'MaxHeadSize', 0.999999, 'AutoScale', 'off');
  124. quivZhandle = quiver3(ox, ox, oz, uz, vz, wz, 'b', 'ShowArrowHead', ShowArrowHeadStr, 'MaxHeadSize', 0.999999, 'AutoScale', 'off');
  125. % Create legend
  126. if(ShowLegend)
  127. legend('Origin', 'X', 'Y', 'Z');
  128. end
  129. % Set initial limits
  130. Xlim = [x(1)-AxisLength x(1)+AxisLength] * LimitRatio;
  131. Ylim = [y(1)-AxisLength y(1)+AxisLength] * LimitRatio;
  132. Zlim = [z(1)-AxisLength z(1)+AxisLength] * LimitRatio;
  133. set(gca, 'Xlim', Xlim, 'Ylim', Ylim, 'Zlim', Zlim);
  134. % Set initial view
  135. view(View(1, :));
  136. %% Plot one sample at a time
  137. for i = 1:numPlotSamples
  138. % Update graph title
  139. if(strcmp(Title, ''))
  140. titleText = sprintf('Sample %i of %i', 1+((i-1)*SamplePlotFreq), numSamples);
  141. else
  142. titleText = strcat(Title, ' (', sprintf('Sample %i of %i', 1+((i-1)*SamplePlotFreq), numSamples), ')');
  143. end
  144. title(titleText);
  145. % Plot body x y z axes
  146. if(strcmp(Trail, 'DotsOnly') || strcmp(Trail, 'All'))
  147. x(1:i) = p(1:i,1);
  148. y(1:i) = p(1:i,2);
  149. z(1:i) = p(1:i,3);
  150. else
  151. x = p(i,1);
  152. y = p(i,2);
  153. z = p(i,3);
  154. end
  155. if(strcmp(Trail, 'All'))
  156. ox(1:i) = p(1:i,1);
  157. oy(1:i) = p(1:i,2);
  158. oz(1:i) = p(1:i,3);
  159. ux(1:i) = R(1,1,1:i);
  160. vx(1:i) = R(2,1,1:i);
  161. wx(1:i) = R(3,1,1:i);
  162. uy(1:i) = R(1,2,1:i);
  163. vy(1:i) = R(2,2,1:i);
  164. wy(1:i) = R(3,2,1:i);
  165. uz(1:i) = R(1,3,1:i);
  166. vz(1:i) = R(2,3,1:i);
  167. wz(1:i) = R(3,3,1:i);
  168. else
  169. ox = p(i,1);
  170. oy = p(i,2);
  171. oz = p(i,3);
  172. ux = R(1,1,i);
  173. vx = R(2,1,i);
  174. wx = R(3,1,i);
  175. uy = R(1,2,i);
  176. vy = R(2,2,i);
  177. wy = R(3,2,i);
  178. uz = R(1,3,i);
  179. vz = R(2,3,i);
  180. wz = R(3,3,i);
  181. end
  182. set(orgHandle, 'xdata', x, 'ydata', y, 'zdata', z);
  183. set(quivXhandle, 'xdata', ox, 'ydata', oy, 'zdata', oz,'udata', ux, 'vdata', vx, 'wdata', wx);
  184. set(quivYhandle, 'xdata', ox, 'ydata', oy, 'zdata', oz,'udata', uy, 'vdata', vy, 'wdata', wy);
  185. set(quivZhandle, 'xdata', ox, 'ydata', oy, 'zdata', oz,'udata', uz, 'vdata', vz, 'wdata', wz);
  186. % Adjust axes for snug fit and draw
  187. axisLimChanged = false;
  188. if((p(i,1) - AxisLength) < Xlim(1)), Xlim(1) = p(i,1) - LimitRatio*AxisLength; axisLimChanged = true; end
  189. if((p(i,2) - AxisLength) < Ylim(1)), Ylim(1) = p(i,2) - LimitRatio*AxisLength; axisLimChanged = true; end
  190. if((p(i,3) - AxisLength) < Zlim(1)), Zlim(1) = p(i,3) - LimitRatio*AxisLength; axisLimChanged = true; end
  191. if((p(i,1) + AxisLength) > Xlim(2)), Xlim(2) = p(i,1) + LimitRatio*AxisLength; axisLimChanged = true; end
  192. if((p(i,2) + AxisLength) > Ylim(2)), Ylim(2) = p(i,2) + LimitRatio*AxisLength; axisLimChanged = true; end
  193. if((p(i,3) + AxisLength) > Zlim(2)), Zlim(2) = p(i,3) + LimitRatio*AxisLength; axisLimChanged = true; end
  194. if(axisLimChanged), set(gca, 'Xlim', Xlim, 'Ylim', Ylim, 'Zlim', Zlim); end
  195. drawnow;
  196. % Adjust view
  197. if(numel(View) > 2)
  198. view(View(i, :));
  199. end
  200. % Add frame to AVI object
  201. if write_video
  202. frame = getframe(fig);
  203. writeVideo(writeObj, frame);
  204. end
  205. end
  206. hold off;
  207. % Close AVI file
  208. if write_video
  209. close(writeObj);
  210. end
  211. end