VectorNav C++ Library
vector.h
Go to the documentation of this file.
1 #ifndef _VN_MATH_VECTOR_H_
7 #define _VN_MATH_VECTOR_H_
8 
9 #include <cassert>
10 #include <sstream>
11 #include <ostream>
12 #include <cmath>
13 
14 #include "exceptions.h"
15 #include "int.h"
16 
17 namespace vn {
18 namespace math {
19 
21 template <size_t tdim, typename T = float>
22 struct vec
23 {
24  // Public Members /////////////////////////////////////////////////////////
25 
26 public:
27 
29  T c[tdim];
30 
31  // Constructors ///////////////////////////////////////////////////////////
32 
33 public:
34 
36  vec() { }
37 
41  explicit vec(T val)
42  {
43  std::fill_n(c, tdim, val);
44  }
45 
46  // Helper Methods /////////////////////////////////////////////////////////
47 
48 public:
49 
53  static vec zero()
54  {
55  return vec<tdim, T>(0);
56  }
57 
61  static vec one()
62  {
63  return vec<tdim, T>(1);
64  }
65 
66  // Operator Overloads /////////////////////////////////////////////////////
67 
68 public:
69 
74  T& operator[](size_t index)
75  {
76  return const_cast<T&>(static_cast<const vec&>(*this)[index]);
77  }
78 
83  const T& operator[](size_t index) const
84  {
85  assert(index < tdim);
86 
87  return c[index];
88  }
89 
93  vec operator-() const
94  {
95  return neg();
96  }
97 
102  vec& operator+=(const vec& rhs)
103  {
104  for (size_t i = 0; i < tdim; i++)
105  c[i] += rhs.c[i];
106 
107  return *this;
108  }
109 
114  vec& operator-=(const vec& rhs)
115  {
116  for (size_t i = 0; i < tdim; i++)
117  c[i] -= rhs.c[i];
118 
119  return *this;
120  }
121 
126  vec& operator*=(const T& rhs)
127  {
128  for (size_t i = 0; i < tdim; i++)
129  c[i] *= rhs;
130 
131  return *this;
132  }
133 
138  vec& operator/=(const T & rhs)
139  {
140  for (size_t i = 0; i < tdim; i++)
141  c[i] /= rhs;
142 
143  return *this;
144  }
145 
146  // Public Methods /////////////////////////////////////////////////////////
147 
148 public:
149 
153  size_t dim() const { return tdim; }
154 
158  vec neg() const
159  {
160  vec v;
161 
162  for (size_t i = 0; i < tdim; i++)
163  v.c[i] = -c[i];
164 
165  return v;
166  }
167 
171  T mag() const
172  {
173  T sumOfSquares = 0;
174 
175  for (size_t i = 0; i < tdim; i++)
176  sumOfSquares += c[i] * c[i];
177 
178  return sqrt(sumOfSquares);
179  }
180 
185  vec add(const vec& toAdd) const
186  {
187  vec v;
188 
189  for (size_t i = 0; i < tdim; i++)
190  v.c[i] = c[i] + toAdd.c[i];
191 
192  return v;
193  }
194 
199  vec sub(const vec& to_sub) const
200  {
201  vec v;
202 
203  for (size_t i = 0; i < tdim; i++)
204  v.c[i] = c[i] - to_sub.c[i];
205 
206  return v;
207  }
208 
213  vec mult(const double& scalar) const
214  {
215  vec v;
216 
217  for (size_t i = 0; i < tdim; i++)
218  v.c[i] = c[i] * scalar;
219 
220  return v;
221  }
222 
227  vec div(const double& scalar) const
228  {
229  vec v;
230 
231  for (size_t i = 0; i < tdim; i++)
232  v.c[i] = c[i] / scalar;
233 
234  return v;
235  }
236 
240  vec norm() const
241  {
242  vec v;
243 
244  T m = mag();
245 
246  for (size_t i = 0; i < tdim; i++)
247  v.c[i] = c[i] / m;
248 
249  return v;
250  }
251 
256  T dot(const vec& rhs) const
257  {
258  T runningSum = 0;
259 
260  for (size_t i = 0; i < tdim; i++)
261  runningSum += c[i] * rhs.c[i];
262 
263  return runningSum;
264  }
265 
266 };
267 
268 // Specializations ////////////////////////////////////////////////////////////
269 
270 #if defined(_MSC_VER)
271  #pragma warning(push)
272 
273  // Disable warning about 'nonstandard extension used : nameless struct/union'.
274  #pragma warning(disable:4201)
275 #endif
276 
278 template <typename T>
279 struct vec<2, T>
280 {
281 
282  // Public Members /////////////////////////////////////////////////////////
283 
284 public:
285 
286  union
287  {
288  struct
289  {
291  T x;
292 
294  T y;
295  };
296 
298  T c[2];
299  };
300 
301  // Constructors ///////////////////////////////////////////////////////////
302 
303 public:
304 
306  vec() { }
307 
311  explicit vec(T val) : x(val), y(val) { }
312 
318  vec(T x_val, T y_val) : x(x_val), y(y_val) { }
319 
320  // Helper Methods /////////////////////////////////////////////////////////
321 
322 public:
323 
327  static vec zero()
328  {
329  return vec<2, T>(0);
330  }
331 
335  static vec one()
336  {
337  return vec<2, T>(1);
338  }
339 
343  static vec unitX()
344  {
345  return vec<2, T>(1, 0);
346  }
347 
351  static vec unitY()
352  {
353  return vec<2, T>(0, 1);
354  }
355 
356 
357  // Operator Overloads /////////////////////////////////////////////////////
358 
359 public:
360 
365  T& operator[](size_t index)
366  {
367  return const_cast<T&>(static_cast<const vec&>(*this)[index]);
368  }
369 
374  const T& operator[](size_t index) const
375  {
376  assert(index < 2);
377 
378  return c[index];
379  }
380 
384  vec operator-() const
385  {
386  return neg();
387  }
388 
393  vec& operator+=(const vec& rhs)
394  {
395  x += rhs.x;
396  y += rhs.y;
397 
398  return *this;
399  }
400 
405  vec& operator-=(const vec& rhs)
406  {
407  x -= rhs.x;
408  y -= rhs.y;
409 
410  return *this;
411  }
412 
417  vec& operator*=(const T& rhs)
418  {
419  x *= rhs;
420  y *= rhs;
421 
422  return *this;
423  }
424 
429  vec& operator/=(const T & rhs)
430  {
431  x /= rhs;
432  y /= rhs;
433 
434  return *this;
435  }
436 
437  // Public Methods /////////////////////////////////////////////////////////
438 
439 public:
440 
444  size_t dim() const { return 2; }
445 
449  vec neg() const
450  {
451  // TODO: Issue when the underlying type is an unsigned integer.
452  #if defined(_MSC_VER)
453  #pragma warning(push)
454  #pragma warning(disable:4146)
455  #endif
456 
457  return vec(-x, -y);
458 
459  #if defined (_MSC_VER)
460  #pragma warning(pop)
461  #endif
462  }
463 
467  T mag() const
468  {
469  // TODO: Might want this method to return a float even if the underlying
470  // data type is integer.
471  #if defined(_MSC_VER)
472  #pragma warning(push)
473  #pragma warning(disable:4244)
474  #endif
475 
476  #if (defined(_MSC_VER) && _MSC_VER <= 1600)
477  // HACK: Visual Studio 2010 has trouble determining the correct 'sqrt'
478  // function for the template int32_t.
479  return sqrt(static_cast<float>(x*x + y*y));
480  #else
481  // HACK:
482  return sqrt(static_cast<float>(x*x + y*y));
483  //return sqrt(x*x + y*y);
484  #endif
485 
486  #if defined (_MSC_VER)
487  #pragma warning(pop)
488  #endif
489  }
490 
495  vec add(const vec& toAdd) const
496  {
497  return vec(x + toAdd.x, y + toAdd.y);
498  }
499 
504  vec sub(const vec& to_sub) const
505  {
506  return vec(x - to_sub.x, y - to_sub.y);
507  }
508 
513  vec mult(const double& scalar) const
514  {
515  return vec(x * scalar, y * scalar);
516  }
517 
522  vec div(const double& scalar) const
523  {
524  return vec(x / scalar, y / scalar);
525  }
526 
530  vec norm() const
531  {
532  T m = mag();
533 
534  return vec(x / m, y / m);
535  }
536 
541  T dot(const vec& rhs) const
542  {
543  return x*rhs.x + y*rhs.y;
544  }
545 
546 };
547 
548 
550 template <typename T>
551 struct vec<3, T>
552 {
553 
554  // Public Members /////////////////////////////////////////////////////////
555 
556 public:
557 
558  union
559  {
560  struct
561  {
563  T x;
564 
566  T y;
567 
569  T z;
570  };
571 
572  struct
573  {
575  T r;
576 
578  T g;
579 
581  T b;
582  };
583 
584  // Union of template class with constructor not allowed until C++11.
585  #if __cplusplus >= 201103L
586 
588  vec<2, T> xy;
589 
590  #endif
591 
593  T c[3];
594  };
595 
596  // Constructors ///////////////////////////////////////////////////////////
597 
598 public:
599 
601  vec() { }
602 
606  explicit vec(T val) : x(val), y(val), z(val) { }
607 
614  vec(const T& x_val, const T& y_val, const T& z_val) : x(x_val), y(y_val), z(z_val) { }
615 
616  // Helper Methods /////////////////////////////////////////////////////////
617 
618 public:
619 
623  static vec zero()
624  {
625  return vec<3, T>(0);
626  }
627 
631  static vec one()
632  {
633  return vec<3, T>(1);
634  }
635 
639  static vec unitX()
640  {
641  return vec<3, T>(1, 0, 0);
642  }
643 
647  static vec unitY()
648  {
649  return vec<3, T>(0, 1, 0);
650  }
651 
655  static vec unitZ()
656  {
657  return vec<3, T>(0, 0, 1);
658  }
659 
660  // Operator Overloads /////////////////////////////////////////////////////
661 
662 public:
663 
668  T& operator[](size_t index)
669  {
670  return const_cast<T&>(static_cast<const vec&>(*this)[index]);
671  }
672 
677  const T& operator[](size_t index) const
678  {
679  assert(index < 3);
680 
681  return c[index];
682  }
683 
687  vec operator-() const
688  {
689  return neg();
690  }
691 
696  vec& operator+=(const vec& rhs)
697  {
698  for (size_t i = 0; i < 3; i++)
699  c[i] += rhs.c[i];
700 
701  return *this;
702  }
703 
708  vec& operator-=(const vec& rhs)
709  {
710  for (size_t i = 0; i < 3; i++)
711  c[i] -= rhs.c[i];
712 
713  return *this;
714  }
715 
720  vec& operator*=(const T& rhs)
721  {
722  for (size_t i = 0; i < 3; i++)
723  c[i] *= rhs;
724 
725  return *this;
726  }
727 
732  vec& operator/=(const T & rhs)
733  {
734  for (size_t i = 0; i < 3; i++)
735  c[i] /= rhs;
736 
737  return *this;
738  }
739 
740  // Public Methods /////////////////////////////////////////////////////////
741 
742 public:
743 
747  size_t dim() const { return 3; }
748 
752  vec neg() const
753  {
754  // TODO: Issue when the underlying type is an unsigned integer.
755  #if defined(_MSC_VER)
756  #pragma warning(push)
757  #pragma warning(disable:4146)
758  #endif
759 
760  return vec(-x, -y, -z);
761 
762  #if defined (_MSC_VER)
763  #pragma warning(pop)
764  #endif
765  }
766 
770  T mag() const
771  {
772  // TODO: Might want this method to return a float even if the underlying
773  // data type is integer.
774  #if defined(_MSC_VER)
775  #pragma warning(push)
776  #pragma warning(disable:4244)
777  #endif
778 
779  #if (defined(_MSC_VER) && _MSC_VER <= 1600)
780  // HACK: Visual Studio 2010 has trouble determining the correct 'sqrt'
781  // function for the template int32_t.
782  return sqrt(static_cast<float>(x*x + y*y + z*z));
783  #else
784  // HACK:
785  return sqrt(static_cast<float>(x*x + y*y + z*z));
786  //return sqrt(x*x + y*y + z*z);
787  #endif
788 
789  #if defined (_MSC_VER)
790  #pragma warning(pop)
791  #endif
792  }
793 
798  vec add(const vec& toAdd) const
799  {
800  return vec(x + toAdd.x, y + toAdd.y, z + toAdd.z);
801  }
802 
807  vec sub(const vec& to_sub) const
808  {
809  return vec(x - to_sub.x, y - to_sub.y, z - to_sub.z);
810  }
811 
816  vec mult(const double& scalar) const
817  {
818  return vec(x * scalar, y * scalar, z * scalar);
819  }
820 
825  vec div(const double& scalar) const
826  {
827  return vec(x / scalar, y / scalar, z / scalar);
828  }
829 
833  vec norm() const
834  {
835  T m = mag();
836 
837  return vec(x / m, y / m, z / m);
838  }
839 
844  T dot(const vec& rhs) const
845  {
846  return x*rhs.x + y*rhs.y + z*rhs.z;
847  }
848 
854  vec<3, T> cross(const vec<3, T>& rhs) const
855  {
856  vec<3, T> v;
857 
858  v.c[0] = c[1] * rhs.c[2] - c[2] * rhs.c[1];
859  v.c[1] = c[2] * rhs.c[0] - c[0] * rhs.c[2];
860  v.c[2] = c[0] * rhs.c[1] - c[1] * rhs.c[0];
861 
862  return v;
863  }
864 
865 };
866 
868 template <typename T>
869 struct vec<4, T>
870 {
871 
872  // Public Members /////////////////////////////////////////////////////////
873 
874 public:
875 
876  union
877  {
878  struct
879  {
881  T x;
882 
884  T y;
885 
887  T z;
888 
890  T w;
891  };
892 
893  struct
894  {
896  T r;
897 
899  T g;
900 
902  T b;
903 
905  T a;
906  };
907 
908  // Union of template class with constructor not allowed until C++11.
909  #if __cplusplus >= 201103L
910 
912  vec<2, T> xy;
913 
915  vec<3, T> xyz;
916 
918  vec<3, T> rgb;
919 
920  #endif
921 
923  T c[4];
924  };
925 
926  // Constructors ///////////////////////////////////////////////////////////
927 
928 public:
929 
931  vec() { }
932 
936  explicit vec(T val) : x(val), y(val), z(val), w(val) { }
937 
945  vec(T x_val, T y_val, T z_val, T w_val) : x(x_val), y(y_val), z(z_val), w(w_val) { }
946 
947  // Helper Methods /////////////////////////////////////////////////////////
948 
949 public:
950 
954  static vec zero()
955  {
956  return vec<4, T>(0);
957  }
958 
962  static vec one()
963  {
964  return vec<4, T>(1);
965  }
966 
970  static vec unitX()
971  {
972  return vec<4, T>(1, 0, 0, 0);
973  }
974 
978  static vec unitY()
979  {
980  return vec<4, T>(0, 1, 0, 0);
981  }
982 
986  static vec unitZ()
987  {
988  return vec<4, T>(0, 0, 1, 0);
989  }
990 
994  static vec unitW()
995  {
996  return vec<4, T>(0, 0, 0, 1);
997  }
998  // Operator Overloads /////////////////////////////////////////////////////
999 
1000 public:
1001 
1006  T& operator[](size_t index)
1007  {
1008  return const_cast<T&>(static_cast<const vec&>(*this)[index]);
1009  }
1010 
1015  const T& operator[](size_t index) const
1016  {
1017  assert(index < 4);
1018 
1019  return c[index];
1020  }
1021 
1025  vec operator-() const
1026  {
1027  return neg();
1028  }
1029 
1034  vec& operator+=(const vec& rhs)
1035  {
1036  x += rhs.x;
1037  y += rhs.y;
1038  z += rhs.z;
1039  w += rhs.w;
1040 
1041  return *this;
1042  }
1043 
1048  vec& operator-=(const vec& rhs)
1049  {
1050  x -= rhs.x;
1051  y -= rhs.y;
1052  z -= rhs.z;
1053  w -= rhs.w;
1054 
1055  return *this;
1056  }
1057 
1062  vec& operator*=(const T& rhs)
1063  {
1064  x *= rhs;
1065  y *= rhs;
1066  z *= rhs;
1067  w *= rhs;
1068 
1069  return *this;
1070  }
1071 
1076  vec& operator/=(const T & rhs)
1077  {
1078  x /= rhs;
1079  y /= rhs;
1080  z /= rhs;
1081  w /= rhs;
1082 
1083  return *this;
1084  }
1085 
1086  // Public Methods /////////////////////////////////////////////////////////
1087 
1088 public:
1089 
1093  size_t dim() const { return 4; }
1094 
1098  vec neg() const
1099  {
1100  // TODO: Issue when the underlying type is an unsigned integer.
1101  #if defined(_MSC_VER)
1102  #pragma warning(push)
1103  #pragma warning(disable:4146)
1104  #endif
1105 
1106  return vec(-x, -y, -z, -w);
1107 
1108  #if defined (_MSC_VER)
1109  #pragma warning(pop)
1110  #endif
1111  }
1112 
1116  T mag() const
1117  {
1118  // TODO: Might want this method to return a float even if the underlying
1119  // data type is integer.
1120  #if defined(_MSC_VER)
1121  #pragma warning(push)
1122  #pragma warning(disable:4244)
1123  #endif
1124 
1125  #if (defined(_MSC_VER) && _MSC_VER <= 1600)
1126  // HACK: Visual Studio 2010 has trouble determining the correct 'sqrt'
1127  // function for the template int32_t.
1128  return sqrt(static_cast<float>(x*x + y*y + z*z + w*w));
1129  #else
1130  // HACK:
1131  return sqrt(static_cast<float>(x*x + y*y + z*z + w*w));
1132  //return sqrt(x*x + y*y + z*z + w*w);
1133  #endif
1134 
1135  #if defined (_MSC_VER)
1136  #pragma warning(pop)
1137  #endif
1138  }
1139 
1144  vec add(const vec& toAdd) const
1145  {
1146  return vec(x + toAdd.x, y + toAdd.y, z + toAdd.z, w + toAdd.w);
1147  }
1148 
1153  vec sub(const vec& to_sub) const
1154  {
1155  return vec(x - to_sub.x, y - to_sub.y, z - to_sub.z, w - to_sub.w);
1156  }
1157 
1162  vec mult(const double& scalar) const
1163  {
1164  return vec(x * scalar, y * scalar, z * scalar, w * scalar);
1165  }
1166 
1171  vec div(const double& scalar) const
1172  {
1173  return vec(x / scalar, y / scalar, z / scalar, w / scalar);
1174  }
1175 
1179  vec norm() const
1180  {
1181  T m = mag();
1182 
1183  return vec(x / m, y / m, z / m, w / m);
1184  }
1185 
1190  T dot(const vec& rhs) const
1191  {
1192  return x*rhs.x + y*rhs.y + z*rhs.z + w*rhs.w;
1193  }
1194 };
1195 
1196 #if defined (_MSC_VER)
1197  #pragma warning(pop)
1198 #endif
1199 
1200 // Operator Overloads /////////////////////////////////////////////////////////
1201 
1207 template <size_t tdim, typename T>
1208 vec<tdim, T> operator+(vec<tdim, T> lhs, const vec<tdim, T>& rhs)
1209 {
1210  lhs += rhs;
1211 
1212  return lhs;
1213 }
1214 
1220 template <size_t tdim, typename T>
1221 vec<tdim, T> operator-(vec<tdim, T> lhs, const vec<tdim, T>& rhs)
1222 {
1223  lhs -= rhs;
1224 
1225  return lhs;
1226 }
1227 
1228 #if defined(_MSC_VER)
1229  #pragma warning(push)
1230 
1231  // The operator* and operator/ throw a warning when a vec*f is multiplied by a double.
1232  #pragma warning(disable:4244)
1233 #endif
1234 
1240 template <size_t tdim, typename T, typename S>
1241 vec<tdim, T> operator*(vec<tdim, T> lhs, const S& rhs)
1242 {
1243  lhs *= rhs;
1244 
1245  return lhs;
1246 }
1247 
1253 template <size_t tdim, typename T, typename S>
1254 vec<tdim, T> operator*(const S& rhs, vec<tdim, T> lhs)
1255 {
1256  lhs *= rhs;
1257 
1258  return lhs;
1259 }
1260 
1266 template <size_t tdim, typename T, typename S>
1267 vec<tdim, T> operator/(vec<tdim, T> lhs, const S& rhs)
1268 {
1269  lhs /= rhs;
1270 
1271  return lhs;
1272 }
1273 
1274 #if defined (_MSC_VER)
1275  #pragma warning(pop)
1276 #endif
1277 
1278 // Specific Typedefs //////////////////////////////////////////////////////////
1279 
1281 typedef vec<2> vec2;
1282 
1284 typedef vec<3> vec3;
1285 
1287 typedef vec<4> vec4;
1288 
1290 typedef vec<2, float> vec2f;
1291 
1293 typedef vec<3, float> vec3f;
1294 
1296 typedef vec<4, float> vec4f;
1297 
1299 typedef vec<2, double> vec2d;
1300 
1302 typedef vec<3, double> vec3d;
1303 
1305 typedef vec<4, double> vec4d;
1306 
1308 typedef vec<2, long double> vec2ld;
1309 
1311 typedef vec<3, long double> vec3ld;
1312 
1314 typedef vec<4, long double> vec4ld;
1315 
1317 typedef vec<2, int32_t> vec2i32;
1318 
1320 typedef vec2i32 ivec2;
1321 
1323 typedef vec<3, int32_t> vec3i32;
1324 
1326 typedef vec<4, int32_t> vec4i32;
1327 
1329 typedef vec<2, uint32_t> vec2u32;
1330 
1332 typedef vec<3, uint32_t> vec3u32;
1333 
1335 typedef vec<4, uint32_t> vec4u32;
1336 
1337 // Common functions for working with vectors.
1338 
1344 template <size_t tdim, typename T> std::string str(vec<tdim, T> v)
1345 {
1346  std::stringstream ss;
1347  ss << "(";
1348  for (size_t i = 0; i < v.dim(); i++)
1349  {
1350  ss << v[i];
1351 
1352  if (i + 1 < v.dim())
1353  ss << "; ";
1354  }
1355  ss << ")";
1356 
1357  return ss.str();
1358 }
1359 
1366 template <size_t tdim, typename T> std::ostream& operator<<(std::ostream& out, vec<tdim, T> v)
1367 {
1368  out << str(v);
1369  return out;
1370 }
1371 
1372 }
1373 }
1374 
1375 #endif
T z
Z (2-component).
Definition: vector.h:569
vec & operator*=(const T &rhs)
Multiplies the vector by a scalar.
Definition: vector.h:720
Template for a Euclidean vector.
Definition: vector.h:22
T & operator[](size_t index)
Indexing into the vector's components.
Definition: vector.h:74
T dot(const vec &rhs) const
Computes the dot product of this and the provided vector.
Definition: vector.h:844
T dot(const vec &rhs) const
Computes the dot product of this and the provided vector.
Definition: vector.h:256
vec & operator+=(const vec &rhs)
Adds a vector to this vector.
Definition: vector.h:696
T w
W (3-component).
Definition: vector.h:890
const T & operator[](size_t index) const
Indexing into the vector's components.
Definition: vector.h:374
Vector with 4 component specialization.
Definition: vector.h:869
T z
Z (2-component).
Definition: vector.h:887
vec(T val)
Creates new vector with components initialized to val.
Definition: vector.h:41
const T & operator[](size_t index) const
Indexing into the vector's components.
Definition: vector.h:677
vec & operator/=(const T &rhs)
Divides the vector by a scalar.
Definition: vector.h:732
T & operator[](size_t index)
Indexing into the vector's components.
Definition: vector.h:668
vec mult(const double &scalar) const
Multiplies the vector by a scalar.
Definition: vector.h:1162
vec sub(const vec &to_sub) const
Subtracts a vector from this vector.
Definition: vector.h:199
size_t dim() const
The vector's dimension.
Definition: vector.h:153
vec(T val)
Creates new vector with components initialized to val.
Definition: vector.h:936
static vec unitX()
Unit vector pointing in the X (0-component) direction.
Definition: vector.h:970
T dot(const vec &rhs) const
Computes the dot product of this and the provided vector.
Definition: vector.h:1190
vec div(const double &scalar) const
Divides the vector by a scalar.
Definition: vector.h:227
vec(const T &x_val, const T &y_val, const T &z_val)
Creates a new vector with its components initialized to the provided values.
Definition: vector.h:614
vec sub(const vec &to_sub) const
Subtracts a vector from this vector.
Definition: vector.h:504
size_t dim() const
The vector's dimension.
Definition: vector.h:1093
vec(T val)
Creates new vector with components initialized to val.
Definition: vector.h:606
T y
Y (1-component).
Definition: vector.h:566
static vec one()
Vector with all of its components set to 1.
Definition: vector.h:335
vec & operator+=(const vec &rhs)
Adds a vector to this vector.
Definition: vector.h:1034
vec & operator/=(const T &rhs)
Divides the vector by a scalar.
Definition: vector.h:138
vec(T x_val, T y_val)
Creates a new vector with its components inintialized to the provided values.
Definition: vector.h:318
T mag() const
The vector's magnitude.
Definition: vector.h:1116
T r
Red (0-component).
Definition: vector.h:896
vec neg() const
Negates the vector.
Definition: vector.h:158
vec norm() const
Normalizes the vector.
Definition: vector.h:1179
static vec unitZ()
Unit vector pointing in the Z (2-component) direction.
Definition: vector.h:655
const T & operator[](size_t index) const
Indexing into the vector's components.
Definition: vector.h:1015
static vec unitY()
Unit vector pointing in the Y (1-component) direction.
Definition: vector.h:978
vec add(const vec &toAdd) const
Adds a vector to this vector.
Definition: vector.h:495
vec sub(const vec &to_sub) const
Subtracts a vector from this vector.
Definition: vector.h:807
T y
Y (1-component).
Definition: vector.h:884
static vec unitY()
Unit vector pointing in the Y (1-component) direction.
Definition: vector.h:647
vec operator-() const
Negates the vector.
Definition: vector.h:384
vec div(const double &scalar) const
Divides the vector by a scalar.
Definition: vector.h:522
T mag() const
The vector's magnitude.
Definition: vector.h:770
vec div(const double &scalar) const
Divides the vector by a scalar.
Definition: vector.h:1171
vec & operator*=(const T &rhs)
Multiplies the vector by a scalar.
Definition: vector.h:1062
vec()
Creates a new vector with uninitialized components.
Definition: vector.h:306
T dot(const vec &rhs) const
Computes the dot product of this and the provided vector.
Definition: vector.h:541
Vector with 3 component specialization.
Definition: vector.h:551
size_t dim() const
The vector's dimension.
Definition: vector.h:444
vec mult(const double &scalar) const
Multiplies the vector by a scalar.
Definition: vector.h:816
size_t dim() const
The vector's dimension.
Definition: vector.h:747
T g
Green (1-component).
Definition: vector.h:899
vec neg() const
Negates the vector.
Definition: vector.h:1098
vec(T val)
Creates new vector with components initialized to val.
Definition: vector.h:311
vec mult(const double &scalar) const
Multiplies the vector by a scalar.
Definition: vector.h:513
T x
X (0-component).
Definition: vector.h:563
T c[tdim]
The vector's components.
Definition: vector.h:29
T b
Blue (2-component).
Definition: vector.h:902
static vec one()
Vector with all of its components set to 1.
Definition: vector.h:61
vec & operator+=(const vec &rhs)
Adds a vector to this vector.
Definition: vector.h:393
vec div(const double &scalar) const
Divides the vector by a scalar.
Definition: vector.h:825
vec(T x_val, T y_val, T z_val, T w_val)
Creates a new vector with its components inintialized to the provided values.
Definition: vector.h:945
T mag() const
The vector's magnitude.
Definition: vector.h:171
vec & operator/=(const T &rhs)
Divides the vector by a scalar.
Definition: vector.h:1076
T g
Green (1-component).
Definition: vector.h:578
static vec zero()
Vector with all of its components set to 0.
Definition: vector.h:954
T & operator[](size_t index)
Indexing into the vector's components.
Definition: vector.h:1006
vec norm() const
Normalizes the vector.
Definition: vector.h:240
static vec zero()
Vector with all of its components set to 0.
Definition: vector.h:623
static vec unitZ()
Unit vector pointing in the Z (2-component) direction.
Definition: vector.h:986
vec()
Creates a new vector with uninitialized components.
Definition: vector.h:36
T b
Blue (2-component).
Definition: vector.h:581
vec & operator-=(const vec &rhs)
Subtracts a vector from this vector.
Definition: vector.h:708
vec & operator*=(const T &rhs)
Multiplies the vector by a scalar.
Definition: vector.h:417
vec & operator+=(const vec &rhs)
Adds a vector to this vector.
Definition: vector.h:102
static vec zero()
Vector with all of its components set to 0.
Definition: vector.h:327
T x
X (0-component).
Definition: vector.h:881
vec operator-() const
Negates the vector.
Definition: vector.h:93
static vec unitX()
Unit vector pointing in the X (0-component) direction.
Definition: vector.h:343
vec & operator-=(const vec &rhs)
Subtracts a vector from this vector.
Definition: vector.h:405
static vec unitY()
Unit vector pointing in the Y (1-component) direction.
Definition: vector.h:351
vec mult(const double &scalar) const
Multiplies the vector by a scalar.
Definition: vector.h:213
T y
Y (1-component).
Definition: vector.h:294
T & operator[](size_t index)
Indexing into the vector's components.
Definition: vector.h:365
vec< 3, T > cross(const vec< 3, T > &rhs) const
Computes the cross product of this and the provided vector.
Definition: vector.h:854
vec()
Creates a new vector with uninitialized components.
Definition: vector.h:931
vec norm() const
Normalizes the vector.
Definition: vector.h:833
T c[3]
The vector's components.
Definition: vector.h:593
static vec zero()
Vector with all of its components set to 0.
Definition: vector.h:53
vec neg() const
Negates the vector.
Definition: vector.h:449
Definition: attitude.h:8
Vector with 2 component specialization.
Definition: vector.h:279
const T & operator[](size_t index) const
Indexing into the vector's components.
Definition: vector.h:83
vec add(const vec &toAdd) const
Adds a vector to this vector.
Definition: vector.h:1144
static vec unitW()
Unit vector pointing in the W (3-component) direction.
Definition: vector.h:994
static vec unitX()
Unit vector pointing in the X (0-component) direction.
Definition: vector.h:639
vec()
Creates a new vector with uninitialized components.
Definition: vector.h:601
T x
X (0-component).
Definition: vector.h:291
vec operator-() const
Negates the vector.
Definition: vector.h:687
vec neg() const
Negates the vector.
Definition: vector.h:752
T a
Alpha (3-component).
Definition: vector.h:905
vec sub(const vec &to_sub) const
Subtracts a vector from this vector.
Definition: vector.h:1153
vec & operator/=(const T &rhs)
Divides the vector by a scalar.
Definition: vector.h:429
T mag() const
The vector's magnitude.
Definition: vector.h:467
vec & operator*=(const T &rhs)
Multiplies the vector by a scalar.
Definition: vector.h:126
static vec one()
Vector with all of its components set to 1.
Definition: vector.h:962
vec norm() const
Normalizes the vector.
Definition: vector.h:530
static vec one()
Vector with all of its components set to 1.
Definition: vector.h:631
vec & operator-=(const vec &rhs)
Subtracts a vector from this vector.
Definition: vector.h:1048
vec add(const vec &toAdd) const
Adds a vector to this vector.
Definition: vector.h:185
vec add(const vec &toAdd) const
Adds a vector to this vector.
Definition: vector.h:798
vec & operator-=(const vec &rhs)
Subtracts a vector from this vector.
Definition: vector.h:114
T r
Red (0-component).
Definition: vector.h:575
vec operator-() const
Negates the vector.
Definition: vector.h:1025