使用MATLAB生成三维圆柱形Voronoi图

发布于:2025-07-24 ⋅ 阅读:(11) ⋅ 点赞:(0)

使用MATLAB生成三维圆柱形Voronoi图。首先生成圆柱体内的随机点,然后计算Voronoi图,最后将结果可视化:

function cylindrical_voronoi_3d()
    % 参数设置
    numPoints = 100;      % Voronoi种子点数量
    cylinderRadius = 5;   % 圆柱体半径
    cylinderHeight = 10;  % 圆柱体高度
    resolution = 50;      % 圆柱面网格分辨率
    
    % 生成圆柱体内的随机点
    r = cylinderRadius * sqrt(rand(numPoints, 1));      % 半径方向均匀分布
    theta = 2 * pi * rand(numPoints, 1);                % 角度方向均匀分布
    z = cylinderHeight * rand(numPoints, 1);            % 高度方向均匀分布
    
    % 转换为笛卡尔坐标
    x = r .* cos(theta);
    y = r .* sin(theta);
    points = [x, y, z];
    
    % 计算3D Voronoi图
    [v, c] = voronoin(points);
    
    % 创建圆柱体表面用于裁剪
    [cylX, cylY, cylZ] = cylinder(cylinderRadius, resolution);
    cylZ = cylinderHeight * cylZ;
    
    % 可视化设置
    figure('Color', 'white', 'Position', [100, 100, 1200, 900])
    hold on;
    axis equal;
    grid on;
    view(3);
    xlabel('X');
    ylabel('Y');
    zlabel('Z');
    title('3D Cylindrical Voronoi Diagram');
    
    % 绘制圆柱体表面
    surf(cylX, cylY, cylZ, 'FaceAlpha', 0.1, 'EdgeColor', 'none', 'FaceColor', [0.8 0.8 0.8]);
    
    % 绘制Voronoi单元(裁剪到圆柱体内)
    for i = 1:length(c)
        % 获取当前单元的顶点
        vertices = v(c{i}, :);
        
        % 检查单元是否有效(非无穷远单元)
        if all(isfinite(vertices), 'all')
            % 计算单元中心
            centroid = mean(vertices);
            
            % 计算顶点到中心的距离
            dists = sqrt(sum((vertices - centroid).^2, 2));
            
            % 创建凸包
            try
                [k, vol] = convhull(vertices);
                
                % 绘制裁剪后的单元
                patch('Vertices', vertices, 'Faces', k, ...
                      'FaceColor', rand(1,3), 'FaceAlpha', 0.7, ...
                      'EdgeColor', 'k', 'LineWidth', 1.2);
            catch
                % 跳过无效的凸包
            end
        end
    end
    
    % 绘制种子点
    scatter3(points(:,1), points(:,2), points(:,3), 50, ...
             'filled', 'MarkerEdgeColor', 'k', ...
             'MarkerFaceColor', 'r');
    
    % 设置视角
    view(135, 30);
    camlight;
    lighting gouraud;
    
    % 添加注释
    annotation('textbox', [0.05, 0.9, 0.2, 0.05], 'String', ...
               sprintf('Points: %d\nRadius: %.1f\nHeight: %.1f', ...
                       numPoints, cylinderRadius, cylinderHeight), ...
               'BackgroundColor', 'white', 'FontSize', 10);
    
    hold off;
end
  1. 参数设置

    • numPoints:Voronoi种子点数量(增加点数会创建更精细的图案)
    • cylinderRadius:圆柱体半径
    • cylinderHeight:圆柱体高度
    • resolution:圆柱面网格分辨率
  2. 点生成方法

    • 在柱坐标中生成随机点(半径、角度、高度)
    • 半径使用sqrt(rand)确保点在圆柱体内均匀分布
    • 将柱坐标转换为笛卡尔坐标
  3. Voronoi计算

    • 使用voronoin函数计算3D Voronoi图
    • 返回顶点(v)和单元信息(c)
  4. 可视化处理

    • 创建半透明圆柱体作为参考
    • 对每个Voronoi单元:
      • 计算凸包以处理3D多面体
      • 使用随机颜色绘制每个单元
      • 保持边缘为黑色以增强可视化效果
    • 用红色标记种子点
  5. 裁剪处理

    • 通过只处理有限单元(非无穷远单元)自动裁剪
    • 单元自然限制在点集凸包内,适合圆柱形

    参考 使用MATLAB生成三维圆柱形voronoi youwenfan.com/contentcsa/50989.html

特点:

  • 生成类似晶体结构的3D圆柱形Voronoi图案
  • 每个Voronoi单元是凸多面体
  • 单元在圆柱边界自然终止
  • 使用半透明渲染展示内部结构

调整:

  1. 增加点数(如200-500)获得更精细的结构

  2. 修改圆柱尺寸比例创建不同形状:

    cylinderRadius = 3;
    cylinderHeight = 15;  % 细长圆柱
    
  3. 改变渲染风格:

    % 改为单色渲染
    'FaceColor', [0.2 0.4 0.8] % 替代 rand(1,3)
    
  4. 添加旋转动画:

    for az = 0:5:360
        view(az, 30);
        drawnow;
        pause(0.05);
    end
    

此方法通过生成圆柱体内的随机点并计算其3D Voronoi图,自然地创建了圆柱形Voronoi结构。每个单元在圆柱边界自动终止,无需复杂的几何裁剪操作。


网站公告

今日签到

点亮在社区的每一天
去签到