Changeset a7c344


Ignore:
Timestamp:
Jun 19, 2010, 4:06:59 PM (15 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, 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:
c39cc4
Parents:
b32dbb (diff), 27ac00 (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.
Message:

Merge branch 'StructureRefactoring' of jupiter:espack into StructureRefactoring

Location:
src
Files:
5 added
24 edited

Legend:

Unmodified
Added
Removed
  • src/Legacy/oldmenu.cpp

    rb32dbb ra7c344  
    2727#include "vector_ops.hpp"
    2828#include "Plane.hpp"
     29#include "Line.hpp"
    2930
    3031#include "UIElements/UIFactory.hpp"
     
    185186          // rotate vector around first angle
    186187          first->x = x;
    187           first->x = RotateVector(first->x,z,b - M_PI);
     188          first->x = Line(zeroVec,z).rotateVector(first->x,b - M_PI);
    188189          Log() << Verbose(0) << "Rotated vector: " << first->x << endl,
    189190          // remove the projection onto the rotation plane of the second angle
     
    201202          // rotate another vector around second angle
    202203          n = y;
    203           n = RotateVector(n,x,c - M_PI);
     204          n = Line(zeroVec,x).rotateVector(n,c - M_PI);
    204205          Log() << Verbose(0) << "2nd Rotated vector: " << n << endl;
    205206
  • src/Line.cpp

    rb32dbb ra7c344  
    1111
    1212#include "vector.hpp"
    13 
    14 Line::Line(Vector &_origin, Vector &_direction) :
    15   origin(new Vector(_origin)),
     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"
     19#include "Plane.hpp"
     20
     21using namespace std;
     22
     23Line::Line(const Vector &_origin, const Vector &_direction) :
    1624  direction(new Vector(_direction))
    1725{
    1826  direction->Normalize();
    19 }
     27  origin.reset(new Vector(_origin.partition(*direction).second));
     28}
     29
     30Line::Line(const Line &src) :
     31  origin(new Vector(*src.origin)),
     32  direction(new Vector(*src.direction))
     33{}
    2034
    2135Line::~Line()
     
    2438
    2539double Line::distance(const Vector &point) const{
    26   return fabs(point.ScalarProduct(*direction) - origin->ScalarProduct(*direction));
     40  // get any vector from line to point
     41  Vector helper = point - *origin;
     42  // partition this vector along direction
     43  // the residue points from the line to the point
     44  return helper.partition(*direction).second.Norm();
    2745}
    2846
    2947Vector Line::getClosestPoint(const Vector &point) const{
    30   double factor = point.ScalarProduct(*direction) - origin->ScalarProduct(*direction);
    31   return (point - (factor * (*direction)));
    32 }
     48  // get any vector from line to point
     49  Vector helper = point - *origin;
     50  // partition this vector along direction
     51  // add only the part along the direction
     52  return *origin + helper.partition(*direction).first;
     53}
     54
     55Vector Line::getDirection() const{
     56  return *direction;
     57}
     58
     59Vector Line::getOrigin() const{
     60  return *origin;
     61}
     62
     63vector<Vector> Line::getPointsOnLine() const{
     64  vector<Vector> res;
     65  res.reserve(2);
     66  res.push_back(*origin);
     67  res.push_back(*origin+*direction);
     68  return res;
     69}
     70
     71/** Calculates the intersection of the two lines that are both on the same plane.
     72 * This is taken from Weisstein, Eric W. "Line-Line Intersection." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-LineIntersection.html
     73 * \param *out output stream for debugging
     74 * \param *Line1a first vector of first line
     75 * \param *Line1b second vector of first line
     76 * \param *Line2a first vector of second line
     77 * \param *Line2b second vector of second line
     78 * \return true - \a this will contain the intersection on return, false - lines are parallel
     79 */
     80Vector Line::getIntersection(const Line& otherLine) const{
     81  Info FunctionInfo(__func__);
     82
     83  pointset line1Points = getPointsOnLine();
     84
     85  Vector Line1a = line1Points[0];
     86  Vector Line1b = line1Points[1];
     87
     88  pointset line2Points = otherLine.getPointsOnLine();
     89
     90  Vector Line2a = line2Points[0];
     91  Vector Line2b = line2Points[1];
     92
     93  Vector res;
     94
     95  auto_ptr<GSLMatrix> M = auto_ptr<GSLMatrix>(new GSLMatrix(4,4));
     96
     97  M->SetAll(1.);
     98  for (int i=0;i<3;i++) {
     99    M->Set(0, i, Line1a[i]);
     100    M->Set(1, i, Line1b[i]);
     101    M->Set(2, i, Line2a[i]);
     102    M->Set(3, i, Line2b[i]);
     103  }
     104
     105  //Log() << Verbose(1) << "Coefficent matrix is:" << endl;
     106  //for (int i=0;i<4;i++) {
     107  //  for (int j=0;j<4;j++)
     108  //    cout << "\t" << M->Get(i,j);
     109  //  cout << endl;
     110  //}
     111  if (fabs(M->Determinant()) > MYEPSILON) {
     112    Log() << Verbose(1) << "Determinant of coefficient matrix is NOT zero." << endl;
     113    throw SkewException(__FILE__,__LINE__);
     114  }
     115
     116  Log() << Verbose(1) << "INFO: Line1a = " << Line1a << ", Line1b = " << Line1b << ", Line2a = " << Line2a << ", Line2b = " << Line2b << "." << endl;
     117
     118
     119  // constuct a,b,c
     120  Vector a = Line1b - Line1a;
     121  Vector b = Line2b - Line2a;
     122  Vector c = Line2a - Line1a;
     123  Vector d = Line2b - Line1b;
     124  Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl;
     125  if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) {
     126   res.Zero();
     127   Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl;
     128   throw LinearDependenceException(__FILE__,__LINE__);
     129  }
     130
     131  // check for parallelity
     132  Vector parallel;
     133  double factor = 0.;
     134  if (fabs(a.ScalarProduct(b)*a.ScalarProduct(b)/a.NormSquared()/b.NormSquared() - 1.) < MYEPSILON) {
     135    parallel = Line1a - Line2a;
     136    factor = parallel.ScalarProduct(a)/a.Norm();
     137    if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
     138      res = Line2a;
     139      Log() << Verbose(1) << "Lines conincide." << endl;
     140      return res;
     141    } else {
     142      parallel = Line1a - Line2b;
     143      factor = parallel.ScalarProduct(a)/a.Norm();
     144      if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
     145        res = Line2b;
     146        Log() << Verbose(1) << "Lines conincide." << endl;
     147        return res;
     148      }
     149    }
     150    Log() << Verbose(1) << "Lines are parallel." << endl;
     151    res.Zero();
     152    throw LinearDependenceException(__FILE__,__LINE__);
     153  }
     154
     155  // obtain s
     156  double s;
     157  Vector temp1, temp2;
     158  temp1 = c;
     159  temp1.VectorProduct(b);
     160  temp2 = a;
     161  temp2.VectorProduct(b);
     162  Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl;
     163  if (fabs(temp2.NormSquared()) > MYEPSILON)
     164    s = temp1.ScalarProduct(temp2)/temp2.NormSquared();
     165  else
     166    s = 0.;
     167  Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl;
     168
     169  // construct intersection
     170  res = a;
     171  res.Scale(s);
     172  res += Line1a;
     173  Log() << Verbose(1) << "Intersection is at " << res << "." << endl;
     174
     175  return res;
     176}
     177
     178/** Rotates the vector by an angle of \a alpha around this line.
     179 * \param rhs Vector to rotate
     180 * \param alpha rotation angle in radian
     181 */
     182Vector Line::rotateVector(const Vector &rhs, double alpha) const{
     183  Vector helper = rhs;
     184
     185  // translate the coordinate system so that the line goes through (0,0,0)
     186  helper -= *origin;
     187
     188  // partition the vector into a part that gets rotated and a part that lies along the line
     189  pair<Vector,Vector> parts = helper.partition(*direction);
     190
     191  // we just keep anything that is along the axis
     192  Vector res = parts.first;
     193
     194  // the rest has to be rotated
     195  Vector a = parts.second;
     196  // we only have to do the rest, if we actually could partition the vector
     197  if(!a.IsZero()){
     198    // construct a vector that is orthogonal to a and direction and has length |a|
     199    Vector y = a;
     200    // direction is normalized, so the result has length |a|
     201    y.VectorProduct(*direction);
     202
     203    res += cos(alpha) * a + sin(alpha) * y;
     204  }
     205
     206  // translate the coordinate system back
     207  res += *origin;
     208  return res;
     209}
     210
     211Plane Line::getOrthogonalPlane(const Vector &origin) const{
     212  return Plane(getDirection(),origin);
     213}
     214
     215Line makeLineThrough(const Vector &x1, const Vector &x2){
     216  if(x1==x2){
     217    throw LinearDependenceException(__FILE__,__LINE__);
     218  }
     219  return Line(x1,x1-x2);
     220}
  • src/Line.hpp

    rb32dbb ra7c344  
    1212
    1313#include <memory>
     14#include <vector>
    1415
    1516class Vector;
     17class Plane;
    1618
    1719class Line : public Space
    1820{
    1921public:
    20   Line(Vector &_origin, Vector &_direction);
     22  Line(const Vector &_origin, const Vector &_direction);
     23  Line(const Line& _src);
    2124  virtual ~Line();
    2225
    23   virtual double distance(const Vector &point) const=0;
    24   virtual Vector getClosestPoint(const Vector &point) const=0;
     26  virtual double distance(const Vector &point) const;
     27  virtual Vector getClosestPoint(const Vector &point) const;
     28
     29  Vector getDirection() const;
     30  Vector getOrigin() const;
     31
     32  std::vector<Vector> getPointsOnLine() const;
     33
     34  Vector getIntersection(const Line& otherLine) const;
     35
     36  Vector rotateVector(const Vector &rhs, double alpha) const;
     37
     38  Plane getOrthogonalPlane(const Vector &origin) const;
    2539
    2640private:
     
    2943};
    3044
     45/**
     46 * Named constructor to make a line through two points
     47 */
     48Line makeLineThrough(const Vector &x1, const Vector &x2);
     49
    3150#endif /* LINE_HPP_ */
  • src/Makefile.am

    rb32dbb ra7c344  
    8888                                  Exceptions/LinearDependenceException.cpp \
    8989                                  Exceptions/MathException.cpp \
     90                                  Exceptions/SkewException.cpp \
    9091                                  Exceptions/ZeroVectorException.cpp
    9192                                 
     
    9394                                  Exceptions/LinearDependenceException.hpp \
    9495                                  Exceptions/MathException.hpp \
     96                                  Exceptions/SkewException.hpp \
    9597                                  Exceptions/ZeroVectorException.hpp
    9698
  • src/Plane.cpp

    rb32dbb ra7c344  
    1414#include "Helpers/Assert.hpp"
    1515#include <cmath>
     16#include "Line.hpp"
     17#include "Exceptions/MultipleSolutionsException.hpp"
    1618
    1719/**
     
    4244/**
    4345 * Constructs a plane from two direction vectors and a offset.
    44  * If no offset is given a plane through origin is assumed
    4546 */
    4647Plane::Plane(const Vector &y1, const Vector &y2, double _offset) throw(ZeroVectorException,LinearDependenceException) :
     
    113114}
    114115
    115 Vector Plane::getOffsetVector() {
     116Vector Plane::getOffsetVector() const {
    116117  return getOffset()*getNormal();
    117118}
    118119
    119 vector<Vector> Plane::getPointsOnPlane(){
     120vector<Vector> Plane::getPointsOnPlane() const{
    120121  std::vector<Vector> res;
    121122  res.reserve(3);
     
    147148 * \return true -  \a this contains intersection point on return, false - line is parallel to plane (even if in-plane)
    148149 */
    149 Vector Plane::GetIntersection(const Vector &Origin, const Vector &LineVector)
     150Vector Plane::GetIntersection(const Line& line) const
    150151{
    151152  Info FunctionInfo(__func__);
    152153  Vector res;
    153154
    154   // find intersection of a line defined by Offset and Direction with a  plane defined by triangle
    155   Vector Direction = LineVector - Origin;
    156   Direction.Normalize();
    157   Log() << Verbose(1) << "INFO: Direction is " << Direction << "." << endl;
    158   //Log() << Verbose(1) << "INFO: PlaneNormal is " << *PlaneNormal << " and PlaneOffset is " << *PlaneOffset << "." << endl;
    159   double factor1 = Direction.ScalarProduct(*normalVector.get());
    160   if (fabs(factor1) < MYEPSILON) { // Uniqueness: line parallel to plane?
    161     Log() << Verbose(1) << "BAD: Line is parallel to plane, no intersection." << endl;
    162     throw LinearDependenceException(__FILE__,__LINE__);
    163   }
    164 
    165   double factor2 = Origin.ScalarProduct(*normalVector.get());
    166   if (fabs(factor2-offset) < MYEPSILON) { // Origin is in-plane
    167     Log() << Verbose(1) << "GOOD: Origin of line is in-plane." << endl;
    168     res = Origin;
    169     return res;
    170   }
    171 
     155  double factor1 = getNormal().ScalarProduct(line.getDirection());
     156  if(fabs(factor1)<MYEPSILON){
     157    // the plane is parallel... under all circumstances this is bad luck
     158    // we no have either no or infinite solutions
     159    if(isContained(line.getOrigin())){
     160      throw MultipleSolutionsException<Vector>(__FILE__,__LINE__,line.getOrigin());
     161    }
     162    else{
     163      throw LinearDependenceException(__FILE__,__LINE__);
     164    }
     165  }
     166
     167  double factor2 = getNormal().ScalarProduct(line.getOrigin());
    172168  double scaleFactor = (offset-factor2)/factor1;
    173169
    174   //factor = Origin->ScalarProduct(PlaneNormal)*(-PlaneOffset->ScalarProduct(PlaneNormal))/(Direction.ScalarProduct(PlaneNormal));
    175   Direction.Scale(scaleFactor);
    176   res = Origin + Direction;
    177   Log() << Verbose(1) << "INFO: Scaled direction is " << Direction << "." << endl;
    178 
    179   // test whether resulting vector really is on plane
    180   ASSERT(fabs(res.ScalarProduct(*normalVector) - offset) < MYEPSILON,
    181          "Calculated line-Plane intersection does not lie on plane.");
     170  res = line.getOrigin() + scaleFactor * line.getDirection();
     171
     172  // tests to make sure the resulting vector really is on plane and line
     173  ASSERT(isContained(res),"Calculated line-Plane intersection does not lie on plane.");
     174  ASSERT(line.isContained(res),"Calculated line-Plane intersection does not lie on line.");
    182175  return res;
    183176};
     177
     178Vector Plane::mirrorVector(const Vector &rhs) const {
     179  Vector helper = getVectorToPoint(rhs);
     180  // substract twice the Vector to the plane
     181  return rhs+2*helper;
     182}
     183
     184Line Plane::getOrthogonalLine(const Vector &origin) const{
     185  return Line(origin,getNormal());
     186}
    184187
    185188/************ Methods inherited from Space ****************/
  • src/Plane.hpp

    rb32dbb ra7c344  
    1717
    1818class Vector;
     19class Line;
    1920
    2021class Plane : public Space
     
    4243   * Same as getOffset()*getNormal();
    4344   */
    44   Vector getOffsetVector();
     45  Vector getOffsetVector() const;
    4546
    4647  /**
    4748   * returns three seperate points on this plane
    4849   */
    49   std::vector<Vector> getPointsOnPlane();
     50  std::vector<Vector> getPointsOnPlane() const;
    5051
    5152  // some calculations
    52   Vector GetIntersection(const Vector &Origin, const Vector &LineVector);
     53  Vector GetIntersection(const Line &Line) const;
     54
     55  Vector mirrorVector(const Vector &rhs) const;
     56
     57  /**
     58   * get a Line that is orthogonal to this plane, going through a chosen
     59   * point.
     60   *
     61   * The point does not have to lie on the plane itself.
     62   */
     63  Line getOrthogonalLine(const Vector &origin) const;
    5364
    5465  /****** Methods inherited from Space ***********/
  • src/Space.cpp

    rb32dbb ra7c344  
    1919}
    2020
     21Vector Space::getVectorToPoint(const Vector &origin) const{
     22  Vector support = getClosestPoint(origin);
     23  return support-origin;
     24}
     25
    2126bool Space::isContained(const Vector &point) const{
    2227  return (distance(point)) < MYEPSILON;
  • src/Space.hpp

    rb32dbb ra7c344  
    1717  virtual ~Space();
    1818
     19  /**
     20   * Calculates the distance between a Space and a Vector.
     21   */
    1922  virtual double distance(const Vector &point) const=0;
     23
     24  /**
     25   * get the closest point inside the space to another point
     26   */
    2027  virtual Vector getClosestPoint(const Vector &point) const=0;
     28
     29  /**
     30   * get the shortest Vector from a point to a Space.
     31   *
     32   * The Vector always points from the given Vector to the point in space
     33   * returned by Plane::getClosestPoint().
     34   */
     35  virtual Vector getVectorToPoint(const Vector &point) const;
     36
     37  /**
     38   * Test wether a point is contained in the space.
     39   *
     40   * returns true, when the point lies inside and false
     41   * otherwise.
     42   */
    2143  virtual bool isContained(const Vector &point) const;
     44
     45  /**
     46   * Tests if this space contains the center of the coordinate system.
     47   */
    2248  virtual bool hasZero() const;
    2349
  • src/atom.cpp

    rb32dbb ra7c344  
    6868atom *atom::GetTrueFather()
    6969{
    70   atom *walker = this;
    71   do {
    72     if (walker == walker->father) // top most father is the one that points on itself
    73       break;
    74     walker = walker->father;
    75   } while (walker != NULL);
    76   return walker;
     70  if(father == this){ // top most father is the one that points on itself
     71    return this;
     72  }
     73  else if(!father) {
     74    return 0;
     75  }
     76  else {
     77    return father->GetTrueFather();
     78  }
    7779};
    7880
  • src/boundary.cpp

    rb32dbb ra7c344  
    620620  for (TriangleMap::iterator runner = TesselStruct->TrianglesOnBoundary.begin(); runner != TesselStruct->TrianglesOnBoundary.end(); runner++)
    621621    { // go through every triangle, calculate volume of its pyramid with CoG as peak
    622       x = (*runner->second->endpoints[0]->node->node) - (*runner->second->endpoints[1]->node->node);
    623       y = (*runner->second->endpoints[0]->node->node) - (*runner->second->endpoints[2]->node->node);
    624       const double a = sqrt(runner->second->endpoints[0]->node->node->DistanceSquared(*runner->second->endpoints[1]->node->node));
    625       const double b = sqrt(runner->second->endpoints[0]->node->node->DistanceSquared(*runner->second->endpoints[2]->node->node));
    626       const double c = sqrt(runner->second->endpoints[2]->node->node->DistanceSquared(*runner->second->endpoints[1]->node->node));
     622      x = runner->second->getEndpoint(0) - runner->second->getEndpoint(1);
     623      y = runner->second->getEndpoint(0) - runner->second->getEndpoint(2);
     624      const double a = x.Norm();
     625      const double b = y.Norm();
     626      const double c = runner->second->getEndpoint(2).distance(runner->second->getEndpoint(1));
    627627      const double G = sqrt(((a + b + c) * (a + b + c) - 2 * (a * a + b * b + c * c)) / 16.); // area of tesselated triangle
    628       x = Plane(*(runner->second->endpoints[0]->node->node),
    629                 *(runner->second->endpoints[1]->node->node),
    630                 *(runner->second->endpoints[2]->node->node)).getNormal();
    631       x.Scale(runner->second->endpoints[1]->node->node->ScalarProduct(x));
     628      x = runner->second->getPlane().getNormal();
     629      x.Scale(runner->second->getEndpoint(1).ScalarProduct(x));
    632630      const double h = x.Norm(); // distance of CoG to triangle
    633631      const double PyramidVolume = (1. / 3.) * G * h; // this formula holds for _all_ pyramids (independent of n-edge base or (not) centered peak)
  • src/molecule_geometry.cpp

    rb32dbb ra7c344  
    1616#include "molecule.hpp"
    1717#include "World.hpp"
     18#include "Plane.hpp"
    1819
    1920/************************************* Functions for class molecule *********************************/
     
    251252void molecule::Mirror(const Vector *n)
    252253{
    253   ActOnAllVectors( &Vector::Mirror, *n );
     254  Plane p(*n,0);
     255  // TODO: replace with simpler construct (e.g. Boost::foreach)
     256  // once the structure of the atom list is fully reworked
     257  atom *Walker = start;
     258  while (Walker->next != end) {
     259    Walker = Walker->next;
     260    (*Walker->node) = p.mirrorVector(*Walker->node);
     261  }
    254262};
    255263
  • src/periodentafel.cpp

    rb32dbb ra7c344  
    212212
    213213  // fill elements DB
    214   strncpy(filename, path, MAXSTRINGSIZE);
    215   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    216   strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
     214  snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDELEMENTSDB);
    217215  infile.open(filename);
    218216  if (infile != NULL) {
     
    258256
    259257  // fill valence DB per element
    260   strncpy(filename, path, MAXSTRINGSIZE);
    261   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    262   strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
     258  snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDVALENCEDB);
    263259  infile.open(filename);
    264260  if (infile != NULL) {
     
    277273
    278274  // fill valence DB per element
    279   strncpy(filename, path, MAXSTRINGSIZE);
    280   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    281   strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
     275  snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDORBITALDB);
    282276  infile.open(filename);
    283277  if (infile != NULL) {
     
    296290
    297291  // fill H-BondDistance DB per element
    298   strncpy(filename, path, MAXSTRINGSIZE);
    299   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    300   strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
     292  snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDHBONDDISTANCEDB);
    301293  infile.open(filename);
    302294  if (infile != NULL) {
     
    318310
    319311  // fill H-BondAngle DB per element
    320   strncpy(filename, path, MAXSTRINGSIZE);
    321   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    322   strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
     312  snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDHBONDANGLEDB);
    323313  infile.open(filename);
    324314  if (infile != NULL) {
     
    363353  char filename[MAXSTRINGSIZE];
    364354
    365   strncpy(filename, path, MAXSTRINGSIZE);
    366   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    367   strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
     355  snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDELEMENTSDB);
    368356  f.open(filename);
    369357  if (f != NULL) {
  • src/tesselation.cpp

    rb32dbb ra7c344  
    1717#include "triangleintersectionlist.hpp"
    1818#include "vector.hpp"
     19#include "Line.hpp"
    1920#include "vector_ops.hpp"
    2021#include "verbose.hpp"
     
    469470
    470471  try {
    471     *Intersection = Plane(NormalVector, *(endpoints[0]->node->node)).GetIntersection(*MolCenter, *x);
    472   }
    473   catch (LinearDependenceException &excp) {
    474     Log() << Verbose(1) << excp;
    475     DoeLog(1) && (eLog() << Verbose(1) << "Alas! Intersection with plane failed - at least numerically - the intersection is not on the plane!" << endl);
    476     return false;
    477   }
    478 
    479   DoLog(1) && (Log() << Verbose(1) << "INFO: Triangle is " << *this << "." << endl);
    480   DoLog(1) && (Log() << Verbose(1) << "INFO: Line is from " << *MolCenter << " to " << *x << "." << endl);
    481   DoLog(1) && (Log() << Verbose(1) << "INFO: Intersection is " << *Intersection << "." << endl);
    482 
    483   if (Intersection->DistanceSquared(*endpoints[0]->node->node) < MYEPSILON) {
    484     DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with first endpoint." << endl);
    485     return true;
    486   }   else if (Intersection->DistanceSquared(*endpoints[1]->node->node) < MYEPSILON) {
    487     DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with second endpoint." << endl);
    488     return true;
    489   }   else if (Intersection->DistanceSquared(*endpoints[2]->node->node) < MYEPSILON) {
    490     DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with third endpoint." << endl);
    491     return true;
    492   }
    493   // Calculate cross point between one baseline and the line from the third endpoint to intersection
    494   int i = 0;
    495   do {
    496     try {
    497       CrossPoint = GetIntersectionOfTwoLinesOnPlane(*(endpoints[i%3]->node->node),
    498                                                     *(endpoints[(i+1)%3]->node->node),
    499                                                     *(endpoints[(i+2)%3]->node->node),
    500                                                     *Intersection);
     472    Line centerLine = makeLineThrough(*MolCenter, *x);
     473    *Intersection = Plane(NormalVector, *(endpoints[0]->node->node)).GetIntersection(centerLine);
     474
     475    DoLog(1) && (Log() << Verbose(1) << "INFO: Triangle is " << *this << "." << endl);
     476    DoLog(1) && (Log() << Verbose(1) << "INFO: Line is from " << *MolCenter << " to " << *x << "." << endl);
     477    DoLog(1) && (Log() << Verbose(1) << "INFO: Intersection is " << *Intersection << "." << endl);
     478
     479    if (Intersection->DistanceSquared(*endpoints[0]->node->node) < MYEPSILON) {
     480      DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with first endpoint." << endl);
     481      return true;
     482    }   else if (Intersection->DistanceSquared(*endpoints[1]->node->node) < MYEPSILON) {
     483      DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with second endpoint." << endl);
     484      return true;
     485    }   else if (Intersection->DistanceSquared(*endpoints[2]->node->node) < MYEPSILON) {
     486      DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with third endpoint." << endl);
     487      return true;
     488    }
     489    // Calculate cross point between one baseline and the line from the third endpoint to intersection
     490    int i = 0;
     491    do {
     492      Line line1 = makeLineThrough(*(endpoints[i%3]->node->node),*(endpoints[(i+1)%3]->node->node));
     493      Line line2 = makeLineThrough(*(endpoints[(i+2)%3]->node->node),*Intersection);
     494      CrossPoint = line1.getIntersection(line2);
    501495      helper = (*endpoints[(i+1)%3]->node->node) - (*endpoints[i%3]->node->node);
    502496      CrossPoint -= (*endpoints[i%3]->node->node);  // cross point was returned as absolute vector
     
    505499      if ((s < -MYEPSILON) || ((s-1.) > MYEPSILON)) {
    506500        DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << "outside of triangle." << endl);
    507         i=4;
    508         break;
     501        return false;
    509502      }
    510503      i++;
    511     } catch (LinearDependenceException &excp){
    512       break;
    513     }
    514   } while (i < 3);
    515   if (i == 3) {
     504    } while (i < 3);
    516505    DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " inside of triangle." << endl);
    517506    return true;
    518   } else {
    519     DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " outside of triangle." << endl);
     507  }
     508  catch (MathException &excp) {
     509    Log() << Verbose(1) << excp;
     510    DoeLog(1) && (eLog() << Verbose(1) << "Alas! Intersection with plane failed - at least numerically - the intersection is not on the plane!" << endl);
    520511    return false;
    521512  }
     
    544535  GetCenter(&Direction);
    545536  try {
    546     *ClosestPoint = Plane(NormalVector, *(endpoints[0]->node->node)).GetIntersection(*x, Direction);
    547   }
    548   catch (LinearDependenceException &excp) {
     537    Line l = makeLineThrough(*x, Direction);
     538    *ClosestPoint = Plane(NormalVector, *(endpoints[0]->node->node)).GetIntersection(l);
     539  }
     540  catch (MathException &excp) {
    549541    (*ClosestPoint) = (*x);
    550542  }
     
    569561    Direction = (*endpoints[(i+1)%3]->node->node) - (*endpoints[i%3]->node->node);
    570562    // calculate intersection, line can never be parallel to Direction (is the same vector as PlaneNormal);
    571     CrossPoint[i] = Plane(Direction, InPlane).GetIntersection(*(endpoints[i%3]->node->node), *(endpoints[(i+1)%3]->node->node));
     563    Line l = makeLineThrough(*(endpoints[i%3]->node->node), *(endpoints[(i+1)%3]->node->node));
     564    CrossPoint[i] = Plane(Direction, InPlane).GetIntersection(l);
    572565    CrossDirection[i] = CrossPoint[i] - InPlane;
    573566    CrossPoint[i] -= (*endpoints[i%3]->node->node);  // cross point was returned as absolute vector
     
    740733}
    741734
     735Vector BoundaryTriangleSet::getEndpoint(int i) const{
     736  ASSERT(i>=0 && i<3,"Index of Endpoint out of Range");
     737
     738  return *endpoints[i]->node->node;
     739}
     740
     741string BoundaryTriangleSet::getEndpointName(int i) const{
     742  ASSERT(i>=0 && i<3,"Index of Endpoint out of Range");
     743
     744  return endpoints[i]->node->getName();
     745}
     746
    742747/** output operator for BoundaryTriangleSet.
    743748 * \param &ost output stream
     
    746751ostream &operator <<(ostream &ost, const BoundaryTriangleSet &a)
    747752{
    748   ost << "[" << a.Nr << "|" << a.endpoints[0]->node->getName() << "," << a.endpoints[1]->node->getName() << "," << a.endpoints[2]->node->getName() << "]";
     753  ost << "[" << a.Nr << "|" << a.getEndpointName(0) << "," << a.getEndpointName(1) << "," << a.getEndpointName(2) << "]";
    749754  //  ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << ","
    750755  //      << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "," << a.endpoints[2]->node->Name << " at " << *a.endpoints[2]->node->node << "]";
     
    15141519        CenterVector.Zero();
    15151520        for (int i = 0; i < 3; i++)
    1516           CenterVector += (*BTS->endpoints[i]->node->node);
     1521          CenterVector += BTS->getEndpoint(i);
    15171522        CenterVector.Scale(1. / 3.);
    15181523        DoLog(2) && (Log() << Verbose(2) << "CenterVector of base triangle is " << CenterVector << endl);
     
    48684873  if (LastTriangle != NULL) {
    48694874    stringstream sstr;
    4870     sstr << "-"<< TrianglesOnBoundary.size() << "-" << LastTriangle->endpoints[0]->node->getName() << "_" << LastTriangle->endpoints[1]->node->getName() << "_" << LastTriangle->endpoints[2]->node->getName();
     4875    sstr << "-"<< TrianglesOnBoundary.size() << "-" << LastTriangle->getEndpointName(0) << "_" << LastTriangle->getEndpointName(1) << "_" << LastTriangle->getEndpointName(2);
    48714876    NumberName = sstr.str();
    48724877    if (DoTecplotOutput) {
  • src/tesselation.hpp

    rb32dbb ra7c344  
    171171
    172172    Plane getPlane() const;
     173    Vector getEndpoint(int) const;
     174    std::string getEndpointName(int) const;
    173175
    174176    class BoundaryPointSet *endpoints[3];
     
    177179    Vector SphereCenter;
    178180    int Nr;
     181
     182  private:
     183
    179184};
    180185
  • src/tesselationhelpers.cpp

    rb32dbb ra7c344  
    1515#include "tesselationhelpers.hpp"
    1616#include "vector.hpp"
     17#include "Line.hpp"
    1718#include "vector_ops.hpp"
    1819#include "verbose.hpp"
     
    724725  // calculate the intersection between this projected baseline and Base
    725726  Vector *Intersection = new Vector;
    726   *Intersection = GetIntersectionOfTwoLinesOnPlane(*(Base->endpoints[0]->node->node),
    727                                                    *(Base->endpoints[1]->node->node),
    728                                                      NewOffset, NewDirection);
     727  Line line1 = makeLineThrough(*(Base->endpoints[0]->node->node),*(Base->endpoints[1]->node->node));
     728  Line line2 = makeLineThrough(NewOffset, NewDirection);
     729  *Intersection = line1.getIntersection(line2);
    729730  Normal = (*Intersection) - (*Base->endpoints[0]->node->node);
    730731  DoLog(1) && (Log() << Verbose(1) << "Found closest point on " << *Base << " at " << *Intersection << ", factor in line is " << fabs(Normal.ScalarProduct(Baseline)/Baseline.NormSquared()) << "." << endl);
  • src/unittests/Makefile.am

    rb32dbb ra7c344  
    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/PlaneUnittest.cpp

    rb32dbb ra7c344  
    1717
    1818#include "vector.hpp"
     19#include "Line.hpp"
    1920
    2021CPPUNIT_TEST_SUITE_REGISTRATION( PlaneUnittest );
     
    153154  CPPUNIT_ASSERT(fabs(p4->distance(e1)-1) < MYEPSILON);
    154155  CPPUNIT_ASSERT_EQUAL(zeroVec,p4->getClosestPoint(e1));
    155 
    156 
    157 }
     156}
     157
     158void PlaneUnittest::mirrorTest(){
     159  Vector fixture;
     160
     161  // some Vectors that lie on the planes
     162  fixture = p1->mirrorVector(e1);
     163  CPPUNIT_ASSERT_EQUAL(fixture,e1);
     164  fixture = p1->mirrorVector(e2);
     165  CPPUNIT_ASSERT_EQUAL(fixture,e2);
     166  fixture = p1->mirrorVector(e3);
     167  CPPUNIT_ASSERT_EQUAL(fixture,e3);
     168
     169  fixture = p2->mirrorVector(zeroVec);
     170  CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
     171  fixture = p2->mirrorVector(e1);
     172  CPPUNIT_ASSERT_EQUAL(fixture,e1);
     173  fixture = p2->mirrorVector(e2);
     174  CPPUNIT_ASSERT_EQUAL(fixture,e2);
     175
     176  fixture = p3->mirrorVector(zeroVec);
     177  CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
     178  fixture = p3->mirrorVector(e1);
     179  CPPUNIT_ASSERT_EQUAL(fixture,e1);
     180  fixture = p3->mirrorVector(e3);
     181  CPPUNIT_ASSERT_EQUAL(fixture,e3);
     182
     183  fixture = p4->mirrorVector(zeroVec);
     184  CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
     185  fixture = p4->mirrorVector(e2);
     186  CPPUNIT_ASSERT_EQUAL(fixture,e2);
     187  fixture = p4->mirrorVector(e3);
     188  CPPUNIT_ASSERT_EQUAL(fixture,e3);
     189
     190  // some Vectors outside of the planes
     191  {
     192    Vector t = (2./3.)*(e1+e2+e3);
     193    fixture = p1->mirrorVector(zeroVec);
     194    CPPUNIT_ASSERT_EQUAL(fixture,t);
     195  }
     196
     197  fixture = p2->mirrorVector(e3);
     198  CPPUNIT_ASSERT_EQUAL(fixture,-1*e3);
     199  fixture = p3->mirrorVector(e2);
     200  CPPUNIT_ASSERT_EQUAL(fixture,-1*e2);
     201  fixture = p4->mirrorVector(e1);
     202  CPPUNIT_ASSERT_EQUAL(fixture,-1*e1);
     203}
     204
     205void PlaneUnittest::LineIntersectionTest(){
     206  Vector fixture;
     207  // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ???
     208  Line l1 = makeLineThrough(zeroVec,Vector(2,1,0));
     209  CPPUNIT_ASSERT_NO_THROW(fixture = Plane(e1, zeroVec).GetIntersection(l1) );
     210  CPPUNIT_ASSERT_EQUAL( zeroVec, fixture );
     211
     212  // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ???
     213  Line l2 = makeLineThrough(e1,Vector(0,1,1));
     214  CPPUNIT_ASSERT_NO_THROW(fixture = Plane(e2, Vector(2,1,0)).GetIntersection(l2) );
     215  CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture );
     216}
  • src/unittests/PlaneUnittest.hpp

    rb32dbb ra7c344  
    2020  CPPUNIT_TEST ( pointsTest );
    2121  CPPUNIT_TEST ( operationsTest );
     22  CPPUNIT_TEST ( mirrorTest );
     23  CPPUNIT_TEST ( LineIntersectionTest );
    2224  CPPUNIT_TEST_SUITE_END();
    2325
     
    3032  void pointsTest();
    3133  void operationsTest();
     34  void mirrorTest();
     35  void LineIntersectionTest();
    3236
    3337private:
  • src/unittests/vectorunittest.cpp

    rb32dbb ra7c344  
    216216}
    217217
    218 /** UnitTest for line intersections.
    219  */
    220 void VectorTest::LineIntersectionTest()
    221 {
    222   // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ???
    223   CPPUNIT_ASSERT_NO_THROW(fixture = Plane(unit, zero).GetIntersection(zero, two) );
    224   CPPUNIT_ASSERT_EQUAL( zero, fixture );
    225 
    226   // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ???
    227   CPPUNIT_ASSERT_NO_THROW(fixture = Plane(otherunit, two).GetIntersection( unit, notunit) );
    228   CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture );
    229 
    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 );
    253 };
    254 
    255 /** UnitTest for vector rotations.
    256  */
    257 void VectorTest::VectorRotationTest()
    258 {
    259   fixture = Vector(-1.,0.,0.);
    260 
    261   // zero vector does not change
    262   fixture = RotateVector(zero,unit, 1.);
    263   CPPUNIT_ASSERT_EQUAL( zero, fixture );
    264 
    265   fixture = RotateVector(zero, two, 1.);
    266   CPPUNIT_ASSERT_EQUAL( zero,  fixture);
    267 
    268   // vector on axis does not change
    269   fixture = RotateVector(unit,unit, 1.);
    270   CPPUNIT_ASSERT_EQUAL( unit, fixture );
    271 
    272   // rotations
    273   fixture = RotateVector(otherunit, unit, M_PI);
    274   CPPUNIT_ASSERT_EQUAL( Vector(0.,-1.,0.), fixture );
    275 
    276   fixture = RotateVector(otherunit, unit, 2. * M_PI);
    277   CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
    278 
    279   fixture = RotateVector(otherunit,unit, 0);
    280   CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
    281 
    282   fixture = RotateVector(Vector(0.,0.,1.), notunit, M_PI);
    283   CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
    284 }
    285218
    286219/**
  • src/unittests/vectorunittest.hpp

    rb32dbb ra7c344  
    2727    CPPUNIT_TEST ( ProjectionTest );
    2828    CPPUNIT_TEST ( NormalsTest );
    29     CPPUNIT_TEST ( LineIntersectionTest );
    30     CPPUNIT_TEST ( VectorRotationTest );
    3129    CPPUNIT_TEST ( IsInParallelepipedTest );
    3230    CPPUNIT_TEST_SUITE_END();
  • src/vector.cpp

    rb32dbb ra7c344  
    213213{
    214214  Vector tmp;
    215   tmp[0] = x[1]* (y[2]) - x[2]* (y[1]);
    216   tmp[1] = x[2]* (y[0]) - x[0]* (y[2]);
    217   tmp[2] = x[0]* (y[1]) - x[1]* (y[0]);
     215  tmp[0] = x[1]* y[2] - x[2]* y[1];
     216  tmp[1] = x[2]* y[0] - x[0]* y[2];
     217  tmp[2] = x[0]* y[1] - x[1]* y[0];
    218218  (*this) = tmp;
    219219};
     
    232232  *this -= tmp;
    233233};
    234 
    235 /** Calculates the minimum distance vector of this vector to the plane.
    236  * \param *out output stream for debugging
    237  * \param *PlaneNormal normal of plane
    238  * \param *PlaneOffset offset of plane
    239  * \return distance to plane
    240  * \return distance vector onto to plane
    241  */
    242 Vector Vector::GetDistanceVectorToPlane(const Vector &PlaneNormal, const Vector &PlaneOffset) const
    243 {
    244   Vector temp = (*this) - PlaneOffset;
    245   temp.MakeNormalTo(PlaneNormal);
    246   temp.Scale(-1.);
    247   // then add connecting vector from plane to point
    248   temp += (*this)-PlaneOffset;
    249   double sign = temp.ScalarProduct(PlaneNormal);
    250   if (fabs(sign) > MYEPSILON)
    251     sign /= fabs(sign);
    252   else
    253     sign = 0.;
    254 
    255   temp.Normalize();
    256   temp.Scale(sign);
    257   return temp;
    258 };
    259 
    260234
    261235/** Calculates the minimum distance of this vector to the plane.
     
    551525  MatrixMultiplication(M);
    552526};
     527
     528std::pair<Vector,Vector> Vector::partition(const Vector &rhs) const{
     529  double factor = ScalarProduct(rhs)/rhs.NormSquared();
     530  Vector res= factor * rhs;
     531  return make_pair(res,(*this)-res);
     532}
     533
     534std::pair<pointset,Vector> Vector::partition(const pointset &points) const{
     535  Vector helper = *this;
     536  pointset res;
     537  for(pointset::const_iterator iter=points.begin();iter!=points.end();++iter){
     538    pair<Vector,Vector> currPart = helper.partition(*iter);
     539    res.push_back(currPart.first);
     540    helper = currPart.second;
     541  }
     542  return make_pair(res,helper);
     543}
    553544
    554545/** Do a matrix multiplication.
     
    611602};
    612603
    613 /** Mirrors atom against a given plane.
    614  * \param n[] normal vector of mirror plane.
    615  */
    616 void Vector::Mirror(const Vector &n)
    617 {
    618   double projection;
    619   projection = ScalarProduct(n)/n.NormSquared();    // remove constancy from n (keep as logical one)
    620   // withdraw projected vector twice from original one
    621   for (int i=NDIM;i--;)
    622     at(i) -= 2.*projection*n[i];
    623 };
    624 
    625604/** Calculates orthonormal vector to one given vectors.
    626605 * Just subtracts the projection onto the given vector from this vector.
     
    633612  bool result = false;
    634613  double factor = y1.ScalarProduct(*this)/y1.NormSquared();
    635   Vector x1;
    636   x1 = factor * y1;
     614  Vector x1 = factor * y1;
    637615  SubtractVector(x1);
    638616  for (int i=NDIM;i--;)
  • src/vector.hpp

    rb32dbb ra7c344  
    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.
     
    3944
    4045  double DistanceSquared(const Vector &y) const;
    41   Vector GetDistanceVectorToPlane(const Vector &PlaneNormal, const Vector &PlaneOffset) const;
    4246  double DistanceToSpace(const Space& space) const;
    4347  double PeriodicDistance(const Vector &y, const double * const cell_size) const;
     
    5660  void ProjectIt(const Vector &y);
    5761  Vector Projection(const Vector &y) const;
    58   void Mirror(const Vector &x);
    5962  void ScaleAll(const double *factor);
    6063  void Scale(const double factor);
     
    6669  bool IsInParallelepiped(const Vector &offset, const double * const parallelepiped) const;
    6770  void WrapPeriodically(const double * const M, const double * const Minv);
     71  std::pair<Vector,Vector> partition(const Vector&) const;
     72  std::pair<pointset,Vector> partition(const pointset&) const;
    6873
    6974  // Accessors ussually come in pairs... and sometimes even more than that
  • src/vector_ops.cpp

    rb32dbb ra7c344  
    1515#include "Helpers/fast_functions.hpp"
    1616#include "Exceptions/LinearDependenceException.hpp"
     17#include "Exceptions/SkewException.hpp"
    1718
    1819#include <gsl/gsl_linalg.h>
     
    110111  return true;
    111112};
    112 
    113 /** Rotates the vector relative to the origin around the axis given by \a *axis by an angle of \a alpha.
    114  * \param *axis rotation axis
    115  * \param alpha rotation angle in radian
    116  */
    117 Vector RotateVector(const Vector &vec,const Vector &axis, const double alpha)
    118 {
    119   Vector a,y;
    120   Vector res;
    121   // normalise this vector with respect to axis
    122   a = vec;
    123   a.ProjectOntoPlane(axis);
    124   // construct normal vector
    125   try {
    126     y = Plane(axis,a,0).getNormal();
    127   }
    128   catch (MathException &excp) {
    129     // The normal vector cannot be created if there is linar dependency.
    130     // Then the vector to rotate is on the axis and any rotation leads to the vector itself.
    131     return vec;
    132   }
    133   y.Scale(vec.Norm());
    134   // scale normal vector by sine and this vector by cosine
    135   y.Scale(sin(alpha));
    136   a.Scale(cos(alpha));
    137   res = vec.Projection(axis);
    138   // add scaled normal vector onto this vector
    139   res += y;
    140   // add part in axis direction
    141   res += a;
    142   return res;
    143 };
    144 
    145 /** Calculates the intersection of the two lines that are both on the same plane.
    146  * This is taken from Weisstein, Eric W. "Line-Line Intersection." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-LineIntersection.html
    147  * \param *out output stream for debugging
    148  * \param *Line1a first vector of first line
    149  * \param *Line1b second vector of first line
    150  * \param *Line2a first vector of second line
    151  * \param *Line2b second vector of second line
    152  * \return true - \a this will contain the intersection on return, false - lines are parallel
    153  */
    154 Vector GetIntersectionOfTwoLinesOnPlane(const Vector &Line1a, const Vector &Line1b, const Vector &Line2a, const Vector &Line2b)
    155 {
    156   Info FunctionInfo(__func__);
    157 
    158   Vector res;
    159 
    160   auto_ptr<GSLMatrix> M = auto_ptr<GSLMatrix>(new GSLMatrix(4,4));
    161 
    162   M->SetAll(1.);
    163   for (int i=0;i<3;i++) {
    164     M->Set(0, i, Line1a[i]);
    165     M->Set(1, i, Line1b[i]);
    166     M->Set(2, i, Line2a[i]);
    167     M->Set(3, i, Line2b[i]);
    168   }
    169 
    170   //Log() << Verbose(1) << "Coefficent matrix is:" << endl;
    171   //for (int i=0;i<4;i++) {
    172   //  for (int j=0;j<4;j++)
    173   //    cout << "\t" << M->Get(i,j);
    174   //  cout << endl;
    175   //}
    176   if (fabs(M->Determinant()) > MYEPSILON) {
    177     Log() << Verbose(1) << "Determinant of coefficient matrix is NOT zero." << endl;
    178     throw LinearDependenceException(__FILE__,__LINE__);
    179   }
    180 
    181   Log() << Verbose(1) << "INFO: Line1a = " << Line1a << ", Line1b = " << Line1b << ", Line2a = " << Line2a << ", Line2b = " << Line2b << "." << endl;
    182 
    183 
    184   // constuct a,b,c
    185   Vector a = Line1b - Line1a;
    186   Vector b = Line2b - Line2a;
    187   Vector c = Line2a - Line1a;
    188   Vector d = Line2b - Line1b;
    189   Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl;
    190   if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) {
    191    res.Zero();
    192    Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl;
    193    throw LinearDependenceException(__FILE__,__LINE__);
    194   }
    195 
    196   // check for parallelity
    197   Vector parallel;
    198   double factor = 0.;
    199   if (fabs(a.ScalarProduct(b)*a.ScalarProduct(b)/a.NormSquared()/b.NormSquared() - 1.) < MYEPSILON) {
    200     parallel = Line1a - Line2a;
    201     factor = parallel.ScalarProduct(a)/a.Norm();
    202     if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
    203       res = Line2a;
    204       Log() << Verbose(1) << "Lines conincide." << endl;
    205       return res;
    206     } else {
    207       parallel = Line1a - Line2b;
    208       factor = parallel.ScalarProduct(a)/a.Norm();
    209       if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
    210         res = Line2b;
    211         Log() << Verbose(1) << "Lines conincide." << endl;
    212         return res;
    213       }
    214     }
    215     Log() << Verbose(1) << "Lines are parallel." << endl;
    216     res.Zero();
    217     throw LinearDependenceException(__FILE__,__LINE__);
    218   }
    219 
    220   // obtain s
    221   double s;
    222   Vector temp1, temp2;
    223   temp1 = c;
    224   temp1.VectorProduct(b);
    225   temp2 = a;
    226   temp2.VectorProduct(b);
    227   Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl;
    228   if (fabs(temp2.NormSquared()) > MYEPSILON)
    229     s = temp1.ScalarProduct(temp2)/temp2.NormSquared();
    230   else
    231     s = 0.;
    232   Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl;
    233 
    234   // construct intersection
    235   res = a;
    236   res.Scale(s);
    237   res += Line1a;
    238   Log() << Verbose(1) << "Intersection is at " << res << "." << endl;
    239 
    240   return res;
    241 };
  • src/vector_ops.hpp

    rb32dbb ra7c344  
    1010
    1111bool LSQdistance(Vector &res,const Vector **vectors, int num);
    12 Vector RotateVector(const Vector &vec,const Vector &axis, const double alpha);
    13 Vector GetIntersectionOfTwoLinesOnPlane(const Vector &Line1a, const Vector &Line1b, const Vector &Line2a, const Vector &Line2b);
    1412
    1513#endif /* VECTOR_OPS_HPP_ */
Note: See TracChangeset for help on using the changeset viewer.