方法
对于三角形所在的平面上一点
,有:
记,
,
,即:
方程两边同乘和同乘
,得:
矩阵形式:
解:
实现
bool pointinTriangle(const Eigen::Vector3f &A, const Eigen::Vector3f &B,
const Eigen::Vector3f &C, const Eigen::Vector3f &P)
{
Vector3f v0 = C - A;
Vector3f v1 = B - A;
Vector3f v2 = P - A;
float dot00 = v0.dot(v0);
float dot01 = v0.dot(v1);
float dot02 = v0.dot(v2);
float dot11 = v1.dot(v1);
float dot12 = v1.dot(v2);
Matrix2f ACoff;
ACoff<< dot00, dot01,
dot01, dot11;
Vector2f b(dot02, dot12);
Vector2f x = ACoff.lu().solve(b);
if (x[0] < 0 || x[0] > 1) // u out of range
{
return false;
}
if (x[1] < 0 || x[1] > 1) // v out of range
{
return false;
}
return x[0] + x[1] <= 1;
}