source: src/vector.cpp@ c26f44

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 c26f44 was a33931, checked in by Frederik Heber <heber@…>, 16 years ago

Merge branch 'ConcaveHull' into ConvexHull

Conflicts:

.gitignore
molecuilder/src/Makefile.am
molecuilder/src/atom.cpp
molecuilder/src/tesselation.cpp

no serious overlaps, just a free Frees that were not present in ConcaveHull were MemoryAllocator class was added.

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