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