Plotting many spheres in 3D real time
이전 댓글 표시
I am simulating particles in 3D and want to visualize them in real time with a GUI. Right now I'm using a scatter3 to visualize them, but I want a more accurate representation of their sizes and scatter only has markers in pixels. I want to plot each particle as a sphere, but I can't find a way to quickly plot many spheres in different positions. For N particles I have a 3xn matrix, handles.particles , for the 3D positions of the spheres. I want to plot them on the axis handles.simDisplay (there are other axis on the GUI that should not be changed.)
Right now the relevant code in my update timer is
[handles.SphereX,handles.SphereY,handles.SphereZ] = sphere;
subplot(handles.simDisplay); %plot on the correct axis
set(gcf, 'CurrentAxes', handles.simDisplay);%set the axis (does this even do anything?)
for i=1:handles.nParticles
surf(handles.SphereX+handles.particles(i,1),...
handles.SphereY+handles.particles(i,2),...
handles.SphereZ+handles.particles(i,3));
end
This is easy but very slow. It also does not clear previously drawn spheres. Any suggestions?
답변 (1개)
Jan
2014년 5월 4일
Replace
subplot(handles.simDisplay); %plot on the correct axis
set(gcf, 'CurrentAxes', handles.simDisplay);
by
axes(handles.simDisplay);
Reduce the number of patches:
[X, Y, Z] = sphere(16);
If you mention "clear previously drawn spheres", it would be faster, not to clear them, but to adjust their position:
axes('NextPlot', 'add');
[X, Y, Z] = sphere(16);
tic
OldH = [];
for k = 1:100
H = surf(X + rand, Y + rand, Z + rand);
if ~isempty(OldH)
delete(OldH);
end
drawnow;
OldH = H;
end
toc
tic
H = surf(X, Y, Z);
for k = 1:100
set(H, 'XData', X + rand, 'YData', Y + rand, 'ZData', Z + rand);
drawnow;
end
toc
Elapsed time is 2.662571 seconds.
Elapsed time is 0.780100 seconds.
카테고리
도움말 센터 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!