18 #ifndef ION_MATH_VECTOR_H_
19 #define ION_MATH_VECTOR_H_
27 #include "base/integral_types.h"
42 template <
int Dimension,
typename T>
52 void Set(
T e0,
T e1,
T e2);
53 void Set(
T e0,
T e1,
T e2,
T e3);
61 DCHECK(index >= 0 && index < Dimension);
71 DCHECK(index >= 0 && index < Dimension);
80 const T*
Data()
const {
return &elem_[0]; }
83 void Print(std::ostream& out,
const char tag)
const {
85 for (
int i = 0; i < Dimension; ++i) {
87 if (i != Dimension - 1)
95 void Read(std::istream& in) {
98 for (
int i = 0; i < Dimension; ++i) {
100 if (i != Dimension - 1 && !base::GetExpectedChar<','>(in))
179 template <
int Dimension,
typename T>
248 return Vector(s / v[0], s / v[1], s / v[2]);
264 static const Vector ToVector(
const BaseType& b) {
267 return static_cast<const Vector&
>(b);
271 template<
int D,
typename U>
friend class Point;
275 template <
int Dimension,
typename T>
276 std::ostream& operator<<(std::ostream& out, const Vector<Dimension, T>& v) {
282 template <
int Dimension,
typename T>
284 v.
template Read<'V'>(in);
295 template <
int Dimension,
typename T>
320 static const Point Zero() {
return ToPoint(BaseType::Zero()); }
328 void operator-=(
const VectorType& v) { BaseType::Subtract(v); }
337 return ToPoint(BaseType::Sum(p0, p1));
341 return ToPoint(BaseType::Sum(p, v));
344 return ToPoint(BaseType::Sum(p, v));
349 return ToPoint(BaseType::Difference(p, v));
353 return ToVector(BaseType::Difference(p0, p1));
358 return ToPoint(BaseType::Scale(p, s));
362 return ToPoint(BaseType::Scale(p, s));
365 return ToPoint(BaseType::Product(v, s));
368 return ToPoint(BaseType::Quotient(v, s));
371 return ToPoint(BaseType::Divide(p, s));
376 return BaseType::AreValuesEqual(p0, p1);
379 return !BaseType::AreValuesEqual(p0, p1);
387 static const Point ToPoint(
const BaseType& b) {
390 return *
static_cast<const Point*
>(&b);
393 static const VectorType ToVector(
const BaseType& b) {
394 return VectorType::ToVector(b);
399 template <
int Dimension,
typename T>
400 std::ostream& operator<<(std::ostream& out, const Point<Dimension, T>& p) {
406 template <
int Dimension,
typename T>
408 v.
template Read<'P'>(in);
418 template <
int Dimension,
typename T>
424 template <
int Dimension,
typename T>
431 template <
int Dimension,
typename T>
439 template <
int Dimension,
typename T>
448 template <
int Dimension,
typename T>
452 for (
int i = 0; i < Dimension - 1; ++i)
454 elem_[Dimension - 1] = s;
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]);
463 template <
int Dimension,
typename T>
469 template <
int Dimension,
typename T>
476 template <
int Dimension,
typename T>
484 template <
int Dimension,
typename T>
494 template <
int Dimension,
typename T>
495 template <
typename U>
500 return Vector(static_cast<U>(1));
502 static const VectorBase
Fill(U
value) {
return VectorBase(value); }
505 template <
int Dimension,
typename T>
506 template <
typename U>
511 return Vector(static_cast<U>(1), static_cast<U>(0));
514 return Vector(static_cast<U>(0), static_cast<U>(1));
519 template <
int Dimension,
typename T>
520 template <
typename U>
525 return Vector(static_cast<U>(1), static_cast<U>(0), static_cast<U>(0));
528 return Vector(static_cast<U>(0), static_cast<U>(1), static_cast<U>(0));
531 return Vector(static_cast<U>(0), static_cast<U>(0), static_cast<U>(1));
538 template <
int Dimension,
typename T>
539 template <
typename U>
544 return Vector(static_cast<U>(1), static_cast<U>(0), static_cast<U>(0),
548 return Vector(static_cast<U>(0), static_cast<U>(1), static_cast<U>(0),
552 return Vector(static_cast<U>(0), static_cast<U>(0), static_cast<U>(1),
556 return Vector(static_cast<U>(0), static_cast<U>(0), static_cast<U>(0),
560 return VectorBase(value, value, value, value);
565 template <
int Dimension,
typename T>
572 template <
int Dimension,
typename T>
577 template <
int Dimension,
typename T>
584 template <
int Dimension,
typename T>
591 template <
int Dimension,
typename T>
598 template <
int Dimension,
typename T>
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]);
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]);
617 template <
int Dimension,
typename T>
619 for (
int i = 0; i < Dimension; ++i)
620 elem_[i] = static_cast<T>(elem_[i] * s);
623 template <
int Dimension,
typename T>
627 for (
int i = 0; i < Dimension; ++i)
628 elem_[i] = static_cast<T>(elem_[i] / s);
631 template <
int Dimension,
typename T>
634 for (
int i = 0; i < Dimension; ++i)
635 result.elem_[i] = static_cast<T>(-elem_[i]);
639 template <
int Dimension,
typename T>
643 for (
int i = 0; i < Dimension; ++i)
644 result.elem_[i] = static_cast<T>(v0.elem_[i] * v1.elem_[i]);
648 template <
int Dimension,
typename T>
652 for (
int i = 0; i < Dimension; ++i)
653 result.elem_[i] = static_cast<T>(v0.elem_[i] / v1.elem_[i]);
657 template <
int Dimension,
typename T>
661 for (
int i = 0; i < Dimension; ++i)
662 result.elem_[i] = static_cast<T>(v0.elem_[i] + v1.elem_[i]);
666 template <
int Dimension,
typename T>
670 for (
int i = 0; i < Dimension; ++i)
671 result.elem_[i] = static_cast<T>(v0.elem_[i] - v1.elem_[i]);
675 template <
int Dimension,
typename T>
679 for (
int i = 0; i < Dimension; ++i)
680 result.elem_[i] = static_cast<T>(v.elem_[i] * s);
684 template <
int Dimension,
typename T>
688 for (
int i = 0; i < Dimension; ++i)
689 result.elem_[i] = static_cast<T>(v.elem_[i] / s);
693 template <
int Dimension,
typename T>
696 for (
int i = 0; i < Dimension; ++i) {
697 if (v0.elem_[i] != v1.elem_[i])
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
747 #undef ION_INSTANTIATE_VECTOR_TYPE
752 #endif // ION_MATH_VECTOR_H_
static const Vector AxisZ()
friend const Vector operator/(T s, const Vector &v)
friend const Vector operator+(const Vector &v0, const Vector &v1)
Binary operators.
Vector(T e0, T e1, T e2, T e3)
friend const Point operator-(const Point &p, const VectorType &v)
Subtracting a Vector from a Point produces another Point.
static const VectorBase Fill(U value)
static bool AreValuesEqual(const VectorBase &v0, const VectorBase &v1)
Returns true if all values in two instances are equal.
const Vector operator-() const
Unary negation operator.
void operator-=(const VectorType &v)
friend bool operator!=(const Point &p0, const Point &p1)
T * Data()
Returns a pointer to the data for interfacing with other libraries.
friend bool operator==(const Point &p0, const Point &p1)
Exact equality and inequality comparisons.
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...
friend const Point operator/(const Point &p, T s)
math::Vector< 4, U > Vector
const T & operator[](int index) const
Read-only element accessor.
static const VectorBase Fill(U value)
Point()
The default constructor zero-intializes all elements.
void Print(std::ostream &out, const char tag) const
This is used for printing Vectors and Points to a stream.
static const Vector Zero()
Returns a Vector containing all zeroes.
friend bool operator!=(const Vector &v0, const Vector &v1)
static const VectorBase Fill(U value)
const VectorBase Negation() const
Unary negation.
Point(T e0)
Dimension-specific constructors that are passed individual element values.
math::Vector< 1, U > Vector
friend const Vector operator*(const Vector &v, const Vector &s)
static const Vector AxisX()
friend const Point operator+(const Point &p0, const Point &p1)
Adding two Points produces another Point.
static const Vector AxisW()
Returns a Vector representing the W axis if it exists.
void Read(std::istream &in)
This is used for reading Vectors and Points from a stream.
static const Vector AxisW()
math::Vector< 3, U > Vector
static const Vector Fill(T value)
Returns a Vector with all elements set to the given value.
static const Vector AxisZ()
Returns a Vector representing the Z axis if it exists.
math::VectorBase< 1, U > VectorBase
friend const VectorType operator-(const Point &p0, const Point &p1)
Subtracting two Points results in a Vector.
friend bool operator==(const Vector &v0, const Vector &v1)
Exact equality and inequality comparisons.
Vector()
The default constructor zero-initializes all elements.
math::Vector< 2, U > Vector
Vector(const Vector< Dimension, U > &v)
Copy constructor from a Vector of the same Dimension and any value type that is compatible (via stati...
void Set(T e0)
Sets the vector values.
friend const Vector operator-(const Vector &v0, const Vector &v1)
static const VectorBase Fill(T value)
Returns an instance with all elements set to the given value.
friend const Point operator/(const Point &v, const Point &s)
void Subtract(const VectorBase &v)
Self-modifying subtraction.
static const Vector AxisX()
Point(const Point< Dimension, U > &p)
Copy constructor from a Point of the same Dimension and any value type that is compatible (via static...
Vector(T e0)
Dimension-specific constructors that are passed individual element values.
void operator+=(const Vector &v)
Self-modifying operators.
void operator+=(const Point &v)
Self-modifying operators.
Helper struct to aid in Zero, Fill, and Axis functions.
friend const Point operator+(const VectorType &v, const Point &p)
friend const Vector operator/(const Vector &v, T s)
math::VectorBase< 3, U > VectorBase
std::istream & GetExpectedChar(std::istream &in)
Reads a single character from the stream and returns the stream.
const Point operator-() const
Unary operators.
void Add(const VectorBase &v)
Derived classes use these protected functions to implement type-safe functions and operators...
friend const Vector operator*(T s, const Vector &v)
Copyright 2016 Google Inc.
static const Vector AxisY()
Returns a Vector representing the Y axis if it exists.
Point(T e0, T e1, T e2, T e3)
friend const Point operator*(const Point &p, T s)
Binary scale and division operators.
static const Vector AxisZ()
static const VectorBase Quotient(const VectorBase &v0, const VectorBase &v1)
Binary component-wise division.
static const Vector AxisY()
math::VectorBase< 4, U > VectorBase
static const VectorBase Scale(const VectorBase &v, T s)
Binary multiplication by a scalar.
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...
static const VectorBase Difference(const VectorBase &v0, const VectorBase &v1)
Binary component-wise subtraction.
friend const Point operator*(T s, const Point &p)
static const Vector AxisY()
static const Point Fill(T value)
Returns a Point with all elements set to the given value.
static const VectorBase Product(const VectorBase &v0, const VectorBase &v1)
Binary component-wise multiplication.
VectorBase()
The default constructor zero-initializes all elements.
static const Vector AxisY()
void operator-=(const Vector &v)
static const Point Zero()
Returns a Point containing all zeroes.
math::VectorBase< 2, U > VectorBase
friend const Point operator*(const Point &v, const Point &s)
void operator+=(const VectorType &v)
#define ION_STATIC_ASSERT(expr, message)
Copyright 2016 Google Inc.
static const VectorBase Sum(const VectorBase &v0, const VectorBase &v1)
Binary component-wise addition.
std::istream & operator>>(std::istream &in, Angle< T > &a)
Vector< Dimension, T > VectorType
Convenience typedef for the corresponding Vector type.
ION_INSTANTIATE_VECTOR_TYPE(VectorBase)
T & operator[](int index)
Mutable element accessor.
friend const Point operator+(const Point &p, const VectorType &v)
Adding a Vector to a Point produces another Point.
static const Vector AxisX()
Returns a Vector representing the X axis.
static const Vector AxisX()
static const VectorBase Fill(U value)
void Divide(T s)
Self-modifying division by a scalar.
friend const Vector operator*(const Vector &v, T s)
void Multiply(T s)
Self-modifying multiplication by a scalar.
static const VectorBase Zero()
Returns an instance containing all zeroes.
friend const Vector operator/(const Vector &v, const Vector &s)
static const Vector AxisX()