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