已知两个坐标系下的坐标,求坐标系之间的转换矩阵(二)

包含平移和旋转变换:

#include <iostream>
#include <GTEngine/Mathematics/GteConvertCoordinates.h>
using namespace gte;

// #define Vector4<double> Vector<4, double>
int main(int argc, char const *argv[])
{
// // Affine change of basis.
	ConvertCoordinates<4, double> convert;

	Vector<4, double> X, Y, P0, P1, diff;
	Matrix<4, 4, double> U, V, A, B;
	bool isRHU, isRHV;

    V.SetCol(0,  Vector<4, double>{1.0, 0.0, 0.0, 0.0});
	V.SetCol(1,  Vector<4, double>{0.0, 1.0, 0.0, 0.0});
	V.SetCol(2,  Vector<4, double>{0.0, 0.0, 1.0, 0.0});
	V.SetCol(3,  Vector<4, double>{0.0, 0.0, 0.0, 1.0});
	
	U.SetCol(0,  Vector<4, double>{0.866, 0.5, 0.0, 0.0});
	U.SetCol(1,  Vector<4, double>{-0.5, 0.866, 0.0, 0.0});
	U.SetCol(2,  Vector<4, double>{0, 0, 1.0, 0.0});
	U.SetCol(3,  Vector<4, double>{10.0, 5.0, 0.0, 1.0});
	convert(U, true, V, false);
    
	isRHU = convert.IsRightHandedU();  // false
	isRHV = conv
### 计算两个坐标系之间转换矩阵的方法 对于给定的两对点来计算坐标变换矩阵,实际上通常需要至少三对不共线的空间点以确保能够唯一确定刚体运动所对应的变换矩阵。然而,在仅有两对点的情况下,可以假设某些条件简化问题。 当有更多点对时,可以通过奇异值分解(SVD)方法得更精确的结果[^2]。具体到仅有的两对点情况: #### 基本思路 1. **构建基础框架** 需要先基于这两对点建立初步的方向信息。设第一个坐标系中的两点分别为 \(P_1\) 和 \(Q_1\) ,第坐标系中相应的两点为 \(P_2\) 和 \(Q_2\) 。通过这两个矢量 \(\overrightarrow{P_1 Q_1}\) 和 \(\overrightarrow{P_2 Q_2}\),可以获得两者间的主要旋转方向。 2. **定义辅助结构** 使用上述得到的方向向量作为新坐标系的第一轴,并据此推导其余两个正交基底形成完整的右手直角坐标系。这一步骤涉及到计算叉乘以及可能的标准化操作,从而获得一组相互垂直的标准基向量集合 {i', j', k'}[^1]。 3. **创建旋转和平移部分** 构造一个 3×3 的旋转矩阵 R,其各列为新的标准基 i'、j' 和 k';同时还需要考虑平移分量 t,即从源坐标系原点至目标坐标系原点的位置偏移量。最终形成的齐次变换矩阵 H 将会是一个 4×4 的方阵,其中左上角是R,最右侧一列表达t,而最后一行为 [0, 0, 0, 1]^T。 ```python import numpy as np def compute_transformation_matrix(P1, P2, Q1, Q2): v1 = (Q1 - P1).reshape(-1,) v2 = (Q2 - P2).reshape(-1,) # Normalize vectors to get unit direction vectors u_v1 = v1 / np.linalg.norm(v1) u_v2 = v2 / np.linalg.norm(v2) # Compute rotation axis and angle using Rodrigues formula or SVD method. cross_product = np.cross(u_v1, u_v2) dot_product = np.dot(u_v1, u_v2) # For simplicity here we assume the simplest case where points are not collinear, # otherwise more complex handling would be required. # Constructing an orthogonal basis from given directions if np.allclose(cross_product, 0): raise ValueError('Vectors cannot be parallel.') z_axis = cross_product / np.linalg.norm(cross_product) y_axis = u_v1 x_axis = np.cross(y_axis, z_axis) rot_mat = np.column_stack((x_axis, y_axis, z_axis)) trans_vec = P2.reshape(-1,) - np.matmul(rot_mat, P1.reshape(-1,)) homog_rot = np.vstack([np.hstack([rot_mat, trans_vec[:, None]]), [0, 0, 0, 1]]) return homog_rot ```
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值