1. 程式人生 > 其它 >矩陣Matrix到尤拉角Euler轉換

矩陣Matrix到尤拉角Euler轉換

參考文獻:

http://www.geometrictools.com/Documentation/EulerAngles.pdf

但是這裡的公式不能直接用,原因是左右手系空間不同,我這邊採用Direct3D預設的右手系,參考:

https://docs.microsoft.com/en-us/windows/win32/direct3d9/d3dxmatrixrotationyawpitchroll

所以需要自行推導右手系公式,已知各個軸旋轉矩陣公式:

R(\theta_{x})=\begin{bmatrix} 1 & 0 & 0\\ 0 & cos(\theta_{x}) & sin(\theta_{x})\\ 0 & -sin(\theta_{x}) & cos(\theta_{x}) \end{bmatrix}R(\theta_{y})=\begin{bmatrix} cos(\theta_{y}) & 0 & -sin(\theta_{y})\\ 0 & 1 & 0\\ sin(\theta_{y}) & 0 & cos(\theta_{y}) \end{bmatrix}R(\theta_{z})=\begin{bmatrix} cos(\theta_{z}) & sin(\theta_{z}) & 0\\ -sin(\theta_{z}) & cos(\theta_{z}) & 0\\ 0 & 0 & 1 \end{bmatrix}

尤拉角變換順序為YXZ,則先計算YX矩陣

R(\theta_{y})\cdot R(\theta_{x})=\begin{bmatrix} cos(\theta_{y}) & sin(\theta_{y})\cdot sin(\theta_{x}) & -sin(\theta_{y})\cdot cos(\theta_{x})\\ 0 & cos(\theta_{x}) & sin(\theta_{x})\\ sin(\theta_{y}) & -cos(\theta_{y})\cdot sin(\theta_{x}) & cos(\theta_{y})\cdot cos(\theta_{x}) \end{bmatrix}

最終YXZ矩陣

R(\theta_{y})\cdot R(\theta_{x})\cdot R(\theta_{z})=\begin{bmatrix} cos(\theta_{y})\cdot cos(\theta_{z})-sin(\theta_{y})\cdot sin(\theta_{x})\cdot sin(\theta_{z}) & cos(\theta_{y})\cdot sin(\theta_{z})+sin(\theta_{y})\cdot sin(\theta_{x})\cdot cos(\theta_{z}) & -sin(\theta_{y})\cdot cos(\theta_{x}))\\ -cos(\theta_{x})\cdot sin(\theta_{z}) & cos(\theta_{x})\cdot cos(\theta_{z}) & sin(\theta_{x})\\ sin(\theta_{y})\cdot cos(\theta_{z})+cos(\theta_{y})\cdot sin(\theta_{x})\cdot sin(\theta_{z}) & sin(\theta_{y})\cdot sin(\theta_{z})-cos(\theta_{y})\cdot sin(\theta_{x})\cdot cos(\theta_{z}) & cos(\theta_{y})\cdot cos(\theta_{x})) \end{bmatrix}

可以直接得知sin(\theta_{x})=r12,即\theta_{x}=arcsin(r12),然後需要分三種情況

  1. \theta_{x}\in \left (-\frac{\pi }{2}, \frac{\pi }{2}\right ),可知tan(\theta_{y})=\frac {sin(\theta_{y})\cdot cos(\theta_{x})}{cos(\theta_{y})\cdot cos(\theta_{x})},即\theta_{y}=arctan(\frac {-r02} {r22}),同理\theta_{z}=arctan(\frac {-r10} {r11})
  2. \theta_{x}=\frac{\pi}{2},則sin(\theta_{x})=1,YXZ矩陣可簡化為
    R(\theta_{yxz})=\begin{bmatrix} cos(\theta_{y})\cdot cos(\theta_{z})-sin(\theta_{y})\cdot sin(\theta_{z}) & cos(\theta_{y})\cdot sin(\theta_{z})+sin(\theta_{y})\cdot cos(\theta_{z}) & 0\\ 0 & 0 & 1\\ sin(\theta_{y})\cdot cos(\theta_{z})+cos(\theta_{y})\cdot sin(\theta_{z}) & sin(\theta_{y})\cdot sin(\theta_{z})-cos(\theta_{y})\cdot cos(\theta_{z}) & 0 \end{bmatrix}
    根據兩角和公式
    ,可得
    R(\theta_{yxz})=\begin{bmatrix} cos(\theta_{y}+\theta_{z}) & sin(\theta_{y}+\theta_{z}) & 0\\ 0 & 0 & 1\\ sin(\theta_{y}+\theta_{z}) & -cos(\theta_{y}+\theta_{z}) & 0 \end{bmatrix},即 \theta_{y}+\theta_{z}=arctan(\frac {r01}{r00}),且結果不唯一
  3. \theta_{x}=-\frac {\pi}{2},則sin(\theta_{x})=-1,YXZ矩陣簡化為
    R(\theta_{yxz})=\begin{bmatrix} cos(\theta_{y})\cdot cos(\theta_{z})+sin(\theta_{y})\cdot sin(\theta_{z}) & cos(\theta_{y})\cdot sin(\theta_{z})-sin(\theta_{y})\cdot cos(\theta_{z}) & 0\\ 0 & 0 & -1\\ sin(\theta_{y})\cdot cos(\theta_{z})-cos(\theta_{y})\cdot sin(\theta_{z}) & sin(\theta_{y})\cdot sin(\theta_{z})+cos(\theta_{y})\cdot cos(\theta_{z}) & 0 \end{bmatrix}
    可得
    R(\theta_{yxz})=\begin{bmatrix} cos(\theta_{y}-\theta_{z}) & -sin(\theta_{y}-\theta_{z}) & 0\\ 0 & 0 & -1\\ sin(\theta_{y}-\theta_{z}) & cos(\theta_{y}-\theta_{z}) & 0 \end{bmatrix},即 \theta_{y}-\theta_{z}=arctan(\frac {-r01}{r00})

基於以上思路,就能實現D3DXMATRIX到尤拉角的轉換程式碼

D3DXVECTOR3* D3DXMatrixToEulerAngles(D3DXVECTOR3* pOut, const D3DXMATRIX* pM)
{
	if (pM->_23 < 0.999f) // some fudge for imprecision
	{
		if (pM->_23 > -0.999f) // some fudge for imprecision
		{
			pOut->x = asin(pM->_23);
			pOut->y = atan2(-pM->_13, pM->_33);
			pOut->z = atan2(-pM->_21, pM->_22);
		}
		else
		{
			// WARNING.  Not unique.  YA - ZA = atan2(-r01,r00)
			pOut->x = -D3DX_PI * 0.5f;
			pOut->y = atan2(-pM->_12, pM->_11);
			pOut->z = 0.0f;
		}
	}
	else
	{
		// WARNING.  Not unique.  YA + ZA = atan2(r01,r00)
		pOut->x = D3DX_PI * 0.5f;
		pOut->y = atan2(pM->_12, pM->_11);
		pOut->z = 0.0f;
	}
	return pOut;
}