C++ 实现:根据旋转前后的向量求旋转矩阵

// 获取从一个向量转到另一个向量的矩阵

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;
				……………………
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欧特克_Glodon

很高兴能帮助到您!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值