FreeEnergyVector.C

Go to the documentation of this file.
00001 
00007 // written by David Hurwitz.
00008 
00009 #include <math.h>
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include "InfoStream.h"
00013 #include "FreeEnergyEnums.h"
00014 #include "FreeEnergyAssert.h"
00015 #include "Vector.h"
00016 #include "FreeEnergyVector.h"
00017 
00018 AVector::AVector(Vector_t Type) {
00019 //------------------------------------------------
00020 // init this vector to (0,0,0) for kRegular Type
00021 // init this vector to (x,y,z) for kRandom Type
00022 // where x, y, and z are in the range (0:1)
00023 //
00024 // Note:  The assumption is made that srand() has
00025 //        already been called.
00026 //------------------------------------------------
00027   if (Type == kRegular) {
00028     m_x = 0.0;
00029     m_y = 0.0;
00030     m_z = 0.0;
00031   }
00032   else {
00033     ASSERT(Type == kRandom);
00034     Random();
00035   }
00036 }
00037 
00038 
00039 AVector::AVector(double x, double y, double z) {
00040 //-------------------------------------------
00041 // init this vector to (x,y,z)
00042 //-------------------------------------------
00043   m_x = x;
00044   m_y = y;
00045   m_z = z;
00046 }
00047 
00048 
00049 AVector::AVector(const AVector& Vector) {
00050 //-------------------------------------------
00051 // init this vector to Vector
00052 //-------------------------------------------
00053   m_x = Vector.m_x;
00054   m_y = Vector.m_y;
00055   m_z = Vector.m_z;
00056 }
00057 
00058 
00059 AVector& AVector::Random() {
00060 //------------------------------------------------
00061 // replace this vector with a new random vector
00062 // where x, y, and z are in the range (0:1)
00063 //------------------------------------------------
00064   double RandMax;
00065    
00066   RandMax = RAND_MAX;
00067   m_x = (double)rand() / RandMax;
00068   m_y = (double)rand() / RandMax;
00069   m_z = (double)rand() / RandMax;
00070   ASSERT(m_x <= 1.0);
00071   ASSERT(m_y <= 1.0);
00072   ASSERT(m_z <= 1.0);
00073   return(*this);
00074 }
00075 
00076 
00077 Bool_t AVector::Set(double x, double y, double z) {
00078 //-------------------------------------------
00079 // int this vector to (x,y,z)
00080 //-------------------------------------------
00081   m_x = x;
00082   m_y = y;
00083   m_z = z;
00084   return(kTrue);
00085 }
00086 
00087 
00088 AVector AVector::operator+ (const AVector& vector) {
00089 //----------------------------------------------------------
00090 // create a new vector: the sum of this one and passed one
00091 //----------------------------------------------------------
00092   AVector Point(vector.m_x+m_x, vector.m_y+m_y, vector.m_z+m_z);
00093   return(Point);
00094 }
00095 
00096 
00097 AVector AVector::operator- (const AVector& vector) {
00098 //----------------------------------------------------------
00099 // create a new vector: this one minus the passed one
00100 //----------------------------------------------------------
00101   AVector Point(m_x-vector.m_x, m_y-vector.m_y, m_z-vector.m_z);
00102   return(Point);
00103 }
00104 
00105 
00106 AVector AVector::operator/ (double divisor) {
00107 //---------------------------------------------------
00108 // create a new vector: this one divided by divisor
00109 //---------------------------------------------------
00110   AVector Point(m_x/divisor, m_y/divisor, m_z/divisor);
00111   return(Point);
00112 }
00113 
00114 
00115 AVector& AVector::operator/= (double divisor) {
00116 //------------------------------------------------------
00117 // divide this vector by divisor and return a ref to it
00118 //------------------------------------------------------
00119   m_x /= divisor;
00120   m_y /= divisor;
00121   m_z /= divisor;
00122   return(*this);
00123 }
00124 
00125 
00126 AVector& AVector::operator*= (double scalar) {
00127 //------------------------------------------------------
00128 // multiply this vector by scalar and return a ref to it
00129 //------------------------------------------------------
00130   m_x *= scalar;
00131   m_y *= scalar;
00132   m_z *= scalar;
00133   return(*this);
00134 }
00135 
00136 
00137 AVector AVector::operator* (double scalar) {
00138 //---------------------------------------------------
00139 // create a new vector: this one divided by divisor
00140 //---------------------------------------------------
00141   AVector Point(m_x*scalar, m_y*scalar, m_z*scalar);
00142   return(Point);
00143 }
00144 
00145 
00146 void SetEqual(AVector& Vec1, const Vector& Vec2) {
00147 //------------------------------------------------------------------------
00148 // used for casting Vector -> AVector
00149 //------------------------------------------------------------------------
00150   Vec1.m_x = Vec2.x;
00151   Vec1.m_y = Vec2.y;
00152   Vec1.m_z = Vec2.z;
00153 }
00154 
00155 
00156 void SetEqual(Vector& Vec1,  const AVector& Vec2) {
00157 //------------------------------------------------------------------------
00158 // used for casting AVector -> Vector
00159 //------------------------------------------------------------------------
00160   Vec1.x = Vec2.m_x;
00161   Vec1.y = Vec2.m_y;
00162   Vec1.z = Vec2.m_z;
00163 }
00164 
00165 
00166 AVector& AVector::operator= (const AVector& vector) {
00167 //-------------------------------------------
00168 // set this vector to the passed one
00169 //-------------------------------------------
00170   m_x = vector.m_x;
00171   m_y = vector.m_y;
00172   m_z = vector.m_z;
00173   return(*this);
00174 }
00175 
00176 
00177 AVector& AVector::operator+= (const AVector& vector) {
00178 //--------------------------------------------------
00179 // set this vector to this one plus the passed one
00180 //--------------------------------------------------
00181   m_x += vector.m_x;
00182   m_y += vector.m_y;
00183   m_z += vector.m_z;
00184   return(*this);
00185 }
00186 
00187 
00188 double& AVector::operator[] (int index) {
00189 //-------------------------------------------------------------------
00190 // return one element of this vector
00191 // note: this op is used to get AND set an element
00192 //       (since it returns double&)
00193 //-------------------------------------------------------------------
00194   ASSERT( (index>=0) && (index<3) );
00195 
00196   switch(index) {
00197     case 0:  return(m_x);
00198     case 1:  return(m_y);
00199     case 2:  return(m_z);
00200     default: return(m_x);   // should never get here
00201   }
00202 }
00203 
00204 
00205 double AVector::Dist() {
00206 //-------------------------------------------------------------------
00207 // calculate distance from this point to (0, 0, 0)
00208 //-------------------------------------------------------------------
00209   return( sqrt(m_x*m_x + m_y*m_y + m_z*m_z) );
00210 }
00211 
00212 
00213 double AVector::DistSqr() {
00214 //-------------------------------------------------------------------
00215 // calculate distance-squared from this point to (0, 0, 0)
00216 //-------------------------------------------------------------------
00217   return(m_x*m_x + m_y*m_y + m_z*m_z);
00218 }
00219 
00220 
00221 double AVector::Dist(const AVector& Vector) {
00222 //-------------------------------------------------------------------
00223 // calculate distance between this point and Vector
00224 //-------------------------------------------------------------------
00225   double d1 = (m_x - Vector.m_x);
00226   double d2 = (m_y - Vector.m_y);
00227   double d3 = (m_z - Vector.m_z);
00228   return( sqrt(d1*d1 + d2*d2 + d3*d3) );
00229 }
00230 
00231 
00232 double AVector::DistSqr(const AVector& Vector) {
00233 //-------------------------------------------------------------------
00234 // calculate distance-squared between this point and Vector
00235 //-------------------------------------------------------------------
00236   double d1 = (m_x - Vector.m_x);
00237   double d2 = (m_y - Vector.m_y);
00238   double d3 = (m_z - Vector.m_z);
00239   return(d1*d1 + d2*d2 + d3*d3);
00240 }
00241 
00242 
00243 AVector AVector::cross(const AVector& Vector) {
00244 //-------------------------------------------------------------------
00245 // calculate this vector crossed with Vector (this x Vector).
00246 // see Mathematical Handbook, p.118.
00247 //-------------------------------------------------------------------
00248   AVector CrossProduct;
00249 
00250   CrossProduct.m_x = (m_y * Vector.m_z) - (m_z * Vector.m_y);
00251   CrossProduct.m_y = (m_z * Vector.m_x) - (m_x * Vector.m_z);
00252   CrossProduct.m_z = (m_x * Vector.m_y) - (m_y * Vector.m_x);
00253   return(CrossProduct);
00254 }
00255 
00256 
00257 double AVector::dot(const AVector& Vector) {
00258 //-------------------------------------------------------------------
00259 // calculate dot product of this vector and Vector
00260 // see Mathematical Handbook, p.118.
00261 //-------------------------------------------------------------------
00262   return(m_x*Vector.m_x + m_y*Vector.m_y + m_z*Vector.m_z);
00263 }
00264 
00265 
00266 void AVector::Out() {
00267 //-------------------------------------------------------------------
00268 // write it
00269 //-------------------------------------------------------------------
00270   char  Str1[20], Str2[20], Str3[20];
00271 
00272   sprintf(Str1, "%8.3f", m_x);
00273   sprintf(Str2, "%8.3f", m_y);
00274   sprintf(Str3, "%8.3f", m_z);
00275   iout << "(" << Str1 << "," << Str2 << "," << Str3 << ")";
00276 }
00277 
00278 
00279 void AVector::Output() {
00280 //-------------------------------------------------------------------
00281 // write it to standard output
00282 //-------------------------------------------------------------------
00283   char  Word1[20], Word2[20], Word3[20];
00284 
00285   if ( (fabs(m_x)<99999) && (fabs(m_y)<99999) && (fabs(m_z)<99999) ) {
00286     sprintf(Word1, "%10.3f", m_x);
00287     sprintf(Word2, "%10.3f", m_y);
00288     sprintf(Word3, "%10.3f", m_z);
00289   }
00290   else {
00291     sprintf(Word1, "%10.2e", m_x);
00292     sprintf(Word2, "%10.2e", m_y);
00293     sprintf(Word3, "%10.2e", m_z);
00294   }
00295   iout << "( " << Word1 << " " << Word2 << " " << Word3 << " )";
00296 }
00297 
00298 
00299 AVector& AVector::Scale(AVector& SmallVec, AVector& BigVec) {
00300 //-------------------------------------------------------------------
00301 // scale this vector, whose (x,y,z) are in the range (0:1),
00302 // to be in the range (SmallVec:BigVec)
00303 //-------------------------------------------------------------------
00304   m_x = SmallVec.m_x + (BigVec.m_x - SmallVec.m_x) * m_x;
00305   m_y = SmallVec.m_y + (BigVec.m_y - SmallVec.m_y) * m_y;
00306   m_z = SmallVec.m_z + (BigVec.m_z - SmallVec.m_z) * m_z;
00307   return(*this);
00308 }
00309 

Generated on Tue Sep 19 01:17:12 2017 for NAMD by  doxygen 1.4.7