//计算位于三角形内部任意一点相对于三个顶点的重心坐标
bool GetBaryCoord(CVector3d *tri,const CVector3d& cp,CVector3d &bCoord)
{
CVector3d v0 = tri[0] - cp;
CVector3d v1 = tri[1] - cp;
CVector3d v2 = tri[2] - cp;
CVector3d va = tri[1] - tri[0];
CVector3d vb = tri[2] - tri[0];
double d = va.Dot(vb);
if (d > -EPSILON)
{
double r = v1.Dot(v2) / d;
double g = v0.Dot(v2) / d;
double b = v0.Dot(v1) / d;
bCoord.Set(r,g,b);
return true;
}
else
{
return false;
}
}
//还没有好好测试~~
可能会出点小问题
果然是写错了
//计算位于三角形内部任意一点相对于三个顶点的重心坐标
bool GetBaryCoord(CVector3d *tri,CVector3d &bCoord)
{
CVector3d v0 = tri[0] - cp;
CVector3d v1 = tri[1] - cp;
CVector3d v2 = tri[2] - cp;
CVector3d va = tri[1] - tri[0];
CVector3d vb = tri[2] - tri[0];
CVector3d vd = va.Cross(vb);
double lend = vd.Length();
CVector3d vr,vg,vvb;
double r,b;
if (lend > -EPSILON)
{
vr = v1.Cross(v2);
r = vr.Length() / lend;
vg = v0.Cross(v2);
g = vg.Length() / lend;
vvb = v0.Cross(v1);
b = vvb.Length() / lend;
bCoord.Set(r,b);
return true;
}
else
{
return false;
}
}
但是这个会不会是遇到数值上的问题呢?如何去避免?……