vector4.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. // Copyright 2010 Michael Smith, all rights reserved.
  14. // Derived closely from:
  15. /****************************************
  16. * 3D Vector Classes
  17. * By Bill Perone (billperone@yahoo.com)
  18. * Original: 9-16-2002
  19. * Revised: 19-11-2003
  20. * 11-12-2003
  21. * 18-12-2003
  22. * 06-06-2004
  23. *
  24. * Copyright 2003, This code is provided "as is" and you can use it freely as long as
  25. * credit is given to Bill Perone in the application it is used in
  26. *
  27. * Notes:
  28. * if a*b = 0 then a & b are orthogonal
  29. * a%b = -b%a
  30. * a*(b%c) = (a%b)*c
  31. * a%b = a(cast to matrix)*b
  32. * (a%b).length() = area of parallelogram formed by a & b
  33. * (a%b).length() = a.length()*b.length() * sin(angle between a & b)
  34. * (a%b).length() = 0 if angle between a & b = 0 or a.length() = 0 or b.length() = 0
  35. * a * (b%c) = volume of parallelpiped formed by a, b, c
  36. * vector triple product: a%(b%c) = b*(a*c) - c*(a*b)
  37. * scalar triple product: a*(b%c) = c*(a%b) = b*(c%a)
  38. * vector quadruple product: (a%b)*(c%d) = (a*c)*(b*d) - (a*d)*(b*c)
  39. * if a is unit vector along b then a%b = -b%a = -b(cast to matrix)*a = 0
  40. * vectors a1...an are linearly dependent if there exists a vector of scalars (b) where a1*b1 + ... + an*bn = 0
  41. * or if the matrix (A) * b = 0
  42. *
  43. ****************************************/
  44. #pragma once
  45. #include <cmath>
  46. #include <float.h>
  47. #include <string.h>
  48. #if MATH_CHECK_INDEXES
  49. #include <assert.h>
  50. #endif
  51. #include "rotations.h"
  52. template <typename T>
  53. class Matrix4;
  54. template <typename T>
  55. class Vector4
  56. {
  57. public:
  58. T M0, M1, M2, M3;
  59. // trivial ctor
  60. constexpr Vector4<T>()
  61. : M0(0)
  62. , M1(0)
  63. , M2(0)
  64. , M3(0){}
  65. // setting ctor
  66. constexpr Vector4<T>(const T x0, const T x1, const T x2,const T x3)
  67. : M0(x0)
  68. , M1(x1)
  69. , M2(x2)
  70. , M3(x3){}
  71. // function call operator
  72. void operator ()(const T x0, const T x1, const T x2,const T x3)
  73. {
  74. M0= x0; M1= x1; M2= x2;M3= x3;
  75. }
  76. // gets the length of this vector
  77. float length(void) const;
  78. // dot product
  79. T operator *(const Vector4<T> &v) const;
  80. // uniform scaling
  81. Vector4<T> operator *(const T num) const;
  82. // uniform scaling
  83. Vector4<T> &operator *=(const T num);
  84. // uniform scaling
  85. Vector4<T> &operator /=(const T num);
  86. // uniform scaling
  87. Vector4<T> operator /(const T num) const;
  88. // subtraction
  89. Vector4<T> &operator -=(const Vector4<T> &v);
  90. // subtraction
  91. Vector4<T> operator -(const Vector4<T> &v) const;
  92. // negation
  93. Vector4<T> operator -(void) const;
  94. // addition
  95. Vector4<T> &operator +=(const Vector4<T> &v);
  96. // addition
  97. Vector4<T> operator +(const Vector4<T> &v) const;
  98. // test for equality
  99. bool operator ==(const Vector4<T> &v) const;
  100. // test for inequality
  101. bool operator !=(const Vector4<T> &v) const;
  102. // non-uniform scaling
  103. Vector4<T> &operator *=(const Vector4<T> &v) {
  104. M0 *= v.M0; M1 *= v.M1; M2 *= v.M2;M3 *= v.M3;
  105. return *this;
  106. }
  107. // check if any elements are NAN
  108. bool is_nan(void) const WARN_IF_UNUSED;
  109. // check if any elements are infinity
  110. bool is_inf(void) const WARN_IF_UNUSED;
  111. // check if all elements are zero
  112. bool is_zero(void) const WARN_IF_UNUSED {
  113. return (fabsf(M0) < FLT_EPSILON) &&
  114. (fabsf(M1) < FLT_EPSILON) &&
  115. (fabsf(M2) < FLT_EPSILON) &&
  116. (fabsf(M3) < FLT_EPSILON);
  117. }
  118. // gets the length of this vector squared
  119. T length_squared() const
  120. {
  121. return (T)(*this * *this);
  122. }
  123. // normalizes this vector
  124. void normalize()
  125. {
  126. *this /= length();
  127. }
  128. // zero the vector
  129. void zero()
  130. {
  131. M0 = M1 = M2 = M3 = 0;
  132. }
  133. // returns the normalized version of this vector
  134. Vector4<T> normalized() const
  135. {
  136. return *this/length();
  137. }
  138. };
  139. typedef Vector4<int16_t> Vector4i;
  140. typedef Vector4<uint16_t> Vector4ui;
  141. typedef Vector4<int32_t> Vector4l;
  142. typedef Vector4<uint32_t> Vector4ul;
  143. typedef Vector4<float> Vector4f;