/* $Id: vector.hpp,v 1.19 2005/11/20 16:15:28 pohlt Exp $ */ #ifndef _VECTOR_HPP_ #define _VECTOR_HPP_ /**********************************************************/ #include "global.hpp" #include "serialize.hpp" #include /**********************************************************/ template class VectorTemplate { public: T x, y; VectorTemplate ( const T initX = 0, const T initY = 0 ): x (initX), y (initY) {} VectorTemplate& set( const T initX, const T initY ) {x = initX; y = initY; return *this;} VectorTemplate& setAngle( const T angle, const T length ) {x = length * COS_REAL( angle ); y = length * SIN_REAL( angle ); return *this;} bool operator==( const VectorTemplate& a ) { return x == a.x && y == a.y; } VectorTemplate& operator+=( const VectorTemplate& a ) {x += a.x; y += a.y; return *this;} VectorTemplate& operator-=( const VectorTemplate& a ) {x -= a.x; y -= a.y; return *this;} VectorTemplate& operator*=( const T a ) {x *= a; y *= a; return *this;} VectorTemplate& operator/=( const T a ) {x /= a; y /= a; return *this;} VectorTemplate operator+( const VectorTemplate& b ) const {return VectorTemplate( x+b.x, y+b.y );} VectorTemplate operator-( const VectorTemplate& b ) const {return VectorTemplate( x-b.x, y-b.y );} VectorTemplate operator-( void ) const {return VectorTemplate( -x, -y );} VectorTemplate operator*( const T a ) const {return VectorTemplate( x*a, y*a );} VectorTemplate operator/( const T a ) const {return VectorTemplate( x/a, y/a );} T operator*( const VectorTemplate& b ) const {return x*b.x + y*b.y;} T abs2() const {return (x*x + y*y);} T abs() const {return SQRT_REAL( this->abs2() );} VectorTemplate getNorm() const {VectorTemplate aux( *this ); aux /= this->abs(); return aux;} VectorTemplate& normalize() {(*this) /= this->abs(); return *this;} VectorTemplate getIntVector() const; }; template VectorTemplate elementProd( const VectorTemplate& a, const VectorTemplate& b ) { return VectorTemplate( a.x*b.x, a.y*b.y ); } // A specialization of the Serialize class for Vectors template class Serialize > { public: static Uint32 sizeOf( VectorTemplate item ) { return 2 * Serialize::sizeOf(); } static Uint32 sizeOf() { return 2 * Serialize::sizeOf(); } /* This method serializes one data value stored in item into the * memory starting at buffer */ static void serialize( VectorTemplate item, Uint8*& buffer ) { Serialize::serialize( item.x, buffer ); Serialize::serialize( item.y, buffer ); } /* This method serializes numberItems data values stored consecutively * in the array item into the memory starting at buffer */ static void serialize( int numberItems, VectorTemplate* item, Uint8*& buffer ) { for (int i = 0; i < numberItems; i++) { serialize( item[i], buffer ); } } /* This method deserializes one data value to be stored in item from the * memory starting at buffer */ static void deserialize( Uint8*& buffer, VectorTemplate& item ) { Serialize::deserialize( buffer, item.x ); Serialize::deserialize( buffer, item.y ); } /* This method deserializes numberItems data values to be stored consecutively * in the array item from the memory starting at buffer */ static void deserialize( int numberItems, Uint8*& buffer, VectorTemplate* item ) { for ( int i = 0; i < numberItems; i++) { deserialize( buffer, item[i] ); } } }; /**********************************************************/ typedef VectorTemplate Vector; typedef VectorTemplate IntVector; /**********************************************************/ template<> inline IntVector Vector::getIntVector() const { return IntVector( ROUND( x ), ROUND( y ) ); } /**********************************************************/ template<> inline IntVector IntVector::getIntVector() const { return *this; } /**********************************************************/ inline Vector operator+( const Vector& a, const IntVector& b ) { return Vector( a.x + b.x, a.y + b.y ); } /**********************************************************/ inline Vector operator-( const Vector& a, const IntVector& b ) { return Vector( a.x + b.x, a.y + b.y ); } /**********************************************************/ inline Vector operator+( const IntVector& a, const Vector& b ) { return Vector( a.x + b.x, a.y + b.y ); } /**********************************************************/ inline Vector operator-( const IntVector& a, const Vector& b ) { return Vector( a.x + b.x, a.y + b.y ); } /**********************************************************/ #endif // _VECTOR_HPP_