下面是我写的包围盒代码,他并没有包围着我所给出的点。我给出了两个点,并由此延伸出来8个点,所有可以构建三维的obb包围盒。请各位大佬帮我看一下我的代码哪里错了。
x1=1600;x2=1430;
y1=1600;y2=1430;
z1=584.6;z2=584.6;
inputdata = [x1-r,y1,z1;x2-r,y2,z2;x1,y1,z1;x2,y2,z2;x1+r,y1,z1;x2+r,y2,z2;x1-r,y1,z1+h;x2-r,y2,z2+h;x1+r,y1,z1+h;x2+r,y2,z2+h]';
theta=acos(dot((a),(b))/(norm(a)*norm(b)))*180/3.1415;
inputada1 = inputdata*theta;
[m,n] = size(inputdata);%获得输入数据的维度
cov = zeros(m,m);%创建协方差矩阵
E = zeros(m,1);%得到矩阵的列数
%获得每维数据的平均值
for i = 1:m
E(i) = mean(inputdata(i,:),2);%bmean(X,1)为列向量的均值,mean(X,2)为行向量的均值。
end
%计算协方差
for i = 1:m
for j = 1:m
cov(i,j) = ((inputdata(i,:)-E(i))*(inputdata(j,:)-E(j))')./(n-1);
s = cov(i,j);
[d,v] = eig(s);%求出协方差矩阵的特征向量,d为特征值矩阵,v为特征向量矩阵
%施密特正交化 变成标准基
end
end
s = cov;
[v,d] = eig(s);%求出协方差矩阵的特征向量,v为特征值矩阵,d为特征向量矩阵
%施密特正交化 变成标准基
[m1,n1] = size(v);%求出特征向量v行和列的大小
b = zeros(m1,n1);
% %正交化
b(:,1) = v(:,1);
for i1 = 2:n1
for j1 = 1:i1-1
b(:,i1) = b(:,i1)-dot(v(:,i1),b(:,j1))/dot(b(:,j1),b(:,j1))*b(:,j1);
end
b(:,i1) = b(:,i1)+v(:,i1);
end
%单位化
% % for k=1:n1
% % b(:,k)=b(:,k)/norm(b(:,k));
% % end
coordMapped1 =(inputdata(1,:)-E(1,1));
coordMapped2 =(inputdata(2,:)-E(2,1));
coordMapped3 =(inputdata(3,:)-E(3,1));
coordMapped = [coordMapped1;coordMapped2;coordMapped3]';
New = coordMapped*b;
minExtent = min(coordMapped);
maxExtent = max(coordMapped);
%%半长度
half.x = (maxExtent(1,1)-minExtent(1,1))/2;
half.y = (maxExtent(1,2)-minExtent(1,2))/2;
half.z = (maxExtent(1,3)-minExtent(1,3))/2;
%%中心点
center = (maxExtent+minExtent)/2;
%offSet = (maxExtent-minExtent)/2+minExtent;
%%建立包围盒
weight = [half.x,half.y,half.z;half.x,-half.y,half.z;-half.x,half.y,half.z;-half.x,-half.y,half.z;half.x,half.y,-half.z;half.x,-half.y,-half.z;-half.x,half.y,-half.z;-half.x,-half.y,-half.z];
coord = center + weight;%*offSet';
coord = [coord;center];
这个哪部分写错了,求指教
1.png
(21.75 KB, 下载次数: 0)
2021-1-3 22:41 上传