Fixed mix function for bool and bvec* type third parameter, issue #59

This commit is contained in:
Christophe Riccio 2013-03-17 14:03:37 +01:00
parent 356e70e653
commit dab66f81e5
6 changed files with 142 additions and 74 deletions

View File

@ -220,7 +220,7 @@ namespace glm
/// ///
/// If genTypeU is a boolean scalar or vector: /// If genTypeU is a boolean scalar or vector:
/// Selects which vector each returned component comes /// Selects which vector each returned component comes
/// from. For a component of a that is false, the /// from. For a component of <a> that is false, the
/// corresponding component of x is returned. For a /// corresponding component of x is returned. For a
/// component of a that is true, the corresponding /// component of a that is true, the corresponding
/// component of y is returned. Components of x and y that /// component of y is returned. Components of x and y that

View File

@ -420,93 +420,87 @@ namespace detail
} }
// mix // mix
template <typename genTypeT, typename genTypeU> template <typename genType>
GLM_FUNC_QUALIFIER genTypeT mix GLM_FUNC_QUALIFIER genType mix
( (
genTypeT const & x, genType const & x,
genTypeT const & y, genType const & y,
genTypeU const & a genType const & a
) )
{ {
// It could be a vector too GLM_STATIC_ASSERT(detail::type<genType>::is_float , "'genType' is not floating-point type");
//GLM_STATIC_ASSERT(
// detail::type<genTypeT>::is_float &&
// detail::type<genTypeU>::is_float);
//return x + a * (y - x); return x + a * (y - x);
return genTypeT(genTypeU(x) + a * genTypeU(y - x));
} }
template <typename valTypeA, typename valTypeB> template <typename valType>
GLM_FUNC_QUALIFIER detail::tvec2<valTypeA> mix GLM_FUNC_QUALIFIER detail::tvec2<valType> mix
( (
detail::tvec2<valTypeA> const & x, detail::tvec2<valType> const & x,
detail::tvec2<valTypeA> const & y, detail::tvec2<valType> const & y,
valTypeB const & a valType const & a
) )
{ {
return detail::tvec2<valTypeA>( GLM_STATIC_ASSERT(detail::type<valType>::is_float , "'genType' is not floating-point type");
detail::tvec2<valTypeB>(x) + a * detail::tvec2<valTypeB>(y - x));
return x + a * (y - x);
} }
template <typename valTypeA, typename valTypeB> template <typename valType>
GLM_FUNC_QUALIFIER detail::tvec3<valTypeA> mix GLM_FUNC_QUALIFIER detail::tvec3<valType> mix
( (
detail::tvec3<valTypeA> const & x, detail::tvec3<valType> const & x,
detail::tvec3<valTypeA> const & y, detail::tvec3<valType> const & y,
valTypeB const & a valType const & a
) )
{ {
return detail::tvec3<valTypeA>( return x + a * (y - x);
detail::tvec3<valTypeB>(x) + a * detail::tvec3<valTypeB>(y - x));
} }
template <typename valTypeA, typename valTypeB> template <typename valType>
GLM_FUNC_QUALIFIER detail::tvec4<valTypeA> mix GLM_FUNC_QUALIFIER detail::tvec4<valType> mix
( (
detail::tvec4<valTypeA> const & x, detail::tvec4<valType> const & x,
detail::tvec4<valTypeA> const & y, detail::tvec4<valType> const & y,
valTypeB const & a valType const & a
) )
{ {
return detail::tvec4<valTypeA>( return x + a * (y - x);
detail::tvec4<valTypeB>(x) + a * detail::tvec4<valTypeB>(y - x));
} }
template <typename valTypeA, typename valTypeB> template <typename valType>
GLM_FUNC_QUALIFIER detail::tvec2<valTypeA> mix GLM_FUNC_QUALIFIER detail::tvec2<valType> mix
( (
detail::tvec2<valTypeA> const & x, detail::tvec2<valType> const & x,
detail::tvec2<valTypeA> const & y, detail::tvec2<valType> const & y,
detail::tvec2<valTypeB> const & a detail::tvec2<valType> const & a
) )
{ {
return detail::tvec2<valTypeA>( return x + a * (y - x);
detail::tvec2<valTypeB>(x) + a * detail::tvec2<valTypeB>(y - x));
} }
template <typename valTypeA, typename valTypeB> template <typename valType>
GLM_FUNC_QUALIFIER detail::tvec3<valTypeA> mix GLM_FUNC_QUALIFIER detail::tvec3<valType> mix
( (
detail::tvec3<valTypeA> const & x, detail::tvec3<valType> const & x,
detail::tvec3<valTypeA> const & y, detail::tvec3<valType> const & y,
detail::tvec3<valTypeB> const & a detail::tvec3<valType> const & a
) )
{ {
return detail::tvec3<valTypeA>( GLM_STATIC_ASSERT(detail::type<valType>::is_float , "'genType' is not floating-point type");
detail::tvec3<valTypeB>(x) + a * detail::tvec3<valTypeB>(y - x));
return x + a * (y - x);
} }
template <typename valTypeA, typename valTypeB> template <typename valType>
GLM_FUNC_QUALIFIER detail::tvec4<valTypeA> mix GLM_FUNC_QUALIFIER detail::tvec4<valType> mix
( (
detail::tvec4<valTypeA> const & x, detail::tvec4<valType> const & x,
detail::tvec4<valTypeA> const & y, detail::tvec4<valType> const & y,
detail::tvec4<valTypeB> const & a detail::tvec4<valType> const & a
) )
{ {
return detail::tvec4<valTypeA>( return x + a * (y - x);
detail::tvec4<valTypeB>(x) + a * detail::tvec4<valTypeB>(y - x));
} }
//template <typename genTypeT> //template <typename genTypeT>
@ -525,15 +519,63 @@ namespace detail
// return x + a * (y - x); // return x + a * (y - x);
//} //}
template <typename genType> template <>
GLM_FUNC_QUALIFIER genType mix GLM_FUNC_QUALIFIER float mix
( (
genType const & x, float const & x,
genType const & y, float const & y,
bool const & a bool const & a
) )
{ {
GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'mix' only accept floating-point inputs"); return a ? y : x;
}
template <>
GLM_FUNC_QUALIFIER double mix
(
double const & x,
double const & y,
bool const & a
)
{
return a ? y : x;
}
template <typename T>
GLM_FUNC_QUALIFIER detail::tvec2<T> mix
(
detail::tvec2<T> const & x,
detail::tvec2<T> const & y,
bool a
)
{
GLM_STATIC_ASSERT(detail::type<T>::is_float, "'mix' only accept floating-point inputs");
return a ? y : x;
}
template <typename T>
GLM_FUNC_QUALIFIER detail::tvec3<T> mix
(
detail::tvec3<T> const & x,
detail::tvec3<T> const & y,
bool a
)
{
GLM_STATIC_ASSERT(detail::type<T>::is_float, "'mix' only accept floating-point inputs");
return a ? y : x;
}
template <typename T>
GLM_FUNC_QUALIFIER detail::tvec4<T> mix
(
detail::tvec4<T> const & x,
detail::tvec4<T> const & y,
bool a
)
{
GLM_STATIC_ASSERT(detail::type<T>::is_float, "'mix' only accept floating-point inputs");
return a ? y : x; return a ? y : x;
} }
@ -552,8 +594,7 @@ namespace detail
for for
( (
typename detail::tvec2<T>::size_type i = 0; typename detail::tvec2<T>::size_type i = 0;
i < detail::tvec2<T>::value_size(); i < x.length(); ++i
++i
) )
{ {
result[i] = a[i] ? y[i] : x[i]; result[i] = a[i] ? y[i] : x[i];
@ -575,8 +616,7 @@ namespace detail
for for
( (
typename detail::tvec3<T>::size_type i = 0; typename detail::tvec3<T>::size_type i = 0;
i < detail::tvec3<T>::value_size(); i < x.length(); ++i
++i
) )
{ {
result[i] = a[i] ? y[i] : x[i]; result[i] = a[i] ? y[i] : x[i];
@ -598,8 +638,7 @@ namespace detail
for for
( (
typename detail::tvec4<T>::size_type i = 0; typename detail::tvec4<T>::size_type i = 0;
i < detail::tvec4<T>::value_size(); i < x.length(); ++i
++i
) )
{ {
result[i] = a[i] ? y[i] : x[i]; result[i] = a[i] ? y[i] : x[i];

View File

@ -432,6 +432,20 @@ namespace detail
/// @see gtc_half_float /// @see gtc_half_float
hvec4 abs(hvec4 const & x); hvec4 abs(hvec4 const & x);
/// Selects which vector each returned component comes
/// from. For a component of <a> that is false, the
/// corresponding component of x is returned. For a
/// component of a that is true, the corresponding
/// component of y is returned. Components of x and y that
/// are not selected are allowed to be invalid floating point
/// values and will have no effect on the results. Thus, this
/// provides different functionality than
/// genType mix(genType x, genType y, genType(a))
/// where a is a Boolean vector.
///
/// @see gtc_half_float
half mix(half const & x, half const & y, bool const & a);
/// @} /// @}
}// namespace glm }// namespace glm

View File

@ -1036,4 +1036,15 @@ namespace detail
float(v.w) >= float(0) ? v.w : -v.w); float(v.w) >= float(0) ? v.w : -v.w);
} }
template <>
GLM_FUNC_QUALIFIER glm::half mix
(
glm::half const & x,
glm::half const & y,
bool const & a
)
{
return a ? y : x;
}
}//namespace glm }//namespace glm

View File

@ -47,7 +47,8 @@ GLM 0.9.4.3: 2013-03-17
- Added a docx copy of the manual - Added a docx copy of the manual
- Fixed GLM_GTX_matrix_interpolation - Fixed GLM_GTX_matrix_interpolation
- Fixed isnan and isinf on Android with Clang - Fixed isnan and isinf on Android with Clang
- Autodetected C++ version using __cplusplus value
- Fixed mix for bool and bvec* third parameter
================================================================================ ================================================================================
GLM 0.9.4.2: 2013-02-14 GLM 0.9.4.2: 2013-02-14

View File

@ -189,7 +189,8 @@ namespace test_mix
{glm::vec2(0.0f), glm::vec2(1.0f), glm::bvec2(false), glm::vec2(0.0f)}, {glm::vec2(0.0f), glm::vec2(1.0f), glm::bvec2(false), glm::vec2(0.0f)},
{glm::vec2(0.0f), glm::vec2(1.0f), glm::bvec2(true), glm::vec2(1.0f)}, {glm::vec2(0.0f), glm::vec2(1.0f), glm::bvec2(true), glm::vec2(1.0f)},
{glm::vec2(-1.0f), glm::vec2(1.0f), glm::bvec2(false), glm::vec2(-1.0f)}, {glm::vec2(-1.0f), glm::vec2(1.0f), glm::bvec2(false), glm::vec2(-1.0f)},
{glm::vec2(-1.0f), glm::vec2(1.0f), glm::bvec2(true), glm::vec2(1.0f)} {glm::vec2(-1.0f), glm::vec2(1.0f), glm::bvec2(true), glm::vec2(1.0f)},
{glm::vec2(-1.0f), glm::vec2(1.0f), glm::bvec2(true, false), glm::vec2(1.0f, -1.0f)}
}; };
test<glm::vec3, bool> TestVec3Bool[] = test<glm::vec3, bool> TestVec3Bool[] =
@ -205,7 +206,8 @@ namespace test_mix
{glm::vec3(0.0f), glm::vec3(1.0f), glm::bvec3(false), glm::vec3(0.0f)}, {glm::vec3(0.0f), glm::vec3(1.0f), glm::bvec3(false), glm::vec3(0.0f)},
{glm::vec3(0.0f), glm::vec3(1.0f), glm::bvec3(true), glm::vec3(1.0f)}, {glm::vec3(0.0f), glm::vec3(1.0f), glm::bvec3(true), glm::vec3(1.0f)},
{glm::vec3(-1.0f), glm::vec3(1.0f), glm::bvec3(false), glm::vec3(-1.0f)}, {glm::vec3(-1.0f), glm::vec3(1.0f), glm::bvec3(false), glm::vec3(-1.0f)},
{glm::vec3(-1.0f), glm::vec3(1.0f), glm::bvec3(true), glm::vec3(1.0f)} {glm::vec3(-1.0f), glm::vec3(1.0f), glm::bvec3(true), glm::vec3(1.0f)},
{glm::vec3(1.0f, 2.0f, 3.0f), glm::vec3(4.0f, 5.0f, 6.0f), glm::bvec3(true, false, true), glm::vec3(4.0f, 2.0f, 6.0f)}
}; };
test<glm::vec4, bool> TestVec4Bool[] = test<glm::vec4, bool> TestVec4Bool[] =
@ -221,7 +223,8 @@ namespace test_mix
{glm::vec4(0.0f), glm::vec4(1.0f), glm::bvec4(false), glm::vec4(0.0f)}, {glm::vec4(0.0f), glm::vec4(1.0f), glm::bvec4(false), glm::vec4(0.0f)},
{glm::vec4(0.0f), glm::vec4(1.0f), glm::bvec4(true), glm::vec4(1.0f)}, {glm::vec4(0.0f), glm::vec4(1.0f), glm::bvec4(true), glm::vec4(1.0f)},
{glm::vec4(-1.0f), glm::vec4(1.0f), glm::bvec4(false), glm::vec4(-1.0f)}, {glm::vec4(-1.0f), glm::vec4(1.0f), glm::bvec4(false), glm::vec4(-1.0f)},
{glm::vec4(-1.0f), glm::vec4(1.0f), glm::bvec4(true), glm::vec4(1.0f)} {glm::vec4(-1.0f), glm::vec4(1.0f), glm::bvec4(true), glm::vec4(1.0f)},
{glm::vec4(1.0f, 2.0f, 3.0f, 4.0f), glm::vec4(5.0f, 6.0f, 7.0f, 8.0f), glm::bvec4(true, false, true, false), glm::vec4(5.0f, 2.0f, 7.0f, 4.0f)}
}; };
int run() int run()
@ -292,7 +295,7 @@ namespace test_mix
{ {
for(std::size_t i = 0; i < sizeof(TestVec4Bool) / sizeof(test<glm::vec4, bool>); ++i) for(std::size_t i = 0; i < sizeof(TestVec4Bool) / sizeof(test<glm::vec4, bool>); ++i)
{ {
glm::vec3 Result = glm::mix(TestVec4Bool[i].x, TestVec4Bool[i].y, TestVec4Bool[i].a); glm::vec4 Result = glm::mix(TestVec4Bool[i].x, TestVec4Bool[i].y, TestVec4Bool[i].a);
Error += glm::epsilonEqual(Result.x, TestVec4Bool[i].Result.x, glm::epsilon<float>()) ? 0 : 1; Error += glm::epsilonEqual(Result.x, TestVec4Bool[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
Error += glm::epsilonEqual(Result.y, TestVec4Bool[i].Result.y, glm::epsilon<float>()) ? 0 : 1; Error += glm::epsilonEqual(Result.y, TestVec4Bool[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
Error += glm::epsilonEqual(Result.z, TestVec4Bool[i].Result.z, glm::epsilon<float>()) ? 0 : 1; Error += glm::epsilonEqual(Result.z, TestVec4Bool[i].Result.z, glm::epsilon<float>()) ? 0 : 1;
@ -305,10 +308,10 @@ namespace test_mix
for(std::size_t i = 0; i < sizeof(TestBVec4) / sizeof(test<glm::vec4, glm::bvec4>); ++i) for(std::size_t i = 0; i < sizeof(TestBVec4) / sizeof(test<glm::vec4, glm::bvec4>); ++i)
{ {
glm::vec4 Result = glm::mix(TestBVec4[i].x, TestBVec4[i].y, TestBVec4[i].a); glm::vec4 Result = glm::mix(TestBVec4[i].x, TestBVec4[i].y, TestBVec4[i].a);
Error += glm::epsilonEqual(Result.x, TestBVec3[i].Result.x, glm::epsilon<float>()) ? 0 : 1; Error += glm::epsilonEqual(Result.x, TestBVec4[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
Error += glm::epsilonEqual(Result.y, TestBVec3[i].Result.y, glm::epsilon<float>()) ? 0 : 1; Error += glm::epsilonEqual(Result.y, TestBVec4[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
Error += glm::epsilonEqual(Result.z, TestBVec3[i].Result.z, glm::epsilon<float>()) ? 0 : 1; Error += glm::epsilonEqual(Result.z, TestBVec4[i].Result.z, glm::epsilon<float>()) ? 0 : 1;
Error += glm::epsilonEqual(Result.w, TestBVec3[i].Result.w, glm::epsilon<float>()) ? 0 : 1; Error += glm::epsilonEqual(Result.w, TestBVec4[i].Result.w, glm::epsilon<float>()) ? 0 : 1;
} }
} }