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