/////////////////////////////////////////////////////////////////////////////////// /// 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 { inline static genFIType call(genFIType const & x) { return x >= genFIType(0) ? x : -x; } }; template struct compute_abs { inline static genFIType call(genFIType const & x) { return x; } }; template class vecType> struct compute_mix_vector { inline static vecType call(vecType const & x, vecType const & y, vecType const & a) { return vecType(vecType(x) + a * vecType(y - x)); } }; template class vecType> struct compute_mix_vector { inline static vecType call(vecType const & x, vecType const & y, vecType const & a) { vecType Result; for(length_t i = 0; i < x.length(); ++i) Result[i] = a[i] ? y[i] : x[i]; return Result; } }; template class vecType> struct compute_mix_scalar { inline static vecType call(vecType const & x, vecType const & y, U const & a) { return vecType(vecType(x) + a * vecType(y - x)); } }; template class vecType> struct compute_mix_scalar { inline static vecType call(vecType const & x, vecType const & y, bool const & a) { return a ? y : x; } }; template struct compute_mix { inline static T call(T const & x, T const & y, U const & a) { return static_cast(static_cast(x) + a * static_cast(y - x)); } }; template struct compute_mix { inline static T call(T const & x, T const & y, bool const & a) { return a ? y : x; } }; }//namespace detail // abs template inline genFIType abs ( genFIType const & x ) { return detail::compute_abs::is_signed>::call(x); } VECTORIZE_VEC(abs) // sign //Try something like based on x >> 31 to get the sign bit template inline genFIType sign ( genFIType const & x ) { if(x > genFIType(0)) return genFIType(1); else if(x < genFIType(0)) return genFIType(-1); return genFIType(0); } VECTORIZE_VEC(sign) // floor template inline genType floor(genType const & x) { return ::std::floor(x); } VECTORIZE_VEC(floor) // trunc template inline genType trunc(genType const & x) { // TODO, add C++11 std::trunk return x < 0 ? -floor(-x) : floor(x); } VECTORIZE_VEC(trunc) // round template inline genType round(genType const& x) { // TODO, add C++11 std::round return x < 0 ? genType(int(x - genType(0.5))) : genType(int(x + genType(0.5))); } VECTORIZE_VEC(round) // roundEven template inline genType roundEven(genType const & x) { 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); if((Integer % 2) == 0) return IntegerPart; if(x <= static_cast(0)) // Work around... return IntegerPart - static_cast(1); return IntegerPart + static_cast(1); } VECTORIZE_VEC(roundEven) // ceil template inline genType ceil(genType const & x) { return ::std::ceil(x); } VECTORIZE_VEC(ceil) // fract template inline genType fract ( genType const & x ) { return x - floor(x); } VECTORIZE_VEC(fract) // mod template inline genType mod ( genType const & x, genType const & y ) { return x - y * floor(x / y); } VECTORIZE_VEC_SCA(mod) VECTORIZE_VEC_VEC(mod) // modf template inline genType modf ( genType const & x, genType & i ) { return std::modf(x, &i); } template inline detail::tvec2 modf ( detail::tvec2 const & x, detail::tvec2 & i ) { return detail::tvec2( modf(x.x, i.x), modf(x.y, i.y)); } template inline 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 inline 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)); } // min template inline genType min ( genType const & x, genType const & y ) { return x < y ? x : y; } VECTORIZE_VEC_SCA(min) VECTORIZE_VEC_VEC(min) // max template inline genType max ( genType const & x, genType const & y ) { return x > y ? x : y; } VECTORIZE_VEC_SCA(max) VECTORIZE_VEC_VEC(max) // clamp template inline genType clamp ( genType const & x, genType const & minVal, genType const & maxVal ) { return min(maxVal, max(minVal, x)); } template inline detail::tvec2 clamp ( detail::tvec2 const & x, T const & minVal, T const & maxVal ) { return detail::tvec2( clamp(x.x, minVal, maxVal), clamp(x.y, minVal, maxVal)); } template inline detail::tvec3 clamp ( detail::tvec3 const & x, T const & minVal, T const & maxVal ) { return detail::tvec3( clamp(x.x, minVal, maxVal), clamp(x.y, minVal, maxVal), clamp(x.z, minVal, maxVal)); } template inline detail::tvec4 clamp ( detail::tvec4 const & x, T const & minVal, T 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 inline 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 inline 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 inline 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)); } template class vecType> inline vecType mix ( vecType const & x, vecType const & y, vecType const & a ) { return detail::compute_mix_vector::call(x, y, a); } template class vecType> inline vecType mix ( vecType const & x, vecType const & y, U const & a ) { return detail::compute_mix_scalar::call(x, y, a); } template inline genTypeT mix ( genTypeT const & x, genTypeT const & y, genTypeU const & a ) { return detail::compute_mix::call(x, y, a); } // step template inline genType step ( genType const & edge, genType const & x ) { return mix(genType(1), genType(0), glm::lessThan(x, edge)); } template