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