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 #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 } 00097 void Get(double *c) 00098 { 00099 c[0]=_vx; 00100 c[1]=_vy; 00101 c[2]=_vz; 00102 } 00104 double operator[] ( unsigned int i) const; 00105 00107 vector3& operator= ( const vector3& v) 00108 { 00109 _vx = v._vx; 00110 _vy = v._vy; 00111 _vz = v._vz; 00112 return *this; 00113 } 00114 00116 const double *AsArray() const 00117 { 00118 return &_vx; 00119 } 00120 00123 vector3& operator+= ( const vector3& v) 00124 { 00125 _vx += v._vx; 00126 _vy += v._vy; 00127 _vz += v._vz; 00128 return *this; 00129 }; 00132 vector3& operator-= ( const vector3& v) 00133 { 00134 _vx -= v._vx; 00135 _vy -= v._vy; 00136 _vz -= v._vz; 00137 return *this; 00138 }; 00141 vector3& operator+= ( const double* f) 00142 { 00143 _vx += f[0]; 00144 _vy += f[1]; 00145 _vz += f[2]; 00146 return *this; 00147 }; 00150 vector3& operator-= ( const double* f) 00151 { 00152 _vx -= f[0]; 00153 _vy -= f[1]; 00154 _vz -= f[2]; 00155 return *this; 00156 }; 00159 vector3& operator*= ( const double& c) 00160 { 00161 _vx *= c; 00162 _vy *= c; 00163 _vz *= c; 00164 return *this; 00165 }; 00166 00169 vector3& operator/= ( const double& c) 00170 { 00171 double inv = 1.0 / c; 00172 return( (*this) *= inv ); 00173 }; 00177 vector3& operator*= ( const matrix3x3 &); 00178 00180 void randomUnitVector(OBRandom *oeRand= NULL); 00181 00182 // Member Functions 00183 00186 vector3& normalize () ; 00187 00189 bool CanBeNormalized () const; 00190 00192 inline double length_2 () const 00193 { 00194 return _vx*_vx + _vy*_vy + _vz*_vz; 00195 }; 00197 double length () const 00198 { 00199 return sqrt( length_2() ); 00200 }; 00202 const double & x () const 00203 { 00204 return _vx ; 00205 } ; 00207 const double & y () const 00208 { 00209 return _vy ; 00210 } ; 00212 const double & z () const 00213 { 00214 return _vz ; 00215 } ; 00217 double & x () 00218 { 00219 return _vx ; 00220 } ; 00222 double & y () 00223 { 00224 return _vy ; 00225 } ; 00227 double & z () 00228 { 00229 return _vz ; 00230 } ; 00231 00233 // @{ 00238 int operator== ( const vector3& ) const; 00242 int operator!= ( const vector3& other ) const 00243 { 00244 return ! ( (*this) == other ); 00245 } 00255 bool IsApprox( const vector3 & other, const double & precision ) const; 00257 00259 00261 double distSq(const vector3 &vv) const 00262 { 00263 double dx = x() - vv.x(); 00264 double dy = y() - vv.y(); 00265 double dz = z() - vv.z(); 00266 return( dx*dx + dy*dy + dz*dz ); 00267 } 00268 00271 bool createOrthoVector(vector3 &v) const; 00272 00273 }; 00274 00276 OBAPI std::ostream& operator<< ( std::ostream&, const vector3& ); 00277 00278 // Sum, Difference, Scalar Product 00280 inline OBAPI vector3 operator+ ( const vector3& v1, const vector3& v2) 00281 { 00282 return vector3(v1.x()+v2.x(), v1.y()+v2.y(), v1.z()+v2.z()); 00283 } 00285 inline OBAPI vector3 operator- ( const vector3& v1, const vector3& v2) 00286 { 00287 return vector3(v1.x()-v2.x(), v1.y()-v2.y(), v1.z()-v2.z()); 00288 } 00290 inline OBAPI vector3 operator- ( const vector3& v) 00291 { 00292 return vector3(-v.x(), -v.y(), -v.z()); 00293 } 00295 inline OBAPI vector3 operator* ( const double& c, const vector3& v) 00296 { 00297 return vector3( c*v.x(), c*v.y(), c*v.z()); 00298 } 00300 inline OBAPI vector3 operator* ( const vector3& v, const double& c) 00301 { 00302 return vector3( c*v.x(), c*v.y(), c*v.z()); 00303 } 00305 inline OBAPI vector3 operator/ ( const vector3& v, const double& c) 00306 { 00307 return vector3( v.x()/c, v.y()/c, v.z()/c); 00308 } 00309 // @removed@ misleading operation 00310 // friend vector3 operator* ( const vector3 &,const vector3 &); 00311 00312 //vector and matrix ops 00313 // @removed@ misleading operation; matrix multiplication is not commutitative 00314 // friend vector3 operator *(const vector3 &v,const matrix3x3 &m); 00315 00317 OBAPI vector3 operator *(const matrix3x3 &m, const vector3 &v); 00318 00320 inline OBAPI double dot ( const vector3& v1, const vector3& v2 ) 00321 { 00322 return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z() ; 00323 } 00325 OBAPI vector3 cross ( const vector3&, const vector3& ); 00326 00328 OBAPI double vectorAngle ( const vector3& v1, const vector3& v2 ); 00329 00331 OBAPI double CalcTorsionAngle(const vector3 &a, const vector3 &b, 00332 const vector3 &c, const vector3 &d); 00333 00335 OBAPI double Point2Plane(vector3 a, vector3 b, vector3 c, vector3 d); 00337 OBAPI double Point2PlaneAngle(const vector3 a, const vector3 b, const vector3 c, const vector3 d); 00338 00339 // The global constant vector3 objects 00341 extern OBAPI const vector3 VZero; 00343 extern OBAPI const vector3 VX; 00345 extern OBAPI const vector3 VY; 00347 extern OBAPI const vector3 VZ; 00348 00349 } 00350 00351 #endif // OB_VECTOR_H 00352
This file is part of the documentation for Open Babel, version 2.2.0.