Changeset 1f591b for molecuilder/src/vector.cpp
- Timestamp:
- Apr 13, 2010, 1:22:42 PM (16 years ago)
- Children:
- e7ea64
- Parents:
- 0f55b2
- File:
-
- 1 edited
-
molecuilder/src/vector.cpp (modified) (33 diffs)
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/vector.cpp
r0f55b2 r1f591b 70 70 * \return \f$| x - y |^2\f$ 71 71 */ 72 double Vector::DistanceSquared(const Vector * consty) const72 double Vector::DistanceSquared(const Vector &y) const 73 73 { 74 74 double res = 0.; 75 75 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]); 77 77 return (res); 78 78 }; … … 82 82 * \return \f$| x - y |\f$ 83 83 */ 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)); 84 double Vector::Distance(const Vector &y) const 85 { 86 return (sqrt(DistanceSquared(y))); 90 87 }; 91 88 … … 95 92 * \return \f$| x - y |\f$ 96 93 */ 97 double Vector::PeriodicDistance(const Vector * consty, const double * const cell_size) const94 double Vector::PeriodicDistance(const Vector &y, const double * const cell_size) const 98 95 { 99 96 double res = Distance(y), tmp, matrix[NDIM*NDIM]; … … 119 116 TranslationVector.MatrixMultiplication(matrix); 120 117 // add onto the original vector to compare with 121 Shiftedy.CopyVector(y); 122 Shiftedy.AddVector(&TranslationVector); 118 Shiftedy = y + TranslationVector; 123 119 // get distance and compare with minimum so far 124 tmp = Distance( &Shiftedy);120 tmp = Distance(Shiftedy); 125 121 if (tmp < res) res = tmp; 126 122 } … … 133 129 * \return \f$| x - y |^2\f$ 134 130 */ 135 double Vector::PeriodicDistanceSquared(const Vector * consty, const double * const cell_size) const131 double Vector::PeriodicDistanceSquared(const Vector &y, const double * const cell_size) const 136 132 { 137 133 double res = DistanceSquared(y), tmp, matrix[NDIM*NDIM]; … … 157 153 TranslationVector.MatrixMultiplication(matrix); 158 154 // add onto the original vector to compare with 159 Shiftedy.CopyVector(y); 160 Shiftedy.AddVector(&TranslationVector); 155 Shiftedy = y + TranslationVector; 161 156 // get distance and compare with minimum so far 162 tmp = DistanceSquared( &Shiftedy);157 tmp = DistanceSquared(Shiftedy); 163 158 if (tmp < res) res = tmp; 164 159 } … … 180 175 // Output(out); 181 176 // Log() << Verbose(0) << endl; 182 TestVector .CopyVector(this);177 TestVector = (*this); 183 178 TestVector.InverseMatrixMultiplication(matrix); 184 179 for(int i=NDIM;i--;) { // correct periodically … … 190 185 } 191 186 TestVector.MatrixMultiplication(matrix); 192 CopyVector(&TestVector);187 (*this) = TestVector; 193 188 // Log() << Verbose(2) << "New corrected vector is: "; 194 189 // Output(out); … … 201 196 * \return \f$\langle x, y \rangle\f$ 202 197 */ 203 double Vector::ScalarProduct(const Vector * consty) const198 double Vector::ScalarProduct(const Vector &y) const 204 199 { 205 200 double res = 0.; 206 201 for (int i=NDIM;i--;) 207 res += x[i]*y ->x[i];202 res += x[i]*y[i]; 208 203 return (res); 209 204 }; … … 216 211 * \return \f$ x \times y \f& 217 212 */ 218 void Vector::VectorProduct(const Vector * consty)213 void Vector::VectorProduct(const Vector &y) 219 214 { 220 215 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; 225 220 }; 226 221 … … 230 225 * \return \f$\langle x, y \rangle\f$ 231 226 */ 232 void Vector::ProjectOntoPlane(const Vector * const y) 233 { 234 Vector tmp; 235 tmp.CopyVector(y); 227 void Vector::ProjectOntoPlane(const Vector &y) 228 { 229 Vector tmp = y; 236 230 tmp.Normalize(); 237 tmp .Scale(ScalarProduct(&tmp));238 this->SubtractVector(&tmp);231 tmp *= ScalarProduct(tmp); 232 (*this) -= tmp; 239 233 }; 240 234 … … 245 239 * \return distance to plane 246 240 */ 247 double Vector::DistanceToPlane(const Vector * const PlaneNormal, const Vector * const PlaneOffset) const 248 { 249 Vector temp; 250 241 double Vector::DistanceToPlane(const Vector &PlaneNormal, const Vector &PlaneOffset) const 242 { 251 243 // 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.; 256 247 // then add connecting vector from plane to point 257 temp .AddVector(this);258 temp .SubtractVector(PlaneOffset);248 temp += (*this); 249 temp -= PlaneOffset; 259 250 double sign = temp.ScalarProduct(PlaneNormal); 260 251 if (fabs(sign) > MYEPSILON) … … 269 260 * \param *y array to second vector 270 261 */ 271 void Vector::ProjectIt(const Vector * consty)272 { 273 Vector helper (*y);262 void Vector::ProjectIt(const Vector &y) 263 { 264 Vector helper = y; 274 265 helper.Scale(-(ScalarProduct(y))); 275 AddVector( &helper);266 AddVector(helper); 276 267 }; 277 268 … … 280 271 * \return Vector 281 272 */ 282 Vector Vector::Projection(const Vector * consty) const283 { 284 Vector helper (*y);285 helper.Scale((ScalarProduct(y)/y ->NormSquared()));273 Vector Vector::Projection(const Vector &y) const 274 { 275 Vector helper = y; 276 helper.Scale((ScalarProduct(y)/y.NormSquared())); 286 277 287 278 return helper; … … 293 284 double Vector::Norm() const 294 285 { 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())); 299 287 }; 300 288 … … 304 292 double Vector::NormSquared() const 305 293 { 306 return (ScalarProduct( this));294 return (ScalarProduct(*this)); 307 295 }; 308 296 … … 363 351 * @return true - vector is normalized, false - vector is not 364 352 */ 365 bool Vector::IsNormalTo(const Vector * constnormal) const353 bool Vector::IsNormalTo(const Vector &normal) const 366 354 { 367 355 if (ScalarProduct(normal) < MYEPSILON) … … 374 362 * @return true - vector is normalized, false - vector is not 375 363 */ 376 bool Vector::IsEqualTo(const Vector * consta) const364 bool Vector::IsEqualTo(const Vector &a) const 377 365 { 378 366 bool status = true; 379 367 for (int i=0;i<NDIM;i++) { 380 if (fabs(x[i] - a ->x[i]) > MYEPSILON)368 if (fabs(x[i] - a[i]) > MYEPSILON) 381 369 status = false; 382 370 } … … 388 376 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$ 389 377 */ 390 double Vector::Angle(const Vector * consty) const391 { 392 double norm1 = Norm(), norm2 = y ->Norm();378 double Vector::Angle(const Vector &y) const 379 { 380 double norm1 = Norm(), norm2 = y.Norm(); 393 381 double angle = -1; 394 382 if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON)) … … 446 434 const Vector& Vector::operator+=(const Vector& b) 447 435 { 448 this->AddVector( &b);436 this->AddVector(b); 449 437 return *this; 450 438 }; … … 457 445 const Vector& Vector::operator-=(const Vector& b) 458 446 { 459 this->SubtractVector( &b);447 this->SubtractVector(b); 460 448 return *this; 461 449 }; … … 480 468 { 481 469 Vector x = *this; 482 x.AddVector( &b);470 x.AddVector(b); 483 471 return x; 484 472 }; … … 492 480 { 493 481 Vector x = *this; 494 x.SubtractVector( &b);482 x.SubtractVector(b); 495 483 return x; 496 484 }; … … 556 544 * \param trans[] translation vector. 557 545 */ 558 void Vector::Translate(const Vector * consttrans)559 { 560 for (int i=NDIM;i--;) 561 x[i] += trans ->x[i];546 void Vector::Translate(const Vector &trans) 547 { 548 for (int i=NDIM;i--;) 549 x[i] += trans[i]; 562 550 }; 563 551 … … 639 627 * \param *factors three-component vector with the factor for each given vector 640 628 */ 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]; 629 void 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); 645 634 }; 646 635 … … 648 637 * \param n[] normal vector of mirror plane. 649 638 */ 650 void Vector::Mirror(const Vector * constn)639 void Vector::Mirror(const Vector &n) 651 640 { 652 641 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) 654 643 // withdraw projected vector twice from original one 655 644 for (int i=NDIM;i--;) 656 x[i] -= 2.*projection*n ->x[i];645 x[i] -= 2.*projection*n[i]; 657 646 }; 658 647 … … 667 656 { 668 657 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); 674 661 for (int i=NDIM;i--;) 675 662 result = result || (fabs(x[i]) > MYEPSILON); … … 684 671 * \return true - success, false - failure (null vector given) 685 672 */ 686 bool Vector::GetOneNormalVector(const Vector * constGivenVector)673 bool Vector::GetOneNormalVector(const Vector &GivenVector) 687 674 { 688 675 int Components[NDIM]; // contains indices of non-zero components … … 695 682 // find two components != 0 696 683 for (j=0;j<NDIM;j++) 697 if (fabs(GivenVector ->x[j]) > MYEPSILON)684 if (fabs(GivenVector[j]) > MYEPSILON) 698 685 Components[Last++] = j; 699 686 … … 701 688 case 3: // threecomponent system 702 689 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]])); 704 691 x[Components[2]] = 0.; 705 692 // 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; 708 695 return true; 709 696 break; … … 720 707 }; 721 708 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 vector724 * \param *B second plane vector725 * \param *C third plane vector726 * \return scaling parameter for this vector727 */728 double Vector::CutsPlaneAt(const Vector * const A, const Vector * const B, const Vector * const C) const729 {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 739 709 /** Adds vector \a *y componentwise. 740 710 * \param *y vector 741 711 */ 742 void Vector::AddVector(const Vector * consty)743 { 744 for (int i=NDIM;i--;) 745 this->x[i] += y ->x[i];712 void Vector::AddVector(const Vector &y) 713 { 714 for (int i=NDIM;i--;) 715 this->x[i] += y[i]; 746 716 } 747 717 … … 749 719 * \param *y vector 750 720 */ 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 } 721 void Vector::SubtractVector(const Vector &y) 722 { 723 for (int i=NDIM;i--;) 724 this->x[i] -= y[i]; 767 725 } 768 726 … … 964 922 bool Vector::IsInParallelepiped(const Vector &offset, const double * const parallelepiped) const 965 923 { 966 Vector a; 967 a.CopyVector(this); 968 a.SubtractVector(&offset); 924 Vector a = (*this) - offset; 969 925 a.InverseMatrixMultiplication(parallelepiped); 970 926 bool isInside = true;
Note:
See TracChangeset
for help on using the changeset viewer.
