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-2005 by Geoffrey R. Hutchison
00006  
00007 This file is part of the Open Babel project.
00008 For more information, see <http://openbabel.sourceforge.net/>
00009  
00010 This program is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU General Public License as published by
00012 the Free Software Foundation version 2 of the License.
00013  
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 ***********************************************************************/
00019 
00020 #ifndef OB_VECTOR_H
00021 #define OB_VECTOR_H
00022 
00023 #if HAVE_IOSTREAM
00024 #include <iostream>
00025 #elif HAVE_IOSTREAM_H
00026 #include <iostream.h>
00027 #endif
00028 
00029 #if HAVE_FSTREAM
00030 #include <fstream>
00031 #elif HAVE_FSTREAM_H
00032 #include <fstream.h>
00033 #endif
00034 
00035 #include <math.h>
00036 #include "obutil.h"
00037 
00038 #ifndef PI
00039 #define PI 3.1415926535897932384626433
00040 #endif
00041 
00042 #ifndef RAD_TO_DEG
00043 #define RAD_TO_DEG 180.0/PI
00044 #endif
00045 
00046 #ifndef DEG_TO_RAD
00047 #define DEG_TO_RAD PI/180.0
00048 #endif
00049 
00050 namespace OpenBabel
00051 {
00052 
00053 class matrix3x3;
00054 
00055 // class introduction in vector3.cpp
00056 class   OBAPI vector3
00057 {
00058 private :
00059     double              _vx, _vy, _vz ;
00060 
00061 public :
00063     vector3 (const double x=0.0, const double y=0.0, const double z=0.0)
00064     {
00065         _vx = x;
00066         _vy = y;
00067         _vz = z;
00068     };
00070     vector3 (const vector3& v)
00071     {
00072         _vx = v._vx;
00073         _vy = v._vy;
00074         _vz = v._vz;
00075     };
00076 
00078     void Set(const double x, const double y, const double z)
00079     {
00080         _vx = x ;
00081         _vy = y ;
00082         _vz = z ;
00083     };
00085     void Set(const double *c)
00086     {
00087         _vx = c[0];
00088         _vy = c[1];
00089         _vz = c[2];
00090     }
00092     void SetX(const double x)
00093     {
00094         _vx = x;
00095     };
00097     void SetY(const double y)
00098     {
00099         _vy = y;
00100     };
00102     void SetZ(const double z)
00103     {
00104         _vz = z;
00105     };
00107     void Get(double *c)
00108     {
00109         c[0]=_vx;
00110         c[1]=_vy;
00111         c[2]=_vz;
00112     };
00114     double& operator[] ( unsigned int i);
00115 
00117     vector3& operator= ( const vector3& v)
00118     {
00119         _vx = v._vx;
00120         _vy = v._vy;
00121         _vz = v._vz;
00122         return *this;
00123     };
00124 
00126     friend OBAPI std::ostream& operator<< ( std::ostream&, const vector3& ) ;
00127 
00128     //  Comparison
00129     friend OBAPI int operator== ( const vector3&, const vector3& ) ;
00130     friend OBAPI int operator!= ( const vector3&, const vector3& ) ;
00131 
00132     //  Sum, Difference, Scalar Product
00134     friend OBAPI vector3 operator+ ( const vector3& v1, const vector3& v2)
00135     {
00136         return vector3(v1._vx+v2._vx, v1._vy+v2._vy, v1._vz+v2._vz);
00137     };
00139     friend OBAPI vector3 operator- ( const vector3& v1, const vector3& v2)
00140     {
00141         return vector3(v1._vx-v2._vx, v1._vy-v2._vy, v1._vz-v2._vz);
00142     };
00144     friend OBAPI vector3 operator- ( const vector3& v)
00145     {
00146         return vector3(-v._vx, -v._vy, -v._vz);
00147     };
00149     friend OBAPI vector3 operator* ( const double& c, const vector3& v)
00150     {
00151         return vector3( c*v._vx, c*v._vy, c*v._vz);
00152     };
00154     friend OBAPI vector3 operator* ( const vector3& v, const double& c)
00155     {
00156         return vector3( c*v._vx, c*v._vy, c*v._vz);
00157     };
00159     friend OBAPI vector3 operator/ ( const vector3& v, const double& c)
00160     {
00161         return vector3( v._vx/c, v._vy/c, v._vz/c);
00162     };
00163     // @removed@ misleading operation
00164     // friend vector3 operator* ( const vector3 &,const vector3 &);
00165 
00166     //vector and matrix ops
00167     // @removed@ misleading operation; matrix multiplication is not commutitative
00168     //     friend vector3 operator *(const vector3 &v,const matrix3x3 &m);
00169 
00171     friend OBAPI vector3 operator *(const matrix3x3 &m,const vector3 &v);
00172 
00173     //  Immediate Sum, Difference, Scalar Product
00174     vector3& operator+= ( const vector3& v)
00175     {
00176         _vx += v._vx;
00177         _vy += v._vy;
00178         _vz += v._vz;
00179         return *this;
00180     };
00181     vector3& operator-= ( const vector3& v)
00182     {
00183         _vx -= v._vx;
00184         _vy -= v._vy;
00185         _vz -= v._vz;
00186         return *this;
00187     };
00188     vector3& operator+= ( const double* f)
00189     {
00190         _vx += f[0];
00191         _vy += f[1];
00192         _vz += f[2];
00193         return *this;
00194     };
00195     vector3& operator-= ( const double* f)
00196     {
00197         _vx -= f[0];
00198         _vy -= f[1];
00199         _vz -= f[2];
00200         return *this;
00201     };
00202     vector3& operator*= ( const double& c)
00203     {
00204         _vx *= c;
00205         _vy *= c;
00206         _vz *= c;
00207         return *this;
00208     };
00209     vector3& operator/= ( const double& c)
00210     {
00211         _vx /= c;
00212         _vy /= c;
00213         _vz /= c;
00214         return *this;
00215     };
00217     vector3& operator*= ( const matrix3x3 &);
00218 
00220     void randomUnitVector(OBRandom *oeRand= 0L);
00221 
00222     //  Member Functions
00223 
00225     friend OBAPI double dot ( const vector3&, const vector3& ) ;
00226 
00228     friend OBAPI vector3 cross ( const vector3&, const vector3& ) ;
00229 
00231     friend OBAPI double vectorAngle ( const vector3& v1, const vector3& v2 );
00232 
00234     friend OBAPI double CalcTorsionAngle(const vector3 &a, const vector3 &b,
00235                                    const vector3 &c, const vector3 &d);
00236 
00238     vector3& normalize () ;
00239 
00241     double length () const
00242     {
00243         return sqrt(_vx*_vx + _vy*_vy + _vz*_vz);
00244     };
00246     double length_2 () const
00247     {
00248         return _vx*_vx + _vy*_vy + _vz*_vz;
00249     };
00251     double x () const
00252     {
00253         return _vx ;
00254     } ;
00256     double y () const
00257     {
00258         return _vy ;
00259     } ;
00261     double z () const
00262     {
00263         return _vz ;
00264     } ;
00265 
00267 
00269     inline double distSq(const vector3 &vv) const
00270     {
00271         return( (_vx - vv.x() )*(_vx - vv.x() ) +
00272                 (_vy - vv.y() )*(_vy - vv.y() ) +
00273                 (_vz - vv.z() )*(_vz - vv.z() ) );
00274     }
00275 
00277     void createOrthoVector(vector3 &v) const;
00278 
00279 } ;
00280 
00282 OBAPI double Point2Plane(vector3 a, vector3 b, vector3 c, vector3 d);
00283 
00284 //  The global constant vector3s
00285 extern OBAPI const vector3 VZero;
00286 extern OBAPI const vector3 VX;
00287 extern OBAPI const vector3 VY;
00288 extern OBAPI const vector3 VZ;
00289 
00290 #ifndef SWIG
00291 OBAPI vector3 center_coords(double*,int);
00292 #endif
00293 }
00294 
00295 #endif // OB_VECTOR_H
00296