如何在 MATLAB 中绘制具有特定颜色图的 3D 三角形网格形状?

How to plot a 3D triangle mesh shape with a specific colormap in MATLAB?

3D 三角形网格形状由顶点和三角形面表示。 例如matlab中的shapeshape.X, shape.Y, shape.Z(the vertices) and shape.TRIV(the triangle faces)可以看作是一个3D三角形网格形状

我的问题是如何在 MATLAB 中用特定的颜色图可视化这样的形状

(例如,颜色映射可以定义为length(shape.X)距离向量,其元素为所有顶点到单个顶点的欧几里德距离M,在这种情况下,较冷的颜色与较小的距离相关,而较热的颜色与较大的距离相关.)

您可以使用 patch 对象来显示您的 3D 形状,然后使用生成的补丁的 FaceVertexCDataFaceColor 属性来设置一个可以自动映射到的值坐标区颜色限制。

vertices = cat(2, shape.X(:), shape.Y(:), shape.Z(:));

%// Create the patch object
h = patch('Vertices', vertices, ...
          'Faces', shape.TRIV);

%// Compute distance of each vertex from the origin
distances = sqrt(sum(bsxfun(@minus, vertices, [0 0 0]).^2, 2));

%// Set the vertex colors and use interpolation to shade the faces
set(h, 'FaceColor', 'interp', ...
       'FaceVertexCData', distances);

%// Scale the color limits to your data
set(gca, 'clim', [min(distances(:)), max(distances(:))])

根据@Suever 的回答,我添加了一些额外的代码来使形状图更平滑并添加了相机灯。顺便说一句,如果形状是没有面的点云,最好选择scatter3而不是plot3

vertices = cat(2, shape.X(:), shape.Y(:), shape.Z(:));

%// Compute distance of each vertex from the origin
distances = sqrt(sum(bsxfun(@minus, vertices, [0 0 0]).^2, 2)); 

%// Create the patch object
h = patch('Vertices', vertices, 'Faces', shape.TRIV);

%// Set the vertex colors and use interpolation to shade the faces
set(h, 'FaceColor', 'interp', 'FaceVertexCData', distances,'EdgeColor', 'none');
%or use shading interp instead of setting 'EdgeColor'=='none' to make the shape smooth;

%// Scale the color limits to your data
set(gca, 'clim', [min(distances(:)), max(distances(:))])

% add a colorbar
colorbar

% change the colormap
colormap(jet(64))

%use the same unit length along each axis  and fit the axes box tightly around the data.
axis image 

% turn off the coordinate
axis off

% set the camlight strength, trial and error
set(h, 'AmbientStrength',0.25, 'SpecularStrength',0.0,'DiffuseStrength',0.5);
lighting phong;
camlight left; %left,right,head
set(gcf,'Renderer','opengl');  %‘opengl’,'zbuffer'

如果是点云:

h2=scatter3(X,Y,Z,'.');
view([0,90]);
h2.CData=distances;
axis image;
set(gca, 'clim', [min(distances(:)), max(distances(:))]);
colormap(jet(64));