如何在Matlab中绘制3D网格(立方体)

17

您好,我想绘制一个透明的立方体网格并在其中画线。类似于这样:enter image description here

但是,我只能绘制出一个二维网格:

[X,Y] = meshgrid(-8:.5:8);
Z = X+1;
surf(X,Y,Z)
我使用 Matlab R2009b。 如果在Matlab中无法绘制此图,您能否向我推荐一种可以使用的软件。

1
这是一个非常奇怪的视角。那是什么?透视图?等轴测图?还是其他什么东西? - Rook
6个回答

19
考虑这种向量化的解决方案。它具有创建单个图形对象的优点。请参阅此处获取更多信息。
%# these don't all have to be the same
x = -8:2:8; y = -8:2:8; z = -8:2:8;

[X1 Y1 Z1] = meshgrid(x([1 end]),y,z);
X1 = permute(X1,[2 1 3]); Y1 = permute(Y1,[2 1 3]); Z1 = permute(Z1,[2 1 3]);
X1(end+1,:,:) = NaN; Y1(end+1,:,:) = NaN; Z1(end+1,:,:) = NaN;
[X2 Y2 Z2] = meshgrid(x,y([1 end]),z);
X2(end+1,:,:) = NaN; Y2(end+1,:,:) = NaN; Z2(end+1,:,:) = NaN;
[X3 Y3 Z3] = meshgrid(x,y,z([1 end]));
X3 = permute(X3,[3 1 2]); Y3 = permute(Y3,[3 1 2]); Z3 = permute(Z3,[3 1 2]);
X3(end+1,:,:) = NaN; Y3(end+1,:,:) = NaN; Z3(end+1,:,:) = NaN;

%#figure('Renderer','opengl')
h = line([X1(:);X2(:);X3(:)], [Y1(:);Y2(:);Y3(:)], [Z1(:);Z2(:);Z3(:)]);
set(h, 'Color',[0.5 0.5 1], 'LineWidth',1, 'LineStyle','-')

%#set(gca, 'Box','on', 'LineWidth',2, 'XTick',x, 'YTick',y, 'ZTick',z, ...
%#  'XLim',[x(1) x(end)], 'YLim',[y(1) y(end)], 'ZLim',[z(1) z(end)])
%#xlabel x, ylabel y, zlabel z
axis off
view(3), axis vis3d
camproj perspective, rotate3d on

screenshot


13

如果您不介意使用一些for循环,可以尝试以下方法:

clf
figure(1)
for g = 0:.2:2
for i = 0:.2:2

   plot3([g g], [0 2], [i, i])
   hold on
end
end

for g = 0:.2:2
for i = 0:.2:2

   plot3([0 2], [g g], [i, i])
   hold on
end
end

for g = 0:.2:2
for i = 0:.2:2

   plot3([i i], [g g], [0 2])
   hold on
end
end

您只需要通过更改线条属性使网格透明,我认为您无法更改Alpha值来实现这一点。希望这有帮助。


12

斯蒂芬的回答更加向量化的版本可能是以下内容:

i = 0:0.2:2;
[X Y] = meshgrid(i,i);                         
x = [X(:) X(:)]';                                
y = [Y(:) Y(:)]';
z = [repmat(i(1),1,length(x)); repmat(i(end),1,length(x))];
col = 'b';
hold on;
plot3(x,y,z,col);                                         
plot3(y,z,x,col);
plot3(z,x,y,col);

很遗憾,就我所知,MATLAB目前不支持透明线条。如果您确实需要它们是透明的,我建议使用“patch”。


我正在尝试使用您的代码绘制旋转的立方体(而不是网格)。您能否解释一下最后三行为什么是这样的? - Alex Kreimer

1

我知道这是一个晚回复,但如果有人想做同样的事情,这仍然是有效的。

假设你正在绘制立方体(/它们的边缘),除了已经提供的答案之外,另一个选择是使用Oliver的“plotcube”代码: plotcube

这种解决方案的优点是,您可以:

  1. 更改面的透明度(FaceAlpha),和/或
  2. 更改边缘的透明度(EdgeAlpha),和/或
  3. 更改线条的颜色(EdgeColor)。

所有这些都可以是常量或变量。 (例如固定边缘颜色,或随Z值变化的颜色等)

要添加2.和3.(上述内容)的功能,请更改Oliver代码中的“cellfun(@patch ...”部分,并添加四行额外的代码,如下所示:(用这个替换整个cellfun部分,包括新的“EdgeAlpha”和“EdgeColor”行):

cellfun(@patch,XYZ{1},XYZ{2},XYZ{3},...
  repmat({clr},6,1),...
  repmat({'FaceAlpha'},6,1),...
  repmat({alpha},6,1),...
  repmat({'EdgeAlpha'},6,1),...
  repmat({0.2},6,1),...      % Set this value to whatever you want; even a variable / matrix
  repmat({'EdgeColor'},6,1),...  
  repmat({'black'},6,1)...
  );

如果想了解“patch”的更多信息,请参阅patch文档。

重要提示: - 对于大型模型(许多立方体),运行速度非常慢。 例如,在MATLAB中对数千个块运行此“plotcube”函数。我认为这是由于多次调用“patch”函数造成的。 更好的解决方案是矢量化;首先将所有点(顶点/面/任何内容)放在单个矩阵中,然后仅调用一次@patch函数(没有“for”循环)。这将需要以某种方式更改代码以更新所有XYZ数据。

希望这能帮助到某些人。

以下是“plotcube”代码,以防链接到Oliver的原始代码有一天失效:

function plotcube(varargin)
% PLOTCUBE - Display a 3D-cube in the current axes
%
%   PLOTCUBE(EDGES,ORIGIN,ALPHA,COLOR) displays a 3D-cube in the current axes
%   with the following properties:
%   * EDGES : 3-elements vector that defines the length of cube edges
%   * ORIGIN: 3-elements vector that defines the start point of the cube
%   * ALPHA : scalar that defines the transparency of the cube faces (from 0
%             to 1)
%   * COLOR : 3-elements vector that defines the faces color of the cube
%
% Example:
%   >> plotcube([5 5 5],[ 2  2  2],.8,[1 0 0]);
%   >> plotcube([5 5 5],[10 10 10],.8,[0 1 0]);
%   >> plotcube([5 5 5],[20 20 20],.8,[0 0 1]);

% Default input arguments
inArgs = { ...
  [10 56 100] , ... % Default edge sizes (x,y and z)
  [10 10  10] , ... % Default coordinates of the origin point of the cube
  .7          , ... % Default alpha value for the cube's faces
  [1 0 0]       ... % Default Color for the cube
  };

% Replace default input arguments by input values
inArgs(1:nargin) = varargin;

% Create all variables
[edges,origin,alpha,clr] = deal(inArgs{:});

XYZ = { ...
  [0 0 0 0]  [0 0 1 1]  [0 1 1 0] ; ...
  [1 1 1 1]  [0 0 1 1]  [0 1 1 0] ; ...
  [0 1 1 0]  [0 0 0 0]  [0 0 1 1] ; ...
  [0 1 1 0]  [1 1 1 1]  [0 0 1 1] ; ...
  [0 1 1 0]  [0 0 1 1]  [0 0 0 0] ; ...
  [0 1 1 0]  [0 0 1 1]  [1 1 1 1]   ...
  };

XYZ = mat2cell(...
  cellfun( @(x,y,z) x*y+z , ...
    XYZ , ...
    repmat(mat2cell(edges,1,[1 1 1]),6,1) , ...
    repmat(mat2cell(origin,1,[1 1 1]),6,1) , ...
    'UniformOutput',false), ...
  6,[1 1 1]);


cellfun(@patch,XYZ{1},XYZ{2},XYZ{3},...
  repmat({clr},6,1),...
  repmat({'FaceAlpha'},6,1),...
  repmat({alpha},6,1)...
  );

view(3);

0

你可以通过设置颜色=[0.65, 0.65, 0.65]来使内部线条半透明。并且你可以使用虚线样式来绘制内部线条,实线样式来绘制边界线条,使其更像一个三维物体。

在我的软件包中,我编写了一个mesh3函数来绘制三维张量积网格。


0
clear all 
close all
clc
Nx=11;
Ny=11;
Nz=11;
clf
hold on
[i,j]=meshgrid(1:Nx,1:Ny);
k=zeros(Ny,Nx)+Nz;
surf(i,j,k)
[i,k]=meshgrid(1:Nx,1:Nz);
j=zeros(Nz,Nx)+Ny;
surf(i,j,k)
[j,k]=meshgrid(1:Ny,1:Nz);
i=zeros(Nz,Ny)+Nx;
surf(i,j,k)
[i,j]=meshgrid(1:Nx,1:Ny);
k=zeros(Ny,Nx)+1;
surf(i,j,k)
[i,k]=meshgrid(1:Nx,1:Nz);
j=zeros(Nz,Nx)+1;
surf(i,j,k)
[j,k]=meshgrid(1:Ny,1:Nz);
i=zeros(Nz,Ny)+1;
surf(i,j,k)
view(30,30)

1
除了代码之外,一些解释会使这个答案更有帮助。 - Tot Zam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接