1 | /** \file vector.cpp
|
---|
2 | *
|
---|
3 | * Function implementations for the class vector.
|
---|
4 | *
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "Helpers/MemDebug.hpp"
|
---|
8 |
|
---|
9 | #include "vector.hpp"
|
---|
10 | #include "verbose.hpp"
|
---|
11 | #include "World.hpp"
|
---|
12 | #include "Helpers/Assert.hpp"
|
---|
13 | #include "Helpers/fast_functions.hpp"
|
---|
14 |
|
---|
15 | #include <iostream>
|
---|
16 |
|
---|
17 | using namespace std;
|
---|
18 |
|
---|
19 |
|
---|
20 | /************************************ Functions for class vector ************************************/
|
---|
21 |
|
---|
22 | /** Constructor of class vector.
|
---|
23 | */
|
---|
24 | Vector::Vector()
|
---|
25 | {
|
---|
26 | x[0] = x[1] = x[2] = 0.;
|
---|
27 | };
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Copy constructor
|
---|
31 | */
|
---|
32 |
|
---|
33 | Vector::Vector(const Vector& src)
|
---|
34 | {
|
---|
35 | x[0] = src[0];
|
---|
36 | x[1] = src[1];
|
---|
37 | x[2] = src[2];
|
---|
38 | }
|
---|
39 |
|
---|
40 | /** Constructor of class vector.
|
---|
41 | */
|
---|
42 | Vector::Vector(const double x1, const double x2, const double x3)
|
---|
43 | {
|
---|
44 | x[0] = x1;
|
---|
45 | x[1] = x2;
|
---|
46 | x[2] = x3;
|
---|
47 | };
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Assignment operator
|
---|
51 | */
|
---|
52 | Vector& Vector::operator=(const Vector& src){
|
---|
53 | // check for self assignment
|
---|
54 | if(&src!=this){
|
---|
55 | x[0] = src[0];
|
---|
56 | x[1] = src[1];
|
---|
57 | x[2] = src[2];
|
---|
58 | }
|
---|
59 | return *this;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** Desctructor of class vector.
|
---|
63 | */
|
---|
64 | Vector::~Vector() {};
|
---|
65 |
|
---|
66 | /** Calculates square of distance between this and another vector.
|
---|
67 | * \param *y array to second vector
|
---|
68 | * \return \f$| x - y |^2\f$
|
---|
69 | */
|
---|
70 | double Vector::DistanceSquared(const Vector &y) const
|
---|
71 | {
|
---|
72 | double res = 0.;
|
---|
73 | for (int i=NDIM;i--;)
|
---|
74 | res += (x[i]-y[i])*(x[i]-y[i]);
|
---|
75 | return (res);
|
---|
76 | };
|
---|
77 |
|
---|
78 | /** Calculates distance between this and another vector.
|
---|
79 | * \param *y array to second vector
|
---|
80 | * \return \f$| x - y |\f$
|
---|
81 | */
|
---|
82 | double Vector::distance(const Vector &y) const
|
---|
83 | {
|
---|
84 | return (sqrt(DistanceSquared(y)));
|
---|
85 | };
|
---|
86 |
|
---|
87 | Vector Vector::getClosestPoint(const Vector &point) const{
|
---|
88 | // the closest point to a single point space is always the single point itself
|
---|
89 | return *this;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** Calculates distance between this and another vector in a periodic cell.
|
---|
93 | * \param *y array to second vector
|
---|
94 | * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
|
---|
95 | * \return \f$| x - y |\f$
|
---|
96 | */
|
---|
97 | double Vector::PeriodicDistance(const Vector &y, const double * const cell_size) const
|
---|
98 | {
|
---|
99 | double res = distance(y), tmp, matrix[NDIM*NDIM];
|
---|
100 | Vector Shiftedy, TranslationVector;
|
---|
101 | int N[NDIM];
|
---|
102 | matrix[0] = cell_size[0];
|
---|
103 | matrix[1] = cell_size[1];
|
---|
104 | matrix[2] = cell_size[3];
|
---|
105 | matrix[3] = cell_size[1];
|
---|
106 | matrix[4] = cell_size[2];
|
---|
107 | matrix[5] = cell_size[4];
|
---|
108 | matrix[6] = cell_size[3];
|
---|
109 | matrix[7] = cell_size[4];
|
---|
110 | matrix[8] = cell_size[5];
|
---|
111 | // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
|
---|
112 | for (N[0]=-1;N[0]<=1;N[0]++)
|
---|
113 | for (N[1]=-1;N[1]<=1;N[1]++)
|
---|
114 | for (N[2]=-1;N[2]<=1;N[2]++) {
|
---|
115 | // create the translation vector
|
---|
116 | TranslationVector.Zero();
|
---|
117 | for (int i=NDIM;i--;)
|
---|
118 | TranslationVector[i] = (double)N[i];
|
---|
119 | TranslationVector.MatrixMultiplication(matrix);
|
---|
120 | // add onto the original vector to compare with
|
---|
121 | Shiftedy = y + TranslationVector;
|
---|
122 | // get distance and compare with minimum so far
|
---|
123 | tmp = distance(Shiftedy);
|
---|
124 | if (tmp < res) res = tmp;
|
---|
125 | }
|
---|
126 | return (res);
|
---|
127 | };
|
---|
128 |
|
---|
129 | /** Calculates distance between this and another vector in a periodic cell.
|
---|
130 | * \param *y array to second vector
|
---|
131 | * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
|
---|
132 | * \return \f$| x - y |^2\f$
|
---|
133 | */
|
---|
134 | double Vector::PeriodicDistanceSquared(const Vector &y, const double * const cell_size) const
|
---|
135 | {
|
---|
136 | double res = DistanceSquared(y), tmp, matrix[NDIM*NDIM];
|
---|
137 | Vector Shiftedy, TranslationVector;
|
---|
138 | int N[NDIM];
|
---|
139 | matrix[0] = cell_size[0];
|
---|
140 | matrix[1] = cell_size[1];
|
---|
141 | matrix[2] = cell_size[3];
|
---|
142 | matrix[3] = cell_size[1];
|
---|
143 | matrix[4] = cell_size[2];
|
---|
144 | matrix[5] = cell_size[4];
|
---|
145 | matrix[6] = cell_size[3];
|
---|
146 | matrix[7] = cell_size[4];
|
---|
147 | matrix[8] = cell_size[5];
|
---|
148 | // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
|
---|
149 | for (N[0]=-1;N[0]<=1;N[0]++)
|
---|
150 | for (N[1]=-1;N[1]<=1;N[1]++)
|
---|
151 | for (N[2]=-1;N[2]<=1;N[2]++) {
|
---|
152 | // create the translation vector
|
---|
153 | TranslationVector.Zero();
|
---|
154 | for (int i=NDIM;i--;)
|
---|
155 | TranslationVector[i] = (double)N[i];
|
---|
156 | TranslationVector.MatrixMultiplication(matrix);
|
---|
157 | // add onto the original vector to compare with
|
---|
158 | Shiftedy = y + TranslationVector;
|
---|
159 | // get distance and compare with minimum so far
|
---|
160 | tmp = DistanceSquared(Shiftedy);
|
---|
161 | if (tmp < res) res = tmp;
|
---|
162 | }
|
---|
163 | return (res);
|
---|
164 | };
|
---|
165 |
|
---|
166 | /** Keeps the vector in a periodic cell, defined by the symmetric \a *matrix.
|
---|
167 | * \param *out ofstream for debugging messages
|
---|
168 | * Tries to translate a vector into each adjacent neighbouring cell.
|
---|
169 | */
|
---|
170 | void Vector::KeepPeriodic(const double * const matrix)
|
---|
171 | {
|
---|
172 | // int N[NDIM];
|
---|
173 | // bool flag = false;
|
---|
174 | //vector Shifted, TranslationVector;
|
---|
175 | // Log() << Verbose(1) << "Begin of KeepPeriodic." << endl;
|
---|
176 | // Log() << Verbose(2) << "Vector is: ";
|
---|
177 | // Output(out);
|
---|
178 | // Log() << Verbose(0) << endl;
|
---|
179 | InverseMatrixMultiplication(matrix);
|
---|
180 | for(int i=NDIM;i--;) { // correct periodically
|
---|
181 | if (at(i) < 0) { // get every coefficient into the interval [0,1)
|
---|
182 | at(i) += ceil(at(i));
|
---|
183 | } else {
|
---|
184 | at(i) -= floor(at(i));
|
---|
185 | }
|
---|
186 | }
|
---|
187 | MatrixMultiplication(matrix);
|
---|
188 | // Log() << Verbose(2) << "New corrected vector is: ";
|
---|
189 | // Output(out);
|
---|
190 | // Log() << Verbose(0) << endl;
|
---|
191 | // Log() << Verbose(1) << "End of KeepPeriodic." << endl;
|
---|
192 | };
|
---|
193 |
|
---|
194 | /** Calculates scalar product between this and another vector.
|
---|
195 | * \param *y array to second vector
|
---|
196 | * \return \f$\langle x, y \rangle\f$
|
---|
197 | */
|
---|
198 | double Vector::ScalarProduct(const Vector &y) const
|
---|
199 | {
|
---|
200 | double res = 0.;
|
---|
201 | for (int i=NDIM;i--;)
|
---|
202 | res += x[i]*y[i];
|
---|
203 | return (res);
|
---|
204 | };
|
---|
205 |
|
---|
206 |
|
---|
207 | /** Calculates VectorProduct between this and another vector.
|
---|
208 | * -# returns the Product in place of vector from which it was initiated
|
---|
209 | * -# ATTENTION: Only three dim.
|
---|
210 | * \param *y array to vector with which to calculate crossproduct
|
---|
211 | * \return \f$ x \times y \f&
|
---|
212 | */
|
---|
213 | void Vector::VectorProduct(const Vector &y)
|
---|
214 | {
|
---|
215 | Vector tmp;
|
---|
216 | tmp[0] = x[1]* y[2] - x[2]* y[1];
|
---|
217 | tmp[1] = x[2]* y[0] - x[0]* y[2];
|
---|
218 | tmp[2] = x[0]* y[1] - x[1]* y[0];
|
---|
219 | (*this) = tmp;
|
---|
220 | };
|
---|
221 |
|
---|
222 |
|
---|
223 | /** projects this vector onto plane defined by \a *y.
|
---|
224 | * \param *y normal vector of plane
|
---|
225 | * \return \f$\langle x, y \rangle\f$
|
---|
226 | */
|
---|
227 | void Vector::ProjectOntoPlane(const Vector &y)
|
---|
228 | {
|
---|
229 | Vector tmp;
|
---|
230 | tmp = y;
|
---|
231 | tmp.Normalize();
|
---|
232 | tmp.Scale(ScalarProduct(tmp));
|
---|
233 | *this -= tmp;
|
---|
234 | };
|
---|
235 |
|
---|
236 | /** Calculates the minimum distance of this vector to the plane.
|
---|
237 | * \sa Vector::GetDistanceVectorToPlane()
|
---|
238 | * \param *out output stream for debugging
|
---|
239 | * \param *PlaneNormal normal of plane
|
---|
240 | * \param *PlaneOffset offset of plane
|
---|
241 | * \return distance to plane
|
---|
242 | */
|
---|
243 | double Vector::DistanceToSpace(const Space &space) const
|
---|
244 | {
|
---|
245 | return space.distance(*this);
|
---|
246 | };
|
---|
247 |
|
---|
248 | /** Calculates the projection of a vector onto another \a *y.
|
---|
249 | * \param *y array to second vector
|
---|
250 | */
|
---|
251 | void Vector::ProjectIt(const Vector &y)
|
---|
252 | {
|
---|
253 | (*this) += (-ScalarProduct(y))*y;
|
---|
254 | };
|
---|
255 |
|
---|
256 | /** Calculates the projection of a vector onto another \a *y.
|
---|
257 | * \param *y array to second vector
|
---|
258 | * \return Vector
|
---|
259 | */
|
---|
260 | Vector Vector::Projection(const Vector &y) const
|
---|
261 | {
|
---|
262 | Vector helper = y;
|
---|
263 | helper.Scale((ScalarProduct(y)/y.NormSquared()));
|
---|
264 |
|
---|
265 | return helper;
|
---|
266 | };
|
---|
267 |
|
---|
268 | /** Calculates norm of this vector.
|
---|
269 | * \return \f$|x|\f$
|
---|
270 | */
|
---|
271 | double Vector::Norm() const
|
---|
272 | {
|
---|
273 | return (sqrt(NormSquared()));
|
---|
274 | };
|
---|
275 |
|
---|
276 | /** Calculates squared norm of this vector.
|
---|
277 | * \return \f$|x|^2\f$
|
---|
278 | */
|
---|
279 | double Vector::NormSquared() const
|
---|
280 | {
|
---|
281 | return (ScalarProduct(*this));
|
---|
282 | };
|
---|
283 |
|
---|
284 | /** Normalizes this vector.
|
---|
285 | */
|
---|
286 | void Vector::Normalize()
|
---|
287 | {
|
---|
288 | double factor = Norm();
|
---|
289 | (*this) *= 1/factor;
|
---|
290 | };
|
---|
291 |
|
---|
292 | /** Zeros all components of this vector.
|
---|
293 | */
|
---|
294 | void Vector::Zero()
|
---|
295 | {
|
---|
296 | at(0)=at(1)=at(2)=0;
|
---|
297 | };
|
---|
298 |
|
---|
299 | /** Zeros all components of this vector.
|
---|
300 | */
|
---|
301 | void Vector::One(const double one)
|
---|
302 | {
|
---|
303 | at(0)=at(1)=at(2)=one;
|
---|
304 | };
|
---|
305 |
|
---|
306 | /** Checks whether vector has all components zero.
|
---|
307 | * @return true - vector is zero, false - vector is not
|
---|
308 | */
|
---|
309 | bool Vector::IsZero() const
|
---|
310 | {
|
---|
311 | return (fabs(x[0])+fabs(x[1])+fabs(x[2]) < MYEPSILON);
|
---|
312 | };
|
---|
313 |
|
---|
314 | /** Checks whether vector has length of 1.
|
---|
315 | * @return true - vector is normalized, false - vector is not
|
---|
316 | */
|
---|
317 | bool Vector::IsOne() const
|
---|
318 | {
|
---|
319 | return (fabs(Norm() - 1.) < MYEPSILON);
|
---|
320 | };
|
---|
321 |
|
---|
322 | /** Checks whether vector is normal to \a *normal.
|
---|
323 | * @return true - vector is normalized, false - vector is not
|
---|
324 | */
|
---|
325 | bool Vector::IsNormalTo(const Vector &normal) const
|
---|
326 | {
|
---|
327 | if (ScalarProduct(normal) < MYEPSILON)
|
---|
328 | return true;
|
---|
329 | else
|
---|
330 | return false;
|
---|
331 | };
|
---|
332 |
|
---|
333 | /** Checks whether vector is normal to \a *normal.
|
---|
334 | * @return true - vector is normalized, false - vector is not
|
---|
335 | */
|
---|
336 | bool Vector::IsEqualTo(const Vector &a) const
|
---|
337 | {
|
---|
338 | bool status = true;
|
---|
339 | for (int i=0;i<NDIM;i++) {
|
---|
340 | if (fabs(x[i] - a[i]) > MYEPSILON)
|
---|
341 | status = false;
|
---|
342 | }
|
---|
343 | return status;
|
---|
344 | };
|
---|
345 |
|
---|
346 | /** Calculates the angle between this and another vector.
|
---|
347 | * \param *y array to second vector
|
---|
348 | * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
|
---|
349 | */
|
---|
350 | double Vector::Angle(const Vector &y) const
|
---|
351 | {
|
---|
352 | double norm1 = Norm(), norm2 = y.Norm();
|
---|
353 | double angle = -1;
|
---|
354 | if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
|
---|
355 | angle = this->ScalarProduct(y)/norm1/norm2;
|
---|
356 | // -1-MYEPSILON occured due to numerical imprecision, catch ...
|
---|
357 | //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
|
---|
358 | if (angle < -1)
|
---|
359 | angle = -1;
|
---|
360 | if (angle > 1)
|
---|
361 | angle = 1;
|
---|
362 | return acos(angle);
|
---|
363 | };
|
---|
364 |
|
---|
365 |
|
---|
366 | double& Vector::operator[](size_t i){
|
---|
367 | ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
|
---|
368 | return x[i];
|
---|
369 | }
|
---|
370 |
|
---|
371 | const double& Vector::operator[](size_t i) const{
|
---|
372 | ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
|
---|
373 | return x[i];
|
---|
374 | }
|
---|
375 |
|
---|
376 | double& Vector::at(size_t i){
|
---|
377 | return (*this)[i];
|
---|
378 | }
|
---|
379 |
|
---|
380 | const double& Vector::at(size_t i) const{
|
---|
381 | return (*this)[i];
|
---|
382 | }
|
---|
383 |
|
---|
384 | double* Vector::get(){
|
---|
385 | return x;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /** Compares vector \a to vector \a b component-wise.
|
---|
389 | * \param a base vector
|
---|
390 | * \param b vector components to add
|
---|
391 | * \return a == b
|
---|
392 | */
|
---|
393 | bool Vector::operator==(const Vector& b) const
|
---|
394 | {
|
---|
395 | return IsEqualTo(b);
|
---|
396 | };
|
---|
397 |
|
---|
398 | bool Vector::operator!=(const Vector& b) const
|
---|
399 | {
|
---|
400 | return !IsEqualTo(b);
|
---|
401 | }
|
---|
402 |
|
---|
403 | /** Sums vector \a to this lhs component-wise.
|
---|
404 | * \param a base vector
|
---|
405 | * \param b vector components to add
|
---|
406 | * \return lhs + a
|
---|
407 | */
|
---|
408 | const Vector& Vector::operator+=(const Vector& b)
|
---|
409 | {
|
---|
410 | this->AddVector(b);
|
---|
411 | return *this;
|
---|
412 | };
|
---|
413 |
|
---|
414 | /** Subtracts vector \a from this lhs component-wise.
|
---|
415 | * \param a base vector
|
---|
416 | * \param b vector components to add
|
---|
417 | * \return lhs - a
|
---|
418 | */
|
---|
419 | const Vector& Vector::operator-=(const Vector& b)
|
---|
420 | {
|
---|
421 | this->SubtractVector(b);
|
---|
422 | return *this;
|
---|
423 | };
|
---|
424 |
|
---|
425 | /** factor each component of \a a times a double \a m.
|
---|
426 | * \param a base vector
|
---|
427 | * \param m factor
|
---|
428 | * \return lhs.x[i] * m
|
---|
429 | */
|
---|
430 | const Vector& operator*=(Vector& a, const double m)
|
---|
431 | {
|
---|
432 | a.Scale(m);
|
---|
433 | return a;
|
---|
434 | };
|
---|
435 |
|
---|
436 | /** Sums two vectors \a and \b component-wise.
|
---|
437 | * \param a first vector
|
---|
438 | * \param b second vector
|
---|
439 | * \return a + b
|
---|
440 | */
|
---|
441 | Vector const Vector::operator+(const Vector& b) const
|
---|
442 | {
|
---|
443 | Vector x = *this;
|
---|
444 | x.AddVector(b);
|
---|
445 | return x;
|
---|
446 | };
|
---|
447 |
|
---|
448 | /** Subtracts vector \a from \b component-wise.
|
---|
449 | * \param a first vector
|
---|
450 | * \param b second vector
|
---|
451 | * \return a - b
|
---|
452 | */
|
---|
453 | Vector const Vector::operator-(const Vector& b) const
|
---|
454 | {
|
---|
455 | Vector x = *this;
|
---|
456 | x.SubtractVector(b);
|
---|
457 | return x;
|
---|
458 | };
|
---|
459 |
|
---|
460 | /** Factors given vector \a a times \a m.
|
---|
461 | * \param a vector
|
---|
462 | * \param m factor
|
---|
463 | * \return m * a
|
---|
464 | */
|
---|
465 | Vector const operator*(const Vector& a, const double m)
|
---|
466 | {
|
---|
467 | Vector x(a);
|
---|
468 | x.Scale(m);
|
---|
469 | return x;
|
---|
470 | };
|
---|
471 |
|
---|
472 | /** Factors given vector \a a times \a m.
|
---|
473 | * \param m factor
|
---|
474 | * \param a vector
|
---|
475 | * \return m * a
|
---|
476 | */
|
---|
477 | Vector const operator*(const double m, const Vector& a )
|
---|
478 | {
|
---|
479 | Vector x(a);
|
---|
480 | x.Scale(m);
|
---|
481 | return x;
|
---|
482 | };
|
---|
483 |
|
---|
484 | ostream& operator<<(ostream& ost, const Vector& m)
|
---|
485 | {
|
---|
486 | ost << "(";
|
---|
487 | for (int i=0;i<NDIM;i++) {
|
---|
488 | ost << m[i];
|
---|
489 | if (i != 2)
|
---|
490 | ost << ",";
|
---|
491 | }
|
---|
492 | ost << ")";
|
---|
493 | return ost;
|
---|
494 | };
|
---|
495 |
|
---|
496 |
|
---|
497 | void Vector::ScaleAll(const double *factor)
|
---|
498 | {
|
---|
499 | for (int i=NDIM;i--;)
|
---|
500 | x[i] *= factor[i];
|
---|
501 | };
|
---|
502 |
|
---|
503 |
|
---|
504 |
|
---|
505 | void Vector::Scale(const double factor)
|
---|
506 | {
|
---|
507 | for (int i=NDIM;i--;)
|
---|
508 | x[i] *= factor;
|
---|
509 | };
|
---|
510 |
|
---|
511 | /** Given a box by its matrix \a *M and its inverse *Minv the vector is made to point within that box.
|
---|
512 | * \param *M matrix of box
|
---|
513 | * \param *Minv inverse matrix
|
---|
514 | */
|
---|
515 | void Vector::WrapPeriodically(const double * const M, const double * const Minv)
|
---|
516 | {
|
---|
517 | MatrixMultiplication(Minv);
|
---|
518 | // truncate to [0,1] for each axis
|
---|
519 | for (int i=0;i<NDIM;i++) {
|
---|
520 | //x[i] += 0.5; // set to center of box
|
---|
521 | while (x[i] >= 1.)
|
---|
522 | x[i] -= 1.;
|
---|
523 | while (x[i] < 0.)
|
---|
524 | x[i] += 1.;
|
---|
525 | }
|
---|
526 | MatrixMultiplication(M);
|
---|
527 | };
|
---|
528 |
|
---|
529 | std::pair<Vector,Vector> Vector::partition(const Vector &rhs) const{
|
---|
530 | double factor = ScalarProduct(rhs)/rhs.NormSquared();
|
---|
531 | Vector res= factor * rhs;
|
---|
532 | return make_pair(res,(*this)-res);
|
---|
533 | }
|
---|
534 |
|
---|
535 | std::pair<pointset,Vector> Vector::partition(const pointset &points) const{
|
---|
536 | Vector helper = *this;
|
---|
537 | pointset res;
|
---|
538 | for(pointset::const_iterator iter=points.begin();iter!=points.end();++iter){
|
---|
539 | pair<Vector,Vector> currPart = helper.partition(*iter);
|
---|
540 | res.push_back(currPart.first);
|
---|
541 | helper = currPart.second;
|
---|
542 | }
|
---|
543 | return make_pair(res,helper);
|
---|
544 | }
|
---|
545 |
|
---|
546 | /** Do a matrix multiplication.
|
---|
547 | * \param *matrix NDIM_NDIM array
|
---|
548 | */
|
---|
549 | void Vector::MatrixMultiplication(const double * const M)
|
---|
550 | {
|
---|
551 | // do the matrix multiplication
|
---|
552 | at(0) = M[0]*x[0]+M[3]*x[1]+M[6]*x[2];
|
---|
553 | at(1) = M[1]*x[0]+M[4]*x[1]+M[7]*x[2];
|
---|
554 | at(2) = M[2]*x[0]+M[5]*x[1]+M[8]*x[2];
|
---|
555 | };
|
---|
556 |
|
---|
557 | /** Do a matrix multiplication with the \a *A' inverse.
|
---|
558 | * \param *matrix NDIM_NDIM array
|
---|
559 | */
|
---|
560 | bool Vector::InverseMatrixMultiplication(const double * const A)
|
---|
561 | {
|
---|
562 | double B[NDIM*NDIM];
|
---|
563 | double detA = RDET3(A);
|
---|
564 | double detAReci;
|
---|
565 |
|
---|
566 | // calculate the inverse B
|
---|
567 | if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
|
---|
568 | detAReci = 1./detA;
|
---|
569 | B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
|
---|
570 | B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
|
---|
571 | B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
|
---|
572 | B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
|
---|
573 | B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
|
---|
574 | B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
|
---|
575 | B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
|
---|
576 | B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
|
---|
577 | B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
|
---|
578 |
|
---|
579 | // do the matrix multiplication
|
---|
580 | at(0) = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
|
---|
581 | at(1) = B[1]*x[0]+B[4]*x[1]+B[7]*x[2];
|
---|
582 | at(2) = B[2]*x[0]+B[5]*x[1]+B[8]*x[2];
|
---|
583 |
|
---|
584 | return true;
|
---|
585 | } else {
|
---|
586 | return false;
|
---|
587 | }
|
---|
588 | };
|
---|
589 |
|
---|
590 |
|
---|
591 | /** Creates this vector as the b y *factors' components scaled linear combination of the given three.
|
---|
592 | * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
|
---|
593 | * \param *x1 first vector
|
---|
594 | * \param *x2 second vector
|
---|
595 | * \param *x3 third vector
|
---|
596 | * \param *factors three-component vector with the factor for each given vector
|
---|
597 | */
|
---|
598 | void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors)
|
---|
599 | {
|
---|
600 | (*this) = (factors[0]*x1) +
|
---|
601 | (factors[1]*x2) +
|
---|
602 | (factors[2]*x3);
|
---|
603 | };
|
---|
604 |
|
---|
605 | /** Calculates orthonormal vector to one given vectors.
|
---|
606 | * Just subtracts the projection onto the given vector from this vector.
|
---|
607 | * The removed part of the vector is Vector::Projection()
|
---|
608 | * \param *x1 vector
|
---|
609 | * \return true - success, false - vector is zero
|
---|
610 | */
|
---|
611 | bool Vector::MakeNormalTo(const Vector &y1)
|
---|
612 | {
|
---|
613 | bool result = false;
|
---|
614 | double factor = y1.ScalarProduct(*this)/y1.NormSquared();
|
---|
615 | Vector x1 = factor * y1;
|
---|
616 | SubtractVector(x1);
|
---|
617 | for (int i=NDIM;i--;)
|
---|
618 | result = result || (fabs(x[i]) > MYEPSILON);
|
---|
619 |
|
---|
620 | return result;
|
---|
621 | };
|
---|
622 |
|
---|
623 | /** Creates this vector as one of the possible orthonormal ones to the given one.
|
---|
624 | * Just scan how many components of given *vector are unequal to zero and
|
---|
625 | * try to get the skp of both to be zero accordingly.
|
---|
626 | * \param *vector given vector
|
---|
627 | * \return true - success, false - failure (null vector given)
|
---|
628 | */
|
---|
629 | bool Vector::GetOneNormalVector(const Vector &GivenVector)
|
---|
630 | {
|
---|
631 | int Components[NDIM]; // contains indices of non-zero components
|
---|
632 | int Last = 0; // count the number of non-zero entries in vector
|
---|
633 | int j; // loop variables
|
---|
634 | double norm;
|
---|
635 |
|
---|
636 | for (j=NDIM;j--;)
|
---|
637 | Components[j] = -1;
|
---|
638 |
|
---|
639 | // in two component-systems we need to find the one position that is zero
|
---|
640 | int zeroPos = -1;
|
---|
641 | // find two components != 0
|
---|
642 | for (j=0;j<NDIM;j++){
|
---|
643 | if (fabs(GivenVector[j]) > MYEPSILON)
|
---|
644 | Components[Last++] = j;
|
---|
645 | else
|
---|
646 | // this our zero Position
|
---|
647 | zeroPos = j;
|
---|
648 | }
|
---|
649 |
|
---|
650 | switch(Last) {
|
---|
651 | case 3: // threecomponent system
|
---|
652 | // the position of the zero is arbitrary in three component systems
|
---|
653 | zeroPos = Components[2];
|
---|
654 | case 2: // two component system
|
---|
655 | norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]]));
|
---|
656 | at(zeroPos) = 0.;
|
---|
657 | // in skp both remaining parts shall become zero but with opposite sign and third is zero
|
---|
658 | at(Components[1]) = -1./GivenVector[Components[1]] / norm;
|
---|
659 | at(Components[0]) = 1./GivenVector[Components[0]] / norm;
|
---|
660 | return true;
|
---|
661 | break;
|
---|
662 | case 1: // one component system
|
---|
663 | // set sole non-zero component to 0, and one of the other zero component pendants to 1
|
---|
664 | at((Components[0]+2)%NDIM) = 0.;
|
---|
665 | at((Components[0]+1)%NDIM) = 1.;
|
---|
666 | at(Components[0]) = 0.;
|
---|
667 | return true;
|
---|
668 | break;
|
---|
669 | default:
|
---|
670 | return false;
|
---|
671 | }
|
---|
672 | };
|
---|
673 |
|
---|
674 | /** Adds vector \a *y componentwise.
|
---|
675 | * \param *y vector
|
---|
676 | */
|
---|
677 | void Vector::AddVector(const Vector &y)
|
---|
678 | {
|
---|
679 | for(int i=NDIM;i--;)
|
---|
680 | x[i] += y[i];
|
---|
681 | }
|
---|
682 |
|
---|
683 | /** Adds vector \a *y componentwise.
|
---|
684 | * \param *y vector
|
---|
685 | */
|
---|
686 | void Vector::SubtractVector(const Vector &y)
|
---|
687 | {
|
---|
688 | for(int i=NDIM;i--;)
|
---|
689 | x[i] -= y[i];
|
---|
690 | }
|
---|
691 |
|
---|
692 | /**
|
---|
693 | * Checks whether this vector is within the parallelepiped defined by the given three vectors and
|
---|
694 | * their offset.
|
---|
695 | *
|
---|
696 | * @param offest for the origin of the parallelepiped
|
---|
697 | * @param three vectors forming the matrix that defines the shape of the parallelpiped
|
---|
698 | */
|
---|
699 | bool Vector::IsInParallelepiped(const Vector &offset, const double * const parallelepiped) const
|
---|
700 | {
|
---|
701 | Vector a = (*this)-offset;
|
---|
702 | a.InverseMatrixMultiplication(parallelepiped);
|
---|
703 | bool isInside = true;
|
---|
704 |
|
---|
705 | for (int i=NDIM;i--;)
|
---|
706 | isInside = isInside && ((a[i] <= 1) && (a[i] >= 0));
|
---|
707 |
|
---|
708 | return isInside;
|
---|
709 | }
|
---|
710 |
|
---|
711 |
|
---|
712 | // some comonly used vectors
|
---|
713 | const Vector zeroVec(0,0,0);
|
---|
714 | const Vector e1(1,0,0);
|
---|
715 | const Vector e2(0,1,0);
|
---|
716 | const Vector e3(0,0,1);
|
---|