Axiom.Math.Quaternion.ToRotationMatrix C# (CSharp) Method

ToRotationMatrix() public method

Gets a 3x3 rotation matrix from this Quaternion.
public ToRotationMatrix ( ) : Axiom.Math.Matrix3
return Axiom.Math.Matrix3
		public Matrix3 ToRotationMatrix()
		{
			Matrix3 rotation = new Matrix3();

			Real tx = 2.0f * this.x;
			Real ty = 2.0f * this.y;
			Real tz = 2.0f * this.z;
			Real twx = tx * this.w;
			Real twy = ty * this.w;
			Real twz = tz * this.w;
			Real txx = tx * this.x;
			Real txy = ty * this.x;
			Real txz = tz * this.x;
			Real tyy = ty * this.y;
			Real tyz = tz * this.y;
			Real tzz = tz * this.z;

			rotation.m00 = 1.0f - ( tyy + tzz );
			rotation.m01 = txy - twz;
			rotation.m02 = txz + twy;
			rotation.m10 = txy + twz;
			rotation.m11 = 1.0f - ( txx + tzz );
			rotation.m12 = tyz - twx;
			rotation.m20 = txz - twy;
			rotation.m21 = tyz + twx;
			rotation.m22 = 1.0f - ( txx + tyy );

			return rotation;
		}

Usage Example

Example #1
0
        /// <summary>
        /// Creates an inverse translation Matrix
        /// </summary>
        /// <param name="translation"></param>
        /// <param name="scale"></param>
        /// <param name="orientation"></param>
        /// <returns></returns>
        public static Matrix4 ComposeInverse(Vector3 translation, Vector3 scale, Quaternion orientation)
        {
            // Invert the parameters
            Vector3    invTranslate = -translation;
            Vector3    invScale     = new Vector3(1f / scale.x, 1f / scale.y, 1f / scale.z);
            Quaternion invRot       = orientation.Inverse();

            // Because we're inverting, order is translation, rotation, scale
            // So make translation relative to scale & rotation
            invTranslate *= invScale;              // scale
            invTranslate  = invRot * invTranslate; // rotate

            // Next, make a 3x3 rotation matrix and apply inverse scale
            Matrix3 rot3x3, scale3x3;

            rot3x3       = invRot.ToRotationMatrix();
            scale3x3     = Matrix3.Zero;
            scale3x3.m00 = invScale.x;
            scale3x3.m11 = invScale.y;
            scale3x3.m22 = invScale.z;

            // Set up final matrix with scale, rotation and translation
            Matrix4 result = scale3x3 * rot3x3;

            result.Translation = invTranslate;

            return(result);
        }
All Usage Examples Of Axiom.Math.Quaternion::ToRotationMatrix