【问题标题】:How to plot a 3D triangle mesh shape with a specific colormap in MATLAB?如何在 MATLAB 中绘制具有特定颜色图的 3D 三角形网格形状?
【发布时间】:2016-09-08 00:19:04
【问题描述】:

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

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

(例如,colormap可以定义为length(shape.X)距离向量,其元素是所有顶点到单个顶点@的欧几里得距离987654324@,在这种情况下,较冷的颜色与较小的距离有关,较热的颜色与较大的距离有关。)

【问题讨论】:

    标签: matlab plot 3d mesh point-clouds


    【解决方案1】:

    您可以使用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(:))])
    

    【讨论】:

    • 如果形状是没有面的点云(shape.TRIV)怎么办?
    • @GuWang 如果你有点云,那么你需要使用plot3scatter3
    • 如何使 patch 绘制的形状更平滑?以及如何为形状添加光源?
    • @GuWang 你可以打开 FaceLighting 然后创建一个light
    • 你能给我一些使用 FaceLighting 的示例代码吗?
    【解决方案2】:

    根据@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));
    

    【讨论】:

      猜你喜欢
      • 2020-12-15
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 2011-03-11
      • 2015-11-13
      • 1970-01-01
      相关资源
      最近更新 更多