Quellcode durchsuchen

Implementing fcompMin / fcompMax, closes g-truc/glm#1215

Maximilian Sackel vor 1 Jahr
Ursprung
Commit
c9ca4dc77c
3 geänderte Dateien mit 88 neuen und 0 gelöschten Zeilen
  1. 10 0
      glm/gtx/component_wise.hpp
  2. 19 0
      glm/gtx/component_wise.inl
  3. 59 0
      test/gtx/gtx_component_wise.cpp

+ 10 - 0
glm/gtx/component_wise.hpp

@@ -61,6 +61,16 @@ namespace glm
 	template<typename genType>
 	GLM_FUNC_DECL typename genType::value_type compMax(genType const& v);
 
+    /// Find the minimum float between single vector components.
+	/// @see gtx_component_wise
+	template<typename genType>
+	GLM_FUNC_DECL typename genType::value_type fcompMin(genType const& v);
+
+	/// Find the maximum float between single vector components.
+	/// @see gtx_component_wise
+	template<typename genType>
+	GLM_FUNC_DECL typename genType::value_type fcompMax(genType const& v);
+
 	/// @}
 }//namespace glm
 

+ 19 - 0
glm/gtx/component_wise.inl

@@ -1,6 +1,7 @@
 /// @ref gtx_component_wise
 
 #include <limits>
+#include <cmath>
 
 namespace glm{
 namespace detail
@@ -124,4 +125,22 @@ namespace detail
 			Result = max(Result, v[i]);
 		return Result;
 	}
+
+    template<length_t L, typename T, qualifier Q, typename = typename std::enable_if<std::is_floating_point<T>::value, T>::type>
+	GLM_FUNC_QUALIFIER T fcompMin(vec<L, T, Q> const& v)
+	{
+		T Result(v[0]);
+		for(length_t i = 1, n = v.length(); i < n; ++i)
+			Result = std::fmin(Result, v[i]);
+		return Result;
+	}
+
+	template<length_t L, typename T, qualifier Q, typename = typename std::enable_if<std::is_floating_point<T>::value, T>::type>
+	GLM_FUNC_QUALIFIER T fcompMax(vec<L, T, Q> const& v)
+	{
+		T Result(v[0]);
+		for(length_t i = 1, n = v.length(); i < n; ++i)
+			Result = std::fmax(Result, v[i]);
+		return Result;
+	}
 }//namespace glm

+ 59 - 0
test/gtx/gtx_component_wise.cpp

@@ -105,12 +105,71 @@ namespace compScale
 	}
 }// compScale
 
+namespace fcompMax
+{
+	static int run()
+	{
+		int Error(0);
+
+		{
+            float const A = glm::fcompMax(glm::vec4(NAN, 0.2f, 0.5f, 1.0f));
+
+			Error += A == 1.0f ? 0 : 1;
+		}
+
+        {
+            float const A = glm::fcompMax(glm::vec4(2.0f, NAN, 0.3f, 0.7f));
+
+			Error += A == 2.0f ? 0 : 1;
+		}
+
+        {
+            float const A = glm::fcompMax(glm::vec4(NAN, NAN, NAN, NAN));
+
+			Error += std::isnan(A) ? 0 : 1;
+		}
+
+		return Error;
+	}
+}// fcompMax
+
+namespace fcompMin
+{
+	static int run()
+	{
+		int Error(0);
+
+		{
+            float const A = glm::fcompMin(glm::vec4(NAN, 0.2f, 0.5f, 1.0f));
+
+			Error += A == 0.2f ? 0 : 1;
+		}
+
+        {
+            float const A = glm::fcompMin(glm::vec4(2.0f, NAN, 0.3f, 0.7f));
+
+			Error += A == 0.3f ? 0 : 1;
+		}
+
+        {
+            float const A = glm::fcompMin(glm::vec4(NAN, NAN, NAN, NAN));
+
+			Error += std::isnan(A) ? 0 : 1;
+		}
+
+
+		return Error;
+	}
+}// fcompMin
+
 int main()
 {
 	int Error(0);
 
 	Error += compNormalize::run();
 	Error += compScale::run();
+	Error += fcompMax::run();
+	Error += fcompMin::run();
 
 	return Error;
 }