Fix idMatX::IsOrthonormal()

The original implementation was pretty broken (but not used anyway),
it is now fixed and improved a bit (got rid of one inner loop).
This (at least part of the problem) was detected by PVS-Studio,
see http://www.viva64.com/en/b/0120/ Fragment 3
This commit is contained in:
Daniel Gibson 2012-05-13 03:04:34 +02:00
parent 5b2c78b9ff
commit 1e087d9bf6

View file

@ -3109,7 +3109,7 @@ idMatX::IsOrthonormal
============
*/
bool idMatX::IsOrthonormal( const float epsilon ) const {
float *ptr1, *ptr2, sum;
float *ptr1, *ptr2, sum, colVecSum, *colVecPtr;
if ( !IsSquare() ) {
return false;
@ -3117,6 +3117,8 @@ bool idMatX::IsOrthonormal( const float epsilon ) const {
ptr1 = mat;
for ( int i = 0; i < numRows; i++ ) {
colVecSum = 0;
colVecPtr = mat+i; // row 0 col i - don't worry, numRows == numColums because IsSquare()
for ( int j = 0; j < numColumns; j++ ) {
ptr2 = mat + j;
sum = ptr1[0] * ptr2[0] - (float) ( i == j );
@ -3127,16 +3129,14 @@ bool idMatX::IsOrthonormal( const float epsilon ) const {
if ( idMath::Fabs( sum ) > epsilon ) {
return false;
}
// row j, col i - this works because numRows == numColumns
colVecSum += colVecPtr[0] * colVecPtr[0];
colVecPtr += numColumns; // next row, same column
}
ptr1 += numColumns;
ptr2 = mat + i;
sum = ptr2[0] * ptr2[0] - 1.0f;
for ( int j = 1; j < numRows; j++ ) {
ptr2 += numColumns;
sum += ptr2[i] * ptr2[j];
}
if ( idMath::Fabs( sum ) > epsilon ) {
// check that length of *column* vector i is 1 (no need for sqrt because sqrt(1)==1)
if ( idMath::Fabs( colVecSum - 1.0f ) > epsilon ) {
return false;
}
}