Changeset 45ef76 for src


Ignore:
Timestamp:
May 27, 2010, 2:15:57 PM (15 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, 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.

Location:
src
Files:
2 added
8 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}
  • src/Line.hpp

    r47191b r45ef76  
    1212
    1313#include <memory>
     14#include <vector>
    1415
    1516class Vector;
     
    1819{
    1920public:
    20   Line(Vector &_origin, Vector &_direction);
     21  Line(const Vector &_origin, const Vector &_direction);
     22  Line(const Line& _src);
    2123  virtual ~Line();
    2224
    23   virtual double distance(const Vector &point) const=0;
    24   virtual Vector getClosestPoint(const Vector &point) const=0;
     25  virtual double distance(const Vector &point) const;
     26  virtual Vector getClosestPoint(const Vector &point) const;
     27
     28  Vector getDirection() const;
     29  Vector getOrigin() const;
     30
     31  std::vector<Vector> getPointsOnLine() const;
     32
     33  Vector getIntersection(const Line& otherLine) const;
    2534
    2635private:
     
    2938};
    3039
     40/**
     41 * Named constructor to make a line through two points
     42 */
     43Line makeLineThrough(const Vector &x1, const Vector &x2);
     44
    3145#endif /* LINE_HPP_ */
  • src/Plane.cpp

    r47191b r45ef76  
    113113}
    114114
    115 Vector Plane::getOffsetVector() {
     115Vector Plane::getOffsetVector() const {
    116116  return getOffset()*getNormal();
    117117}
    118118
    119 vector<Vector> Plane::getPointsOnPlane(){
     119vector<Vector> Plane::getPointsOnPlane() const{
    120120  std::vector<Vector> res;
    121121  res.reserve(3);
     
    147147 * \return true -  \a this contains intersection point on return, false - line is parallel to plane (even if in-plane)
    148148 */
    149 Vector Plane::GetIntersection(const Vector &Origin, const Vector &LineVector)
     149Vector Plane::GetIntersection(const Vector &Origin, const Vector &LineVector) const
    150150{
    151151  Info FunctionInfo(__func__);
  • src/Plane.hpp

    r47191b r45ef76  
    4242   * Same as getOffset()*getNormal();
    4343   */
    44   Vector getOffsetVector();
     44  Vector getOffsetVector() const;
    4545
    4646  /**
    4747   * returns three seperate points on this plane
    4848   */
    49   std::vector<Vector> getPointsOnPlane();
     49  std::vector<Vector> getPointsOnPlane() const;
    5050
    5151  // some calculations
    52   Vector GetIntersection(const Vector &Origin, const Vector &LineVector);
     52  Vector GetIntersection(const Vector &Origin, const Vector &LineVector) const;
    5353
    5454  /****** Methods inherited from Space ***********/
  • src/unittests/Makefile.am

    r47191b r45ef76  
    2323  InfoUnitTest \
    2424  LinearSystemOfEquationsUnitTest \
     25  LineUnittest \
    2526  LinkedCellUnitTest \
    2627  ListOfBondsUnitTest \
     
    6465  infounittest.cpp \
    6566  linearsystemofequationsunittest.cpp \
     67  LineUnittest.cpp \
    6668  LinkedCellUnitTest.cpp \
    6769  listofbondsunittest.cpp \
     
    9799  infounittest.hpp \
    98100  linearsystemofequationsunittest.hpp \
     101  LineUnittest.hpp \
    99102  LinkedCellUnitTest.hpp \
    100103  listofbondsunittest.hpp \
     
    153156LinearSystemOfEquationsUnitTest_LDADD = ${ALLLIBS}
    154157
     158LineUnittest_SOURCES = UnitTestMain.cpp LineUnittest.cpp LineUnittest.hpp
     159LineUnittest_LDADD = ${ALLLIBS}
     160
    155161LinkedCellUnitTest_SOURCES = UnitTestMain.cpp LinkedCellUnitTest.cpp LinkedCellUnitTest.hpp
    156162LinkedCellUnitTest_LDADD = ${ALLLIBS}
  • src/unittests/vectorunittest.cpp

    r47191b r45ef76  
    228228  CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture );
    229229
    230   // four vectors equal to zero
    231   CPPUNIT_ASSERT_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(zero, zero, zero, zero), LinearDependenceException);
    232   //CPPUNIT_ASSERT_EQUAL( zero, fixture );
    233 
    234   // four vectors equal to unit
    235   CPPUNIT_ASSERT_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, unit, unit, unit), LinearDependenceException);
    236   //CPPUNIT_ASSERT_EQUAL( zero, fixture );
    237 
    238   // two equal lines
    239   CPPUNIT_ASSERT_NO_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, unit, two));
    240   CPPUNIT_ASSERT_EQUAL( unit, fixture );
    241 
    242   // line from (1,0,0) to (2,1,0) cuts line from (1,0,0) to (0,1,0) at ???
    243   CPPUNIT_ASSERT_NO_THROW( fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, unit, otherunit) );
    244   CPPUNIT_ASSERT_EQUAL( unit, fixture );
    245 
    246   // line from (1,0,0) to (0,0,0) cuts line from (0,0,0) to (2,1,0) at ???
    247   CPPUNIT_ASSERT_NO_THROW( fixture = GetIntersectionOfTwoLinesOnPlane(unit, zero, zero, two) );
    248   CPPUNIT_ASSERT_EQUAL( zero, fixture );
    249 
    250   // line from (1,0,0) to (2,1,0) cuts line from (0,0,0) to (0,1,0) at ???
    251   CPPUNIT_ASSERT_NO_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, zero, otherunit) );
    252   CPPUNIT_ASSERT_EQUAL( Vector(0., -1., 0.), fixture );
    253230};
    254231
  • src/vector.cpp

    r47191b r45ef76  
    552552};
    553553
     554std::pair<Vector,Vector> Vector::partition(const Vector &rhs) const{
     555  double factor = ScalarProduct(rhs)/rhs.NormSquared();
     556  Vector res= factor * rhs;
     557  return make_pair(res,(*this)-res);
     558}
     559
     560std::pair<pointset,Vector> Vector::partition(const pointset &points) const{
     561  Vector helper = *this;
     562  pointset res;
     563  for(pointset::const_iterator iter=points.begin();iter!=points.end();++iter){
     564    pair<Vector,Vector> currPart = helper.partition(*iter);
     565    res.push_back(currPart.first);
     566    helper = currPart.second;
     567  }
     568  return make_pair(res,helper);
     569}
     570
    554571/** Do a matrix multiplication.
    555572 * \param *matrix NDIM_NDIM array
     
    633650  bool result = false;
    634651  double factor = y1.ScalarProduct(*this)/y1.NormSquared();
    635   Vector x1;
    636   x1 = factor * y1;
     652  Vector x1 = factor * y1;
    637653  SubtractVector(x1);
    638654  for (int i=NDIM;i--;)
  • src/vector.hpp

    r47191b r45ef76  
    1616
    1717#include <memory>
     18#include <vector>
    1819
    1920#include "defs.hpp"
     
    2122
    2223/********************************************** declarations *******************************/
     24
     25class Vector;
     26
     27typedef std::vector<Vector> pointset;
    2328
    2429/** Single vector.
     
    6671  bool IsInParallelepiped(const Vector &offset, const double * const parallelepiped) const;
    6772  void WrapPeriodically(const double * const M, const double * const Minv);
     73  std::pair<Vector,Vector> partition(const Vector&) const;
     74  std::pair<pointset,Vector> partition(const pointset&) const;
    6875
    6976  // Accessors ussually come in pairs... and sometimes even more than that
Note: See TracChangeset for help on using the changeset viewer.