/// @ref core /// @file glm/detail/func_geometric.inl #include "../exponential.hpp" #include "../common.hpp" #include "type_vec2.hpp" #include "type_vec4.hpp" #include "type_float.hpp" namespace glm{ namespace detail { template struct compute_length { GLM_FUNC_QUALIFIER static T call(vec const& v) { return sqrt(dot(v, v)); } }; template struct compute_distance { GLM_FUNC_QUALIFIER static T call(vec const& p0, vec const& p1) { return length(p1 - p0); } }; template struct compute_dot{}; template struct compute_dot, T, Aligned> { GLM_FUNC_QUALIFIER static T call(vec<1, T, Q> const& a, vec<1, T, Q> const& b) { return a.x * b.x; } }; template struct compute_dot, T, Aligned> { GLM_FUNC_QUALIFIER static T call(vec<2, T, Q> const& a, vec<2, T, Q> const& b) { vec<2, T, Q> tmp(a * b); return tmp.x + tmp.y; } }; template struct compute_dot, T, Aligned> { GLM_FUNC_QUALIFIER static T call(vec<3, T, Q> const& a, vec<3, T, Q> const& b) { vec<3, T, Q> tmp(a * b); return tmp.x + tmp.y + tmp.z; } }; template struct compute_dot, T, Aligned> { GLM_FUNC_QUALIFIER static T call(vec<4, T, Q> const& a, vec<4, T, Q> const& b) { vec<4, T, Q> tmp(a * b); return (tmp.x + tmp.y) + (tmp.z + tmp.w); } }; template struct compute_cross { GLM_FUNC_QUALIFIER static vec<3, T, Q> call(vec<3, T, Q> const& x, vec<3, T, Q> const& y) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'cross' accepts only floating-point inputs"); return vec<3, T, Q>( x.y * y.z - y.y * x.z, x.z * y.x - y.z * x.x, x.x * y.y - y.x * x.y); } }; template struct compute_normalize { GLM_FUNC_QUALIFIER static vec call(vec const& v) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'normalize' accepts only floating-point inputs"); return v * inversesqrt(dot(v, v)); } }; template struct compute_faceforward { GLM_FUNC_QUALIFIER static vec call(vec const& N, vec const& I, vec const& Nref) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'normalize' accepts only floating-point inputs"); return dot(Nref, I) < static_cast(0) ? N : -N; } }; template struct compute_reflect { GLM_FUNC_QUALIFIER static vec call(vec const& I, vec const& N) { return I - N * dot(N, I) * static_cast(2); } }; template struct compute_refract { GLM_FUNC_QUALIFIER static vec call(vec const& I, vec const& N, T eta) { T const dotValue(dot(N, I)); T const k(static_cast(1) - eta * eta * (static_cast(1) - dotValue * dotValue)); return (eta * I - (eta * dotValue + std::sqrt(k)) * N) * static_cast(k >= static_cast(0)); } }; }//namespace detail // length template GLM_FUNC_QUALIFIER genType length(genType x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'length' accepts only floating-point inputs"); return abs(x); } template GLM_FUNC_QUALIFIER T length(vec const& v) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'length' accepts only floating-point inputs"); return detail::compute_length::value>::call(v); } // distance template GLM_FUNC_QUALIFIER genType distance(genType const& p0, genType const& p1) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'distance' accepts only floating-point inputs"); return length(p1 - p0); } template GLM_FUNC_QUALIFIER T distance(vec const& p0, vec const& p1) { return detail::compute_distance::value>::call(p0, p1); } // dot template GLM_FUNC_QUALIFIER T dot(T x, T y) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'dot' accepts only floating-point inputs"); return x * y; } template GLM_FUNC_QUALIFIER T dot(vec const& x, vec const& y) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'dot' accepts only floating-point inputs"); return detail::compute_dot, T, detail::is_aligned::value>::call(x, y); } // cross template GLM_FUNC_QUALIFIER vec<3, T, Q> cross(vec<3, T, Q> const& x, vec<3, T, Q> const& y) { return detail::compute_cross::value>::call(x, y); } // normalize template GLM_FUNC_QUALIFIER genType normalize(genType const& x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'normalize' accepts only floating-point inputs"); return x < genType(0) ? genType(-1) : genType(1); } template GLM_FUNC_QUALIFIER vec normalize(vec const& x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'normalize' accepts only floating-point inputs"); return detail::compute_normalize::value>::call(x); } // faceforward template GLM_FUNC_QUALIFIER genType faceforward(genType const& N, genType const& I, genType const& Nref) { return dot(Nref, I) < static_cast(0) ? N : -N; } template GLM_FUNC_QUALIFIER vec faceforward(vec const& N, vec const& I, vec const& Nref) { return detail::compute_faceforward::value>::call(N, I, Nref); } // reflect template GLM_FUNC_QUALIFIER genType reflect(genType const& I, genType const& N) { return I - N * dot(N, I) * genType(2); } template GLM_FUNC_QUALIFIER vec reflect(vec const& I, vec const& N) { return detail::compute_reflect::value>::call(I, N); } // refract template GLM_FUNC_QUALIFIER genType refract(genType const& I, genType const& N, genType eta) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'refract' accepts only floating-point inputs"); genType const dotValue(dot(N, I)); genType const k(static_cast(1) - eta * eta * (static_cast(1) - dotValue * dotValue)); return (eta * I - (eta * dotValue + sqrt(k)) * N) * static_cast(k >= static_cast(0)); } template GLM_FUNC_QUALIFIER vec refract(vec const& I, vec const& N, T eta) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'refract' accepts only floating-point inputs"); return detail::compute_refract::value>::call(I, N, eta); } }//namespace glm #if GLM_ARCH != GLM_ARCH_PURE && GLM_HAS_UNRESTRICTED_UNIONS # include "func_geometric_simd.inl" #endif