2
0
Эх сурвалжийг харах

Added and to *GLM_EXT_scalar_common* and *GLM_EXT_vector_common*

Christophe 5 жил өмнө
parent
commit
2a8664fe50

+ 24 - 0
glm/ext/scalar_common.hpp

@@ -151,6 +151,30 @@ namespace glm
 	template<typename genType>
 	GLM_FUNC_DECL genType mirrorRepeat(genType const& Texcoord);
 
+	/// Returns a value equal to the nearest integer to x.
+	/// The fraction 0.5 will round in a direction chosen by the
+	/// implementation, presumably the direction that is fastest.
+	///
+	/// @param x The values of the argument must be greater or equal to zero.
+	/// @tparam T floating point scalar types.
+	///
+	/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/round.xml">GLSL round man page</a>
+	/// @see ext_scalar_common extension.
+	template<typename genType>
+	GLM_FUNC_DECL int iround(genType const& x);
+
+	/// Returns a value equal to the nearest integer to x.
+	/// The fraction 0.5 will round in a direction chosen by the
+	/// implementation, presumably the direction that is fastest.
+	///
+	/// @param x The values of the argument must be greater or equal to zero.
+	/// @tparam T floating point scalar types.
+	///
+	/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/round.xml">GLSL round man page</a>
+	/// @see ext_scalar_common extension.
+	template<typename genType>
+	GLM_FUNC_DECL uint uround(genType const& x);
+
 	/// @}
 }//namespace glm
 

+ 18 - 0
glm/ext/scalar_common.inl

@@ -149,4 +149,22 @@ namespace glm
 		genType const Mirror = Clamp + Rest;
 		return mix(Rest, static_cast<genType>(1) - Rest, Mirror >= static_cast<genType>(1));
 	}
+
+	template<typename genType>
+	GLM_FUNC_QUALIFIER int iround(genType const& x)
+	{
+		GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'iround' only accept floating-point inputs");
+		assert(static_cast<genType>(0.0) <= x);
+
+		return static_cast<int>(x + static_cast<genType>(0.5));
+	}
+
+	template<typename genType>
+	GLM_FUNC_QUALIFIER uint uround(genType const& x)
+	{
+		GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'uround' only accept floating-point inputs");
+		assert(static_cast<genType>(0.0) <= x);
+
+		return static_cast<uint>(x + static_cast<genType>(0.5));
+	}
 }//namespace glm

+ 24 - 0
glm/ext/vector_common.hpp

@@ -198,6 +198,30 @@ namespace glm
 	template<length_t L, typename T, qualifier Q>
 	GLM_FUNC_DECL vec<L, T, Q> mirrorRepeat(vec<L, T, Q> const& Texcoord);
 
+	/// Returns a value equal to the nearest integer to x.
+	/// The fraction 0.5 will round in a direction chosen by the
+	/// implementation, presumably the direction that is fastest.
+	///
+	/// @param x The values of the argument must be greater or equal to zero.
+	/// @tparam T floating point scalar types.
+	///
+	/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/round.xml">GLSL round man page</a>
+	/// @see ext_vector_common extension.
+	template<length_t L, typename T, qualifier Q>
+	GLM_FUNC_DECL vec<L, int, Q> iround(vec<L, T, Q> const& x);
+
+	/// Returns a value equal to the nearest integer to x.
+	/// The fraction 0.5 will round in a direction chosen by the
+	/// implementation, presumably the direction that is fastest.
+	///
+	/// @param x The values of the argument must be greater or equal to zero.
+	/// @tparam T floating point scalar types.
+	///
+	/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/round.xml">GLSL round man page</a>
+	/// @see ext_vector_common extension.
+	template<length_t L, typename T, qualifier Q>
+	GLM_FUNC_DECL vec<L, uint, Q> uround(vec<L, T, Q> const& x);
+
 	/// @}
 }//namespace glm
 

+ 18 - 0
glm/ext/vector_common.inl

@@ -126,4 +126,22 @@ namespace glm
 		vec<L, T, Q> const Mirror = Clamp + Rest;
 		return mix(Rest, vec<L, T, Q>(1) - Rest, glm::greaterThanEqual(Mirror, vec<L, T, Q>(1)));
 	}
+
+	template<length_t L, typename T, qualifier Q>
+	GLM_FUNC_QUALIFIER vec<L, int, Q> iround(vec<L, T, Q> const& x)
+	{
+		GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'iround' only accept floating-point inputs");
+		assert(all(lessThanEqual(vec<L, T, Q>(0), x)));
+
+		return vec<L, int, Q>(x + static_cast<T>(0.5));
+	}
+
+	template<length_t L, typename T, qualifier Q>
+	GLM_FUNC_QUALIFIER vec<L, uint, Q> uround(vec<L, T, Q> const& x)
+	{
+		GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'uround' only accept floating-point inputs");
+		assert(all(lessThanEqual(vec<L, T, Q>(0), x)));
+
+		return vec<L, uint, Q>(x + static_cast<T>(0.5));
+	}
 }//namespace glm

+ 2 - 24
glm/gtc/integer.hpp

@@ -19,6 +19,8 @@
 #include "../common.hpp"
 #include "../integer.hpp"
 #include "../exponential.hpp"
+#include "../ext/scalar_common.hpp"
+#include "../ext/vector_common.hpp"
 #include <limits>
 
 #if GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED)
@@ -35,30 +37,6 @@ namespace glm
 	template<typename genIUType>
 	GLM_FUNC_DECL genIUType log2(genIUType x);
 
-	/// Returns a value equal to the nearest integer to x.
-	/// The fraction 0.5 will round in a direction chosen by the
-	/// implementation, presumably the direction that is fastest.
-	///
-	/// @param x The values of the argument must be greater or equal to zero.
-	/// @tparam T floating point scalar types.
-	///
-	/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/round.xml">GLSL round man page</a>
-	/// @see gtc_integer
-	template<length_t L, typename T, qualifier Q>
-	GLM_FUNC_DECL vec<L, int, Q> iround(vec<L, T, Q> const& x);
-
-	/// Returns a value equal to the nearest integer to x.
-	/// The fraction 0.5 will round in a direction chosen by the
-	/// implementation, presumably the direction that is fastest.
-	///
-	/// @param x The values of the argument must be greater or equal to zero.
-	/// @tparam T floating point scalar types.
-	///
-	/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/round.xml">GLSL round man page</a>
-	/// @see gtc_integer
-	template<length_t L, typename T, qualifier Q>
-	GLM_FUNC_DECL vec<L, uint, Q> uround(vec<L, T, Q> const& x);
-
 	/// @}
 } //namespace glm
 

+ 0 - 35
glm/gtc/integer.inl

@@ -30,39 +30,4 @@ namespace detail
 		};
 #	endif//GLM_HAS_BITSCAN_WINDOWS
 }//namespace detail
-	template<typename genType>
-	GLM_FUNC_QUALIFIER int iround(genType x)
-	{
-		GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'iround' only accept floating-point inputs");
-		assert(static_cast<genType>(0.0) <= x);
-
-		return static_cast<int>(x + static_cast<genType>(0.5));
-	}
-
-	template<length_t L, typename T, qualifier Q>
-	GLM_FUNC_QUALIFIER vec<L, int, Q> iround(vec<L, T, Q> const& x)
-	{
-		GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'iround' only accept floating-point inputs");
-		assert(all(lessThanEqual(vec<L, T, Q>(0), x)));
-
-		return vec<L, int, Q>(x + static_cast<T>(0.5));
-	}
-
-	template<typename genType>
-	GLM_FUNC_QUALIFIER uint uround(genType x)
-	{
-		GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'uround' only accept floating-point inputs");
-		assert(static_cast<genType>(0.0) <= x);
-
-		return static_cast<uint>(x + static_cast<genType>(0.5));
-	}
-
-	template<length_t L, typename T, qualifier Q>
-	GLM_FUNC_QUALIFIER vec<L, uint, Q> uround(vec<L, T, Q> const& x)
-	{
-		GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'uround' only accept floating-point inputs");
-		assert(all(lessThanEqual(vec<L, T, Q>(0), x)));
-
-		return vec<L, uint, Q>(x + static_cast<T>(0.5));
-	}
 }//namespace glm

+ 1 - 0
readme.md

@@ -57,6 +57,7 @@ glm::mat4 camera(float Translate, glm::vec2 const& Rotate)
 #### Features:
 - Added *GLM_EXT_scalar_reciprocal* with tests
 - Added *GLM_EXT_vector_reciprocal* with tests
+- Added `glm::iround` and `glm::uround` to *GLM_EXT_scalar_common* and *GLM_EXT_vector_common*
 
 #### Improvements:
 - Added `constexpr` qualifier for `cross` product #1040

+ 33 - 0
test/ext/ext_scalar_common.cpp

@@ -298,6 +298,36 @@ static int test_mirrorRepeat()
 	return Error;
 }
 
+static int test_iround()
+{
+	int Error = 0;
+
+	for(float f = 0.0f; f < 3.1f; f += 0.05f)
+	{
+		int RoundFast = static_cast<int>(glm::iround(f));
+		int RoundSTD = static_cast<int>(glm::round(f));
+		Error += RoundFast == RoundSTD ? 0 : 1;
+		assert(!Error);
+	}
+
+	return Error;
+}
+
+static int test_uround()
+{
+	int Error = 0;
+
+	for(float f = 0.0f; f < 3.1f; f += 0.05f)
+	{
+		int RoundFast = static_cast<int>(glm::uround(f));
+		int RoundSTD = static_cast<int>(glm::round(f));
+		Error += RoundFast == RoundSTD ? 0 : 1;
+		assert(!Error);
+	}
+
+	return Error;
+}
+
 int main()
 {
 	int Error = 0;
@@ -323,5 +353,8 @@ int main()
 	Error += test_mirrorClamp();
 	Error += test_mirrorRepeat();
 
+	Error += test_iround();
+	Error += test_uround();
+
 	return Error;
 }

+ 33 - 0
test/ext/ext_vector_common.cpp

@@ -303,6 +303,36 @@ static int test_mirrorRepeat()
 	return Error;
 }
 
+static int test_iround()
+{
+	int Error = 0;
+
+	for(float f = 0.0f; f < 3.1f; f += 0.05f)
+	{
+		int RoundFast = static_cast<int>(glm::iround(f));
+		int RoundSTD = static_cast<int>(glm::round(f));
+		Error += RoundFast == RoundSTD ? 0 : 1;
+		assert(!Error);
+	}
+
+	return Error;
+}
+
+static int test_uround()
+{
+	int Error = 0;
+
+	for(float f = 0.0f; f < 3.1f; f += 0.05f)
+	{
+		int RoundFast = static_cast<int>(glm::uround(f));
+		int RoundSTD = static_cast<int>(glm::round(f));
+		Error += RoundFast == RoundSTD ? 0 : 1;
+		assert(!Error);
+	}
+
+	return Error;
+}
+
 int main()
 {
 	int Error = 0;
@@ -328,5 +358,8 @@ int main()
 	Error += test_mirrorClamp();
 	Error += test_mirrorRepeat();
 
+	Error += test_iround();
+	Error += test_uround();
+
 	return Error;
 }