#ifndef VECTOR2D_HH #define VECTOR2D_HH ///////////////////////////////////////////////////////////////////////// //! file="amma/Geometry/AnGeo2/Vector2d.hh" //! lib=Mag2 //! userlevel=Normal //! author="Radek Marik" //! date="26.04.1994" //! docentry="Geometry.2-D;Basic Types.Numerical.Specialised" //! rcsid="$Id: Vector2d.hh,v 1.13 2000/10/04 12:48:52 ees1cg Exp $" #include "amma/Point2d.hh" #include class Vector2dTC; class VectorC; class Matrix2d2C; class ostream; class istream; // ------------------------------------------------------------------- // ******** Vector2dC ************************************************ // ------------------------------------------------------------------- //: Vector in 2d space // The class Vector2dC represents free column vector in 2D Euclidian space. class Vector2dC: public Point2dC { public: inline Vector2dC(); //: Creates the vector [0,0]. Vector2dC(SizeT dim); //: Creates the vector, [0,0] // This is for compatability with N-d code. // NB. dim MUST equal 2. Vector2dC(RealT xCoor, RealT yCoor); //: Creates the vector [xCoor, yCoor]. Vector2dC(const Point2dC & p); //: Creates the vector from the origin of coordinate system //: to the point 'p'. Vector2dC(const Vector2dC & vector); //: Copy constructor. Vector2dC(const Point2dC & start, const Point2dC & end); //: Creates the vector beginning in the point 'start' and ending //: in the point 'end'. Vector2dC(const VectorC & v); //: Creates the vector from the first two coordinates of //: the N-dimensional vector 'v'. inline RealT Dot(const Vector2dC & v) const; //: Returns the value of the scalar product of 'this' vector and //: the vector 'v'. inline RealT Cross(const Vector2dC & v) const; //: Returns the third coordinate of the cross product of this vector //: and the vector 'v'. inline Vector2dC operator+(const Vector2dC & v) const; //: Returns the sum of 2 vectors. inline Vector2dC operator-(const Vector2dC & v) const; //: Returns the difference of 2 vectors. inline Vector2dC operator*(RealT lambda) const; //: Multiplies both coordinates of the vector by the scale 'lambda' //: and returns the result. inline Vector2dC operator*(const Vector2dC & v) const; // Returns the vector which coordinates are the results of multiplication // item by item of this vector and the vector 'v'. inline Vector2dC operator/(RealT lambda) const; //: Divides both coordinates of the vector by the scale 'lambda'' //: and returns the result. inline Vector2dC operator/(const Vector2dC & v) const; // Returns the points which coordinates are results of division // of this vector and 'v' point item by item. inline Vector2dC & operator+=(const Vector2dC & v); //: Adds to this vector another vector 'v'. inline Vector2dC & operator-=(const Vector2dC & v); //: Subtracts from this vector the vector 'v'. inline Vector2dC & operator*=(RealT lambda); //: Multiplies both coordinates of the vector by the scale 'lambda'. inline Vector2dC & operator/=(RealT lambda); //: Divides both coordinates of the vector by the scale 'lambda'. inline RealT SqrNorm() const; //: Returns the square of the norm. inline RealT Norm() const; //: Returns the Euclidian size of the vector. inline RealT Modulus() const; //: Returns the Euclidian size of the vector. inline Vector2dC Unit() const; //: Returns the unit vector of this vector. inline Vector2dC Perpendicular() const; //: Return the perpendicular vector rotated to the left //: from the original one. inline RealT ProjectionSizeInto(const Vector2dC & v) const; //: Returns the size of the projection of this vector into the vector 'v'. inline Vector2dC ProjectionInto(const Vector2dC & v) const; //: Returns the projection of this vector into the vector 'v'. inline AngleT Angle() const; //: Returns the oriented angle (rad) from the axes x in //: the range -PI to PI. inline AngleT LineAngle() const; //: Returns the oriented angle (rad) from the axes x in the range //: -PI/2 to PI/2. inline Vector2dC Rotate(AngleT ang) const; //: Get a new vector rotated by ang radians. Vector2dTC T() const; //: Returns the row vector with the same coordinates as this vector has. Matrix2d2C ProductT() const; //: Returns the matrix which is the result of the multiplication x*x.T(). protected: Vector2dC(const Vector2dTC & vector); private: static const AngleT PIhalf; friend class Vector2dTC; friend istream & operator>>(istream & inS, Vector2dC & vector); friend ostream & operator<<(ostream & outS, const Vector2dC & vector); }; Vector2dC operator*(RealT alpha, const Vector2dC & v); ostream & operator<<(ostream & outS, const Vector2dC & vector); istream & operator>>(istream & inS, Vector2dC & vector); namespace StdMath { inline void SetToZero(Vector2dC &dat) { dat = Vector2dC(); } //: Set 'dat' to zero. } // ------------------------------------------------------------------- // ******** Vector2dTC *********************************************** // ------------------------------------------------------------------- class Vector2dTC : public Vector2dC { public: Vector2dC T() const; Vector2dTC operator*(const Matrix2d2C & matrix) const; RealT operator*(const Vector2dC & vector) const; protected: Vector2dTC(const Vector2dC & vector); friend class Vector2dC; }; #include "amma/StdMath.hh" /*-------------------------------------------------------------------*/ /********* Vector2dC *************************************************/ /*-------------------------------------------------------------------*/ inline Vector2dC::Vector2dC() //==================== : Point2dC(0,0) {} inline Vector2dC::Vector2dC(SizeT dim) //==================== : Point2dC(0,0) { assert(dim == 2); // See class def. } inline RealT Vector2dC::Dot(const Vector2dC & vector) const //============================================ { return X() * vector.X() + Y() * vector.Y(); } inline RealT Vector2dC::Cross(const Vector2dC & vector) const //============================================== { return X() * vector.Y() - Y() * vector.X(); } inline Vector2dC Vector2dC::operator+(const Vector2dC & v) const //============================================= { return Vector2dC(X()+v.X(), Y()+v.Y()); } inline Vector2dC Vector2dC::operator-(const Vector2dC & v) const //============================================= { return Vector2dC(X()-v.X(), Y()-v.Y()); } inline Vector2dC Vector2dC::operator*(RealT lambda) const //====================================================== { return Vector2dC(X()*lambda, Y()*lambda); } inline Vector2dC Vector2dC::operator*(const Vector2dC & v) const { return Vector2dC(X()*v.X(),Y()*v.Y()); } inline Vector2dC Vector2dC::operator/(const Vector2dC & v) const { return Vector2dC(X()/v.X(),Y()/v.Y()); } inline Vector2dC Vector2dC::operator/(RealT lambda) const //====================================================== { return Vector2dC(X()/lambda, Y()/lambda); } inline Vector2dC & Vector2dC::operator+=(const Vector2dC & v) //======================================== { X()+=v.X(); Y()+=v.Y(); return *this; } inline Vector2dC & Vector2dC::operator-=(const Vector2dC & v) //======================================== { X()-=v.X(); Y()-=v.Y(); return *this; } inline Vector2dC & Vector2dC::operator*=(RealT lambda) //================================================= { X()*=lambda; Y()*=lambda; return *this; } inline Vector2dC & Vector2dC::operator/=(RealT lambda) //================================================= { X()/=lambda; Y()/=lambda; return *this; } inline RealT Vector2dC::SqrNorm() const //======================== { return Dot(*this); } inline RealT Vector2dC::Norm() const //===================== { return ::Sqrt(SqrNorm()); } inline RealT Vector2dC::Modulus() const //======================== { return Norm(); } inline Vector2dC Vector2dC::Unit() const //===================== { RealT modulus = Modulus(); return Vector2dC(X()/modulus, Y()/modulus); } inline Vector2dC Vector2dC::Perpendicular() const //============================== { return Vector2dC(-Y(),X()); } inline RealT Vector2dC::ProjectionSizeInto(const Vector2dC & v) const //====================================================== { return Dot(v.Unit()); } inline Vector2dC Vector2dC::ProjectionInto(const Vector2dC & v) const //================================================== { return v * (Dot(v)/v.SqrNorm()); } inline Vector2dC Vector2dC::Rotate(AngleT ang) const //================================= { const RealT ACos = Cos(ang); const RealT ASin = Sin(ang); return Vector2dC(ACos * X() + ASin * Y(),ASin * X() + ACos * Y()); } inline Vector2dC::AngleT Vector2dC::Angle() const //====================== { return ATan2(Y(), X()); } inline Vector2dC::AngleT Vector2dC::LineAngle() const //========================== { return X() == 0 ? PIhalf : ATan(Y()/X()); } #endif // IAPS - Image analysis program system. // End of include file Vector2d.hh