📄 object_drawing_functions.m
字号:
v = [0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1];
vx = v(:,1) * (ax - ix) + ix;
vy = v(:,2) * (ay - iy) + iy;
vz = v(:,3) * (az - iz) + iz;
f = [1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8];
patch('Vertices',[vx vy vz],'Faces',f,'facecolor','none','LineWidth',1, ...
'facealpha',0.3,'edgecolor','c','linestyle','-');
% show label
text(ix,iy,iz,obj.obj_id,'VerticalAlignment','bottom', ...
'BackgroundColor','w','edgecolor','c','fontsize',12);
%===================================================================
function draw_a_sampled_current(obj)
ix = obj.min_x; iy = obj.min_y; iz = obj.min_z;
ax = obj.max_x; ay = obj.max_y; az = obj.max_z;
len = max([ax-ix ay-iy az-iz]);
cx = (ix+ax)/2; cy = (iy+ay)/2; cz = (iz+az)/2;
ir = len/20;
switch (obj.direction)
case ('xn')
[v1, f1] = a_vector([cx+3*ir cx-3*ir], [iy iy], [iz iz], ir, 2*ir, 0.5);
[v2, f2] = a_vector([cx+3*ir cx-3*ir], [iy iy], [az az], ir, 2*ir, 0.5);
[v3, f3] = a_vector([cx+3*ir cx-3*ir], [ay ay], [iz iz], ir, 2*ir, 0.5);
[v4, f4] = a_vector([cx+3*ir cx-3*ir], [ay ay], [az az], ir, 2*ir, 0.5);
[v5, f5] = a_vector([cx+3*ir cx-3*ir], [cy cy], [cz cz], ir, 2*ir, 0.5);
case ('xp')
[v1, f1] = a_vector([cx-3*ir cx+3*ir], [iy iy], [iz iz], ir, 2*ir, 0.5);
[v2, f2] = a_vector([cx-3*ir cx+3*ir], [iy iy], [az az], ir, 2*ir, 0.5);
[v3, f3] = a_vector([cx-3*ir cx+3*ir], [ay ay], [iz iz], ir, 2*ir, 0.5);
[v4, f4] = a_vector([cx-3*ir cx+3*ir], [ay ay], [az az], ir, 2*ir, 0.5);
[v5, f5] = a_vector([cx-3*ir cx+3*ir], [cy cy], [cz cz], ir, 2*ir, 0.5);
case ('yn')
[v1, f1] = a_vector([ix ix], [cy+3*ir cy-3*ir], [iz iz], ir, 2*ir, 0.5);
[v2, f2] = a_vector([ax ax], [cy+3*ir cy-3*ir], [iz iz], ir, 2*ir, 0.5);
[v3, f3] = a_vector([ix ix], [cy+3*ir cy-3*ir], [az az], ir, 2*ir, 0.5);
[v4, f4] = a_vector([ax ax], [cy+3*ir cy-3*ir], [az az], ir, 2*ir, 0.5);
[v5, f5] = a_vector([cx cx], [cy+3*ir cy-3*ir], [cz cz], ir, 2*ir, 0.5);
case ('yp')
[v1, f1] = a_vector([ix ix], [cy-3*ir cy+3*ir], [iz iz], ir, 2*ir, 0.5);
[v2, f2] = a_vector([ax ax], [cy-3*ir cy+3*ir], [iz iz], ir, 2*ir, 0.5);
[v3, f3] = a_vector([ix ix], [cy-3*ir cy+3*ir], [az az], ir, 2*ir, 0.5);
[v4, f4] = a_vector([ax ax], [cy-3*ir cy+3*ir], [az az], ir, 2*ir, 0.5);
[v5, f5] = a_vector([cx cx], [cy-3*ir cy+3*ir], [cz cz], ir, 2*ir, 0.5);
case ('zn')
[v1, f1] = a_vector([ix ix], [iy iy], [cz+3*ir cz-3*ir], ir, 2*ir, 0.5);
[v2, f2] = a_vector([ix ix], [ay ay], [cz+3*ir cz-3*ir], ir, 2*ir, 0.5);
[v3, f3] = a_vector([ax ax], [iy iy], [cz+3*ir cz-3*ir], ir, 2*ir, 0.5);
[v4, f4] = a_vector([ax ax], [ay ay], [cz+3*ir cz-3*ir], ir, 2*ir, 0.5);
[v5, f5] = a_vector([cx cx], [cy cy], [cz+3*ir cz-3*ir], ir, 2*ir, 0.5);
case ('zp')
[v1, f1] = a_vector([ix ix], [iy iy], [cz-3*ir cz+3*ir], ir, 2*ir, 0.5);
[v2, f2] = a_vector([ix ix], [ay ay], [cz-3*ir cz+3*ir], ir, 2*ir, 0.5);
[v3, f3] = a_vector([ax ax], [iy iy], [cz-3*ir cz+3*ir], ir, 2*ir, 0.5);
[v4, f4] = a_vector([ax ax], [ay ay], [cz-3*ir cz+3*ir], ir, 2*ir, 0.5);
[v5, f5] = a_vector([cx cx], [cy cy], [cz-3*ir cz+3*ir], ir, 2*ir, 0.5);
end
v = [v1; v2; v3; v4; v5];
f = f1;
f = [f; max(max(f)) + f2];
f = [f; max(max(f)) + f3];
f = [f; max(max(f)) + f4];
f = [f; max(max(f)) + f5];
col = [1 0.6 0.2];
patch('Vertices',v,'Faces',f,'facecolor',col, ...
'facealpha',1,'edgecolor','none');
% draw boundaries
v = [0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1];
vx = v(:,1) * (ax - ix) + ix;
vy = v(:,2) * (ay - iy) + iy;
vz = v(:,3) * (az - iz) + iz;
f = [1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8];
patch('Vertices',[vx vy vz],'Faces',f,'facecolor','none','LineWidth',1, ...
'facealpha',0.3,'edgecolor',col,'linestyle','-');
% show label
text(ix,iy,iz,obj.obj_id,'VerticalAlignment','bottom', ...
'BackgroundColor','w','edgecolor',col,'fontsize',12);
%========================================================================
function draw_axis_outside_domain(obj)
dx = obj.dx;
dy = obj.dy;
dz = obj.dz;
lx = obj.lx;
ly = obj.ly;
lz = obj.lz;
d = max([dx dy dz]);
refx = lx - 10*d;
refy = ly - 10*d;
refz = lz;
[vt1, f1] = a_vector([refx refx], [refy refy], [refz refz+5*d], 0.5*d, d, 0.3);
[vt2, f2] = a_vector([refx refx], [refy refy+5*d], [refz refz], 0.5*d, d, 0.3);
[vt3, f3] = a_vector([refx refx+5*d], [refy refy], [refz refz], 0.5*d, d, 0.3);
vertices = [vt1; vt2; vt3];
f2 = f2 + max(max(f1));
f3 = f3 + max(max(f2));
faces = [f1;f2;f3];
col = 'g';
patch('Vertices',vertices,'Faces',faces,'facecolor',col, ...
'facealpha',1,'edgecolor','none');
text(refx+6*d,refy,refz,'x','fontsize',12);
text(refx,refy+6*d,refz,'y','fontsize',12);
text(refx,refy,refz+6*d,'z','fontsize',12);
%========================================================================
function draw_axis_at_origin(obj)
dx = obj.dx;
dy = obj.dy;
dz = obj.dz;
d = max([dx dy dz]);
[vt1, f1] = a_vector([0 0], [0 0], [0 5*d], 0.5*d, d, 0.3);
[vt2, f2] = a_vector([0 0], [0 5*d], [0 0], 0.5*d, d, 0.3);
[vt3, f3] = a_vector([0 5*d], [0 0], [0 0], 0.5*d, d, 0.3);
vertices = [vt1; vt2; vt3];
f2 = f2 + max(max(f1));
f3 = f3 + max(max(f2));
faces = [f1;f2;f3];
col = 'g';
patch('Vertices',vertices,'Faces',faces,'facecolor',col, ...
'facealpha',1,'edgecolor','none');
text(6*d,0,0,'x','fontsize',12);
text(0,6*d,0,'y','fontsize',12);
text(0,0,6*d,'z','fontsize',12);
% ===============================================================
function [vertices, faces] = a_circle(cx, cy, cz, ir, dn, lr)
% cx: x coordinates of the circle center
% cy: y coordinates of the circle center
% cz: z coordinates of the circle center
% ir: radius of the circle
% dn: normal direction
% lr: line radius
t = -lr:lr/20:lr;
[X,Y,Z] = cylinder(ir-lr+sqrt(lr^2-t.^2),72);
Z = Z*2*lr-lr;
[f1,v1] = surf2patch(X,Y,Z,Z);
[X,Y,Z] = cylinder(ir-lr-sqrt(lr^2-t.^2),72);
Z = Z*2*lr-lr;
[f2,v2] = surf2patch(X,Y,Z,Z);
faces = [f1; f2+max(max(f1))];
v = [v1;v2];
switch dn
case 'x'
v(:,1) = v(:,1) + cy;
v(:,2) = v(:,2) + cz;
v(:,3) = v(:,3) + cx;
vertices = [v(:,3) v(:,1) v(:,2)];
case 'y'
v(:,1) = v(:,1) + cz;
v(:,2) = v(:,2) + cx;
v(:,3) = v(:,3) + cy;
vertices = [v(:,2) v(:,3) v(:,1)];
case 'z'
v(:,1) = v(:,1) + cx;
v(:,2) = v(:,2) + cy;
v(:,3) = v(:,3) + cz;
vertices = [v(:,1) v(:,2) v(:,3)];
end
% ===============================================================
function [vertices, faces] = a_plus(cx, cy, cz, len, dn, lr)
% cx: x coordinates of the plus center
% cy: y coordinates of the plus center
% cz: z coordinates of the plus center
% len: size of the plus
% dn: normal direction
% lr: line radius
switch dn
case 'x'
[v1, f1] = a_line([cx cx], [cy-len/2 cy+len/2], [cz cz], lr);
[v2, f2] = a_line([cx cx], [cy cy], [cz-len/2 cz+len/2], lr);
case 'y'
[v1, f1] = a_line([cx-len/2 cx+len/2], [cy cy], [cz cz], lr);
[v2, f2] = a_line([cx cx], [cy cy], [cz-len/2 cz+len/2], lr);
case 'z'
[v1, f1] = a_line([cx cx], [cy-len/2 cy+len/2], [cz cz], lr);
[v2, f2] = a_line([cx-len/2 cx+len/2], [cy cy], [cz cz], lr);
end
vertices = [v1; v2];
faces = [f1; max(max(f1)) + f2];
%========================================================================
function [vertices, faces] = a_minus(cx, cy, cz, len, dn, lr, dir)
% cx: x coordinates of the plus center
% cy: y coordinates of the plus center
% cz: z coordinates of the plus center
% len: size of the plus
% dn: normal direction
% lr: line radius
% dir: text direction
switch dn
case 'x'
if dir=='z'
[v, f] = a_line([cx cx], [cy-len/2 cy+len/2], [cz cz], lr);
else
[v, f] = a_line([cx cx], [cy cy], [cz-len/2 cz+len/2], lr);
end
case 'y'
if dir=='z'
[v, f] = a_line([cx-len/2 cx+len/2], [cy cy], [cz cz], lr);
else
[v, f] = a_line([cx cx], [cy cy], [cz-len/2 cz+len/2], lr);
end
case 'z'
if dir=='x'
[v, f] = a_line([cx cx], [cy-len/2 cy+len/2], [cz cz], lr);
else
[v, f] = a_line([cx-len/2 cx+len/2], [cy cy], [cz cz], lr);
end
end
vertices = v;
faces = f;
%========================================================================
function [vertices,faces] = a_vector(ix, iy, iz, ir, hr, hl)
% ix: x coordinates of the start and end points
% iy: y coordinates of the start and end points
% iz: z coordinates of the start and end points
% ir: radius of the vector line
% hr: radius of the head
% hl: head length to vector length ratio
N=36;
[x y z] = cylinder(1,N-1);
x = x(1,:).'; y = y(1,:).'; z = z(1,:).';
v = [[0 0 0]; x*ir y*ir z; x*ir y*ir z+(1-hl); ...
x*hr y*hr z+(1-hl); [0 0 1]];
za = zeros(1,N-1);
f1 = [za+1; za+1; [2:N]; [3:N+1]];
f2 = [[2:N]; [3:N+1]; [3+N:2*N+1]; [2+N:2*N]];
f3 = [[2+N:2*N]; [3+N:2*N+1]; [3+2*N:3*N+1]; [2+2*N:3*N]];
f4 = [[2+2*N:3*N]; [3+2*N:3*N+1]; za+3*N+2; za+3*N+2];
faces = [f1 f2 f3 f4].';
% scale, rotate, and move the vector
lenx = ix(2)-ix(1);
leny = iy(2)-iy(1);
lenz = iz(2)-iz(1);
lenxyz = (lenx^2+leny^2+lenz^2)^0.5;
lenxy = (lenx^2+leny^2)^0.5;
theta = atan2(lenxy, lenz);
phi = atan2(leny, lenx);
v(:, 3) = v(:, 3) * lenxyz;
% rotation matrix
Rx = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Ry = [cos(theta) 0 sin(theta); 0 1 0; -sin(theta) 0 cos(theta)];
Rz = [cos(phi) -sin(phi) 0; sin(phi) cos(phi) 0; 0 0 1];
vt = (Rz * (Ry *(v.'))).';
vt(:,1) = vt(:,1) + ix(1);
vt(:,2) = vt(:,2) + iy(1);
vt(:,3) = vt(:,3) + iz(1);
vertices = vt;
% ===============================================================
function [vertices, faces] = a_line(ix, iy, iz, ir)
% ix: x coordinates of the start and end points
% iy: y coordinates of the start and end points
% iz: z coordinates of the start and end points
% ir: radius of the line
N=36;
[x y z] = cylinder(1, N-1);
x = x(1,:).'; y = y(1,:).'; z = z(1,:).';
v = [[0 0 0]; x*ir y*ir z; x*ir y*ir z+1; [0 0 1]];
za = ones(1,N-1);
f1 = [za; za; [2:N]; [3:N+1]];
f2 = [[2:N]; [3:N+1]; [3+N:2*N+1]; [2+N:2*N]];
f3 = [[2+N:2*N]; [3+N:2*N+1]; za+2*N+1; za+2*N+1];
faces = [f1 f2 f3].';
% scale, rotate, and move the line
lenx = ix(2)-ix(1);
leny = iy(2)-iy(1);
lenz = iz(2)-iz(1);
lenxyz = (lenx^2+leny^2+lenz^2)^0.5;
lenxy = (lenx^2+leny^2)^0.5;
theta = atan2(lenxy, lenz);
phi = atan2(leny, lenx);
v(:, 3) = v(:, 3) * lenxyz;
% rotation matrix
Rx = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Ry = [cos(theta) 0 sin(theta); 0 1 0; -sin(theta) 0 cos(theta)];
Rz = [cos(phi) -sin(phi) 0; sin(phi) cos(phi) 0; 0 0 1];
vt = (Rz * (Ry *(v.'))).';
vt(:,1) = vt(:,1) + ix(1);
vt(:,2) = vt(:,2) + iy(1);
vt(:,3) = vt(:,3) + iz(1);
vertices = vt;
% ===============================================================
function [vertices, faces] = a_sphere(cx, cy, cz, ir)
% cx: x coordinates of the sphere center
% cy: y coordinates of the sphere center
% cz: z coordinates of the sphere center
% ir: radius of the sphere
N=36;
[x,y,z] = sphere(N);
x = x * ir + cx;
y = y * ir + cy;
z = z * ir + cz;
[faces,vertices] = surf2patch(x,y,z,z);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -