Changeset 075729 for molecuilder/src/vector.cpp
- Timestamp:
- Apr 27, 2010, 2:25:42 PM (15 years ago)
- Children:
- 90c4460
- Parents:
- 1561e2 (diff), 2bc713 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/vector.cpp
r1561e2 r075729 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 364 delete(M); 331 Log() << Verbose(1) << "INFO: Line1a = " << *Line1a << ", Line1b = " << *Line1b << ", Line2a = " << *Line2a << ", Line2b = " << *Line2b << "." << endl;365 DoLog(1) && (Log() << Verbose(1) << "INFO: Line1a = " << *Line1a << ", Line1b = " << *Line1b << ", Line2a = " << *Line2a << ", Line2b = " << *Line2b << "." << endl); 332 366 333 367 … … 345 379 d.CopyVector(Line2b); 346 380 d.SubtractVector(Line1b); 347 Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl;381 DoLog(1) && (Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl); 348 382 if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) { 349 383 Zero(); 350 Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl;384 DoLog(1) && (Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl); 351 385 return false; 352 386 } … … 361 395 if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) { 362 396 CopyVector(Line2a); 363 Log() << Verbose(1) << "Lines conincide." << endl;397 DoLog(1) && (Log() << Verbose(1) << "Lines conincide." << endl); 364 398 return true; 365 399 } else { … … 369 403 if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) { 370 404 CopyVector(Line2b); 371 Log() << Verbose(1) << "Lines conincide." << endl;405 DoLog(1) && (Log() << Verbose(1) << "Lines conincide." << endl); 372 406 return true; 373 407 } 374 408 } 375 Log() << Verbose(1) << "Lines are parallel." << endl;409 DoLog(1) && (Log() << Verbose(1) << "Lines are parallel." << endl); 376 410 Zero(); 377 411 return false; … … 385 419 temp2.CopyVector(&a); 386 420 temp2.VectorProduct(&b); 387 Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl;421 DoLog(1) && (Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl); 388 422 if (fabs(temp2.NormSquared()) > MYEPSILON) 389 423 s = temp1.ScalarProduct(&temp2)/temp2.NormSquared(); 390 424 else 391 425 s = 0.; 392 Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(&temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl;426 DoLog(1) && (Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(&temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl); 393 427 394 428 // construct intersection … … 396 430 Scale(s); 397 431 AddVector(Line1a); 398 Log() << Verbose(1) << "Intersection is at " << *this << "." << endl;432 DoLog(1) && (Log() << Verbose(1) << "Intersection is at " << *this << "." << endl); 399 433 400 434 return true; … … 665 699 void Vector::Output() const 666 700 { 667 Log() << Verbose(0) << "(";701 DoLog(0) && (Log() << Verbose(0) << "("); 668 702 for (int i=0;i<NDIM;i++) { 669 Log() << Verbose(0) << x[i];703 DoLog(0) && (Log() << Verbose(0) << x[i]); 670 704 if (i != 2) 671 Log() << Verbose(0) << ",";672 } 673 Log() << Verbose(0) << ")";705 DoLog(0) && (Log() << Verbose(0) << ","); 706 } 707 DoLog(0) && (Log() << Verbose(0) << ")"); 674 708 }; 675 709 … … 780 814 x[i] = C.x[i]; 781 815 } else { 782 eLog() << Verbose(1) << "inverse of matrix does not exists: det A = " << detA << "." << endl;816 DoeLog(1) && (eLog()<< Verbose(1) << "inverse of matrix does not exists: det A = " << detA << "." << endl); 783 817 } 784 818 }; … … 806 840 projection = ScalarProduct(n)/n->ScalarProduct(n); // remove constancy from n (keep as logical one) 807 841 // withdraw projected vector twice from original one 808 Log() << Verbose(1) << "Vector: ";842 DoLog(1) && (Log() << Verbose(1) << "Vector: "); 809 843 Output(); 810 Log() << Verbose(0) << "\t";844 DoLog(0) && (Log() << Verbose(0) << "\t"); 811 845 for (int i=NDIM;i--;) 812 846 x[i] -= 2.*projection*n->x[i]; 813 Log() << Verbose(0) << "Projected vector: ";847 DoLog(0) && (Log() << Verbose(0) << "Projected vector: "); 814 848 Output(); 815 Log() << Verbose(0) << endl;849 DoLog(0) && (Log() << Verbose(0) << endl); 816 850 }; 817 851 … … 832 866 x2.SubtractVector(y2); 833 867 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) { 834 eLog() << Verbose(2) << "Given vectors are linear dependent." << endl;868 DoeLog(2) && (eLog()<< Verbose(2) << "Given vectors are linear dependent." << endl); 835 869 return false; 836 870 } … … 866 900 Zero(); 867 901 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) { 868 eLog() << Verbose(2) << "Given vectors are linear dependent." << endl;902 DoeLog(2) && (eLog()<< Verbose(2) << "Given vectors are linear dependent." << endl); 869 903 return false; 870 904 } … … 917 951 double norm; 918 952 919 Log() << Verbose(4);953 DoLog(4) && (Log() << Verbose(4)); 920 954 GivenVector->Output(); 921 Log() << Verbose(0) << endl;955 DoLog(0) && (Log() << Verbose(0) << endl); 922 956 for (j=NDIM;j--;) 923 957 Components[j] = -1; … … 926 960 if (fabs(GivenVector->x[j]) > MYEPSILON) 927 961 Components[Last++] = j; 928 Log() << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl;962 DoLog(4) && (Log() << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl); 929 963 930 964 switch(Last) { … … 976 1010 977 1011 for (j=0;j<num;j++) { 978 Log() << Verbose(1) << j << "th atom's vector: ";1012 DoLog(1) && (Log() << Verbose(1) << j << "th atom's vector: "); 979 1013 (vectors[j])->Output(); 980 Log() << Verbose(0) << endl;1014 DoLog(0) && (Log() << Verbose(0) << endl); 981 1015 } 982 1016 … … 1104 1138 j += i+1; 1105 1139 do { 1106 Log() << Verbose(0) << coords[i] << "[0.." << cell_size[j] << "]: ";1140 DoLog(0) && (Log() << Verbose(0) << coords[i] << "[0.." << cell_size[j] << "]: "); 1107 1141 cin >> x[i]; 1108 1142 } while (((x[i] < 0) || (x[i] >= cell_size[j])) && (check)); … … 1135 1169 B2 = cos(beta) * x2->Norm() * c; 1136 1170 C = c * c; 1137 Log() << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl;1171 DoLog(2) && (Log() << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl); 1138 1172 int flag = 0; 1139 1173 if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping … … 1174 1208 D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2]; 1175 1209 D3 = y->x[0]/x1->x[0]*A-B1; 1176 Log() << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n";1210 DoLog(2) && (Log() << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n"); 1177 1211 if (fabs(D1) < MYEPSILON) { 1178 Log() << Verbose(2) << "D1 == 0!\n";1212 DoLog(2) && (Log() << Verbose(2) << "D1 == 0!\n"); 1179 1213 if (fabs(D2) > MYEPSILON) { 1180 Log() << Verbose(3) << "D2 != 0!\n";1214 DoLog(3) && (Log() << Verbose(3) << "D2 != 0!\n"); 1181 1215 x[2] = -D3/D2; 1182 1216 E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2; 1183 1217 E2 = -x1->x[1]/x1->x[0]; 1184 Log() << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n";1218 DoLog(3) && (Log() << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n"); 1185 1219 F1 = E1*E1 + 1.; 1186 1220 F2 = -E1*E2; 1187 1221 F3 = E1*E1 + D3*D3/(D2*D2) - C; 1188 Log() << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";1222 DoLog(3) && (Log() << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n"); 1189 1223 if (fabs(F1) < MYEPSILON) { 1190 Log() << Verbose(4) << "F1 == 0!\n";1191 Log() << Verbose(4) << "Gleichungssystem linear\n";1224 DoLog(4) && (Log() << Verbose(4) << "F1 == 0!\n"); 1225 DoLog(4) && (Log() << Verbose(4) << "Gleichungssystem linear\n"); 1192 1226 x[1] = F3/(2.*F2); 1193 1227 } else { 1194 1228 p = F2/F1; 1195 1229 q = p*p - F3/F1; 1196 Log() << Verbose(4) << "p " << p << "\tq " << q << endl;1230 DoLog(4) && (Log() << Verbose(4) << "p " << p << "\tq " << q << endl); 1197 1231 if (q < 0) { 1198 Log() << Verbose(4) << "q < 0" << endl;1232 DoLog(4) && (Log() << Verbose(4) << "q < 0" << endl); 1199 1233 return false; 1200 1234 } … … 1203 1237 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2]; 1204 1238 } else { 1205 Log() << Verbose(2) << "Gleichungssystem unterbestimmt\n";1239 DoLog(2) && (Log() << Verbose(2) << "Gleichungssystem unterbestimmt\n"); 1206 1240 return false; 1207 1241 } … … 1209 1243 E1 = A/x1->x[0]+x1->x[1]/x1->x[0]*D3/D1; 1210 1244 E2 = x1->x[1]/x1->x[0]*D2/D1 - x1->x[2]; 1211 Log() << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n";1245 DoLog(2) && (Log() << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n"); 1212 1246 F1 = E2*E2 + D2*D2/(D1*D1) + 1.; 1213 1247 F2 = -(E1*E2 + D2*D3/(D1*D1)); 1214 1248 F3 = E1*E1 + D3*D3/(D1*D1) - C; 1215 Log() << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";1249 DoLog(2) && (Log() << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n"); 1216 1250 if (fabs(F1) < MYEPSILON) { 1217 Log() << Verbose(3) << "F1 == 0!\n";1218 Log() << Verbose(3) << "Gleichungssystem linear\n";1251 DoLog(3) && (Log() << Verbose(3) << "F1 == 0!\n"); 1252 DoLog(3) && (Log() << Verbose(3) << "Gleichungssystem linear\n"); 1219 1253 x[2] = F3/(2.*F2); 1220 1254 } else { 1221 1255 p = F2/F1; 1222 1256 q = p*p - F3/F1; 1223 Log() << Verbose(3) << "p " << p << "\tq " << q << endl;1257 DoLog(3) && (Log() << Verbose(3) << "p " << p << "\tq " << q << endl); 1224 1258 if (q < 0) { 1225 Log() << Verbose(3) << "q < 0" << endl;1259 DoLog(3) && (Log() << Verbose(3) << "q < 0" << endl); 1226 1260 return false; 1227 1261 } … … 1261 1295 for (j=2;j>=0;j--) { 1262 1296 k = (i & pot(2,j)) << j; 1263 Log() << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl;1297 DoLog(2) && (Log() << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl); 1264 1298 sign[j] = (k == 0) ? 1. : -1.; 1265 1299 } 1266 Log() << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";1300 DoLog(2) && (Log() << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n"); 1267 1301 // apply sign matrix 1268 1302 for (j=NDIM;j--;) … … 1270 1304 // calculate angle and check 1271 1305 ang = x2->Angle (this); 1272 Log() << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t";1306 DoLog(1) && (Log() << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t"); 1273 1307 if (fabs(ang - cos(beta)) < MYEPSILON) { 1274 1308 break;
Note:
See TracChangeset
for help on using the changeset viewer.