Daniele Bartolini 10 лет назад
Родитель
Сommit
a15406300a
4 измененных файлов с 92 добавлено и 81 удалено
  1. 0 79
      src/core/math/matrix3x3.h
  2. 1 1
      src/core/math/matrix4x4.h
  3. 88 0
      src/core/math/quaternion.cpp
  4. 3 1
      src/core/math/quaternion.h

+ 0 - 79
src/core/math/matrix3x3.h

@@ -6,8 +6,6 @@
 #pragma once
 
 #include "math_types.h"
-#include "vector3.h"
-#include "quaternion.h"
 
 namespace crown
 {
@@ -235,83 +233,6 @@ inline void set_identity(Matrix3x3& m)
 	m.z.z = 1.0f;
 }
 
-/// Returns the rotation portion of the matrix @a m as a Quaternion.
-inline Quaternion rotation(const Matrix3x3& m)
-{
-	const float ww = m.x.x + m.y.y + m.z.z;
-	const float xx = m.x.x - m.y.y - m.z.z;
-	const float yy = m.y.y - m.x.x - m.z.z;
-	const float zz = m.z.z - m.x.x - m.y.y;
-	float max = ww;
-	uint32_t index = 0;
-
-	if (xx > max)
-	{
-		max = xx;
-		index = 1;
-	}
-
-	if (yy > max)
-	{
-		max = yy;
-		index = 2;
-	}
-
-	if (zz > max)
-	{
-		max = zz;
-		index = 3;
-	}
-
-	const float biggest = sqrtf(max + 1.0f) * 0.5f;
-	const float mult = 0.25f / biggest;
-
-	Quaternion tmp;
-	switch (index)
-	{
-		case 0:
-		{
-			tmp.w = biggest;
-			tmp.x = (m.y.z - m.z.y) * mult;
-			tmp.y = (m.z.x - m.x.z) * mult;
-			tmp.z = (m.x.y - m.y.x) * mult;
-			break;
-		}
-		case 1:
-		{
-			tmp.x = biggest;
-			tmp.w = (m.y.z - m.z.y) * mult;
-			tmp.y = (m.x.y + m.y.x) * mult;
-			tmp.z = (m.z.x + m.x.z) * mult;
-			break;
-		}
-		case 2:
-		{
-			tmp.y = biggest;
-			tmp.w = (m.z.x - m.x.z) * mult;
-			tmp.x = (m.x.y + m.y.x) * mult;
-			tmp.z = (m.y.z + m.z.y) * mult;
-			break;
-		}
-		case 3:
-		{
-			tmp.z = biggest;
-			tmp.w = (m.x.y - m.y.x) * mult;
-			tmp.x = (m.z.x + m.x.z) * mult;
-			tmp.y = (m.y.z + m.z.y) * mult;
-			break;
-		}
-		default:
-		{
-			CE_FATAL("You should not be here");
-			break;
-		}
-	}
-
-	normalize(tmp);
-	return tmp;
-}
-
 /// Returns the scale of the matrix @a m.
 inline Vector3 scale(const Matrix3x3& m)
 {

+ 1 - 1
src/core/math/matrix4x4.h

@@ -582,7 +582,7 @@ inline Matrix3x3 to_matrix3x3(const Matrix4x4& m)
 /// Returns the rotation portion of the matrix @a m as a Quaternion.
 inline Quaternion rotation(const Matrix4x4& m)
 {
-	return rotation(to_matrix3x3(m));
+	return quaternion(to_matrix3x3(m));
 }
 
 /// Sets the rotation portion of the matrix @a m.

+ 88 - 0
src/core/math/quaternion.cpp

@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "quaternion.h"
+
+namespace crown
+{
+
+/// Returns the rotation portion of the matrix @a m as a Quaternion.
+Quaternion quaternion(const Matrix3x3& m)
+{
+	const float ww = m.x.x + m.y.y + m.z.z;
+	const float xx = m.x.x - m.y.y - m.z.z;
+	const float yy = m.y.y - m.x.x - m.z.z;
+	const float zz = m.z.z - m.x.x - m.y.y;
+	float max = ww;
+	uint32_t index = 0;
+
+	if (xx > max)
+	{
+		max = xx;
+		index = 1;
+	}
+
+	if (yy > max)
+	{
+		max = yy;
+		index = 2;
+	}
+
+	if (zz > max)
+	{
+		max = zz;
+		index = 3;
+	}
+
+	const float biggest = sqrtf(max + 1.0f) * 0.5f;
+	const float mult = 0.25f / biggest;
+
+	Quaternion tmp;
+	switch (index)
+	{
+		case 0:
+		{
+			tmp.w = biggest;
+			tmp.x = (m.y.z - m.z.y) * mult;
+			tmp.y = (m.z.x - m.x.z) * mult;
+			tmp.z = (m.x.y - m.y.x) * mult;
+			break;
+		}
+		case 1:
+		{
+			tmp.x = biggest;
+			tmp.w = (m.y.z - m.z.y) * mult;
+			tmp.y = (m.x.y + m.y.x) * mult;
+			tmp.z = (m.z.x + m.x.z) * mult;
+			break;
+		}
+		case 2:
+		{
+			tmp.y = biggest;
+			tmp.w = (m.z.x - m.x.z) * mult;
+			tmp.x = (m.x.y + m.y.x) * mult;
+			tmp.z = (m.y.z + m.z.y) * mult;
+			break;
+		}
+		case 3:
+		{
+			tmp.z = biggest;
+			tmp.w = (m.x.y - m.y.x) * mult;
+			tmp.x = (m.z.x + m.x.z) * mult;
+			tmp.y = (m.y.z + m.z.y) * mult;
+			break;
+		}
+		default:
+		{
+			CE_FATAL("You should not be here");
+			break;
+		}
+	}
+
+	normalize(tmp);
+	return tmp;
+}
+
+} // namespace crown

+ 3 - 1
src/core/math/quaternion.h

@@ -6,8 +6,8 @@
 #pragma once
 
 #include "types.h"
-#include "vector3.h"
 #include "math_types.h"
+#include "math_utils.h"
 
 namespace crown
 {
@@ -36,6 +36,8 @@ inline Quaternion quaternion(const Vector3& axis, float angle)
 	return q;
 }
 
+Quaternion quaternion(const Matrix3x3& m);
+
 inline Quaternion& operator*=(Quaternion& a, const Quaternion& b)
 {
 	const float t_w = a.w*b.w - a.x*b.x - a.y*b.y - a.z*b.z;