问题描述
我正在使用 delaunay 三角化将散点图转换为曲面。为了使该图动画化,我想更新 trisurf
句柄而不是创建新的 trisurf
图以减少开销并提高绘图速度。
基本上,在 for 循环中,我想更新 trisurf
句柄 h
的属性以获得再次调用 trisurf
会产生的相同图。
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
解决方法
您可以更改 trisurf()
返回的 Patch 对象的一些属性:
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,'Vertices',[X(:) Y(:) Z(:)],'FaceVertexCData',mag(:) );
pause(0.25); %just so we can see the result
end
其中 z
被假定为始终是标量,就像对 trisurf()
的原始调用一样。
- 问:这些选项是否同样快?
- A:我在我的计算机(R2019a,Linux)上运行了一些测试(见下面的代码),发现当 x/y 位置的数量是 2 到 20 之间的随机数时,多个 {{1}使用
set()
的 } 调用比使用Vertices
和相关属性的set()
调用快约 20%,并且这些策略比多个XData
调用快大约一个数量级.然而,当 x/y 位置的数量允许从 2 到 200 变化时,三种方法的运行时间大致相同。
trisurf()