| 1 | /// @ref gtx_component_wise |
| 2 | |
| 3 | #include <limits> |
| 4 | |
| 5 | namespace glm{ |
| 6 | namespace detail |
| 7 | { |
| 8 | template<length_t L, typename T, typename floatType, qualifier Q, bool isInteger, bool signedType> |
| 9 | struct compute_compNormalize |
| 10 | {}; |
| 11 | |
| 12 | template<length_t L, typename T, typename floatType, qualifier Q> |
| 13 | struct compute_compNormalize<L, T, floatType, Q, true, true> |
| 14 | { |
| 15 | GLM_FUNC_QUALIFIER static vec<L, floatType, Q> call(vec<L, T, Q> const& v) |
| 16 | { |
| 17 | floatType const Min = static_cast<floatType>(std::numeric_limits<T>::min()); |
| 18 | floatType const Max = static_cast<floatType>(std::numeric_limits<T>::max()); |
| 19 | return (vec<L, floatType, Q>(v) - Min) / (Max - Min) * static_cast<floatType>(2) - static_cast<floatType>(1); |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | template<length_t L, typename T, typename floatType, qualifier Q> |
| 24 | struct compute_compNormalize<L, T, floatType, Q, true, false> |
| 25 | { |
| 26 | GLM_FUNC_QUALIFIER static vec<L, floatType, Q> call(vec<L, T, Q> const& v) |
| 27 | { |
| 28 | return vec<L, floatType, Q>(v) / static_cast<floatType>(std::numeric_limits<T>::max()); |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | template<length_t L, typename T, typename floatType, qualifier Q> |
| 33 | struct compute_compNormalize<L, T, floatType, Q, false, true> |
| 34 | { |
| 35 | GLM_FUNC_QUALIFIER static vec<L, floatType, Q> call(vec<L, T, Q> const& v) |
| 36 | { |
| 37 | return v; |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | template<length_t L, typename T, typename floatType, qualifier Q, bool isInteger, bool signedType> |
| 42 | struct compute_compScale |
| 43 | {}; |
| 44 | |
| 45 | template<length_t L, typename T, typename floatType, qualifier Q> |
| 46 | struct compute_compScale<L, T, floatType, Q, true, true> |
| 47 | { |
| 48 | GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, floatType, Q> const& v) |
| 49 | { |
| 50 | floatType const Max = static_cast<floatType>(std::numeric_limits<T>::max()) + static_cast<floatType>(0.5); |
| 51 | vec<L, floatType, Q> const Scaled(v * Max); |
| 52 | vec<L, T, Q> const Result(Scaled - static_cast<floatType>(0.5)); |
| 53 | return Result; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | template<length_t L, typename T, typename floatType, qualifier Q> |
| 58 | struct compute_compScale<L, T, floatType, Q, true, false> |
| 59 | { |
| 60 | GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, floatType, Q> const& v) |
| 61 | { |
| 62 | return vec<L, T, Q>(vec<L, floatType, Q>(v) * static_cast<floatType>(std::numeric_limits<T>::max())); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | template<length_t L, typename T, typename floatType, qualifier Q> |
| 67 | struct compute_compScale<L, T, floatType, Q, false, true> |
| 68 | { |
| 69 | GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, floatType, Q> const& v) |
| 70 | { |
| 71 | return v; |
| 72 | } |
| 73 | }; |
| 74 | }//namespace detail |
| 75 | |
| 76 | template<typename floatType, length_t L, typename T, qualifier Q> |
| 77 | GLM_FUNC_QUALIFIER vec<L, floatType, Q> compNormalize(vec<L, T, Q> const& v) |
| 78 | { |
| 79 | GLM_STATIC_ASSERT(std::numeric_limits<floatType>::is_iec559, "'compNormalize' accepts only floating-point types for 'floatType' template parameter"); |
| 80 | |
| 81 | return detail::compute_compNormalize<L, T, floatType, Q, std::numeric_limits<T>::is_integer, std::numeric_limits<T>::is_signed>::call(v); |
| 82 | } |
| 83 | |
| 84 | template<typename T, length_t L, typename floatType, qualifier Q> |
| 85 | GLM_FUNC_QUALIFIER vec<L, T, Q> compScale(vec<L, floatType, Q> const& v) |
| 86 | { |
| 87 | GLM_STATIC_ASSERT(std::numeric_limits<floatType>::is_iec559, "'compScale' accepts only floating-point types for 'floatType' template parameter"); |
| 88 | |
| 89 | return detail::compute_compScale<L, T, floatType, Q, std::numeric_limits<T>::is_integer, std::numeric_limits<T>::is_signed>::call(v); |
| 90 | } |
| 91 | |
| 92 | template<length_t L, typename T, qualifier Q> |
| 93 | GLM_FUNC_QUALIFIER T compAdd(vec<L, T, Q> const& v) |
| 94 | { |
| 95 | T Result(0); |
| 96 | for(length_t i = 0, n = v.length(); i < n; ++i) |
| 97 | Result += v[i]; |
| 98 | return Result; |
| 99 | } |
| 100 | |
| 101 | template<length_t L, typename T, qualifier Q> |
| 102 | GLM_FUNC_QUALIFIER T compMul(vec<L, T, Q> const& v) |
| 103 | { |
| 104 | T Result(1); |
| 105 | for(length_t i = 0, n = v.length(); i < n; ++i) |
| 106 | Result *= v[i]; |
| 107 | return Result; |
| 108 | } |
| 109 | |
| 110 | template<length_t L, typename T, qualifier Q> |
| 111 | GLM_FUNC_QUALIFIER T compMin(vec<L, T, Q> const& v) |
| 112 | { |
| 113 | T Result(v[0]); |
| 114 | for(length_t i = 1, n = v.length(); i < n; ++i) |
| 115 | Result = min(Result, v[i]); |
| 116 | return Result; |
| 117 | } |
| 118 | |
| 119 | template<length_t L, typename T, qualifier Q> |
| 120 | GLM_FUNC_QUALIFIER T compMax(vec<L, T, Q> const& v) |
| 121 | { |
| 122 | T Result(v[0]); |
| 123 | for(length_t i = 1, n = v.length(); i < n; ++i) |
| 124 | Result = max(Result, v[i]); |
| 125 | return Result; |
| 126 | } |
| 127 | }//namespace glm |