我有2套不同的3D点,通过它们我可以拟合平面和表面。用Matlab的plot()
函数可视化时,这两个表面相交。但是,当我尝试使用拟合函数生成这些相同的图时,我发现它们没有相交。
我使用Matlab的contours()
和interp2()
函数,尝试从拟合函数中提取相交线。但是,由于两个曲面与从meshgrid()
函数获得的生成的曲面图没有相交,我根本没有任何结果。
clc; clear; close all;
Xs = [18.5, 18, 17.5
18.5, 18, 16.5
19.5, 18, 17.5
19.5, 18, 16.5
17.5, 18, 18.5
16.5, 18, 18.5];
Xn = [18.5, 18.5, 18
18.5, 19.5, 18
19.5, 18.5, 18
19.5, 19, 17.5
19.5, 19.5, 17
18, 18.5, 18.5
18, 19.5, 18.5];
% fit plane
[sfp,gofp,obj] = fit([Xs(:,1), Xs(:,3)],Xs(:,2), 'poly11');
plot(sfp,[Xs(:,1), Xs(:,3)],Xs(:,2))
hold on;
% fit surface
[sfs,gofs,obj] = fit([Xn(:,1), Xn(:,3)],Xn(:,2), 'poly22');
plot(sfs,[Xn(:,1), Xn(:,3)],Xn(:,2))
% generate grid
[x, z] = meshgrid(linspace(-20, 20));
% expressions for both surfaces as derived from curve fitting
Yp = sfp.p10*x + sfp.p01*z + sfp.p00;
Ys = sfs.p20*(x^2) + sfs.p02*(z^2) + sfs.p10*x + sfs.p01*z + sfs.p11*x*z + sfs.p00
% Plots the surfaces from the fitted plane equations
surface(x, y, yp, 'FaceColor', [0.5 1.0 0.5], 'EdgeColor', 'none');
surface(x, y, ys, 'FaceColor', [1.0 0.5 0.0], 'EdgeColor', 'none');
view(3); camlight; axis vis3d
% Obtain the eq. of intersection
ydiff = ys - yp;
C = contours(x, z, ydiff, [0 0]);
% Extract the x- and y-coords from the contour matrix.
xcoord = C(1, 2:end);
zcoord = C(2, 2:end);
% Interpolate on the first surface to find z-coords for the intersection
% line.
ycoord = interp2(x, z, yp, xcoord, zcoord);
% Visualize the line.
line(xcoord, ycoord, zcoord, 'Color', 'b', 'LineWidth', 5);
我不知道预期的结果是什么,但是与contours()
的结果相反,我确定表面相交,因此我希望得到一些结果。