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