解决的实际问题是:UG中的一条轮廓曲线(封闭曲线),使用等弧长导出1000点时,导出的igs文件中的数据点是按照X的大小进行排序的,而不是沿着曲线的某一方向排序, 使用下面的matlab算法可以将数据点按照某一方向进行排序。排好序后的数据点方便进行NURBS曲线拟合。
%matlab脚本
%多个数据排序
%只考虑截面2 4 6 8 10 12
clc
clear
% close all
for a=2:2:12
str1='截面';
str2=num2str(a);
str3='/半理论截面';%可以在 '/理论截面' '/包络截面' '/半理论截面' '/半包络截面' 等之间切换
str4='.dat';
str5=[str1 str2 str3 str2 str4];%输入文件的格式
str6='sort.dat';
str7=[str1 str2 str3 str2 str6];%输出文件的格式
point=load(str5);
[m,n]=size(point);
%排序
Y=point(:,2);%根据Y的大小进行排序
[big,y]=max(Y);
sortpoint=[];
sortpoint(1,:)=point(y,:);
point(y,:)=zeros;%将统计过的point中的元素置为零,根据实际情况调整,0或inf无穷
t=0;
while t<m-1
mpoint=sortpoint(end,:);
S=[];
for i=1:m
s=sqrt((point(i,1)-mpoint(1))^2+(point(i,2)-mpoint(2))^2+(point(i,3)-mpoint(3))^2);
S=[S;s];
end
[T,r]=min(S);
sortpoint=[sortpoint;point(r,:)];
point(r,:)=zeros;
t=t+1;
end
%去掉重复的点,在实际过程中可能会有重复的点
for i=1:m-1%将重复的点置为零向量
for j=i+1:m
if sortpoint(i,:)==sortpoint(j,:)
sortpoint(j,:)=zeros;
end
end
end
%去除sortpoint中的零向量%
zero_rows = find(all(sortpoint==0,2));
sortpoint(zero_rows,:) = [];
dlmwrite(str7,sortpoint);%导出排序后的点集
plot3(sortpoint(:,1),sortpoint(:,2),sortpoint(:,3));%画出排序后的点
hold on
% axis equal;
end