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