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