source: src/vector.cpp@ 5417c5

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 5417c5 was 29812d, checked in by Saskia Metzler <metzler@…>, 16 years ago

Ticket 11: use templates and/or traits to fix Malloc/ReAlloc-Free warnings in a clean manner

  • Property mode set to 100755
File size: 37.0 KB
Line 
1/** \file vector.cpp
2 *
3 * Function implementations for the class vector.
4 *
5 */
6
7
8#include "defs.hpp"
9#include "helpers.hpp"
10#include "memoryallocator.hpp"
11#include "leastsquaremin.hpp"
12#include "vector.hpp"
13#include "verbose.hpp"
14
15/************************************ Functions for class vector ************************************/
16
17/** Constructor of class vector.
18 */
19Vector::Vector() { x[0] = x[1] = x[2] = 0.; };
20
21/** Constructor of class vector.
22 */
23Vector::Vector(double x1, double x2, double x3) { x[0] = x1; x[1] = x2; x[2] = x3; };
24
25/** Desctructor of class vector.
26 */
27Vector::~Vector() {};
28
29/** Calculates square of distance between this and another vector.
30 * \param *y array to second vector
31 * \return \f$| x - y |^2\f$
32 */
33double Vector::DistanceSquared(const Vector *y) const
34{
35 double res = 0.;
36 for (int i=NDIM;i--;)
37 res += (x[i]-y->x[i])*(x[i]-y->x[i]);
38 return (res);
39};
40
41/** Calculates distance between this and another vector.
42 * \param *y array to second vector
43 * \return \f$| x - y |\f$
44 */
45double Vector::Distance(const Vector *y) const
46{
47 double res = 0.;
48 for (int i=NDIM;i--;)
49 res += (x[i]-y->x[i])*(x[i]-y->x[i]);
50 return (sqrt(res));
51};
52
53/** Calculates distance between this and another vector in a periodic cell.
54 * \param *y array to second vector
55 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
56 * \return \f$| x - y |\f$
57 */
58double Vector::PeriodicDistance(const Vector *y, const double *cell_size) const
59{
60 double res = Distance(y), tmp, matrix[NDIM*NDIM];
61 Vector Shiftedy, TranslationVector;
62 int N[NDIM];
63 matrix[0] = cell_size[0];
64 matrix[1] = cell_size[1];
65 matrix[2] = cell_size[3];
66 matrix[3] = cell_size[1];
67 matrix[4] = cell_size[2];
68 matrix[5] = cell_size[4];
69 matrix[6] = cell_size[3];
70 matrix[7] = cell_size[4];
71 matrix[8] = cell_size[5];
72 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
73 for (N[0]=-1;N[0]<=1;N[0]++)
74 for (N[1]=-1;N[1]<=1;N[1]++)
75 for (N[2]=-1;N[2]<=1;N[2]++) {
76 // create the translation vector
77 TranslationVector.Zero();
78 for (int i=NDIM;i--;)
79 TranslationVector.x[i] = (double)N[i];
80 TranslationVector.MatrixMultiplication(matrix);
81 // add onto the original vector to compare with
82 Shiftedy.CopyVector(y);
83 Shiftedy.AddVector(&TranslationVector);
84 // get distance and compare with minimum so far
85 tmp = Distance(&Shiftedy);
86 if (tmp < res) res = tmp;
87 }
88 return (res);
89};
90
91/** Calculates distance between this and another vector in a periodic cell.
92 * \param *y array to second vector
93 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
94 * \return \f$| x - y |^2\f$
95 */
96double Vector::PeriodicDistanceSquared(const Vector *y, const double *cell_size) const
97{
98 double res = DistanceSquared(y), tmp, matrix[NDIM*NDIM];
99 Vector Shiftedy, TranslationVector;
100 int N[NDIM];
101 matrix[0] = cell_size[0];
102 matrix[1] = cell_size[1];
103 matrix[2] = cell_size[3];
104 matrix[3] = cell_size[1];
105 matrix[4] = cell_size[2];
106 matrix[5] = cell_size[4];
107 matrix[6] = cell_size[3];
108 matrix[7] = cell_size[4];
109 matrix[8] = cell_size[5];
110 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
111 for (N[0]=-1;N[0]<=1;N[0]++)
112 for (N[1]=-1;N[1]<=1;N[1]++)
113 for (N[2]=-1;N[2]<=1;N[2]++) {
114 // create the translation vector
115 TranslationVector.Zero();
116 for (int i=NDIM;i--;)
117 TranslationVector.x[i] = (double)N[i];
118 TranslationVector.MatrixMultiplication(matrix);
119 // add onto the original vector to compare with
120 Shiftedy.CopyVector(y);
121 Shiftedy.AddVector(&TranslationVector);
122 // get distance and compare with minimum so far
123 tmp = DistanceSquared(&Shiftedy);
124 if (tmp < res) res = tmp;
125 }
126 return (res);
127};
128
129/** Keeps the vector in a periodic cell, defined by the symmetric \a *matrix.
130 * \param *out ofstream for debugging messages
131 * Tries to translate a vector into each adjacent neighbouring cell.
132 */
133void Vector::KeepPeriodic(ofstream *out, double *matrix)
134{
135// int N[NDIM];
136// bool flag = false;
137 //vector Shifted, TranslationVector;
138 Vector TestVector;
139// *out << Verbose(1) << "Begin of KeepPeriodic." << endl;
140// *out << Verbose(2) << "Vector is: ";
141// Output(out);
142// *out << endl;
143 TestVector.CopyVector(this);
144 TestVector.InverseMatrixMultiplication(matrix);
145 for(int i=NDIM;i--;) { // correct periodically
146 if (TestVector.x[i] < 0) { // get every coefficient into the interval [0,1)
147 TestVector.x[i] += ceil(TestVector.x[i]);
148 } else {
149 TestVector.x[i] -= floor(TestVector.x[i]);
150 }
151 }
152 TestVector.MatrixMultiplication(matrix);
153 CopyVector(&TestVector);
154// *out << Verbose(2) << "New corrected vector is: ";
155// Output(out);
156// *out << endl;
157// *out << Verbose(1) << "End of KeepPeriodic." << endl;
158};
159
160/** Calculates scalar product between this and another vector.
161 * \param *y array to second vector
162 * \return \f$\langle x, y \rangle\f$
163 */
164double Vector::ScalarProduct(const Vector *y) const
165{
166 double res = 0.;
167 for (int i=NDIM;i--;)
168 res += x[i]*y->x[i];
169 return (res);
170};
171
172
173/** Calculates VectorProduct between this and another vector.
174 * -# returns the Product in place of vector from which it was initiated
175 * -# ATTENTION: Only three dim.
176 * \param *y array to vector with which to calculate crossproduct
177 * \return \f$ x \times y \f&
178 */
179void Vector::VectorProduct(const Vector *y)
180{
181 Vector tmp;
182 tmp.x[0] = x[1]* (y->x[2]) - x[2]* (y->x[1]);
183 tmp.x[1] = x[2]* (y->x[0]) - x[0]* (y->x[2]);
184 tmp.x[2] = x[0]* (y->x[1]) - x[1]* (y->x[0]);
185 this->CopyVector(&tmp);
186
187};
188
189
190/** projects this vector onto plane defined by \a *y.
191 * \param *y normal vector of plane
192 * \return \f$\langle x, y \rangle\f$
193 */
194void Vector::ProjectOntoPlane(const Vector *y)
195{
196 Vector tmp;
197 tmp.CopyVector(y);
198 tmp.Normalize();
199 tmp.Scale(ScalarProduct(&tmp));
200 this->SubtractVector(&tmp);
201};
202
203/** Calculates the intersection point between a line defined by \a *LineVector and \a *LineVector2 and a plane defined by \a *Normal and \a *PlaneOffset.
204 * According to [Bronstein] the vectorial plane equation is:
205 * -# \f$\stackrel{r}{\rightarrow} \cdot \stackrel{N}{\rightarrow} + D = 0\f$,
206 * where \f$\stackrel{r}{\rightarrow}\f$ is the vector to be testet, \f$\stackrel{N}{\rightarrow}\f$ is the plane's normal vector and
207 * \f$D = - \stackrel{a}{\rightarrow} \stackrel{N}{\rightarrow}\f$, the offset with respect to origin, if \f$\stackrel{a}{\rightarrow}\f$,
208 * is an offset vector onto the plane. The line is parametrized by \f$\stackrel{x}{\rightarrow} + k \stackrel{t}{\rightarrow}\f$, where
209 * \f$\stackrel{x}{\rightarrow}\f$ is the offset and \f$\stackrel{t}{\rightarrow}\f$ the directional vector (NOTE: No need to normalize
210 * the latter). Inserting the parametrized form into the plane equation and solving for \f$k\f$, which we insert then into the parametrization
211 * of the line yields the intersection point on the plane.
212 * \param *out output stream for debugging
213 * \param *PlaneNormal Plane's normal vector
214 * \param *PlaneOffset Plane's offset vector
215 * \param *Origin first vector of line
216 * \param *LineVector second vector of line
217 * \return true - \a this contains intersection point on return, false - line is parallel to plane
218 */
219bool Vector::GetIntersectionWithPlane(ofstream *out, Vector *PlaneNormal, Vector *PlaneOffset, Vector *Origin, Vector *LineVector)
220{
221 double factor;
222 Vector Direction, helper;
223
224 // find intersection of a line defined by Offset and Direction with a plane defined by triangle
225 Direction.CopyVector(LineVector);
226 Direction.SubtractVector(Origin);
227 //*out << Verbose(4) << "INFO: Direction is " << Direction << "." << endl;
228 factor = Direction.ScalarProduct(PlaneNormal);
229 if (factor < MYEPSILON) { // Uniqueness: line parallel to plane?
230 *out << Verbose(2) << "WARNING: Line is parallel to plane, no intersection." << endl;
231 return false;
232 }
233 helper.CopyVector(PlaneOffset);
234 helper.SubtractVector(Origin);
235 factor = helper.ScalarProduct(PlaneNormal)/factor;
236 //factor = Origin->ScalarProduct(PlaneNormal)*(-PlaneOffset->ScalarProduct(PlaneNormal))/(Direction.ScalarProduct(PlaneNormal));
237 Direction.Scale(factor);
238 CopyVector(Origin);
239 //*out << Verbose(4) << "INFO: Scaled direction is " << Direction << "." << endl;
240 AddVector(&Direction);
241
242 // test whether resulting vector really is on plane
243 helper.CopyVector(this);
244 helper.SubtractVector(PlaneOffset);
245 if (helper.ScalarProduct(PlaneNormal) < MYEPSILON) {
246 //*out << Verbose(2) << "INFO: Intersection at " << *this << " is good." << endl;
247 return true;
248 } else {
249 *out << Verbose(2) << "WARNING: Intersection point " << *this << " is not on plane." << endl;
250 return false;
251 }
252};
253
254/** Calculates the intersection of the two lines that are both on the same plane.
255 * We construct auxiliary plane with its vector normal to one line direction and the PlaneNormal, then a vector
256 * from the first line's offset onto the plane. Finally, scale by factor is 1/cos(angle(line1,line2..)) = 1/SP(...), and
257 * project onto the first line's direction and add its offset.
258 * \param *out output stream for debugging
259 * \param *Line1a first vector of first line
260 * \param *Line1b second vector of first line
261 * \param *Line2a first vector of second line
262 * \param *Line2b second vector of second line
263 * \param *PlaneNormal normal of plane, is supplemental/arbitrary
264 * \return true - \a this will contain the intersection on return, false - lines are parallel
265 */
266bool Vector::GetIntersectionOfTwoLinesOnPlane(ofstream *out, Vector *Line1a, Vector *Line1b, Vector *Line2a, Vector *Line2b, const Vector *PlaneNormal)
267{
268 bool result = true;
269 Vector Direction, OtherDirection;
270 Vector AuxiliaryNormal;
271 Vector Distance;
272 const Vector *Normal = NULL;
273 Vector *ConstructedNormal = NULL;
274 bool FreeNormal = false;
275
276 // construct both direction vectors
277 Zero();
278 Direction.CopyVector(Line1b);
279 Direction.SubtractVector(Line1a);
280 if (Direction.IsZero())
281 return false;
282 OtherDirection.CopyVector(Line2b);
283 OtherDirection.SubtractVector(Line2a);
284 if (OtherDirection.IsZero())
285 return false;
286
287 Direction.Normalize();
288 OtherDirection.Normalize();
289
290 //*out << Verbose(4) << "INFO: Normalized Direction " << Direction << " and OtherDirection " << OtherDirection << "." << endl;
291
292 if (fabs(OtherDirection.ScalarProduct(&Direction) - 1.) < MYEPSILON) { // lines are parallel
293 if ((Line1a == Line2a) || (Line1a == Line2b))
294 CopyVector(Line1a);
295 else if ((Line1b == Line2b) || (Line1b == Line2b))
296 CopyVector(Line1b);
297 else
298 return false;
299 *out << Verbose(4) << "INFO: Intersection is " << *this << "." << endl;
300 return true;
301 } else {
302 // check whether we have a plane normal vector
303 if (PlaneNormal == NULL) {
304 ConstructedNormal = new Vector;
305 ConstructedNormal->MakeNormalVector(&Direction, &OtherDirection);
306 Normal = ConstructedNormal;
307 FreeNormal = true;
308 } else
309 Normal = PlaneNormal;
310
311 AuxiliaryNormal.MakeNormalVector(&OtherDirection, Normal);
312 //*out << Verbose(4) << "INFO: PlaneNormal is " << *Normal << " and AuxiliaryNormal " << AuxiliaryNormal << "." << endl;
313
314 Distance.CopyVector(Line2a);
315 Distance.SubtractVector(Line1a);
316 //*out << Verbose(4) << "INFO: Distance is " << Distance << "." << endl;
317 if (Distance.IsZero()) {
318 // offsets are equal, match found
319 CopyVector(Line1a);
320 result = true;
321 } else {
322 CopyVector(Distance.Projection(&AuxiliaryNormal));
323 //*out << Verbose(4) << "INFO: Projected Distance is " << *this << "." << endl;
324 double factor = Direction.ScalarProduct(&AuxiliaryNormal);
325 //*out << Verbose(4) << "INFO: Scaling factor is " << factor << "." << endl;
326 Scale(1./(factor*factor));
327 //*out << Verbose(4) << "INFO: Scaled Distance is " << *this << "." << endl;
328 CopyVector(Projection(&Direction));
329 //*out << Verbose(4) << "INFO: Distance, projected into Direction, is " << *this << "." << endl;
330 if (this->IsZero())
331 result = false;
332 else
333 result = true;
334 AddVector(Line1a);
335 }
336
337 if (FreeNormal)
338 delete(ConstructedNormal);
339 }
340 if (result)
341 *out << Verbose(4) << "INFO: Intersection is " << *this << "." << endl;
342
343 return result;
344};
345
346/** Calculates the projection of a vector onto another \a *y.
347 * \param *y array to second vector
348 */
349void Vector::ProjectIt(const Vector *y)
350{
351 Vector helper(*y);
352 helper.Scale(-(ScalarProduct(y)));
353 AddVector(&helper);
354};
355
356/** Calculates the projection of a vector onto another \a *y.
357 * \param *y array to second vector
358 * \return Vector
359 */
360Vector Vector::Projection(const Vector *y) const
361{
362 Vector helper(*y);
363 helper.Scale((ScalarProduct(y)/y->NormSquared()));
364
365 return helper;
366};
367
368/** Calculates norm of this vector.
369 * \return \f$|x|\f$
370 */
371double Vector::Norm() const
372{
373 double res = 0.;
374 for (int i=NDIM;i--;)
375 res += this->x[i]*this->x[i];
376 return (sqrt(res));
377};
378
379/** Calculates squared norm of this vector.
380 * \return \f$|x|^2\f$
381 */
382double Vector::NormSquared() const
383{
384 return (ScalarProduct(this));
385};
386
387/** Normalizes this vector.
388 */
389void Vector::Normalize()
390{
391 double res = 0.;
392 for (int i=NDIM;i--;)
393 res += this->x[i]*this->x[i];
394 if (fabs(res) > MYEPSILON)
395 res = 1./sqrt(res);
396 Scale(&res);
397};
398
399/** Zeros all components of this vector.
400 */
401void Vector::Zero()
402{
403 for (int i=NDIM;i--;)
404 this->x[i] = 0.;
405};
406
407/** Zeros all components of this vector.
408 */
409void Vector::One(double one)
410{
411 for (int i=NDIM;i--;)
412 this->x[i] = one;
413};
414
415/** Initialises all components of this vector.
416 */
417void Vector::Init(double x1, double x2, double x3)
418{
419 x[0] = x1;
420 x[1] = x2;
421 x[2] = x3;
422};
423
424/** Checks whether vector has all components zero.
425 * @return true - vector is zero, false - vector is not
426 */
427bool Vector::IsZero() const
428{
429 return (fabs(x[0])+fabs(x[1])+fabs(x[2]) < MYEPSILON);
430};
431
432/** Checks whether vector has length of 1.
433 * @return true - vector is normalized, false - vector is not
434 */
435bool Vector::IsOne() const
436{
437 return (fabs(Norm() - 1.) < MYEPSILON);
438};
439
440/** Checks whether vector is normal to \a *normal.
441 * @return true - vector is normalized, false - vector is not
442 */
443bool Vector::IsNormalTo(const Vector *normal) const
444{
445 if (ScalarProduct(normal) < MYEPSILON)
446 return true;
447 else
448 return false;
449};
450
451/** Calculates the angle between this and another vector.
452 * \param *y array to second vector
453 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
454 */
455double Vector::Angle(const Vector *y) const
456{
457 double norm1 = Norm(), norm2 = y->Norm();
458 double angle = -1;
459 if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
460 angle = this->ScalarProduct(y)/norm1/norm2;
461 // -1-MYEPSILON occured due to numerical imprecision, catch ...
462 //cout << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
463 if (angle < -1)
464 angle = -1;
465 if (angle > 1)
466 angle = 1;
467 return acos(angle);
468};
469
470/** Rotates the vector relative to the origin around the axis given by \a *axis by an angle of \a alpha.
471 * \param *axis rotation axis
472 * \param alpha rotation angle in radian
473 */
474void Vector::RotateVector(const Vector *axis, const double alpha)
475{
476 Vector a,y;
477 // normalise this vector with respect to axis
478 a.CopyVector(this);
479 a.ProjectOntoPlane(axis);
480 // construct normal vector
481 bool rotatable = y.MakeNormalVector(axis,&a);
482 // The normal vector cannot be created if there is linar dependency.
483 // Then the vector to rotate is on the axis and any rotation leads to the vector itself.
484 if (!rotatable) {
485 return;
486 }
487 y.Scale(Norm());
488 // scale normal vector by sine and this vector by cosine
489 y.Scale(sin(alpha));
490 a.Scale(cos(alpha));
491 CopyVector(Projection(axis));
492 // add scaled normal vector onto this vector
493 AddVector(&y);
494 // add part in axis direction
495 AddVector(&a);
496};
497
498/** Compares vector \a to vector \a b component-wise.
499 * \param a base vector
500 * \param b vector components to add
501 * \return a == b
502 */
503bool operator==(const Vector& a, const Vector& b)
504{
505 bool status = true;
506 for (int i=0;i<NDIM;i++)
507 status = status && (fabs(a.x[i] - b.x[i]) < MYEPSILON);
508 return status;
509};
510
511/** Sums vector \a to this lhs component-wise.
512 * \param a base vector
513 * \param b vector components to add
514 * \return lhs + a
515 */
516Vector& operator+=(Vector& a, const Vector& b)
517{
518 a.AddVector(&b);
519 return a;
520};
521
522/** Subtracts vector \a from this lhs component-wise.
523 * \param a base vector
524 * \param b vector components to add
525 * \return lhs - a
526 */
527Vector& operator-=(Vector& a, const Vector& b)
528{
529 a.SubtractVector(&b);
530 return a;
531};
532
533/** factor each component of \a a times a double \a m.
534 * \param a base vector
535 * \param m factor
536 * \return lhs.x[i] * m
537 */
538Vector& operator*=(Vector& a, const double m)
539{
540 a.Scale(m);
541 return a;
542};
543
544/** Sums two vectors \a and \b component-wise.
545 * \param a first vector
546 * \param b second vector
547 * \return a + b
548 */
549Vector& operator+(const Vector& a, const Vector& b)
550{
551 Vector *x = new Vector;
552 x->CopyVector(&a);
553 x->AddVector(&b);
554 return *x;
555};
556
557/** Subtracts vector \a from \b component-wise.
558 * \param a first vector
559 * \param b second vector
560 * \return a - b
561 */
562Vector& operator-(const Vector& a, const Vector& b)
563{
564 Vector *x = new Vector;
565 x->CopyVector(&a);
566 x->SubtractVector(&b);
567 return *x;
568};
569
570/** Factors given vector \a a times \a m.
571 * \param a vector
572 * \param m factor
573 * \return m * a
574 */
575Vector& operator*(const Vector& a, const double m)
576{
577 Vector *x = new Vector;
578 x->CopyVector(&a);
579 x->Scale(m);
580 return *x;
581};
582
583/** Factors given vector \a a times \a m.
584 * \param m factor
585 * \param a vector
586 * \return m * a
587 */
588Vector& operator*(const double m, const Vector& a )
589{
590 Vector *x = new Vector;
591 x->CopyVector(&a);
592 x->Scale(m);
593 return *x;
594};
595
596/** Prints a 3dim vector.
597 * prints no end of line.
598 * \param *out output stream
599 */
600bool Vector::Output(ofstream *out) const
601{
602 if (out != NULL) {
603 *out << "(";
604 for (int i=0;i<NDIM;i++) {
605 *out << x[i];
606 if (i != 2)
607 *out << ",";
608 }
609 *out << ")";
610 return true;
611 } else
612 return false;
613};
614
615ostream& operator<<(ostream& ost, const Vector& m)
616{
617 ost << "(";
618 for (int i=0;i<NDIM;i++) {
619 ost << m.x[i];
620 if (i != 2)
621 ost << ",";
622 }
623 ost << ")";
624 return ost;
625};
626
627/** Scales each atom coordinate by an individual \a factor.
628 * \param *factor pointer to scaling factor
629 */
630void Vector::Scale(double **factor)
631{
632 for (int i=NDIM;i--;)
633 x[i] *= (*factor)[i];
634};
635
636void Vector::Scale(double *factor)
637{
638 for (int i=NDIM;i--;)
639 x[i] *= *factor;
640};
641
642void Vector::Scale(double factor)
643{
644 for (int i=NDIM;i--;)
645 x[i] *= factor;
646};
647
648/** Translate atom by given vector.
649 * \param trans[] translation vector.
650 */
651void Vector::Translate(const Vector *trans)
652{
653 for (int i=NDIM;i--;)
654 x[i] += trans->x[i];
655};
656
657/** Do a matrix multiplication.
658 * \param *matrix NDIM_NDIM array
659 */
660void Vector::MatrixMultiplication(double *M)
661{
662 Vector C;
663 // do the matrix multiplication
664 C.x[0] = M[0]*x[0]+M[3]*x[1]+M[6]*x[2];
665 C.x[1] = M[1]*x[0]+M[4]*x[1]+M[7]*x[2];
666 C.x[2] = M[2]*x[0]+M[5]*x[1]+M[8]*x[2];
667 // transfer the result into this
668 for (int i=NDIM;i--;)
669 x[i] = C.x[i];
670};
671
672/** Calculate the inverse of a 3x3 matrix.
673 * \param *matrix NDIM_NDIM array
674 */
675double * Vector::InverseMatrix(double *A)
676{
677 double *B = Malloc<double>(NDIM * NDIM, "Vector::InverseMatrix: *B");
678 double detA = RDET3(A);
679 double detAReci;
680
681 for (int i=0;i<NDIM*NDIM;++i)
682 B[i] = 0.;
683 // calculate the inverse B
684 if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
685 detAReci = 1./detA;
686 B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
687 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
688 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
689 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
690 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
691 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
692 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
693 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
694 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
695 }
696 return B;
697};
698
699/** Do a matrix multiplication with the \a *A' inverse.
700 * \param *matrix NDIM_NDIM array
701 */
702void Vector::InverseMatrixMultiplication(double *A)
703{
704 Vector C;
705 double B[NDIM*NDIM];
706 double detA = RDET3(A);
707 double detAReci;
708
709 // calculate the inverse B
710 if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
711 detAReci = 1./detA;
712 B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
713 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
714 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
715 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
716 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
717 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
718 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
719 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
720 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
721
722 // do the matrix multiplication
723 C.x[0] = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
724 C.x[1] = B[1]*x[0]+B[4]*x[1]+B[7]*x[2];
725 C.x[2] = B[2]*x[0]+B[5]*x[1]+B[8]*x[2];
726 // transfer the result into this
727 for (int i=NDIM;i--;)
728 x[i] = C.x[i];
729 } else {
730 cerr << "ERROR: inverse of matrix does not exists: det A = " << detA << "." << endl;
731 }
732};
733
734
735/** Creates this vector as the b y *factors' components scaled linear combination of the given three.
736 * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
737 * \param *x1 first vector
738 * \param *x2 second vector
739 * \param *x3 third vector
740 * \param *factors three-component vector with the factor for each given vector
741 */
742void Vector::LinearCombinationOfVectors(const Vector *x1, const Vector *x2, const Vector *x3, double *factors)
743{
744 for(int i=NDIM;i--;)
745 x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i];
746};
747
748/** Mirrors atom against a given plane.
749 * \param n[] normal vector of mirror plane.
750 */
751void Vector::Mirror(const Vector *n)
752{
753 double projection;
754 projection = ScalarProduct(n)/n->ScalarProduct(n); // remove constancy from n (keep as logical one)
755 // withdraw projected vector twice from original one
756 cout << Verbose(1) << "Vector: ";
757 Output((ofstream *)&cout);
758 cout << "\t";
759 for (int i=NDIM;i--;)
760 x[i] -= 2.*projection*n->x[i];
761 cout << "Projected vector: ";
762 Output((ofstream *)&cout);
763 cout << endl;
764};
765
766/** Calculates normal vector for three given vectors (being three points in space).
767 * Makes this vector orthonormal to the three given points, making up a place in 3d space.
768 * \param *y1 first vector
769 * \param *y2 second vector
770 * \param *y3 third vector
771 * \return true - success, vectors are linear independent, false - failure due to linear dependency
772 */
773bool Vector::MakeNormalVector(const Vector *y1, const Vector *y2, const Vector *y3)
774{
775 Vector x1, x2;
776
777 x1.CopyVector(y1);
778 x1.SubtractVector(y2);
779 x2.CopyVector(y3);
780 x2.SubtractVector(y2);
781 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) {
782 cout << Verbose(4) << "WARNING: Given vectors are linear dependent." << endl;
783 return false;
784 }
785// cout << Verbose(4) << "relative, first plane coordinates:";
786// x1.Output((ofstream *)&cout);
787// cout << endl;
788// cout << Verbose(4) << "second plane coordinates:";
789// x2.Output((ofstream *)&cout);
790// cout << endl;
791
792 this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
793 this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
794 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
795 Normalize();
796
797 return true;
798};
799
800
801/** Calculates orthonormal vector to two given vectors.
802 * Makes this vector orthonormal to two given vectors. This is very similar to the other
803 * vector::MakeNormalVector(), only there three points whereas here two difference
804 * vectors are given.
805 * \param *x1 first vector
806 * \param *x2 second vector
807 * \return true - success, vectors are linear independent, false - failure due to linear dependency
808 */
809bool Vector::MakeNormalVector(const Vector *y1, const Vector *y2)
810{
811 Vector x1,x2;
812 x1.CopyVector(y1);
813 x2.CopyVector(y2);
814 Zero();
815 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) {
816 cout << Verbose(4) << "WARNING: Given vectors are linear dependent." << endl;
817 return false;
818 }
819// cout << Verbose(4) << "relative, first plane coordinates:";
820// x1.Output((ofstream *)&cout);
821// cout << endl;
822// cout << Verbose(4) << "second plane coordinates:";
823// x2.Output((ofstream *)&cout);
824// cout << endl;
825
826 this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
827 this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
828 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
829 Normalize();
830
831 return true;
832};
833
834/** Calculates orthonormal vector to one given vectors.
835 * Just subtracts the projection onto the given vector from this vector.
836 * The removed part of the vector is Vector::Projection()
837 * \param *x1 vector
838 * \return true - success, false - vector is zero
839 */
840bool Vector::MakeNormalVector(const Vector *y1)
841{
842 bool result = false;
843 double factor = y1->ScalarProduct(this)/y1->NormSquared();
844 Vector x1;
845 x1.CopyVector(y1);
846 x1.Scale(factor);
847 SubtractVector(&x1);
848 for (int i=NDIM;i--;)
849 result = result || (fabs(x[i]) > MYEPSILON);
850
851 return result;
852};
853
854/** Creates this vector as one of the possible orthonormal ones to the given one.
855 * Just scan how many components of given *vector are unequal to zero and
856 * try to get the skp of both to be zero accordingly.
857 * \param *vector given vector
858 * \return true - success, false - failure (null vector given)
859 */
860bool Vector::GetOneNormalVector(const Vector *GivenVector)
861{
862 int Components[NDIM]; // contains indices of non-zero components
863 int Last = 0; // count the number of non-zero entries in vector
864 int j; // loop variables
865 double norm;
866
867 cout << Verbose(4);
868 GivenVector->Output((ofstream *)&cout);
869 cout << endl;
870 for (j=NDIM;j--;)
871 Components[j] = -1;
872 // find two components != 0
873 for (j=0;j<NDIM;j++)
874 if (fabs(GivenVector->x[j]) > MYEPSILON)
875 Components[Last++] = j;
876 cout << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl;
877
878 switch(Last) {
879 case 3: // threecomponent system
880 case 2: // two component system
881 norm = sqrt(1./(GivenVector->x[Components[1]]*GivenVector->x[Components[1]]) + 1./(GivenVector->x[Components[0]]*GivenVector->x[Components[0]]));
882 x[Components[2]] = 0.;
883 // in skp both remaining parts shall become zero but with opposite sign and third is zero
884 x[Components[1]] = -1./GivenVector->x[Components[1]] / norm;
885 x[Components[0]] = 1./GivenVector->x[Components[0]] / norm;
886 return true;
887 break;
888 case 1: // one component system
889 // set sole non-zero component to 0, and one of the other zero component pendants to 1
890 x[(Components[0]+2)%NDIM] = 0.;
891 x[(Components[0]+1)%NDIM] = 1.;
892 x[Components[0]] = 0.;
893 return true;
894 break;
895 default:
896 return false;
897 }
898};
899
900/** Determines parameter needed to multiply this vector to obtain intersection point with plane defined by \a *A, \a *B and \a *C.
901 * \param *A first plane vector
902 * \param *B second plane vector
903 * \param *C third plane vector
904 * \return scaling parameter for this vector
905 */
906double Vector::CutsPlaneAt(Vector *A, Vector *B, Vector *C)
907{
908// cout << Verbose(3) << "For comparison: ";
909// cout << "A " << A->Projection(this) << "\t";
910// cout << "B " << B->Projection(this) << "\t";
911// cout << "C " << C->Projection(this) << "\t";
912// cout << endl;
913 return A->ScalarProduct(this);
914};
915
916/** Creates a new vector as the one with least square distance to a given set of \a vectors.
917 * \param *vectors set of vectors
918 * \param num number of vectors
919 * \return true if success, false if failed due to linear dependency
920 */
921bool Vector::LSQdistance(Vector **vectors, int num)
922{
923 int j;
924
925 for (j=0;j<num;j++) {
926 cout << Verbose(1) << j << "th atom's vector: ";
927 (vectors[j])->Output((ofstream *)&cout);
928 cout << endl;
929 }
930
931 int np = 3;
932 struct LSQ_params par;
933
934 const gsl_multimin_fminimizer_type *T =
935 gsl_multimin_fminimizer_nmsimplex;
936 gsl_multimin_fminimizer *s = NULL;
937 gsl_vector *ss, *y;
938 gsl_multimin_function minex_func;
939
940 size_t iter = 0, i;
941 int status;
942 double size;
943
944 /* Initial vertex size vector */
945 ss = gsl_vector_alloc (np);
946 y = gsl_vector_alloc (np);
947
948 /* Set all step sizes to 1 */
949 gsl_vector_set_all (ss, 1.0);
950
951 /* Starting point */
952 par.vectors = vectors;
953 par.num = num;
954
955 for (i=NDIM;i--;)
956 gsl_vector_set(y, i, (vectors[0]->x[i] - vectors[1]->x[i])/2.);
957
958 /* Initialize method and iterate */
959 minex_func.f = &LSQ;
960 minex_func.n = np;
961 minex_func.params = (void *)&par;
962
963 s = gsl_multimin_fminimizer_alloc (T, np);
964 gsl_multimin_fminimizer_set (s, &minex_func, y, ss);
965
966 do
967 {
968 iter++;
969 status = gsl_multimin_fminimizer_iterate(s);
970
971 if (status)
972 break;
973
974 size = gsl_multimin_fminimizer_size (s);
975 status = gsl_multimin_test_size (size, 1e-2);
976
977 if (status == GSL_SUCCESS)
978 {
979 printf ("converged to minimum at\n");
980 }
981
982 printf ("%5d ", (int)iter);
983 for (i = 0; i < (size_t)np; i++)
984 {
985 printf ("%10.3e ", gsl_vector_get (s->x, i));
986 }
987 printf ("f() = %7.3f size = %.3f\n", s->fval, size);
988 }
989 while (status == GSL_CONTINUE && iter < 100);
990
991 for (i=(size_t)np;i--;)
992 this->x[i] = gsl_vector_get(s->x, i);
993 gsl_vector_free(y);
994 gsl_vector_free(ss);
995 gsl_multimin_fminimizer_free (s);
996
997 return true;
998};
999
1000/** Adds vector \a *y componentwise.
1001 * \param *y vector
1002 */
1003void Vector::AddVector(const Vector *y)
1004{
1005 for (int i=NDIM;i--;)
1006 this->x[i] += y->x[i];
1007}
1008
1009/** Adds vector \a *y componentwise.
1010 * \param *y vector
1011 */
1012void Vector::SubtractVector(const Vector *y)
1013{
1014 for (int i=NDIM;i--;)
1015 this->x[i] -= y->x[i];
1016}
1017
1018/** Copy vector \a *y componentwise.
1019 * \param *y vector
1020 */
1021void Vector::CopyVector(const Vector *y)
1022{
1023 for (int i=NDIM;i--;)
1024 this->x[i] = y->x[i];
1025}
1026
1027/** Copy vector \a y componentwise.
1028 * \param y vector
1029 */
1030void Vector::CopyVector(const Vector y)
1031{
1032 for (int i=NDIM;i--;)
1033 this->x[i] = y.x[i];
1034}
1035
1036
1037/** Asks for position, checks for boundary.
1038 * \param cell_size unitary size of cubic cell, coordinates must be within 0...cell_size
1039 * \param check whether bounds shall be checked (true) or not (false)
1040 */
1041void Vector::AskPosition(double *cell_size, bool check)
1042{
1043 char coords[3] = {'x','y','z'};
1044 int j = -1;
1045 for (int i=0;i<3;i++) {
1046 j += i+1;
1047 do {
1048 cout << Verbose(0) << coords[i] << "[0.." << cell_size[j] << "]: ";
1049 cin >> x[i];
1050 } while (((x[i] < 0) || (x[i] >= cell_size[j])) && (check));
1051 }
1052};
1053
1054/** Solves a vectorial system consisting of two orthogonal statements and a norm statement.
1055 * This is linear system of equations to be solved, however of the three given (skp of this vector\
1056 * with either of the three hast to be zero) only two are linear independent. The third equation
1057 * is that the vector should be of magnitude 1 (orthonormal). This all leads to a case-based solution
1058 * where very often it has to be checked whether a certain value is zero or not and thus forked into
1059 * another case.
1060 * \param *x1 first vector
1061 * \param *x2 second vector
1062 * \param *y third vector
1063 * \param alpha first angle
1064 * \param beta second angle
1065 * \param c norm of final vector
1066 * \return a vector with \f$\langle x1,x2 \rangle=A\f$, \f$\langle x1,y \rangle = B\f$ and with norm \a c.
1067 * \bug this is not yet working properly
1068 */
1069bool Vector::SolveSystem(Vector *x1, Vector *x2, Vector *y, double alpha, double beta, double c)
1070{
1071 double D1,D2,D3,E1,E2,F1,F2,F3,p,q=0., A, B1, B2, C;
1072 double ang; // angle on testing
1073 double sign[3];
1074 int i,j,k;
1075 A = cos(alpha) * x1->Norm() * c;
1076 B1 = cos(beta + M_PI/2.) * y->Norm() * c;
1077 B2 = cos(beta) * x2->Norm() * c;
1078 C = c * c;
1079 cout << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl;
1080 int flag = 0;
1081 if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping
1082 if (fabs(x1->x[1]) > MYEPSILON) {
1083 flag = 1;
1084 } else if (fabs(x1->x[2]) > MYEPSILON) {
1085 flag = 2;
1086 } else {
1087 return false;
1088 }
1089 }
1090 switch (flag) {
1091 default:
1092 case 0:
1093 break;
1094 case 2:
1095 flip(&x1->x[0],&x1->x[1]);
1096 flip(&x2->x[0],&x2->x[1]);
1097 flip(&y->x[0],&y->x[1]);
1098 //flip(&x[0],&x[1]);
1099 flip(&x1->x[1],&x1->x[2]);
1100 flip(&x2->x[1],&x2->x[2]);
1101 flip(&y->x[1],&y->x[2]);
1102 //flip(&x[1],&x[2]);
1103 case 1:
1104 flip(&x1->x[0],&x1->x[1]);
1105 flip(&x2->x[0],&x2->x[1]);
1106 flip(&y->x[0],&y->x[1]);
1107 //flip(&x[0],&x[1]);
1108 flip(&x1->x[1],&x1->x[2]);
1109 flip(&x2->x[1],&x2->x[2]);
1110 flip(&y->x[1],&y->x[2]);
1111 //flip(&x[1],&x[2]);
1112 break;
1113 }
1114 // now comes the case system
1115 D1 = -y->x[0]/x1->x[0]*x1->x[1]+y->x[1];
1116 D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2];
1117 D3 = y->x[0]/x1->x[0]*A-B1;
1118 cout << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n";
1119 if (fabs(D1) < MYEPSILON) {
1120 cout << Verbose(2) << "D1 == 0!\n";
1121 if (fabs(D2) > MYEPSILON) {
1122 cout << Verbose(3) << "D2 != 0!\n";
1123 x[2] = -D3/D2;
1124 E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2;
1125 E2 = -x1->x[1]/x1->x[0];
1126 cout << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n";
1127 F1 = E1*E1 + 1.;
1128 F2 = -E1*E2;
1129 F3 = E1*E1 + D3*D3/(D2*D2) - C;
1130 cout << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
1131 if (fabs(F1) < MYEPSILON) {
1132 cout << Verbose(4) << "F1 == 0!\n";
1133 cout << Verbose(4) << "Gleichungssystem linear\n";
1134 x[1] = F3/(2.*F2);
1135 } else {
1136 p = F2/F1;
1137 q = p*p - F3/F1;
1138 cout << Verbose(4) << "p " << p << "\tq " << q << endl;
1139 if (q < 0) {
1140 cout << Verbose(4) << "q < 0" << endl;
1141 return false;
1142 }
1143 x[1] = p + sqrt(q);
1144 }
1145 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
1146 } else {
1147 cout << Verbose(2) << "Gleichungssystem unterbestimmt\n";
1148 return false;
1149 }
1150 } else {
1151 E1 = A/x1->x[0]+x1->x[1]/x1->x[0]*D3/D1;
1152 E2 = x1->x[1]/x1->x[0]*D2/D1 - x1->x[2];
1153 cout << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n";
1154 F1 = E2*E2 + D2*D2/(D1*D1) + 1.;
1155 F2 = -(E1*E2 + D2*D3/(D1*D1));
1156 F3 = E1*E1 + D3*D3/(D1*D1) - C;
1157 cout << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
1158 if (fabs(F1) < MYEPSILON) {
1159 cout << Verbose(3) << "F1 == 0!\n";
1160 cout << Verbose(3) << "Gleichungssystem linear\n";
1161 x[2] = F3/(2.*F2);
1162 } else {
1163 p = F2/F1;
1164 q = p*p - F3/F1;
1165 cout << Verbose(3) << "p " << p << "\tq " << q << endl;
1166 if (q < 0) {
1167 cout << Verbose(3) << "q < 0" << endl;
1168 return false;
1169 }
1170 x[2] = p + sqrt(q);
1171 }
1172 x[1] = (-D2 * x[2] - D3)/D1;
1173 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
1174 }
1175 switch (flag) { // back-flipping
1176 default:
1177 case 0:
1178 break;
1179 case 2:
1180 flip(&x1->x[0],&x1->x[1]);
1181 flip(&x2->x[0],&x2->x[1]);
1182 flip(&y->x[0],&y->x[1]);
1183 flip(&x[0],&x[1]);
1184 flip(&x1->x[1],&x1->x[2]);
1185 flip(&x2->x[1],&x2->x[2]);
1186 flip(&y->x[1],&y->x[2]);
1187 flip(&x[1],&x[2]);
1188 case 1:
1189 flip(&x1->x[0],&x1->x[1]);
1190 flip(&x2->x[0],&x2->x[1]);
1191 flip(&y->x[0],&y->x[1]);
1192 //flip(&x[0],&x[1]);
1193 flip(&x1->x[1],&x1->x[2]);
1194 flip(&x2->x[1],&x2->x[2]);
1195 flip(&y->x[1],&y->x[2]);
1196 flip(&x[1],&x[2]);
1197 break;
1198 }
1199 // one z component is only determined by its radius (without sign)
1200 // thus check eight possible sign flips and determine by checking angle with second vector
1201 for (i=0;i<8;i++) {
1202 // set sign vector accordingly
1203 for (j=2;j>=0;j--) {
1204 k = (i & pot(2,j)) << j;
1205 cout << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl;
1206 sign[j] = (k == 0) ? 1. : -1.;
1207 }
1208 cout << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";
1209 // apply sign matrix
1210 for (j=NDIM;j--;)
1211 x[j] *= sign[j];
1212 // calculate angle and check
1213 ang = x2->Angle (this);
1214 cout << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t";
1215 if (fabs(ang - cos(beta)) < MYEPSILON) {
1216 break;
1217 }
1218 // unapply sign matrix (is its own inverse)
1219 for (j=NDIM;j--;)
1220 x[j] *= sign[j];
1221 }
1222 return true;
1223};
1224
1225/**
1226 * Checks whether this vector is within the parallelepiped defined by the given three vectors and
1227 * their offset.
1228 *
1229 * @param offest for the origin of the parallelepiped
1230 * @param three vectors forming the matrix that defines the shape of the parallelpiped
1231 */
1232bool Vector::IsInParallelepiped(Vector offset, double *parallelepiped)
1233{
1234 Vector a;
1235 a.CopyVector(this);
1236 a.SubtractVector(&offset);
1237 a.InverseMatrixMultiplication(parallelepiped);
1238 bool isInside = true;
1239
1240 for (int i=NDIM;i--;)
1241 isInside = isInside && ((a.x[i] <= 1) && (a.x[i] >= 0));
1242
1243 return isInside;
1244}
Note: See TracBrowser for help on using the repository browser.