From 183dd1ec64e92f367ec920764a9e89a0980ec9a3 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Thu, 1 May 2014 01:59:36 +0800 Subject: [PATCH] inverse and conjugate, negate for matrix --- cocos/2d/CCDirector.cpp | 3 +-- cocos/2d/CCNode.cpp | 6 ++---- cocos/deprecated/CCDeprecated.cpp | 4 ++-- cocos/math/Matrix.cpp | 30 ++++++++++++++++-------------- cocos/math/Matrix.h | 8 ++++---- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/cocos/2d/CCDirector.cpp b/cocos/2d/CCDirector.cpp index 3e8c87577f..e30dd58631 100644 --- a/cocos/2d/CCDirector.cpp +++ b/cocos/2d/CCDirector.cpp @@ -739,8 +739,7 @@ Vector2 Director::convertToGL(const Vector2& uiPoint) Matrix transform; GLToClipTransform(&transform); - Matrix transformInv; - transform.invert(&transformInv); + Matrix transformInv = transform.getInversed(); // Calculate z=0 using -> transform*[0, 0, 0, 1]/w float zClip = transform.m[14]/transform.m[15]; diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index bd327cf99d..376e07ab9b 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -1452,7 +1452,7 @@ AffineTransform Node::getParentToNodeAffineTransform() const const Matrix& Node::getParentToNodeTransform() const { if ( _inverseDirty ) { - _transform.invert(&_inverse); + _inverse = _transform.getInversed(); _inverseDirty = false; } @@ -1489,9 +1489,7 @@ AffineTransform Node::getWorldToNodeAffineTransform() const Matrix Node::getWorldToNodeTransform() const { - Matrix result = getNodeToWorldTransform(); - result.invert(); - return result; + return getNodeToWorldTransform().getInversed(); } diff --git a/cocos/deprecated/CCDeprecated.cpp b/cocos/deprecated/CCDeprecated.cpp index b246f022e5..b95b106742 100644 --- a/cocos/deprecated/CCDeprecated.cpp +++ b/cocos/deprecated/CCDeprecated.cpp @@ -274,13 +274,13 @@ Matrix* kmMat4Identity(Matrix* pOut) Matrix* kmMat4Inverse(Matrix* pOut, const Matrix* pM) { - pM->invert(pOut); + *pOut = pM->getInversed(); return pOut; } Matrix* kmMat4Transpose(Matrix* pOut, const Matrix* pIn) { - pIn->transpose(pOut); + *pOut = pIn->getTransposed(); return pOut; } diff --git a/cocos/math/Matrix.cpp b/cocos/math/Matrix.cpp index 5865789f12..6392351e34 100644 --- a/cocos/math/Matrix.cpp +++ b/cocos/math/Matrix.cpp @@ -634,12 +634,14 @@ void Matrix::getBackVector(Vector3* dst) const dst->z = m[10]; } -bool Matrix::invert() +Matrix Matrix::getInversed() const { - return invert(this); + Matrix mat(*this); + mat.inverse(); + return mat; } -bool Matrix::invert(Matrix* dst) const +bool Matrix::inverse() { float a0 = m[0] * m[5] - m[1] * m[4]; float a1 = m[0] * m[6] - m[2] * m[4]; @@ -683,7 +685,7 @@ bool Matrix::invert(Matrix* dst) const inverse.m[14] = -m[12] * a3 + m[13] * a1 - m[14] * a0; inverse.m[15] = m[8] * a3 - m[9] * a1 + m[10] * a0; - multiply(inverse, 1.0f / det, dst); + multiply(inverse, 1.0f / det, this); return true; } @@ -724,14 +726,14 @@ void Matrix::multiply(const Matrix& m1, const Matrix& m2, Matrix* dst) void Matrix::negate() { - negate(this); + MathUtil::negateMatrix(m, m); } -void Matrix::negate(Matrix* dst) const +Matrix Matrix::getNegated() const { - GP_ASSERT(dst); - - MathUtil::negateMatrix(m, dst->m); + Matrix mat(*this); + mat.negate(); + return mat; } void Matrix::rotate(const Quaternion& q) @@ -946,14 +948,14 @@ void Matrix::translate(const Vector3& t, Matrix* dst) const void Matrix::transpose() { - transpose(this); + MathUtil::transposeMatrix(m, m); } -void Matrix::transpose(Matrix* dst) const +Matrix Matrix::getTransposed() const { - GP_ASSERT(dst); - - MathUtil::transposeMatrix(m, dst->m); + Matrix mat(*this); + mat.transpose(); + return mat; } NS_CC_MATH_END diff --git a/cocos/math/Matrix.h b/cocos/math/Matrix.h index 1c10cd0cb1..5d8cea94ac 100644 --- a/cocos/math/Matrix.h +++ b/cocos/math/Matrix.h @@ -467,7 +467,7 @@ public: * * @return true if the the matrix can be inverted, false otherwise. */ - bool invert(); + bool inverse(); /** * Stores the inverse of this matrix in the specified matrix. @@ -476,7 +476,7 @@ public: * * @return true if the the matrix can be inverted, false otherwise. */ - bool invert(Matrix* dst) const; + Matrix getInversed() const; /** * Determines if this matrix is equal to the identity matrix. @@ -535,7 +535,7 @@ public: * * @param dst A matrix to store the result in. */ - void negate(Matrix* dst) const; + Matrix getNegated() const; /** * Post-multiplies this matrix by the matrix corresponding to the @@ -856,7 +856,7 @@ public: * * @param dst A matrix to store the result in. */ - void transpose(Matrix* dst) const; + Matrix getTransposed() const; /** * Calculates the sum of this matrix with the given matrix.