Open Babel  3.0
vector3.h
Go to the documentation of this file.
1 /**********************************************************************
2 vector3.h - Handle 3D coordinates.
3 
4 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
5 Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
6 Some portions Copyright (C) 2006 by Benoit Jacob
7 
8 This file is part of the Open Babel project.
9 For more information, see <http://openbabel.org/>
10 
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation version 2 of the License.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19 ***********************************************************************/
20 
21 #ifndef OB_VECTOR_H
22 #define OB_VECTOR_H
23 
24 #include <ostream>
25 #include <math.h>
26 #include <iostream>
27 
28 #include <openbabel/babelconfig.h>
29 
30 #ifndef RAD_TO_DEG
31 #define RAD_TO_DEG (180.0/M_PI)
32 #endif
33 
34 #ifndef DEG_TO_RAD
35 #define DEG_TO_RAD (M_PI/180.0)
36 #endif
37 
38 namespace OpenBabel
39 {
40 
41  class matrix3x3; // declared in math/matrix3x3.h
42 
43  // class introduction in vector3.cpp
44  class OBAPI vector3
45  {
46  private :
47  double _vx, _vy, _vz ;
48 
49  public :
51  vector3 (const double inX=0.0, const double inY=0.0, const double inZ=0.0):
52  _vx(inX), _vy(inY), _vz(inZ)
53  {}
54  vector3 (double inV[3]):
55  _vx(inV[0]), _vy(inV[1]), _vz(inV[2])
56  {}
58  vector3 (const vector3& v):
59  _vx(v._vx), _vy(v._vy), _vz(v._vz)
60  { }
61 
63  ~vector3() { }
64 
66  typedef double* iterator;
67 
69  typedef const double* const_iterator;
70 
72  typedef std::ptrdiff_t difference_type;
73 
75  iterator begin() { return &_vx; }
76 
78  iterator end() { return &_vx + 3; }
79 
81  const_iterator begin() const { return &_vx; }
82 
84  const_iterator end() const { return &_vx + 3; }
85 
87  void Set(const double inX, const double inY, const double inZ)
88  {
89  _vx = inX;
90  _vy = inY;
91  _vz = inZ;
92  }
94  void Set(const double *c)
95  {
96  _vx = c[0];
97  _vy = c[1];
98  _vz = c[2];
99  }
101  void SetX(const double inX)
102  {
103  _vx = inX;
104  }
106  void SetY(const double inY)
107  {
108  _vy = inY;
109  }
111  void SetZ(const double inZ)
112  {
113  _vz = inZ;
114  }
116  double GetX() const
117  {
118  return _vx;
119  }
121  double GetY() const
122  {
123  return _vy;
124  }
126  double GetZ() const
127  {
128  return _vz;
129  }
132  void Get(double *c)
133  {
134  c[0]=_vx;
135  c[1]=_vy;
136  c[2]=_vz;
137  }
139  double operator[] ( unsigned int i) const;
140 
142  vector3& operator= ( const vector3& v)
143  {
144  _vx = v._vx;
145  _vy = v._vy;
146  _vz = v._vz;
147  return *this;
148  }
149 
151  const double *AsArray() const
152  {
153  return &_vx;
154  }
155 
158  vector3& operator+= ( const vector3& v)
159  {
160  _vx += v._vx;
161  _vy += v._vy;
162  _vz += v._vz;
163  return *this;
164  };
167  vector3& operator-= ( const vector3& v)
168  {
169  _vx -= v._vx;
170  _vy -= v._vy;
171  _vz -= v._vz;
172  return *this;
173  };
176  vector3& operator+= ( const double* f)
177  {
178  _vx += f[0];
179  _vy += f[1];
180  _vz += f[2];
181  return *this;
182  };
185  vector3& operator-= ( const double* f)
186  {
187  _vx -= f[0];
188  _vy -= f[1];
189  _vz -= f[2];
190  return *this;
191  };
194  vector3& operator*= ( const double& c)
195  {
196  _vx *= c;
197  _vy *= c;
198  _vz *= c;
199  return *this;
200  };
201 
204  vector3& operator/= ( const double& c)
205  {
206  double inv = 1.0 / c;
207  return( (*this) *= inv );
208  };
212  vector3& operator*= ( const matrix3x3 &);
213 
215  void randomUnitVector();
216 
217  // Member Functions
218 
221  vector3& normalize () ;
222 
224  bool CanBeNormalized () const;
225 
227  inline double length_2 () const
228  {
229  return _vx*_vx + _vy*_vy + _vz*_vz;
230  };
232  double length () const
233  {
234  return sqrt( length_2() );
235  };
237  const double & x () const
238  {
239  return _vx ;
240  } ;
242  const double & y () const
243  {
244  return _vy ;
245  } ;
247  const double & z () const
248  {
249  return _vz ;
250  } ;
252  double & x ()
253  {
254  return _vx ;
255  } ;
257  double & y ()
258  {
259  return _vy ;
260  } ;
262  double & z ()
263  {
264  return _vz ;
265  } ;
266 
268  // @{
273  int operator== ( const vector3& ) const;
277  int operator!= ( const vector3& other ) const
278  {
279  return ! ( (*this) == other );
280  }
290  bool IsApprox( const vector3 & other, const double & precision ) const;
292 
294 
296  double distSq(const vector3 &vv) const
297  {
298  double dx = x() - vv.x();
299  double dy = y() - vv.y();
300  double dz = z() - vv.z();
301  return( dx*dx + dy*dy + dz*dz );
302  }
303 
306  bool createOrthoVector(vector3 &v) const;
307 
308  };
309 
311  OBAPI std::ostream& operator<< ( std::ostream&, const vector3& );
312 
313  // Sum, Difference, Scalar Product
315  inline OBAPI vector3 operator+ ( const vector3& v1, const vector3& v2)
316  {
317  return vector3(v1.x()+v2.x(), v1.y()+v2.y(), v1.z()+v2.z());
318  }
320  inline OBAPI vector3 operator- ( const vector3& v1, const vector3& v2)
321  {
322  return vector3(v1.x()-v2.x(), v1.y()-v2.y(), v1.z()-v2.z());
323  }
325  inline OBAPI vector3 operator- ( const vector3& v)
326  {
327  return vector3(-v.x(), -v.y(), -v.z());
328  }
330  inline OBAPI vector3 operator* ( const double& c, const vector3& v)
331  {
332  return vector3( c*v.x(), c*v.y(), c*v.z());
333  }
335  inline OBAPI vector3 operator* ( const vector3& v, const double& c)
336  {
337  return vector3( c*v.x(), c*v.y(), c*v.z());
338  }
340  inline OBAPI vector3 operator/ ( const vector3& v, const double& c)
341  {
342  return vector3( v.x()/c, v.y()/c, v.z()/c);
343  }
344  // @removed@ misleading operation
345  // friend vector3 operator* ( const vector3 &,const vector3 &);
346 
347  //vector and matrix ops
348  // @removed@ misleading operation; matrix multiplication is not commutitative
349  // friend vector3 operator *(const vector3 &v,const matrix3x3 &m);
350 
352  OBAPI vector3 operator *(const matrix3x3 &m, const vector3 &v);
353 
355  inline OBAPI double dot ( const vector3& v1, const vector3& v2 )
356  {
357  return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z() ;
358  }
360  OBAPI vector3 cross ( const vector3&, const vector3& );
361 
363  OBAPI double vectorAngle ( const vector3& v1, const vector3& v2 );
364 
366  OBAPI double CalcTorsionAngle(const vector3 &a, const vector3 &b,
367  const vector3 &c, const vector3 &d);
368 
370  OBAPI double Point2PlaneSigned(vector3 a, vector3 b, vector3 c, vector3 d);
372  OBAPI double Point2Plane(vector3 a, vector3 b, vector3 c, vector3 d);
374  OBAPI double Point2PlaneAngle(const vector3 a, const vector3 b, const vector3 c, const vector3 d);
375 
377  OBAPI double Point2Line(const vector3& a, const vector3& b, const vector3& c);
378 
379  // The global constant vector3 objects
381  extern OBAPI const vector3 VZero;
383  extern OBAPI const vector3 VX;
385  extern OBAPI const vector3 VY;
387  extern OBAPI const vector3 VZ;
388 
389 }
390 
391 #endif // OB_VECTOR_H
392 
const vector3 VZero
The zero vector: <0.0, 0.0, 0.0>
vector3 operator/(const vector3 &v, const double &c)
Division by a scalar.
Definition: vector3.h:340
double length() const
Definition: vector3.h:232
const_iterator end() const
/return const_iterator to end
Definition: vector3.h:84
iterator begin()
Definition: vector3.h:75
vector3 operator+(const vector3 &v1, const vector3 &v2)
Vector addition.
Definition: vector3.h:315
void Set(const double inX, const double inY, const double inZ)
Set x,y and z-component of a vector.
Definition: vector3.h:87
void SetZ(const double inZ)
Access function to set the z-coordinate of the vector.
Definition: vector3.h:111
~vector3()
Destructor.
Definition: vector3.h:63
const vector3 VX
The x unit vector: <1.0, 0.0, 0.0>
double vectorAngle(const vector3 &v1, const vector3 &v2)
Calculate the angle between vectors (in degrees)
Definition: vector3.cpp:194
void SetY(const double inY)
Access function to set the y-coordinate of the vector.
Definition: vector3.h:106
const double & x() const
Access function to get the x-coordinate of the vector.
Definition: vector3.h:237
double length_2() const
Definition: vector3.h:227
vector3 cross(const vector3 &, const vector3 &)
Cross product of two vectors.
Definition: vector3.cpp:163
bool IsApprox(const double &a, const double &b, const double precision=1e-11)
Definition: obutil.h:226
void Set(const double *c)
Set x,y and z-component of a vector from c[0]..c[2].
Definition: vector3.h:94
double distSq(const vector3 &vv) const
}@
Definition: vector3.h:296
std::ptrdiff_t difference_type
A signed integral type for differences between two iterators.
Definition: vector3.h:72
double dot(const vector3 &v1, const vector3 &v2)
Dot product of two vectors.
Definition: vector3.h:355
iterator end()
Definition: vector3.h:78
vector3(const vector3 &v)
Copy Constructor.
Definition: vector3.h:58
double GetZ() const
Access function to get the z-coordinate of the vector.
Definition: vector3.h:126
const double * const_iterator
A random access iterator over const x, y, z.
Definition: vector3.h:69
vector3 operator-(const vector3 &v1, const vector3 &v2)
Vector subtraction.
Definition: vector3.h:320
const double * AsArray() const
Definition: vector3.h:151
double & x()
Access function to set the x-coordinate of the vector.
Definition: vector3.h:252
double GetX() const
Access function to get the x-coordinate of the vector.
Definition: vector3.h:116
const double & y() const
Access function to get the y-coordinate of the vector.
Definition: vector3.h:242
double Point2Plane(vector3 a, vector3 b, vector3 c, vector3 d)
Calculate the distance of point a to the plane determined by b,c,d.
Definition: vector3.cpp:316
double CalcTorsionAngle(const vector3 &a, const vector3 &b, const vector3 &c, const vector3 &d)
Calculate the torsion angle between vectors (in degrees)
Definition: vector3.cpp:222
Represents a vector in 3-dimensional real space.
Definition: vector3.h:44
const vector3 VY
The y unit vector: <0.0, 1.0, 0.0>
vector3(const double inX=0.0, const double inY=0.0, const double inZ=0.0)
Constructor.
Definition: vector3.h:51
double & y()
Access function to set the y-coordinate of the vector.
Definition: vector3.h:257
Represents a real 3x3 matrix.
Definition: matrix3x3.h:42
const vector3 VZ
The z unit vector: <0.0, 0.0, 1.0>
std::ostream & operator<<(std::ostream &, const vector3 &)
Prints a representation of the vector as a row vector of the form "<0.1,1,2>".
Definition: vector3.cpp:109
double Point2PlaneAngle(const vector3 a, const vector3 b, const vector3 c, const vector3 d)
Calculate the angle between point a and the plane determined by b,c,d.
Definition: vector3.cpp:322
double GetY() const
Access function to get the y-coordinate of the vector.
Definition: vector3.h:121
double Point2PlaneSigned(vector3 a, vector3 b, vector3 c, vector3 d)
Calculate the signed distance of point a to the plane determined by b,c,d.
Definition: vector3.cpp:308
double & z()
Access function to set the z-coordinate of the vector.
Definition: vector3.h:262
const_iterator begin() const
/return const_iterator to beginning
Definition: vector3.h:81
double * iterator
A random access iterator over x, y, z.
Definition: vector3.h:66
vector3 operator*(const double &c, const vector3 &v)
Multiplication with a scalar.
Definition: vector3.h:330
const double & z() const
Access function to get the z-coordinate of the vector.
Definition: vector3.h:247
void Get(double *c)
Set c[0]..c[2] to the components of the vector.
Definition: vector3.h:132
double Point2Line(const vector3 &a, const vector3 &b, const vector3 &c)
Calculate the distance of a point a to a line determined by b and c.
Definition: vector3.cpp:337
vector3(double inV[3])
Definition: vector3.h:54
bool operator==(const OBBitVec &bv1, const OBBitVec &bv2)
Definition: bitvec.cpp:525
void SetX(const double inX)
Access function to set the x-coordinate of the vector.
Definition: vector3.h:101
Global namespace for all Open Babel code.
Definition: alias.h:22