-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplane_line_intersect.m
58 lines (54 loc) · 1.88 KB
/
plane_line_intersect.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function [I,check]=plane_line_intersect(n,V0,P0,P1)
%plane_line_intersect computes the intersection of a plane and a segment(or
%a straight line)
% Inputs:
% n: normal vector of the Plane
% V0: any point that belongs to the Plane
% P0: end point 1 of the segment P0P1
% P1: end point 2 of the segment P0P1
%
%Outputs:
% I is the point of interection
% Check is an indicator:
% 0 => disjoint (no intersection)
% 1 => the plane intersects P0P1 in the unique point I
% 2 => the segment lies in the plane
% 3=>the intersection lies outside the segment P0P1
%
% Example:
% Determine the intersection of following the plane x+y+z+3=0 with the segment P0P1:
% The plane is represented by the normal vector n=[1 1 1]
% and an arbitrary point that lies on the plane, ex: V0=[1 1 -5]
% The segment is represented by the following two points
% P0=[-5 1 -1]
%P1=[1 2 3]
% [I,check]=plane_line_intersect([1 1 1],[1 1 -5],[-5 1 -1],[1 2 3]);
%This function is written by :
% Nassim Khaled
% Wayne State University
% Research Assistant and Phd candidate
%If you have any comments or face any problems, please feel free to leave
%your comments and i will try to reply to you as fast as possible.
I=[0 0 0];
u = P1-P0;
w = P0 - V0;
D = dot(n,u);
N = -dot(n,w);
check=0;
if abs(D) < 10^-7 % The segment is parallel to plane
if N == 0 % The segment lies in plane
check=2;
return
else
check=0; %no intersection
return
end
end
%compute the intersection parameter
sI = N / D;
I = P0+ sI.*u;
if (sI < 0 || sI > 1)
check= 3; %The intersection point lies outside the segment, so there is no intersection
else
check=1;
end