///////////////////////////////////////////////////////////////////////////////////
/// OpenGL Mathematics (glm.g-truc.net)
///
/// Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///
/// @ref core
/// @file glm/core/func_common.inl
/// @date 2008-08-03 / 2011-06-15
/// @author Christophe Riccio
///////////////////////////////////////////////////////////////////////////////////
namespace glm{
namespace detail
{
template
struct Abs_
{};
template
struct Abs_
{
GLM_FUNC_QUALIFIER static genFIType get(genFIType const & x)
{
GLM_STATIC_ASSERT(
detail::type::is_float ||
detail::type::is_int, "'abs' only accept floating-point and integer inputs");
return x >= genFIType(0) ? x : -x;
// TODO, perf comp with: *(((int *) &x) + 1) &= 0x7fffffff;
}
};
template
struct Abs_
{
GLM_FUNC_QUALIFIER static genFIType get(genFIType const & x)
{
GLM_STATIC_ASSERT(
detail::type::is_uint, "'abs' only accept floating-point and integer inputs");
return x;
}
};
}//namespace detail
// abs
template
GLM_FUNC_QUALIFIER genFIType abs
(
genFIType const & x
)
{
return detail::Abs_::is_signed>::get(x);
}
VECTORIZE_VEC(abs)
// sign
//Try something like based on x >> 31 to get the sign bit
template
GLM_FUNC_QUALIFIER genFIType sign
(
genFIType const & x
)
{
GLM_STATIC_ASSERT(
detail::type::is_float ||
detail::type::is_int, "'sign' only accept signed inputs");
genFIType result;
if(x > genFIType(0))
result = genFIType(1);
else if(x < genFIType(0))
result = genFIType(-1);
else
result = genFIType(0);
return result;
}
VECTORIZE_VEC(sign)
// floor
template <>
GLM_FUNC_QUALIFIER detail::half floor(detail::half const & x)
{
return detail::half(::std::floor(float(x)));
}
template
GLM_FUNC_QUALIFIER genType floor(genType const & x)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'floor' only accept floating-point inputs");
return ::std::floor(x);
}
VECTORIZE_VEC(floor)
// trunc
template
GLM_FUNC_QUALIFIER genType trunc(genType const & x)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'trunc' only accept floating-point inputs");
return x < 0 ? -floor(-x) : floor(x);
}
VECTORIZE_VEC(trunc)
// round
template
GLM_FUNC_QUALIFIER genType round(genType const& x)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'round' only accept floating-point inputs");
if(x < 0)
return genType(int(x - genType(0.5)));
return genType(int(x + genType(0.5)));
}
VECTORIZE_VEC(round)
/*
// roundEven
template
GLM_FUNC_QUALIFIER genType roundEven(genType const& x)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'roundEven' only accept floating-point inputs");
return genType(int(x + genType(int(x) % 2)));
}
*/
// roundEven
template
GLM_FUNC_QUALIFIER genType roundEven(genType const & x)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'roundEven' only accept floating-point inputs");
int Integer = int(x);
genType IntegerPart = genType(Integer);
genType FractionalPart = fract(x);
if(FractionalPart > genType(0.5) || FractionalPart < genType(0.5))
{
return round(x);
}
else if((Integer % 2) == 0)
{
return IntegerPart;
}
else if(x <= genType(0)) // Work around...
{
return IntegerPart - 1;
}
else
{
return IntegerPart + 1;
}
//else // Bug on MinGW 4.5.2
//{
// return mix(IntegerPart + genType(-1), IntegerPart + genType(1), x <= genType(0));
//}
}
VECTORIZE_VEC(roundEven)
// ceil
template
GLM_FUNC_QUALIFIER genType ceil(genType const & x)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'ceil' only accept floating-point inputs");
return ::std::ceil(x);
}
VECTORIZE_VEC(ceil)
// fract
template
GLM_FUNC_QUALIFIER genType fract
(
genType const & x
)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'fract' only accept floating-point inputs");
return x - ::std::floor(x);
}
VECTORIZE_VEC(fract)
// mod
template
GLM_FUNC_QUALIFIER genType mod
(
genType const & x,
genType const & y
)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'mod' only accept floating-point inputs");
return x - y * floor(x / y);
}
VECTORIZE_VEC_SCA(mod)
VECTORIZE_VEC_VEC(mod)
// modf
template
GLM_FUNC_QUALIFIER genType modf
(
genType const & x,
genType & i
)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'modf' only accept floating-point inputs");
return std::modf(x, &i);
}
template
GLM_FUNC_QUALIFIER detail::tvec2 modf
(
detail::tvec2 const & x,
detail::tvec2 & i
)
{
return detail::tvec2(
modf(x.x, i.x),
modf(x.y, i.y));
}
template
GLM_FUNC_QUALIFIER detail::tvec3 modf
(
detail::tvec3 const & x,
detail::tvec3 & i
)
{
return detail::tvec3(
modf(x.x, i.x),
modf(x.y, i.y),
modf(x.z, i.z));
}
template
GLM_FUNC_QUALIFIER detail::tvec4 modf
(
detail::tvec4 const & x,
detail::tvec4 & i
)
{
return detail::tvec4(
modf(x.x, i.x),
modf(x.y, i.y),
modf(x.z, i.z),
modf(x.w, i.w));
}
//// Only valid if (INT_MIN <= x-y <= INT_MAX)
//// min(x,y)
//r = y + ((x - y) & ((x - y) >> (sizeof(int) *
//CHAR_BIT - 1)));
//// max(x,y)
//r = x - ((x - y) & ((x - y) >> (sizeof(int) *
//CHAR_BIT - 1)));
// min
template
GLM_FUNC_QUALIFIER genType min
(
genType const & x,
genType const & y
)
{
GLM_STATIC_ASSERT(
detail::type::is_float ||
detail::type::is_int ||
detail::type::is_uint, "'min' only accept numbers");
return x < y ? x : y;
}
VECTORIZE_VEC_SCA(min)
VECTORIZE_VEC_VEC(min)
// max
template
GLM_FUNC_QUALIFIER genType max
(
genType const & x,
genType const & y
)
{
GLM_STATIC_ASSERT(
detail::type::is_float ||
detail::type::is_int ||
detail::type::is_uint, "'max' only accept numbers");
return x > y ? x : y;
}
VECTORIZE_VEC_SCA(max)
VECTORIZE_VEC_VEC(max)
// clamp
template
GLM_FUNC_QUALIFIER valType clamp
(
valType const & x,
valType const & minVal,
valType const & maxVal
)
{
GLM_STATIC_ASSERT(
detail::type::is_float ||
detail::type::is_int ||
detail::type::is_uint, "'clamp' only accept numbers");
return min(maxVal, max(minVal, x));
}
template
GLM_FUNC_QUALIFIER detail::tvec2 clamp
(
detail::tvec2 const & x,
typename detail::tvec2::value_type const & minVal,
typename detail::tvec2::value_type const & maxVal
)
{
return detail::tvec2(
clamp(x.x, minVal, maxVal),
clamp(x.y, minVal, maxVal));
}
template
GLM_FUNC_QUALIFIER detail::tvec3 clamp
(
detail::tvec3 const & x,
typename detail::tvec3::value_type const & minVal,
typename detail::tvec3::value_type const & maxVal
)
{
return detail::tvec3(
clamp(x.x, minVal, maxVal),
clamp(x.y, minVal, maxVal),
clamp(x.z, minVal, maxVal));
}
template
GLM_FUNC_QUALIFIER detail::tvec4 clamp
(
detail::tvec4 const & x,
typename detail::tvec4::value_type const & minVal,
typename detail::tvec4::value_type const & maxVal
)
{
return detail::tvec4(
clamp(x.x, minVal, maxVal),
clamp(x.y, minVal, maxVal),
clamp(x.z, minVal, maxVal),
clamp(x.w, minVal, maxVal));
}
template
GLM_FUNC_QUALIFIER detail::tvec2 clamp
(
detail::tvec2 const & x,
detail::tvec2 const & minVal,
detail::tvec2 const & maxVal
)
{
return detail::tvec2(
clamp(x.x, minVal.x, maxVal.x),
clamp(x.y, minVal.y, maxVal.y));
}
template
GLM_FUNC_QUALIFIER detail::tvec3 clamp
(
detail::tvec3 const & x,
detail::tvec3 const & minVal,
detail::tvec3 const & maxVal
)
{
return detail::tvec3(
clamp(x.x, minVal.x, maxVal.x),
clamp(x.y, minVal.y, maxVal.y),
clamp(x.z, minVal.z, maxVal.z));
}
template
GLM_FUNC_QUALIFIER detail::tvec4 clamp
(
detail::tvec4 const & x,
detail::tvec4 const & minVal,
detail::tvec4 const & maxVal
)
{
return detail::tvec4(
clamp(x.x, minVal.x, maxVal.x),
clamp(x.y, minVal.y, maxVal.y),
clamp(x.z, minVal.z, maxVal.z),
clamp(x.w, minVal.w, maxVal.w));
}
// mix
template
GLM_FUNC_QUALIFIER genType mix
(
genType const & x,
genType const & y,
genType const & a
)
{
GLM_STATIC_ASSERT(detail::type::is_float , "'genType' is not floating-point type");
return x + a * (y - x);
}
template
GLM_FUNC_QUALIFIER detail::tvec2 mix
(
detail::tvec2 const & x,
detail::tvec2 const & y,
valType const & a
)
{
GLM_STATIC_ASSERT(detail::type::is_float , "'genType' is not floating-point type");
return x + a * (y - x);
}
template
GLM_FUNC_QUALIFIER detail::tvec3 mix
(
detail::tvec3 const & x,
detail::tvec3 const & y,
valType const & a
)
{
return x + a * (y - x);
}
template
GLM_FUNC_QUALIFIER detail::tvec4 mix
(
detail::tvec4 const & x,
detail::tvec4 const & y,
valType const & a
)
{
return x + a * (y - x);
}
template
GLM_FUNC_QUALIFIER detail::tvec2 mix
(
detail::tvec2 const & x,
detail::tvec2 const & y,
detail::tvec2 const & a
)
{
return x + a * (y - x);
}
template
GLM_FUNC_QUALIFIER detail::tvec3 mix
(
detail::tvec3 const & x,
detail::tvec3 const & y,
detail::tvec3 const & a
)
{
GLM_STATIC_ASSERT(detail::type::is_float , "'genType' is not floating-point type");
return x + a * (y - x);
}
template
GLM_FUNC_QUALIFIER detail::tvec4 mix
(
detail::tvec4 const & x,
detail::tvec4 const & y,
detail::tvec4 const & a
)
{
return x + a * (y - x);
}
//template
//GLM_FUNC_QUALIFIER genTypeT mix
//(
// genTypeT const & x,
// genTypeT const & y,
// float const & a
//)
//{
// // It could be a vector too
// //GLM_STATIC_ASSERT(
// // detail::type::is_float &&
// // detail::type::is_float);
// return x + a * (y - x);
//}
template <>
GLM_FUNC_QUALIFIER float mix
(
float const & x,
float const & y,
bool const & a
)
{
return a ? y : x;
}
template <>
GLM_FUNC_QUALIFIER double mix
(
double const & x,
double const & y,
bool const & a
)
{
return a ? y : x;
}
template
GLM_FUNC_QUALIFIER detail::tvec2 mix
(
detail::tvec2 const & x,
detail::tvec2 const & y,
bool a
)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'mix' only accept floating-point inputs");
return a ? y : x;
}
template
GLM_FUNC_QUALIFIER detail::tvec3 mix
(
detail::tvec3 const & x,
detail::tvec3 const & y,
bool a
)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'mix' only accept floating-point inputs");
return a ? y : x;
}
template
GLM_FUNC_QUALIFIER detail::tvec4 mix
(
detail::tvec4 const & x,
detail::tvec4 const & y,
bool a
)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'mix' only accept floating-point inputs");
return a ? y : x;
}
template
GLM_FUNC_QUALIFIER detail::tvec2 mix
(
detail::tvec2 const & x,
detail::tvec2 const & y,
typename detail::tvec2::bool_type a
)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'mix' only accept floating-point inputs");
detail::tvec2 result;
for
(
typename detail::tvec2::size_type i = 0;
i < x.length(); ++i
)
{
result[i] = a[i] ? y[i] : x[i];
}
return result;
}
template
GLM_FUNC_QUALIFIER detail::tvec3 mix
(
detail::tvec3 const & x,
detail::tvec3 const & y,
typename detail::tvec3::bool_type a
)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'mix' only accept floating-point inputs");
detail::tvec3 result;
for
(
typename detail::tvec3::size_type i = 0;
i < x.length(); ++i
)
{
result[i] = a[i] ? y[i] : x[i];
}
return result;
}
template
GLM_FUNC_QUALIFIER detail::tvec4 mix
(
detail::tvec4 const & x,
detail::tvec4 const & y,
typename detail::tvec4::bool_type a
)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'mix' only accept floating-point inputs");
detail::tvec4 result;
for
(
typename detail::tvec4::size_type i = 0;
i < x.length(); ++i
)
{
result[i] = a[i] ? y[i] : x[i];
}
return result;
}
// step
template
GLM_FUNC_QUALIFIER genType step
(
genType const & edge,
genType const & x
)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'step' only accept floating-point inputs");
return x < edge ? genType(0) : genType(1);
}
template
GLM_FUNC_QUALIFIER detail::tvec2 step
(
typename detail::tvec2::value_type const & edge,
detail::tvec2 const & x
)
{
return detail::tvec2(
x.x < edge ? T(0) : T(1),
x.y < edge ? T(0) : T(1));
}
template
GLM_FUNC_QUALIFIER detail::tvec3 step
(
typename detail::tvec3::value_type const & edge,
detail::tvec3 const & x
)
{
return detail::tvec3(
x.x < edge ? T(0) : T(1),
x.y < edge ? T(0) : T(1),
x.z < edge ? T(0) : T(1));
}
template
GLM_FUNC_QUALIFIER detail::tvec4 step
(
typename detail::tvec4::value_type const & edge,
detail::tvec4 const & x
)
{
return detail::tvec4(
x.x < edge ? T(0) : T(1),
x.y < edge ? T(0) : T(1),
x.z < edge ? T(0) : T(1),
x.w < edge ? T(0) : T(1));
}
template
GLM_FUNC_QUALIFIER detail::tvec2 step
(
detail::tvec2 const & edge,
detail::tvec2 const & x
)
{
return detail::tvec2(
x.x < edge.x ? T(0) : T(1),
x.y < edge.y ? T(0) : T(1));
}
template
GLM_FUNC_QUALIFIER detail::tvec3 step
(
detail::tvec3 const & edge,
detail::tvec3 const & x
)
{
return detail::tvec3(
x.x < edge.x ? T(0) : T(1),
x.y < edge.y ? T(0) : T(1),
x.z < edge.z ? T(0) : T(1));
}
template
GLM_FUNC_QUALIFIER detail::tvec4 step
(
detail::tvec4 const & edge,
detail::tvec4 const & x
)
{
return detail::tvec4(
x.x < edge.x ? T(0) : T(1),
x.y < edge.y ? T(0) : T(1),
x.z < edge.z ? T(0) : T(1),
x.w < edge.w ? T(0) : T(1));
}
// smoothstep
template
GLM_FUNC_QUALIFIER genType smoothstep
(
genType const & edge0,
genType const & edge1,
genType const & x
)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'smoothstep' only accept floating-point inputs");
genType tmp = clamp((x - edge0) / (edge1 - edge0), genType(0), genType(1));
return tmp * tmp * (genType(3) - genType(2) * tmp);
}
template
GLM_FUNC_QUALIFIER detail::tvec2 smoothstep
(
typename detail::tvec2::value_type const & edge0,
typename detail::tvec2::value_type const & edge1,
detail::tvec2 const & x
)
{
return detail::tvec2(
smoothstep(edge0, edge1, x.x),
smoothstep(edge0, edge1, x.y));
}
template
GLM_FUNC_QUALIFIER detail::tvec3 smoothstep
(
typename detail::tvec3::value_type const & edge0,
typename detail::tvec3::value_type const & edge1,
detail::tvec3 const & x
)
{
return detail::tvec3(
smoothstep(edge0, edge1, x.x),
smoothstep(edge0, edge1, x.y),
smoothstep(edge0, edge1, x.z));
}
template
GLM_FUNC_QUALIFIER detail::tvec4 smoothstep
(
typename detail::tvec4::value_type const & edge0,
typename detail::tvec4::value_type const & edge1,
detail::tvec4 const & x
)
{
return detail::tvec4(
smoothstep(edge0, edge1, x.x),
smoothstep(edge0, edge1, x.y),
smoothstep(edge0, edge1, x.z),
smoothstep(edge0, edge1, x.w));
}
template
GLM_FUNC_QUALIFIER detail::tvec2 smoothstep
(
detail::tvec2 const & edge0,
detail::tvec2 const & edge1,
detail::tvec2 const & x
)
{
return detail::tvec2(
smoothstep(edge0.x, edge1.x, x.x),
smoothstep(edge0.y, edge1.y, x.y));
}
template
GLM_FUNC_QUALIFIER detail::tvec3 smoothstep
(
detail::tvec3 const & edge0,
detail::tvec3 const & edge1,
detail::tvec3 const & x
)
{
return detail::tvec3(
smoothstep(edge0.x, edge1.x, x.x),
smoothstep(edge0.y, edge1.y, x.y),
smoothstep(edge0.z, edge1.z, x.z));
}
template
GLM_FUNC_QUALIFIER detail::tvec4 smoothstep
(
detail::tvec4 const & edge0,
detail::tvec4 const & edge1,
detail::tvec4 const & x
)
{
return detail::tvec4(
smoothstep(edge0.x, edge1.x, x.x),
smoothstep(edge0.y, edge1.y, x.y),
smoothstep(edge0.z, edge1.z, x.z),
smoothstep(edge0.w, edge1.w, x.w));
}
// TODO: Not working on MinGW...
template
GLM_FUNC_QUALIFIER bool isnan(genType const & x)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'isnan' only accept floating-point inputs");
# if(GLM_COMPILER & (GLM_COMPILER_VC | GLM_COMPILER_INTEL))
return _isnan(x) != 0;
# elif(GLM_COMPILER & (GLM_COMPILER_GCC | GLM_COMPILER_CLANG))
# if(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
return _isnan(x) != 0;
# else
return std::isnan(x);
# endif
# elif(GLM_COMPILER & GLM_COMPILER_CUDA)
return isnan(x) != 0;
# else
return std::isnan(x);
# endif
}
template
GLM_FUNC_QUALIFIER typename detail::tvec2::bool_type isnan
(
detail::tvec2 const & x
)
{
return typename detail::tvec2::bool_type(
isnan(x.x),
isnan(x.y));
}
template
GLM_FUNC_QUALIFIER typename detail::tvec3::bool_type isnan
(
detail::tvec3 const & x
)
{
return typename detail::tvec3::bool_type(
isnan(x.x),
isnan(x.y),
isnan(x.z));
}
template
GLM_FUNC_QUALIFIER typename detail::tvec4::bool_type isnan
(
detail::tvec4 const & x
)
{
return typename detail::tvec4::bool_type(
isnan(x.x),
isnan(x.y),
isnan(x.z),
isnan(x.w));
}
template
GLM_FUNC_QUALIFIER bool isinf(
genType const & x)
{
GLM_STATIC_ASSERT(detail::type::is_float, "'isinf' only accept floating-point inputs");
# if(GLM_COMPILER & (GLM_COMPILER_INTEL | GLM_COMPILER_VC))
return _fpclass(x) == _FPCLASS_NINF || _fpclass(x) == _FPCLASS_PINF;
# elif(GLM_COMPILER & (GLM_COMPILER_GCC | GLM_COMPILER_CLANG))
# if(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
return _isinf(x) != 0;
# else
return std::isinf(x);
# endif
# elif(GLM_COMPILER & GLM_COMPILER_CUDA)
// http://developer.download.nvidia.com/compute/cuda/4_2/rel/toolkit/docs/online/group__CUDA__MATH__DOUBLE_g13431dd2b40b51f9139cbb7f50c18fab.html#g13431dd2b40b51f9139cbb7f50c18fab
return isinf(double(x)) != 0;
# else
return std::isinf(x);
# endif
}
template
GLM_FUNC_QUALIFIER typename detail::tvec2