component_wise.inl 4.6 KB

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