| 1 | /*
 | 
|---|
| 2 |  * ellipsoid.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Jan 20, 2009
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include <gsl/gsl_multimin.h>
 | 
|---|
| 11 | #include <gsl/gsl_vector.h>
 | 
|---|
| 12 | 
 | 
|---|
| 13 | #include <iomanip>
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include <set>
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include "boundary.hpp"
 | 
|---|
| 18 | #include "ellipsoid.hpp"
 | 
|---|
| 19 | #include "linkedcell.hpp"
 | 
|---|
| 20 | #include "log.hpp"
 | 
|---|
| 21 | #include "tesselation.hpp"
 | 
|---|
| 22 | #include "vector.hpp"
 | 
|---|
| 23 | #include "Matrix.hpp"
 | 
|---|
| 24 | #include "verbose.hpp"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | /** Determines squared distance for a given point \a x to surface of ellipsoid.
 | 
|---|
| 27 |  * \param x given point
 | 
|---|
| 28 |  * \param EllipsoidCenter center of ellipsoid
 | 
|---|
| 29 |  * \param EllipsoidLength[3] three lengths of half axis of ellipsoid
 | 
|---|
| 30 |  * \param EllipsoidAngle[3] three rotation angles of ellipsoid
 | 
|---|
| 31 |  * \return squared distance from point to surface
 | 
|---|
| 32 |  */
 | 
|---|
| 33 | double SquaredDistanceToEllipsoid(Vector &x, Vector &EllipsoidCenter, double *EllipsoidLength, double *EllipsoidAngle)
 | 
|---|
| 34 | {
 | 
|---|
| 35 |   Vector helper, RefPoint;
 | 
|---|
| 36 |   double distance = -1.;
 | 
|---|
| 37 |   Matrix Matrix;
 | 
|---|
| 38 |   double InverseLength[3];
 | 
|---|
| 39 |   double psi,theta,phi; // euler angles in ZX'Z'' convention
 | 
|---|
| 40 | 
 | 
|---|
| 41 |   //Log() << Verbose(3) << "Begin of SquaredDistanceToEllipsoid" << endl;
 | 
|---|
| 42 | 
 | 
|---|
| 43 |   for(int i=0;i<3;i++)
 | 
|---|
| 44 |     InverseLength[i] = 1./EllipsoidLength[i];
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   // 1. translate coordinate system so that ellipsoid center is in origin
 | 
|---|
| 47 |   RefPoint = helper = x - EllipsoidCenter;
 | 
|---|
| 48 |   //Log() << Verbose(4) << "Translated given point is at " << RefPoint << "." << endl;
 | 
|---|
| 49 | 
 | 
|---|
| 50 |   // 2. transform coordinate system by inverse of rotation matrix and of diagonal matrix
 | 
|---|
| 51 |   psi = EllipsoidAngle[0];
 | 
|---|
| 52 |   theta = EllipsoidAngle[1];
 | 
|---|
| 53 |   phi = EllipsoidAngle[2];
 | 
|---|
| 54 |   Matrix.at(0,0) = cos(psi)*cos(phi) - sin(psi)*cos(theta)*sin(phi);
 | 
|---|
| 55 |   Matrix.at(1,0) = -cos(psi)*sin(phi) - sin(psi)*cos(theta)*cos(phi);
 | 
|---|
| 56 |   Matrix.at(2,0) = sin(psi)*sin(theta);
 | 
|---|
| 57 |   Matrix.at(0,1) = sin(psi)*cos(phi) + cos(psi)*cos(theta)*sin(phi);
 | 
|---|
| 58 |   Matrix.at(1,1) = cos(psi)*cos(theta)*cos(phi) - sin(psi)*sin(phi);
 | 
|---|
| 59 |   Matrix.at(2,1) = -cos(psi)*sin(theta);
 | 
|---|
| 60 |   Matrix.at(0,2) = sin(theta)*sin(phi);
 | 
|---|
| 61 |   Matrix.at(1,2) = sin(theta)*cos(phi);
 | 
|---|
| 62 |   Matrix.at(2,2) = cos(theta);
 | 
|---|
| 63 |   helper.MatrixMultiplication(Matrix);
 | 
|---|
| 64 |   helper.ScaleAll(InverseLength);
 | 
|---|
| 65 |   //Log() << Verbose(4) << "Transformed RefPoint is at " << helper << "." << endl;
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   // 3. construct intersection point with unit sphere and ray between origin and x
 | 
|---|
| 68 |   helper.Normalize(); // is simply normalizes vector in distance direction
 | 
|---|
| 69 |   //Log() << Verbose(4) << "Transformed intersection is at " << helper << "." << endl;
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   // 4. transform back the constructed intersection point
 | 
|---|
| 72 |   psi = -EllipsoidAngle[0];
 | 
|---|
| 73 |   theta = -EllipsoidAngle[1];
 | 
|---|
| 74 |   phi = -EllipsoidAngle[2];
 | 
|---|
| 75 |   helper.ScaleAll(EllipsoidLength);
 | 
|---|
| 76 |   Matrix.at(0,0) = cos(psi)*cos(phi) - sin(psi)*cos(theta)*sin(phi);
 | 
|---|
| 77 |   Matrix.at(1,0) = -cos(psi)*sin(phi) - sin(psi)*cos(theta)*cos(phi);
 | 
|---|
| 78 |   Matrix.at(2,0) = sin(psi)*sin(theta);
 | 
|---|
| 79 |   Matrix.at(0,1) = sin(psi)*cos(phi) + cos(psi)*cos(theta)*sin(phi);
 | 
|---|
| 80 |   Matrix.at(1,1) = cos(psi)*cos(theta)*cos(phi) - sin(psi)*sin(phi);
 | 
|---|
| 81 |   Matrix.at(2,1) = -cos(psi)*sin(theta);
 | 
|---|
| 82 |   Matrix.at(0,2) = sin(theta)*sin(phi);
 | 
|---|
| 83 |   Matrix.at(1,2) = sin(theta)*cos(phi);
 | 
|---|
| 84 |   Matrix.at(2,2) = cos(theta);
 | 
|---|
| 85 |   helper.MatrixMultiplication(Matrix);
 | 
|---|
| 86 |   //Log() << Verbose(4) << "Intersection is at " << helper << "." << endl;
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   // 5. determine distance between backtransformed point and x
 | 
|---|
| 89 |   distance = RefPoint.DistanceSquared(helper);
 | 
|---|
| 90 |   //Log() << Verbose(4) << "Squared distance between intersection and RefPoint is " << distance << "." << endl;
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   return distance;
 | 
|---|
| 93 |   //Log() << Verbose(3) << "End of SquaredDistanceToEllipsoid" << endl;
 | 
|---|
| 94 | };
 | 
|---|
| 95 | 
 | 
|---|
| 96 | /** structure for ellipsoid minimisation containing points to fit to.
 | 
|---|
| 97 |  */
 | 
|---|
| 98 | struct EllipsoidMinimisation {
 | 
|---|
| 99 |   int N;      //!< dimension of vector set
 | 
|---|
| 100 |   Vector *x;  //!< array of vectors
 | 
|---|
| 101 | };
 | 
|---|
| 102 | 
 | 
|---|
| 103 | /** Sum of squared distance to ellipsoid to be minimised.
 | 
|---|
| 104 |  * \param *x parameters for the ellipsoid
 | 
|---|
| 105 |  * \param *params EllipsoidMinimisation with set of data points to minimise distance to and dimension
 | 
|---|
| 106 |  * \return sum of squared distance, \sa SquaredDistanceToEllipsoid()
 | 
|---|
| 107 |  */
 | 
|---|
| 108 | double SumSquaredDistance (const gsl_vector * x, void * params)
 | 
|---|
| 109 | {
 | 
|---|
| 110 |   Vector *set= ((struct EllipsoidMinimisation *)params)->x;
 | 
|---|
| 111 |   int N = ((struct EllipsoidMinimisation *)params)->N;
 | 
|---|
| 112 |   double SumDistance = 0.;
 | 
|---|
| 113 |   double distance;
 | 
|---|
| 114 |   Vector Center;
 | 
|---|
| 115 |   double EllipsoidLength[3], EllipsoidAngle[3];
 | 
|---|
| 116 | 
 | 
|---|
| 117 |   // put parameters into suitable ellipsoid form
 | 
|---|
| 118 |   for (int i=0;i<3;i++) {
 | 
|---|
| 119 |     Center[i] = gsl_vector_get(x, i+0);
 | 
|---|
| 120 |     EllipsoidLength[i] = gsl_vector_get(x, i+3);
 | 
|---|
| 121 |     EllipsoidAngle[i] = gsl_vector_get(x, i+6);
 | 
|---|
| 122 |   }
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   // go through all points and sum distance
 | 
|---|
| 125 |   for (int i=0;i<N;i++) {
 | 
|---|
| 126 |     distance = SquaredDistanceToEllipsoid(set[i], Center, EllipsoidLength, EllipsoidAngle);
 | 
|---|
| 127 |     if (!isnan(distance)) {
 | 
|---|
| 128 |       SumDistance += distance;
 | 
|---|
| 129 |     } else {
 | 
|---|
| 130 |       SumDistance = GSL_NAN;
 | 
|---|
| 131 |       break;
 | 
|---|
| 132 |     }
 | 
|---|
| 133 |   }
 | 
|---|
| 134 | 
 | 
|---|
| 135 |   //Log() << Verbose(0) << "Current summed distance is " << SumDistance << "." << endl;
 | 
|---|
| 136 |   return SumDistance;
 | 
|---|
| 137 | };
 | 
|---|
| 138 | 
 | 
|---|
| 139 | /** Finds best fitting ellipsoid parameter set in Least square sense for a given point set.
 | 
|---|
| 140 |  * \param *out output stream for debugging
 | 
|---|
| 141 |  * \param *set given point set
 | 
|---|
| 142 |  * \param N number of points in set
 | 
|---|
| 143 |  * \param EllipsoidParamter[3] three parameters in ellipsoid equation
 | 
|---|
| 144 |  * \return true - fit successful, false - fit impossible
 | 
|---|
| 145 |  */
 | 
|---|
| 146 | bool FitPointSetToEllipsoid(Vector *set, int N, Vector *EllipsoidCenter, double *EllipsoidLength, double *EllipsoidAngle)
 | 
|---|
| 147 | {
 | 
|---|
| 148 |   int status = GSL_SUCCESS;
 | 
|---|
| 149 |   DoLog(2) && (Log() << Verbose(2) << "Begin of FitPointSetToEllipsoid " << endl);
 | 
|---|
| 150 |   if (N >= 3) { // check that enough points are given (9 d.o.f.)
 | 
|---|
| 151 |     struct EllipsoidMinimisation par;
 | 
|---|
| 152 |     const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
 | 
|---|
| 153 |     gsl_multimin_fminimizer *s = NULL;
 | 
|---|
| 154 |     gsl_vector *ss, *x;
 | 
|---|
| 155 |     gsl_multimin_function minex_func;
 | 
|---|
| 156 | 
 | 
|---|
| 157 |     size_t iter = 0;
 | 
|---|
| 158 |     double size;
 | 
|---|
| 159 | 
 | 
|---|
| 160 |     /* Starting point */
 | 
|---|
| 161 |     x = gsl_vector_alloc (9);
 | 
|---|
| 162 |     for (int i=0;i<3;i++) {
 | 
|---|
| 163 |       gsl_vector_set (x, i+0, EllipsoidCenter->at(i));
 | 
|---|
| 164 |       gsl_vector_set (x, i+3, EllipsoidLength[i]);
 | 
|---|
| 165 |       gsl_vector_set (x, i+6, EllipsoidAngle[i]);
 | 
|---|
| 166 |     }
 | 
|---|
| 167 |     par.x = set;
 | 
|---|
| 168 |     par.N = N;
 | 
|---|
| 169 | 
 | 
|---|
| 170 |     /* Set initial step sizes */
 | 
|---|
| 171 |     ss = gsl_vector_alloc (9);
 | 
|---|
| 172 |     for (int i=0;i<3;i++) {
 | 
|---|
| 173 |       gsl_vector_set (ss, i+0, 0.1);
 | 
|---|
| 174 |       gsl_vector_set (ss, i+3, 1.0);
 | 
|---|
| 175 |       gsl_vector_set (ss, i+6, M_PI/20.);
 | 
|---|
| 176 |     }
 | 
|---|
| 177 | 
 | 
|---|
| 178 |     /* Initialize method and iterate */
 | 
|---|
| 179 |     minex_func.n = 9;
 | 
|---|
| 180 |     minex_func.f = &SumSquaredDistance;
 | 
|---|
| 181 |     minex_func.params = (void *)∥
 | 
|---|
| 182 | 
 | 
|---|
| 183 |     s = gsl_multimin_fminimizer_alloc (T, 9);
 | 
|---|
| 184 |     gsl_multimin_fminimizer_set (s, &minex_func, x, ss);
 | 
|---|
| 185 | 
 | 
|---|
| 186 |     do {
 | 
|---|
| 187 |       iter++;
 | 
|---|
| 188 |       status = gsl_multimin_fminimizer_iterate(s);
 | 
|---|
| 189 | 
 | 
|---|
| 190 |       if (status)
 | 
|---|
| 191 |         break;
 | 
|---|
| 192 | 
 | 
|---|
| 193 |       size = gsl_multimin_fminimizer_size (s);
 | 
|---|
| 194 |       status = gsl_multimin_test_size (size, 1e-2);
 | 
|---|
| 195 | 
 | 
|---|
| 196 |       if (status == GSL_SUCCESS) {
 | 
|---|
| 197 |         for (int i=0;i<3;i++) {
 | 
|---|
| 198 |           EllipsoidCenter->at(i) = gsl_vector_get (s->x,i+0);
 | 
|---|
| 199 |           EllipsoidLength[i] = gsl_vector_get (s->x, i+3);
 | 
|---|
| 200 |           EllipsoidAngle[i] = gsl_vector_get (s->x, i+6);
 | 
|---|
| 201 |         }
 | 
|---|
| 202 |         DoLog(4) && (Log() << Verbose(4) << setprecision(3) << "Converged fit at: " << *EllipsoidCenter << ", lengths " << EllipsoidLength[0] << ", " << EllipsoidLength[1] << ", " << EllipsoidLength[2] << ", angles " << EllipsoidAngle[0] << ", " << EllipsoidAngle[1] << ", " << EllipsoidAngle[2] << " with summed distance " << s->fval << "." << endl);
 | 
|---|
| 203 |       }
 | 
|---|
| 204 | 
 | 
|---|
| 205 |     } while (status == GSL_CONTINUE && iter < 1000);
 | 
|---|
| 206 | 
 | 
|---|
| 207 |     gsl_vector_free(x);
 | 
|---|
| 208 |     gsl_vector_free(ss);
 | 
|---|
| 209 |     gsl_multimin_fminimizer_free (s);
 | 
|---|
| 210 | 
 | 
|---|
| 211 |   } else {
 | 
|---|
| 212 |     DoLog(3) && (Log() << Verbose(3) << "Not enough points provided for fit to ellipsoid." << endl);
 | 
|---|
| 213 |     return false;
 | 
|---|
| 214 |   }
 | 
|---|
| 215 |   DoLog(2) && (Log() << Verbose(2) << "End of FitPointSetToEllipsoid" << endl);
 | 
|---|
| 216 |   if (status == GSL_SUCCESS)
 | 
|---|
| 217 |     return true;
 | 
|---|
| 218 |   else
 | 
|---|
| 219 |     return false;
 | 
|---|
| 220 | };
 | 
|---|
| 221 | 
 | 
|---|
| 222 | /** Picks a number of random points from a LC neighbourhood as a fitting set.
 | 
|---|
| 223 |  * \param *out output stream for debugging
 | 
|---|
| 224 |  * \param *T Tesselation containing boundary points
 | 
|---|
| 225 |  * \param *LC linked cell list of all atoms
 | 
|---|
| 226 |  * \param *&x random point set on return (not allocated!)
 | 
|---|
| 227 |  * \param PointsToPick number of points in set to pick
 | 
|---|
| 228 |  */
 | 
|---|
| 229 | void PickRandomNeighbouredPointSet(class Tesselation *T, class LinkedCell *LC, Vector *&x, size_t PointsToPick)
 | 
|---|
| 230 | {
 | 
|---|
| 231 |   size_t PointsLeft = 0;
 | 
|---|
| 232 |   size_t PointsPicked = 0;
 | 
|---|
| 233 |   int Nlower[NDIM], Nupper[NDIM];
 | 
|---|
| 234 |   set<int> PickedAtomNrs;   // ordered list of picked atoms
 | 
|---|
| 235 |   set<int>::iterator current;
 | 
|---|
| 236 |   int index;
 | 
|---|
| 237 |   TesselPoint *Candidate = NULL;
 | 
|---|
| 238 |   DoLog(2) && (Log() << Verbose(2) << "Begin of PickRandomPointSet" << endl);
 | 
|---|
| 239 | 
 | 
|---|
| 240 |   // allocate array
 | 
|---|
| 241 |   if (x == NULL) {
 | 
|---|
| 242 |     x = new Vector[PointsToPick];
 | 
|---|
| 243 |   } else {
 | 
|---|
| 244 |     DoeLog(2) && (eLog()<< Verbose(2) << "Given pointer to vector array seems already allocated." << endl);
 | 
|---|
| 245 |   }
 | 
|---|
| 246 | 
 | 
|---|
| 247 |   do {
 | 
|---|
| 248 |     for(int i=0;i<NDIM;i++) // pick three random indices
 | 
|---|
| 249 |       LC->n[i] = (rand() % LC->N[i]);
 | 
|---|
| 250 |     DoLog(2) && (Log() << Verbose(2) << "INFO: Center cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " ... ");
 | 
|---|
| 251 |     // get random cell
 | 
|---|
| 252 |     const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
 | 
|---|
| 253 |     if (List == NULL) {  // set index to it
 | 
|---|
| 254 |       continue;
 | 
|---|
| 255 |     }
 | 
|---|
| 256 |     DoLog(2) && (Log() << Verbose(2) << "with No. " << LC->index << "." << endl);
 | 
|---|
| 257 | 
 | 
|---|
| 258 |     DoLog(2) && (Log() << Verbose(2) << "LC Intervals:");
 | 
|---|
| 259 |     for (int i=0;i<NDIM;i++) {
 | 
|---|
| 260 |       Nlower[i] = ((LC->n[i]-1) >= 0) ? LC->n[i]-1 : 0;
 | 
|---|
| 261 |       Nupper[i] = ((LC->n[i]+1) < LC->N[i]) ? LC->n[i]+1 : LC->N[i]-1;
 | 
|---|
| 262 |       DoLog(0) && (Log() << Verbose(0) << " [" << Nlower[i] << "," << Nupper[i] << "] ");
 | 
|---|
| 263 |     }
 | 
|---|
| 264 |     DoLog(0) && (Log() << Verbose(0) << endl);
 | 
|---|
| 265 | 
 | 
|---|
| 266 |     // count whether there are sufficient atoms in this cell+neighbors
 | 
|---|
| 267 |     PointsLeft=0;
 | 
|---|
| 268 |     for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
 | 
|---|
| 269 |       for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
 | 
|---|
| 270 |         for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
 | 
|---|
| 271 |           const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
 | 
|---|
| 272 |           PointsLeft += List->size();
 | 
|---|
| 273 |         }
 | 
|---|
| 274 |     DoLog(2) && (Log() << Verbose(2) << "There are " << PointsLeft << " atoms in this neighbourhood." << endl);
 | 
|---|
| 275 |     if (PointsLeft < PointsToPick) {  // ensure that we can pick enough points in its neighbourhood at all.
 | 
|---|
| 276 |       continue;
 | 
|---|
| 277 |     }
 | 
|---|
| 278 | 
 | 
|---|
| 279 |     // pre-pick a fixed number of atoms
 | 
|---|
| 280 |     PickedAtomNrs.clear();
 | 
|---|
| 281 |     do {
 | 
|---|
| 282 |       index = (rand() % PointsLeft);
 | 
|---|
| 283 |       current = PickedAtomNrs.find(index);  // not present?
 | 
|---|
| 284 |       if (current == PickedAtomNrs.end()) {
 | 
|---|
| 285 |         //Log() << Verbose(2) << "Picking atom nr. " << index << "." << endl;
 | 
|---|
| 286 |         PickedAtomNrs.insert(index);
 | 
|---|
| 287 |       }
 | 
|---|
| 288 |     } while (PickedAtomNrs.size() < PointsToPick);
 | 
|---|
| 289 | 
 | 
|---|
| 290 |     index = 0; // now go through all and pick those whose from PickedAtomsNr
 | 
|---|
| 291 |     PointsPicked=0;
 | 
|---|
| 292 |     current = PickedAtomNrs.begin();
 | 
|---|
| 293 |     for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
 | 
|---|
| 294 |       for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
 | 
|---|
| 295 |         for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
 | 
|---|
| 296 |           const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
 | 
|---|
| 297 | //          Log() << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << " containing " << List->size() << " points." << endl;
 | 
|---|
| 298 |           if (List != NULL) {
 | 
|---|
| 299 | //            if (List->begin() != List->end())
 | 
|---|
| 300 | //              Log() << Verbose(2) << "Going through candidates ... " << endl;
 | 
|---|
| 301 | //            else
 | 
|---|
| 302 | //              Log() << Verbose(2) << "Cell is empty ... " << endl;
 | 
|---|
| 303 |             for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
 | 
|---|
| 304 |               if ((current != PickedAtomNrs.end()) && (*current == index)) {
 | 
|---|
| 305 |                 Candidate = (*Runner);
 | 
|---|
| 306 |                 DoLog(2) && (Log() << Verbose(2) << "Current picked node is " << **Runner << " with index " << index << "." << endl);
 | 
|---|
| 307 |                 x[PointsPicked++] = *Candidate->node;    // we have one more atom picked
 | 
|---|
| 308 |                 current++;    // next pre-picked atom
 | 
|---|
| 309 |               }
 | 
|---|
| 310 |               index++;  // next atom nr.
 | 
|---|
| 311 |             }
 | 
|---|
| 312 | //          } else {
 | 
|---|
| 313 | //            Log() << Verbose(2) << "List for this index not allocated!" << endl;
 | 
|---|
| 314 |           }
 | 
|---|
| 315 |         }
 | 
|---|
| 316 |     DoLog(2) && (Log() << Verbose(2) << "The following points were picked: " << endl);
 | 
|---|
| 317 |     for (size_t i=0;i<PointsPicked;i++)
 | 
|---|
| 318 |       DoLog(2) && (Log() << Verbose(2) << x[i] << endl);
 | 
|---|
| 319 |     if (PointsPicked == PointsToPick)  // break out of loop if we have all
 | 
|---|
| 320 |       break;
 | 
|---|
| 321 |   } while(1);
 | 
|---|
| 322 | 
 | 
|---|
| 323 |   DoLog(2) && (Log() << Verbose(2) << "End of PickRandomPointSet" << endl);
 | 
|---|
| 324 | };
 | 
|---|
| 325 | 
 | 
|---|
| 326 | /** Picks a number of random points from a set of boundary points as a fitting set.
 | 
|---|
| 327 |  * \param *out output stream for debugging
 | 
|---|
| 328 |  * \param *T Tesselation containing boundary points
 | 
|---|
| 329 |  * \param *&x random point set on return (not allocated!)
 | 
|---|
| 330 |  * \param PointsToPick number of points in set to pick
 | 
|---|
| 331 |  */
 | 
|---|
| 332 | void PickRandomPointSet(class Tesselation *T, Vector *&x, size_t PointsToPick)
 | 
|---|
| 333 | {
 | 
|---|
| 334 |   size_t PointsLeft = (size_t) T->PointsOnBoundaryCount;
 | 
|---|
| 335 |   size_t PointsPicked = 0;
 | 
|---|
| 336 |   double value, threshold;
 | 
|---|
| 337 |   PointMap *List = &T->PointsOnBoundary;
 | 
|---|
| 338 |   DoLog(2) && (Log() << Verbose(2) << "Begin of PickRandomPointSet" << endl);
 | 
|---|
| 339 | 
 | 
|---|
| 340 |   // allocate array
 | 
|---|
| 341 |   if (x == NULL) {
 | 
|---|
| 342 |     x = new Vector[PointsToPick];
 | 
|---|
| 343 |   } else {
 | 
|---|
| 344 |     DoeLog(2) && (eLog()<< Verbose(2) << "Given pointer to vector array seems already allocated." << endl);
 | 
|---|
| 345 |   }
 | 
|---|
| 346 | 
 | 
|---|
| 347 |   if (List != NULL)
 | 
|---|
| 348 |     for (PointMap::iterator Runner = List->begin(); Runner != List->end(); Runner++) {
 | 
|---|
| 349 |       threshold = 1. - (double)(PointsToPick - PointsPicked)/(double)PointsLeft;
 | 
|---|
| 350 |       value = (double)rand()/(double)RAND_MAX;
 | 
|---|
| 351 |       //Log() << Verbose(3) << "Current node is " << *Runner->second->node << " with " << value << " ... " << threshold << ": ";
 | 
|---|
| 352 |       if (value > threshold) {
 | 
|---|
| 353 |         x[PointsPicked] = (*Runner->second->node->node);
 | 
|---|
| 354 |         PointsPicked++;
 | 
|---|
| 355 |         //Log() << Verbose(0) << "IN." << endl;
 | 
|---|
| 356 |       } else {
 | 
|---|
| 357 |         //Log() << Verbose(0) << "OUT." << endl;
 | 
|---|
| 358 |       }
 | 
|---|
| 359 |       PointsLeft--;
 | 
|---|
| 360 |     }
 | 
|---|
| 361 |   DoLog(2) && (Log() << Verbose(2) << "The following points were picked: " << endl);
 | 
|---|
| 362 |   for (size_t i=0;i<PointsPicked;i++)
 | 
|---|
| 363 |     DoLog(3) && (Log() << Verbose(3) << x[i] << endl);
 | 
|---|
| 364 | 
 | 
|---|
| 365 |   DoLog(2) && (Log() << Verbose(2) << "End of PickRandomPointSet" << endl);
 | 
|---|
| 366 | };
 | 
|---|
| 367 | 
 | 
|---|
| 368 | /** Finds best fitting ellipsoid parameter set in least square sense for a given point set.
 | 
|---|
| 369 |  * \param *out output stream for debugging
 | 
|---|
| 370 |  * \param *T Tesselation containing boundary points
 | 
|---|
| 371 |  * \param *LCList linked cell list of all atoms
 | 
|---|
| 372 |  * \param N number of unique points in ellipsoid fit, must be greater equal 6
 | 
|---|
| 373 |  * \param number of fits (i.e. parameter sets in output file)
 | 
|---|
| 374 |  * \param *filename name for output file
 | 
|---|
| 375 |  */
 | 
|---|
| 376 | void FindDistributionOfEllipsoids(class Tesselation *T, class LinkedCell *LCList, int N, int number, const char *filename)
 | 
|---|
| 377 | {
 | 
|---|
| 378 |   ofstream output;
 | 
|---|
| 379 |   Vector *x = NULL;
 | 
|---|
| 380 |   Vector Center;
 | 
|---|
| 381 |   Vector EllipsoidCenter;
 | 
|---|
| 382 |   double EllipsoidLength[3];
 | 
|---|
| 383 |   double EllipsoidAngle[3];
 | 
|---|
| 384 |   double distance, MaxDistance, MinDistance;
 | 
|---|
| 385 |   DoLog(0) && (Log() << Verbose(0) << "Begin of FindDistributionOfEllipsoids" << endl);
 | 
|---|
| 386 | 
 | 
|---|
| 387 |   // construct center of gravity of boundary point set for initial ellipsoid center
 | 
|---|
| 388 |   Center.Zero();
 | 
|---|
| 389 |   for (PointMap::iterator Runner = T->PointsOnBoundary.begin(); Runner != T->PointsOnBoundary.end(); Runner++)
 | 
|---|
| 390 |     Center += (*Runner->second->node->node);
 | 
|---|
| 391 |   Center.Scale(1./T->PointsOnBoundaryCount);
 | 
|---|
| 392 |   DoLog(1) && (Log() << Verbose(1) << "Center is at " << Center << "." << endl);
 | 
|---|
| 393 | 
 | 
|---|
| 394 |   // Output header
 | 
|---|
| 395 |   output.open(filename, ios::trunc);
 | 
|---|
| 396 |   output << "# Nr.\tCenterX\tCenterY\tCenterZ\ta\tb\tc\tpsi\ttheta\tphi" << endl;
 | 
|---|
| 397 | 
 | 
|---|
| 398 |   // loop over desired number of parameter sets
 | 
|---|
| 399 |   for (;number >0;number--) {
 | 
|---|
| 400 |     DoLog(1) && (Log() << Verbose(1) << "Determining data set " << number << " ... " << endl);
 | 
|---|
| 401 |     // pick the point set
 | 
|---|
| 402 |     x = NULL;
 | 
|---|
| 403 |     //PickRandomPointSet(T, LCList, x, N);
 | 
|---|
| 404 |     PickRandomNeighbouredPointSet(T, LCList, x, N);
 | 
|---|
| 405 | 
 | 
|---|
| 406 |     // calculate some sensible starting values for parameter fit
 | 
|---|
| 407 |     MaxDistance = 0.;
 | 
|---|
| 408 |     MinDistance = x[0].ScalarProduct(x[0]);
 | 
|---|
| 409 |     for (int i=0;i<N;i++) {
 | 
|---|
| 410 |       distance = x[i].ScalarProduct(x[i]);
 | 
|---|
| 411 |       if (distance > MaxDistance)
 | 
|---|
| 412 |         MaxDistance = distance;
 | 
|---|
| 413 |       if (distance < MinDistance)
 | 
|---|
| 414 |         MinDistance = distance;
 | 
|---|
| 415 |     }
 | 
|---|
| 416 |     //Log() << Verbose(2) << "MinDistance " << MinDistance << ", MaxDistance " << MaxDistance << "." << endl;
 | 
|---|
| 417 |     EllipsoidCenter = Center;  // use Center of Gravity as initial center of ellipsoid
 | 
|---|
| 418 |     for (int i=0;i<3;i++)
 | 
|---|
| 419 |       EllipsoidAngle[i] = 0.;
 | 
|---|
| 420 |     EllipsoidLength[0] = sqrt(MaxDistance);
 | 
|---|
| 421 |     EllipsoidLength[1] = sqrt((MaxDistance+MinDistance)/2.);
 | 
|---|
| 422 |     EllipsoidLength[2] = sqrt(MinDistance);
 | 
|---|
| 423 | 
 | 
|---|
| 424 |     // fit the parameters
 | 
|---|
| 425 |     if (FitPointSetToEllipsoid(x, N, &EllipsoidCenter, &EllipsoidLength[0], &EllipsoidAngle[0])) {
 | 
|---|
| 426 |       DoLog(1) && (Log() << Verbose(1) << "Picking succeeded!" << endl);
 | 
|---|
| 427 |       // output obtained parameter set
 | 
|---|
| 428 |       output << number << "\t";
 | 
|---|
| 429 |       for (int i=0;i<3;i++)
 | 
|---|
| 430 |         output << setprecision(9) << EllipsoidCenter[i] << "\t";
 | 
|---|
| 431 |       for (int i=0;i<3;i++)
 | 
|---|
| 432 |         output << setprecision(9) << EllipsoidLength[i] << "\t";
 | 
|---|
| 433 |       for (int i=0;i<3;i++)
 | 
|---|
| 434 |         output << setprecision(9) << EllipsoidAngle[i] << "\t";
 | 
|---|
| 435 |       output << endl;
 | 
|---|
| 436 |     } else { // increase N to pick one more
 | 
|---|
| 437 |       DoLog(1) && (Log() << Verbose(1) << "Picking failed!" << endl);
 | 
|---|
| 438 |       number++;
 | 
|---|
| 439 |     }
 | 
|---|
| 440 |     delete[](x);  // free allocated memory for point set
 | 
|---|
| 441 |   }
 | 
|---|
| 442 |   // close output and finish
 | 
|---|
| 443 |   output.close();
 | 
|---|
| 444 | 
 | 
|---|
| 445 |   DoLog(0) && (Log() << Verbose(0) << "End of FindDistributionOfEllipsoids" << endl);
 | 
|---|
| 446 | };
 | 
|---|