MATLAB 3D 动画制作(二)- 3D 动画动作设计
MATLAB 3D 动画制作(二)- 3D 动画动作设计
MATLAB 3D Animation – 3D Animation Action Design
Auther: Sonictl Northeastern University.
前言:本系列教程将逐步完成一个有趣的MATLAB 3D动画,MATLAB 3D图形的绘制和3D动画的制作可以让工程人员很直观地展示工作内容,特别是在虚拟现实,控制系统设计,人机交互,计算机视觉,等领域都能得到应用。
MATLAB 3D 动画制作(二)- 3D 动画动作设计 是 MATLAB 3D 动画制作(一)- 3D 图形设计 的进阶篇。
Introduction: This series of tutorials are going to discuss a process of making 3D animation using MATLAB step by step. The 3D animation in MATLAB can let the engineers exhibit their work. It can be used widely, especially at the fields of Virtual Reality, Control system Designing, Human-machine interactivity, Computer Vision, etc.
首先介绍一下本教程的最终效果:
Below is the final effect of this 3D animation.
Section 2: 3D Animation Action Design (3D 动画动作设计)
请在“爱问共享资料”搜索“MATLAB_3D实例_sTopEu”下载本实例的源码及说明文档资料包。
关于3D动作设计,暂时没有时间详述。
3D动画的原理就是: 计算新的坐标,绘图一次,再计算新的坐标,再绘图,如此往复循环,就实现了3D动画。
此处我们用到的是欧拉旋转矩阵来计算物体的旋转角:
物体的XYZ坐标 –> 用欧拉旋转矩阵计算出新的坐标 –> 绘图一次
循环
关于对“MATLAB_3D实例_sTopEu”资料包的说明:
资料包的文件列表如下:
使用时,运行一次sTopMain.m,点击“add to Path”,以此将它添加进MATLAB工作目录。
也许报错。然后在MATLAB 命令行窗口中输入“sTopMain(animation_A.dat)”,回车即可。
animaiton_A.dat – 数据文件,存储了物体旋转角数据,以及帧间延迟时间
createInnerBars.m
createOuterBars.m
cylinderWeights.m
torus.m – 都是建立3D物体的
EuMat.m – 欧拉旋转矩阵
multiplyEuMat.m – 用欧拉旋转矩阵计算新的坐标
sTopEuler.m – 绘图,装配
sTopMain.m – 主函数,实现动画
In this section, We are going to discuss the approach to build the 3D object in MATLAB figure window. We are going to begin with the simple object like sphere, bar, cylinder, torus. And we also discuss make a more complicated 3D object by assembling these simple 3D object together.
1.1 introduction to the Tools we are going to use:
- surf function in MATLAB
- cylinder function in MATLAB
- sphere function in MATLAB
1.1.1 surf function in MATLAB
surf(X,Y,Z) uses Z for the color data and surface height. X and Y are vectors or matrices defining thex and y components of a surface. If X and Y are vectors, length(X) = n and length(Y) = m, where[m,n] = size(Z). In this case, the vertices of the surface faces are (X(j), Y(i), Z(i,j)) triples. To create X and Y matrices for arbitrary domains, use the meshgrid function.
Examples
Color a sphere with the pattern of +1s and -1s in a Hadamard matrix.
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
figure
surf(X,Y,Z)
Read More about surf: http://www.mathworks.com/help/matlab/ref/surf.html
1.1.2 cylinder function in MATLAB
[X,Y,Z] = cylinder(r,n) returns the x-, y-, and z-coordinates of a cylinder based on the profile curve defined by vector r. The cylinder has n equally spaced points around its circumference.
Examples
[X,Y,Z]=cylinder(1,20);
surf(X,Y,Z);
Read More about cylinder in MATLAB: http://www.mathworks.com/help/matlab/ref/cylinder.html
1.1.3 sphere function in MATLAB
sphere generates a sphere consisting of 20-by-20 faces.
sphere(n) draws a surf plot of an n-by-n sphere in the current figure.
[X,Y,Z] = sphere(n) returns the coordinates of a sphere in three matrices that are (n+1)-by-(n+1) in size. You draw the sphere with surf(X,Y,Z) or mesh(X,Y,Z).
Examples
[x,y,z] = sphere;
surf(x,y,z) % sphere centered at origin
Read More about sphere in MATLAB: http://www.mathworks.com/help/matlab/ref/sphere.html
1.2 Creating the Coordinates for the simple object like sphere, cylinder, bar, torus.
1.2.1 sphere:
[x,y,z] = sphere(20);
x = x+1; y = y-1; z = z*2;
surf(x,y,z)
axis([-3 3 -3 3 -3 3])
axis square
1.2.2 cylinder
[x,y,z] = cylinder(1,20);
z = z*3;
surf(x,y,z)
axis([-3 3 -3 3 -3 3])
axis square
1.2.3 bar
r2 = 1.2; r1 = 1;
t=0:0.01:1;
x=t;
for i=1:numel(t)
if ((i>10 && i<20) || (i>80 && i<90))
x(i)=r2;
else
x(i)=r1;
end
end
[X,Y,Z] = cylinder(x,30);
surf(X,Y,Z*3,'EdgeColor','none','FaceColor','blue') % Here is the key point
camlight left;
lighting gouraud; % phong is more demanding but gives nicer results
xlabel('x'); ylabel('y'); zlabel('z');
axis([-3 3 -3 3 -3 3])
axis square
We can see the effect below:
1.2.4 torus
a = 2; % average radius
n = 40; % num of segment for whole torus.
r = 0.2; % radius of cutted circle
kpi = 1.5; % completion of turos, form 0 to 2, 2 means 2pi
theta = -pi * (0:2:kpi*n)/n ;
phi = 2*pi* (0:2:n)'/n ;
x = (a + r*cos(phi)) * cos(theta) ;
y = (a + r*cos(phi)) * sin(theta) ;
z = r * sin(phi) * ones(size(theta)) ;
surf(x,y,z,'EdgeColor','none','FaceColor','red') % Here is the key point
camlight left;
lighting gouraud; % phong is more demanding but gives nicer results
xlabel('x'); ylabel('y'); zlabel('z');
axis([-3 3 -3 3 -3 3])
axis square
Note: If we use the coordinate transformation method, we can modify the posture of our object:
surf(z,y,x)
1.3 Generate the complicated 3-D object by assembling them together
Here, we are going to generate the 3-D object like the figure shows below:
In order to use the code for drawing 3-D object more organizedly, more effeciently and more conveniently, We save these code as functions into different .m files. So that we can call them to draw what we want to draw. After finished the .m files shows below, this tutorial will show you how to build the 3D object above step by step.
1.3.1 Create the coordinates for the Inter Bars
Below is the code for the function of creating the blue inter bar. Save it as createInnerBars.m
function [ Xbar1,Ybar1,Zbar1,Xbar2,Ybar2,Zbar2 ] = ...
createInnerBars( bar1R1,bar1R2,bar2R1,bar2R2)
%CREATEINNERBARS Creates the bars of the inner (blue) ring
%
% bar[i]R[j]: There are two bars which are perpendicular to each other.
% They are melted with the inner ring, so the ring together with the bars
% form the inner part of the top.
% R1 always describes the radius of the bar itself. Additionally, both
% bars have two attatched weigths each. Both of the attatched weigths are
% cylinders of R2.
[Zbar1,Ybar1,Xbar1]=cylinderWeights(bar1R1,bar1R2); % bar1 is the lying one
Xbar1=Xbar1-0.5; % move the center to (0,0,0)
Xbar1=Xbar1*10; % extend the length
[Xbar2,Ybar2,Zbar2]=cylinderWeights(bar2R1,bar2R2);
Zbar2=Zbar2-0.5;
Zbar2=Zbar2*10;
1.3.2 Create the coordinates for the cylinderWeights
Below is the code for the function of creating the cylinerWeight for the blue inter bar. Save it ascylinderWeights.m
function [X,Y,Z] = cylinderWeights(r1,r2)
%CYLINDERWEIGHTS creates the innermost blue bars with heavy looking cylinders
%representing attatched weights.
t=0:0.01:1;
x=t;
for i=1:numel(t)
if ((i>10 && i<20) || (i>80 && i<90))
x(i)=r2;
else
x(i)=r1;
end
end
[X,Y,Z]=cylinder(x,30);
1.3.3 Create the coordinates for the Outer Bars
Below is the code for the function of Creating the red motionless supporting bar. Save it ascreateOuterBars.m
function [ Xupper,Yupper,Zupper,Xlower,Ylower,Zlower ] = ...
createOuterBars( radius )
%CREATEOUTERBARS Creates the red motionless supporting bar
[Xupper,Yupper,Zupper] = cylinder(radius);
Zupper=Zupper.*5;
Zupper=Zupper+5.9;
[Xlower,Ylower,Zlower] = cylinder(radius);
Zlower=Zlower.*5;
Zlower=Zlower-10.9;
1.3.4 Create the coordinates for Torus
Below is the code for the function of creating torus. Save it as torus.m
function [x, y, z] = torus (a, n, r, kpi)
% TORUS Generate a torus.
% torus (r, n, a, kpi) generates a plot of a
% torus with central radius a and
% lateral radius r.
% n controls the number of facets
% on the surface.
% kpi makes it possible to draw a whole torus,
% or e.g. half of it.
%
% This script is a modification of a
% program from:
% MATLAB Primer, 6th Edition
% Kermit Sigmon and Timothy A. Davis
% Section 11.5, page 65.
theta = -pi * (0:2:kpi*n)/n ;
phi = 2*pi* (0:2:n)'/n ;
x = (a + r*cos(phi)) * cos(theta) ;
y = (a + r*cos(phi)) * sin(theta) ;
z = r * sin(phi) * ones(size(theta)) ;
1.3.5 Create the coordinates for sTopEuler. (The final object we want)
Below is the code for the function of creating the sTopEuler. Save it as sTopEuler.m
% coordinates of the three rings (blue, red and green)
[X1,Y1,Z1]=torus(5-0.3,30,0.3,2); % the top (blue)
[X2,Z2,Y2]=torus(5+0.3,30,0.3,2); % inner gimbal (green, semi-transparent)
[X3,Z3,Y3]=torus(5+0.9,30,0.3,1); % outer gimbal (red, semi-transparent)
% handles to surf plots of the three rings
H1=surf(X1,Y1,Z1,'EdgeColor','none','FaceColor','blue'); hold on;
H2=surf(X2,Y2,Z2,'EdgeColor','none','FaceColor','green','FaceAlpha',0.3);
H3=surf(X3,Y3,Z3,'EdgeColor','none','FaceColor','red','FaceAlpha',0.3);
% creates the bars inside the inner (blue) ring. The wide cylinders
% represent weights.
[Xbar1,Ybar1,Zbar1,Xbar2,Ybar2,Zbar2]=createInnerBars(0.2,0.5,0.2,0.5);
bar1=surf(Xbar1,Ybar1,Zbar1,'EdgeColor','none','FaceColor','blue');
bar2=surf(Xbar2,Ybar2,Zbar2,'EdgeColor','none','FaceColor','blue');
% the red motionless supporting bar
[ Xupper,Yupper,Zupper,Xlower,Ylower,Zlower ] = createOuterBars( 0.3 );
surf(Xlower,Ylower,Zlower,...
'EdgeColor','none','FaceColor','red','FaceAlpha',0.3);
hold off;
camlight left;
lighting gouraud; % phong is more demanding but gives nicer results
bb=7;
axis([-bb bb -bb bb -bb bb]);
axis square;
xlabel('x'); ylabel('y'); zlabel('z');
Until now, you can run the assembling whold objects file sTopEuler.m to draw the 3-D object above. Or, after you added the sTopEuler.m into path of MATLAB, you can directly run this function by typing in “sTopEuler” at the command window of MATLAB.
Below, we are going to show you how to use the functions we built to draw the 3-D object above step-by-step.
1.3.6 Build the 3D object above step by step
- a. Draw the blue ring:
% coordinates of the three rings (blue, red and green)
[X1,Y1,Z1]=torus(5-0.3,30,0.3,2); % the top (blue)
H1=surf(X1,Y1,Z1,'EdgeColor','none','FaceColor','blue'); hold on;
camlight left;
lighting gouraud; % phong is more demanding but gives nicer results
bb=7;
axis([-bb bb -bb bb -bb bb]);
axis square;
xlabel('x'); ylabel('y'); zlabel('z');
- b. Draw the green ring:
[X2,Z2,Y2]=torus(5+0.3,30,0.3,2); % inner gimbal (green, semi-transparent)
H2=surf(X2,Y2,Z2,'EdgeColor','none','FaceColor','green','FaceAlpha',0.3);
% coordinates of the three rings (blue, red and green)
[X1,Y1,Z1]=torus(5-0.3,30,0.3,2); % the top (blue)
H1=surf(X1,Y1,Z1,'EdgeColor','none','FaceColor','blue'); hold on;
camlight left;
lighting gouraud; % phong is more demanding but gives nicer results
- c. Draw the red half-ring:
[X3,Z3,Y3]=torus(5+0.9,30,0.3,1); % outer gimbal (red, semi-transparent)
H3=surf(X3,Y3,Z3,'EdgeColor','none','FaceColor','red','FaceAlpha',0.3);
camlight left;
lighting gouraud; % phong is more demanding but gives nicer results
- d. Draw the left part:
% creates the bars inside the inner (blue) ring. The wide cylinders
% represent weights.
[Xbar1,Ybar1,Zbar1,Xbar2,Ybar2,Zbar2]=createInnerBars(0.2,0.5,0.2,0.5);
bar1=surf(Xbar1,Ybar1,Zbar1,'EdgeColor','none','FaceColor','blue');
bar2=surf(Xbar2,Ybar2,Zbar2,'EdgeColor','none','FaceColor','blue');
% the red motionless supporting bar
[ Xupper,Yupper,Zupper,Xlower,Ylower,Zlower ] = createOuterBars( 0.3 );
surf(Xlower,Ylower,Zlower,...
'EdgeColor','none','FaceColor','red','FaceAlpha',0.3);
hold off;
camlight left;
lighting gouraud; % phong is more demanding but gives nicer results
bb=7;
axis([-bb bb -bb bb -bb bb]);
axis square;
xlabel('x'); ylabel('y'); zlabel('z');
Thank you!!
Reference: : http://www.mathworks.com/matlabcentral/fileexchange/28309
参考文档:经典教程—matlab三维图形的绘制 http://ishare.iask.sina.com.cn/f/35139508.html