Browse Source

- Added identity functions #765

Christophe Riccio 7 years ago
parent
commit
1afa681512

+ 40 - 0
glm/detail/qualifier.hpp

@@ -152,5 +152,45 @@ namespace detail
 		typedef glm_u64vec4 type;
 	};
 #	endif
+
+	enum genTypeEnum
+	{
+		GENTYPE_VEC,
+		GENTYPE_MAT,
+		GENTYPE_QUAT
+	};
+
+	template <typename genType>
+	struct genTypeTrait
+	{};
+
+	template <length_t C, length_t R, typename T>
+	struct genTypeTrait<mat<C, R, T> >
+	{
+		static const genTypeEnum GENTYPE = GENTYPE_MAT;
+	};
+
+	template<typename genType, genTypeEnum type>
+	struct init_gentype
+	{
+	};
+
+	template<typename genType>
+	struct init_gentype<genType, GENTYPE_QUAT>
+	{
+		GLM_FUNC_QUALIFIER static genType identity()
+		{
+			return genType(1, 0, 0, 0);
+		}
+	};
+
+	template<typename genType>
+	struct init_gentype<genType, GENTYPE_MAT>
+	{
+		GLM_FUNC_QUALIFIER static genType identity()
+		{
+			return genType(1);
+		}
+	};
 }//namespace detail
 }//namespace glm

+ 4 - 0
glm/gtc/matrix_transform.hpp

@@ -36,6 +36,10 @@ namespace glm
 	/// @addtogroup gtc_matrix_transform
 	/// @{
 
+	/// Builds an identity matrix.
+	template<typename genType>
+	GLM_FUNC_DECL genType identity();
+
 	/// Builds a translation 4 * 4 matrix created from a vector of 3 components.
 	///
 	/// @param m Input matrix multiplied by this translation matrix.

+ 6 - 0
glm/gtc/matrix_transform.inl

@@ -7,6 +7,12 @@
 
 namespace glm
 {
+	template<typename genType>
+	GLM_FUNC_QUALIFIER genType identity()
+	{
+		return detail::init_gentype<genType, detail::genTypeTrait<genType>::GENTYPE>::identity();
+	}
+
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER mat<4, 4, T, Q> translate(mat<4, 4, T, Q> const& m, vec<3, T, Q> const& v)
 	{

+ 5 - 0
glm/gtc/quaternion.hpp

@@ -19,6 +19,7 @@
 #include "../vec3.hpp"
 #include "../vec4.hpp"
 #include "../gtc/constants.hpp"
+#include "../gtc/matrix_transform.hpp"
 
 #if GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED)
 #	pragma message("GLM: GLM_GTC_quaternion extension included")
@@ -159,6 +160,10 @@ namespace glm
 	template<typename T, qualifier Q>
 	GLM_FUNC_DECL bool operator!=(tquat<T, Q> const& q1, tquat<T, Q> const& q2);
 
+	/// Builds an identity quaternion.
+	template<typename genType>
+	GLM_FUNC_DECL genType identity();
+
 	/// Returns the length of the quaternion.
 	///
 	/// @tparam T Floating-point scalar types.

+ 6 - 0
glm/gtc/quaternion.inl

@@ -10,6 +10,12 @@
 namespace glm{
 namespace detail
 {
+	template <typename T>
+	struct genTypeTrait<tquat<T> >
+	{
+		static const genTypeEnum GENTYPE = GENTYPE_QUAT;
+	};
+
 	template<typename T, qualifier Q, bool Aligned>
 	struct compute_dot<tquat<T, Q>, T, Aligned>
 	{

+ 1 - 0
readme.md

@@ -63,6 +63,7 @@ glm::mat4 camera(float Translate, glm::vec2 const& Rotate)
 - Added detection of Visual C++ 2017 toolsets
 - Added missing equal and notEqual with epsilon for quaternion types in EXT_vector_relational
 - Added missing equal and notEqual with epsilon for matrix types in EXT_vector_relational
+- Added identity functions #765
 
 #### Fixes:
 - Fixed build problems due to printf and std::clock_t #778

+ 3 - 57
test/gtc/gtc_quaternion.cpp

@@ -1,4 +1,5 @@
 #include <glm/gtc/quaternion.hpp>
+#include <glm/gtc/matrix_transform.hpp>
 #include <glm/ext/vector_relational.hpp>
 #include <glm/glm.hpp>
 #include <vector>
@@ -317,71 +318,16 @@ static int test_constexpr()
 	return 0;
 }
 
-using namespace glm;
-
-enum genTypeEnum
-{
-	GENTYPE_VEC,
-	GENTYPE_MAT,
-	GENTYPE_QUAT
-};
-
-template <typename genType>
-struct genTypeTrait
-{};
-
-template <typename T>
-struct genTypeTrait<tquat<T> >
-{
-	static const genTypeEnum GENTYPE = GENTYPE_QUAT;
-};
-
-template <length_t C, length_t R, typename T>
-struct genTypeTrait<mat<C, R, T> >
-{
-	static const genTypeEnum GENTYPE = GENTYPE_MAT;
-};
-
-template<typename genType, genTypeEnum type>
-struct init_gentype
-{
-};
-
-template<typename genType>
-struct init_gentype<genType, GENTYPE_QUAT>
-{
-	static genType identity()
-	{
-		return genType(1, 0, 0, 0);
-	}
-};
-
-template<typename genType>
-struct init_gentype<genType, GENTYPE_MAT>
-{
-	static genType identity()
-	{
-		return genType(1);
-	}
-};
-
-template<typename genType>
-inline genType identity()
-{
-	//return init_gentype<genType, genType::GENTYPE>::identity();
-	return init_gentype<genType, genTypeTrait<genType>::GENTYPE>::identity();
-}
-
 int test_identity()
 {
 	int Error = 0;
 
-	glm::quat const Q = identity<glm::quat>();
+	glm::quat const Q = glm::identity<glm::quat>();
 
 	Error += glm::all(glm::equal(Q, glm::quat(1, 0, 0, 0), 0.0001f)) ? 0 : 1;
 	Error += glm::any(glm::notEqual(Q, glm::quat(1, 0, 0, 0), 0.0001f)) ? 1 : 0;
 
-	glm::mat4 const M = identity<glm::mat4x4>();
+	glm::mat4 const M = glm::identity<glm::mat4x4>();
 	glm::mat4 const N(1.0f);
 
 	Error += glm::all(glm::equal(M[0], N[0], 0.0001f)) ? 0 : 1;