使用 MATLAB 可视化立方晶格等三维阵列
Visualize a three-dimensional array like cubic lattice using MATLAB
我想用 MATLAB 可视化一个像立方晶格一样的三维数组。
我已阅读How to plot 3D grid (cube) in Matlab, and Simple cubic lattice using three-dimensional array
如果数组中的元素只有0和1,我知道如何用三维数组画一个简单的立方体格子,小立方体的大小是一样的。
但是,现在我有一个像这样的三维数组,
cube(:,:,1) =
1 0 1
0 1 1
2 1 0
cube(:,:,2) =
0 0 1
1 5 1
0 1 0
cube(:,:,3) =
1 1 1
0 1 1
2 0 1
数组立方体除了0和1之外还有值。我想把数组形象化为立方体格子,其中cube(:,:,1)表示立方体格子的第一层,
cube(:,:,2) denotes the second floor, and
cube(:,:,3) the third floor.
值0表示什么都没有,而值1表示一个蓝色的小立方体。
大于1的值表示球体,球体的直径根据值的不同而不同。
期望的结果是这样的:
Visualization of a three-dimensional array, 1 denotes a small green
cube,0 denotes nothing,Value greater than 1 denotes white sphere, the
diameter of sphere varies according to the value.
为了更清楚地解释我的问题,展示一个二维数组的可视化
1 denotes a small black sphere,0 denotes nothing,Value greater than 1
denotes white sphere, the diameter of sphere varies according to the
value.
想要的效果图
可以,当立方体的边长为 1
当设置边长为2时,drawCube([ix,iy,iz],2,Royal_Blue)。出现问题,立方体重叠,
让我告诉你我的尝试。它基于独立绘制每个立方体和圆圈。如果 A
很大,这会很慢。
结果:
代码应该是自解释的。
% Create some data. This piece of code just creates some matrix A with
% some 1s and 0s and inserts a 2 and a 3 to specific positions. Subsitute
% this with your own data matrix.
th=0.2;
A=double(rand(10,10,10)<th);
A(1,1,1)=2;
A(5,5,5)=3;
% A nice color. I just dont like the standard blue so I picked another one.
Royal_Blue=[65 105 225]/255;
%%%%%%%%%%%%%%%%%%%%%%
%% Draw cubes
% Obtain all the linear indexes (search mathworks for help between
% subscripts vs linear indices) of the locations where a cube is wanted
% (A==1)
ind=find(A==1);
% Create a figure
fig=figure();
hold on
% Draw the cubes one by one
for ii=1:length(ind)
% For each linear index get its subscript (that also
% will be x,y,z position)
[ix,iy,iz]=ind2sub(size(A),ind(ii));
% Use the drawcube function to draw a single cube in the
% desired position with the desired size (1) and colour.
drawCube([ix,iy,iz],1,Royal_Blue);
end
% Nice plotting code. This just makes the drawing nicer.
camlight left
lighting gouraud
axis equal
axis off
view(-50,25)
%%%%%%%%%%%%%%%
%% Now draw the spheres
% This code is the same as the previous one but I just draw
% spheres instead of cubes.
ind=find(A>1);
% create an sphere
[X,Y,Z] = sphere;
for ii=1:length(ind)
[ix,iy,iz]=ind2sub(size(A),ind(ii));
% scale sphere
Xs=X*A(ix,iy,iz)/2;
Ys=Y*A(ix,iy,iz)/2;
Zs=Z*A(ix,iy,iz)/2;
surf(Xs+ix,Ys+iy,Zs+iz,'edgecolor','none','facecolor',[1 1 1]);
end
% Change the background colour to black
whitebg(fig,'k')
% MAke sure it stays black
set(gcf, 'InvertHardCopy', 'off');
函数drawCube
如下:
function drawCube( origin, size,color)
% From
% http://www.mathworks.com/matlabcentral/newsreader/view_thread/235581
if nargin<3
color='b';
end
x=([0 1 1 0 0 0;1 1 0 0 1 1;1 1 0 0 1 1;0 1 1 0 0 0]-0.5)*size+origin(1);
y=([0 0 1 1 0 0;0 1 1 0 0 0;0 1 1 0 1 1;0 0 1 1 1 1]-0.5)*size+origin(2);
z=([0 0 0 0 0 1;0 0 0 0 0 1;1 1 1 1 0 1;1 1 1 1 0 1]-0.5)*size+origin(3);
for i=1:6
h=patch(x(:,i),y(:,i),z(:,i),color);
set(h,'edgecolor','none')
end
end
我想用 MATLAB 可视化一个像立方晶格一样的三维数组。
我已阅读How to plot 3D grid (cube) in Matlab, and Simple cubic lattice using three-dimensional array
如果数组中的元素只有0和1,我知道如何用三维数组画一个简单的立方体格子,小立方体的大小是一样的。
但是,现在我有一个像这样的三维数组,
cube(:,:,1) =
1 0 1
0 1 1
2 1 0
cube(:,:,2) =
0 0 1
1 5 1
0 1 0
cube(:,:,3) =
1 1 1
0 1 1
2 0 1
数组立方体除了0和1之外还有值。我想把数组形象化为立方体格子,其中cube(:,:,1)表示立方体格子的第一层,
cube(:,:,2) denotes the second floor, and
cube(:,:,3) the third floor.
值0表示什么都没有,而值1表示一个蓝色的小立方体。
大于1的值表示球体,球体的直径根据值的不同而不同。
期望的结果是这样的:
Visualization of a three-dimensional array, 1 denotes a small green cube,0 denotes nothing,Value greater than 1 denotes white sphere, the diameter of sphere varies according to the value.
为了更清楚地解释我的问题,展示一个二维数组的可视化
1 denotes a small black sphere,0 denotes nothing,Value greater than 1 denotes white sphere, the diameter of sphere varies according to the value.
让我告诉你我的尝试。它基于独立绘制每个立方体和圆圈。如果 A
很大,这会很慢。
结果:
代码应该是自解释的。
% Create some data. This piece of code just creates some matrix A with
% some 1s and 0s and inserts a 2 and a 3 to specific positions. Subsitute
% this with your own data matrix.
th=0.2;
A=double(rand(10,10,10)<th);
A(1,1,1)=2;
A(5,5,5)=3;
% A nice color. I just dont like the standard blue so I picked another one.
Royal_Blue=[65 105 225]/255;
%%%%%%%%%%%%%%%%%%%%%%
%% Draw cubes
% Obtain all the linear indexes (search mathworks for help between
% subscripts vs linear indices) of the locations where a cube is wanted
% (A==1)
ind=find(A==1);
% Create a figure
fig=figure();
hold on
% Draw the cubes one by one
for ii=1:length(ind)
% For each linear index get its subscript (that also
% will be x,y,z position)
[ix,iy,iz]=ind2sub(size(A),ind(ii));
% Use the drawcube function to draw a single cube in the
% desired position with the desired size (1) and colour.
drawCube([ix,iy,iz],1,Royal_Blue);
end
% Nice plotting code. This just makes the drawing nicer.
camlight left
lighting gouraud
axis equal
axis off
view(-50,25)
%%%%%%%%%%%%%%%
%% Now draw the spheres
% This code is the same as the previous one but I just draw
% spheres instead of cubes.
ind=find(A>1);
% create an sphere
[X,Y,Z] = sphere;
for ii=1:length(ind)
[ix,iy,iz]=ind2sub(size(A),ind(ii));
% scale sphere
Xs=X*A(ix,iy,iz)/2;
Ys=Y*A(ix,iy,iz)/2;
Zs=Z*A(ix,iy,iz)/2;
surf(Xs+ix,Ys+iy,Zs+iz,'edgecolor','none','facecolor',[1 1 1]);
end
% Change the background colour to black
whitebg(fig,'k')
% MAke sure it stays black
set(gcf, 'InvertHardCopy', 'off');
函数drawCube
如下:
function drawCube( origin, size,color)
% From
% http://www.mathworks.com/matlabcentral/newsreader/view_thread/235581
if nargin<3
color='b';
end
x=([0 1 1 0 0 0;1 1 0 0 1 1;1 1 0 0 1 1;0 1 1 0 0 0]-0.5)*size+origin(1);
y=([0 0 1 1 0 0;0 1 1 0 0 0;0 1 1 0 1 1;0 0 1 1 1 1]-0.5)*size+origin(2);
z=([0 0 0 0 0 1;0 0 0 0 0 1;1 1 1 1 0 1;1 1 1 1 0 1]-0.5)*size+origin(3);
for i=1:6
h=patch(x(:,i),y(:,i),z(:,i),color);
set(h,'edgecolor','none')
end
end