I need to plot a list of 3d lines in matlab. What is the quickest way to do that?
I am currently doing something like
%edges is a MX2 matrix, holding the list of edges
%points are the vertices' coordinates
hold on; %so all the lines will be saved
for i=1:size(edges,1)
a=edges(i,1); %get first point's index
b=edges(i,2); %get second point's index
p=[points(:,a) points(:,b)]; %construct a 3X2 matrix out of the 2 points
plot3(p(1,:),p(2,:),p(3,:)); %plot a line
end
But this is not only slow during the actual loop, but also at the end, the resulting plot is very slow and irresponsive when I try to, for instance, rotate it using the drag & rotate tool.
I know the same plot using opengl etc would run much faster…
I think you can do something like this (caution – brain compiled code…)
Where
edgesshould be anNx2matrix of indices andpointsshould be anMx3matrix of coordinates (the transpose of yourpointsarray).From my experience, calling
patchdirectly can be significantly faster than repeated calls toplot.To give some idea, the times to generate 1000 randomly generated line segments, using my (admittedly old!) MATLAB 7.1 are as follows:
patch: 0.03 seconds.plot: 0.5 seconds.EDIT: One way to get the edge colour behaving as you want (specifying a single colour per edge) is to introduce duplicate vertices as follows:
This works-around the issue that the edge colour can only be specified indirectly via vertex colour data. If we were to rely only on the vertex colours then all edges sharing a common vertex might end up with the colour assigned to that vertex – check out the ‘flat ‘edgecolour description here.
It would be nicer if MATLAB allowed you to directly specify the edge colour data – but it doesn’t seem to support that…
Hope this helps.