Changeset b8d1aeb for src/vector.cpp
- Timestamp:
- Feb 2, 2010, 11:38:06 AM (15 years ago)
- Branches:
- Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
- Children:
- 7ba324
- Parents:
- 9fe36b (diff), 2ededc2 (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
-
src/vector.cpp
r9fe36b rb8d1aeb 8 8 #include "defs.hpp" 9 9 #include "helpers.hpp" 10 #include "memoryallocator.hpp" 10 #include "info.hpp" 11 #include "gslmatrix.hpp" 11 12 #include "leastsquaremin.hpp" 12 13 #include "log.hpp" 14 #include "memoryallocator.hpp" 13 15 #include "vector.hpp" 14 16 #include "verbose.hpp" 17 18 #include <gsl/gsl_linalg.h> 19 #include <gsl/gsl_matrix.h> 20 #include <gsl/gsl_permutation.h> 21 #include <gsl/gsl_vector.h> 22 23 #include <cassert> 15 24 16 25 /************************************ Functions for class vector ************************************/ … … 215 224 * \param *Origin first vector of line 216 225 * \param *LineVector second vector of line 217 * \return true - \a this contains intersection point on return, false - line is parallel to plane 226 * \return true - \a this contains intersection point on return, false - line is parallel to plane (even if in-plane) 218 227 */ 219 228 bool Vector::GetIntersectionWithPlane(const Vector * const PlaneNormal, const Vector * const PlaneOffset, const Vector * const Origin, const Vector * const LineVector) 220 229 { 230 Info FunctionInfo(__func__); 221 231 double factor; 222 232 Vector Direction, helper; … … 226 236 Direction.SubtractVector(Origin); 227 237 Direction.Normalize(); 228 //Log() << Verbose(4) << "INFO: Direction is " << Direction << "." << endl; 238 Log() << Verbose(1) << "INFO: Direction is " << Direction << "." << endl; 239 //Log() << Verbose(1) << "INFO: PlaneNormal is " << *PlaneNormal << " and PlaneOffset is " << *PlaneOffset << "." << endl; 229 240 factor = Direction.ScalarProduct(PlaneNormal); 230 if (fa ctor< MYEPSILON) { // Uniqueness: line parallel to plane?231 eLog() << Verbose(2) << "Line is parallel to plane, no intersection." << endl;241 if (fabs(factor) < MYEPSILON) { // Uniqueness: line parallel to plane? 242 Log() << Verbose(1) << "BAD: Line is parallel to plane, no intersection." << endl; 232 243 return false; 233 244 } … … 235 246 helper.SubtractVector(Origin); 236 247 factor = helper.ScalarProduct(PlaneNormal)/factor; 237 if (fa ctor< MYEPSILON) { // Origin is in-plane238 //Log() << Verbose(2) << "Origin of line is in-plane, simple." << endl;248 if (fabs(factor) < MYEPSILON) { // Origin is in-plane 249 Log() << Verbose(1) << "GOOD: Origin of line is in-plane." << endl; 239 250 CopyVector(Origin); 240 251 return true; … … 243 254 Direction.Scale(factor); 244 255 CopyVector(Origin); 245 //Log() << Verbose(4) << "INFO: Scaled direction is " << Direction << "." << endl;256 Log() << Verbose(1) << "INFO: Scaled direction is " << Direction << "." << endl; 246 257 AddVector(&Direction); 247 258 … … 250 261 helper.SubtractVector(PlaneOffset); 251 262 if (helper.ScalarProduct(PlaneNormal) < MYEPSILON) { 252 //Log() << Verbose(2) << "INFO: Intersection at " << *this << " is good." << endl;263 Log() << Verbose(1) << "GOOD: Intersection is " << *this << "." << endl; 253 264 return true; 254 265 } else { … … 286 297 287 298 /** Calculates the intersection of the two lines that are both on the same plane. 288 * We construct auxiliary plane with its vector normal to one line direction and the PlaneNormal, then a vector 289 * from the first line's offset onto the plane. Finally, scale by factor is 1/cos(angle(line1,line2..)) = 1/SP(...), and 290 * project onto the first line's direction and add its offset. 299 * This is taken from Weisstein, Eric W. "Line-Line Intersection." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-LineIntersection.html 291 300 * \param *out output stream for debugging 292 301 * \param *Line1a first vector of first line … … 299 308 bool Vector::GetIntersectionOfTwoLinesOnPlane(const Vector * const Line1a, const Vector * const Line1b, const Vector * const Line2a, const Vector * const Line2b, const Vector *PlaneNormal) 300 309 { 301 bool result = true; 302 Vector Direction, OtherDirection; 303 Vector AuxiliaryNormal; 304 Vector Distance; 305 const Vector *Normal = NULL; 306 Vector *ConstructedNormal = NULL; 307 bool FreeNormal = false; 308 309 // construct both direction vectors 310 Zero(); 311 Direction.CopyVector(Line1b); 312 Direction.SubtractVector(Line1a); 313 if (Direction.IsZero()) 310 Info FunctionInfo(__func__); 311 312 GSLMatrix *M = new GSLMatrix(4,4); 313 314 M->SetAll(1.); 315 for (int i=0;i<3;i++) { 316 M->Set(0, i, Line1a->x[i]); 317 M->Set(1, i, Line1b->x[i]); 318 M->Set(2, i, Line2a->x[i]); 319 M->Set(3, i, Line2b->x[i]); 320 } 321 322 //Log() << Verbose(1) << "Coefficent matrix is:" << endl; 323 //for (int i=0;i<4;i++) { 324 // for (int j=0;j<4;j++) 325 // cout << "\t" << M->Get(i,j); 326 // cout << endl; 327 //} 328 if (fabs(M->Determinant()) > MYEPSILON) { 329 Log() << Verbose(1) << "Determinant of coefficient matrix is NOT zero." << endl; 314 330 return false; 315 OtherDirection.CopyVector(Line2b); 316 OtherDirection.SubtractVector(Line2a); 317 if (OtherDirection.IsZero()) 331 } 332 Log() << Verbose(1) << "INFO: Line1a = " << *Line1a << ", Line1b = " << *Line1b << ", Line2a = " << *Line2a << ", Line2b = " << *Line2b << "." << endl; 333 334 335 // constuct a,b,c 336 Vector a; 337 Vector b; 338 Vector c; 339 Vector d; 340 a.CopyVector(Line1b); 341 a.SubtractVector(Line1a); 342 b.CopyVector(Line2b); 343 b.SubtractVector(Line2a); 344 c.CopyVector(Line2a); 345 c.SubtractVector(Line1a); 346 d.CopyVector(Line2b); 347 d.SubtractVector(Line1b); 348 Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl; 349 if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) { 350 Zero(); 351 Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl; 352 return false; 353 } 354 355 // check for parallelity 356 Vector parallel; 357 double factor = 0.; 358 if (fabs(a.ScalarProduct(&b)*a.ScalarProduct(&b)/a.NormSquared()/b.NormSquared() - 1.) < MYEPSILON) { 359 parallel.CopyVector(Line1a); 360 parallel.SubtractVector(Line2a); 361 factor = parallel.ScalarProduct(&a)/a.Norm(); 362 if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) { 363 CopyVector(Line2a); 364 Log() << Verbose(1) << "Lines conincide." << endl; 365 return true; 366 } else { 367 parallel.CopyVector(Line1a); 368 parallel.SubtractVector(Line2b); 369 factor = parallel.ScalarProduct(&a)/a.Norm(); 370 if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) { 371 CopyVector(Line2b); 372 Log() << Verbose(1) << "Lines conincide." << endl; 373 return true; 374 } 375 } 376 Log() << Verbose(1) << "Lines are parallel." << endl; 377 Zero(); 318 378 return false; 319 320 Direction.Normalize(); 321 OtherDirection.Normalize(); 322 323 //Log() << Verbose(4) << "INFO: Normalized Direction " << Direction << " and OtherDirection " << OtherDirection << "." << endl; 324 325 if (fabs(OtherDirection.ScalarProduct(&Direction) - 1.) < MYEPSILON) { // lines are parallel 326 if ((Line1a == Line2a) || (Line1a == Line2b)) 327 CopyVector(Line1a); 328 else if ((Line1b == Line2b) || (Line1b == Line2b)) 329 CopyVector(Line1b); 330 else 331 return false; 332 Log() << Verbose(4) << "INFO: Intersection is " << *this << "." << endl; 333 return true; 334 } else { 335 // check whether we have a plane normal vector 336 if (PlaneNormal == NULL) { 337 ConstructedNormal = new Vector; 338 ConstructedNormal->MakeNormalVector(&Direction, &OtherDirection); 339 Normal = ConstructedNormal; 340 FreeNormal = true; 341 } else 342 Normal = PlaneNormal; 343 344 AuxiliaryNormal.MakeNormalVector(&OtherDirection, Normal); 345 //Log() << Verbose(4) << "INFO: PlaneNormal is " << *Normal << " and AuxiliaryNormal " << AuxiliaryNormal << "." << endl; 346 347 Distance.CopyVector(Line2a); 348 Distance.SubtractVector(Line1a); 349 //Log() << Verbose(4) << "INFO: Distance is " << Distance << "." << endl; 350 if (Distance.IsZero()) { 351 // offsets are equal, match found 352 CopyVector(Line1a); 353 result = true; 354 } else { 355 CopyVector(Distance.Projection(&AuxiliaryNormal)); 356 //Log() << Verbose(4) << "INFO: Projected Distance is " << *this << "." << endl; 357 double factor = Direction.ScalarProduct(&AuxiliaryNormal); 358 //Log() << Verbose(4) << "INFO: Scaling factor is " << factor << "." << endl; 359 Scale(1./(factor*factor)); 360 //Log() << Verbose(4) << "INFO: Scaled Distance is " << *this << "." << endl; 361 CopyVector(Projection(&Direction)); 362 //Log() << Verbose(4) << "INFO: Distance, projected into Direction, is " << *this << "." << endl; 363 if (this->IsZero()) 364 result = false; 365 else 366 result = true; 367 AddVector(Line1a); 368 } 369 370 if (FreeNormal) 371 delete(ConstructedNormal); 372 } 373 if (result) 374 Log() << Verbose(4) << "INFO: Intersection is " << *this << "." << endl; 375 376 return result; 379 } 380 381 // obtain s 382 double s; 383 Vector temp1, temp2; 384 temp1.CopyVector(&c); 385 temp1.VectorProduct(&b); 386 temp2.CopyVector(&a); 387 temp2.VectorProduct(&b); 388 Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl; 389 if (fabs(temp2.NormSquared()) > MYEPSILON) 390 s = temp1.ScalarProduct(&temp2)/temp2.NormSquared(); 391 else 392 s = 0.; 393 Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(&temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl; 394 395 // construct intersection 396 CopyVector(&a); 397 Scale(s); 398 AddVector(Line1a); 399 Log() << Verbose(1) << "Intersection is at " << *this << "." << endl; 400 401 return true; 377 402 }; 378 403 … … 480 505 else 481 506 return false; 507 }; 508 509 /** Checks whether vector is normal to \a *normal. 510 * @return true - vector is normalized, false - vector is not 511 */ 512 bool Vector::IsEqualTo(const Vector * const a) const 513 { 514 bool status = true; 515 for (int i=0;i<NDIM;i++) { 516 if (fabs(x[i] - a->x[i]) > MYEPSILON) 517 status = false; 518 } 519 return status; 482 520 }; 483 521 … … 626 664 return *x; 627 665 }; 666 667 Vector& Vector::operator=(const Vector& src) { 668 CopyVector(src); 669 return *this; 670 } 671 672 double& Vector::operator[](int i){ 673 assert(i<NDIM && "Invalid Vector dimension requested"); 674 return x[i]; 675 } 628 676 629 677 /** Prints a 3dim vector. … … 1040 1088 void Vector::CopyVector(const Vector * const y) 1041 1089 { 1042 for (int i=NDIM;i--;) 1043 this->x[i] = y->x[i]; 1090 // check for self assignment 1091 if(y!=this){ 1092 for (int i=NDIM;i--;) 1093 this->x[i] = y->x[i]; 1094 } 1044 1095 } 1045 1096 … … 1049 1100 void Vector::CopyVector(const Vector &y) 1050 1101 { 1051 for (int i=NDIM;i--;) 1052 this->x[i] = y.x[i]; 1102 // check for self assignment 1103 if(&y!=this) { 1104 for (int i=NDIM;i--;) 1105 this->x[i] = y.x[i]; 1106 } 1053 1107 } 1054 1108
Note:
See TracChangeset
for help on using the changeset viewer.