Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

colvarmodule::quaternion Class Reference

1-dimensional vector of real numbers with four components and a quaternion algebra More...

#include <colvartypes.h>

List of all members.

Public Member Functions

 quaternion (cvm::real const &x, cvm::real const &y, cvm::real const &z)
 Constructor from a 3-d vector.
 quaternion (cvm::real const qv[4])
 Constructor component by component.
 quaternion (cvm::real const &q0i, cvm::real const &q1i, cvm::real const &q2i, cvm::real const &q3i)
 Constructor component by component.
void set_from_euler_angles (cvm::real const &phi_in, cvm::real const &theta_in, cvm::real const &psi_in)
 quaternion ()
 Default constructor.
void set (cvm::real const value=0.0)
 Set all components to a scalar.
void reset ()
 Set all components to zero (null quaternion).
void reset_rotation ()
 Set the q0 component to 1 and the others to 0 (quaternion representing no rotation).
cvm::realoperator[] (int const &i)
 Access the quaternion as a 4-d array (return a reference).
cvm::real operator[] (int const &i) const
 Access the quaternion as a 4-d array (return a value).
cvm::real norm2 () const
 Square norm of the quaternion.
cvm::real norm () const
 Norm of the quaternion.
cvm::quaternion conjugate () const
 Return the conjugate quaternion.
void operator *= (cvm::real const &a)
void operator/= (cvm::real const &a)
void set_positive ()
void operator+= (cvm::quaternion const &h)
void operator-= (cvm::quaternion const &h)
cvm::rvector get_vector () const
 Return the vector component.
cvm::rvector rotate (cvm::rvector const &v) const
 Rotate v through this quaternion (put it in the rotated reference frame).
cvm::quaternion rotate (cvm::quaternion const &Q2) const
 Rotate Q2 through this quaternion (put it in the rotated reference frame).
cvm::rmatrix rotation_matrix () const
 Return the 3x3 matrix associated to this quaternion.
cvm::real cos2 (cvm::quaternion const &q) const
 Return the square cosine between the orientation associated to this quaternion and another.
cvm::real dist2 (cvm::quaternion const &Q2) const
 Square distance from another quaternion on the 4-dimensional unit sphere: returns the square of the angle along the shorter of the two geodesics.
cvm::quaternion dist2_grad (cvm::quaternion const &Q2) const
void match (cvm::quaternion &Q2) const
 Choose the closest between Q2 and -Q2 and save it back. Not required for dist2() and dist2_grad().
cvm::real inner (cvm::quaternion const &Q2) const
 Inner product (as a 4-d vector) with Q2; requires match() if the largest overlap is looked for.

Static Public Member Functions

size_t output_width (size_t const &real_width)
 Tell the number of characters required to print a quaternion, given that of a real number.
cvm::quaternion promote (cvm::rvector const &v)
 Promote a 3-vector to a quaternion.

Public Attributes

cvm::real q0
cvm::real q1
cvm::real q2
cvm::real q3

Friends

std::ostream & operator<< (std::ostream &os, cvm::quaternion const &q)
 Formatted output operator.
std::istream & operator>> (std::istream &is, cvm::quaternion &q)
 Formatted input operator.
cvm::quaternion operator+ (cvm::quaternion const &h, cvm::quaternion const &q)
cvm::quaternion operator- (cvm::quaternion const &h, cvm::quaternion const &q)
cvm::quaternion operator * (cvm::quaternion const &h, cvm::quaternion const &q)
 Provides the quaternion product. NOTE: for the inner product use:
 h.inner (q); 
.
cvm::quaternion operator * (cvm::real const &c, cvm::quaternion const &q)
cvm::quaternion operator * (cvm::quaternion const &q, cvm::real const &c)
cvm::quaternion operator/ (cvm::quaternion const &q, cvm::real const &c)


Detailed Description

1-dimensional vector of real numbers with four components and a quaternion algebra

Definition at line 552 of file colvartypes.h.


Constructor & Destructor Documentation

colvarmodule::quaternion::quaternion cvm::real const &  x,
cvm::real const &  y,
cvm::real const &  z
[inline]
 

Constructor from a 3-d vector.

Definition at line 559 of file colvartypes.h.

00560     : q0 (0.0), q1 (x), q2 (y), q3 (z)
00561   {}

colvarmodule::quaternion::quaternion cvm::real const   qv[4]  )  [inline]
 

Constructor component by component.

Definition at line 564 of file colvartypes.h.

00565     : q0 (qv[0]), q1 (qv[1]), q2 (qv[2]), q3 (qv[3])
00566   {}

colvarmodule::quaternion::quaternion cvm::real const &  q0i,
cvm::real const &  q1i,
cvm::real const &  q2i,
cvm::real const &  q3i
[inline]
 

Constructor component by component.

Definition at line 569 of file colvartypes.h.

00573     : q0 (q0i), q1 (q1i), q2 (q2i), q3 (q3i)
00574   {}

colvarmodule::quaternion::quaternion  )  [inline]
 

Default constructor.

Definition at line 597 of file colvartypes.h.

00598   {
00599     reset();
00600   }


Member Function Documentation

cvm::quaternion colvarmodule::quaternion::conjugate  )  const [inline]
 

Return the conjugate quaternion.

Definition at line 681 of file colvartypes.h.

00682   {
00683     return cvm::quaternion (q0, -q1, -q2, -q3);
00684   }

cvm::real colvarmodule::quaternion::cos2 cvm::quaternion const &  q  )  const [inline]
 

Return the square cosine between the orientation associated to this quaternion and another.

Definition at line 802 of file colvartypes.h.

References get_vector(), colvarmodule::rvector::norm2(), outer(), and rotate().

00803   {
00804     // get a vector orthogonal to both axes of rotation
00805     cvm::rvector const op = (cvm::rvector::outer (this->get_vector(), q.get_vector()));
00806     cvm::real const  opl2 = op.norm2();
00807     // rotate it with both quaternions and get the normalized inner product
00808     return ( (opl2 > 0.0) ?
00809              ((this->rotate (op)) * (q.rotate (op))) / opl2 :
00810              1.0 );
00811   }

cvm::real colvarmodule::quaternion::dist2 cvm::quaternion const &  Q2  )  const [inline]
 

Square distance from another quaternion on the 4-dimensional unit sphere: returns the square of the angle along the shorter of the two geodesics.

Definition at line 817 of file colvartypes.h.

References PI, q0, q1, q2, and q3.

Referenced by colvarvalue::dist2(), and simple_scalar_dist_functions().

00818   {
00819     cvm::real const cos_omega = this->q0*Q2.q0 + this->q1*Q2.q1 +
00820       this->q2*Q2.q2 + this->q3*Q2.q3;
00821 
00822     cvm::real const omega = ::acos ( (cos_omega > 1.0) ? 1.0 :
00823                                      ( (cos_omega < -1.0) ? -1.0 : cos_omega) );
00824 
00825     // get the minimum distance: x and -x are the same quaternion
00826     if (cos_omega > 0.0)
00827       return omega * omega;
00828     else
00829       return (PI-omega) * (PI-omega);
00830   }

cvm::quaternion colvarmodule::quaternion::dist2_grad cvm::quaternion const &  Q2  )  const [inline]
 

Gradient of the square distance: returns a 4-vector equivalent to the one provided by slerp

Definition at line 834 of file colvartypes.h.

References PI, q0, q1, q2, and q3.

Referenced by colvarvalue::dist2_grad(), colvar::orientation::dist2_lgrad(), and colvar::orientation::dist2_rgrad().

00835   {
00836     cvm::real const cos_omega = this->q0*Q2.q0 + this->q1*Q2.q1 + this->q2*Q2.q2 + this->q3*Q2.q3;
00837     cvm::real const omega = ::acos ( (cos_omega > 1.0) ? 1.0 :
00838                                      ( (cos_omega < -1.0) ? -1.0 : cos_omega) );
00839     cvm::real const sin_omega = ::sin (omega);
00840 
00841     if (::fabs (sin_omega) < 1.0E-14) {
00842       // return a null 4d vector
00843       return cvm::quaternion (0.0, 0.0, 0.0, 0.0);
00844     }
00845 
00846     cvm::quaternion const
00847       grad1 ((-1.0)*sin_omega*Q2.q0 + cos_omega*(this->q0-cos_omega*Q2.q0)/sin_omega,
00848              (-1.0)*sin_omega*Q2.q1 + cos_omega*(this->q1-cos_omega*Q2.q1)/sin_omega,
00849              (-1.0)*sin_omega*Q2.q2 + cos_omega*(this->q2-cos_omega*Q2.q2)/sin_omega,
00850              (-1.0)*sin_omega*Q2.q3 + cos_omega*(this->q3-cos_omega*Q2.q3)/sin_omega);
00851 
00852     if (cos_omega > 0.0) {
00853       return 2.0*omega*grad1;
00854     }
00855     else {
00856       return -2.0*(PI-omega)*grad1;
00857     }
00858   }

cvm::rvector colvarmodule::quaternion::get_vector  )  const [inline]
 

Return the vector component.

Definition at line 720 of file colvartypes.h.

Referenced by cos2(), and rotate().

00721   {
00722     return cvm::rvector (q1, q2, q3);
00723   }

cvm::real colvarmodule::quaternion::inner cvm::quaternion const &  Q2  )  const [inline]
 

Inner product (as a 4-d vector) with Q2; requires match() if the largest overlap is looked for.

Definition at line 871 of file colvartypes.h.

Referenced by operator *().

00872   {
00873     cvm::real const prod = this->q0*Q2.q0 + this->q1*Q2.q1 +
00874       this->q2*Q2.q2 + this->q3*Q2.q3;
00875     return prod;
00876   }

void colvarmodule::quaternion::match cvm::quaternion Q2  )  const [inline]
 

Choose the closest between Q2 and -Q2 and save it back. Not required for dist2() and dist2_grad().

Definition at line 862 of file colvartypes.h.

00863   {
00864     cvm::real const cos_omega = this->q0*Q2.q0 + this->q1*Q2.q1 +
00865       this->q2*Q2.q2 + this->q3*Q2.q3;
00866     if (cos_omega < 0.0) Q2 *= -1.0;
00867   }

cvm::real colvarmodule::quaternion::norm  )  const [inline]
 

Norm of the quaternion.

Definition at line 675 of file colvartypes.h.

00676   {
00677     return ::sqrt (this->norm2());
00678   }

cvm::real colvarmodule::quaternion::norm2  )  const [inline]
 

Square norm of the quaternion.

Definition at line 669 of file colvartypes.h.

Referenced by colvarvalue::apply_constraints(), and colvarvalue::norm2().

00670   {
00671     return q0*q0 + q1*q1 + q2*q2 + q3*q3;
00672   }

void colvarmodule::quaternion::operator *= cvm::real const &  a  )  [inline]
 

Definition at line 686 of file colvartypes.h.

00687   {
00688     q0 *= a; q1 *= a; q2 *= a; q3 *= a;
00689   }

void colvarmodule::quaternion::operator+= cvm::quaternion const &  h  )  [inline]
 

Definition at line 705 of file colvartypes.h.

References q0, q1, q2, and q3.

00706   {
00707     q0+=h.q0; q1+=h.q1; q2+=h.q2; q3+=h.q3;
00708   }

void colvarmodule::quaternion::operator-= cvm::quaternion const &  h  )  [inline]
 

Definition at line 709 of file colvartypes.h.

References q0, q1, q2, and q3.

00710   {
00711     q0-=h.q0; q1-=h.q1; q2-=h.q2; q3-=h.q3;
00712   }

void colvarmodule::quaternion::operator/= cvm::real const &  a  )  [inline]
 

Definition at line 691 of file colvartypes.h.

00692   {
00693     q0 /= a; q1 /= a; q2 /= a; q3 /= a;
00694   }

cvm::real colvarmodule::quaternion::operator[] int const &  i  )  const [inline]
 

Access the quaternion as a 4-d array (return a value).

Definition at line 651 of file colvartypes.h.

References colvarmodule::fatal_error().

00651                                                   {
00652     switch (i) {
00653     case 0:
00654       return this->q0;
00655     case 1:
00656       return this->q1;
00657     case 2:
00658       return this->q2;
00659     case 3:
00660       return this->q3;
00661     default:
00662       cvm::fatal_error ("Error: trying to access a quaternion "
00663                         "component which is not between 0 and 3.\n");
00664       return this->q0;
00665     }
00666   }

cvm::real& colvarmodule::quaternion::operator[] int const &  i  )  [inline]
 

Access the quaternion as a 4-d array (return a reference).

Definition at line 634 of file colvartypes.h.

References colvarmodule::fatal_error().

00634                                               {
00635     switch (i) {
00636     case 0:
00637       return this->q0;
00638     case 1:
00639       return this->q1;
00640     case 2:
00641       return this->q2;
00642     case 3:
00643       return this->q3;
00644     default:
00645       cvm::fatal_error ("Error: incorrect quaternion component.\n");
00646       return q0;
00647     }
00648   }

size_t colvarmodule::quaternion::output_width size_t const &  real_width  )  [inline, static]
 

Tell the number of characters required to print a quaternion, given that of a real number.

Definition at line 623 of file colvartypes.h.

Referenced by colvarvalue::output_width().

00624   {
00625     return 4*real_width + 13;
00626   }

cvm::quaternion colvarmodule::quaternion::promote cvm::rvector const &  v  )  [inline, static]
 

Promote a 3-vector to a quaternion.

Definition at line 715 of file colvartypes.h.

References colvarmodule::rvector::x, colvarmodule::rvector::y, and colvarmodule::rvector::z.

00716   {
00717     return cvm::quaternion (0.0, v.x, v.y, v.z);
00718   }

void colvarmodule::quaternion::reset  )  [inline]
 

Set all components to zero (null quaternion).

Definition at line 609 of file colvartypes.h.

00610   {
00611     q0 = q1 = q2 = q3 = 0.0;
00612   }

void colvarmodule::quaternion::reset_rotation  )  [inline]
 

Set the q0 component to 1 and the others to 0 (quaternion representing no rotation).

Definition at line 616 of file colvartypes.h.

00617   {
00618     q0 = 1.0;
00619     q1 = q2 = q3 = 0.0;
00620   }

cvm::quaternion colvarmodule::quaternion::rotate cvm::quaternion const &  Q2  )  const [inline]
 

Rotate Q2 through this quaternion (put it in the rotated reference frame).

Definition at line 772 of file colvartypes.h.

References get_vector(), q0, colvarmodule::rvector::x, colvarmodule::rvector::y, and colvarmodule::rvector::z.

00773   {
00774     cvm::rvector const vq_rot = this->rotate (Q2.get_vector());
00775     return cvm::quaternion (Q2.q0, vq_rot.x, vq_rot.y, vq_rot.z);
00776   }

cvm::rvector colvarmodule::quaternion::rotate cvm::rvector const &  v  )  const [inline]
 

Rotate v through this quaternion (put it in the rotated reference frame).

Definition at line 765 of file colvartypes.h.

Referenced by colvar::rmsd::calc_gradients(), cos2(), and colvarmodule::rotation::rotate().

00766   {
00767     return ((*this) * promote (v) * ((*this).conjugate())).get_vector();
00768   }

cvm::rmatrix colvarmodule::quaternion::rotation_matrix  )  const [inline]
 

Return the 3x3 matrix associated to this quaternion.

Definition at line 779 of file colvartypes.h.

References colvarmodule::rmatrix::xx(), colvarmodule::rmatrix::xy(), colvarmodule::rmatrix::xz(), colvarmodule::rmatrix::yx(), colvarmodule::rmatrix::yy(), colvarmodule::rmatrix::yz(), colvarmodule::rmatrix::zx(), colvarmodule::rmatrix::zy(), and colvarmodule::rmatrix::zz().

Referenced by colvarmodule::rotation::matrix().

00780   {
00781     cvm::rmatrix R;
00782 
00783     R.xx() = q0*q0 + q1*q1 - q2*q2 - q3*q3;
00784     R.yy() = q0*q0 - q1*q1 + q2*q2 - q3*q3;
00785     R.zz() = q0*q0 - q1*q1 - q2*q2 + q3*q3;
00786 
00787     R.xy() = 2.0 * (q1*q2 - q0*q3);
00788     R.xz() = 2.0 * (q0*q2 + q1*q3);
00789 
00790     R.yx() = 2.0 * (q0*q3 + q1*q2);
00791     R.yz() = 2.0 * (q2*q3 - q0*q1);
00792 
00793     R.zx() = 2.0 * (q1*q3 - q0*q2);
00794     R.zy() = 2.0 * (q0*q1 + q2*q3);
00795 
00796     return R;
00797   }

void colvarmodule::quaternion::set cvm::real const   value = 0.0  )  [inline]
 

Set all components to a scalar.

Definition at line 603 of file colvartypes.h.

00604   {
00605     q0 = q1 = q2 = q3 = value;
00606   }

void colvarmodule::quaternion::set_from_euler_angles cvm::real const &  phi_in,
cvm::real const &  theta_in,
cvm::real const &  psi_in
[inline]
 

"Constructor" after Euler angles (in radians)

http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles

Definition at line 579 of file colvartypes.h.

00582   {
00583     q0 = ( (::cos (phi_in/2.0)) * (::cos (theta_in/2.0)) * (::cos (psi_in/2.0)) +
00584            (::sin (phi_in/2.0)) * (::sin (theta_in/2.0)) * (::sin (psi_in/2.0)) );
00585 
00586     q1 = ( (::sin (phi_in/2.0)) * (::cos (theta_in/2.0)) * (::cos (psi_in/2.0)) -
00587            (::cos (phi_in/2.0)) * (::sin (theta_in/2.0)) * (::sin (psi_in/2.0)) );
00588 
00589     q2 = ( (::cos (phi_in/2.0)) * (::sin (theta_in/2.0)) * (::cos (psi_in/2.0)) +
00590            (::sin (phi_in/2.0)) * (::cos (theta_in/2.0)) * (::sin (psi_in/2.0)) );
00591 
00592     q3 = ( (::cos (phi_in/2.0)) * (::cos (theta_in/2.0)) * (::sin (psi_in/2.0)) -
00593            (::sin (phi_in/2.0)) * (::sin (theta_in/2.0)) * (::cos (psi_in/2.0)) );
00594   }

void colvarmodule::quaternion::set_positive  )  [inline]
 

Definition at line 696 of file colvartypes.h.

00697   {
00698     if (q0 > 0.0) return;
00699     q0 = -q0;
00700     q1 = -q1;
00701     q2 = -q2;
00702     q3 = -q3;
00703   }


Friends And Related Function Documentation

cvm::quaternion operator * cvm::quaternion const &  q,
cvm::real const &  c
[friend]
 

Definition at line 751 of file colvartypes.h.

00753   {
00754     return cvm::quaternion (q.q0*c, q.q1*c, q.q2*c, q.q3*c);
00755   }

cvm::quaternion operator * cvm::real const &  c,
cvm::quaternion const &  q
[friend]
 

Definition at line 746 of file colvartypes.h.

00748   {
00749     return cvm::quaternion (c*q.q0, c*q.q1, c*q.q2, c*q.q3);
00750   }

cvm::quaternion operator * cvm::quaternion const &  h,
cvm::quaternion const &  q
[friend]
 

Provides the quaternion product. NOTE: for the inner product use:

 h.inner (q); 
.

Definition at line 738 of file colvartypes.h.

00739   {
00740     return cvm::quaternion (h.q0*q.q0 - h.q1*q.q1 - h.q2*q.q2 - h.q3*q.q3,
00741                             h.q0*q.q1 + h.q1*q.q0 + h.q2*q.q3 - h.q3*q.q2,
00742                             h.q0*q.q2 + h.q2*q.q0 + h.q3*q.q1 - h.q1*q.q3,
00743                             h.q0*q.q3 + h.q3*q.q0 + h.q1*q.q2 - h.q2*q.q1);
00744   }

cvm::quaternion operator+ cvm::quaternion const &  h,
cvm::quaternion const &  q
[friend]
 

Definition at line 726 of file colvartypes.h.

00727   {
00728     return cvm::quaternion (h.q0+q.q0, h.q1+q.q1, h.q2+q.q2, h.q3+q.q3);
00729   }

cvm::quaternion operator- cvm::quaternion const &  h,
cvm::quaternion const &  q
[friend]
 

Definition at line 731 of file colvartypes.h.

00732   {
00733     return cvm::quaternion (h.q0-q.q0, h.q1-q.q1, h.q2-q.q2, h.q3-q.q3);
00734   }

cvm::quaternion operator/ cvm::quaternion const &  q,
cvm::real const &  c
[friend]
 

Definition at line 756 of file colvartypes.h.

00758   {
00759     return cvm::quaternion (q.q0/c, q.q1/c, q.q2/c, q.q3/c);
00760   }

std::ostream& operator<< std::ostream &  os,
cvm::quaternion const &  q
[friend]
 

Formatted output operator.

std::istream& operator>> std::istream &  is,
cvm::quaternion q
[friend]
 

Formatted input operator.


Member Data Documentation

cvm::real colvarmodule::quaternion::q0
 

Definition at line 556 of file colvartypes.h.

Referenced by dist2(), dist2_grad(), colvarvalue::inverse(), operator+=(), operator-=(), operator<<(), operator>>(), and rotate().

cvm::real colvarmodule::quaternion::q1
 

Definition at line 556 of file colvartypes.h.

Referenced by dist2(), dist2_grad(), colvarvalue::inverse(), operator+=(), operator-=(), operator<<(), and operator>>().

cvm::real colvarmodule::quaternion::q2
 

Definition at line 556 of file colvartypes.h.

Referenced by dist2(), dist2_grad(), colvarvalue::inverse(), operator+=(), operator-=(), operator<<(), and operator>>().

cvm::real colvarmodule::quaternion::q3
 

Definition at line 556 of file colvartypes.h.

Referenced by dist2(), dist2_grad(), colvarvalue::inverse(), operator+=(), operator-=(), operator<<(), and operator>>().


The documentation for this class was generated from the following file:
Generated on Mon Nov 23 04:59:34 2009 for NAMD by  doxygen 1.3.9.1