✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,代码获取、论文复现及科研仿真合作可私信。
🍎个人主页:Matlab科研工作室
🍊个人信条:格物致知。
更多Matlab完整代码及仿真定制内容点击👇
🔥 内容介绍
光线追踪是一种计算机图形技术,它模拟光线在场景中的传播,以生成逼真的图像。它通过计算光线从光源发出,与场景中的物体交互,最终到达观察者的路径来实现。
龙格-库塔法
龙格-库塔法是一种求解常微分方程的数值方法。它通过使用一系列中间步骤来逼近方程的解,从而提高了计算的精度和效率。
光线追踪中的龙格-库塔法
在光线追踪中,光线路径可以用常微分方程来表示:
dx/dt = v(x, t)
其中:
-
x 是光线的位置
-
t 是时间
-
v 是光线的速度
使用龙格-库塔法,我们可以将该方程离散化为一系列步骤:
k1 = v(x, t)
k2 = v(x + h * k1 / 2, t + h / 2)
k3 = v(x + h * k2 / 2, t + h / 2)
k4 = v(x + h * k3, t + h)
x = x + h * (k1 + 2 * k2 + 2 * k3 + k4) / 6
其中:
-
h 是时间步长
实现
我们可以使用以下步骤来实现基于龙格-库塔法的【光线追踪】:
-
初始化光线位置和方向
-
循环迭代,直到光线到达场景中的某个物体或超过最大步数
-
在每个迭代中,使用龙格-库塔法更新光线位置
-
检查光线是否与场景中的任何物体相交
-
如果相交,计算光线与物体的交互,并根据材料属性更新光线方向
-
重复步骤 3-5,直到光线到达观察者或超过最大步数
优点
使用龙格-库塔法进行【光线追踪】具有以下优点:
-
**高精度:**龙格-库塔法是一种高阶方法,可以提供比低阶方法更高的精度。
-
**稳定性:**龙格-库塔法是一种稳定的方法,这意味着即使时间步长较大,它也能产生合理的解。
-
**效率:**龙格-库塔法是一种高效的方法,因为它只需要少量中间步骤即可获得高精度。
局限性
龙格-库塔法也有一些局限性,包括:
-
**计算成本:**龙格-库塔法需要比低阶方法更多的计算量。
-
**自适应步长:**龙格-库塔法不能自动调整时间步长,因此可能需要手动调整以获得最佳性能。
结论
基于龙格-库塔法的【光线追踪】是一种功能强大且准确的技术,可用于生成逼真的图像。它的高精度、稳定性和效率使其成为各种图形应用程序的理想选择。然而,需要考虑其计算成本和自适应步长的局限性。
📣 部分代码
clear;
TIR = 0;
in = [1, 0, 1]; %Direction vector for incident ray
ix = [0; 0; 0]; %Position vector for ray on first plane
n = [0, 0, -1]; %Unit normal of first plane
ray_energy = 1; %Preset energy of the incident ray
%The medium variable "med_var" keeps track of which medium the ray is
%currently travelling through. When med_var = 1, the ray is currently in
%medium 1 whereas when med_var = -1 the ray is currently travelling in
%medium 2.
med_var = 1;
%The direction variable "dir_var" stores the z-direction in which the ray
%is travelling. When dir_var = 1, the ray being followed is travelling in
%the positive z-direction otherwise it is travelling in the negative
%z-direction.
dir_var = 1;
%The lambda parameter controls the slope of the exponential
%distribution from which the distances between successive interfaces
%are randomly chosen
lambda = 1;
r_z = [0, 0, 1]; %General ray along the z-axis
%Refractive indices of the two media
n1 = 1;
n2 = 1.1;
%Calculating the critical angle
theta_c = asind(n1/n2);
%Normalising the incident ray direction vector
in_unit = in/norm(in);
i = 0;
while ix(3)>=0
i = i+1;
ix_m(i,:) = ix;
if i == 1
r_z = in_unit;
else
r_z = [0,0,dir_var];
end
th_m1 = acosd(dot(r_z, -sign(r_z(3))*n)); %Calculating the incident angle
%Outer if statement checks which media the refracted ray is travelling
%through wih each iteration.
%The inner if statements check for total internal reflection by checking
%whether the refracted ray returned is complex.
%NB: Total internal reflection can only occur when the light ray is
%travelling from a more dense medium to a less dense medium
rec_v = 0;
if med_var == -1
rf = refract(r_z, sign(r_z(3))*n, n2, n1);
if(isreal(rf) == 0 && th_m1>theta_c)
rf = [0,0,0];
TIR = i;
rec_v = 1;
end
else
rf = refract(r_z, sign(r_z(3))*n, n1, n2);
end
rl = reflect(r_z, sign(r_z(3))*n);
if rec_v == 1
th_m2 = -acosd(dot(rf,-sign(rf(3))*n));
else
th_m2 = acosd(dot(rf,-sign(rf(3))*n)); %Calculating the refraction angle
end
%Calculating the reflection coefficient at each interface
rs = ((n1*cosd(th_m1) - n2*cosd(th_m2))/(n1*cosd(th_m1) + n2*cosd(th_m2)))^2;
rp = ((n1*cosd(th_m2) - n2*cosd(th_m1))/(n1*cosd(th_m2) + n2*cosd(th_m1)))^2;
R_coef = (rp+rs)/2;
ray_energy = ray_energy*R_coef;
%Randomising the distance between successive interfaces over an exponential
%distribution
sy = rand(1);
s = (-log(sy))/lambda;
if i == 1
%Calculating the rotation matrix
R_mat = vecRotMat(rf,r_z);
ix = ix + s*rf';
rf_m(1,:) = rf;
med_var = -med_var;
else
%By comparing a randomly generated number to the reflection coefficent at
%each interface a decision is made as to whether the reflected or the
%refracted ray willl be followed.
if ((rand(1) <= R_coef) || (rec_v == 1))
rl_r = R_mat'*rl';
rl = rl_r';
%Using the reflected vector
ix = ix + s*rl';
R_mat = vecRotMat(rl,r_z);
dir_var = sign(rl(3));
else
med_var = -med_var;
rf_r = R_mat'*rf';
rf = rf_r';
%Using the refracted vector
ix = ix + s*rf';
R_mat = vecRotMat(rf,r_z);
dir_var = sign(rf(3));
end
end
%Using the George Marsaglia method to generate random normal vectors
%distributed evenly over the surface of a half-sphere
S_n = 2;
while S_n>1
n_3 = -1 + rand(1,1);
n_1 = -1 + 2*rand(1,1);
S_n = n_3^2 + n_1^2;
n(2) = 1 - 2*S_n;
end
n(3) = (2*n_3)*sqrt((1-S_n));
n(1) = (2*n_1)*sqrt((1-S_n));
n(2) = 1 - 2*S_n;
end
str3 = sprintf('The ray exited the material after %d interfaces', i);
disp(str3);
ix_m(i+1,:) = [0,0,0];
ix_m2 = zeros(i+1,3);
%NB: ix_m2 is a matrix which stores the coordinates at which the light rays
%intercept each plane. The columns have been re-ordered to comply with the
%altered optic coordinate system.
ix_m2(:,1) = ix_m(:,3);
ix_m2(:,2) = ix_m(:,1);
ix_m2(:,3) = ix_m(:,2);
ix2(1) = ix(3);
ix2(2) = ix(1);
ix2(3) = ix(2);
%Outer if statement checks whether TIR has occurred at interfcae 2 in which
%case no plot is generated
if any(ix_m(2,:))
figure();
for k = 1:i
if any(ix_m2(k+1,:))
%Displaying each of the rays between interfaces
vectarrow(ix_m2(k,:),ix_m2(k+1,:));
hold on;
else
hold on;
vectarrow(ix_m2(k,:),ix2);
end
end
hold on;
scatter3(0,0,0,'filled','g');
hold on;
scatter3(ix(3),ix(1),ix(2),'filled','r');
xlabel('z')
ylabel('x')
zlabel('y')
end
%Printing all the data to a text file
fid = fopen('R13Data.txt','w');
fprintf(fid,'Data for test run of Ray_Trace12\r\n\r\nFor this run the ray exited the material after %d interfaces\r\n\r\n',i);
fprintf(fid,'Preset Parameters:\r\n\r\n');
fprintf(fid,'n1 = %.2f\n n2 = %.2f\r\n',n1,n2);
fprintf(fid, 'lambda = %.2f\r\n\r\n',lambda);
fprintf(fid, 'Intercept Points:\r\n\r\nix_m =\r\n\r\n');
for j = 1:(i)
fprintf(fid, '%.5f %.5f %.5f\r\n',ix_m(j,:));
end
fclose(fid);
⛳️ 运行结果
🔗 参考文献
🎈 部分理论引用网络文献,若有侵权联系博主删除
🎁 关注我领取海量matlab电子书和数学建模资料
👇 私信完整代码和数据获取及论文数模仿真定制
1 各类智能优化算法改进及应用
生产调度、经济调度、装配线调度、充电优化、车间调度、发车优化、水库调度、三维装箱、物流选址、货位优化、公交排班优化、充电桩布局优化、车间布局优化、集装箱船配载优化、水泵组合优化、解医疗资源分配优化、设施布局优化、可视域基站和无人机选址优化、背包问题、 风电场布局、时隙分配优化、 最佳分布式发电单元分配、多阶段管道维修、 工厂-中心-需求点三级选址问题、 应急生活物质配送中心选址、 基站选址、 道路灯柱布置、 枢纽节点部署、 输电线路台风监测装置、 集装箱船配载优化、 机组优化、 投资优化组合、云服务器组合优化、 天线线性阵列分布优化
2 机器学习和深度学习方面
2.1 bp时序、回归预测和分类
2.2 ENS声神经网络时序、回归预测和分类
2.3 SVM/CNN-SVM/LSSVM/RVM支持向量机系列时序、回归预测和分类
2.4 CNN/TCN卷积神经网络系列时序、回归预测和分类
2.5 ELM/KELM/RELM/DELM极限学习机系列时序、回归预测和分类
2.6 GRU/Bi-GRU/CNN-GRU/CNN-BiGRU门控神经网络时序、回归预测和分类
2.7 ELMAN递归神经网络时序、回归\预测和分类
2.8 LSTM/BiLSTM/CNN-LSTM/CNN-BiLSTM/长短记忆神经网络系列时序、回归预测和分类
2.9 RBF径向基神经网络时序、回归预测和分类