Finding intersection between two 3D lines
22 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to find whether there is an intersection (point) between 2 lines in 3D.
For a more extensive explanation: I made a (PDE) mesh and now, with my code, I am able to plot a line between the origin and every node of my mesh (line1 every loop). And also I made a more simple line (line2, blue) which stays at the same height.
Now I want to know whether line1 and line2 intersect. Because when line1 intersect with line2, the algoritm is not allowed to plot it (this I want to add with an 'if' statement when I know how to find out whether they intersect.
In the image you see the result I now got.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029650/image.png)
Besides, I would really like to plot my mesh and lines in the same plot, but for now I couldn't found out how. So if you know also a solution for that.
Thanks a lot!
(I also added my STL file from which I made the mesh)![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029660/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029660/image.png)
model = createpde(1);
importGeometry(model,'Bovenste GS.stl');
mesh = generateMesh(model, 'Hmax',5);
% pdeplot3D(mesh)
for i = 1:length(mesh.Nodes)
A = mesh.Nodes(:,i);
B = [0,0,0];
C = [-150,-150,0];
% sqrt(sum((A - B) .^ 2))
v=[transpose(A);B];
h=[C;B];
line1 = plot3(v(:,1),v(:,2),v(:,3),'r');
hold on;
end
line2 = plot3(h(:,1),h(:,2),h(:,3),'b');
hold off;
2 个评论
回答(1 个)
Bjorn Gustavsson
2022-6-12
You can do this rather automatic with geometry and linear algebra if you write your the positions
and
along lines as:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029685/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029690/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029695/image.png)
If
and
are equal we can subtract the two equations to get:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029700/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029705/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029710/image.png)
for some lengths
and
away from the respective "reference points". This gives us 3 equations (the x, y and z components) in 2 unknowns (
and
):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029715/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029720/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029725/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029730/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1029735/image.png)
This we can solve rather easily in matlab:
function [does_intersect,r_intersect] lines_intersect(r10,e1,r20,e2,tol_intersect)
if nargin < 5
tol_intersect = 1e-6; % adjust to suit your preferences
end
l1l2 = [e1,e2]\(r20-r10); % lengths along lines
r1 = r10 + l1l2(1)*e1;
r2 = r20 + l1l2(2)*e2;
dr = r1-r2;
if norm(dr) < tol_intersect
does_intersect = 1;
r_intersect = (r1 + r2)/2;
else
does_intersect = 0;
r_intersect = [];
end
Here the inputs should be 3-by-1 column arrays.
HTH
2 个评论
Bjorn Gustavsson
2022-6-13
My plan was to have dr be the array between the closest points on the two lines (you better check this function because I very much hoofed this function up at the prompt). Therefore checking that the norm of dr is smaller than tol_intersect will make the function return a does_intersect for all lines that are closer than than that tol_intersect - just because of numerical accuracy we typically cannot rely on all points that "physically" or mathematically do intersect will actually intersect perfectly "numerically" with finite precision arithmetic.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Geometry and Mesh 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!