Skip to content

Commit

Permalink
Implementing fcompMin / fcompMax, closes #1215
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian Sackel authored and christophe-lunarg committed Feb 7, 2024
1 parent 7b53739 commit c9ca4dc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
10 changes: 10 additions & 0 deletions glm/gtx/component_wise.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions glm/gtx/component_wise.inl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// @ref gtx_component_wise

#include <limits>
#include <cmath>

namespace glm{
namespace detail
Expand Down Expand Up @@ -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 changes: 59 additions & 0 deletions test/gtx/gtx_component_wise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit c9ca4dc

Please sign in to comment.