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