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

This commit is contained in:
Maximilian Sackel 2024-01-25 11:17:00 +01:00 committed by Christophe
parent 7b53739128
commit c9ca4dc77c
3 changed files with 88 additions and 0 deletions

View File

@ -61,6 +61,16 @@ namespace glm
template<typename genType> template<typename genType>
GLM_FUNC_DECL typename genType::value_type compMax(genType const& v); 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 }//namespace glm

View File

@ -1,6 +1,7 @@
/// @ref gtx_component_wise /// @ref gtx_component_wise
#include <limits> #include <limits>
#include <cmath>
namespace glm{ namespace glm{
namespace detail namespace detail
@ -124,4 +125,22 @@ namespace detail
Result = max(Result, v[i]); Result = max(Result, v[i]);
return Result; 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 }//namespace glm

View File

@ -105,12 +105,71 @@ namespace compScale
} }
}// 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 main()
{ {
int Error(0); int Error(0);
Error += compNormalize::run(); Error += compNormalize::run();
Error += compScale::run(); Error += compScale::run();
Error += fcompMax::run();
Error += fcompMin::run();
return Error; return Error;
} }