| 1 | tim | 741 | /********************************************************************** | 
| 2 |  |  | vector3.h - Handle 3D coordinates. | 
| 3 |  |  |  | 
| 4 |  |  | Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc. | 
| 5 |  |  | Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison | 
| 6 |  |  |  | 
| 7 |  |  | This file is part of the Open Babel project. | 
| 8 |  |  | For more information, see <http://openbabel.sourceforge.net/> | 
| 9 |  |  |  | 
| 10 |  |  | This program is free software; you can redistribute it and/or modify | 
| 11 |  |  | it under the terms of the GNU General Public License as published by | 
| 12 |  |  | the Free Software Foundation version 2 of the License. | 
| 13 |  |  |  | 
| 14 |  |  | This program is distributed in the hope that it will be useful, | 
| 15 |  |  | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 16 |  |  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 17 |  |  | GNU General Public License for more details. | 
| 18 |  |  | ***********************************************************************/ | 
| 19 |  |  |  | 
| 20 |  |  | #ifndef OB_VECTOR_H | 
| 21 |  |  | #define OB_VECTOR_H | 
| 22 |  |  |  | 
| 23 |  |  | #if HAVE_IOSTREAM | 
| 24 |  |  | #include <iostream> | 
| 25 |  |  | #elif HAVE_IOSTREAM_H | 
| 26 |  |  | #include <iostream.h> | 
| 27 |  |  | #endif | 
| 28 |  |  |  | 
| 29 |  |  | #if HAVE_FSTREAM | 
| 30 |  |  | #include <fstream> | 
| 31 |  |  | #elif HAVE_FSTREAM_H | 
| 32 |  |  | #include <fstream.h> | 
| 33 |  |  | #endif | 
| 34 |  |  |  | 
| 35 |  |  | #include <math.h> | 
| 36 |  |  | #include "obutil.hpp" | 
| 37 |  |  |  | 
| 38 |  |  | #ifndef PI | 
| 39 |  |  | #define PI 3.1415926535897932384626433 | 
| 40 |  |  | #endif | 
| 41 |  |  |  | 
| 42 |  |  | #ifndef RAD_TO_DEG | 
| 43 |  |  | #define RAD_TO_DEG 180.0/PI | 
| 44 |  |  | #endif | 
| 45 |  |  |  | 
| 46 |  |  | #ifndef DEG_TO_RAD | 
| 47 |  |  | #define DEG_TO_RAD PI/180.0 | 
| 48 |  |  | #endif | 
| 49 |  |  |  | 
| 50 |  |  | namespace OpenBabel | 
| 51 |  |  | { | 
| 52 |  |  |  | 
| 53 |  |  | class matrix3x3; | 
| 54 |  |  |  | 
| 55 |  |  | // class introduction in vector3.cpp | 
| 56 |  |  | class   OBAPI vector3 | 
| 57 |  |  | { | 
| 58 |  |  | private : | 
| 59 |  |  | double              _vx, _vy, _vz ; | 
| 60 |  |  |  | 
| 61 |  |  | public : | 
| 62 |  |  | //! Constructor | 
| 63 |  |  | vector3 (const double x=0.0, const double y=0.0, const double z=0.0) | 
| 64 |  |  | { | 
| 65 |  |  | _vx = x; | 
| 66 |  |  | _vy = y; | 
| 67 |  |  | _vz = z; | 
| 68 |  |  | }; | 
| 69 |  |  | //! Copy Constructor | 
| 70 |  |  | vector3 (const vector3& v) | 
| 71 |  |  | { | 
| 72 |  |  | _vx = v._vx; | 
| 73 |  |  | _vy = v._vy; | 
| 74 |  |  | _vz = v._vz; | 
| 75 |  |  | }; | 
| 76 |  |  |  | 
| 77 |  |  | //! set x,y and z-component of a vector | 
| 78 |  |  | void Set(const double x, const double y, const double z) | 
| 79 |  |  | { | 
| 80 |  |  | _vx = x ; | 
| 81 |  |  | _vy = y ; | 
| 82 |  |  | _vz = z ; | 
| 83 |  |  | }; | 
| 84 |  |  | //! set x,y and z-component of a vector from c[0]..c[2] | 
| 85 |  |  | void Set(const double *c) | 
| 86 |  |  | { | 
| 87 |  |  | _vx = c[0]; | 
| 88 |  |  | _vy = c[1]; | 
| 89 |  |  | _vz = c[2]; | 
| 90 |  |  | } | 
| 91 |  |  | //! access function to get the x-coordinate of the vector | 
| 92 |  |  | void SetX(const double x) | 
| 93 |  |  | { | 
| 94 |  |  | _vx = x; | 
| 95 |  |  | }; | 
| 96 |  |  | //! access function to get the y-coordinate of the vector | 
| 97 |  |  | void SetY(const double y) | 
| 98 |  |  | { | 
| 99 |  |  | _vy = y; | 
| 100 |  |  | }; | 
| 101 |  |  | //! access function to get the z-coordinate of the vector | 
| 102 |  |  | void SetZ(const double z) | 
| 103 |  |  | { | 
| 104 |  |  | _vz = z; | 
| 105 |  |  | }; | 
| 106 |  |  | //! set c[0]..c[2] to the components of the vector | 
| 107 |  |  | void Get(double *c) | 
| 108 |  |  | { | 
| 109 |  |  | c[0]=_vx; | 
| 110 |  |  | c[1]=_vy; | 
| 111 |  |  | c[2]=_vz; | 
| 112 |  |  | }; | 
| 113 |  |  | //! access function | 
| 114 |  |  | double& operator[] ( unsigned int i); | 
| 115 |  |  |  | 
| 116 |  |  | //! assignment | 
| 117 |  |  | vector3& operator= ( const vector3& v) | 
| 118 |  |  | { | 
| 119 |  |  | _vx = v._vx; | 
| 120 |  |  | _vy = v._vy; | 
| 121 |  |  | _vz = v._vz; | 
| 122 |  |  | return *this; | 
| 123 |  |  | }; | 
| 124 |  |  |  | 
| 125 |  |  | //! prints a representation of the vector as a row vector of the form "<0.1,1,2>" | 
| 126 |  |  | friend OBAPI std::ostream& operator<< ( std::ostream&, const vector3& ) ; | 
| 127 |  |  |  | 
| 128 |  |  | //  Comparison | 
| 129 |  |  | friend OBAPI int operator== ( const vector3&, const vector3& ) ; | 
| 130 |  |  | friend OBAPI int operator!= ( const vector3&, const vector3& ) ; | 
| 131 |  |  |  | 
| 132 |  |  | //  Sum, Difference, Scalar Product | 
| 133 |  |  | //! vector addition | 
| 134 |  |  | friend OBAPI vector3 operator+ ( const vector3& v1, const vector3& v2) | 
| 135 |  |  | { | 
| 136 |  |  | return vector3(v1._vx+v2._vx, v1._vy+v2._vy, v1._vz+v2._vz); | 
| 137 |  |  | }; | 
| 138 |  |  | //! vector subtraction | 
| 139 |  |  | friend OBAPI vector3 operator- ( const vector3& v1, const vector3& v2) | 
| 140 |  |  | { | 
| 141 |  |  | return vector3(v1._vx-v2._vx, v1._vy-v2._vy, v1._vz-v2._vz); | 
| 142 |  |  | }; | 
| 143 |  |  | //! unary minus | 
| 144 |  |  | friend OBAPI vector3 operator- ( const vector3& v) | 
| 145 |  |  | { | 
| 146 |  |  | return vector3(-v._vx, -v._vy, -v._vz); | 
| 147 |  |  | }; | 
| 148 |  |  | //! multiplication with a scalar | 
| 149 |  |  | friend OBAPI vector3 operator* ( const double& c, const vector3& v) | 
| 150 |  |  | { | 
| 151 |  |  | return vector3( c*v._vx, c*v._vy, c*v._vz); | 
| 152 |  |  | }; | 
| 153 |  |  | //! multiplication with a scalar | 
| 154 |  |  | friend OBAPI vector3 operator* ( const vector3& v, const double& c) | 
| 155 |  |  | { | 
| 156 |  |  | return vector3( c*v._vx, c*v._vy, c*v._vz); | 
| 157 |  |  | }; | 
| 158 |  |  | //! division by a scalar | 
| 159 |  |  | friend OBAPI vector3 operator/ ( const vector3& v, const double& c) | 
| 160 |  |  | { | 
| 161 |  |  | return vector3( v._vx/c, v._vy/c, v._vz/c); | 
| 162 |  |  | }; | 
| 163 |  |  | // @removed@ misleading operation | 
| 164 |  |  | // friend vector3 operator* ( const vector3 &,const vector3 &); | 
| 165 |  |  |  | 
| 166 |  |  | //vector and matrix ops | 
| 167 |  |  | // @removed@ misleading operation; matrix multiplication is not commutitative | 
| 168 |  |  | //     friend vector3 operator *(const vector3 &v,const matrix3x3 &m); | 
| 169 |  |  |  | 
| 170 |  |  | //! multiplication of matrix and vector | 
| 171 |  |  | friend OBAPI vector3 operator *(const matrix3x3 &m,const vector3 &v); | 
| 172 |  |  |  | 
| 173 |  |  | //  Immediate Sum, Difference, Scalar Product | 
| 174 |  |  | vector3& operator+= ( const vector3& v) | 
| 175 |  |  | { | 
| 176 |  |  | _vx += v._vx; | 
| 177 |  |  | _vy += v._vy; | 
| 178 |  |  | _vz += v._vz; | 
| 179 |  |  | return *this; | 
| 180 |  |  | }; | 
| 181 |  |  | vector3& operator-= ( const vector3& v) | 
| 182 |  |  | { | 
| 183 |  |  | _vx -= v._vx; | 
| 184 |  |  | _vy -= v._vy; | 
| 185 |  |  | _vz -= v._vz; | 
| 186 |  |  | return *this; | 
| 187 |  |  | }; | 
| 188 |  |  | vector3& operator+= ( const double* f) | 
| 189 |  |  | { | 
| 190 |  |  | _vx += f[0]; | 
| 191 |  |  | _vy += f[1]; | 
| 192 |  |  | _vz += f[2]; | 
| 193 |  |  | return *this; | 
| 194 |  |  | }; | 
| 195 |  |  | vector3& operator-= ( const double* f) | 
| 196 |  |  | { | 
| 197 |  |  | _vx -= f[0]; | 
| 198 |  |  | _vy -= f[1]; | 
| 199 |  |  | _vz -= f[2]; | 
| 200 |  |  | return *this; | 
| 201 |  |  | }; | 
| 202 |  |  | vector3& operator*= ( const double& c) | 
| 203 |  |  | { | 
| 204 |  |  | _vx *= c; | 
| 205 |  |  | _vy *= c; | 
| 206 |  |  | _vz *= c; | 
| 207 |  |  | return *this; | 
| 208 |  |  | }; | 
| 209 |  |  | vector3& operator/= ( const double& c) | 
| 210 |  |  | { | 
| 211 |  |  | _vx /= c; | 
| 212 |  |  | _vy /= c; | 
| 213 |  |  | _vz /= c; | 
| 214 |  |  | return *this; | 
| 215 |  |  | }; | 
| 216 |  |  | //! multiplication of matrix and vector | 
| 217 |  |  | vector3& operator*= ( const matrix3x3 &); | 
| 218 |  |  |  | 
| 219 |  |  | //! create a random unit vector | 
| 220 |  |  | void randomUnitVector(OBRandom *oeRand= 0L); | 
| 221 |  |  |  | 
| 222 |  |  | //  Member Functions | 
| 223 |  |  |  | 
| 224 |  |  | //! dot product of two vectors | 
| 225 |  |  | friend OBAPI double dot ( const vector3&, const vector3& ) ; | 
| 226 |  |  |  | 
| 227 |  |  | //! cross product of two vectors | 
| 228 |  |  | friend OBAPI vector3 cross ( const vector3&, const vector3& ) ; | 
| 229 |  |  |  | 
| 230 |  |  | //! calculate angle between vectors | 
| 231 |  |  | friend OBAPI double vectorAngle ( const vector3& v1, const vector3& v2 ); | 
| 232 |  |  |  | 
| 233 |  |  | //! calculate the torsion angle between vectors | 
| 234 |  |  | friend OBAPI double CalcTorsionAngle(const vector3 &a, const vector3 &b, | 
| 235 |  |  | const vector3 &c, const vector3 &d); | 
| 236 |  |  |  | 
| 237 |  |  | //! scales a vector to give it length one. | 
| 238 |  |  | vector3& normalize () ; | 
| 239 |  |  |  | 
| 240 |  |  | //! vector length | 
| 241 |  |  | double length () const | 
| 242 |  |  | { | 
| 243 |  |  | return sqrt(_vx*_vx + _vy*_vy + _vz*_vz); | 
| 244 |  |  | }; | 
| 245 |  |  | //! vector length squared | 
| 246 |  |  | double length_2 () const | 
| 247 |  |  | { | 
| 248 |  |  | return _vx*_vx + _vy*_vy + _vz*_vz; | 
| 249 |  |  | }; | 
| 250 |  |  | //! access function to get the x-coordinate of the vector | 
| 251 |  |  | double x () const | 
| 252 |  |  | { | 
| 253 |  |  | return _vx ; | 
| 254 |  |  | } ; | 
| 255 |  |  | //! access function to get the y-coordinate of the vector | 
| 256 |  |  | double y () const | 
| 257 |  |  | { | 
| 258 |  |  | return _vy ; | 
| 259 |  |  | } ; | 
| 260 |  |  | //! access function to get the z-coordinate of the vector | 
| 261 |  |  | double z () const | 
| 262 |  |  | { | 
| 263 |  |  | return _vz ; | 
| 264 |  |  | } ; | 
| 265 |  |  |  | 
| 266 |  |  | //! square to the distance between *this and vv | 
| 267 |  |  | /*! equivalent to length_2(*this-vv) | 
| 268 |  |  | */ | 
| 269 |  |  | inline double distSq(const vector3 &vv) const | 
| 270 |  |  | { | 
| 271 |  |  | return( (_vx - vv.x() )*(_vx - vv.x() ) + | 
| 272 |  |  | (_vy - vv.y() )*(_vy - vv.y() ) + | 
| 273 |  |  | (_vz - vv.z() )*(_vz - vv.z() ) ); | 
| 274 |  |  | } | 
| 275 |  |  |  | 
| 276 |  |  | //! creates a vector of length one, orthogonal to *this. | 
| 277 |  |  | void createOrthoVector(vector3 &v) const; | 
| 278 |  |  |  | 
| 279 |  |  | } ; | 
| 280 |  |  |  | 
| 281 |  |  | //! \brief Calculate the distance of point a to the plane determined by b,c,d | 
| 282 |  |  | OBAPI double Point2Plane(vector3 a, vector3 b, vector3 c, vector3 d); | 
| 283 |  |  |  | 
| 284 |  |  | //  The global constant vector3s | 
| 285 |  |  | extern OBAPI const vector3 VZero; | 
| 286 |  |  | extern OBAPI const vector3 VX; | 
| 287 |  |  | extern OBAPI const vector3 VY; | 
| 288 |  |  | extern OBAPI const vector3 VZ; | 
| 289 |  |  |  | 
| 290 |  |  | OBAPI vector3 center_coords(double*,int); | 
| 291 |  |  | } | 
| 292 |  |  |  | 
| 293 |  |  | #endif // OB_VECTOR_H | 
| 294 |  |  |  | 
| 295 |  |  | //! \file | 
| 296 |  |  | //! \brief Handle 3D coordinates. |