1 | /*
|
---|
2 | * ellipsoid.cpp
|
---|
3 | *
|
---|
4 | * Created on: Jan 20, 2009
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | // include config.h
|
---|
9 | #ifdef HAVE_CONFIG_H
|
---|
10 | #include <config.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include "Helpers/MemDebug.hpp"
|
---|
14 |
|
---|
15 | #include <gsl/gsl_multimin.h>
|
---|
16 | #include <gsl/gsl_vector.h>
|
---|
17 |
|
---|
18 | #include <iomanip>
|
---|
19 |
|
---|
20 | #include <set>
|
---|
21 |
|
---|
22 | #include "BoundaryPointSet.hpp"
|
---|
23 | #include "boundary.hpp"
|
---|
24 | #include "ellipsoid.hpp"
|
---|
25 | #include "linkedcell.hpp"
|
---|
26 | #include "Helpers/Log.hpp"
|
---|
27 | #include "tesselation.hpp"
|
---|
28 | #include "LinearAlgebra/Vector.hpp"
|
---|
29 | #include "LinearAlgebra/Matrix.hpp"
|
---|
30 | #include "Helpers/Verbose.hpp"
|
---|
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 | {
|
---|
41 | Vector helper, RefPoint;
|
---|
42 | double distance = -1.;
|
---|
43 | Matrix Matrix;
|
---|
44 | double InverseLength[3];
|
---|
45 | double psi,theta,phi; // euler angles in ZX'Z'' convention
|
---|
46 |
|
---|
47 | //Log() << Verbose(3) << "Begin of SquaredDistanceToEllipsoid" << endl;
|
---|
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
|
---|
53 | RefPoint = helper = x - EllipsoidCenter;
|
---|
54 | //Log() << Verbose(4) << "Translated given point is at " << RefPoint << "." << endl;
|
---|
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];
|
---|
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));
|
---|
69 | helper *= Matrix;
|
---|
70 | helper.ScaleAll(InverseLength);
|
---|
71 | //Log() << Verbose(4) << "Transformed RefPoint is at " << helper << "." << endl;
|
---|
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
|
---|
75 | //Log() << Verbose(4) << "Transformed intersection is at " << helper << "." << endl;
|
---|
76 |
|
---|
77 | // 4. transform back the constructed intersection point
|
---|
78 | psi = -EllipsoidAngle[0];
|
---|
79 | theta = -EllipsoidAngle[1];
|
---|
80 | phi = -EllipsoidAngle[2];
|
---|
81 | helper.ScaleAll(EllipsoidLength);
|
---|
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));
|
---|
91 | helper *= Matrix;
|
---|
92 | //Log() << Verbose(4) << "Intersection is at " << helper << "." << endl;
|
---|
93 |
|
---|
94 | // 5. determine distance between backtransformed point and x
|
---|
95 | distance = RefPoint.DistanceSquared(helper);
|
---|
96 | //Log() << Verbose(4) << "Squared distance between intersection and RefPoint is " << distance << "." << endl;
|
---|
97 |
|
---|
98 | return distance;
|
---|
99 | //Log() << Verbose(3) << "End of SquaredDistanceToEllipsoid" << endl;
|
---|
100 | };
|
---|
101 |
|
---|
102 | /** structure for ellipsoid minimisation containing points to fit to.
|
---|
103 | */
|
---|
104 | struct EllipsoidMinimisation {
|
---|
105 | int N; //!< dimension of vector set
|
---|
106 | Vector *x; //!< array of vectors
|
---|
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 | {
|
---|
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++) {
|
---|
125 | Center[i] = gsl_vector_get(x, i+0);
|
---|
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 |
|
---|
141 | //Log() << Verbose(0) << "Current summed distance is " << SumDistance << "." << endl;
|
---|
142 | return SumDistance;
|
---|
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 | */
|
---|
152 | bool FitPointSetToEllipsoid(Vector *set, int N, Vector *EllipsoidCenter, double *EllipsoidLength, double *EllipsoidAngle)
|
---|
153 | {
|
---|
154 | int status = GSL_SUCCESS;
|
---|
155 | DoLog(2) && (Log() << Verbose(2) << "Begin of FitPointSetToEllipsoid " << endl);
|
---|
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++) {
|
---|
169 | gsl_vector_set (x, i+0, EllipsoidCenter->at(i));
|
---|
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++) {
|
---|
204 | EllipsoidCenter->at(i) = gsl_vector_get (s->x,i+0);
|
---|
205 | EllipsoidLength[i] = gsl_vector_get (s->x, i+3);
|
---|
206 | EllipsoidAngle[i] = gsl_vector_get (s->x, i+6);
|
---|
207 | }
|
---|
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);
|
---|
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 {
|
---|
218 | DoLog(3) && (Log() << Verbose(3) << "Not enough points provided for fit to ellipsoid." << endl);
|
---|
219 | return false;
|
---|
220 | }
|
---|
221 | DoLog(2) && (Log() << Verbose(2) << "End of FitPointSetToEllipsoid" << endl);
|
---|
222 | if (status == GSL_SUCCESS)
|
---|
223 | return true;
|
---|
224 | else
|
---|
225 | return false;
|
---|
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 | */
|
---|
235 | void PickRandomNeighbouredPointSet(class Tesselation *T, class LinkedCell *LC, Vector *&x, size_t PointsToPick)
|
---|
236 | {
|
---|
237 | size_t PointsLeft = 0;
|
---|
238 | size_t PointsPicked = 0;
|
---|
239 | int Nlower[NDIM], Nupper[NDIM];
|
---|
240 | set<int> PickedAtomNrs; // ordered list of picked atoms
|
---|
241 | set<int>::iterator current;
|
---|
242 | int index;
|
---|
243 | TesselPoint *Candidate = NULL;
|
---|
244 | DoLog(2) && (Log() << Verbose(2) << "Begin of PickRandomPointSet" << endl);
|
---|
245 |
|
---|
246 | // allocate array
|
---|
247 | if (x == NULL) {
|
---|
248 | x = new Vector[PointsToPick];
|
---|
249 | } else {
|
---|
250 | DoeLog(2) && (eLog()<< Verbose(2) << "Given pointer to vector array seems already allocated." << endl);
|
---|
251 | }
|
---|
252 |
|
---|
253 | do {
|
---|
254 | for(int i=0;i<NDIM;i++) // pick three random indices
|
---|
255 | LC->n[i] = (rand() % LC->N[i]);
|
---|
256 | DoLog(2) && (Log() << Verbose(2) << "INFO: Center cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " ... ");
|
---|
257 | // get random cell
|
---|
258 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
|
---|
259 | if (List == NULL) { // set index to it
|
---|
260 | continue;
|
---|
261 | }
|
---|
262 | DoLog(2) && (Log() << Verbose(2) << "with No. " << LC->index << "." << endl);
|
---|
263 |
|
---|
264 | DoLog(2) && (Log() << Verbose(2) << "LC Intervals:");
|
---|
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;
|
---|
268 | DoLog(0) && (Log() << Verbose(0) << " [" << Nlower[i] << "," << Nupper[i] << "] ");
|
---|
269 | }
|
---|
270 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
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]++) {
|
---|
277 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
|
---|
278 | PointsLeft += List->size();
|
---|
279 | }
|
---|
280 | DoLog(2) && (Log() << Verbose(2) << "There are " << PointsLeft << " atoms in this neighbourhood." << endl);
|
---|
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()) {
|
---|
291 | //Log() << Verbose(2) << "Picking atom nr. " << index << "." << endl;
|
---|
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]++) {
|
---|
302 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
|
---|
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;
|
---|
304 | if (List != NULL) {
|
---|
305 | // if (List->begin() != List->end())
|
---|
306 | // Log() << Verbose(2) << "Going through candidates ... " << endl;
|
---|
307 | // else
|
---|
308 | // Log() << Verbose(2) << "Cell is empty ... " << endl;
|
---|
309 | for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
|
---|
310 | if ((current != PickedAtomNrs.end()) && (*current == index)) {
|
---|
311 | Candidate = (*Runner);
|
---|
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
|
---|
314 | current++; // next pre-picked atom
|
---|
315 | }
|
---|
316 | index++; // next atom nr.
|
---|
317 | }
|
---|
318 | // } else {
|
---|
319 | // Log() << Verbose(2) << "List for this index not allocated!" << endl;
|
---|
320 | }
|
---|
321 | }
|
---|
322 | DoLog(2) && (Log() << Verbose(2) << "The following points were picked: " << endl);
|
---|
323 | for (size_t i=0;i<PointsPicked;i++)
|
---|
324 | DoLog(2) && (Log() << Verbose(2) << x[i] << endl);
|
---|
325 | if (PointsPicked == PointsToPick) // break out of loop if we have all
|
---|
326 | break;
|
---|
327 | } while(1);
|
---|
328 |
|
---|
329 | DoLog(2) && (Log() << Verbose(2) << "End of PickRandomPointSet" << endl);
|
---|
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 | */
|
---|
338 | void PickRandomPointSet(class Tesselation *T, Vector *&x, size_t PointsToPick)
|
---|
339 | {
|
---|
340 | size_t PointsLeft = (size_t) T->PointsOnBoundaryCount;
|
---|
341 | size_t PointsPicked = 0;
|
---|
342 | double value, threshold;
|
---|
343 | PointMap *List = &T->PointsOnBoundary;
|
---|
344 | DoLog(2) && (Log() << Verbose(2) << "Begin of PickRandomPointSet" << endl);
|
---|
345 |
|
---|
346 | // allocate array
|
---|
347 | if (x == NULL) {
|
---|
348 | x = new Vector[PointsToPick];
|
---|
349 | } else {
|
---|
350 | DoeLog(2) && (eLog()<< Verbose(2) << "Given pointer to vector array seems already allocated." << endl);
|
---|
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;
|
---|
357 | //Log() << Verbose(3) << "Current node is " << *Runner->second->node << " with " << value << " ... " << threshold << ": ";
|
---|
358 | if (value > threshold) {
|
---|
359 | x[PointsPicked] = (Runner->second->node->getPosition());
|
---|
360 | PointsPicked++;
|
---|
361 | //Log() << Verbose(0) << "IN." << endl;
|
---|
362 | } else {
|
---|
363 | //Log() << Verbose(0) << "OUT." << endl;
|
---|
364 | }
|
---|
365 | PointsLeft--;
|
---|
366 | }
|
---|
367 | DoLog(2) && (Log() << Verbose(2) << "The following points were picked: " << endl);
|
---|
368 | for (size_t i=0;i<PointsPicked;i++)
|
---|
369 | DoLog(3) && (Log() << Verbose(3) << x[i] << endl);
|
---|
370 |
|
---|
371 | DoLog(2) && (Log() << Verbose(2) << "End of PickRandomPointSet" << endl);
|
---|
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 | */
|
---|
382 | void FindDistributionOfEllipsoids(class Tesselation *T, class LinkedCell *LCList, int N, int number, const char *filename)
|
---|
383 | {
|
---|
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;
|
---|
391 | DoLog(0) && (Log() << Verbose(0) << "Begin of FindDistributionOfEllipsoids" << endl);
|
---|
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++)
|
---|
396 | Center += (Runner->second->node->getPosition());
|
---|
397 | Center.Scale(1./T->PointsOnBoundaryCount);
|
---|
398 | DoLog(1) && (Log() << Verbose(1) << "Center is at " << Center << "." << endl);
|
---|
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--) {
|
---|
406 | DoLog(1) && (Log() << Verbose(1) << "Determining data set " << number << " ... " << endl);
|
---|
407 | // pick the point set
|
---|
408 | x = NULL;
|
---|
409 | //PickRandomPointSet(T, LCList, x, N);
|
---|
410 | PickRandomNeighbouredPointSet(T, LCList, x, N);
|
---|
411 |
|
---|
412 | // calculate some sensible starting values for parameter fit
|
---|
413 | MaxDistance = 0.;
|
---|
414 | MinDistance = x[0].ScalarProduct(x[0]);
|
---|
415 | for (int i=0;i<N;i++) {
|
---|
416 | distance = x[i].ScalarProduct(x[i]);
|
---|
417 | if (distance > MaxDistance)
|
---|
418 | MaxDistance = distance;
|
---|
419 | if (distance < MinDistance)
|
---|
420 | MinDistance = distance;
|
---|
421 | }
|
---|
422 | //Log() << Verbose(2) << "MinDistance " << MinDistance << ", MaxDistance " << MaxDistance << "." << endl;
|
---|
423 | EllipsoidCenter = Center; // use Center of Gravity as initial center of ellipsoid
|
---|
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
|
---|
431 | if (FitPointSetToEllipsoid(x, N, &EllipsoidCenter, &EllipsoidLength[0], &EllipsoidAngle[0])) {
|
---|
432 | DoLog(1) && (Log() << Verbose(1) << "Picking succeeded!" << endl);
|
---|
433 | // output obtained parameter set
|
---|
434 | output << number << "\t";
|
---|
435 | for (int i=0;i<3;i++)
|
---|
436 | output << setprecision(9) << EllipsoidCenter[i] << "\t";
|
---|
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
|
---|
443 | DoLog(1) && (Log() << Verbose(1) << "Picking failed!" << endl);
|
---|
444 | number++;
|
---|
445 | }
|
---|
446 | delete[](x); // free allocated memory for point set
|
---|
447 | }
|
---|
448 | // close output and finish
|
---|
449 | output.close();
|
---|
450 |
|
---|
451 | DoLog(0) && (Log() << Verbose(0) << "End of FindDistributionOfEllipsoids" << endl);
|
---|
452 | };
|
---|