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