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