vector4.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * vector3.cpp
  3. * Copyright (C) Andrew Tridgell 2012
  4. *
  5. * This file is free software: you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This file is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #pragma GCC optimize("O2")
  19. #include "AP_Math.h"
  20. #define HALF_SQRT_2 0.70710678118654757f
  21. template <typename T>
  22. float Vector4<T>::length(void) const
  23. {
  24. return norm(M0, M1, M2, M3);
  25. }
  26. // dot product
  27. template <typename T>
  28. T Vector4<T>::operator *(const Vector4<T> &v) const
  29. {
  30. return M0*v.M0 + M1*v.M1 + M2*v.M2 +M3*v.M3;
  31. }
  32. template <typename T>
  33. Vector4<T> Vector4<T>::operator *(const T num) const
  34. {
  35. return Vector4<T>(M0*num, M1*num, M2*num,M3*num);
  36. }
  37. template <typename T>
  38. Vector4<T> &Vector4<T>::operator *=(const T num)
  39. {
  40. M0*=num; M1*=num; M2*=num;M3*=num;
  41. return *this;
  42. }
  43. template <typename T>
  44. Vector4<T> &Vector4<T>::operator /=(const T num)
  45. {
  46. M0 /= num; M1 /= num; M2 /= num;M3 /= num;
  47. return *this;
  48. }
  49. template <typename T>
  50. Vector4<T> Vector4<T>::operator /(const T num) const
  51. {
  52. return Vector4<T>(M0/num, M1/num, M2/num, M3/num);
  53. }
  54. template <typename T>
  55. Vector4<T> &Vector4<T>::operator -=(const Vector4<T> &v)
  56. {
  57. M0 -= v.M0; M1 -= v.M1; M2 -= v.M2;M3 -= v.M3;
  58. return *this;
  59. }
  60. template <typename T>
  61. Vector4<T> Vector4<T>::operator -(const Vector4<T> &v) const
  62. {
  63. return Vector4<T>(M0-v.M0, M1-v.M1, M2-v.M2, M3-v.M3);
  64. }
  65. template <typename T>
  66. Vector4<T> Vector4<T>::operator -(void) const
  67. {
  68. return Vector4<T>(-M0,-M1,-M2,-M3);
  69. }
  70. template <typename T>
  71. bool Vector4<T>::is_nan(void) const
  72. {
  73. return isnan(M0) || isnan(M1) || isnan(M2)|| isnan(M3);
  74. }
  75. template <typename T>
  76. bool Vector4<T>::is_inf(void) const
  77. {
  78. return isinf(M0) || isinf(M1) || isinf(M2)|| isinf(M3);
  79. }
  80. template <typename T>
  81. Vector4<T> &Vector4<T>::operator +=(const Vector4<T> &v)
  82. {
  83. M0+=v.M0; M1+=v.M1; M2+=v.M2;M3+=v.M3;
  84. return *this;
  85. }
  86. template <typename T>
  87. Vector4<T> Vector4<T>::operator +(const Vector4<T> &v) const
  88. {
  89. return Vector4<T>(M0+v.M0, M1+v.M1, M2+v.M2, M3+v.M3);
  90. }
  91. template <typename T>
  92. bool Vector4<T>::operator ==(const Vector4<T> &v) const
  93. {
  94. return (is_equal(M0,v.M0) && is_equal(M1,v.M1) && is_equal(M2,v.M2) && is_equal(M3,v.M3));
  95. }
  96. template <typename T>
  97. bool Vector4<T>::operator !=(const Vector4<T> &v) const
  98. {
  99. return (!is_equal(M0,v.M0) || !is_equal(M1,v.M1) || !is_equal(M2,v.M2)||!is_equal(M3,v.M3));
  100. }
  101. // define for float
  102. template float Vector4<float>::length(void) const;
  103. template float Vector4<float>::operator *(const Vector4<float> &v) const;
  104. template Vector4<float> Vector4<float>::operator *(const float num) const;
  105. template Vector4<float> &Vector4<float>::operator *=(const float num);
  106. template Vector4<float> &Vector4<float>::operator /=(const float num);
  107. template Vector4<float> Vector4<float>::operator /(const float num) const;
  108. template Vector4<float> &Vector4<float>::operator -=(const Vector4<float> &v);
  109. template Vector4<float> Vector4<float>::operator -(const Vector4<float> &v) const;
  110. template Vector4<float> Vector4<float>::operator -(void) const;
  111. template bool Vector4<float>::is_nan(void) const;
  112. template bool Vector4<float>::is_inf(void) const;
  113. template Vector4<float> &Vector4<float>::operator +=(const Vector4<float> &v);
  114. template Vector4<float> Vector4<float>::operator +(const Vector4<float> &v) const;
  115. template bool Vector4<float>::operator ==(const Vector4<float> &v) const;
  116. template bool Vector4<float>::operator !=(const Vector4<float> &v) const;