Advertisement Jump to content
Sign in to follow this  
Doublefris

Computing a real power of a quaternion

This topic is 1735 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

Managed to figure it out, also thanks to boost for their exp function smile.png

	// Raise a quaternion to a real power
	template <typename S>
	inline quaternion<S> pow(const quaternion<S> &q, const S ex)
	{
		return exp(ex * log(q) );
	}

	// Raise a unit quaternion to a real power
	template <typename S>
	inline quaternion<S> unit_pow(const quaternion<S> &q, const S ex)
	{
		static const S C = S(1) - std::numeric_limits<S>::epsilon();

		// Check for the case of an identity quaternion.
		// This will protect against divide by zero
		if ( std::abs(real(q)) > C ) 
			return q;

		S a = acos( real(q) );
		S u = a*ex;

		return quaternion<S>(std::cos(u), 
			imag(q) * (std::sin(u) / std::sin(a)));
	}

	// logarithm of a quaternion.
	// when q is unit this gives { 0 + acos(r) * v/|v| }
	template <typename S>
	inline quaternion<S> log ( const quaternion<S> &q )
	{	
		S N = std::sqrt(norm(q));

		return quaternion<S>( std::log(N), 
			std::acos(real(q) / N) * normalize(imag(q)) );	
	}

	// exponential of a quaternion
	template <typename S>
	inline quaternion<S> exp( const quaternion<S> &q )
	{
		S z = length(imag(q));
		S w = std::sin(z) / z;

		return std::exp(real(q)) * 
			quaternion<S>(std::cos(z), w * imag(q));
	}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!