Changeset 45ef76 for src/Line.cpp


Ignore:
Timestamp:
May 27, 2010, 2:15:57 PM (16 years ago)
Author:
Tillmann Crueger <crueger@…>
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:
643e76
Parents:
47191b
Message:

Added several methods to the line class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Line.cpp

    r47191b r45ef76  
    1111
    1212#include "vector.hpp"
     13#include "log.hpp"
     14#include "verbose.hpp"
     15#include "gslmatrix.hpp"
     16#include "info.hpp"
     17#include "Exceptions/LinearDependenceException.hpp"
     18#include "Exceptions/SkewException.hpp"
    1319
    14 Line::Line(Vector &_origin, Vector &_direction) :
    15   origin(new Vector(_origin)),
     20using namespace std;
     21
     22Line::Line(const Vector &_origin, const Vector &_direction) :
    1623  direction(new Vector(_direction))
    1724{
    1825  direction->Normalize();
     26  origin.reset(new Vector(_origin.partition(*direction).second));
    1927}
     28
     29Line::Line(const Line &src) :
     30  origin(new Vector(*src.origin)),
     31  direction(new Vector(*src.direction))
     32{}
    2033
    2134Line::~Line()
     
    2437
    2538double Line::distance(const Vector &point) const{
    26   return fabs(point.ScalarProduct(*direction) - origin->ScalarProduct(*direction));
     39  // get any vector from line to point
     40  Vector helper = point - *origin;
     41  // partition this vector along direction
     42  // the residue points from the line to the point
     43  return helper.partition(*direction).second.Norm();
    2744}
    2845
    2946Vector Line::getClosestPoint(const Vector &point) const{
    30   double factor = point.ScalarProduct(*direction) - origin->ScalarProduct(*direction);
    31   return (point - (factor * (*direction)));
     47  // get any vector from line to point
     48  Vector helper = point - *origin;
     49  // partition this vector along direction
     50  // add only the part along the direction
     51  return *origin + helper.partition(*direction).first;
    3252}
     53
     54Vector Line::getDirection() const{
     55  return *direction;
     56}
     57
     58Vector Line::getOrigin() const{
     59  return *origin;
     60}
     61
     62vector<Vector> Line::getPointsOnLine() const{
     63  vector<Vector> res;
     64  res.reserve(2);
     65  res.push_back(*origin);
     66  res.push_back(*origin+*direction);
     67  return res;
     68}
     69
     70Vector Line::getIntersection(const Line& otherLine) const{
     71  Info FunctionInfo(__func__);
     72
     73  pointset line1Points = getPointsOnLine();
     74
     75  Vector Line1a = line1Points[0];
     76  Vector Line1b = line1Points[1];
     77
     78  pointset line2Points = otherLine.getPointsOnLine();
     79
     80  Vector Line2a = line2Points[0];
     81  Vector Line2b = line2Points[1];
     82
     83  Vector res;
     84
     85  auto_ptr<GSLMatrix> M = auto_ptr<GSLMatrix>(new GSLMatrix(4,4));
     86
     87  M->SetAll(1.);
     88  for (int i=0;i<3;i++) {
     89    M->Set(0, i, Line1a[i]);
     90    M->Set(1, i, Line1b[i]);
     91    M->Set(2, i, Line2a[i]);
     92    M->Set(3, i, Line2b[i]);
     93  }
     94
     95  //Log() << Verbose(1) << "Coefficent matrix is:" << endl;
     96  //for (int i=0;i<4;i++) {
     97  //  for (int j=0;j<4;j++)
     98  //    cout << "\t" << M->Get(i,j);
     99  //  cout << endl;
     100  //}
     101  if (fabs(M->Determinant()) > MYEPSILON) {
     102    Log() << Verbose(1) << "Determinant of coefficient matrix is NOT zero." << endl;
     103    throw SkewException(__FILE__,__LINE__);
     104  }
     105
     106  Log() << Verbose(1) << "INFO: Line1a = " << Line1a << ", Line1b = " << Line1b << ", Line2a = " << Line2a << ", Line2b = " << Line2b << "." << endl;
     107
     108
     109  // constuct a,b,c
     110  Vector a = Line1b - Line1a;
     111  Vector b = Line2b - Line2a;
     112  Vector c = Line2a - Line1a;
     113  Vector d = Line2b - Line1b;
     114  Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl;
     115  if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) {
     116   res.Zero();
     117   Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl;
     118   throw LinearDependenceException(__FILE__,__LINE__);
     119  }
     120
     121  // check for parallelity
     122  Vector parallel;
     123  double factor = 0.;
     124  if (fabs(a.ScalarProduct(b)*a.ScalarProduct(b)/a.NormSquared()/b.NormSquared() - 1.) < MYEPSILON) {
     125    parallel = Line1a - Line2a;
     126    factor = parallel.ScalarProduct(a)/a.Norm();
     127    if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
     128      res = Line2a;
     129      Log() << Verbose(1) << "Lines conincide." << endl;
     130      return res;
     131    } else {
     132      parallel = Line1a - Line2b;
     133      factor = parallel.ScalarProduct(a)/a.Norm();
     134      if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
     135        res = Line2b;
     136        Log() << Verbose(1) << "Lines conincide." << endl;
     137        return res;
     138      }
     139    }
     140    Log() << Verbose(1) << "Lines are parallel." << endl;
     141    res.Zero();
     142    throw LinearDependenceException(__FILE__,__LINE__);
     143  }
     144
     145  // obtain s
     146  double s;
     147  Vector temp1, temp2;
     148  temp1 = c;
     149  temp1.VectorProduct(b);
     150  temp2 = a;
     151  temp2.VectorProduct(b);
     152  Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl;
     153  if (fabs(temp2.NormSquared()) > MYEPSILON)
     154    s = temp1.ScalarProduct(temp2)/temp2.NormSquared();
     155  else
     156    s = 0.;
     157  Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl;
     158
     159  // construct intersection
     160  res = a;
     161  res.Scale(s);
     162  res += Line1a;
     163  Log() << Verbose(1) << "Intersection is at " << res << "." << endl;
     164
     165  return res;
     166}
     167
     168Line makeLineThrough(const Vector &x1, const Vector &x2){
     169  if(x1==x2){
     170    throw LinearDependenceException(__FILE__,__LINE__);
     171  }
     172  return Line(x1,x1-x2);
     173}
Note: See TracChangeset for help on using the changeset viewer.