参考[1]的话比我看得其他几篇文章更加的清晰,所以可以直接看它就可以了。
方法一:Same Side
代码实现
参考[1],用的是Eigen库
static bool SameSide(const Vector3f& p1, const Vector3f& p2, const Vector3f& a, const Vector3f& b)
{
Vector3f ab = b - a;
Vector3f ap1 = p1 - a;
Vector3f cp1 = ab.cross(ap1);
Vector3f ap2 = p2 - a;
Vector3f cp2 = ab.cross(ap2);
if (cp1.dot(cp2) >= 0)
return true;
else
return false;
}
static bool IsInsideTriangle(Vector3f& p, const Vector3f* _v)
{
if (SameSide(p, _v[0], _v[1], _v[2]) && SameSide(p,_v[1], _v[0], _v[2]) && SameSide(p, _v[2], _v[0], _v[1]))
return true;
else
return false;
}
方法二:重心坐标法
参考[1],比方法一更加高效。简单测了一下,方法二比方法一快了一倍!!!
So remember that the three points of the triangle define a plane in space. Pick one of the points and we can consider all other locations on the plane as relative to that point. Let's go with A -- it'll be our origin on the plane. Now what we need are basis vectors so we can give coordinate values to all the locations on the plane. We'll pick the two edges of the triangle that touch A, (C - A) and (B - A). Now we can get to any point on the plane just by starting at A and walking some distance along (C - A) and then from there walking some more in the direction (B - A).
With that in mind we can now describe any point on the plane as
P = A + u * (C - A) + v * (B - A)
Notice now that if u or v < 0 then we've walked in the wrong direction and must be outside the triangle. Also if u or v > 1 then we've walked too far in a direction and are outside the triangle. Finally if u + v > 1 then we've crossed the edge BC again leaving the triangle.

(图1)
重心坐标
看上面的描述,那么任意点$P$可以用下面这个表达式描述:

本文介绍了两种判断三维空间中点是否在三角形内的方法:SameSide代码实现和重心坐标法。 SameSide方法基于向量叉乘判断点的位置,而重心坐标法利用坐标系和向量比例来高效判断。文中提供了详细的代码实现,并指出重心坐标法在效率上优于SameSide方法。同时,解释了如何通过重心坐标计算公式来确定点在三角形内的条件。
最低0.47元/天 解锁文章
1876

被折叠的 条评论
为什么被折叠?



