vectorN.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #pragma once
  14. #include <cmath>
  15. #include <string.h>
  16. #include "matrixN.h"
  17. #ifndef MATH_CHECK_INDEXES
  18. # define MATH_CHECK_INDEXES 0
  19. #endif
  20. #if MATH_CHECK_INDEXES
  21. #include <assert.h>
  22. #endif
  23. template <typename T, uint8_t N>
  24. class MatrixN;
  25. template <typename T, uint8_t N>
  26. class VectorN
  27. {
  28. public:
  29. // trivial ctor
  30. inline VectorN<T,N>() {
  31. memset(_v, 0, sizeof(T)*N);
  32. }
  33. // vector ctor
  34. inline VectorN<T,N>(const T *v) {
  35. memcpy(_v, v, sizeof(T)*N);
  36. }
  37. inline T & operator[](uint8_t i) {
  38. #if MATH_CHECK_INDEXES
  39. assert(i >= 0 && i < N);
  40. #endif
  41. return _v[i];
  42. }
  43. inline const T & operator[](uint8_t i) const {
  44. #if MATH_CHECK_INDEXES
  45. assert(i >= 0 && i < N);
  46. #endif
  47. return _v[i];
  48. }
  49. // test for equality
  50. bool operator ==(const VectorN<T,N> &v) const {
  51. for (uint8_t i=0; i<N; i++) {
  52. if (_v[i] != v[i]) return false;
  53. }
  54. return true;
  55. }
  56. // zero the vector
  57. inline void zero()
  58. {
  59. memset(_v, 0, sizeof(T)*N);
  60. }
  61. // negation
  62. VectorN<T,N> operator -(void) const {
  63. VectorN<T,N> v2;
  64. for (uint8_t i=0; i<N; i++) {
  65. v2[i] = - _v[i];
  66. }
  67. return v2;
  68. }
  69. // addition
  70. VectorN<T,N> operator +(const VectorN<T,N> &v) const {
  71. VectorN<T,N> v2;
  72. for (uint8_t i=0; i<N; i++) {
  73. v2[i] = _v[i] + v[i];
  74. }
  75. return v2;
  76. }
  77. // subtraction
  78. VectorN<T,N> operator -(const VectorN<T,N> &v) const {
  79. VectorN<T,N> v2;
  80. for (uint8_t i=0; i<N; i++) {
  81. v2[i] = _v[i] - v[i];
  82. }
  83. return v2;
  84. }
  85. // uniform scaling
  86. VectorN<T,N> operator *(const T num) const {
  87. VectorN<T,N> v2;
  88. for (uint8_t i=0; i<N; i++) {
  89. v2[i] = _v[i] * num;
  90. }
  91. return v2;
  92. }
  93. // uniform scaling
  94. VectorN<T,N> operator /(const T num) const {
  95. VectorN<T,N> v2;
  96. for (uint8_t i=0; i<N; i++) {
  97. v2[i] = _v[i] / num;
  98. }
  99. return v2;
  100. }
  101. // addition
  102. VectorN<T,N> &operator +=(const VectorN<T,N> &v) {
  103. for (uint8_t i=0; i<N; i++) {
  104. _v[i] += v[i];
  105. }
  106. return *this;
  107. }
  108. // subtraction
  109. VectorN<T,N> &operator -=(const VectorN<T,N> &v) {
  110. for (uint8_t i=0; i<N; i++) {
  111. _v[i] -= v[i];
  112. }
  113. return *this;
  114. }
  115. // uniform scaling
  116. VectorN<T,N> &operator *=(const T num) {
  117. for (uint8_t i=0; i<N; i++) {
  118. _v[i] *= num;
  119. }
  120. return *this;
  121. }
  122. // uniform scaling
  123. VectorN<T,N> &operator /=(const T num) {
  124. for (uint8_t i=0; i<N; i++) {
  125. _v[i] /= num;
  126. }
  127. return *this;
  128. }
  129. // dot product
  130. T operator *(const VectorN<T,N> &v) const {
  131. float ret = 0;
  132. for (uint8_t i=0; i<N; i++) {
  133. ret += _v[i] * v._v[i];
  134. }
  135. return ret;
  136. }
  137. // multiplication of a matrix by a vector, in-place
  138. // C = A * B
  139. void mult(const MatrixN<T,N> &A, const VectorN<T,N> &B) {
  140. for (uint8_t i = 0; i < N; i++) {
  141. _v[i] = 0;
  142. for (uint8_t k = 0; k < N; k++) {
  143. _v[i] += A.v[i][k] * B[k];
  144. }
  145. }
  146. }
  147. private:
  148. T _v[N];
  149. };