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.org/>
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 #include <iostream>
00027 
00028 #include <openbabel/rand.h>
00029 
00030 #ifndef RAD_TO_DEG
00031 #define RAD_TO_DEG (180.0/M_PI)
00032 #endif
00033 
00034 #ifndef DEG_TO_RAD
00035 #define DEG_TO_RAD (M_PI/180.0)
00036 #endif
00037 
00038 namespace OpenBabel
00039 {
00040 
00041   class matrix3x3; // declared in math/matrix3x3.h
00042   class OBRandom; // declared in rand.h
00043 
00044   // class introduction in vector3.cpp
00045   class OBAPI vector3
00046   {
00047   private :
00048     double _vx, _vy, _vz ;
00049 
00050   public :
00052     vector3 (const double inX=0.0, const double inY=0.0, const double inZ=0.0):
00053       _vx(inX), _vy(inY), _vz(inZ)
00054       {}
00055     vector3 (double inV[3]):
00056       _vx(inV[0]), _vy(inV[1]), _vz(inV[2])
00057       {}
00059     vector3 (const vector3& v):
00060       _vx(v._vx), _vy(v._vy), _vz(v._vz)
00061         { }
00062 
00064     ~vector3() { }
00065 
00067     void Set(const double inX, const double inY, const double inZ)
00068     {
00069       _vx = inX;
00070       _vy = inY;
00071       _vz = inZ;
00072     }
00074     void Set(const double *c)
00075     {
00076       _vx = c[0];
00077       _vy = c[1];
00078       _vz = c[2];
00079     }
00081     void SetX(const double inX)
00082     {
00083       _vx = inX;
00084     }
00086     void SetY(const double inY)
00087     {
00088       _vy = inY;
00089     }
00091     void SetZ(const double inZ)
00092     {
00093       _vz = inZ;
00094     }
00096     double GetX() const
00097     {
00098       return _vx;
00099     }
00101     double GetY() const
00102     {
00103       return _vy;
00104     }
00106     double GetZ() const
00107     {
00108       return _vz;
00109     }
00112     void Get(double *c)
00113     {
00114       c[0]=_vx;
00115       c[1]=_vy;
00116       c[2]=_vz;
00117     }
00119     double operator[] ( unsigned int i) const;
00120 
00122     vector3& operator= ( const vector3& v)
00123       {
00124         _vx = v._vx;
00125         _vy = v._vy;
00126         _vz = v._vz;
00127         return *this;
00128       }
00129 
00131     const double *AsArray() const
00132     {
00133       return &_vx;
00134     }
00135 
00138     vector3& operator+= ( const vector3& v)
00139       {
00140         _vx += v._vx;
00141         _vy += v._vy;
00142         _vz += v._vz;
00143         return *this;
00144       };
00147     vector3& operator-= ( const vector3& v)
00148       {
00149         _vx -= v._vx;
00150         _vy -= v._vy;
00151         _vz -= v._vz;
00152         return *this;
00153       };
00156     vector3& operator+= ( const double* f)
00157       {
00158         _vx += f[0];
00159         _vy += f[1];
00160         _vz += f[2];
00161         return *this;
00162       };
00165     vector3& operator-= ( const double* f)
00166       {
00167         _vx -= f[0];
00168         _vy -= f[1];
00169         _vz -= f[2];
00170         return *this;
00171       };
00174     vector3& operator*= ( const double& c)
00175       {
00176         _vx *= c;
00177         _vy *= c;
00178         _vz *= c;
00179         return *this;
00180       };
00181 
00184     vector3& operator/= ( const double& c)
00185       {
00186         double inv = 1.0 / c;
00187         return( (*this) *= inv );
00188       };
00192     vector3& operator*= ( const matrix3x3 &);
00193 
00195     void randomUnitVector(OBRandom *oeRand= NULL);
00196 
00197     //  Member Functions
00198 
00201     vector3& normalize () ;
00202 
00204     bool CanBeNormalized () const;
00205 
00207     inline double length_2 () const
00208     {
00209       return _vx*_vx + _vy*_vy + _vz*_vz;
00210     };
00212     double length () const
00213     {
00214       return sqrt( length_2() );
00215     };
00217     const double & x () const
00218     {
00219       return _vx ;
00220     } ;
00222     const double & y () const
00223     {
00224       return _vy ;
00225     } ;
00227     const double & z () const
00228     {
00229       return _vz ;
00230     } ;
00232     double & x ()
00233     {
00234       return _vx ;
00235     } ;
00237     double & y ()
00238     {
00239       return _vy ;
00240     } ;
00242     double & z ()
00243     {
00244       return _vz ;
00245     } ;
00246 
00248     // @{
00253     int operator== ( const vector3& ) const;
00257     int operator!= ( const vector3& other ) const
00258     {
00259       return ! ( (*this) == other );
00260     }
00270     bool IsApprox( const vector3 & other, const double & precision ) const;
00272 
00274 
00276     double distSq(const vector3 &vv) const
00277     {
00278       double dx = x() - vv.x();
00279       double dy = y() - vv.y();
00280       double dz = z() - vv.z();
00281       return( dx*dx + dy*dy + dz*dz );
00282     }
00283 
00286     bool createOrthoVector(vector3 &v) const;
00287 
00288   };
00289 
00291   OBAPI std::ostream& operator<< ( std::ostream&, const vector3& );
00292 
00293   //  Sum, Difference, Scalar Product
00295   inline OBAPI vector3 operator+ ( const vector3& v1, const vector3& v2)
00296   {
00297     return vector3(v1.x()+v2.x(), v1.y()+v2.y(), v1.z()+v2.z());
00298   }
00300   inline OBAPI vector3 operator- ( const vector3& v1, const vector3& v2)
00301   {
00302     return vector3(v1.x()-v2.x(), v1.y()-v2.y(), v1.z()-v2.z());
00303   }
00305   inline OBAPI vector3 operator- ( const vector3& v)
00306   {
00307     return vector3(-v.x(), -v.y(), -v.z());
00308   }
00310   inline OBAPI vector3 operator* ( const double& c, const vector3& v)
00311     {
00312       return vector3( c*v.x(), c*v.y(), c*v.z());
00313     }
00315   inline OBAPI vector3 operator* ( const vector3& v, const double& c)
00316     {
00317       return vector3( c*v.x(), c*v.y(), c*v.z());
00318     }
00320   inline OBAPI vector3 operator/ ( const vector3& v, const double& c)
00321   {
00322     return vector3( v.x()/c, v.y()/c, v.z()/c);
00323   }
00324   // @removed@ misleading operation
00325   // friend vector3 operator* ( const vector3 &,const vector3 &);
00326 
00327   //vector and matrix ops
00328   // @removed@ misleading operation; matrix multiplication is not commutitative
00329   //     friend vector3 operator *(const vector3 &v,const matrix3x3 &m);
00330 
00332   OBAPI vector3 operator *(const matrix3x3 &m, const vector3 &v);
00333 
00335   inline OBAPI double dot ( const vector3& v1, const vector3& v2 )
00336   {
00337     return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z() ;
00338   }
00340   OBAPI vector3 cross ( const vector3&, const vector3& );
00341 
00343   OBAPI double vectorAngle ( const vector3& v1, const vector3& v2 );
00344 
00346   OBAPI double CalcTorsionAngle(const vector3 &a, const vector3 &b,
00347                                         const vector3 &c, const vector3 &d);
00348 
00350   OBAPI double Point2PlaneSigned(vector3 a, vector3 b, vector3 c, vector3 d);
00352   OBAPI double Point2Plane(vector3 a, vector3 b, vector3 c, vector3 d);
00354   OBAPI double Point2PlaneAngle(const vector3 a, const vector3 b, const vector3 c, const vector3 d);
00355 
00357   OBAPI double Point2Line(const vector3& a, const vector3& b, const vector3& c);
00358 
00359   //  The global constant vector3 objects
00361   extern OBAPI const vector3 VZero;
00363   extern OBAPI const vector3 VX;
00365   extern OBAPI const vector3 VY;
00367   extern OBAPI const vector3 VZ;
00368 
00369 }
00370 
00371 #endif // OB_VECTOR_H
00372 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines