% 读取车门PLY点云数据
ptCloud = pcread('door_cloud.ply');
% 点云预处理
% 1. 去除噪点
[ptCloudDenoised, ~] = pcdenoise(ptCloud);
% 2. 下采样以减少计算量
gridSize = 0.01; % 网格大小(m)
ptCloudDownsampled = pcdownsample(ptCloudDenoised, 'gridAverage', gridSize);
% 获取车门尺寸
points = ptCloudDownsampled.Location;
minX = min(points(:,1));
maxX = max(points(:,1));
minY = min(points(:,2));
maxY = max(points(:,2));
minZ = min(points(:,3));
maxZ = max(points(:,3));
% 路径规划参数
pathSpacing = 0.05; % 路径间距(m)
zigzagWidth = maxX - minX; % 之字形宽度
% 生成之字形喷漆路径
numPaths = ceil((maxZ - minZ) / pathSpacing);
sprayPaths = cell(numPaths, 1);
for i = 1:numPaths
z = maxZ - (i-1) * pathSpacing;
code Copy code
% 找到当前高度附近的点
zIndices = find(abs(points(:,3) - z) < pathSpacing/2);
if ~isempty(zIndices)
% 提取这些点
zPoints = points(zIndices, :);
% 如果点太少,扩大搜索范围
if size(zPoints, 1) < 5
zIndices = find(abs(points(:,3) - z) < pathSpacing);
zPoints = points(zIndices, :);
end
if ~isempty(zPoints)
% 创建之字形路径
% 根据行数决定从左到右还是从右到左
if mod(i, 2) == 1
% 奇数行:从左到右
[~, sortIdx] = sort(zPoints(:,1));
else
% 偶数行:从右到左
[~, sortIdx] = sort(zPoints(:,1), 'descend');
end
sortedPoints = zPoints(sortIdx,:);
% 平滑路径 - 使用移动平均
if size(sortedPoints, 1) > 5
windowSize = min(5, size(sortedPoints, 1));
smoothedPoints = zeros(size(sortedPoints));
for j = 1:size(sortedPoints, 1)
startIdx = max(1, j - floor(windowSize/2));
endIdx = min(size(sortedPoints, 1), j + floor(windowSize/2));
smoothedPoints(j,:) = mean(sortedPoints(startIdx:endIdx,:), 1);
end
sortedPoints = smoothedPoints;
end
% 存储路径
sprayPaths{i} = sortedPoints;
end
end
end
% 可视化
figure;
pcshow(ptCloud, 'MarkerSize', 10);
title('车门点云数据');
xlabel('X (m)');
ylabel('Y (m)');
zlabel('Z (m)');
figure;
pcshow(ptCloudDownsampled, 'MarkerSize', 20);
hold on;
% 显示喷漆路径
cmap = colormap(jet(numPaths));
for i = 1:numPaths
if ~isempty(sprayPaths{i})
plot3(sprayPaths{i}(:,1), sprayPaths{i}(:,2), sprayPaths{i}(:,3), '-', 'LineWidth', 2, 'Color', cmap(i,:));
end
end
% 添加起点和终点标记
for i = 1:numPaths
if ~isempty(sprayPaths{i})
% 标记每条路径的起点
plot3(sprayPaths{i}(1,1), sprayPaths{i}(1,2), sprayPaths{i}(1,3), 'go', 'MarkerSize', 8, 'MarkerFaceColor', 'g');
% 标记每条路径的终点
plot3(sprayPaths{i}(end,1), sprayPaths{i}(end,2), sprayPaths{i}(end,3), 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r');
end
end
title('车门之字形喷漆路径规划');
xlabel('X (m)');
ylabel('Y (m)');
zlabel('Z (m)');
legend('点云数据', '喷漆路径', '路径起点', '路径终点');
hold off;
% 输出路径数据
fprintf('共生成%d条喷漆路径\n', numPaths);
fprintf('路径间距: %.2f m\n', pathSpacing);
增加计算车门点云散点的法向量,统一各点方向在目标喷漆涂面下方的代码