Selaa lähdekoodia

Fixed quaternion pow #346

Christophe Riccio 10 vuotta sitten
vanhempi
sitoutus
063c5c7367
2 muutettua tiedostoa jossa 19 lisäystä ja 23 poistoa
  1. 18 23
      glm/gtx/quaternion.inl
  2. 1 0
      readme.md

+ 18 - 23
glm/gtx/quaternion.inl

@@ -121,33 +121,28 @@ namespace glm
 	}
 
 	template <typename T, precision P>
-	GLM_FUNC_QUALIFIER tquat<T, P> pow
-	(
-		tquat<T, P> const & x,
-		T const & y
-	)
+	GLM_FUNC_QUALIFIER tquat<T, P> pow(tquat<T, P> const & x, T const & y)
 	{
-		if(abs(x.w) > (static_cast<T>(1) - epsilon<T>()))
-			return x;
-		T Angle = acos(y);
+		//Raising to the power of 0 should yield 1
+		//Needed to prevent a division by 0 error later on
+		if(y > -epsilon<T>() && y < epsilon<T>())
+			return tquat<T, P>(1,0,0,0);
+
+		//To deal with non-unit quaternions
+		T magnitude = sqrt(x.x * x.x + x.y * x.y + x.z * x.z + x.w *x.w);
+
+		//Equivalent to raising a real number to a power
+		//Needed to prevent a division by 0 error later on
+		if(abs(x.w / magnitude) > static_cast<T>(1) - epsilon<T>() && abs(x.w / magnitude) < static_cast<T>(1) + epsilon<T>())
+			return tquat<T, P>(pow(x.w, y),0,0,0);
+
+		T Angle = acos(x.w / magnitude);
 		T NewAngle = Angle * y;
 		T Div = sin(NewAngle) / sin(Angle);
-		return tquat<T, P>(
-			cos(NewAngle),
-			x.x * Div,
-			x.y * Div,
-			x.z * Div);
-	}
+		T Mag = pow(magnitude, y-1);
 
-	//template <typename T, precision P>
-	//GLM_FUNC_QUALIFIER tquat<T, P> sqrt
-	//(
-	//	tquat<T, P> const & q
-	//)
-	//{
-	//	T q0 = static_cast<T>(1) - dot(q, q);
-	//	return T(2) * (T(1) + q0) * q;
-	//}
+		return tquat<T, P>(cos(NewAngle) * magnitude * Mag, x.x * Div * Mag, x.y * Div * Mag, x.z * Div * Mag);
+	}
 
 	template <typename T, precision P>
 	GLM_FUNC_QUALIFIER tvec3<T, P> rotate

+ 1 - 0
readme.md

@@ -78,6 +78,7 @@ glm::mat4 camera(float Translate, glm::vec2 const & Rotate)
 - Fixed isfinite with C++98 compilers #343
 - Fixed Intel compiler build error on Linux #354
 - Fixed use of libstdc++ with Clang #351
+- Fixed quaternion pow #346
 
 ##### Deprecation:
 - Removed integer specification for 'mod' in GTC_integer #308