#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Core/Base/TFVector.hh" // This file is part of RAVL, Recognition And Vision Library // Copyright (C) 2001, University of Surrey // This code may be redistributed under the terms of the GNU Lesser // General Public License (LGPL). See the lgpl.licence file for details or // see http://www.gnu.org/copyleft/lesser.html // file-header-ends-here #ifndef RAVL_TFVECTOR_HEADER #define RAVL_TFVECTOR_HEADER 1 ///////////////////////////////////////////////////// //! rcsid="$Id: TFVector.hh,v 1.24 2002/07/29 14:26:37 craftit Exp $" //! docentry="Ravl.Core.Tuples" //! file="Ravl/Core/Base/TFVector.hh" //! lib=RavlCore //! userlevel=Advanced //! author="Charles Galambos" //! date="24/01/2001" #include "Ravl/Assert.hh" #include "Ravl/Math.hh" #include "Ravl/Types.hh" namespace RavlN { class BinIStreamC; class BinOStreamC; template class TFVectorC; template class TFMatrixC; template istream &operator>>(istream &in,TFVectorC &dat); template ostream &operator<<(ostream &in,const TFVectorC &dat); template inline BinIStreamC &operator>>(BinIStreamC &in,TFVectorC &dat); template inline BinOStreamC &operator<<(BinOStreamC &in,const TFVectorC &dat); //! userlevel=Advanced //: Fixed size vector. template class TFVectorC { public: TFVectorC() {} //: Default constructor. // Note: The default constructor of 'DataT' is used to contruct // the elements of the vector, for builtin types this means // their values will be undefined. TFVectorC(const DataT *init); //: Value constructor. // Copy values from 'init'. It is the user's responsibility // to ensure that data has at least 'N' elements. TFVectorC(UIntT size) { RavlAssert(size == N); } //: This constructor is for compatibility with arbitrarily sized vectors. // Used by some templates. UIntT Size() const { return N; } //: Get size of array // Used by some templates. bool Contains(UIntT i) const { return i < N; } //: Test if array contains index i· DataT &operator[](UIntT ind) { #if RAVL_CHECK if(!Contains(ind)) IssueError(__FILE__,__LINE__,"Index %u out of range, 0 - %u",ind,N); #endif return data[ind]; } //: Access item. const DataT &operator[](UIntT ind) const { #if RAVL_CHECK if(!Contains(ind)) IssueError(__FILE__,__LINE__,"Index %u out of range, 0 - %u",ind,N); #endif return data[ind]; } //: Access item. inline bool operator==(const TFVectorC & ind) const; //: is equal inline bool operator!=(const TFVectorC & ind) const; //: is not equal inline void Fill(const DataT &dat); //: Fill array with value 'dat'. inline TFVectorC Abs() const; //: Get an vector with Abs(x) run on all it values. inline const TFVectorC & operator+=(const TFVectorC & ind); //: Add an array to this one, inplace. inline const TFVectorC & operator-=(const TFVectorC & ind); //: Returns this index multiplied by index 'ind' item by item. inline const TFVectorC & operator*=(const TFVectorC & ind); //: Multiplies this index by index 'ind' item by item. inline const TFVectorC & operator/=(const TFVectorC & ind); //: Divides this index by index 'ind' item by item. inline TFVectorC operator+(const TFVectorC & ind) const; //: Adds this index and 'ind'. inline TFVectorC operator-(const TFVectorC & ind) const; //: Subtracts index 'ind' from this index. inline TFVectorC operator*(const TFVectorC & ind) const; //: Returns this object multiplied by index 'ind' item by item. inline TFVectorC operator/(const TFVectorC & ind) const; //: Returns this object devided by index 'ind' item by item. inline TFVectorC operator-() const; //: Returns opposite elements (unary minus operator). inline const TFVectorC & operator*=(const DataT &alpha); //: Multiplies this index by number 'alpha'. inline const TFVectorC & operator/=(const DataT &alpha); //: Divides this index by number 'alpha'. inline TFVectorC operator*(const DataT &alpha) const; //: Returns this object mutliplied by alpha. inline TFVectorC operator/(const DataT &alpha) const; //: Returns this object devided by alpha. inline DataT Dot(const TFVectorC &oth) const { DataT ret = data[0] * oth.data[0]; for(UIntT i = 1;i &OuterProduct(const TFVectorC &av, TFMatrixC &result) const; //: Calculate the outer product of this vector with av. // The output is assigned to 'result'. A reference to 'result' is // returned.

// The implementation is in "Ravl/FMatrix.hh", it must be // included to use this function. TFMatrixC &OuterProduct(TFMatrixC &result) const; //: Calculate the outer product of this vector with itsself. // The output is assigned to 'result'. A reference to 'result' is // returned.

// The implementation is in "Ravl/FMatrix.hh", it must be // included to use this function. inline bool Limit(const DataT &min,const DataT &max); //: Limit all values in this vector to between min and max. // Returns true if all values in the vector are between the limits. // Distance calculations // --------------------- inline DataT MaxValueDistance(const TFVectorC & i) const; //: Returns the distance of two indexes in maximum value metric. inline DataT CityBlockDistance(const TFVectorC & i) const; //: Returns the distance of two indexes in absolute value metric. inline DataT SqrEuclidDistance(const TFVectorC & i) const; //: Returns the distance of two indexes in square Euclid metric. DataT Sum() const; //: Calculate the sum of all the vector elements. DataT SumSqr() const; //: Calculate the sum of the squares of all the vector elements. inline const TFMatrixC &T() const; //: Transpose vector. // The implementation for this can be found in "Ravl/TFMatrix.hh" #if RAVL_NEW_ANSI_CXX_DRAFT friend istream &operator>> <>(istream &in,TFVectorC &dat); friend ostream &operator<< <>(ostream &in,const TFVectorC &dat); friend BinIStreamC &operator>> <>(BinIStreamC &in,TFVectorC &dat); friend BinOStreamC &operator<< <>(BinOStreamC &in,const TFVectorC &dat); #else friend istream &operator>> (istream &in,TFVectorC &dat); friend ostream &operator<< (ostream &in,const TFVectorC &dat); friend BinIStreamC &operator>> (BinIStreamC &in,TFVectorC &dat); friend BinOStreamC &operator<< (BinOStreamC &in,const TFVectorC &dat); #endif protected: DataT data[N]; }; template inline TFVectorC TFVector2(const DataT &v1,const DataT &v2) { TFVectorC ret; ret[0] = v1; ret[1] = v2; return ret; } //: Create a 2d vector. // Helper function to make creation of fixed size vectors easier. template inline TFVectorC TFVector3(const DataT &v1,const DataT &v2,const DataT &v3) { TFVectorC ret; ret[0] = v1; ret[1] = v2; ret[2] = v3; return ret; } //: Create a 3d vector. // Helper function to make creation of fixed size vectors easier. template inline TFVectorC TFVector4(const DataT &v1,const DataT &v2,const DataT &v3,const DataT &v4) { TFVectorC ret; ret[0] = v1; ret[1] = v2; ret[2] = v3; ret[3] = v4; return ret; } //: Create a 3d vector. // Helper function to make creation of fixed size vectors easier. template inline void SetZero(TFVectorC &x) { DataT xv; SetZero(xv); x.Fill(xv); } //: Set vector to zero. template TFVectorC::TFVectorC(const DataT *init) { for(UIntT i = 0;i < N;i++) data[i] = init[i]; } template inline bool TFVectorC::operator==(const TFVectorC & ind) const { for(UIntT i = 0;i < N;i++) if(data[i] != ind[i]) return false; return true; } template inline bool TFVectorC::operator!=(const TFVectorC & ind) const { for(UIntT i = 0;i < N;i++) if(data[i] != ind[i]) return true; return false; } template inline void TFVectorC::Fill(const DataT &dat) { for(UIntT i = 0;i < N;i++) data[i] = dat; } template inline const TFVectorC & TFVectorC::operator+=(const TFVectorC & o) { for(UIntT i = 0;i < N;i++) data[i] += o[i]; return *this; } template inline const TFVectorC & TFVectorC::operator-=(const TFVectorC & o) { for(UIntT i = 0;i < N;i++) data[i] -= o[i]; return *this; } template inline const TFVectorC & TFVectorC::operator*=(const TFVectorC & o) { for(UIntT i = 0;i < N;i++) data[i] *= o[i]; return *this; } template inline const TFVectorC & TFVectorC::operator/=(const TFVectorC & o) { for(UIntT i = 0;i < N;i++) data[i] /= o[i]; return *this; } template inline const TFVectorC & TFVectorC::operator*=(const DataT &alpha) { for(UIntT i = 0;i < N;i++) data[i] *= alpha; return *this; } template inline const TFVectorC & TFVectorC::operator/=(const DataT &alpha) { for(UIntT i = 0;i < N;i++) data[i] /= alpha; return *this; } template inline TFVectorC TFVectorC::operator-() const { TFVectorC ret; for(UIntT i = 0;i < N;i++) ret[i] = -data[i]; return ret; } template inline TFVectorC TFVectorC::operator+(const TFVectorC & o) const { TFVectorC ret; for(UIntT i = 0;i < N;i++) ret[i] = data[i] + o[i]; return ret; } template inline TFVectorC TFVectorC::operator-(const TFVectorC & o) const { TFVectorC ret; for(UIntT i = 0;i < N;i++) ret[i] = data[i] - o[i]; return ret; } template inline TFVectorC TFVectorC::operator*(const TFVectorC & o) const { TFVectorC ret; for(UIntT i = 0;i < N;i++) ret[i] = data[i] * o[i]; return ret; } template inline TFVectorC TFVectorC::operator/(const TFVectorC & o) const { TFVectorC ret; for(UIntT i = 0;i < N;i++) ret[i] = data[i] / o[i]; return ret; } template inline TFVectorC TFVectorC::operator*(const DataT &alpha) const { TFVectorC ret; for(UIntT i = 0;i < N;i++) ret[i] = data[i] * alpha; return ret; } template inline TFVectorC operator*(const DataT &alpha,const TFVectorC & data) { TFVectorC ret; for(UIntT i = 0;i < N;i++) ret[i] = alpha * data[i]; return ret; } template inline TFVectorC TFVectorC::operator/(const DataT &alpha) const { TFVectorC ret; for(UIntT i = 0;i < N;i++) ret[i] = data[i] / alpha; return ret; } template inline bool TFVectorC::Limit(const DataT &min,const DataT &max) { bool ret = true; for(UIntT i =0;i max) { data[i] = max; ret = false; continue; } if(data[i] < min) { data[i] = min; ret = false; } } return ret; } template inline TFVectorC TFVectorC::Abs() const { TFVectorC ret; for(UIntT i =0;i inline DataT TFVectorC::MaxValueDistance(const TFVectorC & o) const { DataT ret = RavlN::Abs(data[0] - o[0]); for(UIntT i =1;i d) ret = d; } return ret; } template inline DataT TFVectorC::CityBlockDistance(const TFVectorC & o) const{ DataT ret = RavlN::Abs(data[0] - o[0]); for(UIntT i =1;i inline DataT TFVectorC::SqrEuclidDistance(const TFVectorC & o) const { DataT ret = RavlN::Sqr(data[0] - o[0]); for(UIntT i =1;i inline DataT TFVectorC::Sum() const { DataT ret = StdCopy(data[0]); for(UIntT i = 1;i inline DataT TFVectorC::SumSqr() const { DataT ret = RavlN::Sqr(data[0]); for(UIntT i = 1;i inline ostream &operator<<(ostream &out,const TFVectorC &dat) { for(UIntT i = 0;i < N;i++) out << dat.data[i] << ' '; return out; } template inline istream &operator>>(istream &in,TFVectorC &dat) { for(UIntT i = 0;i < N;i++) in >> dat.data[i]; return in; } #ifndef VISUAL_CPP template inline ostream &operator<<(ostream &out,const TFVectorC &dat) { for(UIntT i = 0;i < N;i++) out << ((int) dat.data[i]) << ' '; return out; } //: Specialise byte vectors so they're treated as numerical values. template inline istream &operator>>(istream &in,TFVectorC &dat) { int x; for(UIntT i = 0;i < N;i++) { in >> x; dat.data[i] = (ByteT) x; } return in; } //: Specialise byte vectors so they're treated as numerical values. template inline ostream &operator<<(ostream &out,const TFVectorC &dat) { for(UIntT i = 0;i < N;i++) out << ((int) dat.data[i]) << ' '; return out; } //: Specialise byte vectors so they're treated as numerical values. template inline istream &operator>>(istream &in,TFVectorC &dat) { int x; for(UIntT i = 0;i < N;i++) { in >> x; dat.data[i] = (ByteT) x; } return in; } //: Specialise byte vectors so they're treated as numerical values. #endif } #endif