// 获取从一个向量转到另一个向量的矩阵
static AcGeVector3d CrossProduct(AcGeVector3d a, AcGeVector3d b);
static double DotProduct(AcGeVector3d a, AcGeVector3d b);
static double Absolute(AcGeVector3d v);
static AcGeMatrix3d RotationMatrix(double angle, AcGeVector3d u);
static AcGeMatrix3d CalculationMtrix(AcGeVector3d vectorBefore, AcGeVector3d vectorAfter);
// 封装函数:得到从一个向量转到另一个向量的矩阵
注:向量长度不能为0或两向量平行
AcGeVector3d CrossProduct(AcGeVector3d a, AcGeVector3d b)
{
AcGeVector3d c;
///*c.x = a[1] * b[2] - a[2] * b[1];
//c.y = a[2] * b[0] - a[0] * b[2];
//c.z = a[0] * b[1] - a[1] * b[0];*/
c.x = a.y * b.z - a.z * b.y;
c.y = a.z * b.x - a.x * b.z;
c.z = a.x * b.y - a.y * b.x;
return c;
}
double DotProduct(AcGeVector3d a, AcGeVector3d b)
{
double result;
//result = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
result = a.x * b.x + a.y * b.y + a.z * b.z;
return result;
}
double Absolute(AcGeVector3d v)
{
double result;
result = sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
return result;
}
AcGeMatrix3d RotationMatrix(double angle, AcGeVector3d u)
{
double norm = Absolute(u);
u.x = u.x / norm;
u.y = u.y / norm;
u.z = u.z / norm;
AcGeMatrix3d rotatinMatrix;
rotatinMatrix.entry[0][0] = cos(angle) + u.x * u.x * (1 - cos(angle));
rotatinMatrix.entry[0][1] = u.x * u.y * (1 - cos(angle)) - u.z * sin(angle);
rotatinMatrix.entry[0][2] = u.y * sin(angle) + u.x * u.z * (1 - cos(angle));
rotatinMatrix.entry[0][3] = 0.0;
rotatinMatrix.entry[1][0] = u.z * sin(angle) + u.x * u.y * (1 - cos(angle));
rotatinMatrix.entry[1][1] = cos(angle) + u.y * u.y * (1 - cos(angle));
rotatinMatrix.entry[1][2] = -u.x * sin(angle) + u.y * u.z * (1 - cos(angle));
rotatinMatrix.entry[1][3] = 0.0;
rotatinMatrix.entry[2][0] = -u.y * sin(angle) + u.x * u.z * (1 - cos(angle));
rotatinMatrix.entry[2][1] = u.x * sin(angle) + u.y * u.z * (1 - cos(angle));
rotatinMatrix.entry[2][2] = cos(angle) + u.z * u.z * (1 - cos(angle));
rotatinMatrix.entry[2][3] = 0.0;
rotatinMatrix.entry[3][0] = 0.0;
rotatinMatrix.entry[3][1] = 0.0;
rotatinMatrix.entry[3][2] = 0.0;
rotatinMatrix.entry[3][3] = 1.0;
return rotatinMatrix;
}
AcGeMatrix3d CalculationMtrix(AcGeVector3d vectorBefore, AcGeVector3d vectorAfter)
{
double rotationAngle = acos(DotProduct(vectorBefore, vectorAfter) / Absolute(vectorBefore) / Absolute(vectorAfter));
AcGeVector3d rotationAxis = CrossProduct(vectorBefore, vectorAfter);
AcGeMatrix3d rotationMatrix = RotationMatrix(rotationAngle, rotationAxis);
return rotationMatrix;
}
模型矩阵变换:
// 平移矩阵
AcGeMatrix3d transMatx;
transMatx.setToTranslation(100,100,0);
// 缩放矩阵
AcGeMatrix3d scaleMatx1;
scaleMatx1.setToScaling(0.5, AcGePoint3d(0, 0, 0));
// 旋转矩阵
AcGeVector3d vecOrign(1,0,0);
vecOrign.normalize();
AcGeVector3d vecTarget(12,25,10);
vecTarget.normalize();
AcGeMatrix3d rotMatx = CLib_Global::CalculationMtrix(vecOrign,vecTarget);
// 复合矩阵
AcGeMatrix3d compMatx1;
compMatx1 = scaleMatx1.preMultBy(rotMatx);
compMatx1 = compMatx1.preMultBy(transMatx);
AcGeMatrix3d entityMatrix = compMatx1;
……………………