source: src/Line.cpp@ ccf826

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
Last change on this file since ccf826 was 42a101, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Removed RotateVector() function in favor of Line::rotateVector() method.

Line::rotateVector() is able to rotate any vector around any line in space.

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