NCBI C++ ToolKit
vector_math.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: vector_math.hpp 33815 2007-05-04 17:18:18Z kazimird $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Authors: Paul Thiessen
27 *
28 * File Description:
29 * Vector and Matrix classes to simplify 3-d geometry calculations
30 *
31 * ===========================================================================
32 */
33 
34 #ifndef CN3D_VECTORMATH__HPP
35 #define CN3D_VECTORMATH__HPP
36 
37 #include <math.h>
38 
39 #include <corelib/ncbistre.hpp>
40 #include <corelib/ncbistl.hpp>
41 #include <corelib/ncbidiag.hpp>
42 
43 BEGIN_SCOPE(Cn3D)
44 
45 class Vector {
46 public:
47  double x, y, z;
48 
49  Vector(double xi = 0.0, double yi = 0.0, double zi = 0.0)
50  {
51  x=xi; y=yi; z=zi;
52  }
53  Vector(const Vector& v)
54  {
55  x=v.x; y=v.y; z=v.z;
56  }
58  {
59  x=v.x; y=v.y; z=v.z;
60  return *this;
61  }
62  void Set(double xs, double ys, double zs)
63  {
64  x=xs; y=ys; z=zs;
65  }
66  bool operator == (const Vector& other)
67  {
68  return (x == other.x && y == other.y && z == other.z);
69  }
70  bool operator != (const Vector& other)
71  {
72  return !(*this == other);
73  }
74  double& operator [] (unsigned int i)
75  {
76  static double err = 0.0;
77  if (i == 0) return x;
78  else if (i == 1) return y;
79  else if (i == 2) return z;
80  else ERR_POST(ncbi::Error << "Vector operator [] access out of range : " << i);
81  return err;
82  }
83  double operator [] (unsigned int i) const
84  {
85  if (i == 0) return x;
86  else if (i == 1) return y;
87  else if (i == 2) return z;
88  else ERR_POST(ncbi::Error << "Vector operator [] access out of range : " << i);
89  return 0.0;
90  }
91  friend Vector operator - (const Vector& a)
92  {
93  return Vector(-a.x, -a.y, -a.z);
94  }
95  friend Vector operator + (const Vector& a, const Vector& b)
96  {
97  return Vector(a.x+b.x, a.y+b.y, a.z+b.z);
98  }
100  {
101  x+=v.x; y+=v.y; z+=v.z;
102  return *this;
103  }
104  friend Vector operator - (const Vector& a, const Vector& b)
105  {
106  return Vector(a.x-b.x, a.y-b.y, a.z-b.z);
107  }
109  {
110  x-=v.x; y-=v.y; z-=v.z;
111  return *this;
112  }
113  friend Vector operator * (const Vector& v, double f)
114  {
115  return Vector(v.x*f, v.y*f, v.z*f);
116  }
117  friend Vector operator * (double f, const Vector& v)
118  {
119  return Vector(v.x*f, v.y*f, v.z*f);
120  }
122  {
123  x*=f; y*=f; z*=f;
124  return *this;
125  }
126  friend Vector operator / (const Vector& v, double f)
127  {
128  return Vector(v.x/f, v.y/f, v.z/f);
129  }
131  {
132  x/=f; y/=f; z/=f;
133  return *this;
134  }
135  double length(void) const
136  {
137  return sqrt(x*x + y*y + z*z);
138  }
139  double lengthSquared(void) const
140  {
141  return (x*x + y*y + z*z);
142  }
143  void normalize(void)
144  {
145  *this /= length();
146  }
147  friend double vector_dot(const Vector& a, const Vector& b)
148  {
149  return (a.x*b.x + a.y*b.y + a.z*b.z);
150  }
151  friend Vector vector_cross(const Vector& a, const Vector& b)
152  {
153  return Vector(
154  a.y*b.z - a.z*b.y,
155  a.z*b.x - a.x*b.z,
156  a.x*b.y - a.y*b.x
157  );
158  }
159 };
160 
162 {
163  return s << '<' << v.x << ',' << v.y << ',' << v.z << '>';
164 }
165 
166 class Matrix {
167 public:
168  double m[16];
169  Matrix(double m0 =1, double m1 =0, double m2 =0, double m3 =0,
170  double m4 =0, double m5 =1, double m6 =0, double m7 =0,
171  double m8 =0, double m9 =0, double m10 =1, double m11 =0,
172  double m12 =0, double m13 =0, double m14 =0, double m15 =1) {
173  m[0]=m0; m[1]=m1; m[2]=m2; m[3]=m3;
174  m[4]=m4; m[5]=m5; m[6]=m6; m[7]=m7;
175  m[8]=m8; m[9]=m9; m[10]=m10; m[11]=m11;
176  m[12]=m12; m[13]=m13; m[14]=m14; m[15]=m15;
177  }
178  Matrix(const Matrix& o) {
179  for (int i=0; i<16; ++i) m[i]=o.m[i];
180  }
181  void SetToIdentity(void) {
182  m[0] = m[5] = m[10] = m[15] = 1;
183  m[1] = m[2] = m[3] = m[4] = m[6] = m[7] =
184  m[8] = m[9] = m[11] = m[12] = m[13] = m[14] = 0;
185  }
186  Matrix& operator = (const Matrix& o) {
187  for (int i=0; i<16; ++i) m[i]=o.m[i];
188  return *this;
189  }
190  double& operator [] (unsigned int i)
191  {
192  static double err = 0.0;
193  if (i > 15) {
194  ERR_POST(ncbi::Error << "Matrix operator [] access out of range : " << i);
195  return err;
196  }
197  return m[i];
198  }
199  double operator [] (unsigned int i) const
200  {
201  if (i > 15) {
202  ERR_POST(ncbi::Error << "Matrix operator [] access out of range : " << i);
203  return 0.0;
204  }
205  return m[i];
206  }
207 };
208 
209 void SetTranslationMatrix(Matrix* m, const Vector& v, int n =1);
210 void SetScaleMatrix(Matrix* m, const Vector& v);
211 void SetRotationMatrix(Matrix* m, const Vector& v, double rad, int n =1);
212 void ApplyTransformation(Vector* v, const Matrix& m);
213 void ComposeInto(Matrix* C, const Matrix& A, const Matrix& B);
214 void InvertInto(Matrix* I, const Matrix& A);
215 
216 // Rigid body fit algorithm
217 void RigidBodyFit(
218  int natx, const Vector * const *xref, const Vector * const *xvar, const double *weights, // inputs
219  Vector& cgref, Vector& cgvar, Matrix& rotMat); // outputs
220 
221 // RMSD calculator
222 double ComputeRMSD(int nCoords, const Vector * const *masterCoords,
223  const Vector * const *dependentCoords, const Matrix *transformDependentToMaster);
224 
225 END_SCOPE(Cn3D)
226 
227 #endif // CN3D_VECTORMATH__HPP
Matrix(const Matrix &o)
double m[16]
virtual T * operator[](size_t i_)
Definition: njn_matrix.hpp:510
Matrix(double m0=1, double m1=0, double m2=0, double m3=0, double m4=0, double m5=1, double m6=0, double m7=0, double m8=0, double m9=0, double m10=1, double m11=0, double m12=0, double m13=0, double m14=0, double m15=1)
Matrix & operator=(const Matrix &matrix_)
Definition: njn_matrix.hpp:258
void SetToIdentity(void)
Vector & operator+=(const Vector &v)
Definition: vector_math.hpp:99
double lengthSquared(void) const
double y
Definition: vector_math.hpp:47
friend Vector operator+(const Vector &a, const Vector &b)
Definition: vector_math.hpp:95
double x
Definition: vector_math.hpp:47
bool operator==(const Vector &other)
Definition: vector_math.hpp:66
bool operator!=(const Vector &other)
Definition: vector_math.hpp:70
Vector & operator/=(double f)
Vector(const Vector &v)
Definition: vector_math.hpp:53
Vector & operator*=(double f)
double length(void) const
virtual T & operator[](size_t i_)
Definition: njn_vector.hpp:310
double z
Definition: vector_math.hpp:47
friend Vector operator-(const Vector &a)
Definition: vector_math.hpp:91
friend double vector_dot(const Vector &a, const Vector &b)
friend Vector vector_cross(const Vector &a, const Vector &b)
void normalize(void)
Vector & operator=(const Vector &vector_)
Definition: njn_vector.hpp:134
Vector(double xi=0.0, double yi=0.0, double zi=0.0)
Definition: vector_math.hpp:49
friend Vector operator/(const Vector &v, double f)
void Set(double xs, double ys, double zs)
Definition: vector_math.hpp:62
friend Vector operator*(const Vector &v, double f)
Vector & operator-=(const Vector &v)
#define C(s)
Definition: common.h:231
static FILE * f
Definition: readconf.c:23
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
int i
yy_size_t n
unsigned int a
Definition: ncbi_localip.c:102
Defines NCBI C++ diagnostic APIs, classes, and macros.
The NCBI C++/STL use hints.
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
#define A
#define B
void InvertInto(Matrix *I, const Matrix &A)
void SetTranslationMatrix(Matrix *m, const Vector &v, int n=1)
Definition: vector_math.cpp:45
ncbi::CNcbiOstream & operator<<(ncbi::CNcbiOstream &s, const Vector &v)
void SetRotationMatrix(Matrix *m, const Vector &v, double rad, int n=1)
Definition: vector_math.cpp:62
double ComputeRMSD(int nCoords, const Vector *const *masterCoords, const Vector *const *dependentCoords, const Matrix *transformDependentToMaster)
void ComposeInto(Matrix *C, const Matrix &A, const Matrix &B)
void ApplyTransformation(Vector *v, const Matrix &m)
Definition: vector_math.cpp:91
void SetScaleMatrix(Matrix *m, const Vector &v)
Definition: vector_math.cpp:53
void RigidBodyFit(int natx, const Vector *const *xref, const Vector *const *xvar, const double *weights, Vector &cgref, Vector &cgvar, Matrix &rotMat)
Modified on Fri Sep 20 14:57:02 2024 by modify_doxy.py rev. 669887