射线平方相交3D [英] Ray-square intersection 3D

查看:129
本文介绍了射线平方相交3D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何计算射线平面相交,但是如何测试相交点是否在该平面上的正方形内?我正在测试该点是否在距正方形中心的距离之内,但是我不确定这是否正确.

I know how to compute a ray-plane intersection, but how can I test if the intersection point is within a square on that plane? I was testing if the point was within a distance from the center of the square but I'm not sure if this is correct.

推荐答案

以下是适用于任何凸多边形的方法: (请参见答案末尾的正方形简化版)

Here is a method that works for any convex polygon: (see simplified version for squares at the end of answer)

让p1,p2,p3,p4表示正方形的四个顶点,让q表示射线与支撑平面之间的交点.令n表示 一个垂直于支撑平面的矢量(例如取叉积(p2-p1)x(p3-p1)).

Let p1,p2,p3,p4 denote the four vertices of your square and let q denote the intersection between the ray and the supporting plane. Let n denote a vector normal to the supporting plane (take for instance the cross-product (p2-p1) x (p3-p1)).

要确定q是否在平方中,请计算以下四个量:

To determine whether q is in the square, compute the following four quantities:

o1=orient(q,p1,p2,n)
o2=orient(q,p2,p3,n)
o3=orient(q,p3,p4,n)
o4=orient(q,p4,p1,n)

其中

orient(a,b,c,n) =  [(b-a) x (c-a)] . n
x: cross product; .: dot product

如果o1,o2,o3和o4都具有相同的符号,则q在平方中(p1,p2,p3,p4)

If o1,o2,o3 and o4 have all the same sign, then q is in the square (p1,p2,p3,p4)

它也适用于任何凸多边形(p1,p2,p3,p4,...,pn)

It also works for any convex polygon (p1,p2,p3,p4,...,pn)

工作方式:

如果您使用的是2D模式,则可以进行以下计算:

If you were in 2D, you would compute:

o1 = det(p1-q, p2-q)
o2 = det(p2-q, p3-q)
o3 = det(p3-q, p4-q)
o4 = det(p4-q, p1-q)

其中det(v1,v2) = (x1*y2)-(x2*y1)表示两个向量之间的行列式.

where det(v1,v2) = (x1*y2)-(x2*y1) denotes the determinant between two vectors.

在英语中,如果o1,o2,o3,o4具有相同的符号(例如正),则表示角度(p1,q,p2)呈左转".如果所有角度(p1,q,p2),(p2,q,p3),(p3,q,p4)和(p4,q,p1)都左转,则q在多边形内部.每当它在外面时,都会有一个侧面(pi,pj),使得(pi,q,pj)可以右转".

In English, if o1,o2,o3,o4 have the same sign, for instance positive, this means that the angle (p1,q,p2) makes a "left turn". If all angles (p1,q,p2), (p2,q,p3), (p3,q,p4) and (p4,q,p1) are left turns, then q is inside the polygon. Whenever it is outside, there is a side (pi,pj) such that (pi,q,pj) makes a "right turn".

现在,如果我们处于3D的任意平面中,则不再存在左转"和右转"之类的东西,但是我们可以引入法线向量n并测试是否(q-p1,q -p2,n)是正向或负向3D基础(这是orient()计算的).

Now if we are in an arbitrary plane in 3D, there is no longer such a thing as "left turn" and "right turn", but we can introduce the normal vector n, and test whether (q-p1, q-p2, n) is a positively or negatively oriented 3D basis (this is what orient() computes).

正方形的特殊情况

计算

X = (q-p1).(p2-p1) / ||(p2-p1)||
Y = (q-p1).(p3-p1) / ||(p3-p1)||

如果X >= 0 && X <= 1 && Y >= 0 && Y <= 1,则q在平方中(请参阅Mbo的答案).

If X >= 0 && X <= 1 && Y >= 0 && Y <= 1 then q is in the square (see Mbo's answer).

这篇关于射线平方相交3D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆