Changes in src/vector.cpp [b84d5d:a67d19]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/vector.cpp
rb84d5d ra67d19 15 15 #include "vector.hpp" 16 16 #include "verbose.hpp" 17 #include "World.hpp" 17 18 18 19 #include <gsl/gsl_linalg.h> … … 26 27 */ 27 28 Vector::Vector() { x[0] = x[1] = x[2] = 0.; }; 29 30 /** Constructor of class vector. 31 */ 32 Vector::Vector(const Vector * const a) 33 { 34 x[0] = a->x[0]; 35 x[1] = a->x[1]; 36 x[2] = a->x[2]; 37 }; 38 39 /** Constructor of class vector. 40 */ 41 Vector::Vector(const Vector &a) 42 { 43 x[0] = a.x[0]; 44 x[1] = a.x[1]; 45 x[2] = a.x[2]; 46 }; 28 47 29 48 /** Constructor of class vector. … … 234 253 Direction.SubtractVector(Origin); 235 254 Direction.Normalize(); 236 Log() << Verbose(1) << "INFO: Direction is " << Direction << "." << endl;255 DoLog(1) && (Log() << Verbose(1) << "INFO: Direction is " << Direction << "." << endl); 237 256 //Log() << Verbose(1) << "INFO: PlaneNormal is " << *PlaneNormal << " and PlaneOffset is " << *PlaneOffset << "." << endl; 238 257 factor = Direction.ScalarProduct(PlaneNormal); 239 258 if (fabs(factor) < MYEPSILON) { // Uniqueness: line parallel to plane? 240 Log() << Verbose(1) << "BAD: Line is parallel to plane, no intersection." << endl;259 DoLog(1) && (Log() << Verbose(1) << "BAD: Line is parallel to plane, no intersection." << endl); 241 260 return false; 242 261 } … … 245 264 factor = helper.ScalarProduct(PlaneNormal)/factor; 246 265 if (fabs(factor) < MYEPSILON) { // Origin is in-plane 247 Log() << Verbose(1) << "GOOD: Origin of line is in-plane." << endl;266 DoLog(1) && (Log() << Verbose(1) << "GOOD: Origin of line is in-plane." << endl); 248 267 CopyVector(Origin); 249 268 return true; … … 252 271 Direction.Scale(factor); 253 272 CopyVector(Origin); 254 Log() << Verbose(1) << "INFO: Scaled direction is " << Direction << "." << endl;273 DoLog(1) && (Log() << Verbose(1) << "INFO: Scaled direction is " << Direction << "." << endl); 255 274 AddVector(&Direction); 256 275 … … 259 278 helper.SubtractVector(PlaneOffset); 260 279 if (helper.ScalarProduct(PlaneNormal) < MYEPSILON) { 261 Log() << Verbose(1) << "GOOD: Intersection is " << *this << "." << endl;280 DoLog(1) && (Log() << Verbose(1) << "GOOD: Intersection is " << *this << "." << endl); 262 281 return true; 263 282 } else { 264 eLog() << Verbose(2) << "Intersection point " << *this << " is not on plane." << endl;283 DoeLog(2) && (eLog()<< Verbose(2) << "Intersection point " << *this << " is not on plane." << endl); 265 284 return false; 266 285 } 267 286 }; 268 287 269 /** Calculates the minimum distance of this vector to the plane.288 /** Calculates the minimum distance vector of this vector to the plane. 270 289 * \param *out output stream for debugging 271 290 * \param *PlaneNormal normal of plane 272 291 * \param *PlaneOffset offset of plane 273 * \return distance to plane274 */ 275 double Vector::DistanceToPlane(const Vector * const PlaneNormal, const Vector * const PlaneOffset) const292 * \return distance vector onto to plane 293 */ 294 Vector Vector::GetDistanceVectorToPlane(const Vector * const PlaneNormal, const Vector * const PlaneOffset) const 276 295 { 277 296 Vector temp; … … 291 310 sign = 0.; 292 311 293 return (temp.Norm()*sign); 312 temp.Normalize(); 313 temp.Scale(sign); 314 return temp; 315 }; 316 317 /** Calculates the minimum distance of this vector to the plane. 318 * \sa Vector::GetDistanceVectorToPlane() 319 * \param *out output stream for debugging 320 * \param *PlaneNormal normal of plane 321 * \param *PlaneOffset offset of plane 322 * \return distance to plane 323 */ 324 double Vector::DistanceToPlane(const Vector * const PlaneNormal, const Vector * const PlaneOffset) const 325 { 326 return GetDistanceVectorToPlane(PlaneNormal,PlaneOffset).Norm(); 294 327 }; 295 328 … … 319 352 320 353 //Log() << Verbose(1) << "Coefficent matrix is:" << endl; 354 //ostream &output = Log() << Verbose(1); 321 355 //for (int i=0;i<4;i++) { 322 356 // for (int j=0;j<4;j++) 323 // cout << "\t" << M->Get(i,j);324 // cout << endl;357 // output << "\t" << M->Get(i,j); 358 // output << endl; 325 359 //} 326 360 if (fabs(M->Determinant()) > MYEPSILON) { 327 Log() << Verbose(1) << "Determinant of coefficient matrix is NOT zero." << endl;361 DoLog(1) && (Log() << Verbose(1) << "Determinant of coefficient matrix is NOT zero." << endl); 328 362 return false; 329 363 } 330 delete(M); 331 Log() << Verbose(1) << "INFO: Line1a = " << *Line1a << ", Line1b = " << *Line1b << ", Line2a = " << *Line2a << ", Line2b = " << *Line2b << "." << endl; 364 DoLog(1) && (Log() << Verbose(1) << "INFO: Line1a = " << *Line1a << ", Line1b = " << *Line1b << ", Line2a = " << *Line2a << ", Line2b = " << *Line2b << "." << endl); 332 365 333 366 … … 345 378 d.CopyVector(Line2b); 346 379 d.SubtractVector(Line1b); 347 Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl;380 DoLog(1) && (Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl); 348 381 if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) { 349 382 Zero(); 350 Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl;383 DoLog(1) && (Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl); 351 384 return false; 352 385 } … … 361 394 if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) { 362 395 CopyVector(Line2a); 363 Log() << Verbose(1) << "Lines conincide." << endl;396 DoLog(1) && (Log() << Verbose(1) << "Lines conincide." << endl); 364 397 return true; 365 398 } else { … … 369 402 if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) { 370 403 CopyVector(Line2b); 371 Log() << Verbose(1) << "Lines conincide." << endl;404 DoLog(1) && (Log() << Verbose(1) << "Lines conincide." << endl); 372 405 return true; 373 406 } 374 407 } 375 Log() << Verbose(1) << "Lines are parallel." << endl;408 DoLog(1) && (Log() << Verbose(1) << "Lines are parallel." << endl); 376 409 Zero(); 377 410 return false; … … 385 418 temp2.CopyVector(&a); 386 419 temp2.VectorProduct(&b); 387 Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl;420 DoLog(1) && (Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl); 388 421 if (fabs(temp2.NormSquared()) > MYEPSILON) 389 422 s = temp1.ScalarProduct(&temp2)/temp2.NormSquared(); 390 423 else 391 424 s = 0.; 392 Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(&temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl;425 DoLog(1) && (Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(&temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl); 393 426 394 427 // construct intersection … … 396 429 Scale(s); 397 430 AddVector(Line1a); 398 Log() << Verbose(1) << "Intersection is at " << *this << "." << endl;431 DoLog(1) && (Log() << Verbose(1) << "Intersection is at " << *this << "." << endl); 399 432 400 433 return true; … … 584 617 * \return lhs + a 585 618 */ 586 constVector& operator+=(Vector& a, const Vector& b)619 Vector& operator+=(Vector& a, const Vector& b) 587 620 { 588 621 a.AddVector(&b); … … 595 628 * \return lhs - a 596 629 */ 597 constVector& operator-=(Vector& a, const Vector& b)630 Vector& operator-=(Vector& a, const Vector& b) 598 631 { 599 632 a.SubtractVector(&b); … … 606 639 * \return lhs.x[i] * m 607 640 */ 608 constVector& operator*=(Vector& a, const double m)641 Vector& operator*=(Vector& a, const double m) 609 642 { 610 643 a.Scale(m); … … 617 650 * \return a + b 618 651 */ 619 Vector const operator+(const Vector& a, const Vector& b) 620 { 621 Vector x(a); 622 x.AddVector(&b); 623 return x; 652 Vector& operator+(const Vector& a, const Vector& b) 653 { 654 Vector *x = new Vector; 655 x->CopyVector(&a); 656 x->AddVector(&b); 657 return *x; 624 658 }; 625 659 … … 629 663 * \return a - b 630 664 */ 631 Vector const operator-(const Vector& a, const Vector& b) 632 { 633 Vector x(a); 634 x.SubtractVector(&b); 635 return x; 665 Vector& operator-(const Vector& a, const Vector& b) 666 { 667 Vector *x = new Vector; 668 x->CopyVector(&a); 669 x->SubtractVector(&b); 670 return *x; 636 671 }; 637 672 … … 641 676 * \return m * a 642 677 */ 643 Vector const operator*(const Vector& a, const double m) 644 { 645 Vector x(a); 646 x.Scale(m); 647 return x; 678 Vector& operator*(const Vector& a, const double m) 679 { 680 Vector *x = new Vector; 681 x->CopyVector(&a); 682 x->Scale(m); 683 return *x; 648 684 }; 649 685 … … 653 689 * \return m * a 654 690 */ 655 Vector const operator*(const double m, const Vector& a ) 656 { 657 Vector x(a); 658 x.Scale(m); 659 return x; 691 Vector& operator*(const double m, const Vector& a ) 692 { 693 Vector *x = new Vector; 694 x->CopyVector(&a); 695 x->Scale(m); 696 return *x; 660 697 }; 661 698 … … 665 702 void Vector::Output() const 666 703 { 667 Log() << Verbose(0) << "(";704 DoLog(0) && (Log() << Verbose(0) << "("); 668 705 for (int i=0;i<NDIM;i++) { 669 Log() << Verbose(0) << x[i];706 DoLog(0) && (Log() << Verbose(0) << x[i]); 670 707 if (i != 2) 671 Log() << Verbose(0) << ",";672 } 673 Log() << Verbose(0) << ")";708 DoLog(0) && (Log() << Verbose(0) << ","); 709 } 710 DoLog(0) && (Log() << Verbose(0) << ")"); 674 711 }; 675 712 … … 780 817 x[i] = C.x[i]; 781 818 } else { 782 eLog() << Verbose(1) << "inverse of matrix does not exists: det A = " << detA << "." << endl;819 DoeLog(1) && (eLog()<< Verbose(1) << "inverse of matrix does not exists: det A = " << detA << "." << endl); 783 820 } 784 821 }; … … 806 843 projection = ScalarProduct(n)/n->ScalarProduct(n); // remove constancy from n (keep as logical one) 807 844 // withdraw projected vector twice from original one 808 Log() << Verbose(1) << "Vector: ";845 DoLog(1) && (Log() << Verbose(1) << "Vector: "); 809 846 Output(); 810 Log() << Verbose(0) << "\t";847 DoLog(0) && (Log() << Verbose(0) << "\t"); 811 848 for (int i=NDIM;i--;) 812 849 x[i] -= 2.*projection*n->x[i]; 813 Log() << Verbose(0) << "Projected vector: ";850 DoLog(0) && (Log() << Verbose(0) << "Projected vector: "); 814 851 Output(); 815 Log() << Verbose(0) << endl;852 DoLog(0) && (Log() << Verbose(0) << endl); 816 853 }; 817 854 … … 832 869 x2.SubtractVector(y2); 833 870 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) { 834 eLog() << Verbose(2) << "Given vectors are linear dependent." << endl;871 DoeLog(2) && (eLog()<< Verbose(2) << "Given vectors are linear dependent." << endl); 835 872 return false; 836 873 } … … 866 903 Zero(); 867 904 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) { 868 eLog() << Verbose(2) << "Given vectors are linear dependent." << endl;905 DoeLog(2) && (eLog()<< Verbose(2) << "Given vectors are linear dependent." << endl); 869 906 return false; 870 907 } … … 917 954 double norm; 918 955 919 Log() << Verbose(4);956 DoLog(4) && (Log() << Verbose(4)); 920 957 GivenVector->Output(); 921 Log() << Verbose(0) << endl;958 DoLog(0) && (Log() << Verbose(0) << endl); 922 959 for (j=NDIM;j--;) 923 960 Components[j] = -1; … … 926 963 if (fabs(GivenVector->x[j]) > MYEPSILON) 927 964 Components[Last++] = j; 928 Log() << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl;965 DoLog(4) && (Log() << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl); 929 966 930 967 switch(Last) { … … 976 1013 977 1014 for (j=0;j<num;j++) { 978 Log() << Verbose(1) << j << "th atom's vector: ";1015 DoLog(1) && (Log() << Verbose(1) << j << "th atom's vector: "); 979 1016 (vectors[j])->Output(); 980 Log() << Verbose(0) << endl;1017 DoLog(0) && (Log() << Verbose(0) << endl); 981 1018 } 982 1019 … … 1073 1110 void Vector::CopyVector(const Vector * const y) 1074 1111 { 1075 // check for self assignment 1076 if(y!=this){ 1077 for (int i=NDIM;i--;) 1078 this->x[i] = y->x[i]; 1079 } 1112 for (int i=NDIM;i--;) 1113 this->x[i] = y->x[i]; 1080 1114 } 1081 1115 … … 1085 1119 void Vector::CopyVector(const Vector &y) 1086 1120 { 1087 // check for self assignment 1088 if(&y!=this) { 1089 for (int i=NDIM;i--;) 1090 this->x[i] = y.x[i]; 1091 } 1121 for (int i=NDIM;i--;) 1122 this->x[i] = y.x[i]; 1092 1123 } 1093 1124 … … 1104 1135 j += i+1; 1105 1136 do { 1106 Log() << Verbose(0) << coords[i] << "[0.." << cell_size[j] << "]: ";1137 DoLog(0) && (Log() << Verbose(0) << coords[i] << "[0.." << cell_size[j] << "]: "); 1107 1138 cin >> x[i]; 1108 1139 } while (((x[i] < 0) || (x[i] >= cell_size[j])) && (check)); … … 1135 1166 B2 = cos(beta) * x2->Norm() * c; 1136 1167 C = c * c; 1137 Log() << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl;1168 DoLog(2) && (Log() << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl); 1138 1169 int flag = 0; 1139 1170 if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping … … 1174 1205 D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2]; 1175 1206 D3 = y->x[0]/x1->x[0]*A-B1; 1176 Log() << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n";1207 DoLog(2) && (Log() << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n"); 1177 1208 if (fabs(D1) < MYEPSILON) { 1178 Log() << Verbose(2) << "D1 == 0!\n";1209 DoLog(2) && (Log() << Verbose(2) << "D1 == 0!\n"); 1179 1210 if (fabs(D2) > MYEPSILON) { 1180 Log() << Verbose(3) << "D2 != 0!\n";1211 DoLog(3) && (Log() << Verbose(3) << "D2 != 0!\n"); 1181 1212 x[2] = -D3/D2; 1182 1213 E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2; 1183 1214 E2 = -x1->x[1]/x1->x[0]; 1184 Log() << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n";1215 DoLog(3) && (Log() << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n"); 1185 1216 F1 = E1*E1 + 1.; 1186 1217 F2 = -E1*E2; 1187 1218 F3 = E1*E1 + D3*D3/(D2*D2) - C; 1188 Log() << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";1219 DoLog(3) && (Log() << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n"); 1189 1220 if (fabs(F1) < MYEPSILON) { 1190 Log() << Verbose(4) << "F1 == 0!\n";1191 Log() << Verbose(4) << "Gleichungssystem linear\n";1221 DoLog(4) && (Log() << Verbose(4) << "F1 == 0!\n"); 1222 DoLog(4) && (Log() << Verbose(4) << "Gleichungssystem linear\n"); 1192 1223 x[1] = F3/(2.*F2); 1193 1224 } else { 1194 1225 p = F2/F1; 1195 1226 q = p*p - F3/F1; 1196 Log() << Verbose(4) << "p " << p << "\tq " << q << endl;1227 DoLog(4) && (Log() << Verbose(4) << "p " << p << "\tq " << q << endl); 1197 1228 if (q < 0) { 1198 Log() << Verbose(4) << "q < 0" << endl;1229 DoLog(4) && (Log() << Verbose(4) << "q < 0" << endl); 1199 1230 return false; 1200 1231 } … … 1203 1234 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2]; 1204 1235 } else { 1205 Log() << Verbose(2) << "Gleichungssystem unterbestimmt\n";1236 DoLog(2) && (Log() << Verbose(2) << "Gleichungssystem unterbestimmt\n"); 1206 1237 return false; 1207 1238 } … … 1209 1240 E1 = A/x1->x[0]+x1->x[1]/x1->x[0]*D3/D1; 1210 1241 E2 = x1->x[1]/x1->x[0]*D2/D1 - x1->x[2]; 1211 Log() << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n";1242 DoLog(2) && (Log() << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n"); 1212 1243 F1 = E2*E2 + D2*D2/(D1*D1) + 1.; 1213 1244 F2 = -(E1*E2 + D2*D3/(D1*D1)); 1214 1245 F3 = E1*E1 + D3*D3/(D1*D1) - C; 1215 Log() << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";1246 DoLog(2) && (Log() << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n"); 1216 1247 if (fabs(F1) < MYEPSILON) { 1217 Log() << Verbose(3) << "F1 == 0!\n";1218 Log() << Verbose(3) << "Gleichungssystem linear\n";1248 DoLog(3) && (Log() << Verbose(3) << "F1 == 0!\n"); 1249 DoLog(3) && (Log() << Verbose(3) << "Gleichungssystem linear\n"); 1219 1250 x[2] = F3/(2.*F2); 1220 1251 } else { 1221 1252 p = F2/F1; 1222 1253 q = p*p - F3/F1; 1223 Log() << Verbose(3) << "p " << p << "\tq " << q << endl;1254 DoLog(3) && (Log() << Verbose(3) << "p " << p << "\tq " << q << endl); 1224 1255 if (q < 0) { 1225 Log() << Verbose(3) << "q < 0" << endl;1256 DoLog(3) && (Log() << Verbose(3) << "q < 0" << endl); 1226 1257 return false; 1227 1258 } … … 1261 1292 for (j=2;j>=0;j--) { 1262 1293 k = (i & pot(2,j)) << j; 1263 Log() << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl;1294 DoLog(2) && (Log() << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl); 1264 1295 sign[j] = (k == 0) ? 1. : -1.; 1265 1296 } 1266 Log() << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";1297 DoLog(2) && (Log() << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n"); 1267 1298 // apply sign matrix 1268 1299 for (j=NDIM;j--;) … … 1270 1301 // calculate angle and check 1271 1302 ang = x2->Angle (this); 1272 Log() << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t";1303 DoLog(1) && (Log() << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t"); 1273 1304 if (fabs(ang - cos(beta)) < MYEPSILON) { 1274 1305 break;
Note:
See TracChangeset
for help on using the changeset viewer.