% Daniel Kawano, Rose-Hulman Institute of Technology % Last modified: Dec 29, 2017 function animate_iphone(x1, x2, x3, psi, theta, phi, dt, r1, r2, r3, pov) % Convert the displacements to cm: x1 = x1*100; % cm x2 = x2*100; % cm x3 = x3*100; % cm % Load images for the front and back of an iPhone: load iphone_images.mat % Supply approximate length, width, and thickness measurements: L = 5*(2.54); % cm W = 2.5*(2.54); % cm T = 0.25*(2.54); % cm % Set up the figure window: figure set(gcf, 'color', 'w', 'name', 'iPhone toss animation') plot3(0, 0, 0) xlabel('\itx\rm_1 (cm)') ylabel('\itx\rm_2 (cm)') zlabel('\itx\rm_3 (cm) ', 'rotation', 0) axis equal xlim([min(x1)-1.2*(L/2), max(x1)+1.2*(L/2)]) ylim([min(x2)-1.2*(L/2), max(x2)+1.2*(L/2)]) zlim([min(x3)-1.2*(L/2), max(x3)+1.2*(L/2)]) grid on % Set the view based on the user input: if strcmp(pov, 'default') == 1 view(35, 20) elseif strcmp(pov, 'top') == 1 view(-180, 0) elseif strcmp(pov, 'bottom') == 1 view(0, 0) elseif strcmp(pov, 'left') == 1 view(-90, 0) elseif strcmp(pov, 'right') == 1 view(90, 0) elseif strcmp(pov, 'front') == 1 view(0, 90) elseif strcmp(pov, 'back') == 1 view(180, -90) else view(35, 20) end % Define a rectangular box representing the iPhone: [xbox, ybox, zbox] = meshgrid((W/2)*[-1, 1], (L/2)*[-1, 1], T/2); % Draw the iPhone, using the imported images for the front and back: iphone = hgtransform; iphone_front = surface(xbox, flipud(ybox), zbox, iphone_front_image, ... 'facecolor', 'texturemap', 'facealpha', 1, ... 'edgealpha', 1, 'parent', iphone); iphone_back = surface(fliplr(xbox), flipud(ybox), -zbox, ... iphone_back_image, 'facecolor', 'texturemap', ... 'facealpha', 1, 'edgealpha', 1, 'parent', iphone); bottom = patch('xdata', (W/2)*[-1, -1, 1, 1], ... 'ydata', (L/2)*[-1, -1, -1, -1], ... 'zdata', (T/2)*[-1, 1, 1, -1], 'facecolor', 'w', ... 'facealpha', 1, 'edgealpha', 1, 'parent', iphone); top = patch('xdata', (W/2)*[-1, -1, 1, 1], 'ydata', (L/2)*[1, 1, 1, 1], ... 'zdata', (T/2)*[-1, 1, 1, -1], 'facecolor', 'w', ... 'facealpha', 1, 'edgealpha', 1, 'parent', iphone); right = patch('xdata', (W/2)*[1, 1, 1, 1], ... 'ydata', (L/2)*[1, 1, -1, -1], ... 'zdata', (T/2)*[-1, 1, 1, -1], 'facecolor', 'w', ... 'facealpha', 1, 'edgealpha', 1, 'parent', iphone); left = patch('xdata', -(W/2)*[1, 1, 1, 1], ... 'ydata', (L/2)*[1, 1, -1, -1], ... 'zdata', (T/2)*[-1, 1, 1, -1], 'facecolor', 'w', ... 'facealpha', 1, 'edgealpha', 1, 'parent', iphone); % Use the provided Euler angle sequence to specify the appropriate axes of % rotation: sequence = [r1, r2, r3]; for k = 1:3 if sequence(k) == 1 rotation_axes(:,k) = [1, 0, 0]'; elseif sequence(k) == 2 rotation_axes(:,k) = [0, 1, 0]'; else rotation_axes(:,k) = [0, 0, 1]'; end end % Display the iPhone in its initial position and orientation: iphone.Matrix = makehgtform('translate', [x1(1), x2(1), x3(1)]', ... 'axisrotate', rotation_axes(:,1), psi(1), ... 'axisrotate', rotation_axes(:,2), theta(1), ... 'axisrotate', rotation_axes(:,3), phi(1)); drawnow % Animate the iPhone's motion by updating the figure with its current % location and orientation: pause % animation = VideoWriter(strcat('iphone-', pov, '-view.avi')); % animation.FrameRate = round(1/dt)/3; % open(animation); for k = 1:length(psi) iphone.Matrix = makehgtform('translate', [x1(k), x2(k), x3(k)]', ... 'axisrotate', rotation_axes(:,1), psi(k), ... 'axisrotate', rotation_axes(:,2), theta(k), ... 'axisrotate', rotation_axes(:,3), phi(k)); drawnow % writeVideo(animation, getframe(gcf)); end % close(animation);