/////////////////////////////////////////////////////////////////////////////////// /// OpenGL Mathematics (glm.g-truc.net) /// /// Copyright (c) 2005 - 2014 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 /////////////////////////////////////////////////////////////////////////////////// #include "func_vector_relational.hpp" #include "type_vec2.hpp" #include "type_vec3.hpp" #include "type_vec4.hpp" #include "_vectorize.hpp" #include namespace glm{ namespace detail { template struct compute_abs {}; template struct compute_abs { GLM_FUNC_QUALIFIER static genFIType call(genFIType const & x) { GLM_STATIC_ASSERT( std::numeric_limits::is_iec559 || std::numeric_limits::is_signed, "'abs' only accept floating-point and integer scalar or vector inputs"); return x >= genFIType(0) ? x : -x; // TODO, perf comp with: *(((int *) &x) + 1) &= 0x7fffffff; } }; template struct compute_abs { GLM_FUNC_QUALIFIER static genFIType call(genFIType const & x) { GLM_STATIC_ASSERT( !std::numeric_limits::is_signed && std::numeric_limits::is_integer, "'abs' only accept floating-point and integer scalar or vector inputs"); return x; } }; template class vecType> struct compute_mix_vector { GLM_FUNC_QUALIFIER static vecType call(vecType const & x, vecType const & y, vecType const & a) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'mix' only accept floating-point inputs for the interpolator a"); return vecType(vecType(x) + a * vecType(y - x)); } }; template class vecType> struct compute_mix_vector { GLM_FUNC_QUALIFIER static vecType call(vecType const & x, vecType const & y, vecType const & a) { vecType Result(uninitialize); for(detail::component_count_t i = 0; i < detail::component_count(x); ++i) Result[i] = a[i] ? y[i] : x[i]; return Result; } }; template class vecType> struct compute_mix_scalar { GLM_FUNC_QUALIFIER static vecType call(vecType const & x, vecType const & y, U const & a) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'mix' only accept floating-point inputs for the interpolator a"); return vecType(vecType(x) + a * vecType(y - x)); } }; template class vecType> struct compute_mix_scalar { GLM_FUNC_QUALIFIER static vecType call(vecType const & x, vecType const & y, bool const & a) { return a ? y : x; } }; template struct compute_mix { GLM_FUNC_QUALIFIER static T call(T const & x, T const & y, U const & a) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'mix' only accept floating-point inputs for the interpolator a"); return static_cast(static_cast(x) + a * static_cast(y - x)); } }; template struct compute_mix { GLM_FUNC_QUALIFIER static T call(T const & x, T const & y, bool const & a) { return a ? y : x; } }; }//namespace detail // abs template GLM_FUNC_QUALIFIER genFIType abs(genFIType x) { return detail::compute_abs::is_signed>::call(x); } template class vecType> GLM_FUNC_QUALIFIER vecType abs(vecType const & x) { return detail::functor1::call(abs, x); } // sign //Try something like based on x >> 31 to get the sign bit template GLM_FUNC_QUALIFIER genFIType sign(genFIType x) { GLM_STATIC_ASSERT( std::numeric_limits::is_iec559 || (std::numeric_limits::is_signed && std::numeric_limits::is_integer), "'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; } template class vecType> GLM_FUNC_QUALIFIER vecType sign(vecType const & x) { return detail::functor1::call(sign, x); } // floor using ::std::floor; template class vecType> GLM_FUNC_QUALIFIER vecType floor(vecType const & x) { return detail::functor1::call(::std::floor, x); } // trunc # if GLM_LANG & GLM_LANG_CXX0X_FLAG using ::std::trunc; # else template GLM_FUNC_QUALIFIER genType trunc(genType x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'trunc' only accept floating-point inputs"); return x < static_cast(0) ? -floor(-x) : floor(x); } # endif template class vecType> GLM_FUNC_QUALIFIER vecType trunc(vecType const & x) { return detail::functor1::call(::std::trunc, x); } // round # if GLM_LANG & GLM_LANG_CXX0X_FLAG using ::std::round; # else template GLM_FUNC_QUALIFIER genType round(genType x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'round' only accept floating-point inputs"); return x < static_cast(0) ? static_cast(int(x - static_cast(0.5))) : static_cast(int(x + static_cast(0.5))); } # endif template class vecType> GLM_FUNC_QUALIFIER vecType round(vecType const & x) { return detail::functor1::call(::std::round, x); } /* // roundEven template GLM_FUNC_QUALIFIER genType roundEven(genType const& x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'roundEven' only accept floating-point inputs"); return genType(int(x + genType(int(x) % 2))); } */ // roundEven template GLM_FUNC_QUALIFIER genType roundEven(genType x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'roundEven' only accept floating-point inputs"); int Integer = static_cast(x); genType IntegerPart = static_cast(Integer); genType FractionalPart = fract(x); if(FractionalPart > static_cast(0.5) || FractionalPart < static_cast(0.5)) { return round(x); } else if((Integer % 2) == 0) { return IntegerPart; } else if(x <= static_cast(0)) // Work around... { return IntegerPart - static_cast(1); } else { return IntegerPart + static_cast(1); } //else // Bug on MinGW 4.5.2 //{ // return mix(IntegerPart + genType(-1), IntegerPart + genType(1), x <= genType(0)); //} } template class vecType> GLM_FUNC_QUALIFIER vecType roundEven(vecType const & x) { return detail::functor1::call(roundEven, x); } // ceil using ::std::ceil; template class vecType> GLM_FUNC_QUALIFIER vecType ceil(vecType const & x) { return detail::functor1::call(::std::ceil, x); } // fract template GLM_FUNC_QUALIFIER genType fract(genType x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'fract' only accept floating-point inputs"); return x - floor(x); } template class vecType> GLM_FUNC_QUALIFIER vecType fract(vecType const & x) { return detail::functor1::call(fract, x); } // mod template GLM_FUNC_QUALIFIER genType mod(genType x, genType y) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'mod' only accept floating-point inputs"); return x - y * floor(x / y); } template class vecType> GLM_FUNC_QUALIFIER vecType mod(vecType const & a, vecType const & b) { return detail::functor2::call(mod, a, b); } VECTORIZE_VEC_SCA(mod) // modf template GLM_FUNC_QUALIFIER genType modf(genType const & x, genType & i) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'modf' only accept floating-point inputs"); return std::modf(x, &i); } template GLM_FUNC_QUALIFIER tvec1 modf(tvec1 const & x, tvec1 & i) { return tvec1( modf(x.x, i.x)); } template GLM_FUNC_QUALIFIER tvec2 modf(tvec2 const & x, tvec2 & i) { return tvec2( modf(x.x, i.x), modf(x.y, i.y)); } template GLM_FUNC_QUALIFIER tvec3 modf(tvec3 const & x, tvec3 & i) { return tvec3( modf(x.x, i.x), modf(x.y, i.y), modf(x.z, i.z)); } template GLM_FUNC_QUALIFIER tvec4 modf(tvec4 const & x, tvec4 & i) { return 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 x, genType y) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'min' only accept floating-point or integer inputs"); return x < y ? x : y; } template class vecType> GLM_FUNC_QUALIFIER vecType min(vecType const & a, vecType const & b) { return detail::functor2::call(min, a, b); } VECTORIZE_VEC_SCA(min) // max template GLM_FUNC_QUALIFIER genType max(genType x, genType y) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'max' only accept floating-point or integer inputs"); return x > y ? x : y; } template class vecType> GLM_FUNC_QUALIFIER vecType max(vecType const & a, vecType const & b) { return detail::functor2::call(max, a, b); } VECTORIZE_VEC_SCA(max) // clamp template GLM_FUNC_QUALIFIER genType clamp(genType const & x, genType const & minVal, genType const & maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'clamp' only accept floating-point or integer inputs"); return min(maxVal, max(minVal, x)); } template GLM_FUNC_QUALIFIER tvec2 clamp(tvec2 const & x, T const & minVal, T const & maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'clamp' only accept floating-point or integer inputs"); return tvec2( clamp(x.x, minVal, maxVal), clamp(x.y, minVal, maxVal)); } template GLM_FUNC_QUALIFIER tvec3 clamp(tvec3 const & x, T const & minVal, T const & maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'clamp' only accept floating-point or integer inputs"); return tvec3( clamp(x.x, minVal, maxVal), clamp(x.y, minVal, maxVal), clamp(x.z, minVal, maxVal)); } template GLM_FUNC_QUALIFIER tvec4 clamp(tvec4 const & x, T const & minVal, T const & maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'clamp' only accept floating-point or integer inputs"); return 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 tvec2 clamp(tvec2 const & x, tvec2 const & minVal, tvec2 const & maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'clamp' only accept floating-point or integer inputs"); return tvec2( clamp(x.x, minVal.x, maxVal.x), clamp(x.y, minVal.y, maxVal.y)); } template GLM_FUNC_QUALIFIER tvec3 clamp(tvec3 const & x, tvec3 const & minVal, tvec3 const & maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'clamp' only accept floating-point or integer inputs"); return 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 tvec4 clamp(tvec4 const & x, tvec4 const & minVal, tvec4 const & maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'clamp' only accept floating-point or integer inputs"); return 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)); } template class vecType> GLM_FUNC_QUALIFIER vecType mix(vecType const & x, vecType const & y, vecType const & a) { return detail::compute_mix_vector::call(x, y, a); } template class vecType> GLM_FUNC_QUALIFIER vecType mix(vecType const & x, vecType const & y, U const & a) { return detail::compute_mix_scalar::call(x, y, a); } template GLM_FUNC_QUALIFIER genTypeT mix(genTypeT const & x, genTypeT const & y, genTypeU const & a) { return detail::compute_mix::call(x, y, a); } // step template GLM_FUNC_QUALIFIER genType step(genType const & edge, genType const & x) { return mix(genType(1), genType(0), glm::lessThan(x, edge)); } template