简体   繁体   English

MATLAB更新trisurf手柄

[英]MATLAB Update trisurf handle

I'm using a Delaunay triangularization to convert a scatter plot to a surface.我正在使用 Delaunay 三角化将散射 plot 转换为表面。 To animate this plot, I want to update the trisurf handle instead of creating a new trisurf plot to reduce overhead and to increase the plotting speed.要为这个 plot 设置动画,我想更新trisurf句柄而不是创建新的trisurf plot 以减少开销并提高绘图速度。

Basically, in a for loop, I want to update the properties of the trisurf handle h to obtain the same plot that calling trisurf again would yield.基本上,在 for 循环中,我想更新trisurf句柄h的属性,以获得与再次调用trisurf将产生的相同 plot 。

MWE MWE

x = linspace(0,1,11); 
y = x;
[X,Y] = meshgrid(x,y);
mag = hypot(X(:),Y(:)); % exemplary magnitude
T = delaunay(X(:),Y(:));

z = 0

h = trisurf(T, X(:), Y(:), z*ones(size(X(:))), mag, 'FaceColor', 'interp'); view([-90 90]);

for i = 1:10
    % Compute new values for X, Y, z, and mag
    % -> Update properties of handle h to redraw the trisurf plot instead
    %    of recalling the last line before the for loop again, e.g.,
    % h.FaceVertexCData = ...
    % h.Faces = ...
    % h.XData = ...
end

You can change a few properties of the Patch object returned by trisurf() :您可以更改trisurf()返回的补丁 object 的一些属性:

for i = 1:9
  % Compute new values for X, Y, z, and mag
  % As an example:
  x = linspace(0,1,11-i);
  y = x;
  [X,Y] = meshgrid(x,y);
  mag = hypot(X(:),Y(:));
  T = delaunay(X(:),Y(:));

  z = i;
  Z = z*ones(size(X)); %we could have just called `meshgrid()` with 3 arguments instead
  % End recomputation

  % Update trisurf() patch: option 1
  set( h, 'Faces',T, 'XData',X(T).', 'YData',Y(T).', 'ZData',Z(T).', 'CData',mag(T).' );
  pause(0.25); %just so we can see the result
  % Update trisurf() patch: option 2
  set( h, 'Faces',T, 'Vertices',[X(:) Y(:) Z(:)], 'FaceVertexCData',mag(:) );
  pause(0.25); %just so we can see the result
end

where z is assumed to always be a scalar, just like in the original call to trisurf() .其中z假定始终是一个标量,就像对trisurf()的原始调用一样。

  • Q: Are these options equally fast?问:这些选项是否同样快?
  • A: I have run some tests (see code below) on my computer (R2019a, Linux) and found that, when the number of x/y-positions is a random number between 2 and 20, multiple set() calls using Vertices can be some 20% faster than set() calls using XData and related properties, and that these strategies are about an order of magnitude faster than multiple trisurf() calls. A:我在我的计算机(R2019a,Linux)上运行了一些测试(见下面的代码),发现当 x/y 位置的数量是 2 到 20 之间的随机数时,使用Vertices的多个set()调用可以比使用XData和相关属性的set()调用快大约 20%,并且这些策略比多个trisurf()调用快大约一个数量级。 When the number of x/y-positions is allowed to vary from 2 to 200, however, run times are about the same for the three approaches.然而,当 x/y 位置的数量允许在 2 到 200 之间变化时,三种方法的运行时间大致相同。
Nruns=1e3;
Nxy_max=20;

for i=1:Nruns
  if i==round(Nruns/10)
    tic(); %discard first 10% of iterations
  end
  x = linspace(0,1,randi(Nxy_max-1)+1); %randi([2,Nxy_max]) can be a bit slower
  [X,Y,Z] = meshgrid(x,x,randn());
  mag = hypot(X(:),Y(:));
  T = delaunay(X(:),Y(:));
  trisurf(T, X(:), Y(:), Z(:), mag, 'FaceColor', 'interp');
  view([-90 90]);
end
tmean_trisurf=1e3*toc()/(Nruns-round(Nruns/10)+1), %in [ms]

h=trisurf(T, X(:), Y(:), Z(:), mag, 'FaceColor', 'interp');
view([-90 90]);

for i=1:Nruns
  if i==round(Nruns/10)
    tic();
  end
  x = linspace(0,1,randi(Nxy_max-1)+1);
  [X,Y,Z] = meshgrid(x,x,randn());
  mag = hypot(X(:),Y(:));
  T = delaunay(X(:),Y(:));
  set( h, 'Faces',T, 'XData',X(T).', 'YData',Y(T).', 'ZData',Z(T).', 'CData',mag(T).' );
end
tmean_xyzdata=1e3*toc()/(Nruns-round(Nruns/10)+1), %in [ms]

for i=1:Nruns
  if i==round(Nruns/10)
    tic();
  end
  x = linspace(0,1,randi(Nxy_max-1)+1);
  [X,Y,Z] = meshgrid(x,x,randn());
  mag = hypot(X(:),Y(:));
  T = delaunay(X(:),Y(:));
  set( h, 'Faces',T, 'Vertices',[X(:) Y(:) Z(:)], 'FaceVertexCData',mag(:) );
end
tmean_vertices=1e3*toc()/(Nruns-round(Nruns/10)+1), %in [ms]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM