我有一个由3x 3D顶点表示的3D三角形。
我正在寻找一种必须满足以下要求的算法:
例如:
答案 0 :(得分:2)
定义基础向量 b = AB , c = AC 和 n = b x c (矢量积),其中A,B,C - 三角形顶点的坐标
在此基础上表示点P坐标,求解linear equation system(未知t,u,v)。 Gaussian elimination method适用于此。
t * b.X + u * c.X + v * n.X = P.X
t * b.Y + u * c.Y + v * n.Y = P.Y
t * b.Z + u * c.Z + v * n.Z = P.Z
点在'里面'根据你的照片,如果
0 <= t <= 1
0 <= u <= 1
and
t + u <= 1
答案 1 :(得分:1)
假设 ABC 是您的三角形,要知道某个点是否与三角形ABC在同一平面上,我们可以使用cross和dot乘积。如果将 P 指向同一平面,则
(P-A).( (B-A)x(C-A) ) = 0
here [.] is dot product and [x] is cross product.
A, B, C are co ordinates of vertex of the triangle
对于(2),最简单的方法是使用Barycentric coordinate来知道一个点是否位于三角形的内部或边界上。三角形内的任何点P都可以表示为
P = a*A + b*B + c*C where 0 <= a, b, c <= 1 and a+b+c = 1
(on boundary if at least one of a,b,c is zero)
Now, we can write, a = 1 - b - c.
P = (1-b-c)*A + b*B + c*C
=> P-A = b*(B-A) + c*(C-A)
假设, X = P-A, Y = B-A, Z = C-A。然后等式变为
X = b*Y + c*Z
taking dot product with Y and Z, we get
X.Y = b*(Y.Y) + c*(Z.Y)
X.Z = b*(Y.Z) + c*(Z.Z)
define x1 = X.Y, y1 = Y.Y, z1 = Z.Y,
x2 = X.Z, y2 = Y.Z, z2 = Z.Z
现在我们必须用两个未知数来求解下面的线性方程。
x1 = b*y1 + c*z1
x2 = b*y2 + c*z2
求解这两个方程,
b = (x1*z2 - x2*z1)/(y1*z2-y2*z1)
c = (-x1*y2 + x2*y1)/(y1*z2-y2*z1)
a = 1 - b - c
Then we can easily check if a,b,c satisfies the condition.
(actually checking 0 <= b,c <= 1 and b+c <= 1 is enough)