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