component_wise.inl 4.1 KB

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