/*
-----------------------------------------------------------------------------
This source file is part of OGRE
    (Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2006 Torus Knot Software Ltd
Also see acknowledgements in Readme.html

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt.

You may alternatively use this source under the terms of a specific version of
the OGRE Unrestricted License provided you have obtained such a license from
Torus Knot Software Ltd.
-----------------------------------------------------------------------------
*/
// NOTE THAT THIS FILE IS BASED ON MATERIAL FROM:

// Magic Software, Inc.
// http://www.geometrictools.com/
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// FREE SOURCE CODE
// http://www.geometrictools.com/License/WildMagic3License.pdf

#ifndef __Quaternion_H__
#define __Quaternion_H__

#include "OgrePrerequisites.h"
#include "OgreMatrix3.h"
#include "OgreVector3.h"
#include "OgreMath.h"

namespace Ogre {

    /** Implementation of a Quaternion, i.e. a rotation around an axis.
    */
    class _OgreExport Quaternion
    {
    public:
        inline Quaternion (
            Real fW = 1.0,
            Real fX = 0.0, Real fY = 0.0, Real fZ = 0.0)
		{
			w = fW;
			x = fX;
			y = fY;
			z = fZ;
		}
        inline Quaternion (const Quaternion& rkQ)
		{
			w = rkQ.w;
			x = rkQ.x;
			y = rkQ.y;
			z = rkQ.z;
		}
        /// Construct a quaternion from a rotation matrix
        inline Quaternion(const Matrix3& rot)
        {
            this->FromRotationMatrix(rot);
        }
        /// Construct a quaternion from an angle/axis
        inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis)
        {
            this->FromAngleAxis(rfAngle, rkAxis);
        }
#ifndef OGRE_FORCE_ANGLE_TYPES
        inline Quaternion(const Real& rfAngle, const Vector3& rkAxis)
		{
			this->FromAngleAxis(rfAngle, rkAxis);
		}
#endif//OGRE_FORCE_ANGLE_TYPES
        /// Construct a quaternion from 3 orthonormal local axes
        inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
        {
            this->FromAxes(xaxis, yaxis, zaxis);
        }
        /// Construct a quaternion from 3 orthonormal local axes
        inline Quaternion(const Vector3* akAxis)
        {
            this->FromAxes(akAxis);
        }
		/// Construct a quaternion from 4 manual w/x/y/z values
		inline Quaternion(Real* valptr)
		{
			memcpy(&w, valptr, sizeof(Real)*4);
		}

		/// Array accessor operator
		inline Real operator [] ( const size_t i ) const
		{
			assert( i < 4 );

			return *(&w+i);
		}

		/// Array accessor operator
		inline Real& operator [] ( const size_t i )
		{
			assert( i < 4 );

			return *(&w+i);
		}

		/// Pointer accessor for direct copying
		inline Real* ptr()
		{
			return &w;
		}

		/// Pointer accessor for direct copying
		inline const Real* ptr() const
		{
			return &w;
		}

		void FromRotationMatrix (const Matrix3& kRot);
        void ToRotationMatrix (Matrix3& kRot) const
        {
            //LogManager::getSingleton().logMessage("##########ToRotationMatrix");
            Real fTx  = 2.0*x;
            Real fTy  = 2.0*y;
            Real fTz  = 2.0*z;
            Real fTwx = fTx*w;
            Real fTwy = fTy*w;
            Real fTwz = fTz*w;
            Real fTxx = fTx*x;
            Real fTxy = fTy*x;
            Real fTxz = fTz*x;
            Real fTyy = fTy*y;
            Real fTyz = fTz*y;
            Real fTzz = fTz*z;

            kRot[0][0] = 1.0-(fTyy+fTzz);
            kRot[0][1] = fTxy-fTwz;
            kRot[0][2] = fTxz+fTwy;
            kRot[1][0] = fTxy+fTwz;
            kRot[1][1] = 1.0-(fTxx+fTzz);
            kRot[1][2] = fTyz-fTwx;
            kRot[2][0] = fTxz-fTwy;
            kRot[2][1] = fTyz+fTwx;
            kRot[2][2] = 1.0-(fTxx+fTyy);
        }
        inline void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis)
        {
            //LogManager::getSingleton().logMessage("##########FromAngleAxis");
            // assert:  axis[] is unit length
            //
            // The quaternion representing the rotation is
            //   q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k)

            Radian fHalfAngle ( 0.5*rfAngle );
            Real fSin = Math::Sin(fHalfAngle);
            w = Math::Cos(fHalfAngle);
            x = fSin*rkAxis.x;
            y = fSin*rkAxis.y;
            z = fSin*rkAxis.z;
        }
        void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const;
        inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
            Radian rAngle;
            ToAngleAxis ( rAngle, rkAxis );
            dAngle = rAngle;
        }
#ifndef OGRE_FORCE_ANGLE_TYPES
        inline void FromAngleAxis (const Real& rfAngle, const Vector3& rkAxis) {
			FromAngleAxis ( Angle(rfAngle), rkAxis );
		}
        inline void ToAngleAxis (Real& rfAngle, Vector3& rkAxis) const {
			Radian r;
			ToAngleAxis ( r, rkAxis );
			rfAngle = r.valueAngleUnits();
		}
#endif//OGRE_FORCE_ANGLE_TYPES
        void FromAxes (const Vector3* akAxis);
        void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
        void ToAxes (Vector3* akAxis) const;
        void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
        /// Get the local x-axis
        Vector3 xAxis(void) const;
        /// Get the local y-axis
        Vector3 yAxis(void) const;
        /// Get the local z-axis
        Vector3 zAxis(void) const;

        inline Quaternion& operator= (const Quaternion& rkQ)
		{
			w = rkQ.w;
			x = rkQ.x;
			y = rkQ.y;
			z = rkQ.z;
			return *this;
		}
        inline Quaternion operator+ (const Quaternion& rkQ) const
        {
            //LogManager::getSingleton().logMessage("##########operator+");
            return Quaternion(w+rkQ.w,x+rkQ.x,y+rkQ.y,z+rkQ.z);
        }
        Quaternion operator- (const Quaternion& rkQ) const;
        inline Quaternion operator* (const Quaternion& rkQ) const
        {
            //LogManager::getSingleton().logMessage("##########operator* quat quat");
            // NOTE:  Multiplication is not generally commutative, so in most
            // cases p*q != q*p.

            return Quaternion
            (
                w * rkQ.w - x * rkQ.x - y * rkQ.y - z * rkQ.z,
                w * rkQ.x + x * rkQ.w + y * rkQ.z - z * rkQ.y,
                w * rkQ.y + y * rkQ.w + z * rkQ.x - x * rkQ.z,
                w * rkQ.z + z * rkQ.w + x * rkQ.y - y * rkQ.x
            );
        }
        inline Quaternion operator* (Real fScalar) const
        {
            //LogManager::getSingleton().logMessage("##########operator* scalar quat");
            return Quaternion(fScalar*w,fScalar*x,fScalar*y,fScalar*z);
        }
        _OgreExport friend Quaternion operator* (Real fScalar,
            const Quaternion& rkQ);
        Quaternion operator- () const;
        inline bool operator== (const Quaternion& rhs) const
		{
			return (rhs.x == x) && (rhs.y == y) &&
				(rhs.z == z) && (rhs.w == w);
		}
        inline bool operator!= (const Quaternion& rhs) const
		{
			return !operator==(rhs);
		}
        // functions of a quaternion
        Real Dot (const Quaternion& rkQ) const;  // dot product
        Real Norm () const  // squared-length
        {
            //LogManager::getSingleton().logMessage("##########Norm");
            return w*w+x*x+y*y+z*z;
        }
        /// Normalises this quaternion, and returns the previous length
        inline Real normalise(void)
        {
            //LogManager::getSingleton().logMessage("##########normalise");
            Real len = Norm();
            Real factor = 1.0f / Math::Sqrt(len);
            *this = *this * factor;
            return len;
        }
        Quaternion Inverse () const;  // apply to non-zero quaternion
        inline Quaternion UnitInverse () const  // apply to unit-length quaternion
        {
            //LogManager::getSingleton().logMessage("##########UnitInverse");
            // assert:  'this' is unit length
            return Quaternion(w,-x,-y,-z);
        }
        Quaternion Exp () const;
        Quaternion Log () const;

        // rotation of a vector by a quaternion
        Vector3 operator* (const Vector3& rkVector) const
        {
            //LogManager::getSingleton().logMessage("##########operator* vector3 vector3");
		    // nVidia SDK implementation
		    Vector3 uv, uuv;
		    Vector3 qvec(x, y, z);
		    uv = qvec.crossProduct(rkVector);
		    uuv = qvec.crossProduct(uv);
		    uv *= (2.0f * w);
		    uuv *= 2.0f;

		    return rkVector + uv + uuv;

        }

   		/** Calculate the local roll element of this quaternion.
		@param reprojectAxis By default the method returns the 'intuitive' result
			that is, if you projected the local Y of the quaterion onto the X and
			Y axes, the angle between them is returned. If set to false though, the
			result is the actual yaw that will be used to implement the quaternion,
			which is the shortest possible path to get to the same orientation and 
			may involve less axial rotation. 
		*/
		Radian getRoll(bool reprojectAxis = true) const;
   		/** Calculate the local pitch element of this quaternion
		@param reprojectAxis By default the method returns the 'intuitive' result
			that is, if you projected the local Z of the quaterion onto the X and
			Y axes, the angle between them is returned. If set to true though, the
			result is the actual yaw that will be used to implement the quaternion,
			which is the shortest possible path to get to the same orientation and 
			may involve less axial rotation. 
		*/
		Radian getPitch(bool reprojectAxis = true) const;
   		/** Calculate the local yaw element of this quaternion
		@param reprojectAxis By default the method returns the 'intuitive' result
			that is, if you projected the local Z of the quaterion onto the X and
			Z axes, the angle between them is returned. If set to true though, the
			result is the actual yaw that will be used to implement the quaternion,
			which is the shortest possible path to get to the same orientation and 
			may involve less axial rotation. 
		*/
		Radian getYaw(bool reprojectAxis = true) const;		
		/// Equality with tolerance (tolerance is max angle difference)
		bool equals(const Quaternion& rhs, const Radian& tolerance) const;
		
	    // spherical linear interpolation
        static Quaternion Slerp (Real fT, const Quaternion& rkP,
            const Quaternion& rkQ, bool shortestPath = false);

        static Quaternion SlerpExtraSpins (Real fT,
            const Quaternion& rkP, const Quaternion& rkQ,
            int iExtraSpins);

        // setup for spherical quadratic interpolation
        static void Intermediate (const Quaternion& rkQ0,
            const Quaternion& rkQ1, const Quaternion& rkQ2,
            Quaternion& rka, Quaternion& rkB);

        // spherical quadratic interpolation
        static Quaternion Squad (Real fT, const Quaternion& rkP,
            const Quaternion& rkA, const Quaternion& rkB,
            const Quaternion& rkQ, bool shortestPath = false);

        // normalised linear interpolation - faster but less accurate (non-constant rotation velocity)
        static Quaternion nlerp(Real fT, const Quaternion& rkP, 
            const Quaternion& rkQ, bool shortestPath = false);

        // cutoff for sine near zero
        static const Real ms_fEpsilon;

        // special values
        static const Quaternion ZERO;
        static const Quaternion IDENTITY;

		Real w, x, y, z;

        /** Function for writing to a stream. Outputs "Quaternion(w, x, y, z)" with w,x,y,z
            being the member values of the quaternion.
        */
        inline _OgreExport friend std::ostream& operator <<
            ( std::ostream& o, const Quaternion& q )
        {
            o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
            return o;
        }

    };

}




#endif 
