如何使用matlab的trimesh和颜色选项?

发布于 2024-11-14 15:09:36 字数 144 浏览 2 评论 0原文

我想绘制一个三角形网格,并用不同的颜色为每个边缘着色。 trimesh 的 matlab 文档指出有一个颜色参数,但没有说明其结构应该是什么 - 由于三角形共享边,我如何知道在向量内的位置放置哪种颜色C?

I want to plot a triangular mesh, and color each edge with a different color. The matlab documentation for trimesh states there's a color argument, but it doesn't state what its structure should be - as the triangles share edges, how do I know where to position which color inside the vector C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸠书 2024-11-21 15:09:36

昨天我正在为这个问题苦苦挣扎,我想我已经找到了解决方案。事实证明,一般来说,如果你想非常详细地了解哪条边是什么颜色,你就必须非常狡猾。

所以你明白问题是什么了,我首先回顾一下命令 trimesh(Tri,X,Y,Z,C) 如何分配颜色。参数 C 是一个数字向量,其长度等于顶点(不是边)的数量。它对边缘着色的方式如下。如果我们有 4 个顶点并且我们分配

Tri=[1 2 3; 3 2 4]

C=[10 20 30 40]

则 Tri 中找到的第一条边是 1 到 2,并且使用点 1 的颜色对其进行着色,即 10。找到的下一条边是 2 到 3,即使用点 2 的颜色进行着色,即 20。下一条边是 3 to 1,颜色为 30。下一条边是 3 to 2,颜色为 30;请注意,该边缘已被着色为 20,但此新颜色会覆盖旧颜色。然后,边缘 2 到 4 的颜色为 20,边缘 4 到 3 的颜色为 40。

问题是,如果不使用特殊技巧,这种着色方案实际上不可能按照您想要的方式对边缘进行着色。你的例子:如果你的边比顶点多(如我的示例),并且你希望每条边的颜色不同,那么你就不走运了,因为边的颜色根据分配给顶点的颜色< /em>。但是,如果我们为每个顶点制作多个副本(因此,为每条边制作多个副本),那么我们就可以恢复正常了。此外,Tri 不仅接受人脸,还接受单独的边缘,这让我们的生活变得更加轻松。

作为我的意思的一个例子,假设您希望上例中的边 2 到 3 的颜色为 50。为此,创建一个新点,即点 5,它与点 2 相同(即相同的 (x,y,z) 坐标)。重新定义 Tri 和 C 如下:

Tri=[1 2 3; 3 2 4;3 5 3]

C=[10 20 30 40 50]

请注意,Tri 中的最后三个数字并不定义面,而是定义边(边 3 到 5,与 2 到 3 相同) 。该边缘将由分配的任何颜色点 5 着色。通过这种方式,您可以单独覆盖每个边缘的颜色。只需确保将这些边放在 Tri 矩阵的末尾,以便它们的颜色覆盖任何以前不需要的颜色。如果您不关心面是否被填充,您甚至可以拥有一个只有边缘的 Tri 矩阵。这有点麻烦,但它确实有效。

I was struggling with this problem yesterday, and I think I have the solution. It turns out that in general if you want to be very detailed with which edge is which color, you have to be very sneaky.

So you understand what the problem is, I'll first review how the command trimesh(Tri,X,Y,Z,C) assigns colors in the first place. The argument C has be a vector of numbers with length equaling the number of vertices (not edges). The way it colors edges is as follows. If we have 4 vertices and we assign

Tri=[1 2 3; 3 2 4]

and

C=[10 20 30 40]

then the first edge found in Tri is 1 to 2 and it is colored using the color of point 1, which is 10. The next edge found is 2 to 3, which is colored using the color of point 2, which is 20. The next edge is 3 to 1, which has color 30. The next edge is 3 to 2 with color 30; notice this edge has already been colored 20 but this new color overwrites the old color. And then edge 2 to 4 is colored 20 and edge 4 to 3 is colored 40.

The problem is that without using a special trick, this coloring scheme can make it literally impossible to color the edges exactly as you want. Your case in point: if you have more edges than vertices (as in my example) and you want each edge to be colored differently, you are out of luck because the edges are colored according to the colors assigned to the vertices. But if we make multiple copies of each vertex (and, consequently, multiple copies of each edge), then we are back in business. Also, our lives are made easier by the fact that Tri accepts not just faces but also individual edges as well.

As an example of what I mean, suppose you wanted edge 2 to 3 to have the color 50 in the example above. To do this, make a new point, point 5, which is the same as point 2 (i.e., the same (x,y,z) coordinates). Redefine Tri and C as follows:

Tri=[1 2 3; 3 2 4;3 5 3]

C=[10 20 30 40 50]

Notice that the last three numbers in Tri do not define a face but rather an edge (the edge 3 to 5, which is the same as 2 to 3). This edge will be colored by whatever color point 5 is assigned. In this manner, you can overwrite the colors of every edge individually. Just be sure to put these edges at the end of the Tri matrix so that their colorings overwrite any previous undesired colorings. If you don't care about having faces filled in, you can even have a Tri matrix with nothing but edges. Its a bit of a hack, but it works.

悲歌长辞 2024-11-21 15:09:36

您可以使用 TRIMESH 函数查看底层代码href="http://www.mathworks.com/help/techdoc/ref/type.html" rel="nofollow">TYPE 命令如下:

type trimesh

这样做可以让您看到 TRIMESH 实际上通过调用 PATCH 函数,具有一组面\顶点数据。因此,您可以使用着色补丁文档来确定如何定义 TRIMESH 的颜色数据参数,以便获得优势给你想要的颜色。

请注意,传递给 TRIMESH 的颜色数据参数最终定义了 'FaceVertexCData' 属性创建的结果补丁对象。

You can look at the underlying code of the TRIMESH function using the TYPE command like so:

type trimesh

Doing so allows you to see that TRIMESH actually creates the mesh plot by calling the PATCH function with a set of face\vertex data. You can therefore use the documentation for coloring patches to determine how to define your color data argument for TRIMESH so that you get the edge coloring you want.

Note that the color data argument passed to TRIMESH ultimately defines the 'FaceVertexCData' property of the resulting patch object that is created.

◇流星雨 2024-11-21 15:09:36

如果您不提供 C 矩阵,Matlab 会按字面意思设置 C=Z。因此,C 是一个与高度矩阵 (Z) 大小相同的矩阵。你不需要专门给它颜色;相反,您给它一个数字向量,Matlab 根据其当前的颜色图将其解释为颜色。

尝试一下

[x,y]=meshgrid(1:15,1:15);
tri = delaunay(x,y);
z = peaks(15);
trimesh(tri,x,y,z)

,然后使用颜色图

colormap HSV
colormap spring
colormap gray

等。

我很确定您可以在 Matlab 中定义自己的颜色图。根据文件,

颜色图是由 0.0 到 1.0 之间的实数组成的 m×3 矩阵。每一行都是定义一种颜色的 RGB 向量。颜色图的第 k 行定义第 k 个颜色,其中 map(k,:) = [r(k) g(k) b(k)]) 指定红色、绿色和蓝色。

我希望这有帮助。

If you don't provide the C matrix Matlab literally sets C=Z. Therefore C is a matrix the same size as your heights matrix (Z). You don't specifically give it colors; rather, you give it a vector of numbers which Matlab interprets as colors according to its current colormap.

Try this

[x,y]=meshgrid(1:15,1:15);
tri = delaunay(x,y);
z = peaks(15);
trimesh(tri,x,y,z)

and then play around with colormaps

colormap HSV
colormap spring
colormap gray

and so on.

I'm pretty sure you can define you own colormaps in Matlab. According to documentation,

A colormap is an m-by-3 matrix of real numbers between 0.0 and 1.0. Each row is an RGB vector that defines one color. The kth row of the colormap defines the kth color, where map(k,:) = [r(k) g(k) b(k)]) specifies the intensity of red, green, and blue.

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文