vector_math.dart icon indicating copy to clipboard operation
vector_math.dart copied to clipboard

Decomposing a Matrix4 does not result in the same scale as its composition.

Open danielljunior opened this issue 4 years ago • 1 comments

To replicate the error do the following: Create a random Vector3 for position. Create a random Vector3 for scale. Create a random Quaternion for rotation.

Compose a Matrix4 with the above inputs. Decompose this same matrix to other scale, rotation and position objects, for comparison. The position and rotation match, but the scale - except for x:1.0, y:1.0, z:1.0 - does not match.

The following is the code I used in my test:

test(
    'Decomposing a Matrix4 results in roughly the same'
    'translation, rotation and scale as its composition',
    (){

        for( int i=0; i<128; ++i ){
            setVector3ToRandom( position, RNG );
            setVector3ToRandom( scale, RNG );
            rotation.setRandom( RNG );

            Matrix4 transform = Matrix4.compose(
                position, rotation, scale
            );

            transform.decompose(
                decomposedPosition,
                decomposedRotation,
                decomposedScale
            );

            expect(
                decomposedPosition,
                equals( position )
            );

            expect(
                decomposedRotation
                    .absoluteError( rotation ),
                lessThan( EPSILON )
            );

            expect(
                decomposedScale.absoluteError( scale ),
                lessThan( EPSILON )
            );
        }

    }
);

The absolute error on the scale is usually huge, the smallest error I got was aound 700.0. The setVector3ToRandom() method generates values from -1024.0 to 1024.0 on each axys.

Edit: The problem only happens when negative values are used in the scale vector of the composition. Is there reason for that or is it really a bug?

danielljunior avatar Dec 09 '20 05:12 danielljunior

Have you tried to see if the decomposition components, when re-composed, equal the original composition?

GhostCore07 avatar Apr 10 '22 02:04 GhostCore07