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