source: src/Fragmentation/Exporters/SphericalPointDistribution.cpp@ 2971aa

Last change on this file since 2971aa was 2971aa, checked in by Frederik Heber <heber@…>, 11 years ago

tempcommit: Fixes to getAssociatedPoints().

  • Property mode set to 100644
File size: 50.4 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2014 Frederik Heber. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * SphericalPointDistribution.cpp
25 *
26 * Created on: May 30, 2014
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "CodePatterns/MemDebug.hpp"
36
37#include "SphericalPointDistribution.hpp"
38
39#include "CodePatterns/Assert.hpp"
40#include "CodePatterns/IteratorAdaptors.hpp"
41#include "CodePatterns/Log.hpp"
42#include "CodePatterns/toString.hpp"
43
44#include <algorithm>
45#include <boost/assign.hpp>
46#include <cmath>
47#include <functional>
48#include <iterator>
49#include <limits>
50#include <list>
51#include <numeric>
52#include <vector>
53#include <map>
54
55#include "LinearAlgebra/Line.hpp"
56#include "LinearAlgebra/Plane.hpp"
57#include "LinearAlgebra/RealSpaceMatrix.hpp"
58#include "LinearAlgebra/Vector.hpp"
59
60using namespace boost::assign;
61
62// static entities
63const double SphericalPointDistribution::warn_amplitude = 1e-2;
64const double SphericalPointDistribution::L1THRESHOLD = 1e-2;
65const double SphericalPointDistribution::L2THRESHOLD = 2e-1;
66
67typedef std::vector<double> DistanceArray_t;
68
69// class generator: taken from www.cplusplus.com example std::generate
70struct c_unique {
71 unsigned int current;
72 c_unique() {current=0;}
73 unsigned int operator()() {return current++;}
74} UniqueNumber;
75
76struct c_unique_list {
77 unsigned int current;
78 c_unique_list() {current=0;}
79 std::list<unsigned int> operator()() {return std::list<unsigned int>(1, current++);}
80} UniqueNumberList;
81
82/** Calculate the center of a given set of points in \a _positions but only
83 * for those indicated by \a _indices.
84 *
85 */
86inline
87Vector calculateGeographicMidpoint(
88 const SphericalPointDistribution::VectorArray_t &_positions,
89 const SphericalPointDistribution::IndexList_t &_indices)
90{
91 Vector Center;
92 Center.Zero();
93 for (SphericalPointDistribution::IndexList_t::const_iterator iter = _indices.begin();
94 iter != _indices.end(); ++iter)
95 Center += _positions[*iter];
96 if (!_indices.empty())
97 Center *= 1./(double)_indices.size();
98
99 return Center;
100}
101
102inline
103double calculateMinimumDistance(
104 const Vector &_center,
105 const SphericalPointDistribution::VectorArray_t &_points,
106 const SphericalPointDistribution::IndexList_t & _indices)
107{
108 double MinimumDistance = 0.;
109 for (SphericalPointDistribution::IndexList_t::const_iterator iter = _indices.begin();
110 iter != _indices.end(); ++iter) {
111 const double angle = _center.Angle(_points[*iter]);
112 MinimumDistance += angle*angle;
113 }
114 return sqrt(MinimumDistance);
115}
116
117/** Calculates the center of minimum distance for a given set of points \a _points.
118 *
119 * According to http://www.geomidpoint.com/calculation.html this goes a follows:
120 * -# Let CurrentPoint be the geographic midpoint found in Method A. this is used as the starting point for the search.
121 * -# Let MinimumDistance be the sum total of all distances from the current point to all locations in 'Your Places'.
122 * -# Find the total distance between each location in 'Your Places' and all other locations in 'Your Places'. If any one of these locations has a new smallest distance then that location becomes the new CurrentPoint and MinimumDistance.
123 * -# Let TestDistance be PI/2 radians (6225 miles or 10018 km).
124 * -# Find the total distance between each of 8 test points and all locations in 'Your Places'. The test points are arranged in a circular pattern around the CurrentPoint at a distance of TestDistance to the north, northeast, east, southeast, south, southwest, west and northwest.
125 * -# If any of these 8 points has a new smallest distance then that point becomes the new CurrentPoint and MinimumDistance and go back to step 5 using that point.
126 * -# If none of the 8 test points has a new smallest distance then divide TestDistance by 2 and go back to step 5 using the same point.
127 * -# Repeat steps 5 to 7 until no new smallest distance can be found or until TestDistance is less than 0.00000002 radians.
128 *
129 * \param _points set of points
130 * \return Center of minimum distance for all these points, is always of length 1
131 */
132Vector SphericalPointDistribution::calculateCenterOfMinimumDistance(
133 const SphericalPointDistribution::VectorArray_t &_positions,
134 const SphericalPointDistribution::IndexList_t &_indices)
135{
136 ASSERT( _positions.size() >= _indices.size(),
137 "calculateCenterOfMinimumDistance() - less positions than indices given.");
138 Vector center(1.,0.,0.);
139
140 /// first treat some special cases
141 // no positions given: return x axis vector (NOT zero!)
142 {
143 if (_indices.empty())
144 return center;
145 // one position given: return it directly
146 if (_indices.size() == (size_t)1)
147 return _positions[*_indices.begin()];
148 // two positions on a line given: return closest point to (1.,0.,0.)
149// IndexList_t::const_iterator indexiter = _indices.begin();
150// const unsigned int firstindex = *indexiter++;
151// const unsigned int secondindex = *indexiter;
152// if ( fabs(_positions[firstindex].ScalarProduct(_positions[secondindex]) + 1.)
153// < std::numeric_limits<double>::epsilon()*1e4) {
154// Vector candidate;
155// if (_positions[firstindex].ScalarProduct(center) > _positions[secondindex].ScalarProduct(center))
156// candidate = _positions[firstindex];
157// else
158// candidate = _positions[secondindex];
159// // non-uniqueness: all positions on great circle, normal to given line are valid
160// // so, we just pick one because returning a unique point is topmost priority
161// Vector normal;
162// normal.GetOneNormalVector(candidate);
163// Vector othernormal = candidate;
164// othernormal.VectorProduct(normal);
165// // now both normal and othernormal describe the plane containing the great circle
166// Plane greatcircle(normal, zeroVec, othernormal);
167// // check which axis is contained and pick the one not
168// if (greatcircle.isContained(center)) {
169// center = Vector(0.,1.,0.);
170// if (greatcircle.isContained(center))
171// center = Vector(0.,0.,1.);
172// // now we are done cause a plane cannot contain all three axis vectors
173// }
174// center = greatcircle.getClosestPoint(candidate);
175// // assure length of 1
176// center.Normalize();
177//
178// return center;
179// }
180 // given points lie on a great circle and go completely round it
181 // two or more positions on a great circle given: return closest point to (1.,0.,0.)
182 {
183 bool AllNormal = true;
184 Vector Normal;
185 {
186 IndexList_t::const_iterator indexiter = _indices.begin();
187 Normal = _positions[*indexiter++];
188 Normal.VectorProduct(_positions[*indexiter++]);
189 Normal.Normalize();
190 for (;(AllNormal) && (indexiter != _indices.end()); ++indexiter)
191 AllNormal &= _positions[*indexiter].IsNormalTo(Normal, 1e-8);
192 }
193 double AngleSum = 0.;
194 if (AllNormal) {
195 // check by angle sum whether points go round are cover just one half
196 IndexList_t::const_iterator indexiter = _indices.begin();
197 Vector CurrentVector = _positions[*indexiter++];
198 for(; indexiter != _indices.end(); ++indexiter) {
199 AngleSum += CurrentVector.Angle(_positions[*indexiter]);
200 CurrentVector = _positions[*indexiter];
201 }
202 }
203 if (AngleSum - M_PI > std::numeric_limits<double>::epsilon()*1e4) {
204// Vector candidate;
205// double oldSKP = -1.;
206// for (IndexList_t::const_iterator iter = _indices.begin();
207// iter != _indices.end(); ++iter) {
208// const double newSKP = _positions[*iter].ScalarProduct(center);
209// if (newSKP > oldSKP) {
210// candidate = _positions[*iter];
211// oldSKP = newSKP;
212// }
213// }
214 // non-uniqueness: all positions on great circle, normal to given line are valid
215 // so, we just pick one because returning a unique point is topmost priority
216// Vector normal;
217// normal.GetOneNormalVector(candidate);
218// Vector othernormal = candidate;
219// othernormal.VectorProduct(normal);
220// // now both normal and othernormal describe the plane containing the great circle
221// Plane greatcircle(normal, zeroVec, othernormal);
222 // check which axis is contained and pick the one not
223// if (greatcircle.isContained(center)) {
224// center = Vector(0.,1.,0.);
225// if (greatcircle.isContained(center))
226// center = Vector(0.,0.,1.);
227// // now we are done cause a plane cannot contain all three axis vectors
228// }
229// center = greatcircle.getClosestPoint(candidate);
230// center = greatcircle.getNormal();
231 center = Normal;
232 // assure length of 1
233 center.Normalize();
234
235 return center;
236 }
237 }
238 }
239
240 // start with geographic midpoint
241 center = calculateGeographicMidpoint(_positions, _indices);
242 if (!center.IsZero()) {
243 center.Normalize();
244 LOG(5, "DEBUG: Starting with geographical midpoint of " << _positions << " under indices "
245 << _indices << " is " << center);
246 } else {
247 // any point is good actually
248 center = _positions[0];
249 LOG(5, "DEBUG: Starting with first position " << center);
250 }
251
252 // calculate initial MinimumDistance
253 double MinimumDistance = calculateMinimumDistance(center, _positions, _indices);
254 LOG(6, "DEBUG: MinimumDistance to this center is " << MinimumDistance);
255
256 // check all present points whether they may serve as a better center
257 for (SphericalPointDistribution::IndexList_t::const_iterator iter = _indices.begin();
258 iter != _indices.end(); ++iter) {
259 const Vector &centerCandidate = _positions[*iter];
260 const double candidateDistance = calculateMinimumDistance(centerCandidate, _positions, _indices);
261 if (candidateDistance < MinimumDistance) {
262 MinimumDistance = candidateDistance;
263 center = centerCandidate;
264 LOG(6, "DEBUG: new MinimumDistance to current test point " << MinimumDistance
265 << " is " << center);
266 }
267 }
268 LOG(6, "DEBUG: new MinimumDistance to center " << center << " is " << MinimumDistance);
269
270 // now iterate over TestDistance
271 double TestDistance = Vector(1.,0.,0.).Angle(Vector(0.,1.,0.));
272// LOG(6, "DEBUG: initial TestDistance is " << TestDistance);
273
274 const double threshold = sqrt(std::numeric_limits<double>::epsilon());
275 // check each of eight test points at N, NE, E, SE, S, SW, W, NW
276 typedef std::vector<double> angles_t;
277 angles_t testangles;
278 testangles += 0./180.*M_PI, 45./180.*M_PI, 90./180.*M_PI, 135./180.*M_PI,
279 180./180.*M_PI, 225./180.*M_PI, 270./180.*M_PI, 315./180.*M_PI;
280 while (TestDistance > threshold) {
281 Vector OneNormal;
282 OneNormal.GetOneNormalVector(center);
283 Line RotationAxis(zeroVec, OneNormal);
284 Vector North = RotationAxis.rotateVector(center,TestDistance);
285 Line CompassRose(zeroVec, center);
286 bool updatedflag = false;
287 for (angles_t::const_iterator angleiter = testangles.begin();
288 angleiter != testangles.end(); ++angleiter) {
289 Vector centerCandidate = CompassRose.rotateVector(North, *angleiter);
290// centerCandidate.Normalize();
291 const double candidateDistance = calculateMinimumDistance(centerCandidate, _positions, _indices);
292 if (candidateDistance < MinimumDistance) {
293 MinimumDistance = candidateDistance;
294 center = centerCandidate;
295 updatedflag = true;
296 LOG(7, "DEBUG: new MinimumDistance to test point at " << *angleiter/M_PI*180.
297 << "° is " << MinimumDistance);
298 }
299 }
300
301 // if no new point, decrease TestDistance
302 if (!updatedflag) {
303 TestDistance *= 0.5;
304// LOG(6, "DEBUG: TestDistance is now " << TestDistance);
305 }
306 }
307 LOG(4, "DEBUG: Final MinimumDistance to center " << center << " is " << MinimumDistance);
308
309 return center;
310}
311
312Vector calculateCenterOfMinimumDistance(
313 const SphericalPointDistribution::PolygonWithIndices &_points)
314{
315 return SphericalPointDistribution::calculateCenterOfMinimumDistance(_points.polygon, _points.indices);
316}
317
318/** Calculate the center of a given set of points in \a _positions but only
319 * for those indicated by \a _indices.
320 *
321 */
322inline
323Vector calculateCenterOfGravity(
324 const SphericalPointDistribution::VectorArray_t &_positions,
325 const SphericalPointDistribution::IndexList_t &_indices)
326{
327 Vector Center;
328 Center.Zero();
329 for (SphericalPointDistribution::IndexList_t::const_iterator iter = _indices.begin();
330 iter != _indices.end(); ++iter)
331 Center += _positions[*iter];
332 if (!_indices.empty())
333 Center *= 1./(double)_indices.size();
334
335 return Center;
336}
337
338/** Calculate the center of a given set of points in \a _positions but only
339 * for those indicated by \a _indices.
340 *
341 */
342inline
343Vector calculateCenter(
344 const SphericalPointDistribution::VectorArray_t &_positions,
345 const SphericalPointDistribution::IndexList_t &_indices)
346{
347// Vector Center;
348// Center.Zero();
349// for (SphericalPointDistribution::IndexList_t::const_iterator iter = _indices.begin();
350// iter != _indices.end(); ++iter)
351// Center += _positions[*iter];
352// if (!_indices.empty())
353// Center *= 1./(double)_indices.size();
354//
355// return Center;
356 return SphericalPointDistribution::calculateCenterOfMinimumDistance(_positions, _indices);
357}
358
359/** Calculate the center of a given set of points in \a _positions but only
360 * for those indicated by \a _indices.
361 *
362 */
363inline
364Vector calculateCenter(
365 const SphericalPointDistribution::PolygonWithIndices &_polygon)
366{
367 return calculateCenter(_polygon.polygon, _polygon.indices);
368}
369
370inline
371DistanceArray_t calculatePairwiseDistances(
372 const SphericalPointDistribution::VectorArray_t &_points,
373 const SphericalPointDistribution::IndexTupleList_t &_indices
374 )
375{
376 DistanceArray_t result;
377 for (SphericalPointDistribution::IndexTupleList_t::const_iterator firstiter = _indices.begin();
378 firstiter != _indices.end(); ++firstiter) {
379
380 // calculate first center from possible tuple of indices
381 Vector FirstCenter;
382 ASSERT(!firstiter->empty(), "calculatePairwiseDistances() - there is an empty tuple.");
383 if (firstiter->size() == 1) {
384 FirstCenter = _points[*firstiter->begin()];
385 } else {
386 FirstCenter = calculateCenter( _points, *firstiter);
387 if (!FirstCenter.IsZero())
388 FirstCenter.Normalize();
389 }
390
391 for (SphericalPointDistribution::IndexTupleList_t::const_iterator seconditer = firstiter;
392 seconditer != _indices.end(); ++seconditer) {
393 if (firstiter == seconditer)
394 continue;
395
396 // calculate second center from possible tuple of indices
397 Vector SecondCenter;
398 ASSERT(!seconditer->empty(), "calculatePairwiseDistances() - there is an empty tuple.");
399 if (seconditer->size() == 1) {
400 SecondCenter = _points[*seconditer->begin()];
401 } else {
402 SecondCenter = calculateCenter( _points, *seconditer);
403 if (!SecondCenter.IsZero())
404 SecondCenter.Normalize();
405 }
406
407 // calculate distance between both centers
408 double distance = 2.; // greatest distance on surface of sphere with radius 1.
409 if ((!FirstCenter.IsZero()) && (!SecondCenter.IsZero()))
410 distance = (FirstCenter - SecondCenter).NormSquared();
411 result.push_back(distance);
412 }
413 }
414 return result;
415}
416
417/** Decides by an orthonormal third vector whether the sign of the rotation
418 * angle should be negative or positive.
419 *
420 * \return -1 or 1
421 */
422inline
423double determineSignOfRotation(
424 const Vector &_oldPosition,
425 const Vector &_newPosition,
426 const Vector &_RotationAxis
427 )
428{
429 Vector dreiBein(_oldPosition);
430 dreiBein.VectorProduct(_RotationAxis);
431 ASSERT( !dreiBein.IsZero(), "determineSignOfRotation() - dreiBein is zero.");
432 dreiBein.Normalize();
433 const double sign =
434 (dreiBein.ScalarProduct(_newPosition) < 0.) ? -1. : +1.;
435 LOG(6, "DEBUG: oldCenter on plane is " << _oldPosition
436 << ", newCenter on plane is " << _newPosition
437 << ", and dreiBein is " << dreiBein);
438 return sign;
439}
440
441/** Convenience function to recalculate old and new center for determining plane
442 * rotation.
443 */
444inline
445void calculateOldAndNewCenters(
446 Vector &_oldCenter,
447 Vector &_newCenter,
448 const SphericalPointDistribution::PolygonWithIndices &_referencepositions,
449 const SphericalPointDistribution::PolygonWithIndices &_currentpositions)
450{
451 _oldCenter = calculateCenter(_referencepositions.polygon, _referencepositions.indices);
452 // C++11 defines a copy_n function ...
453 _newCenter = calculateCenter( _currentpositions.polygon, _currentpositions.indices);
454}
455/** Returns squared L2 error of the given \a _Matching.
456 *
457 * We compare the pair-wise distances of each associated matching
458 * and check whether these distances each match between \a _old and
459 * \a _new.
460 *
461 * \param _old first set of points (fewer or equal to \a _new)
462 * \param _new second set of points
463 * \param _Matching matching between the two sets
464 * \return pair with L1 and squared L2 error
465 */
466std::pair<double, double> SphericalPointDistribution::calculateErrorOfMatching(
467 const VectorArray_t &_old,
468 const VectorArray_t &_new,
469 const IndexTupleList_t &_Matching)
470{
471 std::pair<double, double> errors( std::make_pair( 0., 0. ) );
472
473 // the error is the deviation from the mean angle
474 std::vector<double> distances;
475 double mean = 0.;
476 for (IndexTupleList_t::const_iterator matchingiter = _Matching.begin();
477 matchingiter != _Matching.end(); ++matchingiter) {
478 // calculate distance on surface as rotation angle
479 const Vector newcenter = calculateCenter(_new, *matchingiter);
480 const Vector &oldcenter = _old[std::distance(_Matching.begin(), matchingiter)];
481 Vector axis = newcenter;
482 axis.VectorProduct(oldcenter);
483 axis.Normalize();
484 const double distance = newcenter.Angle(oldcenter);
485 distances.push_back(distance);
486 mean += distance;
487 }
488 if (!_Matching.empty())
489 mean *= 1./(double)_Matching.size();
490 LOG(5, "DEBUG: Mean distance is " << mean << " for " << _Matching.size() << " points.");
491
492 // analyse errors
493 for (std::vector<double>::const_iterator iter = distances.begin();
494 iter != distances.end(); ++iter) {
495 const double difference = fabs(*iter - mean);
496 if (errors.first < difference) {
497 errors.first = difference;
498 }
499 errors.second += difference*difference;
500
501 }
502 errors.second = sqrt(errors.second);
503
504// if (!_Matching.empty()) {
505// // there is at least one distance, we've checked that before
506// errors.second *= 1./(double)_Matching.size();
507// }
508
509// if (_Matching.size() > 1) {
510// LOG(5, "INFO: Matching is " << _Matching);
511//
512// // calculate all pair-wise distances
513// IndexTupleList_t keys(_old.size(), IndexList_t() );
514// std::generate (keys.begin(), keys.end(), UniqueNumberList);
515//
516// const DistanceArray_t firstdistances = calculatePairwiseDistances(_old, keys);
517// const DistanceArray_t seconddistances = calculatePairwiseDistances(_new, _Matching);
518//
519// ASSERT( firstdistances.size() == seconddistances.size(),
520// "calculateL2ErrorOfMatching() - mismatch in pair-wise distance array sizes.");
521// DistanceArray_t::const_iterator firstiter = firstdistances.begin();
522// DistanceArray_t::const_iterator seconditer = seconddistances.begin();
523// for (;(firstiter != firstdistances.end()) && (seconditer != seconddistances.end());
524// ++firstiter, ++seconditer) {
525// const double gap = fabs(*firstiter - *seconditer);
526// // L1 error
527// if (errors.first < gap)
528// errors.first = gap;
529// // L2 error
530// errors.second += gap*gap;
531// }
532// // there is at least one distance, we've checked that before
533// errors.second *= 1./(double)firstdistances.size();
534// } else {
535// // check whether we have any zero centers: Combining points on new sphere may result
536// // in zero centers
537// for (SphericalPointDistribution::IndexTupleList_t::const_iterator iter = _Matching.begin();
538// iter != _Matching.end(); ++iter) {
539// if ((iter->size() != 1) && (calculateCenter( _new, *iter).IsZero())) {
540// errors.first = 2.;
541// errors.second = 2.;
542// }
543// }
544// }
545 LOG(4, "INFO: Resulting errors for matching (L1, L2): "
546 << errors.first << "," << errors.second << ".");
547
548 return errors;
549}
550
551SphericalPointDistribution::Polygon_t SphericalPointDistribution::removeMatchingPoints(
552 const PolygonWithIndices &_points
553 )
554{
555 SphericalPointDistribution::Polygon_t remainingpoints;
556 IndexArray_t indices(_points.indices.begin(), _points.indices.end());
557 std::sort(indices.begin(), indices.end());
558 LOG(4, "DEBUG: sorted matching is " << indices);
559 IndexArray_t remainingindices(_points.polygon.size(), -1);
560 std::generate(remainingindices.begin(), remainingindices.end(), UniqueNumber);
561 IndexArray_t::iterator remainiter = std::set_difference(
562 remainingindices.begin(), remainingindices.end(),
563 indices.begin(), indices.end(),
564 remainingindices.begin());
565 remainingindices.erase(remainiter, remainingindices.end());
566 LOG(4, "DEBUG: remaining indices are " << remainingindices);
567 for (IndexArray_t::const_iterator iter = remainingindices.begin();
568 iter != remainingindices.end(); ++iter) {
569 remainingpoints.push_back(_points.polygon[*iter]);
570 }
571
572 return remainingpoints;
573}
574
575/** Recursive function to go through all possible matchings.
576 *
577 * \param _MCS structure holding global information to the recursion
578 * \param _matching current matching being build up
579 * \param _indices contains still available indices
580 * \param _remainingweights current weights to fill (each weight a place)
581 * \param _remainiter iterator over the weights, indicating the current position we match
582 * \param _matchingsize
583 */
584void SphericalPointDistribution::recurseMatchings(
585 MatchingControlStructure &_MCS,
586 IndexTupleList_t &_matching,
587 IndexList_t _indices,
588 WeightsArray_t &_remainingweights,
589 WeightsArray_t::iterator _remainiter,
590 const unsigned int _matchingsize
591 )
592{
593 LOG(5, "DEBUG: Recursing with current matching " << _matching
594 << ", remaining indices " << _indices
595 << ", and remaining weights " << _matchingsize);
596 if (!_MCS.foundflag) {
597 LOG(5, "DEBUG: Current matching has size " << _matching.size() << ", places left " << _matchingsize);
598 if (_matchingsize > 0) {
599 // go through all indices
600 for (IndexList_t::iterator iter = _indices.begin();
601 (iter != _indices.end()) && (!_MCS.foundflag);) {
602
603 // check whether we can stay in the current bin or have to move on to next one
604 if (*_remainiter == 0) {
605 // we need to move on
606 if (_remainiter != _remainingweights.end()) {
607 ++_remainiter;
608 } else {
609 // as we check _matchingsize > 0 this should be impossible
610 ASSERT( 0, "recurseMatchings() - we must not come to this position.");
611 }
612 }
613
614 // advance in matching to current bin to fill in
615 const size_t OldIndex = std::distance(_remainingweights.begin(), _remainiter);
616 while (_matching.size() <= OldIndex) { // add empty lists if new bin is opened
617 LOG(6, "DEBUG: Extending _matching.");
618 _matching.push_back( IndexList_t() );
619 }
620 IndexTupleList_t::iterator filliniter = _matching.begin();
621 std::advance(filliniter, OldIndex);
622
623 // check whether connection between bins' indices and candidate is satisfied
624 if (!_MCS.adjacency.empty()) {
625 adjacency_t::const_iterator finder = _MCS.adjacency.find(*iter);
626 ASSERT( finder != _MCS.adjacency.end(),
627 "recurseMatchings() - "+toString(*iter)+" is not in adjacency list.");
628 if ((!filliniter->empty())
629 && (finder->second.find(*filliniter->begin()) == finder->second.end())) {
630 LOG(5, "DEBUG; Skipping index " << *iter
631 << " as is not connected to current set." << *filliniter << ".");
632 ++iter; // note that index-loop does not contain incrementor
633 continue;
634 }
635 }
636
637 // add index to matching
638 filliniter->push_back(*iter);
639 --(*_remainiter);
640 LOG(6, "DEBUG: Adding " << *iter << " to matching at " << OldIndex << ".");
641 // remove index but keep iterator to position (is the next to erase element)
642 IndexList_t::iterator backupiter = _indices.erase(iter);
643 // recurse with decreased _matchingsize
644 recurseMatchings(_MCS, _matching, _indices, _remainingweights, _remainiter, _matchingsize-1);
645 // re-add chosen index and reset index to new position
646 _indices.insert(backupiter, filliniter->back());
647 iter = backupiter;
648 // remove index from _matching to make space for the next one
649 filliniter->pop_back();
650 ++(*_remainiter);
651 }
652 // gone through all indices then exit recursion
653 if (_matching.empty())
654 _MCS.foundflag = true;
655 } else {
656 LOG(4, "INFO: Found matching " << _matching);
657 // calculate errors
658 std::pair<double, double> errors = calculateErrorOfMatching(
659 _MCS.oldpoints, _MCS.newpoints, _matching);
660 if (errors.first < L1THRESHOLD) {
661 _MCS.bestmatching = _matching;
662 _MCS.foundflag = true;
663 } else if (_MCS.bestL2 > errors.second) {
664 _MCS.bestmatching = _matching;
665 _MCS.bestL2 = errors.second;
666 }
667 }
668 }
669}
670
671SphericalPointDistribution::MatchingControlStructure::MatchingControlStructure(
672 const adjacency_t &_adjacency,
673 const VectorArray_t &_oldpoints,
674 const VectorArray_t &_newpoints,
675 const WeightsArray_t &_weights
676 ) :
677 foundflag(false),
678 bestL2(std::numeric_limits<double>::max()),
679 adjacency(_adjacency),
680 oldpoints(_oldpoints),
681 newpoints(_newpoints),
682 weights(_weights)
683{}
684
685/** Finds combinatorially the best matching between points in \a _polygon
686 * and \a _newpolygon.
687 *
688 * We find the matching with the smallest L2 error, where we break when we stumble
689 * upon a matching with zero error.
690 *
691 * As points in \a _polygon may be have a weight greater 1 we have to match it to
692 * multiple points in \a _newpolygon. Eventually, these multiple points are combined
693 * for their center of weight, which is the only thing follow-up function see of
694 * these multiple points. Hence, we actually modify \a _newpolygon in the process
695 * such that the returned IndexList_t indicates a bijective mapping in the end.
696 *
697 * \sa recurseMatchings() for going through all matchings
698 *
699 * \param _polygon here, we have indices 0,1,2,...
700 * \param _newpolygon and here we need to find the correct indices
701 * \return control structure containing the matching and more
702 */
703SphericalPointDistribution::MatchingControlStructure
704SphericalPointDistribution::findBestMatching(
705 const WeightedPolygon_t &_polygon
706 )
707{
708 // transform lists into arrays
709 VectorArray_t oldpoints;
710 VectorArray_t newpoints;
711 WeightsArray_t weights;
712 for (WeightedPolygon_t::const_iterator iter = _polygon.begin();
713 iter != _polygon.end(); ++iter) {
714 oldpoints.push_back(iter->first);
715 weights.push_back(iter->second);
716 }
717 newpoints.insert(newpoints.begin(), points.begin(), points.end() );
718 MatchingControlStructure MCS(adjacency, oldpoints, newpoints, weights);
719
720 // search for bestmatching combinatorially
721 {
722 // translate polygon into vector to enable index addressing
723 IndexList_t indices(points.size());
724 std::generate(indices.begin(), indices.end(), UniqueNumber);
725 IndexTupleList_t matching;
726
727 // walk through all matchings
728 WeightsArray_t remainingweights = MCS.weights;
729 unsigned int placesleft = std::accumulate(remainingweights.begin(), remainingweights.end(), 0);
730 recurseMatchings(MCS, matching, indices, remainingweights, remainingweights.begin(), placesleft);
731 }
732 if (MCS.foundflag)
733 LOG(3, "Found a best matching beneath L1 threshold of " << L1THRESHOLD);
734 else {
735 if (MCS.bestL2 < warn_amplitude)
736 LOG(3, "Picking matching is " << MCS.bestmatching << " with best L2 error of "
737 << MCS.bestL2);
738 else if (MCS.bestL2 < L2THRESHOLD)
739 ELOG(2, "Picking matching is " << MCS.bestmatching
740 << " with rather large L2 error of " << MCS.bestL2);
741 else
742 ASSERT(0, "findBestMatching() - matching "+toString(MCS.bestmatching)
743 +" has L2 error of "+toString(MCS.bestL2)+" that is too large.");
744 }
745
746 return MCS;
747}
748
749SphericalPointDistribution::IndexList_t SphericalPointDistribution::joinPoints(
750 Polygon_t &_newpolygon,
751 const VectorArray_t &_newpoints,
752 const IndexTupleList_t &_bestmatching
753 )
754{
755 // generate trivial index list
756 IndexList_t IndexList(_bestmatching.size(), (size_t)-1);
757 std::generate(IndexList.begin(), IndexList.end(), UniqueNumber);
758 LOG(4, "DEBUG: Our new trivial IndexList reads as " << IndexList);
759
760 // combine all multiple points
761 VectorArray_t newCenters;
762 newCenters.resize(_bestmatching.size());
763 VectorArray_t::iterator centeriter = newCenters.begin();
764 for (IndexTupleList_t::const_iterator tupleiter = _bestmatching.begin();
765 tupleiter != _bestmatching.end(); ++tupleiter, ++centeriter) {
766 ASSERT (tupleiter->size() > 0,
767 "findBestMatching() - encountered tuple in bestmatching with size 0.");
768 if (tupleiter->size() == 1) {
769 // add point and index
770 *centeriter = _newpoints[*tupleiter->begin()];
771 } else {
772 // combine into weighted and normalized center
773 *centeriter = calculateCenter(_newpoints, *tupleiter);
774 (*centeriter).Normalize();
775 LOG(5, "DEBUG: Combining " << tupleiter->size() << " points to weighted center "
776 << *centeriter << ".");
777 }
778 }
779 _newpolygon.insert(_newpolygon.begin(), newCenters.begin(), newCenters.end());
780 LOG(4, "DEBUG: The polygon with centered points is " << _newpolygon);
781
782 return IndexList;
783}
784
785SphericalPointDistribution::Rotation_t SphericalPointDistribution::findPlaneAligningRotation(
786 const PolygonWithIndices &_referencepositions,
787 const PolygonWithIndices &_currentpositions
788 )
789{
790 bool dontcheck = false;
791 // initialize to no rotation
792 Rotation_t Rotation;
793 Rotation.first.Zero();
794 Rotation.first[0] = 1.;
795 Rotation.second = 0.;
796
797 // calculate center of triangle/line/point consisting of first points of matching
798 Vector oldCenter;
799 Vector newCenter;
800 calculateOldAndNewCenters(
801 oldCenter, newCenter,
802 _referencepositions, _currentpositions);
803
804 ASSERT( !oldCenter.IsZero() && !newCenter.IsZero(),
805 "findPlaneAligningRotation() - either old "+toString(oldCenter)
806 +" or new center "+toString(newCenter)+" are zero.");
807 LOG(4, "DEBUG: oldCenter is " << oldCenter << ", newCenter is " << newCenter);
808 if (!oldCenter.IsEqualTo(newCenter)) {
809 // calculate rotation axis and angle
810 Rotation.first = oldCenter;
811 Rotation.first.VectorProduct(newCenter);
812 Rotation.first.Normalize();
813 // construct reference vector to determine direction of rotation
814 const double sign = determineSignOfRotation(newCenter, oldCenter, Rotation.first);
815 Rotation.second = sign * oldCenter.Angle(newCenter);
816 } else {
817 // no rotation required anymore
818 }
819
820#ifndef NDEBUG
821 // check: rotation brings newCenter onto oldCenter position
822 if (!dontcheck) {
823 Line Axis(zeroVec, Rotation.first);
824 Vector test = Axis.rotateVector(newCenter, Rotation.second);
825 LOG(4, "CHECK: rotated newCenter is " << test
826 << ", oldCenter is " << oldCenter);
827 ASSERT( (test - oldCenter).NormSquared() < std::numeric_limits<double>::epsilon()*1e4,
828 "matchSphericalPointDistributions() - rotation does not work as expected by "
829 +toString((test - oldCenter).NormSquared())+".");
830 }
831#endif
832
833 return Rotation;
834}
835
836SphericalPointDistribution::Rotation_t SphericalPointDistribution::findPointAligningRotation(
837 const PolygonWithIndices &remainingold,
838 const PolygonWithIndices &remainingnew)
839{
840 // initialize rotation to zero
841 Rotation_t Rotation;
842 Rotation.first.Zero();
843 Rotation.second = 0.;
844
845 // recalculate center
846 Vector oldCenter;
847 Vector newCenter;
848 calculateOldAndNewCenters(
849 oldCenter, newCenter,
850 remainingold, remainingnew);
851
852 // we rotate at oldCenter and around the radial direction, which is again given
853 // by oldCenter.
854 Rotation.first = oldCenter;
855 Rotation.first.Normalize(); // note weighted sum of normalized weight is not normalized
856
857 // calculate center of the rotational plane
858 newCenter = calculateCenterOfGravity(remainingnew.polygon, remainingnew.indices);
859 oldCenter = calculateCenterOfGravity(remainingold.polygon, remainingold.indices);
860 LOG(6, "DEBUG: Using oldCenter " << oldCenter << " as rotation center and "
861 << Rotation.first << " as axis.");
862
863 LOG(6, "DEBUG: old indices are " << remainingold.indices
864 << ", new indices are " << remainingnew.indices);
865 IndexList_t::const_iterator newiter = remainingnew.indices.begin();
866 IndexList_t::const_iterator olditer = remainingold.indices.begin();
867 for (;
868 (newiter != remainingnew.indices.end()) && (olditer != remainingold.indices.end());
869 ++newiter,++olditer) {
870 Vector newPosition = remainingnew.polygon[*newiter];
871 Vector oldPosition = remainingold.polygon[*olditer];
872 LOG(6, "DEBUG: oldPosition is " << oldPosition << " (length: "
873 << oldPosition.Norm() << ") and newPosition is " << newPosition << " length(: "
874 << newPosition.Norm() << ")");
875
876 if (!oldPosition.IsEqualTo(newPosition)) {
877 oldPosition -= oldCenter;
878 newPosition -= newCenter;
879 oldPosition = (oldPosition - oldPosition.Projection(Rotation.first));
880 newPosition = (newPosition - newPosition.Projection(Rotation.first));
881 LOG(6, "DEBUG: Positions after projection are " << oldPosition << " and " << newPosition);
882
883 // construct reference vector to determine direction of rotation
884 const double sign = determineSignOfRotation(newPosition, oldPosition, Rotation.first);
885 LOG(6, "DEBUG: Determining angle between " << oldPosition << " and " << newPosition);
886 const double angle = sign * newPosition.Angle(oldPosition);
887 LOG(6, "DEBUG: Adding " << angle << " to weighted rotation sum.");
888 Rotation.second += angle;
889 } else {
890 LOG(6, "DEBUG: oldPosition and newPosition are equivalent, hence no orientating rotation.");
891 }
892 }
893 Rotation.second *= 1./(double)remainingnew.indices.size();
894
895 return Rotation;
896}
897
898void SphericalPointDistribution::initSelf(const int _NumberOfPoints)
899{
900 switch (_NumberOfPoints)
901 {
902 case 0:
903 points = get<0>();
904 adjacency = getConnections<0>();
905 break;
906 case 1:
907 points = get<1>();
908 adjacency = getConnections<1>();
909 break;
910 case 2:
911 points = get<2>();
912 adjacency = getConnections<2>();
913 break;
914 case 3:
915 points = get<3>();
916 adjacency = getConnections<3>();
917 break;
918 case 4:
919 points = get<4>();
920 adjacency = getConnections<4>();
921 break;
922 case 5:
923 points = get<5>();
924 adjacency = getConnections<5>();
925 break;
926 case 6:
927 points = get<6>();
928 adjacency = getConnections<6>();
929 break;
930 case 7:
931 points = get<7>();
932 adjacency = getConnections<7>();
933 break;
934 case 8:
935 points = get<8>();
936 adjacency = getConnections<8>();
937 break;
938 case 9:
939 points = get<9>();
940 adjacency = getConnections<9>();
941 break;
942 case 10:
943 points = get<10>();
944 adjacency = getConnections<10>();
945 break;
946 case 11:
947 points = get<11>();
948 adjacency = getConnections<11>();
949 break;
950 case 12:
951 points = get<12>();
952 adjacency = getConnections<12>();
953 break;
954 case 14:
955 points = get<14>();
956 adjacency = getConnections<14>();
957 break;
958 default:
959 ASSERT(0, "SphericalPointDistribution::initSelf() - cannot deal with the case "
960 +toString(_NumberOfPoints)+".");
961 }
962 LOG(3, "DEBUG: Ideal polygon is " << points);
963}
964
965SphericalPointDistribution::Polygon_t
966SphericalPointDistribution::getRemainingPoints(
967 const WeightedPolygon_t &_polygon,
968 const int _N)
969{
970 SphericalPointDistribution::Polygon_t remainingpoints;
971
972 // initialze to given number of points
973 initSelf(_N);
974 LOG(2, "INFO: Matching old polygon " << _polygon
975 << " with new polygon " << points);
976
977 // check whether any points will remain vacant
978 int RemainingPoints = _N;
979 for (WeightedPolygon_t::const_iterator iter = _polygon.begin();
980 iter != _polygon.end(); ++iter)
981 RemainingPoints -= iter->second;
982 if (RemainingPoints == 0)
983 return remainingpoints;
984
985 if (_N > 0) {
986 // combine multiple points and create simple IndexList from IndexTupleList
987 MatchingControlStructure MCS = findBestMatching(_polygon);
988 IndexList_t bestmatching = joinPoints(points, MCS.newpoints, MCS.bestmatching);
989 LOG(2, "INFO: Best matching is " << bestmatching);
990
991 const size_t NumberIds = std::min(bestmatching.size(), (size_t)3);
992 // create old set
993 PolygonWithIndices oldSet;
994 oldSet.indices.resize(NumberIds, -1);
995 std::generate(oldSet.indices.begin(), oldSet.indices.end(), UniqueNumber);
996 for (WeightedPolygon_t::const_iterator iter = _polygon.begin();
997 iter != _polygon.end(); ++iter)
998 oldSet.polygon.push_back(iter->first);
999
1000 // _newpolygon has changed, so now convert to array with matched indices
1001 PolygonWithIndices newSet;
1002 SphericalPointDistribution::IndexList_t::const_iterator beginiter = bestmatching.begin();
1003 SphericalPointDistribution::IndexList_t::const_iterator enditer = bestmatching.begin();
1004 std::advance(enditer, NumberIds);
1005 newSet.indices.resize(NumberIds, -1);
1006 std::copy(beginiter, enditer, newSet.indices.begin());
1007 std::copy(points.begin(),points.end(), std::back_inserter(newSet.polygon));
1008
1009 // determine rotation angles to align the two point distributions with
1010 // respect to bestmatching:
1011 // we use the center between the three first matching points
1012 /// the first rotation brings these two centers to coincide
1013 PolygonWithIndices rotatednewSet = newSet;
1014 {
1015 Rotation_t Rotation = findPlaneAligningRotation(oldSet, newSet);
1016 LOG(5, "DEBUG: Rotating coordinate system by " << Rotation.second
1017 << " around axis " << Rotation.first);
1018 Line Axis(zeroVec, Rotation.first);
1019
1020 // apply rotation angle to bring newCenter to oldCenter
1021 for (VectorArray_t::iterator iter = rotatednewSet.polygon.begin();
1022 iter != rotatednewSet.polygon.end(); ++iter) {
1023 Vector &current = *iter;
1024 LOG(6, "DEBUG: Original point is " << current);
1025 current = Axis.rotateVector(current, Rotation.second);
1026 LOG(6, "DEBUG: Rotated point is " << current);
1027 }
1028
1029#ifndef NDEBUG
1030 // check: rotated "newCenter" should now equal oldCenter
1031 // we don't check in case of two points as these lie on a great circle
1032 // and the center cannot stably be recalculated. We may reactivate this
1033 // when we calculate centers only once
1034 if (oldSet.indices.size() > 2) {
1035 Vector oldCenter;
1036 Vector rotatednewCenter;
1037 calculateOldAndNewCenters(
1038 oldCenter, rotatednewCenter,
1039 oldSet, rotatednewSet);
1040 oldCenter.Normalize();
1041 rotatednewCenter.Normalize();
1042 // check whether centers are anti-parallel (factor -1)
1043 // then we have the "non-unique poles" situation: points lie on great circle
1044 // and both poles are valid solution
1045 if (fabs(oldCenter.ScalarProduct(rotatednewCenter) + 1.)
1046 < std::numeric_limits<double>::epsilon()*1e4)
1047 rotatednewCenter *= -1.;
1048 LOG(4, "CHECK: rotatednewCenter is " << rotatednewCenter
1049 << ", oldCenter is " << oldCenter);
1050 const double difference = (rotatednewCenter - oldCenter).NormSquared();
1051 ASSERT( difference < std::numeric_limits<double>::epsilon()*1e4,
1052 "matchSphericalPointDistributions() - rotation does not work as expected by "
1053 +toString(difference)+".");
1054 }
1055#endif
1056 }
1057 /// the second (orientation) rotation aligns the planes such that the
1058 /// points themselves coincide
1059 if (bestmatching.size() > 1) {
1060 Rotation_t Rotation = findPointAligningRotation(oldSet, rotatednewSet);
1061
1062 // construct RotationAxis and two points on its plane, defining the angle
1063 Rotation.first.Normalize();
1064 const Line RotationAxis(zeroVec, Rotation.first);
1065
1066 LOG(5, "DEBUG: Rotating around self is " << Rotation.second
1067 << " around axis " << RotationAxis);
1068
1069#ifndef NDEBUG
1070 // check: first bestmatching in rotated_newpolygon and remainingnew
1071 // should now equal
1072 {
1073 const IndexList_t::const_iterator iter = bestmatching.begin();
1074
1075 // check whether both old and newPosition are at same distance to oldCenter
1076 Vector oldCenter = calculateCenter(oldSet);
1077 const double distance = fabs(
1078 (oldSet.polygon[0] - oldCenter).NormSquared()
1079 - (rotatednewSet.polygon[*iter] - oldCenter).NormSquared()
1080 );
1081 LOG(4, "CHECK: Squared distance between oldPosition and newPosition "
1082 << " with respect to oldCenter " << oldCenter << " is " << distance);
1083// ASSERT( distance < warn_amplitude,
1084// "matchSphericalPointDistributions() - old and newPosition's squared distance to oldCenter differs by "
1085// +toString(distance));
1086
1087 Vector rotatednew = RotationAxis.rotateVector(
1088 rotatednewSet.polygon[*iter],
1089 Rotation.second);
1090 LOG(4, "CHECK: rotated first new bestmatching is " << rotatednew
1091 << " while old was " << oldSet.polygon[0]);
1092 const double difference = (rotatednew - oldSet.polygon[0]).NormSquared();
1093 ASSERT( difference < distance+warn_amplitude,
1094 "matchSphericalPointDistributions() - orientation rotation ends up off by "
1095 +toString(difference)+", more than "
1096 +toString(distance+warn_amplitude)+".");
1097 }
1098#endif
1099
1100 for (VectorArray_t::iterator iter = rotatednewSet.polygon.begin();
1101 iter != rotatednewSet.polygon.end(); ++iter) {
1102 Vector &current = *iter;
1103 LOG(6, "DEBUG: Original point is " << current);
1104 current = RotationAxis.rotateVector(current, Rotation.second);
1105 LOG(6, "DEBUG: Rotated point is " << current);
1106 }
1107 }
1108
1109 // remove all points in matching and return remaining ones
1110 SphericalPointDistribution::Polygon_t remainingpoints =
1111 removeMatchingPoints(rotatednewSet);
1112 LOG(2, "INFO: Remaining points are " << remainingpoints);
1113 return remainingpoints;
1114 } else
1115 return points;
1116}
1117
1118SphericalPointDistribution::PolygonWithIndexTuples
1119SphericalPointDistribution::getAssociatedPoints(
1120 const WeightedPolygon_t &_polygon,
1121 const int _N)
1122{
1123 SphericalPointDistribution::PolygonWithIndexTuples associatedpoints;
1124
1125 // initialize to given number of points
1126 initSelf(_N);
1127 LOG(2, "INFO: Matching old polygon " << _polygon
1128 << " with new polygon " << points);
1129
1130 // check whether there are any points to associate
1131 if (_polygon.empty()) {
1132 associatedpoints.polygon.insert(
1133 associatedpoints.polygon.end(),
1134 points.begin(), points.end());
1135 return associatedpoints;
1136 }
1137
1138 if (_N > 0) {
1139 // combine multiple points and create simple IndexList from IndexTupleList
1140 MatchingControlStructure MCS = findBestMatching(_polygon);
1141 points.clear();
1142 IndexList_t bestmatching = joinPoints(points, MCS.newpoints, MCS.bestmatching);
1143 LOG(4, "DEBUG: Compare with old polygon " << _polygon);
1144 LOG(2, "INFO: Best matching is " << MCS.bestmatching);
1145
1146 // gather the associated points (not the joined ones)
1147 associatedpoints.polygon = MCS.newpoints;
1148 // gather indices
1149 associatedpoints.indices = MCS.bestmatching;
1150
1151 /// now we only need to rotate the associated points to match the given ones
1152 /// with respect to the joined points in \a points
1153
1154 const size_t NumberIds = std::min(bestmatching.size(), (size_t)3);
1155 // create old set
1156 PolygonWithIndices oldSet;
1157 oldSet.indices.resize(NumberIds, -1);
1158 std::generate(oldSet.indices.begin(), oldSet.indices.end(), UniqueNumber);
1159 for (WeightedPolygon_t::const_iterator iter = _polygon.begin();
1160 iter != _polygon.end(); ++iter)
1161 oldSet.polygon.push_back(iter->first);
1162
1163 // _newpolygon has changed, so now convert to array with matched indices
1164 PolygonWithIndices newSet;
1165 SphericalPointDistribution::IndexList_t::const_iterator beginiter = bestmatching.begin();
1166 SphericalPointDistribution::IndexList_t::const_iterator enditer = bestmatching.begin();
1167 std::advance(enditer, NumberIds);
1168 newSet.indices.resize(NumberIds, -1);
1169 std::copy(beginiter, enditer, newSet.indices.begin());
1170 std::copy(points.begin(),points.end(), std::back_inserter(newSet.polygon));
1171
1172 // determine rotation angles to align the two point distributions with
1173 // respect to bestmatching:
1174 // we use the center between the three first matching points
1175 /// the first rotation brings these two centers to coincide
1176 PolygonWithIndices rotatednewSet = newSet;
1177 {
1178 Rotation_t Rotation = findPlaneAligningRotation(oldSet, newSet);
1179 LOG(5, "DEBUG: Rotating coordinate system by " << Rotation.second
1180 << " around axis " << Rotation.first);
1181 Line Axis(zeroVec, Rotation.first);
1182
1183 // apply rotation angle to bring newCenter to oldCenter in joined points
1184 for (VectorArray_t::iterator iter = rotatednewSet.polygon.begin();
1185 iter != rotatednewSet.polygon.end(); ++iter) {
1186 Vector &current = *iter;
1187 LOG(6, "DEBUG: Original joined point is " << current);
1188 current = Axis.rotateVector(current, Rotation.second);
1189 LOG(6, "DEBUG: Rotated joined point is " << current);
1190 }
1191
1192 // apply rotation angle to the whole set of associated points
1193 for (VectorArray_t::iterator iter = associatedpoints.polygon.begin();
1194 iter != associatedpoints.polygon.end(); ++iter) {
1195 Vector &current = *iter;
1196 LOG(6, "DEBUG: Original associated point is " << current);
1197 current = Axis.rotateVector(current, Rotation.second);
1198 LOG(6, "DEBUG: Rotated associated point is " << current);
1199 }
1200
1201#ifndef NDEBUG
1202 // check: rotated "newCenter" should now equal oldCenter
1203 // we don't check in case of two points as these lie on a great circle
1204 // and the center cannot stably be recalculated. We may reactivate this
1205 // when we calculate centers only once
1206 if (oldSet.indices.size() > 2) {
1207 Vector oldCenter;
1208 Vector rotatednewCenter;
1209 calculateOldAndNewCenters(
1210 oldCenter, rotatednewCenter,
1211 oldSet, rotatednewSet);
1212 oldCenter.Normalize();
1213 rotatednewCenter.Normalize();
1214 // check whether centers are anti-parallel (factor -1)
1215 // then we have the "non-unique poles" situation: points lie on great circle
1216 // and both poles are valid solution
1217 if (fabs(oldCenter.ScalarProduct(rotatednewCenter) + 1.)
1218 < std::numeric_limits<double>::epsilon()*1e4)
1219 rotatednewCenter *= -1.;
1220 LOG(4, "CHECK: rotatednewCenter is " << rotatednewCenter
1221 << ", oldCenter is " << oldCenter);
1222 const double difference = (rotatednewCenter - oldCenter).NormSquared();
1223 ASSERT( difference < std::numeric_limits<double>::epsilon()*1e4,
1224 "matchSphericalPointDistributions() - rotation does not work as expected by "
1225 +toString(difference)+".");
1226 }
1227#endif
1228 }
1229 /// the second (orientation) rotation aligns the planes such that the
1230 /// points themselves coincide
1231 if (bestmatching.size() > 1) {
1232 Rotation_t Rotation = findPointAligningRotation(oldSet, rotatednewSet);
1233
1234 // construct RotationAxis and two points on its plane, defining the angle
1235 Rotation.first.Normalize();
1236 const Line RotationAxis(zeroVec, Rotation.first);
1237
1238 LOG(5, "DEBUG: Rotating around self is " << Rotation.second
1239 << " around axis " << RotationAxis);
1240
1241#ifndef NDEBUG
1242 // check: first bestmatching in rotated_newpolygon and remainingnew
1243 // should now equal
1244 {
1245 const IndexList_t::const_iterator iter = bestmatching.begin();
1246
1247 // check whether both old and newPosition are at same distance to oldCenter
1248 Vector oldCenter = calculateCenter(oldSet);
1249 const double distance = fabs(
1250 (oldSet.polygon[0] - oldCenter).NormSquared()
1251 - (rotatednewSet.polygon[*iter] - oldCenter).NormSquared()
1252 );
1253 LOG(4, "CHECK: Squared distance between oldPosition and newPosition "
1254 << " with respect to oldCenter " << oldCenter << " is " << distance);
1255// ASSERT( distance < warn_amplitude,
1256// "matchSphericalPointDistributions() - old and newPosition's squared distance to oldCenter differs by "
1257// +toString(distance));
1258
1259 Vector rotatednew = RotationAxis.rotateVector(
1260 rotatednewSet.polygon[*iter],
1261 Rotation.second);
1262 LOG(4, "CHECK: rotated first new bestmatching is " << rotatednew
1263 << " while old was " << oldSet.polygon[0]);
1264 const double difference = (rotatednew - oldSet.polygon[0]).NormSquared();
1265 ASSERT( difference < distance+warn_amplitude,
1266 "matchSphericalPointDistributions() - orientation rotation ends up off by "
1267 +toString(difference)+", more than "
1268 +toString(distance+warn_amplitude)+".");
1269 }
1270#endif
1271
1272 // align the set of associated points only here
1273 for (VectorArray_t::iterator iter = associatedpoints.polygon.begin();
1274 iter != associatedpoints.polygon.end(); ++iter) {
1275 Vector &current = *iter;
1276 LOG(6, "DEBUG: Original associated point is " << current);
1277 current = RotationAxis.rotateVector(current, Rotation.second);
1278 LOG(6, "DEBUG: Rotated associated point is " << current);
1279 }
1280 }
1281 }
1282
1283 return associatedpoints;
1284}
1285
1286SphericalPointDistribution::PolygonWithIndexTuples
1287SphericalPointDistribution::getIdentityAssociation(
1288 const WeightedPolygon_t &_polygon)
1289{
1290 unsigned int index = 0;
1291 SphericalPointDistribution::PolygonWithIndexTuples returnpolygon;
1292 for (WeightedPolygon_t::const_iterator iter = _polygon.begin();
1293 iter != _polygon.end(); ++iter, ++index) {
1294 returnpolygon.polygon.push_back( iter->first );
1295 ASSERT( iter->second == 1,
1296 "getIdentityAssociation() - bond with direction "
1297 +toString(iter->second)
1298 +" has degree higher than 1, getIdentityAssociation makes no sense.");
1299 returnpolygon.indices.push_back( IndexList_t(1, index) );
1300 }
1301 return returnpolygon;
1302}
Note: See TracBrowser for help on using the repository browser.