Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vector.h
Go to the documentation of this file.
1 
18 #ifndef ION_MATH_VECTOR_H_
19 #define ION_MATH_VECTOR_H_
20 
26 
27 #include "base/integral_types.h"
28 #include "ion/base/logging.h"
29 #include "ion/base/static_assert.h"
30 #include "ion/base/stringutils.h"
31 
32 namespace ion {
33 namespace math {
34 
36 
39 
40 
42 template <int Dimension, typename T>
43 class VectorBase {
44  public:
46  enum { kDimension = Dimension };
47  typedef T ValueType;
48 
50  void Set(T e0); // Only when Dimension == 1.
51  void Set(T e0, T e1); // Only when Dimension == 2.
52  void Set(T e0, T e1, T e2); // Only when Dimension == 3.
53  void Set(T e0, T e1, T e2, T e3); // Only when Dimension == 4.
54 
56  T& operator[](int index) {
61  DCHECK(index >= 0 && index < Dimension);
62  return elem_[index];
63  }
64 
66  const T& operator[](int index) const {
71  DCHECK(index >= 0 && index < Dimension);
72  return elem_[index];
73  }
74 
76  static bool AreValuesEqual(const VectorBase& v0, const VectorBase& v1);
77 
79  T* Data() { return &elem_[0]; }
80  const T* Data() const { return &elem_[0]; }
81 
83  void Print(std::ostream& out, const char tag) const { // NOLINT
84  out << tag << "[";
85  for (int i = 0; i < Dimension; ++i) {
86  out << elem_[i];
87  if (i != Dimension - 1)
88  out << ", ";
89  }
90  out << "]";
91  }
92 
94  template <char tag>
95  void Read(std::istream& in) { // NOLINT
96  VectorBase v;
97  if (base::GetExpectedChar<tag>(in) && base::GetExpectedChar<'['>(in)) {
98  for (int i = 0; i < Dimension; ++i) {
99  in >> v.elem_[i];
100  if (i != Dimension - 1 && !base::GetExpectedChar<','>(in))
101  return;
102  }
103  if (base::GetExpectedChar<']'>(in))
104  *this = v;
105  }
106  }
107 
108  protected:
110  VectorBase() : elem_() {}
111 
112  explicit VectorBase(T e0); // Only when Dimension == 1.
113  VectorBase(T e0, T e1); // Only when Dimension == 2.
114  VectorBase(T e0, T e1, T e2); // Only when Dimension == 3.
115  VectorBase(T e0, T e1, T e2, T e3); // Only when Dimension == 4.
116 
121 
124  template <typename U> explicit VectorBase(const VectorBase<Dimension, U>& v);
125 
127  static const VectorBase Zero();
128 
130  static const VectorBase Fill(T value);
131 
136 
138  void Add(const VectorBase& v);
140  void Subtract(const VectorBase& v);
142  void Multiply(T s);
144  void Divide(T s);
145 
147  const VectorBase Negation() const;
148 
150  static const VectorBase Product(const VectorBase& v0, const VectorBase& v1);
152  static const VectorBase Quotient(const VectorBase& v0, const VectorBase& v1);
154  static const VectorBase Sum(const VectorBase& v0, const VectorBase& v1);
156  static const VectorBase Difference(const VectorBase& v0,
157  const VectorBase& v1);
159  static const VectorBase Scale(const VectorBase& v, T s);
161  static const VectorBase Divide(const VectorBase& v, T s);
162 
165  template <int Dim, typename U> struct StaticHelper;
166 
167  private:
169  T elem_[Dimension];
170 };
171 
173 
176 
177 
179 template <int Dimension, typename T>
180 class Vector : public VectorBase<Dimension, T> {
181  public:
183  Vector() : BaseType() {}
184 
186  explicit Vector(T e0) : BaseType(e0) {}
187  Vector(T e0, T e1) : BaseType(e0, e1) {}
188  Vector(T e0, T e1, T e2) : BaseType(e0, e1, e2) {}
189  Vector(T e0, T e1, T e2, T e3) : BaseType(e0, e1, e2, e3) {}
190 
193  Vector(const Vector<Dimension - 1, T>& v, T s) : BaseType(v, s) {}
194 
197  template <typename U> explicit Vector(const Vector<Dimension, U>& v)
198  : BaseType(v) {}
199 
201  static const Vector Zero() { return ToVector(BaseType::Zero()); }
202 
204  static const Vector Fill(T value) { return ToVector(BaseType::Fill(value)); }
205 
207  static const Vector AxisX();
209  static const Vector AxisY();
211  static const Vector AxisZ();
213  static const Vector AxisW();
214 
216  void operator+=(const Vector& v) { BaseType::Add(v); }
217  void operator-=(const Vector& v) { BaseType::Subtract(v); }
219  void operator/=(T s) { BaseType::Divide(s); }
220 
222  const Vector operator-() const { return ToVector(BaseType::Negation()); }
223 
225  friend const Vector operator+(const Vector& v0, const Vector& v1) {
226  return ToVector(BaseType::Sum(v0, v1));
227  }
228  friend const Vector operator-(const Vector& v0, const Vector& v1) {
229  return ToVector(BaseType::Difference(v0, v1));
230  }
231  friend const Vector operator*(const Vector& v, T s) {
232  return ToVector(BaseType::Scale(v, s));
233  }
234  friend const Vector operator*(T s, const Vector& v) {
236  return ToVector(BaseType::Scale(v, s));
237  }
238  friend const Vector operator*(const Vector& v, const Vector& s) {
239  return ToVector(BaseType::Product(v, s));
240  }
241  friend const Vector operator/(const Vector& v, const Vector& s) {
242  return ToVector(BaseType::Quotient(v, s));
243  }
244  friend const Vector operator/(const Vector& v, T s) {
245  return ToVector(BaseType::Divide(v, s));
246  }
247  friend const Vector operator/(T s, const Vector& v) {
248  return Vector(s / v[0], s / v[1], s / v[2]);
249  }
250 
252  friend bool operator==(const Vector& v0, const Vector& v1) {
253  return BaseType::AreValuesEqual(v0, v1);
254  }
255  friend bool operator!=(const Vector& v0, const Vector& v1) {
256  return !BaseType::AreValuesEqual(v0, v1);
257  }
258 
259  private:
261  typedef VectorBase<Dimension, T> BaseType;
262 
264  static const Vector ToVector(const BaseType& b) {
267  return static_cast<const Vector&>(b);
268  }
269 
271  template<int D, typename U> friend class Point;
272 };
273 
275 template <int Dimension, typename T>
276 std::ostream& operator<<(std::ostream& out, const Vector<Dimension, T>& v) {
277  v.Print(out, 'V');
278  return out;
279 }
280 
282 template <int Dimension, typename T>
283 std::istream& operator>>(std::istream& in, Vector<Dimension, T>& v) {
284  v. template Read<'V'>(in);
285  return in;
286 }
287 
289 
292 
293 
295 template <int Dimension, typename T>
297  public:
300 
302  Point() : BaseType() {}
303 
305  explicit Point(T e0) : BaseType(e0) {}
306  Point(T e0, T e1) : BaseType(e0, e1) {}
307  Point(T e0, T e1, T e2) : BaseType(e0, e1, e2) {}
308  Point(T e0, T e1, T e2, T e3) : BaseType(e0, e1, e2, e3) {}
309 
312  Point(const Point<Dimension - 1, T>& p, T s) : BaseType(p, s) {}
313 
316  template <typename U> explicit Point(const Point<Dimension, U>& p)
317  : BaseType(p) {}
318 
320  static const Point Zero() { return ToPoint(BaseType::Zero()); }
321 
323  static const Point Fill(T value) { return ToPoint(BaseType::Fill(value)); }
324 
326  void operator+=(const Point& v) { BaseType::Add(v); }
327  void operator+=(const VectorType& v) { BaseType::Add(v); }
328  void operator-=(const VectorType& v) { BaseType::Subtract(v); }
329  void operator*=(T s) { BaseType::Multiply(s); }
330  void operator/=(T s) { BaseType::Divide(s); }
331 
333  const Point operator-() const { return ToPoint(BaseType::Negation()); }
334 
336  friend const Point operator+(const Point& p0, const Point& p1) {
337  return ToPoint(BaseType::Sum(p0, p1));
338  }
340  friend const Point operator+(const Point& p, const VectorType& v) {
341  return ToPoint(BaseType::Sum(p, v));
342  }
343  friend const Point operator+(const VectorType& v, const Point& p) {
344  return ToPoint(BaseType::Sum(p, v));
345  }
346 
348  friend const Point operator-(const Point& p, const VectorType& v) {
349  return ToPoint(BaseType::Difference(p, v));
350  }
352  friend const VectorType operator-(const Point& p0, const Point& p1) {
353  return ToVector(BaseType::Difference(p0, p1));
354  }
355 
357  friend const Point operator*(const Point& p, T s) {
358  return ToPoint(BaseType::Scale(p, s));
359  }
360  friend const Point operator*(T s, const Point& p) {
362  return ToPoint(BaseType::Scale(p, s));
363  }
364  friend const Point operator*(const Point& v, const Point& s) {
365  return ToPoint(BaseType::Product(v, s));
366  }
367  friend const Point operator/(const Point& v, const Point& s) {
368  return ToPoint(BaseType::Quotient(v, s));
369  }
370  friend const Point operator/(const Point& p, T s) {
371  return ToPoint(BaseType::Divide(p, s));
372  }
373 
375  friend bool operator==(const Point& p0, const Point& p1) {
376  return BaseType::AreValuesEqual(p0, p1);
377  }
378  friend bool operator!=(const Point& p0, const Point& p1) {
379  return !BaseType::AreValuesEqual(p0, p1);
380  }
381 
382  private:
384  typedef VectorBase<Dimension, T> BaseType;
385 
387  static const Point ToPoint(const BaseType& b) {
390  return *static_cast<const Point*>(&b);
391  }
393  static const VectorType ToVector(const BaseType& b) {
394  return VectorType::ToVector(b);
395  }
396 };
397 
399 template <int Dimension, typename T>
400 std::ostream& operator<<(std::ostream& out, const Point<Dimension, T>& p) {
401  p.Print(out, 'P');
402  return out;
403 }
404 
406 template <int Dimension, typename T>
407 std::istream& operator>>(std::istream& in, Point<Dimension, T>& v) {
408  v. template Read<'P'>(in);
409  return in;
410 }
411 
413 
416 
417 
418 template <int Dimension, typename T>
420  ION_STATIC_ASSERT(Dimension == 1, "Bad Dimension in VectorBase constructor");
421  elem_[0] = e0;
422 }
423 
424 template <int Dimension, typename T>
426  ION_STATIC_ASSERT(Dimension == 2, "Bad Dimension in VectorBase constructor");
427  elem_[0] = e0;
428  elem_[1] = e1;
429 }
430 
431 template <int Dimension, typename T>
433  ION_STATIC_ASSERT(Dimension == 3, "Bad Dimension in VectorBase constructor");
434  elem_[0] = e0;
435  elem_[1] = e1;
436  elem_[2] = e2;
437 }
438 
439 template <int Dimension, typename T>
441  ION_STATIC_ASSERT(Dimension == 4, "Bad Dimension in VectorBase constructor");
442  elem_[0] = e0;
443  elem_[1] = e1;
444  elem_[2] = e2;
445  elem_[3] = e3;
446 }
447 
448 template <int Dimension, typename T>
450  T s) {
451  ION_STATIC_ASSERT(Dimension >= 2, "Bad Dimension in VectorBase constructor");
452  for (int i = 0; i < Dimension - 1; ++i)
453  elem_[i] = v[i];
454  elem_[Dimension - 1] = s;
455 }
456 
457 template <int Dimension, typename T> template <typename U>
459  for (int i = 0; i < Dimension; ++i)
460  elem_[i] = static_cast<T>(v[i]);
461 }
462 
463 template <int Dimension, typename T>
465  ION_STATIC_ASSERT(Dimension == 1, "Bad Dimension in VectorBase::Set");
466  elem_[0] = e0;
467 }
468 
469 template <int Dimension, typename T>
471  ION_STATIC_ASSERT(Dimension == 2, "Bad Dimension in VectorBase::Set");
472  elem_[0] = e0;
473  elem_[1] = e1;
474 }
475 
476 template <int Dimension, typename T>
477 void VectorBase<Dimension, T>::Set(T e0, T e1, T e2) {
478  ION_STATIC_ASSERT(Dimension == 3, "Bad Dimension in VectorBase::Set");
479  elem_[0] = e0;
480  elem_[1] = e1;
481  elem_[2] = e2;
482 }
483 
484 template <int Dimension, typename T>
485 void VectorBase<Dimension, T>::Set(T e0, T e1, T e2, T e3) {
486  ION_STATIC_ASSERT(Dimension == 4, "Bad Dimension in VectorBase::Set");
487  elem_[0] = e0;
488  elem_[1] = e1;
489  elem_[2] = e2;
490  elem_[3] = e3;
491 }
492 
494 template <int Dimension, typename T>
495 template <typename U>
499  static const Vector AxisX() {
500  return Vector(static_cast<U>(1));
501  }
502  static const VectorBase Fill(U value) { return VectorBase(value); }
503 };
504 
505 template <int Dimension, typename T>
506 template <typename U>
507 struct VectorBase<Dimension, T>::StaticHelper<2, U> {
510  static const Vector AxisX() {
511  return Vector(static_cast<U>(1), static_cast<U>(0));
512  }
513  static const Vector AxisY() {
514  return Vector(static_cast<U>(0), static_cast<U>(1));
515  }
516  static const VectorBase Fill(U value) { return VectorBase(value, value); }
517 };
518 
519 template <int Dimension, typename T>
520 template <typename U>
521 struct VectorBase<Dimension, T>::StaticHelper<3, U> {
524  static const Vector AxisX() {
525  return Vector(static_cast<U>(1), static_cast<U>(0), static_cast<U>(0));
526  }
527  static const Vector AxisY() {
528  return Vector(static_cast<U>(0), static_cast<U>(1), static_cast<U>(0));
529  }
530  static const Vector AxisZ() {
531  return Vector(static_cast<U>(0), static_cast<U>(0), static_cast<U>(1));
532  }
533  static const VectorBase Fill(U value) {
534  return VectorBase(value, value, value);
535  }
536 };
537 
538 template <int Dimension, typename T>
539 template <typename U>
540 struct VectorBase<Dimension, T>::StaticHelper<4, U> {
543  static const Vector AxisX() {
544  return Vector(static_cast<U>(1), static_cast<U>(0), static_cast<U>(0),
545  static_cast<U>(0));
546  }
547  static const Vector AxisY() {
548  return Vector(static_cast<U>(0), static_cast<U>(1), static_cast<U>(0),
549  static_cast<U>(0));
550  }
551  static const Vector AxisZ() {
552  return Vector(static_cast<U>(0), static_cast<U>(0), static_cast<U>(1),
553  static_cast<U>(0));
554  }
555  static const Vector AxisW() {
556  return Vector(static_cast<U>(0), static_cast<U>(0), static_cast<U>(0),
557  static_cast<U>(1));
558  }
559  static const VectorBase Fill(U value) {
560  return VectorBase(value, value, value, value);
561  }
562 };
563 
565 template <int Dimension, typename T>
567  static VectorBase<Dimension, T> v =
568  StaticHelper<Dimension, T>::Fill(static_cast<T>(0));
569  return v;
570 }
571 
572 template <int Dimension, typename T>
574  return StaticHelper<Dimension, T>::Fill(value);
575 }
576 
577 template <int Dimension, typename T>
579  static const Vector<Dimension, T> v =
580  BaseType::template StaticHelper<Dimension, T>::AxisX();
581  return v;
582 }
583 
584 template <int Dimension, typename T>
586  static const Vector<Dimension, T> v =
587  BaseType::template StaticHelper<Dimension, T>::AxisY();
588  return v;
589 }
590 
591 template <int Dimension, typename T>
593  static const Vector<Dimension, T> v =
594  BaseType::template StaticHelper<Dimension, T>::AxisZ();
595  return v;
596 }
597 
598 template <int Dimension, typename T>
600  static const Vector<Dimension, T> v =
601  BaseType::template StaticHelper<Dimension, T>::AxisW();
602  return v;
603 }
604 
605 template <int Dimension, typename T>
607  for (int i = 0; i < Dimension; ++i)
608  elem_[i] = static_cast<T>(elem_[i] + v.elem_[i]);
609 }
610 
611 template <int Dimension, typename T>
613  for (int i = 0; i < Dimension; ++i)
614  elem_[i] = static_cast<T>(elem_[i] - v.elem_[i]);
615 }
616 
617 template <int Dimension, typename T>
619  for (int i = 0; i < Dimension; ++i)
620  elem_[i] = static_cast<T>(elem_[i] * s);
621 }
622 
623 template <int Dimension, typename T>
627  for (int i = 0; i < Dimension; ++i)
628  elem_[i] = static_cast<T>(elem_[i] / s);
629 }
630 
631 template <int Dimension, typename T>
633  VectorBase result;
634  for (int i = 0; i < Dimension; ++i)
635  result.elem_[i] = static_cast<T>(-elem_[i]);
636  return result;
637 }
638 
639 template <int Dimension, typename T>
641  const VectorBase& v0, const VectorBase& v1) {
642  VectorBase result;
643  for (int i = 0; i < Dimension; ++i)
644  result.elem_[i] = static_cast<T>(v0.elem_[i] * v1.elem_[i]);
645  return result;
646 }
647 
648 template <int Dimension, typename T>
650  const VectorBase& v0, const VectorBase& v1) {
651  VectorBase result;
652  for (int i = 0; i < Dimension; ++i)
653  result.elem_[i] = static_cast<T>(v0.elem_[i] / v1.elem_[i]);
654  return result;
655 }
656 
657 template <int Dimension, typename T>
659  const VectorBase& v0, const VectorBase& v1) {
660  VectorBase result;
661  for (int i = 0; i < Dimension; ++i)
662  result.elem_[i] = static_cast<T>(v0.elem_[i] + v1.elem_[i]);
663  return result;
664 }
665 
666 template <int Dimension, typename T>
668  const VectorBase& v0, const VectorBase& v1) {
669  VectorBase result;
670  for (int i = 0; i < Dimension; ++i)
671  result.elem_[i] = static_cast<T>(v0.elem_[i] - v1.elem_[i]);
672  return result;
673 }
674 
675 template <int Dimension, typename T>
677  const VectorBase& v, T s) {
678  VectorBase result;
679  for (int i = 0; i < Dimension; ++i)
680  result.elem_[i] = static_cast<T>(v.elem_[i] * s);
681  return result;
682 }
683 
684 template <int Dimension, typename T>
686  const VectorBase& v, T s) {
687  VectorBase result;
688  for (int i = 0; i < Dimension; ++i)
689  result.elem_[i] = static_cast<T>(v.elem_[i] / s);
690  return result;
691 }
692 
693 template <int Dimension, typename T>
695  const VectorBase& v1) {
696  for (int i = 0; i < Dimension; ++i) {
697  if (v0.elem_[i] != v1.elem_[i])
698  return false;
699  }
700  return true;
701 }
702 
704 
707 
708 
709 #define ION_INSTANTIATE_VECTOR_TYPE(type) \
710 typedef type<1, int8> type ## 1i8; \
711 typedef type<1, uint8> type ## 1ui8; \
712 typedef type<1, int16> type ## 1i16; \
713 typedef type<1, uint16> type ## 1ui16; \
714 typedef type<1, int32> type ## 1i; \
715 typedef type<1, uint32> type ## 1ui; \
716 typedef type<1, float> type ## 1f; \
717 typedef type<1, double> type ## 1d; \
718 typedef type<2, int8> type ## 2i8; \
719 typedef type<2, uint8> type ## 2ui8; \
720 typedef type<2, int16> type ## 2i16; \
721 typedef type<2, uint16> type ## 2ui16; \
722 typedef type<2, int32> type ## 2i; \
723 typedef type<2, uint32> type ## 2ui; \
724 typedef type<2, float> type ## 2f; \
725 typedef type<2, double> type ## 2d; \
726 typedef type<3, int8> type ## 3i8; \
727 typedef type<3, uint8> type ## 3ui8; \
728 typedef type<3, int16> type ## 3i16; \
729 typedef type<3, uint16> type ## 3ui16; \
730 typedef type<3, int32> type ## 3i; \
731 typedef type<3, uint32> type ## 3ui; \
732 typedef type<3, float> type ## 3f; \
733 typedef type<3, double> type ## 3d; \
734 typedef type<4, int8> type ## 4i8; \
735 typedef type<4, uint8> type ## 4ui8; \
736 typedef type<4, int16> type ## 4i16; \
737 typedef type<4, uint16> type ## 4ui16; \
738 typedef type<4, int32> type ## 4i; \
739 typedef type<4, uint32> type ## 4ui; \
740 typedef type<4, float> type ## 4f; \
741 typedef type<4, double> type ## 4d
742 
743 ION_INSTANTIATE_VECTOR_TYPE(VectorBase);
746 
747 #undef ION_INSTANTIATE_VECTOR_TYPE
748 
749 } // namespace math
750 } // namespace ion
751 
752 #endif // ION_MATH_VECTOR_H_
Vector(T e0, T e1)
Definition: vector.h:187
friend const Vector operator/(T s, const Vector &v)
Definition: vector.h:247
friend const Vector operator+(const Vector &v0, const Vector &v1)
Binary operators.
Definition: vector.h:225
Vector(T e0, T e1, T e2, T e3)
Definition: vector.h:189
void operator/=(T s)
Definition: vector.h:219
friend const Point operator-(const Point &p, const VectorType &v)
Subtracting a Vector from a Point produces another Point.
Definition: vector.h:348
static const VectorBase Fill(U value)
Definition: vector.h:559
VectorBase.
Definition: vector.h:43
static bool AreValuesEqual(const VectorBase &v0, const VectorBase &v1)
Returns true if all values in two instances are equal.
Definition: vector.h:694
const Vector operator-() const
Unary negation operator.
Definition: vector.h:222
void operator-=(const VectorType &v)
Definition: vector.h:328
friend bool operator!=(const Point &p0, const Point &p1)
Definition: vector.h:378
T * Data()
Returns a pointer to the data for interfacing with other libraries.
Definition: vector.h:79
friend bool operator==(const Point &p0, const Point &p1)
Exact equality and inequality comparisons.
Definition: vector.h:375
Vector(const Vector< Dimension-1, T > &v, T s)
Constructor for a Vector of dimension N from a Vector of dimension N-1 and a scalar of the correct ty...
Definition: vector.h:193
friend const Point operator/(const Point &p, T s)
Definition: vector.h:370
const T & operator[](int index) const
Read-only element accessor.
Definition: vector.h:66
static const VectorBase Fill(U value)
Definition: vector.h:502
Point()
The default constructor zero-intializes all elements.
Definition: vector.h:302
Point(T e0, T e1)
Definition: vector.h:306
void Print(std::ostream &out, const char tag) const
This is used for printing Vectors and Points to a stream.
Definition: vector.h:83
static const Vector Zero()
Returns a Vector containing all zeroes.
Definition: vector.h:201
friend bool operator!=(const Vector &v0, const Vector &v1)
Definition: vector.h:255
static const VectorBase Fill(U value)
Definition: vector.h:533
const VectorBase Negation() const
Unary negation.
Definition: vector.h:632
Point(T e0)
Dimension-specific constructors that are passed individual element values.
Definition: vector.h:305
#define DCHECK(expr)
Definition: logging.h:331
double value
friend const Vector operator*(const Vector &v, const Vector &s)
Definition: vector.h:238
friend const Point operator+(const Point &p0, const Point &p1)
Adding two Points produces another Point.
Definition: vector.h:336
Point(T e0, T e1, T e2)
Definition: vector.h:307
static const Vector AxisW()
Returns a Vector representing the W axis if it exists.
Definition: vector.h:599
Vector(T e0, T e1, T e2)
Definition: vector.h:188
void Read(std::istream &in)
This is used for reading Vectors and Points from a stream.
Definition: vector.h:95
static const Vector Fill(T value)
Returns a Vector with all elements set to the given value.
Definition: vector.h:204
static const Vector AxisZ()
Returns a Vector representing the Z axis if it exists.
Definition: vector.h:592
math::VectorBase< 1, U > VectorBase
Definition: vector.h:498
friend const VectorType operator-(const Point &p0, const Point &p1)
Subtracting two Points results in a Vector.
Definition: vector.h:352
friend bool operator==(const Vector &v0, const Vector &v1)
Exact equality and inequality comparisons.
Definition: vector.h:252
Vector()
The default constructor zero-initializes all elements.
Definition: vector.h:183
void operator*=(T s)
Definition: vector.h:218
Vector(const Vector< Dimension, U > &v)
Copy constructor from a Vector of the same Dimension and any value type that is compatible (via stati...
Definition: vector.h:197
void Set(T e0)
Sets the vector values.
Definition: vector.h:464
friend const Vector operator-(const Vector &v0, const Vector &v1)
Definition: vector.h:228
static const VectorBase Fill(T value)
Returns an instance with all elements set to the given value.
Definition: vector.h:573
friend const Point operator/(const Point &v, const Point &s)
Definition: vector.h:367
void Subtract(const VectorBase &v)
Self-modifying subtraction.
Definition: vector.h:612
Point(const Point< Dimension, U > &p)
Copy constructor from a Point of the same Dimension and any value type that is compatible (via static...
Definition: vector.h:316
Vector(T e0)
Dimension-specific constructors that are passed individual element values.
Definition: vector.h:186
void operator+=(const Vector &v)
Self-modifying operators.
Definition: vector.h:216
void operator*=(T s)
Definition: vector.h:329
void operator+=(const Point &v)
Self-modifying operators.
Definition: vector.h:326
Helper struct to aid in Zero, Fill, and Axis functions.
Definition: vector.h:165
friend const Point operator+(const VectorType &v, const Point &p)
Definition: vector.h:343
friend const Vector operator/(const Vector &v, T s)
Definition: vector.h:244
math::VectorBase< 3, U > VectorBase
Definition: vector.h:523
std::istream & GetExpectedChar(std::istream &in)
Reads a single character from the stream and returns the stream.
Definition: stringutils.h:215
const Point operator-() const
Unary operators.
Definition: vector.h:333
void Add(const VectorBase &v)
Derived classes use these protected functions to implement type-safe functions and operators...
Definition: vector.h:606
friend const Vector operator*(T s, const Vector &v)
Definition: vector.h:234
Copyright 2016 Google Inc.
static const Vector AxisY()
Returns a Vector representing the Y axis if it exists.
Definition: vector.h:585
Point(T e0, T e1, T e2, T e3)
Definition: vector.h:308
Vector.
Definition: vector.h:180
friend const Point operator*(const Point &p, T s)
Binary scale and division operators.
Definition: vector.h:357
static const VectorBase Quotient(const VectorBase &v0, const VectorBase &v1)
Binary component-wise division.
Definition: vector.h:649
math::VectorBase< 4, U > VectorBase
Definition: vector.h:542
static const VectorBase Scale(const VectorBase &v, T s)
Binary multiplication by a scalar.
Definition: vector.h:676
const T * Data() const
Definition: vector.h:80
Point(const Point< Dimension-1, T > &p, T s)
Constructor for a Point of dimension N from a Point of dimension N-1 and a scalar of the correct type...
Definition: vector.h:312
static const VectorBase Difference(const VectorBase &v0, const VectorBase &v1)
Binary component-wise subtraction.
Definition: vector.h:667
friend const Point operator*(T s, const Point &p)
Definition: vector.h:360
static const Point Fill(T value)
Returns a Point with all elements set to the given value.
Definition: vector.h:323
static const VectorBase Product(const VectorBase &v0, const VectorBase &v1)
Binary component-wise multiplication.
Definition: vector.h:640
VectorBase()
The default constructor zero-initializes all elements.
Definition: vector.h:110
void operator-=(const Vector &v)
Definition: vector.h:217
static const Point Zero()
Returns a Point containing all zeroes.
Definition: vector.h:320
math::VectorBase< 2, U > VectorBase
Definition: vector.h:509
Point.
Definition: vector.h:296
friend const Point operator*(const Point &v, const Point &s)
Definition: vector.h:364
void operator+=(const VectorType &v)
Definition: vector.h:327
#define ION_STATIC_ASSERT(expr, message)
Copyright 2016 Google Inc.
Definition: static_assert.h:35
static const VectorBase Sum(const VectorBase &v0, const VectorBase &v1)
Binary component-wise addition.
Definition: vector.h:658
std::istream & operator>>(std::istream &in, Angle< T > &a)
Definition: angle.h:135
Vector< Dimension, T > VectorType
Convenience typedef for the corresponding Vector type.
Definition: vector.h:299
ION_INSTANTIATE_VECTOR_TYPE(VectorBase)
T & operator[](int index)
Mutable element accessor.
Definition: vector.h:56
friend const Point operator+(const Point &p, const VectorType &v)
Adding a Vector to a Point produces another Point.
Definition: vector.h:340
static const Vector AxisX()
Returns a Vector representing the X axis.
Definition: vector.h:578
void operator/=(T s)
Definition: vector.h:330
static const VectorBase Fill(U value)
Definition: vector.h:516
void Divide(T s)
Self-modifying division by a scalar.
Definition: vector.h:624
friend const Vector operator*(const Vector &v, T s)
Definition: vector.h:231
void Multiply(T s)
Self-modifying multiplication by a scalar.
Definition: vector.h:618
static const VectorBase Zero()
Returns an instance containing all zeroes.
Definition: vector.h:566
friend const Vector operator/(const Vector &v, const Vector &s)
Definition: vector.h:241