mrdoob/three.js — comment on commit 9809101319612e9e0abc9f13e7b42c22cd569e24
File:  line 0
By alteredq:

Ok, I tested and it does seem to perform better - I can quite reliably mess up moon when using `getRotationFromMatrix` repeatedly but I didn't manage it yet with `decompose` followed by `getRotationFromQuaternion`

Just I have unholy mess of the code, I wonder how to get it to you so you could test it on your problematic rotations use case.

I guess the simplest is just to post code here:

```javascript
// Object3D.applyMatrix

applyMatrix: function ( matrix ) {

this.matrix.multiply( matrix, this.matrix );
this.matrix.decompose( this.position, this.quaternion, this.scale );
this.rotation.getRotationFromQuaternion( this.quaternion );

}
```

```javascript
// Vector3.getRotationFromQuaternion

getRotationFromQuaternion: function ( q ) {

var sqw = q.w * q.w;
var sqx = q.x * q.x;
var sqy = q.y * q.y;
var sqz = q.z * q.z;
var unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
var test = q.x * q.y + q.z * q.w;

if ( test > 0.499 * unit ) { // singularity at north pole

this.y = 2 * Math.atan2( q.x, q.w );
this.z = Math.PI / 2;
this.x = 0;

return;

}

if ( test < -0.499 * unit ) { // singularity at south pole

this.y = -2 * Math.atan2( q.x, q.w );
this.z = -Math.PI / 2;
this.x = 0;

return;

}

this.y = Math.atan2( 2 * q.y * q.w - 2 * q.x * q.z, sqx - sqy - sqz + sqw );
this.z = Math.asin( 2 * test / unit );
this.x = Math.atan2( 2 * q.x * q.w - 2 * q.y * q.z, -sqx + sqy - sqz + sqw );

},
```