在 matlab 中绘制一堆 3d 线的最有效方法
我需要在 matlab 中绘制 3d 线列表。最快的方法是什么? 我目前正在做类似的事情
%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
,但这不仅在实际循环期间很慢,而且在最后,当我尝试使用拖动和旋转来旋转它时,结果图非常慢且无响应。旋转工具。
我知道使用 opengl 等的相同情节会运行得更快......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 LINE 低级函数,使用
NaN
绘制为单独的线段:这非常有效,因为它创建了单个线对象。现在您可以自定义线条,但整个线条只能使用一种颜色:
根据评论,如果您希望图形中的每条边都具有指定的颜色,请考虑以下代码。它涉及使用 SURFACE 函数:
You can use the LINE low-level function, using
NaN
to plot as separate segments:This is very efficient as it creates a single line object. Now you can customize the line, but it is restricted to have one color for the entire thing:
According to the comments, if you want to have each edge in your graph in a specified color, consider the following code instead. It involves using the SURFACE function:
我认为你可以做这样的事情(注意 - 大脑编译的代码...)
其中
edges
应该是一个Nx2
索引和点
矩阵应该是一个Mx3
坐标矩阵(points
数组的转置)。根据我的经验,直接调用
patch
比重复调用plot
要快得多。举个例子,使用我的(确实很旧!)MATLAB 7.1 生成 1000 个随机生成的线段的时间如下:
patch
:0.03 秒。编辑:使边缘颜色表现得如您所愿(为每个边缘指定单一颜色)的一种方法是引入重复的顶点,如下所示:
这解决了只能指定边缘颜色的问题间接通过顶点颜色数据。如果我们只依赖顶点颜色,那么共享一个公共顶点的所有边可能最终都会分配给该顶点的颜色 - 请查看'flat 'edgecolour描述此处。
如果 MATLAB 允许您直接指定边缘颜色数据,那就更好了 - 但它似乎不支持这一点......
希望这会有所帮助。
I think you can do something like this (caution - brain compiled code...)
Where
edges
should be anNx2
matrix of indices andpoints
should be anMx3
matrix of coordinates (the transpose of yourpoints
array).From my experience, calling
patch
directly 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.