Intersection of a 2D polygon in 3D and a straight line

6 次查看(过去 30 天)
I have a polygon determined by 5 points in 3D. I want to see if the line, say X-axis interescts with this polygon. Also, I am interested in the intersection point.
For instance, the polygon is determined by the following points that are coplanar:
V1 = [11.2908113470153 -8.47779952423983 8.18414177879808]
V2 = [12.3366023247650 -8.58127758366331 8.28004384038752]
V3 = [1.2757044711089 -8.47363504164875 8.18612938262431]
V4 = [13.3363266184718 -7.53719741280684 9.81580879134791]
V5 = [15.0742258082624 -7.67475550912392 10.0186434441811].
The end points of the line segment along the X-axis are [-30 0 0] and [30 0 0].

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2020-9-28
OK, you'll get a bit of pseudo-code/algebra, that solves most of the problem. Lets write the equation for the plane as:
that is all points in 3-D r who's scalar product with the unit-vector, normal to the plane, is l. We then write the equation for the line:
This gives us the equation for the line-plane intersection:
Which we can solve for s at the intersection:
giving us the intersection-point:
So that makes the first step.
Next step I'd take would be to make a 2-D coordinate system in the plane, any random origin-point will do (one of your polygon-points seems convenient), then a unit-vector towards one of its neighbors will make a good direction, and an arbitrary direction perpendicular to that. Now you have an origin and 2 basis-vectors for your plane and can convert your 5+1 (minus the origin-point...) to their corresponding points in this plane. That completes the second step.
Third step is to use the matlab-function inpolygon - which gives you the in/out-answer.
I leave the 2 last steps for you to do, it shold be rather straightforward, and makes for a nice geometric function, you can keep forever.
HTH
  4 个评论
LC
LC 2020-10-1
编辑:LC 2020-10-1
Ok. So I tried your suggestion. I get the error at the last line of the code. I am unsure where things went wrong. I am showing the small code that I wrote here.
clc
clear
L = 30;
r1 = - [L 0 0]; % First endpoint of the line segment
r0 = [L 0 0]; % Second endpoint of the line segment
v = [-1,-1,1;-1,-1,-1;-1,1,-1;-1,1,1]; % Let us take a facet of cuboid for simplicity
vert1 = v(1,:); % Pick the first vertex to calculate the normal vector to the facet
pol_ang = atan2(v(:,1),v(:,2));
vert_ang = [v pol_ang];
vert_sort = sortrows(vert_ang,4); % Sorting vertices of facet anticlockwise
vert_sort(:,4) = [];
%facet normal vector
norm_vec = cross(vert_sort(2,:) - vert_sort(1,:), vert_sort(length(vert_sort),:)- vert_sort(1,:));
e_n = norm_vec/norm(norm_vec);
ll = dot(v(1,:),e_n);
e_l = (r1-r0)/norm(r1-r0);
r_l = r0 + (ll - dot(r0,e_n))/(dot(e_l, e_n))*e_l;
e_1 = (v(2,:) - v(1,:))/(norm(v(2,:) - v(1,:)));
e_2 = cross(e_n,e_1);
M = [e_1;e_2;e_n];
v_new= dot(M,(v-r0));
Bjorn Gustavsson
Bjorn Gustavsson 2020-10-2
OK, you're almost there.
1, I goofed with the notation. I happened to use twice. In the first answer it denotes the first point on your line. In the second I unfortunately and reclessly used it to denote one of the vertex-points in your plane.
That means you should change your last line to:
v_new= M*(v-v(1,:))';
This means we set the new origin to the first vertex. Here you should also note that the multiplication between your vertices and the rotation-matrix is a vector-matrix multiplication (or 4 of them). (This is a mathematical notation overload, that actually doesn't cause confusion - once you get used to it...) To do the vectors-matrix multiplication in one swoop you have to have the coordinates in the column-direction (this causes me frustration, because my prefered orientation of vectors like this is the one you use, there will be additional transpose-operations to pay for this choise). So that gives you the new 3-D coordinates of the vertices. You have to do the same transformation for your point-projection:
r_lnew= M*(r_l-v(1,:))';
This should set you up nicely for using inpolygon:
inpolygon(r_lnew(1),r_lnew(2),v_new(1,:),v_new(2,:))
Another advice for geometry-problems like this is to plot things along the way - I find that this helps me "anchor my thinking" so I don't spin out of order...
HTH

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by