///////////////////////////////////////////////////////////////////// // Linear Math Vectors // © Dan Andersson 2013 ///////////////////////////////////////////////////////////////////// #ifndef LINEARALGEBRA_VECTOR_H #define LINEARALGEBRA_VECTOR_H #include namespace LinearAlgebra { template struct Vector2 { public: union { struct { ScalarType x, y; }; ScalarType element[2]; char byte[sizeof(ScalarType[2])]; }; static const Vector2 null; static const Vector2 standard_unit_x; static const Vector2 standard_unit_y; Vector2( ); Vector2( const Vector2 &vector ); Vector2( const ScalarType &element ); Vector2( const ScalarType element[2] ); Vector2( const ScalarType &x, const ScalarType &y ); operator ScalarType* ( ); operator const ScalarType* ( ) const; operator char* ( ); operator const char* ( ) const; ScalarType & operator [] ( int i ); const ScalarType & operator [] ( int i ) const; Vector2 & operator = ( const Vector2 &vector ); Vector2 & operator = ( const ScalarType element[2] ); Vector2 & operator *= ( const ScalarType &scalar ); Vector2 & operator /= ( const ScalarType &scalar ); Vector2 & operator += ( const Vector2 &vector ); Vector2 & operator -= ( const Vector2 &vector ); Vector2 operator * ( const ScalarType &scalar ) const; Vector2 operator / ( const ScalarType &scalar ) const; Vector2 operator + ( const Vector2 &vector ) const; Vector2 operator - ( const Vector2 &vector ) const; Vector2 operator - ( ) const; // unary negation bool operator == ( const Vector2 &vector ) const; bool operator != ( const Vector2 &vector ) const; ScalarType GetLength( ) const; ScalarType GetMagnitude( ) const; ScalarType Dot( const Vector2 &vector ) const; //! @return (a.x * b.x, a.y * b.y) Vector2 PiecewiseMultiplication( const Vector2 &vector ) const; Vector2 & Normalize( ); Vector2 GetNormalized( ) const; }; template struct Vector3 { public: union { struct { ScalarType x, y, z; }; struct { Vector2 xy; }; ScalarType element[3]; char byte[sizeof(ScalarType[3])]; }; static const Vector3 null; static const Vector3 standard_unit_x; static const Vector3 standard_unit_y; static const Vector3 standard_unit_z; Vector3( ); Vector3( const Vector3 &vector ); Vector3( const Vector2 &vector, const ScalarType &z ); Vector3( const ScalarType &element ); Vector3( const ScalarType element[3] ); Vector3( const ScalarType &x, const ScalarType &y, const ScalarType &z ); operator ScalarType* (); operator const ScalarType* () const; operator char* ( ); operator const char* ( ) const; ScalarType & operator [] ( int i ); const ScalarType & operator [] ( int i ) const; Vector3 & operator = ( const Vector3 &vector ); Vector3 & operator = ( const ScalarType element[3] ); Vector3 & operator *= ( const ScalarType &scalar ); Vector3 & operator /= ( const ScalarType &scalar ); Vector3 & operator += ( const Vector3 &vector ); Vector3 & operator -= ( const Vector3 &vector ); Vector3 operator * ( const ScalarType &scalar ) const; Vector3 operator / ( const ScalarType &scalar ) const; Vector3 operator + ( const Vector3 &vector ) const; Vector3 operator - ( const Vector3 &vector ) const; Vector3 operator - ( ) const; // unary negation bool operator == ( const Vector3 &vector ) const; bool operator != ( const Vector3 &vector ) const; ScalarType GetLength( ) const; ScalarType GetMagnitude( ) const; ScalarType Dot( const Vector3 &vector ) const; Vector3 Cross( const Vector3 &vector ) const; //! @return (a.x * b.x, a.y * b.y, a.z * b.z) Vector3 PiecewiseMultiplication( const Vector3 &vector ) const; Vector3 & Normalize( ); Vector3 GetNormalized( ) const; }; template struct Vector4 { public: union { struct { ScalarType x, y, z, w; }; struct { Vector2 xy; }; struct { Vector3 xyz; }; ScalarType element[4]; char byte[sizeof(ScalarType[4])]; }; static const Vector4 null; static const Vector4 standard_unit_x; static const Vector4 standard_unit_y; static const Vector4 standard_unit_z; static const Vector4 standard_unit_w; Vector4( ); Vector4( const Vector4 &vector ); Vector4( const Vector3 &vector, const ScalarType &w ); Vector4( const Vector2 &vector, const ScalarType &z, const ScalarType &w ); Vector4( const ScalarType &element ); Vector4( const ScalarType element[4] ); Vector4( const ScalarType &x, const ScalarType &y, const ScalarType &z, const ScalarType &w ); operator ScalarType* (); operator const ScalarType* () const; operator char* ( ); operator const char* ( ) const; ScalarType & operator [] ( int i ); const ScalarType & operator [] ( int i ) const; Vector4 & operator = ( const Vector4 &vector ); Vector4 & operator = ( const ScalarType element[4] ); Vector4 & operator *= ( const ScalarType &scalar ); Vector4 & operator /= ( const ScalarType &scalar ); Vector4 & operator += ( const Vector4 &vector ); Vector4 & operator -= ( const Vector4 &vector ); Vector4 operator * ( const ScalarType &scalar ) const; Vector4 operator / ( const ScalarType &scalar ) const; Vector4 operator + ( const Vector4 &vector ) const; Vector4 operator - ( const Vector4 &vector ) const; Vector4 operator - ( ) const; // unary negation bool operator == ( const Vector4 &vector ) const; bool operator != ( const Vector4 &vector ) const; ScalarType GetLength( ) const; ScalarType GetMagnitude( ) const; ScalarType Dot( const Vector4 &vector ) const; //! @return (a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w ) Vector4 PiecewiseMultiplication( const Vector4 &vector ) const; Vector4 & Normalize( ); Vector4 GetNormalized( ) const; }; /////////////////////////////////////////////////////////////////////////////////// // Body /////////////////////////////////////////////////////////////////////////////////// // Vector2 /////////////////////////////////////// template const Vector2 Vector2::null = Vector2( 0, 0 ); template const Vector2 Vector2::standard_unit_x = Vector2( 1, 0 ); template const Vector2 Vector2::standard_unit_y = Vector2( 0, 1 ); template inline Vector2::Vector2( ) : x(), y() {} template inline Vector2::Vector2( const Vector2 &vector ) { this->x = vector.x; this->y = vector.y; } template inline Vector2::Vector2( const ScalarType &_element ) { this->x = this->y = _element; } template inline Vector2::Vector2( const ScalarType _element[2] ) { this->x = _element[0]; this->y = _element[1]; } template inline Vector2::Vector2( const ScalarType &_x, const ScalarType &_y ) { this->x = _x; this->y = _y; } template inline Vector2::operator ScalarType* () { return this->element; } template inline Vector2::operator const ScalarType* () const { return this->element; } template inline Vector2::operator char* ( ) { return this->byte; } template inline Vector2::operator const char* ( ) const { return this->byte; } template inline ScalarType & Vector2::operator [] ( int i ) { return this->element[i]; } template inline const ScalarType & Vector2::operator [] ( int i ) const { return this->element[i]; } template inline Vector2 & Vector2::operator = ( const Vector2 &vector ) { this->element[0] = vector.element[0]; this->element[1] = vector.element[1]; return *this; } template inline Vector2 & Vector2::operator = ( const ScalarType _element[2] ) { this->element[0] = _element[0]; this->element[1] = _element[1]; return *this; } template inline Vector2 & Vector2::operator *= ( const ScalarType &scalar ) { this->element[0] *= scalar; this->element[1] *= scalar; return *this; } template inline Vector2 & Vector2::operator /= ( const ScalarType &scalar ) { this->element[0] /= scalar; this->element[1] /= scalar; return *this; } template inline Vector2 & Vector2::operator += ( const Vector2 &vector ) { this->element[0] += vector.element[0]; this->element[1] += vector.element[1]; return *this; } template inline Vector2 & Vector2::operator -= ( const Vector2 &vector ) { this->element[0] -= vector.element[0]; this->element[1] -= vector.element[1]; return *this; } template inline Vector2 Vector2::operator * ( const ScalarType &scalar ) const { return Vector2(*this) *= scalar; } template inline Vector2 Vector2::operator / ( const ScalarType &scalar ) const { return Vector2(*this) /= scalar; } template inline Vector2 Vector2::operator + ( const Vector2 &vector ) const { return Vector2(*this) += vector; } template inline Vector2 Vector2::operator - ( const Vector2 &vector ) const { return Vector2(*this) -= vector; } template inline Vector2 Vector2::operator - ( ) const { return Vector2(-this->x, -this->y); } template inline bool Vector2::operator == ( const Vector2 &vector ) const { if( this->x != vector.x ) return false; if( this->y != vector.y ) return false; return true; } template inline bool Vector2::operator != ( const Vector2 &vector ) const { if( this->x != vector.x ) return true; if( this->y != vector.y ) return true; return false; } template inline ScalarType Vector2::GetLength( ) const { return this->GetMagnitude(); } template inline ScalarType Vector2::GetMagnitude( ) const { return (ScalarType) ::sqrt( this->Dot(*this) ); } template inline ScalarType Vector2::Dot( const Vector2 &vector ) const { ScalarType value = 0; value += this->element[0] * vector.element[0]; value += this->element[1] * vector.element[1]; return value; } template inline Vector2 Vector2::PiecewiseMultiplication( const Vector2 &vector ) const { return Vector2( this->x * vector.x, this->y * vector.y ); } template inline Vector2 & Vector2::Normalize( ) { return (*this) /= this->GetLength(); } template inline Vector2 Vector2::GetNormalized( ) const { return Vector2(*this).Normalize(); } // Vector3 /////////////////////////////////////// template const Vector3 Vector3::null = Vector3( 0, 0, 0 ); template const Vector3 Vector3::standard_unit_x = Vector3( 1, 0, 0 ); template const Vector3 Vector3::standard_unit_y = Vector3( 0, 1, 0 ); template const Vector3 Vector3::standard_unit_z = Vector3( 0, 0, 1 ); template inline Vector3::Vector3( ) : x(), y(), z() {} template inline Vector3::Vector3( const Vector3 &vector ) { this->x = vector.x; this->y = vector.y; this->z = vector.z; } template inline Vector3::Vector3( const Vector2 &vector, const ScalarType &_z ) { this->x = vector.x; this->y = vector.y; this->z = _z; } template inline Vector3::Vector3( const ScalarType &_element ) { this->x = this->y = this->z = _element; } template inline Vector3::Vector3( const ScalarType _element[3] ) { this->x = _element[0]; this->y = _element[1]; this->z = _element[2]; } template inline Vector3::Vector3( const ScalarType &_x, const ScalarType &_y, const ScalarType &_z ) { this->x = _x; this->y = _y; this->z = _z; } template inline Vector3::operator ScalarType* () { return this->element; } template inline Vector3::operator const ScalarType* () const { return this->element; } template inline ScalarType & Vector3::operator [] ( int i ) { return this->element[i]; } template inline const ScalarType & Vector3::operator [] ( int i ) const { return this->element[i]; } template inline Vector3 & Vector3::operator = ( const Vector3 &vector ) { this->element[0] = vector.element[0]; this->element[1] = vector.element[1]; this->element[2] = vector.element[2]; return *this; } template inline Vector3 & Vector3::operator = ( const ScalarType element[3] ) { this->element[0] = element[0]; this->element[1] = element[1]; this->element[2] = element[2]; return *this; } template inline Vector3 & Vector3::operator *= ( const ScalarType &scalar ) { this->element[0] *= scalar; this->element[1] *= scalar; this->element[2] *= scalar; return *this; } template inline Vector3 & Vector3::operator /= ( const ScalarType &scalar ) { this->element[0] /= scalar; this->element[1] /= scalar; this->element[2] /= scalar; return *this; } template inline Vector3 & Vector3::operator += ( const Vector3 &vector ) { this->element[0] += vector.element[0]; this->element[1] += vector.element[1]; this->element[2] += vector.element[2]; return *this; } template inline Vector3 & Vector3::operator -= ( const Vector3 &vector ) { this->element[0] -= vector.element[0]; this->element[1] -= vector.element[1]; this->element[2] -= vector.element[2]; return *this; } template inline Vector3 Vector3::operator * ( const ScalarType &scalar ) const { return Vector3(*this) *= scalar; } template inline Vector3 Vector3::operator / ( const ScalarType &scalar ) const { return Vector3(*this) /= scalar; } template inline Vector3 Vector3::operator + ( const Vector3 &vector ) const { return Vector3(*this) += vector; } template inline Vector3 Vector3::operator - ( const Vector3 &vector ) const { return Vector3(*this) -= vector; } template inline Vector3 Vector3::operator - ( ) const { return Vector3(-this->x, -this->y, -this->z); } template inline bool Vector3::operator == ( const Vector3 &vector ) const { if( this->x != vector.x ) return false; if( this->y != vector.y ) return false; if( this->z != vector.z ) return false; return true; } template inline bool Vector3::operator != ( const Vector3 &vector ) const { if( this->x != vector.x ) return true; if( this->y != vector.y ) return true; if( this->z != vector.z ) return true; return false; } template inline ScalarType Vector3::GetLength( ) const { return this->GetMagnitude(); } template inline ScalarType Vector3::GetMagnitude( ) const { return (ScalarType) ::sqrt( this->Dot(*this) ); } template inline ScalarType Vector3::Dot( const Vector3 &vector ) const { ScalarType value = 0; value += this->element[0] * vector.element[0]; value += this->element[1] * vector.element[1]; value += this->element[2] * vector.element[2]; return value; } template inline Vector3 Vector3::Cross( const Vector3 &vector ) const { return Vector3( (this->y*vector.z) - (this->z*vector.y), (this->z*vector.x) - (this->x*vector.z), (this->x*vector.y) - (this->y*vector.x) ); } template inline Vector3 Vector3::PiecewiseMultiplication( const Vector3 &vector ) const { return Vector3( this->x * vector.x, this->y * vector.y, this->z * vector.z ); } template inline Vector3 & Vector3::Normalize( ) { return (*this) /= this->GetLength(); } template inline Vector3 Vector3::GetNormalized( ) const { return Vector3(*this).Normalize(); } // Vector4 /////////////////////////////////////// template const Vector4 Vector4::null = Vector4( 0, 0, 0, 0 ); template const Vector4 Vector4::standard_unit_x = Vector4( 1, 0, 0, 0 ); template const Vector4 Vector4::standard_unit_y = Vector4( 0, 1, 0, 0 ); template const Vector4 Vector4::standard_unit_z = Vector4( 0, 0, 1, 0 ); template const Vector4 Vector4::standard_unit_w = Vector4( 0, 0, 0, 1 ); template inline Vector4::Vector4( ) : x(), y(), z(), w() {} template inline Vector4::Vector4( const Vector4 &vector ) { this->x = vector.x; this->y = vector.y; this->z = vector.z; this->w = vector.w; } template inline Vector4::Vector4( const Vector3 &vector, const ScalarType &_w ) { this->x = vector.x; this->y = vector.y; this->z = vector.z; this->w = _w; } template inline Vector4::Vector4( const Vector2 &vector, const ScalarType &_z, const ScalarType &_w ) { this->x = vector.x; this->y = vector.y; this->z = _z; this->w = _w; } template inline Vector4::Vector4( const ScalarType &_element ) { this->x = this->y = this->z = this->w = _element; } template inline Vector4::Vector4( const ScalarType _element[4] ) { this->x = _element[0]; this->y = _element[1]; this->z = _element[2]; this->w = _element[3]; } template inline Vector4::Vector4( const ScalarType &_x, const ScalarType &_y, const ScalarType &_z, const ScalarType &_w ) { this->x = _x; this->y = _y; this->z = _z; this->w = _w; } template inline Vector4::operator ScalarType* () { return this->element; } template inline Vector4::operator const ScalarType* () const { return this->element; } template inline ScalarType & Vector4::operator [] ( int i ) { return this->element[i]; } template inline const ScalarType & Vector4::operator [] ( int i ) const { return this->element[i]; } template inline Vector4 & Vector4::operator = ( const Vector4 &vector ) { this->element[0] = vector.element[0]; this->element[1] = vector.element[1]; this->element[2] = vector.element[2]; this->element[3] = vector.element[3]; return *this; } template inline Vector4 & Vector4::operator = ( const ScalarType element[4] ) { this->element[0] = element[0]; this->element[1] = element[1]; this->element[2] = element[2]; this->element[3] = element[3]; return *this; } template inline Vector4 & Vector4::operator *= ( const ScalarType &scalar ) { this->element[0] *= scalar; this->element[1] *= scalar; this->element[2] *= scalar; this->element[3] *= scalar; return *this; } template inline Vector4 & Vector4::operator /= ( const ScalarType &scalar ) { this->element[0] /= scalar; this->element[1] /= scalar; this->element[2] /= scalar; this->element[3] /= scalar; return *this; } template inline Vector4 & Vector4::operator += ( const Vector4 &vector ) { this->element[0] += vector.element[0]; this->element[1] += vector.element[1]; this->element[2] += vector.element[2]; this->element[3] += vector.element[3]; return *this; } template inline Vector4 & Vector4::operator -= ( const Vector4 &vector ) { this->element[0] -= vector.element[0]; this->element[1] -= vector.element[1]; this->element[2] -= vector.element[2]; this->element[3] -= vector.element[3]; return *this; } template inline Vector4 Vector4::operator * ( const ScalarType &scalar ) const { return Vector4(*this) *= scalar; } template inline Vector4 Vector4::operator / ( const ScalarType &scalar ) const { return Vector4(*this) /= scalar; } template inline Vector4 Vector4::operator + ( const Vector4 &vector ) const { return Vector4(*this) += vector; } template inline Vector4 Vector4::operator - ( const Vector4 &vector ) const { return Vector4(*this) -= vector; } template inline Vector4 Vector4::operator - ( ) const { return Vector4(-this->x, -this->y, -this->z, -this->w); } template inline bool Vector4::operator == ( const Vector4 &vector ) const { if( this->x != vector.x ) return false; if( this->y != vector.y ) return false; if( this->z != vector.z ) return false; if( this->w != vector.w ) return false; return true; } template inline bool Vector4::operator != ( const Vector4 &vector ) const { if( this->x != vector.x ) return true; if( this->y != vector.y ) return true; if( this->z != vector.z ) return true; if( this->w != vector.w ) return true; return false; } template inline ScalarType Vector4::GetLength( ) const { return this->GetMagnitude(); } template inline ScalarType Vector4::GetMagnitude( ) const { return (ScalarType) ::sqrt( this->Dot(*this) ); } template inline ScalarType Vector4::Dot( const Vector4 &vector ) const { ScalarType value = 0; value += this->element[0] * vector.element[0]; value += this->element[1] * vector.element[1]; value += this->element[2] * vector.element[2]; value += this->element[3] * vector.element[3]; return value; } template inline Vector4 Vector4::PiecewiseMultiplication( const Vector4 &vector ) const { return Vector4( this->x * vector.x, this->y * vector.y, this->z * vector.z, this->w * vector.w ); } template inline Vector4 & Vector4::Normalize( ) { return (*this) /= this->GetLength(); } template inline Vector4 Vector4::GetNormalized( ) const { return Vector4(*this).Normalize(); } } #endif