Matlab 三維圖像繪製(2)——surface property、view、空間圓柱體

主要學習了畫空間圓柱體和空間長方形的繪製方法。

有兩個surface property:'FaceColor'和‘EdgeColor’;

先講‘FaceColor’,它指定了surface畫出曲面的顏色,可以是[r,g,b]的一個向量,分別表示了紅綠藍的顏色配比;

也可以是‘interp’,畫出來是由z的值決定的漸變色,可以使用colormapeditor來調節顏色(在代碼中寫上colormapeditor即可喚出調色板);

然後是‘EdgeColor’,它會在曲面的表面畫出網格,指定顏色的方法同上。

但是有一個疑問沒有解決:就是如何只顯示各個棱的網格線,而不是整個面的網格線??這個留待後面繼續摸索吧。


surface(x,y,z)函數畫出來的圖像如果想要平移,可以直接在帶入參數時修改。比如,沿z軸正方向平移10,就是surface(x,y,z+10);


最後有一個實現視角自動旋轉的小功能:

view(az,el)中,az可以調節物體旋轉的角度,el調節攝像機的俯仰角度

for i=1:120
    view(i*1, 30);
    pause(0.01);
end

代碼如下:

function test()
%% 
clear;
clc;
clf;
z_delta = 6;
%% draw head
A = imread('head.jpg');
[x,y,z]=sphere(30);
h0=surface(x,y,z + z_delta,'EdgeColor','none');
rotate(h0, [0,0,1], 90);
set(h0,'CData',A,'FaceColor','texturemap');%texturemap紋理貼圖

%% draw body
% FaceColor (orange)
face_color = 'interp'; %[1, 0.6, 0];
edge_color = 'b';
<pre name="code" class="plain">colormapeditor;
% up board[x1,y1] = meshgrid(-1:0.1:1, -1:0.1:1);z1 = repmat(-1, 21, 21);h1=surface(x1,y1,z1 + z_delta, 'EdgeColor',edge_color,'FaceColor',face_color);% left boardx2 = repmat(1, 21, 21); y2 = repmat([-1:0.1:1],21,1);for i=1:21 for j=1:21 z2(i,j)=-1-(i-1)*0.25; endendh2=surface(x2,y2,z2 + z_delta,'EdgeColor',edge_color,'FaceColor',face_color);% right boardx3 = repmat(-1, 21, 21);y3 = repmat([-1:0.1:1],21,1);for i=1:21 for j=1:21 z3(i,j)=-1-(i-1)*0.25; endendh3=surface(x3,y3,z3 + z_delta,'EdgeColor',edge_color,'FaceColor',face_color);% front boardy4 = repmat(1, 21, 21); %#ok<*RPMT1>x4 = repmat([-1:0.1:1],21,1);for i=1:21 for j=1:21 z4(i,j)=-1-(i-1)*0.25; endendh4=surface(x4,y4,z4 + z_delta,'EdgeColor',edge_color,'FaceColor',face_color);% back boardy5 = repmat(-1, 21, 21);x5 = repmat([-1:0.1:1],21,1);for i=1:21 for j=1:21 z5(i,j)=-1-(i-1)*0.25; endendh5=surface(x5,y5,z5 + z_delta,'EdgeColor',edge_color,'FaceColor',face_color);%% draw armsrobot = robot_model();r_arm = 0.3;num_of_surf = 36;for i=2:5 if mod(i,2)==1 color_arm = 'r'; else color_arm = 'b'; end [Cylinder(i), EndPlate1(i), EndPlate2(i)] = cylinder3(robot(i).position, robot(i+1).position, r_arm, num_of_surf, color_arm, 1, 0);endfor i=7:10 if mod(i,2)==1 color_arm = 'r'; else color_arm = 'b'; end [Cylinder(i), EndPlate1(i), EndPlate2(i)] = cylinder3(robot(i).position, robot(i+1).position, r_arm, num_of_surf, color_arm, 1, 0);end%% rotate the angle of viewgrid on;axis equal;axis fill;for i=1:120 view(i*1, 30); pause(0.01);endend


下面是由兩個點畫出一個空間圓柱體的代碼,從CSDN上下載的別人的程序,就拿來直接用於畫胳膊了。
 function [Cylinder, EndPlate1, EndPlate2] = cylinder3(X1,X2,r,n,cyl_color,closed,lines)
%
% This function constructs a cylinder connecting two center points 
% 
% Usage :
% [Cylinder EndPlate1 EndPlate2] = cylinder3(X1+20,X2,r,n,'r',closed,lines)
%    
% Cylinder-------Handle of the cylinder
% EndPlate1------Handle of the Starting End plate
% EndPlate2------Handle of the Ending End plate
% X1 and X2 are the 3x1 vectors of the two points
% r is the radius of the cylinder
% n is the no. of elements on the cylinder circumference (more--> refined)
% cyl_color is the color definition like 'r','b',[0.52 0.52 0.52]
% closed=1 for closed cylinder or 0 for hollow open cylinder
% lines=1 for displaying the line segments on the cylinder 0 for only
% surface
% 
% Typical Inputs
% X1=[10 10 10];
% X2=[35 20 40];
% r=1;
% n=20;
% cyl_color='b';
% closed=1;
% 
% NOTE: There is a MATLAB function "cylinder" to revolve a curve about an
% axis. This "Cylinder" provides more customization like direction and etc

%%%%%%%%%%
if (X1(1) > X2(1))
    tmpX = X1; X1 = X2; X2 = tmpX;
end

% Calculating the length of the cylinder
length_cyl=norm(X2-X1);

% Creating a circle in the YZ plane
t=linspace(0,2*pi,n)';
x2=r*cos(t);
x3=r*sin(t);

% Creating the points in the X-Direction
x1=[0 length_cyl];

% Creating (Extruding) the cylinder points in the X-Directions
xx1=repmat(x1,length(x2),1);
xx2=repmat(x2,1,2);
xx3=repmat(x3,1,2);

% Drawing two filled cirlces to close the cylinder
if closed==1
    hold on
    EndPlate1=fill3(xx1(:,1),xx2(:,1),xx3(:,1),'r');
    EndPlate2=fill3(xx1(:,2),xx2(:,2),xx3(:,2),'r');
end

% Plotting the cylinder along the X-Direction with required length starting
% from Origin
Cylinder=mesh(xx1,xx2,xx3);

% Defining Unit vector along the X-direction
unit_Vx=[1 0 0];

% Calulating the angle between the x direction and the required direction
% of cylinder through dot product
angle_X1X2=acos( dot( unit_Vx,(X2-X1) )/( norm(unit_Vx)*norm(X2-X1)) )*180/pi;

% Finding the axis of rotation (single rotation) to roate the cylinder in
% X-direction to the required arbitrary direction through cross product
axis_rot=cross([1 0 0],(X2-X1) );

% Rotating the plotted cylinder and the end plate circles to the required
% angles
if angle_X1X2~=0 % Rotation is not needed if required direction is along X
    rotate(Cylinder,axis_rot,angle_X1X2,[0 0 0])
    if closed==1
        rotate(EndPlate1,axis_rot,angle_X1X2,[0 0 0])
        rotate(EndPlate2,axis_rot,angle_X1X2,[0 0 0])
    end
end

% Till now cylinder has only been aligned with the required direction, but
% position starts from the origin. so it will now be shifted to the right
% position
if closed==1
    set(EndPlate1,'XData',get(EndPlate1,'XData')+X1(1))
    set(EndPlate1,'YData',get(EndPlate1,'YData')+X1(2))
    set(EndPlate1,'ZData',get(EndPlate1,'ZData')+X1(3))
    
    set(EndPlate2,'XData',get(EndPlate2,'XData')+X1(1))
    set(EndPlate2,'YData',get(EndPlate2,'YData')+X1(2))
    set(EndPlate2,'ZData',get(EndPlate2,'ZData')+X1(3))
end
set(Cylinder,'XData',get(Cylinder,'XData')+X1(1))
set(Cylinder,'YData',get(Cylinder,'YData')+X1(2))
set(Cylinder,'ZData',get(Cylinder,'ZData')+X1(3))

% Setting the color to the cylinder and the end plates
set(Cylinder,'FaceColor',cyl_color)
if closed==1
    set([EndPlate1 EndPlate2],'FaceColor',cyl_color)
else
    EndPlate1=[];
    EndPlate2=[];
end

% If lines are not needed making it disapear
if lines==0
    set(Cylinder,'EdgeAlpha',0)
end


發佈了25 篇原創文章 · 獲贊 63 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章