Ignore:
Timestamp:
Apr 13, 2010, 1:22:42 PM (16 years ago)
Author:
Tillmann Crueger <crueger@…>
Children:
e7ea64
Parents:
0f55b2
Message:

Prepared interface of Vector Class for transition to VectorComposites

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/vector.cpp

    r0f55b2 r1f591b  
    7070 * \return \f$| x - y |^2\f$
    7171 */
    72 double Vector::DistanceSquared(const Vector * const y) const
     72double Vector::DistanceSquared(const Vector &y) const
    7373{
    7474  double res = 0.;
    7575  for (int i=NDIM;i--;)
    76     res += (x[i]-y->x[i])*(x[i]-y->x[i]);
     76    res += (x[i]-y[i])*(x[i]-y[i]);
    7777  return (res);
    7878};
     
    8282 * \return \f$| x - y |\f$
    8383 */
    84 double Vector::Distance(const Vector * const y) const
    85 {
    86   double res = 0.;
    87   for (int i=NDIM;i--;)
    88     res += (x[i]-y->x[i])*(x[i]-y->x[i]);
    89   return (sqrt(res));
     84double Vector::Distance(const Vector &y) const
     85{
     86  return (sqrt(DistanceSquared(y)));
    9087};
    9188
     
    9592 * \return \f$| x - y |\f$
    9693 */
    97 double Vector::PeriodicDistance(const Vector * const y, const double * const cell_size) const
     94double Vector::PeriodicDistance(const Vector &y, const double * const cell_size) const
    9895{
    9996  double res = Distance(y), tmp, matrix[NDIM*NDIM];
     
    119116        TranslationVector.MatrixMultiplication(matrix);
    120117        // add onto the original vector to compare with
    121         Shiftedy.CopyVector(y);
    122         Shiftedy.AddVector(&TranslationVector);
     118        Shiftedy = y + TranslationVector;
    123119        // get distance and compare with minimum so far
    124         tmp = Distance(&Shiftedy);
     120        tmp = Distance(Shiftedy);
    125121        if (tmp < res) res = tmp;
    126122      }
     
    133129 * \return \f$| x - y |^2\f$
    134130 */
    135 double Vector::PeriodicDistanceSquared(const Vector * const y, const double * const cell_size) const
     131double Vector::PeriodicDistanceSquared(const Vector &y, const double * const cell_size) const
    136132{
    137133  double res = DistanceSquared(y), tmp, matrix[NDIM*NDIM];
     
    157153        TranslationVector.MatrixMultiplication(matrix);
    158154        // add onto the original vector to compare with
    159         Shiftedy.CopyVector(y);
    160         Shiftedy.AddVector(&TranslationVector);
     155        Shiftedy = y + TranslationVector;
    161156        // get distance and compare with minimum so far
    162         tmp = DistanceSquared(&Shiftedy);
     157        tmp = DistanceSquared(Shiftedy);
    163158        if (tmp < res) res = tmp;
    164159      }
     
    180175//  Output(out);
    181176//  Log() << Verbose(0) << endl;
    182   TestVector.CopyVector(this);
     177  TestVector = (*this);
    183178  TestVector.InverseMatrixMultiplication(matrix);
    184179  for(int i=NDIM;i--;) { // correct periodically
     
    190185  }
    191186  TestVector.MatrixMultiplication(matrix);
    192   CopyVector(&TestVector);
     187  (*this) = TestVector;
    193188//  Log() << Verbose(2) << "New corrected vector is: ";
    194189//  Output(out);
     
    201196 * \return \f$\langle x, y \rangle\f$
    202197 */
    203 double Vector::ScalarProduct(const Vector * const y) const
     198double Vector::ScalarProduct(const Vector &y) const
    204199{
    205200  double res = 0.;
    206201  for (int i=NDIM;i--;)
    207     res += x[i]*y->x[i];
     202    res += x[i]*y[i];
    208203  return (res);
    209204};
     
    216211 *  \return \f$ x \times y \f&
    217212 */
    218 void Vector::VectorProduct(const Vector * const y)
     213void Vector::VectorProduct(const Vector &y)
    219214{
    220215  Vector tmp;
    221   tmp.x[0] = x[1]* (y->x[2]) - x[2]* (y->x[1]);
    222   tmp.x[1] = x[2]* (y->x[0]) - x[0]* (y->x[2]);
    223   tmp.x[2] = x[0]* (y->x[1]) - x[1]* (y->x[0]);
    224   this->CopyVector(&tmp);
     216  tmp[0] = x[1]* (y[2]) - x[2]* (y[1]);
     217  tmp[1] = x[2]* (y[0]) - x[0]* (y[2]);
     218  tmp[2] = x[0]* (y[1]) - x[1]* (y[0]);
     219  (*this) = tmp;
    225220};
    226221
     
    230225 * \return \f$\langle x, y \rangle\f$
    231226 */
    232 void Vector::ProjectOntoPlane(const Vector * const y)
    233 {
    234   Vector tmp;
    235   tmp.CopyVector(y);
     227void Vector::ProjectOntoPlane(const Vector &y)
     228{
     229  Vector tmp = y;
    236230  tmp.Normalize();
    237   tmp.Scale(ScalarProduct(&tmp));
    238   this->SubtractVector(&tmp);
     231  tmp *= ScalarProduct(tmp);
     232  (*this) -= tmp;
    239233};
    240234
     
    245239 * \return distance to plane
    246240 */
    247 double Vector::DistanceToPlane(const Vector * const PlaneNormal, const Vector * const PlaneOffset) const
    248 {
    249   Vector temp;
    250 
     241double Vector::DistanceToPlane(const Vector &PlaneNormal, const Vector &PlaneOffset) const
     242{
    251243  // first create part that is orthonormal to PlaneNormal with withdraw
    252   temp.CopyVector(this);
    253   temp.SubtractVector(PlaneOffset);
    254   temp.MakeNormalTo(*PlaneNormal);
    255   temp.Scale(-1.);
     244  Vector temp = (*this) - PlaneOffset;
     245  temp.MakeNormalTo(PlaneNormal);
     246  temp *= -1.;
    256247  // then add connecting vector from plane to point
    257   temp.AddVector(this);
    258   temp.SubtractVector(PlaneOffset);
     248  temp += (*this);
     249  temp -= PlaneOffset;
    259250  double sign = temp.ScalarProduct(PlaneNormal);
    260251  if (fabs(sign) > MYEPSILON)
     
    269260 * \param *y array to second vector
    270261 */
    271 void Vector::ProjectIt(const Vector * const y)
    272 {
    273   Vector helper(*y);
     262void Vector::ProjectIt(const Vector &y)
     263{
     264  Vector helper = y;
    274265  helper.Scale(-(ScalarProduct(y)));
    275   AddVector(&helper);
     266  AddVector(helper);
    276267};
    277268
     
    280271 * \return Vector
    281272 */
    282 Vector Vector::Projection(const Vector * const y) const
    283 {
    284   Vector helper(*y);
    285   helper.Scale((ScalarProduct(y)/y->NormSquared()));
     273Vector Vector::Projection(const Vector &y) const
     274{
     275  Vector helper = y;
     276  helper.Scale((ScalarProduct(y)/y.NormSquared()));
    286277
    287278  return helper;
     
    293284double Vector::Norm() const
    294285{
    295   double res = 0.;
    296   for (int i=NDIM;i--;)
    297     res += this->x[i]*this->x[i];
    298   return (sqrt(res));
     286  return (sqrt(NormSquared()));
    299287};
    300288
     
    304292double Vector::NormSquared() const
    305293{
    306   return (ScalarProduct(this));
     294  return (ScalarProduct(*this));
    307295};
    308296
     
    363351 * @return true - vector is normalized, false - vector is not
    364352 */
    365 bool Vector::IsNormalTo(const Vector * const normal) const
     353bool Vector::IsNormalTo(const Vector &normal) const
    366354{
    367355  if (ScalarProduct(normal) < MYEPSILON)
     
    374362 * @return true - vector is normalized, false - vector is not
    375363 */
    376 bool Vector::IsEqualTo(const Vector * const a) const
     364bool Vector::IsEqualTo(const Vector &a) const
    377365{
    378366  bool status = true;
    379367  for (int i=0;i<NDIM;i++) {
    380     if (fabs(x[i] - a->x[i]) > MYEPSILON)
     368    if (fabs(x[i] - a[i]) > MYEPSILON)
    381369      status = false;
    382370  }
     
    388376 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
    389377 */
    390 double Vector::Angle(const Vector * const y) const
    391 {
    392   double norm1 = Norm(), norm2 = y->Norm();
     378double Vector::Angle(const Vector &y) const
     379{
     380  double norm1 = Norm(), norm2 = y.Norm();
    393381  double angle = -1;
    394382  if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
     
    446434const Vector& Vector::operator+=(const Vector& b)
    447435{
    448   this->AddVector(&b);
     436  this->AddVector(b);
    449437  return *this;
    450438};
     
    457445const Vector& Vector::operator-=(const Vector& b)
    458446{
    459   this->SubtractVector(&b);
     447  this->SubtractVector(b);
    460448  return *this;
    461449};
     
    480468{
    481469  Vector x = *this;
    482   x.AddVector(&b);
     470  x.AddVector(b);
    483471  return x;
    484472};
     
    492480{
    493481  Vector x = *this;
    494   x.SubtractVector(&b);
     482  x.SubtractVector(b);
    495483  return x;
    496484};
     
    556544 * \param trans[] translation vector.
    557545 */
    558 void Vector::Translate(const Vector * const trans)
    559 {
    560   for (int i=NDIM;i--;)
    561     x[i] += trans->x[i];
     546void Vector::Translate(const Vector &trans)
     547{
     548  for (int i=NDIM;i--;)
     549    x[i] += trans[i];
    562550};
    563551
     
    639627 * \param *factors three-component vector with the factor for each given vector
    640628 */
    641 void Vector::LinearCombinationOfVectors(const Vector * const x1, const Vector * const x2, const Vector * const x3, const double * const factors)
    642 {
    643   for(int i=NDIM;i--;)
    644     x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i];
     629void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors)
     630{
     631  (*this) = (factors[0]*x1) +
     632            (factors[1]*x2) +
     633            (factors[2]*x3);
    645634};
    646635
     
    648637 * \param n[] normal vector of mirror plane.
    649638 */
    650 void Vector::Mirror(const Vector * const n)
     639void Vector::Mirror(const Vector &n)
    651640{
    652641  double projection;
    653   projection = ScalarProduct(n)/n->ScalarProduct(n);    // remove constancy from n (keep as logical one)
     642  projection = ScalarProduct(n)/n.NormSquared();    // remove constancy from n (keep as logical one)
    654643  // withdraw projected vector twice from original one
    655644  for (int i=NDIM;i--;)
    656     x[i] -= 2.*projection*n->x[i];
     645    x[i] -= 2.*projection*n[i];
    657646};
    658647
     
    667656{
    668657  bool result = false;
    669   double factor = y1.ScalarProduct(this)/y1.NormSquared();
    670   Vector x1;
    671   x1.CopyVector(&y1);
    672   x1.Scale(factor);
    673   SubtractVector(&x1);
     658  double factor = y1.ScalarProduct(*this)/y1.NormSquared();
     659  Vector x1 = factor * y1 ;
     660  SubtractVector(x1);
    674661  for (int i=NDIM;i--;)
    675662    result = result || (fabs(x[i]) > MYEPSILON);
     
    684671 * \return true - success, false - failure (null vector given)
    685672 */
    686 bool Vector::GetOneNormalVector(const Vector * const GivenVector)
     673bool Vector::GetOneNormalVector(const Vector &GivenVector)
    687674{
    688675  int Components[NDIM]; // contains indices of non-zero components
     
    695682  // find two components != 0
    696683  for (j=0;j<NDIM;j++)
    697     if (fabs(GivenVector->x[j]) > MYEPSILON)
     684    if (fabs(GivenVector[j]) > MYEPSILON)
    698685      Components[Last++] = j;
    699686
     
    701688    case 3:  // threecomponent system
    702689    case 2:  // two component system
    703       norm = sqrt(1./(GivenVector->x[Components[1]]*GivenVector->x[Components[1]]) + 1./(GivenVector->x[Components[0]]*GivenVector->x[Components[0]]));
     690      norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]]));
    704691      x[Components[2]] = 0.;
    705692      // in skp both remaining parts shall become zero but with opposite sign and third is zero
    706       x[Components[1]] = -1./GivenVector->x[Components[1]] / norm;
    707       x[Components[0]] = 1./GivenVector->x[Components[0]] / norm;
     693      x[Components[1]] = -1./GivenVector[Components[1]] / norm;
     694      x[Components[0]] = 1./GivenVector[Components[0]] / norm;
    708695      return true;
    709696      break;
     
    720707};
    721708
    722 /** Determines parameter needed to multiply this vector to obtain intersection point with plane defined by \a *A, \a *B and \a *C.
    723  * \param *A first plane vector
    724  * \param *B second plane vector
    725  * \param *C third plane vector
    726  * \return scaling parameter for this vector
    727  */
    728 double Vector::CutsPlaneAt(const Vector * const A, const Vector * const B, const Vector * const C) const
    729 {
    730 //  Log() << Verbose(3) << "For comparison: ";
    731 //  Log() << Verbose(0) << "A " << A->Projection(this) << "\t";
    732 //  Log() << Verbose(0) << "B " << B->Projection(this) << "\t";
    733 //  Log() << Verbose(0) << "C " << C->Projection(this) << "\t";
    734 //  Log() << Verbose(0) << endl;
    735   return A->ScalarProduct(this);
    736 };
    737 
    738 
    739709/** Adds vector \a *y componentwise.
    740710 * \param *y vector
    741711 */
    742 void Vector::AddVector(const Vector * const y)
    743 {
    744   for (int i=NDIM;i--;)
    745     this->x[i] += y->x[i];
     712void Vector::AddVector(const Vector &y)
     713{
     714  for (int i=NDIM;i--;)
     715    this->x[i] += y[i];
    746716}
    747717
     
    749719 * \param *y vector
    750720 */
    751 void Vector::SubtractVector(const Vector * const y)
    752 {
    753   for (int i=NDIM;i--;)
    754     this->x[i] -= y->x[i];
    755 }
    756 
    757 /** Copy vector \a *y componentwise.
    758  * \param *y vector
    759  */
    760 void Vector::CopyVector(const Vector * const y)
    761 {
    762   // check for self assignment
    763   if(y!=this){
    764     for (int i=NDIM;i--;)
    765       this->x[i] = y->x[i];
    766   }
     721void Vector::SubtractVector(const Vector &y)
     722{
     723  for (int i=NDIM;i--;)
     724    this->x[i] -= y[i];
    767725}
    768726
     
    964922bool Vector::IsInParallelepiped(const Vector &offset, const double * const parallelepiped) const
    965923{
    966   Vector a;
    967   a.CopyVector(this);
    968   a.SubtractVector(&offset);
     924  Vector a = (*this) - offset;
    969925  a.InverseMatrixMultiplication(parallelepiped);
    970926  bool isInside = true;
Note: See TracChangeset for help on using the changeset viewer.