vector3.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 vector3.h - Handle 3D coordinates.
00003  
00004 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
00005 Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
00006 Some portions Copyright (C) 2006 by Benoit Jacob
00007  
00008 This file is part of the Open Babel project.
00009 For more information, see <http://openbabel.sourceforge.net/>
00010  
00011 This program is free software; you can redistribute it and/or modify
00012 it under the terms of the GNU General Public License as published by
00013 the Free Software Foundation version 2 of the License.
00014  
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 ***********************************************************************/
00020 
00021 #ifndef OB_VECTOR_H
00022 #define OB_VECTOR_H
00023 
00024 #include <ostream>
00025 #include <math.h>
00026 
00027 #include <openbabel/rand.h>
00028 
00029 #ifndef RAD_TO_DEG
00030 #define RAD_TO_DEG (180.0/M_PI)
00031 #endif
00032 
00033 #ifndef DEG_TO_RAD
00034 #define DEG_TO_RAD (M_PI/180.0)
00035 #endif
00036 
00037 namespace OpenBabel
00038 {
00039 
00040   class matrix3x3; // declared in math/matrix3x3.h
00041   class OBRandom; // declared in rand.h
00042 
00043   // class introduction in vector3.cpp
00044   class OBAPI vector3
00045   {
00046   private :
00047     double _vx, _vy, _vz ;
00048 
00049   public :
00051     vector3 (const double inX=0.0, const double inY=0.0, const double inZ=0.0):
00052       _vx(inX), _vy(inY), _vz(inZ)
00053       {}
00055     vector3 (const vector3& v):
00056       _vx(v._vx), _vy(v._vy), _vz(v._vz)
00057         { }
00058 
00060     void Set(const double inX, const double inY, const double inZ)
00061     {
00062       _vx = inX;
00063       _vy = inY;
00064       _vz = inZ;
00065     }
00067     void Set(const double *c)
00068     {
00069       _vx = c[0];
00070       _vy = c[1];
00071       _vz = c[2];
00072     }
00074     void SetX(const double inX)
00075     {
00076       _vx = inX;
00077     }
00079     void SetY(const double inY)
00080     {
00081       _vy = inY;
00082     }
00084     void SetZ(const double inZ)
00085     {
00086       _vz = inZ;
00087     }
00090     void Get(double *c)
00091     {
00092       c[0]=_vx;
00093       c[1]=_vy;
00094       c[2]=_vz;
00095     }
00097     double operator[] ( unsigned int i);
00098 
00100     vector3& operator= ( const vector3& v)
00101       {
00102         _vx = v._vx;
00103         _vy = v._vy;
00104         _vz = v._vz;
00105         return *this;
00106       }
00107 
00109     const double *AsArray() const
00110     {
00111       return &_vx;
00112     }
00113 
00116     vector3& operator+= ( const vector3& v)
00117       {
00118         _vx += v._vx;
00119         _vy += v._vy;
00120         _vz += v._vz;
00121         return *this;
00122       };
00125     vector3& operator-= ( const vector3& v)
00126       {
00127         _vx -= v._vx;
00128         _vy -= v._vy;
00129         _vz -= v._vz;
00130         return *this;
00131       };
00134     vector3& operator+= ( const double* f)
00135       {
00136         _vx += f[0];
00137         _vy += f[1];
00138         _vz += f[2];
00139         return *this;
00140       };
00143     vector3& operator-= ( const double* f)
00144       {
00145         _vx -= f[0];
00146         _vy -= f[1];
00147         _vz -= f[2];
00148         return *this;
00149       };
00152     vector3& operator*= ( const double& c)
00153       {
00154         _vx *= c;
00155         _vy *= c;
00156         _vz *= c;
00157         return *this;
00158       };
00159 
00162     vector3& operator/= ( const double& c)
00163       {
00164         double inv = 1.0 / c;
00165         return( (*this) *= inv );
00166       };
00170     vector3& operator*= ( const matrix3x3 &);
00171 
00173     void randomUnitVector(OBRandom *oeRand= NULL);
00174 
00175     //  Member Functions
00176 
00179     vector3& normalize () ;
00180 
00182     bool CanBeNormalized () const;
00183 
00185     inline double length_2 () const
00186     {
00187       return _vx*_vx + _vy*_vy + _vz*_vz;
00188     };
00190     double length () const
00191     {
00192       return sqrt( length_2() );
00193     };
00195     const double & x () const
00196     {
00197       return _vx ;
00198     } ;
00200     const double & y () const
00201     {
00202       return _vy ;
00203     } ;
00205     const double & z () const
00206     {
00207       return _vz ;
00208     } ;
00210     double & x ()
00211     {
00212       return _vx ;
00213     } ;
00215     double & y ()
00216     {
00217       return _vy ;
00218     } ;
00220     double & z ()
00221     {
00222       return _vz ;
00223     } ;
00224 
00226     // @{
00231     int operator== ( const vector3& ) const;
00235     int operator!= ( const vector3& other ) const
00236     {
00237       return ! ( (*this) == other );
00238     }
00248     bool IsApprox( const vector3 & other, const double & precision ) const;
00250 
00252 
00254     double distSq(const vector3 &vv) const
00255     {
00256       double dx = x() - vv.x();
00257       double dy = y() - vv.y();
00258       double dz = z() - vv.z();
00259       return( dx*dx + dy*dy + dz*dz );
00260     }
00261 
00264     bool createOrthoVector(vector3 &v) const;
00265 
00266   };
00267 
00269   OBAPI std::ostream& ".">operator<< ( std::ostream&, const vector3& );
00270 
00271   //  Sum, Difference, Scalar Product
00273   inline OBAPI vector3 operator+ ( const vector3& v1, const vector3& v2)
00274   {
00275     return vector3(v1.x()+v2.x(), v1.y()+v2.y(), v1.z()+v2.z());
00276   };
00278   inline OBAPI vector3 operator- ( const vector3& v1, const vector3& v2)
00279   {
00280     return vector3(v1.x()-v2.x(), v1.y()-v2.y(), v1.z()-v2.z());
00281   };
00283   inline OBAPI vector3 operator- ( const vector3& v)
00284   {
00285     return vector3(-v.x(), -v.y(), -v.z());
00286   };
00288   inline OBAPI vector3 operator* ( const double& c, const vector3& v)
00289     {
00290       return vector3( c*v.x(), c*v.y(), c*v.z());
00291     };
00293   inline OBAPI vector3 operator* ( const vector3& v, const double& c)
00294     {
00295       return vector3( c*v.x(), c*v.y(), c*v.z());
00296     };
00298   inline OBAPI vector3 operator/ ( const vector3& v, const double& c)
00299   {
00300     return vector3( v.x()/c, v.y()/c, v.z()/c);
00301   };
00302   // @removed@ misleading operation
00303   // friend vector3 operator* ( const vector3 &,const vector3 &);
00304 
00305   //vector and matrix ops
00306   // @removed@ misleading operation; matrix multiplication is not commutitative
00307   //     friend vector3 operator *(const vector3 &v,const matrix3x3 &m);
00308 
00310   OBAPI vector3 operator *(const matrix3x3 &m, const vector3 &v);
00311 
00313   inline OBAPI double dot ( const vector3& v1, const vector3& v2 )
00314   {
00315     return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z() ;
00316   }
00318   OBAPI vector3 cross ( const vector3&, const vector3& );
00319 
00321   OBAPI double vectorAngle ( const vector3& v1, const vector3& v2 );
00322 
00324   OBAPI double CalcTorsionAngle(const vector3 &a, const vector3 &b,
00325                                         const vector3 &c, const vector3 &d);
00326 
00328   OBAPI double Point2Plane(vector3 a, vector3 b, vector3 c, vector3 d);
00330   OBAPI double Point2PlaneAngle(const vector3 a, const vector3 b, const vector3 c, const vector3 d);
00331 
00332   //  The global constant vector3 objects
00334   extern OBAPI const vector3 VZero;
00336   extern OBAPI const vector3 VX;
00338   extern OBAPI const vector3 VY;
00340   extern OBAPI const vector3 VZ;
00341 
00342 }
00343 
00344 #endif // OB_VECTOR_H
00345