Nu/Nu.Math/Matrix3x3.cs
/// <summary>
/// Returns the rotation component of this instance. Quite slow.
/// </summary>
/// <param name="row_normalise">Whether the method should row-normalise (i.e. remove scale from) the Matrix. Pass false if you know it's already normalised.</param>
Returns the rotation component of this instance. Quite slow.
Returns the rotation component of this instance . Quite slow .
public Quaternion ExtractRotation(bool row_normalise = true)
        {
            var row0 = Row0;
            var row1 = Row1;
            var row2 = Row2;
            if (row_normalise)
            {
                row0 = Vector3.Normalize(row0);
                row1 = Vector3.Normalize(row1);
                row2 = Vector3.Normalize(row2);
            }
            Quaternion q = new Quaternion();
            double trace = 0.25 * (row0.X + row1.Y + row2.Z + 1.0);
            if (trace > 0)
            {
                double sq = Math.Sqrt(trace);
                q.W = (float)sq;
                sq = 1.0 / (4.0 * sq);
                q.X = (float)((row1.Z - row2.Y) * sq);
                q.Y = (float)((row2.X - row0.Z) * sq);
                q.Z = (float)((row0.Y - row1.X) * sq);
            }
            else if (row0.X > row1.Y && row0.X > row2.Z)
            {
                double sq = 2.0 * Math.Sqrt(1.0 + row0.X - row1.Y - row2.Z);
                q.X = (float)(0.25 * sq);
                sq = 1.0 / sq;
                q.W = (float)((row2.Y - row1.Z) * sq);
                q.Y = (float)((row1.X + row0.Y) * sq);
                q.Z = (float)((row2.X + row0.Z) * sq);
            }
            else if (row1.Y > row2.Z)
            {
                double sq = 2.0 * Math.Sqrt(1.0 + row1.Y - row0.X - row2.Z);
                q.Y = (float)(0.25 * sq);
                sq = 1.0 / sq;
                q.W = (float)((row2.X - row0.Z) * sq);
                q.X = (float)((row1.X + row0.Y) * sq);
                q.Z = (float)((row2.Y + row1.Z) * sq);
            }
            else
            {
                double sq = 2.0 * Math.Sqrt(1.0 + row2.Z - row0.X - row1.Y);
                q.Z = (float)(0.25 * sq);
                sq = 1.0 / sq;
                q.W = (float)((row1.X - row0.Y) * sq);
                q.X = (float)((row2.X + row0.Z) * sq);
                q.Y = (float)((row2.Y + row1.Z) * sq);
            }
            return Quaternion.Normalize(q);
        }
public Quaternion ExtractRotation ( bool row_normalise = true ) { var row0 = Row0 ; var row1 = Row1 ; var row2 = Row2 ; if ( row_normalise ) { row0 = Vector3 . Normalize ( row0 ) ; row1 = Vector3 . Normalize ( row1 ) ; row2 = Vector3 . Normalize ( row2 ) ; } Quaternion q = new Quaternion ( ) ; double trace = 0.25 * ( row0 . X + row1 . Y + row2 . Z + 1.0 ) ; if ( trace > 0 ) { double sq = Math . Sqrt ( trace ) ; q . W = ( float ) sq ; sq = 1.0 / ( 4.0 * sq ) ; q . X = ( float ) ( ( row1 . Z - row2 . Y ) * sq ) ; q . Y = ( float ) ( ( row2 . X - row0 . Z ) * sq ) ; q . Z = ( float ) ( ( row0 . Y - row1 . X ) * sq ) ; } else if ( row0 . X > row1 . Y && row0 . X > row2 . Z ) { double sq = 2.0 * Math . Sqrt ( 1.0 + row0 . X - row1 . Y - row2 . Z ) ; q . X = ( float ) ( 0.25 * sq ) ; sq = 1.0 / sq ; q . W = ( float ) ( ( row2 . Y - row1 . Z ) * sq ) ; q . Y = ( float ) ( ( row1 . X + row0 . Y ) * sq ) ; q . Z = ( float ) ( ( row2 . X + row0 . Z ) * sq ) ; } else if ( row1 . Y > row2 . Z ) { double sq = 2.0 * Math . Sqrt ( 1.0 + row1 . Y - row0 . X - row2 . Z ) ; q . Y = ( float ) ( 0.25 * sq ) ; sq = 1.0 / sq ; q . W = ( float ) ( ( row2 . X - row0 . Z ) * sq ) ; q . X = ( float ) ( ( row1 . X + row0 . Y ) * sq ) ; q . Z = ( float ) ( ( row2 . Y + row1 . Z ) * sq ) ; } else { double sq = 2.0 * Math . Sqrt ( 1.0 + row2 . Z - row0 . X - row1 . Y ) ; q . Z = ( float ) ( 0.25 * sq ) ; sq = 1.0 / sq ; q . W = ( float ) ( ( row1 . X - row0 . Y ) * sq ) ; q . X = ( float ) ( ( row2 . X + row0 . Z ) * sq ) ; q . Y = ( float ) ( ( row2 . Y + row1 . Z ) * sq ) ; } return Quaternion . Normalize ( q ) ; }
Returns the rotation component of this instance.
Returns the rotation component of this instance .
// code below adapted from Blender
row_normalise
bool
Whether the method should row-normalise  the Matrix. Pass false if you know it's already normalised.
Whether the method should row - normalise the Matrix . Pass false if you know it ' s already normalised .