| 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 "BoundaryPointSet.hpp" | 
|---|
| 18 | #include "boundary.hpp" | 
|---|
| 19 | #include "ellipsoid.hpp" | 
|---|
| 20 | #include "linkedcell.hpp" | 
|---|
| 21 | #include "Helpers/Log.hpp" | 
|---|
| 22 | #include "tesselation.hpp" | 
|---|
| 23 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 24 | #include "LinearAlgebra/Matrix.hpp" | 
|---|
| 25 | #include "Helpers/Verbose.hpp" | 
|---|
| 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 | { | 
|---|
| 36 | Vector helper, RefPoint; | 
|---|
| 37 | double distance = -1.; | 
|---|
| 38 | Matrix Matrix; | 
|---|
| 39 | double InverseLength[3]; | 
|---|
| 40 | double psi,theta,phi; // euler angles in ZX'Z'' convention | 
|---|
| 41 |  | 
|---|
| 42 | //Log() << Verbose(3) << "Begin of SquaredDistanceToEllipsoid" << endl; | 
|---|
| 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 | 
|---|
| 48 | RefPoint = helper = x - EllipsoidCenter; | 
|---|
| 49 | //Log() << Verbose(4) << "Translated given point is at " << RefPoint << "." << endl; | 
|---|
| 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]; | 
|---|
| 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)); | 
|---|
| 64 | helper *= Matrix; | 
|---|
| 65 | helper.ScaleAll(InverseLength); | 
|---|
| 66 | //Log() << Verbose(4) << "Transformed RefPoint is at " << helper << "." << endl; | 
|---|
| 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 | 
|---|
| 70 | //Log() << Verbose(4) << "Transformed intersection is at " << helper << "." << endl; | 
|---|
| 71 |  | 
|---|
| 72 | // 4. transform back the constructed intersection point | 
|---|
| 73 | psi = -EllipsoidAngle[0]; | 
|---|
| 74 | theta = -EllipsoidAngle[1]; | 
|---|
| 75 | phi = -EllipsoidAngle[2]; | 
|---|
| 76 | helper.ScaleAll(EllipsoidLength); | 
|---|
| 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)); | 
|---|
| 86 | helper *= Matrix; | 
|---|
| 87 | //Log() << Verbose(4) << "Intersection is at " << helper << "." << endl; | 
|---|
| 88 |  | 
|---|
| 89 | // 5. determine distance between backtransformed point and x | 
|---|
| 90 | distance = RefPoint.DistanceSquared(helper); | 
|---|
| 91 | //Log() << Verbose(4) << "Squared distance between intersection and RefPoint is " << distance << "." << endl; | 
|---|
| 92 |  | 
|---|
| 93 | return distance; | 
|---|
| 94 | //Log() << Verbose(3) << "End of SquaredDistanceToEllipsoid" << endl; | 
|---|
| 95 | }; | 
|---|
| 96 |  | 
|---|
| 97 | /** structure for ellipsoid minimisation containing points to fit to. | 
|---|
| 98 | */ | 
|---|
| 99 | struct EllipsoidMinimisation { | 
|---|
| 100 | int N;      //!< dimension of vector set | 
|---|
| 101 | Vector *x;  //!< array of vectors | 
|---|
| 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 | { | 
|---|
| 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++) { | 
|---|
| 120 | Center[i] = gsl_vector_get(x, i+0); | 
|---|
| 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 |  | 
|---|
| 136 | //Log() << Verbose(0) << "Current summed distance is " << SumDistance << "." << endl; | 
|---|
| 137 | return SumDistance; | 
|---|
| 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 | */ | 
|---|
| 147 | bool FitPointSetToEllipsoid(Vector *set, int N, Vector *EllipsoidCenter, double *EllipsoidLength, double *EllipsoidAngle) | 
|---|
| 148 | { | 
|---|
| 149 | int status = GSL_SUCCESS; | 
|---|
| 150 | DoLog(2) && (Log() << Verbose(2) << "Begin of FitPointSetToEllipsoid " << endl); | 
|---|
| 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++) { | 
|---|
| 164 | gsl_vector_set (x, i+0, EllipsoidCenter->at(i)); | 
|---|
| 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++) { | 
|---|
| 199 | EllipsoidCenter->at(i) = gsl_vector_get (s->x,i+0); | 
|---|
| 200 | EllipsoidLength[i] = gsl_vector_get (s->x, i+3); | 
|---|
| 201 | EllipsoidAngle[i] = gsl_vector_get (s->x, i+6); | 
|---|
| 202 | } | 
|---|
| 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); | 
|---|
| 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 { | 
|---|
| 213 | DoLog(3) && (Log() << Verbose(3) << "Not enough points provided for fit to ellipsoid." << endl); | 
|---|
| 214 | return false; | 
|---|
| 215 | } | 
|---|
| 216 | DoLog(2) && (Log() << Verbose(2) << "End of FitPointSetToEllipsoid" << endl); | 
|---|
| 217 | if (status == GSL_SUCCESS) | 
|---|
| 218 | return true; | 
|---|
| 219 | else | 
|---|
| 220 | return false; | 
|---|
| 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 | */ | 
|---|
| 230 | void PickRandomNeighbouredPointSet(class Tesselation *T, class LinkedCell *LC, Vector *&x, size_t PointsToPick) | 
|---|
| 231 | { | 
|---|
| 232 | size_t PointsLeft = 0; | 
|---|
| 233 | size_t PointsPicked = 0; | 
|---|
| 234 | int Nlower[NDIM], Nupper[NDIM]; | 
|---|
| 235 | set<int> PickedAtomNrs;   // ordered list of picked atoms | 
|---|
| 236 | set<int>::iterator current; | 
|---|
| 237 | int index; | 
|---|
| 238 | TesselPoint *Candidate = NULL; | 
|---|
| 239 | DoLog(2) && (Log() << Verbose(2) << "Begin of PickRandomPointSet" << endl); | 
|---|
| 240 |  | 
|---|
| 241 | // allocate array | 
|---|
| 242 | if (x == NULL) { | 
|---|
| 243 | x = new Vector[PointsToPick]; | 
|---|
| 244 | } else { | 
|---|
| 245 | DoeLog(2) && (eLog()<< Verbose(2) << "Given pointer to vector array seems already allocated." << endl); | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | do { | 
|---|
| 249 | for(int i=0;i<NDIM;i++) // pick three random indices | 
|---|
| 250 | LC->n[i] = (rand() % LC->N[i]); | 
|---|
| 251 | DoLog(2) && (Log() << Verbose(2) << "INFO: Center cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " ... "); | 
|---|
| 252 | // get random cell | 
|---|
| 253 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell(); | 
|---|
| 254 | if (List == NULL) {  // set index to it | 
|---|
| 255 | continue; | 
|---|
| 256 | } | 
|---|
| 257 | DoLog(2) && (Log() << Verbose(2) << "with No. " << LC->index << "." << endl); | 
|---|
| 258 |  | 
|---|
| 259 | DoLog(2) && (Log() << Verbose(2) << "LC Intervals:"); | 
|---|
| 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; | 
|---|
| 263 | DoLog(0) && (Log() << Verbose(0) << " [" << Nlower[i] << "," << Nupper[i] << "] "); | 
|---|
| 264 | } | 
|---|
| 265 | DoLog(0) && (Log() << Verbose(0) << endl); | 
|---|
| 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]++) { | 
|---|
| 272 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell(); | 
|---|
| 273 | PointsLeft += List->size(); | 
|---|
| 274 | } | 
|---|
| 275 | DoLog(2) && (Log() << Verbose(2) << "There are " << PointsLeft << " atoms in this neighbourhood." << endl); | 
|---|
| 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()) { | 
|---|
| 286 | //Log() << Verbose(2) << "Picking atom nr. " << index << "." << endl; | 
|---|
| 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]++) { | 
|---|
| 297 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell(); | 
|---|
| 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; | 
|---|
| 299 | if (List != NULL) { | 
|---|
| 300 | //            if (List->begin() != List->end()) | 
|---|
| 301 | //              Log() << Verbose(2) << "Going through candidates ... " << endl; | 
|---|
| 302 | //            else | 
|---|
| 303 | //              Log() << Verbose(2) << "Cell is empty ... " << endl; | 
|---|
| 304 | for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
| 305 | if ((current != PickedAtomNrs.end()) && (*current == index)) { | 
|---|
| 306 | Candidate = (*Runner); | 
|---|
| 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 | 
|---|
| 309 | current++;    // next pre-picked atom | 
|---|
| 310 | } | 
|---|
| 311 | index++;  // next atom nr. | 
|---|
| 312 | } | 
|---|
| 313 | //          } else { | 
|---|
| 314 | //            Log() << Verbose(2) << "List for this index not allocated!" << endl; | 
|---|
| 315 | } | 
|---|
| 316 | } | 
|---|
| 317 | DoLog(2) && (Log() << Verbose(2) << "The following points were picked: " << endl); | 
|---|
| 318 | for (size_t i=0;i<PointsPicked;i++) | 
|---|
| 319 | DoLog(2) && (Log() << Verbose(2) << x[i] << endl); | 
|---|
| 320 | if (PointsPicked == PointsToPick)  // break out of loop if we have all | 
|---|
| 321 | break; | 
|---|
| 322 | } while(1); | 
|---|
| 323 |  | 
|---|
| 324 | DoLog(2) && (Log() << Verbose(2) << "End of PickRandomPointSet" << endl); | 
|---|
| 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 | */ | 
|---|
| 333 | void PickRandomPointSet(class Tesselation *T, Vector *&x, size_t PointsToPick) | 
|---|
| 334 | { | 
|---|
| 335 | size_t PointsLeft = (size_t) T->PointsOnBoundaryCount; | 
|---|
| 336 | size_t PointsPicked = 0; | 
|---|
| 337 | double value, threshold; | 
|---|
| 338 | PointMap *List = &T->PointsOnBoundary; | 
|---|
| 339 | DoLog(2) && (Log() << Verbose(2) << "Begin of PickRandomPointSet" << endl); | 
|---|
| 340 |  | 
|---|
| 341 | // allocate array | 
|---|
| 342 | if (x == NULL) { | 
|---|
| 343 | x = new Vector[PointsToPick]; | 
|---|
| 344 | } else { | 
|---|
| 345 | DoeLog(2) && (eLog()<< Verbose(2) << "Given pointer to vector array seems already allocated." << endl); | 
|---|
| 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; | 
|---|
| 352 | //Log() << Verbose(3) << "Current node is " << *Runner->second->node << " with " << value << " ... " << threshold << ": "; | 
|---|
| 353 | if (value > threshold) { | 
|---|
| 354 | x[PointsPicked] = (Runner->second->node->getPosition()); | 
|---|
| 355 | PointsPicked++; | 
|---|
| 356 | //Log() << Verbose(0) << "IN." << endl; | 
|---|
| 357 | } else { | 
|---|
| 358 | //Log() << Verbose(0) << "OUT." << endl; | 
|---|
| 359 | } | 
|---|
| 360 | PointsLeft--; | 
|---|
| 361 | } | 
|---|
| 362 | DoLog(2) && (Log() << Verbose(2) << "The following points were picked: " << endl); | 
|---|
| 363 | for (size_t i=0;i<PointsPicked;i++) | 
|---|
| 364 | DoLog(3) && (Log() << Verbose(3) << x[i] << endl); | 
|---|
| 365 |  | 
|---|
| 366 | DoLog(2) && (Log() << Verbose(2) << "End of PickRandomPointSet" << endl); | 
|---|
| 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 | */ | 
|---|
| 377 | void FindDistributionOfEllipsoids(class Tesselation *T, class LinkedCell *LCList, int N, int number, const char *filename) | 
|---|
| 378 | { | 
|---|
| 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; | 
|---|
| 386 | DoLog(0) && (Log() << Verbose(0) << "Begin of FindDistributionOfEllipsoids" << endl); | 
|---|
| 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++) | 
|---|
| 391 | Center += (Runner->second->node->getPosition()); | 
|---|
| 392 | Center.Scale(1./T->PointsOnBoundaryCount); | 
|---|
| 393 | DoLog(1) && (Log() << Verbose(1) << "Center is at " << Center << "." << endl); | 
|---|
| 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--) { | 
|---|
| 401 | DoLog(1) && (Log() << Verbose(1) << "Determining data set " << number << " ... " << endl); | 
|---|
| 402 | // pick the point set | 
|---|
| 403 | x = NULL; | 
|---|
| 404 | //PickRandomPointSet(T, LCList, x, N); | 
|---|
| 405 | PickRandomNeighbouredPointSet(T, LCList, x, N); | 
|---|
| 406 |  | 
|---|
| 407 | // calculate some sensible starting values for parameter fit | 
|---|
| 408 | MaxDistance = 0.; | 
|---|
| 409 | MinDistance = x[0].ScalarProduct(x[0]); | 
|---|
| 410 | for (int i=0;i<N;i++) { | 
|---|
| 411 | distance = x[i].ScalarProduct(x[i]); | 
|---|
| 412 | if (distance > MaxDistance) | 
|---|
| 413 | MaxDistance = distance; | 
|---|
| 414 | if (distance < MinDistance) | 
|---|
| 415 | MinDistance = distance; | 
|---|
| 416 | } | 
|---|
| 417 | //Log() << Verbose(2) << "MinDistance " << MinDistance << ", MaxDistance " << MaxDistance << "." << endl; | 
|---|
| 418 | EllipsoidCenter = Center;  // use Center of Gravity as initial center of ellipsoid | 
|---|
| 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 | 
|---|
| 426 | if (FitPointSetToEllipsoid(x, N, &EllipsoidCenter, &EllipsoidLength[0], &EllipsoidAngle[0])) { | 
|---|
| 427 | DoLog(1) && (Log() << Verbose(1) << "Picking succeeded!" << endl); | 
|---|
| 428 | // output obtained parameter set | 
|---|
| 429 | output << number << "\t"; | 
|---|
| 430 | for (int i=0;i<3;i++) | 
|---|
| 431 | output << setprecision(9) << EllipsoidCenter[i] << "\t"; | 
|---|
| 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 | 
|---|
| 438 | DoLog(1) && (Log() << Verbose(1) << "Picking failed!" << endl); | 
|---|
| 439 | number++; | 
|---|
| 440 | } | 
|---|
| 441 | delete[](x);  // free allocated memory for point set | 
|---|
| 442 | } | 
|---|
| 443 | // close output and finish | 
|---|
| 444 | output.close(); | 
|---|
| 445 |  | 
|---|
| 446 | DoLog(0) && (Log() << Verbose(0) << "End of FindDistributionOfEllipsoids" << endl); | 
|---|
| 447 | }; | 
|---|