How I can define/ calculate reflection of the ray correctly? As my reflection makes mistake in direction
19 次查看(过去 30 天)
显示 更早的评论
Aknur
2023-3-9
Hello, everyone. Kinsly ask about advice or suggestion about how to find reflection of incidence ray and plot it
How I can make reflected ray in 3d indoor. I wrote function step by step but when it reflects it does not work properly. Maybe you can advice me function or documentation/source. I did everything with geometry. And tried then to solve using Rr = Ri - 2 N (Ri . N)
and my R become like 0.7 1.5 -157 which is not coreect.
The main problem that my ray does not reflect from the surface. I found intersection. Maybe something wrong with my directional vector and angles theta
Thank you in advance
19 个评论
William Rose
2023-3-9
编辑:William Rose
2023-3-19
[edit: Add text to clarify what represents, and corrct spelling mistakes.]
The equation you provided looks correct, for the direction of the ray. The equation you gave does not accont for the location of the ray. To account for the location of the reflected ray, you must specify a point that the incident ray passes through, and some point in the reflecting plane. can be anywhere in the reflecting plane; it does not need to be the intersection point. is needed to establish the location of the plane. The plane's orientation is given by its normal vector, . From these we can calculate where the incident ray intersects the plane; this point is also on the reflected ray.
Direction of reflected ray:
where is the normal to the plane.
= intersection of incident ray Ri with plane:
where
The the equation for the reflected ray is
where and are given above.
Aknur
2023-3-10
Hello @William Rose thank you for yor response. It is very helpful. I became lost cause I find direction vector and then intersection point. This intersection point will be like point where reflection will be. So for reflection equation I need to have a vector right. I am not sure if I can use my direction vector as a Ri?
Also I defined direction vector using my initial point which is source of light and two angles azimuth and polar.
William Rose
2023-3-10
@Aknur, you can specify the incident ray using the initial point and the direction vector , where you have computed using the two angles which you know. The equaiton for the incident ray is , where t is a scalar that varies from to . Then you can compute the intersection point where the incident ray intersects the plane, using the equation I gave before. That intersection point is a point on the reflected ray. Therefore you can specify the reflected ray by , where is given in the equaiton in my answer above.
Aknur
2023-3-12
Thank you, dear @William Rose you comment is very valuable. Yes, I did as you mentioned before to find intersection point. I was confused with my several variables, but now is working.
Thank you
William Rose
2023-3-19
编辑:William Rose
2023-3-19
@Aknur, is a point in the plane. Point can be anywhere in the plane. Point is needed to define the locaiton of the plane. I added text to my comment above, to explain the meaning of point .
The equation the direction vector, which you gave in your comment above, does not appear to be correct. I think that you have added two vectors to each other: the vector [X0;Y0;Z0] which specifies a location, and a direction vector with unit length, []. These vectors should not be added together.
[Edit: change comment about direciton vector]
The direction vector [] is correct, if the polar angle θ is measured from the z axis, and if the azimuth angle ϕ is measured in the X-Y plane, starting at the +X axis.
Aknur
2023-3-25
Dear, @William Rose thank you for your crystal clear explanation. It was so simple and easy for me to understand after your comments. Yes, I supposed that I had to write parametric equation and should add X0, Y0, Z0 values.
Kindly ask one more advice about azimuth angle. If I want also add azimuth Phi angle how I can set it for the next reflected ray. I mean for one case I use given Phi0 angle (I set it as Phi =90) but if I will make reflection than new Phi azimuth angle will be differenent for the reflected ray, and other reflections. For Theta I made it like Theta of reflected ray for the next ray
equal " - Theta0 "
Thank you in advance
William Rose
2023-3-26
Aknur, When a ray is reflected, both the azimuth, phi, and polar angle, theta, of the reflected ray are different from phi and theta for the incident ray. There is no simple formula for reflected phi and theta in terms of incident phi and theta. The formula for the reflected ray, given above, can be used to find phi and theta of the reflected ray. Thank you very much for your kind comments. I am traveling and I am not good at using my cell phone for Matlab Answers.
Aknur
2023-3-27
Dear, @William Rose thank you for you valuable advice. And have a nice travel! I will find a solution for angles
Aknur
2023-4-2
编辑:Aknur
2023-4-3
Dear, @William Rose kindly ask about your help or advice, because I have failed with my attemp to solve Phi angle of reflected ray. Consider the rule of angle of incidence equal to angle of reflection. If I have my angle Theta0, then I can calculate angle of incidence = 90 -Theta0, and this angle of incidence will be equal to angle of reflection. So then for the next ray angle of reflection I can calculate an angle of incidence for the next reflected ray. Then new angle of incidence will be equal to angle of reflection, and so on.
I set that new angle will be equal 90 - Theta0 in case of ray hit the plane which is number is odd, and equal to Theta0 in case if ray hit the plane which is number is even.
And I still have issue with my azimuth angle Phi. If you don't mind I will attach my code and main scrit through which I run it. Very appreciate if you will have time to look at my code.
William Rose
2023-4-2
I will look at your code if you add a lot of comment to it - to the main script and to the funciton. For the script, explain in the comments what you are trying to do and what the different variable are. Are you reflecting a ray off or a series of mirrors? Each mirror should be described by a normal vector and by a point that lies in the plane. Please make it clear in the code which variables are the normals to each plane and the points in each plane. Each ray should be described by a direction vector and by a point on the ray. Please make it clear in the code which variables are the directions of each ray and the points on each ray.
For the function, please explain the role of each input variable and each output variable in the comments. That is a good general practice.
You said "I have failed with my attemp to solve Phi angle of reflected ray." Did you mean to say "theta angle of reflected ray"? I ask this because you refer to theta and 90-theta when discussing angle of incidence.
You said " I still have issue with my azimuth angle Phi." Please explain what issue you have with the azimuth angle. Azimuth angle is measured with respect to some coordinate system. What coordinate system do you want to use when measuring azimuth?
The formula which I provided as my original answer gives the reflected ray direction and a point on the ray (the reflection point, where it insrersects the plane) in "absolute" coordinates, i.e. in terms of an absolute sets of x,y,z axes. The angle of incidence and angle of reflection, which are the subjects of the law of reflection, are measured with respect to the normal to the plane. Therefore it is not true that the angle incidence is the angle down from the z axis, unless the reflecting plane is parallel to the x-y plane.
Aknur
2023-4-3
编辑:Aknur
2023-4-4
Dear, @William Rose
Thank you for your response and help, very appreciate. I changed and add comments to my code and attached it to this comments.
Also Please find my answers for your questions
1) "Are you reflecting a ray off or a series of mirrors? Each mirror should be described by a normal vector and by a point that lies in the plane. Please make it clear in the code which variables are the normals to each plane and the points in each plane"
- I am reflecting inside of 3*3*3 dimension box from almost each plane which will be hit by ray. I calculated normal for all six plane in box and comment it in code 'n'
2) "Each ray should be described by a direction vector and by a point on the ray. Please make it clear in the code which variables are the directions of each ray and the points on each ray."
-XBar, YBar, ZBar are direction vector. And points on each ray will be incident ray.
3)"You said "I have failed with my attemp to solve Phi angle of reflected ray." Did you mean to say "theta angle of reflected ray"? I ask this because you refer to theta and 90-theta when discussing angle of incidence."
-I have two angle Theta0 and Phi0, and they are incidnet angle for the first ray. Then I read that Theta reflected = 180 - Theta incident (Theta0 in my case). And Phi reflected = 180 +Phi incident (Phi0 in my case). But when I tried these formulas I got strange result.
Then I tried to apply another formula geomtrically. For odd number of plane Theta reflected = 90- Theta0, and for even number of plane Theta reflected = Theta0
4) "You said " I still have issue with my azimuth angle Phi." Please explain what issue you have with the azimuth angle. Azimuth angle is measured with respect to some coordinate system. What coordinate system do you want to use when measuring azimuth?"
For azimuth Phi I use angle measured in the XY. When I rotate the box Phi will change also, but I dont know how it will change according to Phi0, which formula I have to use.
Difficulties for me that this box rotate and ray reflected from all planes, and I could exactly understand how two angles Theta and Phi will be changes.
I have attach editted code with comments. Please find it below
I used this sample function to find intersection point and then your formulas.
Thank you for your time and consideration
clc
clear all
close all
X0 = 1.5; %Start point, point of first ray starts
Y0 = 1.5;
Z0 = 3.0;
Theta0 = 30; %Angle measured from the Z axis
Phi0 = 90; %Angle measured in the X-Y plane
K = 5; %Number of Reflection
X = [X0 zeros(1,K)];
Y = [Y0 zeros(1,K)];
Z = [Z0 zeros(1,K)];
Theta = [Theta0 zeros(1,K)];
Phi = [Phi0 zeros(1,K)];
for ii = 1:K
ii
[XBar, YBar, ZBar, p, Xr, Yr, Zr, ThetaBar, PhiBar, Rr] = planeLocation7(X(ii), Y(ii), Z(ii),Theta(ii), Phi(ii));
X(ii+1) = Xr; %Xr, Yr, Zr point of intersection and point on reflected ray
Y(ii+1) = Yr; %Further this point will be considered as start point for the next ray
Z(ii+1) = Zr;
Theta(ii+1) = ThetaBar; %ThetaBar angle of reflection
Phi(ii+1) = PhiBar; %PhiBar angle of reflection in XY plane
end
fprintf('X(%d)=%f\n', ii, X(ii));
fprintf('Y(%d)=%f\n', ii, Y(ii));
fprintf('Z(%d)=%f\n', ii, Z(ii));
fprintf('Theta(%d)=%f\n', ii, Theta(ii));
fprintf('Phi(%d)=%f\n', ii, Phi(ii));
disp("===");
function [XBar, YBar, ZBar, p, Xr, Yr, Zr, ThetaBar, PhiBar, Rr] = planeLocation7(X0,Y0,Z0,Theta0, Phi0)
% Input
% X0, Y0, Z0 start point of intersection
% Theta0 Angle measured from the Z axis
% Phi0 Angle measured in the X-Y plane
XBar = sind(Theta0) * cosd(Phi0); %direction vector with unit length name them as XBar, YBar, ZBar
YBar = sind(Theta0) * sind(Phi0); %[sin (θ)cos(φ), sin (θ)sin(φ), cos(θ)]
ZBar = cosd(Theta0);
% Each plane of cube 3*3*3 with coordinates of four points of each plane,
% and fifth point is any point lies on the plane
planes(:,:,1) = [0 3 3; 0 0 3; 0 3 0; 0 0 0; 0 0 0];
planes(:,:,2) = [0 0 3; 3 0 3; 0 0 0; 3 0 0; 0 0 0];
planes(:,:,3) = [3 0 3; 3 3 3; 3 0 0; 3 3 0; 3 0 0];
planes(:,:,4) = [3 3 3; 0 3 3; 3 3 0; 0 3 0; 0 3 3];
planes(:,:,5) = [0 3 0; 3 3 0; 0 0 0; 3 0 0; 0 0 0];
planes(:,:,6) = [0 3 3; 3 3 3; 0 0 3; 3 0 3; 0 0 3];
location_plane = 6;
asd=[]; %create array for I-intersection point of each ray with one of the plane
jjj = []; % create array for 1 - 6 planes
% Checking planes 1-6 for intersection with incident ray Ri which
% was computed using two angles
for j=1:6 % j is number of plane
j
plane = planes(:,:,j);
p0 = plane(1,:); %p0 is top left point of plane
p1 = plane(2,:); %p1 is top right point of plane
p2 = plane(3,:); %p2 is bottom left point of plane
p3 = plane(4,:); %p3 is bottom right point of plane
V0 = plane(5,:); %point on the plane
% Pi is initial start point on the ray
Pi = [X0 Y0 Z0]; %initial start point
Ri = [XBar YBar ZBar]; %direction vector with unit length
A = p0-p2; %calculate A and B then
B = p0-p3; %then to calculate Normal of each plane
n=cross(A,B); % Normal for each Plane
n
w = V0-Pi; %V0 is point on plane
u = Ri;
N = dot(n,w);
D = dot(n,u);
sI = N/D; % t or slope of equation
%Equation of incident ray
I = Pi+sI.*u; % Pint intersection of incident ray Ri with plane
% Pint is point of the reflected ray
Pint = Pi + sI.*u;
fprintf('Pint: %f\n',Pint);
Rr = Ri - 2 * (dot(Ri, n)) * n;
Rr
%Equation of reflected ray - Ref_ray
Rray= Pint + sI.*Rr;
if any(isnan(I))== 0 || any(isinf(I))== 0
%any(I > 0 & I < 3, "all")
jjj = [jjj j];
asd = [asd; I];
end
%fprintf('I: %f\n',I);
%X=I(1);
%Y=I(2);
%Z=I(3);
%F = sqrt((X-X0)^2 + (Y-Y0)^2 + (Z-Z0)^2);
p = j;
fprintf('Rr: %f\n',Rr);
%E=I(1);
%F=I(2);
%G=I(3);
end
% PPP represent points of intersection of Ri incident ray with any of 6
% planes. asd=[ ] is array of intersection points, jjj = [] array of 6
% planes
PPP = [jjj' asd] %array without NaN
B = PPP(:,end-2:end) % New array of PPP without column, which disp. number planes
% Nr extarct row one where is X, Y, Z coordinates of intersection presented
% and then set crieria for them. As intersection should be inside of box
% 3*3*3
Nr = size(B,1); %rows in B
C = B(1:Nr,:) >= 0 & B(1:Nr,:) <= 3; %criteria that Intersected point should be in 3*3*3 dimension room
C
C = [ones(length(jjj),1), C] %keep column of N planes
D = PPP(all(C,2),:) % cleaned coordinates from PPP of intersection points which
% are inside of room accoridng to the dimension 3*3*3
% Because of start point X0, Y0, Z0 also inside of room dimension then D
% shows two points wwhich are valid for our condition. One of them is
% X0,Y0,Z0
for i = 1:size(D,1)
if (D(i,2) ~= X0 || D(i,3) ~= Y0 || D(i,4) ~= Z0) % using thiis condition we dont consider X0,Y0,Z0
disp(D(i,:))
A = D(i,:) %Final intersection point as vector
end
end
% Theta angle calculation consider Theta0. According to the rule Theta
% reflected = 180-Theta0. Also if we rotate the cube then Theta for odd plane =
% 90-Theta0 with 'minus' and in case if plane is even number than Theta =
% Theta0 with 'minus'.
plane = A(:,1)
if mod(plane,2) == 0 %odd
%ThetaBar = -(Theta0);
ThetaBar = -(90 - Theta0); %before
fprintf('ThetaBar odd: %f\n',ThetaBar);
PhiBar = Phi0;
%fprintf('ThetaBar even: %f\n',ThetaBar);
else
ThetaBar = -( Theta0); %even before
fprintf('ThetaBar even: %f\n',ThetaBar);
%ThetaBar = -(90 - Theta0); %odd
PhiBar = Phi0;
%fprintf('ThetaBar odd: %f\n',ThetaBar);
end
% Here code extarct number of plane to see for the next ray
tempPlane = A(:,1)
% And value of Xr, Yr, Zr will be considered as X0, Y0, Z0 for the next
% ray
% This code extract value of Xr, Yr, Zr from the vector A
Xr = A(:,2);
Yr = A(:,3);
Zr = A(:,4);
%PhiBar = Phi0; %edit it and add where ThetaBar calculated
fprintf('plane: %f\n',plane);
fprintf('Xr: %f\n',Xr);
fprintf('Yr: %f\n',Yr);
fprintf('Zr: %f\n',Zr);
plot3([X0 Xr], [Y0 Yr], [Z0 Zr])
% Below I put incident and reflected ray to this box
Room = [
3 3 0;
0 3 0;
0 3 3;
3 3 3;
0 0 3;
3 0 3;
3 0 0;
0 0 0];
sections_per_edge = 1;
weights = ((1:sections_per_edge-1) / sections_per_edge).';
edges = [];
points = [];
n = size(Room, 1);
for i = 1:n-1
pointA = Room(i, :);
for j = i+1:n
pointB = Room(j, :);
if nnz(pointA - pointB) == 1
edges = [edges; i, j];
points = [points; weights * pointA + (1 - weights) * pointB];
end
end
end
plot3(Room(:,1), Room(:,2), Room(:,3), '.b')
hold on
plot3(points(:,1), points(:,1), points(:,1), '.r', 'markersize', 10)
hold on
line([Room(edges(:,1), 1), Room(edges(:,2), 1)].', ...
[Room(edges(:,1), 2), Room(edges(:,2), 2)].', ...
[Room(edges(:,1), 3), Room(edges(:,2), 3)].', 'color', 'k')
plot3(1.5, 1.5, 3.0, '*', 'markersize', 15)
plot3(1.5, 0, 0, 'o', 'markersize', 7, 'MarkerFaceColor', 'black')
plot3(0, 1.5, 0, 'o', 'markersize', 7, 'MarkerFaceColor', 'black')
plot3(3, 1.5, 0,'o', 'markersize', 7, 'MarkerFaceColor', 'black')
plot3(1.5, 3, 0, 'o', 'markersize', 7, 'MarkerFaceColor', 'black')
plot3([X0 Xr], [Y0 Yr], [Z0 Zr])
end
William Rose
2023-4-5
Thank you for your updated code with comments added.
I am not able to follow the logic of this code, and I cannot determine why you are computing thisnhgs the way you are. YOur orignal quesiton was how to compute the formula for a ray reflected by a plane. My earlier answer did not inclide azimuth or angle. You are interested in azimuth and angle and I am not sure why. I don;t understand why they are relevant for your problem.
Therefore I wrote a script that computes N rays (i.e. N- reflections) from M planes, to demonstrate that you do not need azimuth or angle to answer your quesiton. My code also illustrates that there are additional issues you must address when reflecting from multiple planes: 1. You must determine which plane a ray will strike first, because a given ray may intersect more than one plane. 2. You must propagate the ray forward, and not backward, so that intersections with planes behind the ray are not mistakenly identified as the first reflecting plane. (A plane behind could be closer, but if it is behind then we do not want to reflect off of it.) 3. If the planes do not form a bounding box, then it is possible that the ray esscapes from the planes before N-1 reflections have occurred. The code must deal with this possibility correctly.
You do not need azimuth or angle to do the calculations above. The inclusion of azimuth or angle leads to confusion, in my opinion.
My code, attached, deals with the above issues successfully. By adding and removing comments, you can demonstrate that is gives the expected answers. In al three examples, the ray begins at point (1,1,1). In all three examples, there are three planes passing through the origin: plane 1=Y-Z plane though x=0, plane 2=X-Z plane through y=0, plane 3=X-Y plane through z=0. In exaple 1 and example 2, there is also plane 4=X-Y plane thorugh z=+3. The normal vector of plane 1 is (0,0,1); the normal vector of plane 4 is (0,0,-1).
Example 1: 4 planes, initial ray direction=(-.8, 0, -.6).
Example 2: 4 planes, initial ray direction = (-.8, -.6, 0).
Example 3: 3 planes, initial ray direction = (-2/7,-3/7, -6/7).
William Rose
2023-4-6
@Aknur, I added a section of code to compute and display the altitude and azimuth of each ray, since you are interested in that information. See attached. With the attached, code, the console output, for example 1, is shown below. I this example, there are 4 reflecting planes, and 6 rays are computed. The initial ray direciton is (-.8,0,-.6). Therefore the intial ray has no y-component to its direction. Therefore the initial ray is paralell to the X-Z plane. Therefore the intial plane and all of the reflected rays never intersects plane 2, which is the X-Z plane through y=0.
>> rayReflection
Ray 1 reflects off plane 1 to make ray 2.
Ray 2 reflects off plane 3 to make ray 3.
Ray 3 reflects off plane 4 to make ray 4.
Ray 4 reflects off plane 3 to make ray 5.
Ray 5 reflects off plane 4 to make ray 6.
Ray Altitude Azimuth
___ ________ _______
1 -36.87 180
2 -36.87 0
3 36.87 0
4 -36.87 0
5 36.87 0
6 -36.87 0
Aknur
2023-4-10
编辑:Aknur
2023-4-10
Hello, dear @William Rose thank you so much for your huge help and for your time you have spend for clarifiaction. I understand your logic and steps in your code. it is amazing and very nice code. I think I will still ahve question regarding your code cause I am still research it
I suppose my code seems mess because I want to rotate box with ray inside that is why Phi angle is important for me.
William Rose
2023-4-11
I do not understand why phi becomes important if the box is rotating. If I were simulating reflections inside a rotating box, I would not use phi to compute reflections. I would continue to compute reflections with code similar to the code I provided. The difference would be that the reflection computation would be inside a loop that corresponds to time steps, and the orientation vectors of the reflecting planes would be functions of time.
Aknur
2023-4-11
Because I set the direction vector using two angles theta and phi and reflected angles of theta and phi will set a new direction vector for the next ray. I hope I was able to explain. However, there is also an option to do without any changes in angle phi, but I have to see how it will go with changing angle phi. Because when the plane changes then the angle phi also has to be changed as the plane is different from the incident plane.
采纳的回答
William Rose
2023-3-9
I meant the comment above to be an answer; please consider it to be one. t is a scalar, t>=0 so that the reflected ray is only on one side of the plane.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Propagation and Channel Models 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)