Collection of DPF-based plugins for packaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.1KB

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