2

我正在做光线追踪,我有一个在世界坐标中描述为矩阵的屏幕(我在屏幕坐标中的 X、Y、Z 之前有,通过使用变换和旋转我在世界坐标中得到了它)

Xw (NXM 矩阵) Yw (NXM 矩阵) Zw (我通过拟合 3D 数据 Xw 和 Yw 得到了这个多项式 (5 阶多项式)。我把它作为 f(Xw,Yw))

我也像往常一样描述了光线方程:

X = Ox + t*Dx
Y = Oy + t*Dy
Z = Oz + t*Dz  %(O is the origin point and D is the direction)

所以我所做的是我替换了多项式方程 f(Xw,Yw) 中的 X 和 Y 并将其求解为 t,这样我就可以得到交点。

但显然我使用的方法是错误的(我得到的交点在其他地方)。

任何人都可以帮助我并告诉我错误是什么。请支持我。

谢谢

这是代码的一部分:

X_World_coordinate_scr = ScreenXCoordinates.*Rotation_matrix_screen(1,1) +    ScreenYCoordinates.*Rotation_matrix_screen(1,2) + ScreenZCoordinates.*Rotation_matrix_screen(1,3) + Zerobase_scr(1);
Y_World_coordinate_scr = ScreenXCoordinates.*Rotation_matrix_screen(2,1) + ScreenYCoordinates.*Rotation_matrix_screen(2,2) + ScreenZCoordinates.*Rotation_matrix_screen(2,3) + Zerobase_scr(2);
Z_World_coordinate_scr = ScreenXCoordinates.*Rotation_matrix_screen(3,1) + ScreenYCoordinates.*Rotation_matrix_screen(3,2) + ScreenZCoordinates.*Rotation_matrix_screen(3,3) + Zerobase_scr(3); % converting the screen coordinates to the world coordinates using the rotation matrix and the translation vector

polymodel = polyfitn([X_World_coordinate_scr(:),Y_World_coordinate_scr(:)],Z_World_coordinate_scr(:),5); % using a function from the MAtlab file exchange and I trust this function. I tried it different data and it gives me the f(Xw,Yw).
ScreenPoly = polyn2sym(polymodel); % Function from Matlab file exchange to give the symbolic shape of the polynomial.



syms X Y Z t Dx Ox Dy Oy oz Dz z;

tsun = matlabFunction(LayerPoly, 'vars',[X,Y,Z]); % just to substitue the symboles from X , Y  and Z to (Ox+t*Dx) , (Oy+t*Dy) and (Oz+t*Dz) respectively 
Equation = tsun((Ox+t*Dx),(Oy+t*Dy),(Oz+t*Dz));


Answer = solve(Equation,t); % solving it for t but the equation that it is from the 5th order and the answer is RootOf(.... for z)
a = char(Answer); % preparing it to find the roots (Solutions of t)
R = strrep(a,'RootOf(','');
R1 = strrep(R,', z)','');
b = sym(R1);
PolyCoeffs = coeffs(b,z); % get the coefficient of the polynomail

tfun = matlabFunction(PolyCoeffs, 'vars',[Ox,Oy,oz,Dx,Dy,Dz]);


tCounter = zeros(length(Directions),1);
NaNIndices = find(isnan(Surface(:,1))==1); %I have NaN values and I am taking them out  
tCounter(NaNIndices) = NaN;

NotNaNIndices = find(isnan(Surface(:,1))==0);

 for i = NotNaNIndices' % for loop to calc

 OxNew = Surface(i,1);
 OyNew = Surface(i,2);
 OzNew = Surface(i,3);

 DxNew = Directions(i,1);
 DyNew = Directions(i,2);
 DzNew = Directions(i,3);

 P = tfun(OxNew,OyNew,OzNew ,DxNew,DyNew,DzNew);

 t = roots(P);
 t(imag(t) ~= 0) = []; % getting rid of the complex solutions
 tCounter(i) = t;
 end

请支持

提前致谢

4

0 回答 0