Decomposing a matrix(用于分解变换矩阵至旋转、平移,缩放分量)

文章来源:http://www.ziggyware.com/readarticle.php?article_id=15

 

Matrix decomposition is very useful when you would like to get the translation, scale or rotation from a matrix. This should not be done very often however since it is an expensive operation.

Here is a Matrix Decomposition function declaration

Parameters: const Matrix& mat
The input matrix to decompose into its translation, scale and rotation elements
Vector3& vTrans
The output decomposed translation Vector
Vector3& vScale
The output decomposed scale Vector
Matrix4& mRot
The output decomposed rotation Matrix

    inline void MatrixDecompose(const Matrix4& mat, 
                         Vector3& vTrans,
                         Vector3& vScale,
                         Matrix4& mRot)
    {



Retrieving the translation is as easy as getting the x,y,z values from the thrid row:

        vTrans.x = mat.m_val[3][0];
        vTrans.y = mat.m_val[3][1];
        vTrans.z = mat.m_val[3][2];



We create a temporary Vector3 array to store the 3x3 matrix that contains the scaling and rotation. We will then take this information and seperate it into its scale and rotation components.

        Vector3 vCols[3] = {
                Vector3(mat.m_val[0][0],mat.m_val[0][1],mat.m_val[0][2]),
                Vector3(mat.m_val[1][0],mat.m_val[1][1],mat.m_val[1][2]),
                Vector3(mat.m_val[2][0],mat.m_val[2][1],mat.m_val[2][2])
        };



Retrieving the scale is done by gathering the legnth of the vectors for each column of the 3x3 matrix.

        vScale.x = vCols[0].Length();
        vScale.y = vCols[1].Length();
        vScale.z = vCols[2].Length();



The 3x3 rotation matrix can be obtained by dividing each column of the 3x3 rotation/scale matrix by the retrieved scalar component:

        if(vScale.x != 0)
        {
            vCols[0].x /= vScale.x;
            vCols[0].y /= vScale.x;
            vCols[0].z /= vScale.x;
        }
        if(vScale.y != 0)
        {
            vCols[1].x /= vScale.y;
            vCols[1].y /= vScale.y;
            vCols[1].z /= vScale.y;
        }
        if(vScale.z != 0)
        {
            vCols[2].x /= vScale.z;
            vCols[2].y /= vScale.z;
            vCols[2].z /= vScale.z;
        }

        //unroll the loop to increase the speed

        /*for(int x=0;x<3;x++)
        {
        mRot.m_val[0][x] = vCols[x].x;
        mRot.m_val[1][x] = vCols[x].y;
        mRot.m_val[2][x] = vCols[x].z;
        mRot.m_val[x][3] = 0;
        mRot.m_val[3][x] = 0;
        }*/
        mRot.m_val[0][0] = vCols[0].x;
        mRot.m_val[1][0] = vCols[0].y;
        mRot.m_val[2][0] = vCols[0].z;
        mRot.m_val[0][3] = 0;
        mRot.m_val[3][0] = 0;
        mRot.m_val[0][1] = vCols[1].x;
        mRot.m_val[1][1] = vCols[1].y;
        mRot.m_val[2][1] = vCols[1].z;
        mRot.m_val[1][3] = 0;
        mRot.m_val[3][1] = 0;
        mRot.m_val[0][2] = vCols[2].x;
        mRot.m_val[1][2] = vCols[2].y;
        mRot.m_val[2][2] = vCols[2].z;
        mRot.m_val[2][3] = 0;
        mRot.m_val[3][2] = 0;  
        mRot.m_val[3][3] = 1;
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值