Changeset ef9df36 for src/vector.cpp


Ignore:
Timestamp:
Aug 19, 2009, 12:23:05 PM (16 years ago)
Author:
Frederik Heber <heber@…>
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, Candidate_v1.7.0, 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:
99c484
Parents:
658efb
Message:

VectorUnitTest extended to Projections and Line intersection, some subsequent bug fixes.

Note: VectorUnitTest is running fine.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/vector.cpp

    r658efb ref9df36  
    212212 * \param *PlaneNormal Plane's normal vector
    213213 * \param *PlaneOffset Plane's offset vector
    214  * \param *LineVector first vector of line
    215  * \param *LineVector2 second vector of line
     214 * \param *Origin first vector of line
     215 * \param *LineVector second vector of line
    216216 * \return true -  \a this contains intersection point on return, false - line is parallel to plane
    217217 */
     
    224224  Direction.CopyVector(LineVector);
    225225  Direction.SubtractVector(Origin);
     226  //*out << Verbose(4) << "INFO: Direction is " << Direction << "." << endl;
    226227  factor = Direction.ScalarProduct(PlaneNormal);
    227228  if (factor < MYEPSILON) { // Uniqueness: line parallel to plane?
     
    230231  }
    231232  helper.CopyVector(PlaneOffset);
    232   helper.SubtractVector(LineVector);
     233  helper.SubtractVector(Origin);
    233234  factor = helper.ScalarProduct(PlaneNormal)/factor;
    234235  //factor = Origin->ScalarProduct(PlaneNormal)*(-PlaneOffset->ScalarProduct(PlaneNormal))/(Direction.ScalarProduct(PlaneNormal));
    235236  Direction.Scale(factor);
    236   CopyVector(LineVector);
     237  CopyVector(Origin);
     238  //*out << Verbose(4) << "INFO: Scaled direction is " << Direction << "." << endl;
    237239  AddVector(&Direction);
    238240
     
    241243  helper.SubtractVector(PlaneOffset);
    242244  if (helper.ScalarProduct(PlaneNormal) < MYEPSILON) {
    243     *out << Verbose(2) << "INFO: Intersection at " << *this << " is good." << endl;
     245    //*out << Verbose(2) << "INFO: Intersection at " << *this << " is good." << endl;
    244246    return true;
    245247  } else {
     
    250252
    251253/** Calculates the intersection of the two lines that are both on the same plane.
    252  * Note that we do not check whether they are on the same plane. Vector is calculated with respecy to second line.
     254 * We construct auxiliary plane with its vector normal to one line direction and the PlaneNormal, then a vector
     255 * from the first line's offset onto the plane. Finally, scale by factor is 1/cos(angle(line1,line2..)) = 1/SP(...), and
     256 * project onto the first line's direction and add its offset.
    253257 * \param *out output stream for debugging
    254258 * \param *Line1a first vector of first line
     
    261265bool Vector::GetIntersectionOfTwoLinesOnPlane(ofstream *out, Vector *Line1a, Vector *Line1b, Vector *Line2a, Vector *Line2b, const Vector *PlaneNormal)
    262266{
    263   double factor1, factor2;
    264   Vector helper, Line, LineNormal, *OtherNormal = NULL;
    265   const Vector *Normal;
    266   bool result = false;
    267 
    268   // create Plane normal vector
    269   if (PlaneNormal == NULL) {
    270     OtherNormal = new Vector(0.,0.,0.);
    271     if (!OtherNormal->MakeNormalVector(Line1a, Line1b, Line2a))
    272       if (!OtherNormal->MakeNormalVector(Line1a, Line1b, Line2b)) {
    273         *out << Verbose(1) << "ERROR: GetIntersectionOfTwoLinesOnPlane() cannot create a normal of the plane, everything is linear dependent." << endl;
    274         return false;
    275       }
    276     Normal = OtherNormal;
    277   } else
    278     Normal = PlaneNormal;
    279   *out << Verbose(3) << "INFO: Normal of plane is " << *Normal << "." << endl;
    280 
    281   // check if lines are parallel
    282   helper.CopyVector(Line2b);
    283   helper.SubtractVector(Line2a);
    284   if (fabs(helper.ScalarProduct(Normal)) < MYEPSILON) {
    285     *out << Verbose(1) << "Lines " << helper << " and " << Line << " are parallel, no cross point!" << endl;
    286     result = false;
    287   } else { 
    288     helper.CopyVector(Line2a);
    289     helper.SubtractVector(Line1a);
    290     factor1 = helper.ScalarProduct(Normal);
    291     helper.CopyVector(Line2b);
    292     helper.SubtractVector(Line1a);
    293     factor2 = helper.ScalarProduct(Normal);
    294     if (fabs(factor2) > MYEPSILON) {
    295       CopyVector(Line2a);
    296       helper.Scale(factor1/factor2);
    297       AddVector(&helper);
     267  bool result = true;
     268  Vector Direction, OtherDirection;
     269  Vector AuxiliaryNormal;
     270  Vector Distance;
     271  const Vector *Normal = NULL;
     272  Vector *ConstructedNormal = NULL;
     273  bool FreeNormal = false;
     274
     275  // construct both direction vectors
     276  Zero();
     277  Direction.CopyVector(Line1b);
     278  Direction.SubtractVector(Line1a);
     279  if (Direction.IsZero())
     280    return false;
     281  OtherDirection.CopyVector(Line2b);
     282  OtherDirection.SubtractVector(Line2a);
     283  if (OtherDirection.IsZero())
     284    return false;
     285
     286  Direction.Normalize();
     287  OtherDirection.Normalize();
     288
     289  //*out << Verbose(4) << "INFO: Normalized Direction " << Direction << " and OtherDirection " << OtherDirection << "." << endl;
     290
     291  if (fabs(OtherDirection.ScalarProduct(&Direction) - 1.) < MYEPSILON) { // lines are parallel
     292    if ((Line1a == Line2a) || (Line1a == Line2b))
     293      CopyVector(Line1a);
     294    else if ((Line1b == Line2b) || (Line1b == Line2b))
     295        CopyVector(Line1b);
     296    else
     297      return false;
     298    *out << Verbose(4) << "INFO: Intersection is " << *this << "." << endl;
     299    return true;
     300  } else {
     301    // check whether we have a plane normal vector
     302    if (PlaneNormal == NULL) {
     303      ConstructedNormal = new Vector;
     304      ConstructedNormal->MakeNormalVector(&Direction, &OtherDirection);
     305      Normal = ConstructedNormal;
     306      FreeNormal = true;
     307    } else
     308      Normal = PlaneNormal;
     309
     310    AuxiliaryNormal.MakeNormalVector(&OtherDirection, Normal);
     311    //*out << Verbose(4) << "INFO: PlaneNormal is " << *Normal << " and AuxiliaryNormal " << AuxiliaryNormal << "." << endl;
     312
     313    Distance.CopyVector(Line2a);
     314    Distance.SubtractVector(Line1a);
     315    //*out << Verbose(4) << "INFO: Distance is " << Distance << "." << endl;
     316    if (Distance.IsZero()) {
     317      // offsets are equal, match found
     318      CopyVector(Line1a);
    298319      result = true;
    299320    } else {
    300       Zero();
    301       result = false;
     321      CopyVector(Distance.Projection(&AuxiliaryNormal));
     322      //*out << Verbose(4) << "INFO: Projected Distance is " << *this << "." << endl;
     323      double factor = Direction.ScalarProduct(&AuxiliaryNormal);
     324      //*out << Verbose(4) << "INFO: Scaling factor is " << factor << "." << endl;
     325      Scale(1./(factor*factor));
     326      //*out << Verbose(4) << "INFO: Scaled Distance is " << *this << "." << endl;
     327      CopyVector(Projection(&Direction));
     328      //*out << Verbose(4) << "INFO: Distance, projected into Direction, is " << *this << "." << endl;
     329      if (this->IsZero())
     330        result = false;
     331      else
     332        result = true;
     333      AddVector(Line1a);
    302334    }
    303   }
    304 
    305   if (OtherNormal != NULL)
    306     delete(OtherNormal);
     335
     336    if (FreeNormal)
     337      delete(ConstructedNormal);
     338  }
     339  if (result)
     340    *out << Verbose(4) << "INFO: Intersection is " << *this << "." << endl;
    307341
    308342  return result;
     
    311345/** Calculates the projection of a vector onto another \a *y.
    312346 * \param *y array to second vector
    313  * \return \f$\langle x, y \rangle\f$
    314  */
    315 double Vector::Projection(const Vector *y) const
    316 {
    317   return (ScalarProduct(y));
     347 */
     348void Vector::ProjectIt(const Vector *y)
     349{
     350  Vector helper(*y);
     351  helper.Scale(-(ScalarProduct(y)));
     352  AddVector(&helper);
     353};
     354
     355/** Calculates the projection of a vector onto another \a *y.
     356 * \param *y array to second vector
     357 * \return Vector
     358 */
     359Vector Vector::Projection(const Vector *y) const
     360{
     361  Vector helper(*y);
     362  helper.Scale((ScalarProduct(y)/y->NormSquared()));
     363
     364  return helper;
    318365};
    319366
     
    390437};
    391438
     439/** Checks whether vector is normal to \a *normal.
     440 * @return true - vector is normalized, false - vector is not
     441 */
     442bool Vector::IsNormalTo(const Vector *normal) const
     443{
     444  if (ScalarProduct(normal) < MYEPSILON)
     445    return true;
     446  else
     447    return false;
     448};
     449
    392450/** Calculates the angle between this and another vector.
    393451 * \param *y array to second vector
     
    397455{
    398456  double norm1 = Norm(), norm2 = y->Norm();
    399   double angle = 1;
     457  double angle = -1;
    400458  if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
    401459    angle = this->ScalarProduct(y)/norm1/norm2;
     
    418476  // normalise this vector with respect to axis
    419477  a.CopyVector(this);
    420   a.Scale(Projection(axis));
    421   SubtractVector(&a);
     478  a.ProjectOntoPlane(axis);
    422479  // construct normal vector
    423480  y.MakeNormalVector(axis,this);
     
    430487  // add part in axis direction
    431488  AddVector(&a);
     489};
     490
     491/** Compares vector \a to vector \a b component-wise.
     492 * \param a base vector
     493 * \param b vector components to add
     494 * \return a == b
     495 */
     496bool operator==(const Vector& a, const Vector& b)
     497{
     498  bool status = true;
     499  for (int i=0;i<NDIM;i++)
     500    status = status && (fabs(a.x[i] - b.x[i]) < MYEPSILON);
     501  return status;
    432502};
    433503
     
    703773  x2.SubtractVector(y2);
    704774  if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) {
    705     cout << Verbose(4) << "Given vectors are linear dependent." << endl;
     775    cout << Verbose(4) << "WARNING: Given vectors are linear dependent." << endl;
    706776    return false;
    707777  }
     
    737807  Zero();
    738808  if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) {
    739     cout << Verbose(4) << "Given vectors are linear dependent." << endl;
     809    cout << Verbose(4) << "WARNING: Given vectors are linear dependent." << endl;
    740810    return false;
    741811  }
     
    757827/** Calculates orthonormal vector to one given vectors.
    758828 * Just subtracts the projection onto the given vector from this vector.
     829 * The removed part of the vector is Vector::Projection()
    759830 * \param *x1 vector
    760831 * \return true - success, false - vector is zero
     
    763834{
    764835  bool result = false;
    765   double factor = y1->Projection(this)/y1->Norm()/y1->Norm();
     836  double factor = y1->ScalarProduct(this)/y1->NormSquared();
    766837  Vector x1;
    767838  x1.CopyVector(y1);
     
    820891};
    821892
    822 /** Determines paramter needed to multiply this vector to obtain intersection point with plane defined by \a *A, \a *B and \a *C.
     893/** Determines parameter needed to multiply this vector to obtain intersection point with plane defined by \a *A, \a *B and \a *C.
    823894 * \param *A first plane vector
    824895 * \param *B second plane vector
     
    833904//  cout << "C " << C->Projection(this) << "\t";
    834905//  cout << endl;
    835   return A->Projection(this);
     906  return A->ScalarProduct(this);
    836907};
    837908
     
    9451016  for (int i=NDIM;i--;)
    9461017    this->x[i] = y->x[i];
     1018}
     1019
     1020/** Copy vector \a y componentwise.
     1021 * \param y vector
     1022 */
     1023void Vector::CopyVector(const Vector y)
     1024{
     1025  for (int i=NDIM;i--;)
     1026    this->x[i] = y.x[i];
    9471027}
    9481028
Note: See TracChangeset for help on using the changeset viewer.