| 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/math/quaternion.hpp>
|
|---|
| 46 | #include <cmath>
|
|---|
| 47 | #include <functional>
|
|---|
| 48 | #include <iterator>
|
|---|
| 49 | #include <limits>
|
|---|
| 50 | #include <list>
|
|---|
| 51 | #include <vector>
|
|---|
| 52 | #include <map>
|
|---|
| 53 |
|
|---|
| 54 | #include "LinearAlgebra/Line.hpp"
|
|---|
| 55 | #include "LinearAlgebra/Plane.hpp"
|
|---|
| 56 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
|---|
| 57 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 58 |
|
|---|
| 59 | // static entities
|
|---|
| 60 | const double SphericalPointDistribution::SQRT_3(sqrt(3.0));
|
|---|
| 61 | const double SphericalPointDistribution::warn_amplitude = 1e-2;
|
|---|
| 62 |
|
|---|
| 63 | typedef std::vector<double> DistanceArray_t;
|
|---|
| 64 |
|
|---|
| 65 | // class generator: taken from www.cplusplus.com example std::generate
|
|---|
| 66 | struct c_unique {
|
|---|
| 67 | int current;
|
|---|
| 68 | c_unique() {current=0;}
|
|---|
| 69 | int operator()() {return current++;}
|
|---|
| 70 | } UniqueNumber;
|
|---|
| 71 |
|
|---|
| 72 | inline
|
|---|
| 73 | DistanceArray_t calculatePairwiseDistances(
|
|---|
| 74 | const std::vector<Vector> &_points,
|
|---|
| 75 | const SphericalPointDistribution::IndexList_t &_indices
|
|---|
| 76 | )
|
|---|
| 77 | {
|
|---|
| 78 | DistanceArray_t result;
|
|---|
| 79 | for (SphericalPointDistribution::IndexList_t::const_iterator firstiter = _indices.begin();
|
|---|
| 80 | firstiter != _indices.end(); ++firstiter) {
|
|---|
| 81 | for (SphericalPointDistribution::IndexList_t::const_iterator seconditer = firstiter;
|
|---|
| 82 | seconditer != _indices.end(); ++seconditer) {
|
|---|
| 83 | if (firstiter == seconditer)
|
|---|
| 84 | continue;
|
|---|
| 85 | const double distance = (_points[*firstiter] - _points[*seconditer]).NormSquared();
|
|---|
| 86 | result.push_back(distance);
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | return result;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /** Calculate the center of a given set of points in \a _positions but only
|
|---|
| 93 | * for those indicated by \a _indices.
|
|---|
| 94 | *
|
|---|
| 95 | */
|
|---|
| 96 | inline
|
|---|
| 97 | Vector calculateCenter(
|
|---|
| 98 | const SphericalPointDistribution::VectorArray_t &_positions,
|
|---|
| 99 | const SphericalPointDistribution::IndexList_t &_indices)
|
|---|
| 100 | {
|
|---|
| 101 | Vector Center;
|
|---|
| 102 | Center.Zero();
|
|---|
| 103 | for (SphericalPointDistribution::IndexList_t::const_iterator iter = _indices.begin();
|
|---|
| 104 | iter != _indices.end(); ++iter)
|
|---|
| 105 | Center += _positions[*iter];
|
|---|
| 106 | if (!_indices.empty())
|
|---|
| 107 | Center *= 1./(double)_indices.size();
|
|---|
| 108 |
|
|---|
| 109 | return Center;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /** Decides by an orthonormal third vector whether the sign of the rotation
|
|---|
| 113 | * angle should be negative or positive.
|
|---|
| 114 | *
|
|---|
| 115 | * \return -1 or 1
|
|---|
| 116 | */
|
|---|
| 117 | inline
|
|---|
| 118 | double determineSignOfRotation(
|
|---|
| 119 | const Vector &_oldPosition,
|
|---|
| 120 | const Vector &_newPosition,
|
|---|
| 121 | const Vector &_RotationAxis
|
|---|
| 122 | )
|
|---|
| 123 | {
|
|---|
| 124 | Vector dreiBein(_oldPosition);
|
|---|
| 125 | dreiBein.VectorProduct(_RotationAxis);
|
|---|
| 126 | dreiBein.Normalize();
|
|---|
| 127 | const double sign =
|
|---|
| 128 | (dreiBein.ScalarProduct(_newPosition) < 0.) ? -1. : +1.;
|
|---|
| 129 | LOG(6, "DEBUG: oldCenter on plane is " << _oldPosition
|
|---|
| 130 | << ", newCenter in plane is " << _newPosition
|
|---|
| 131 | << ", and dreiBein is " << dreiBein);
|
|---|
| 132 | return sign;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /** Convenience function to recalculate old and new center for determining plane
|
|---|
| 136 | * rotation.
|
|---|
| 137 | */
|
|---|
| 138 | inline
|
|---|
| 139 | void calculateOldAndNewCenters(
|
|---|
| 140 | Vector &_oldCenter,
|
|---|
| 141 | Vector &_newCenter,
|
|---|
| 142 | const SphericalPointDistribution::VectorArray_t &_referencepositions,
|
|---|
| 143 | const SphericalPointDistribution::VectorArray_t &_currentpositions,
|
|---|
| 144 | const SphericalPointDistribution::IndexList_t &_bestmatching)
|
|---|
| 145 | {
|
|---|
| 146 | const size_t NumberIds = std::min(_bestmatching.size(), (size_t)3);
|
|---|
| 147 | SphericalPointDistribution::IndexList_t continuousIds(NumberIds, -1);
|
|---|
| 148 | std::generate(continuousIds.begin(), continuousIds.end(), UniqueNumber);
|
|---|
| 149 | _oldCenter = calculateCenter(_referencepositions, continuousIds);
|
|---|
| 150 | // C++11 defines a copy_n function ...
|
|---|
| 151 | SphericalPointDistribution::IndexList_t::const_iterator enditer = _bestmatching.begin();
|
|---|
| 152 | std::advance(enditer, NumberIds);
|
|---|
| 153 | SphericalPointDistribution::IndexList_t firstbestmatchingIds(NumberIds, -1);
|
|---|
| 154 | std::copy(_bestmatching.begin(), enditer, firstbestmatchingIds.begin());
|
|---|
| 155 | _newCenter = calculateCenter( _currentpositions, firstbestmatchingIds);
|
|---|
| 156 | }
|
|---|
| 157 | /** Returns squared L2 error of the given \a _Matching.
|
|---|
| 158 | *
|
|---|
| 159 | * We compare the pair-wise distances of each associated matching
|
|---|
| 160 | * and check whether these distances each match between \a _old and
|
|---|
| 161 | * \a _new.
|
|---|
| 162 | *
|
|---|
| 163 | * \param _old first set of returnpolygon (fewer or equal to \a _new)
|
|---|
| 164 | * \param _new second set of returnpolygon
|
|---|
| 165 | * \param _Matching matching between the two sets
|
|---|
| 166 | * \return pair with L1 and squared L2 error
|
|---|
| 167 | */
|
|---|
| 168 | std::pair<double, double> SphericalPointDistribution::calculateErrorOfMatching(
|
|---|
| 169 | const std::vector<Vector> &_old,
|
|---|
| 170 | const std::vector<Vector> &_new,
|
|---|
| 171 | const IndexList_t &_Matching)
|
|---|
| 172 | {
|
|---|
| 173 | std::pair<double, double> errors( std::make_pair( 0., 0. ) );
|
|---|
| 174 |
|
|---|
| 175 | if (_Matching.size() > 1) {
|
|---|
| 176 | LOG(3, "INFO: Matching is " << _Matching);
|
|---|
| 177 |
|
|---|
| 178 | // calculate all pair-wise distances
|
|---|
| 179 | IndexList_t keys(_Matching.size());
|
|---|
| 180 | std::generate (keys.begin(), keys.end(), UniqueNumber);
|
|---|
| 181 | const DistanceArray_t firstdistances = calculatePairwiseDistances(_old, keys);
|
|---|
| 182 | const DistanceArray_t seconddistances = calculatePairwiseDistances(_new, _Matching);
|
|---|
| 183 |
|
|---|
| 184 | ASSERT( firstdistances.size() == seconddistances.size(),
|
|---|
| 185 | "calculateL2ErrorOfMatching() - mismatch in pair-wise distance array sizes.");
|
|---|
| 186 | DistanceArray_t::const_iterator firstiter = firstdistances.begin();
|
|---|
| 187 | DistanceArray_t::const_iterator seconditer = seconddistances.begin();
|
|---|
| 188 | for (;(firstiter != firstdistances.end()) && (seconditer != seconddistances.end());
|
|---|
| 189 | ++firstiter, ++seconditer) {
|
|---|
| 190 | const double gap = *firstiter - *seconditer;
|
|---|
| 191 | // L1 error
|
|---|
| 192 | if (errors.first < gap)
|
|---|
| 193 | errors.first = gap;
|
|---|
| 194 | // L2 error
|
|---|
| 195 | errors.second += gap*gap;
|
|---|
| 196 | }
|
|---|
| 197 | } else
|
|---|
| 198 | ELOG(3, "calculateErrorOfMatching() - Given matching's size is less than 2.");
|
|---|
| 199 | LOG(3, "INFO: Resulting errors for matching (L1, L2): "
|
|---|
| 200 | << errors.first << "," << errors.second << ".");
|
|---|
| 201 |
|
|---|
| 202 | return errors;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | SphericalPointDistribution::Polygon_t SphericalPointDistribution::removeMatchingPoints(
|
|---|
| 206 | const VectorArray_t &_points,
|
|---|
| 207 | const IndexList_t &_matchingindices
|
|---|
| 208 | )
|
|---|
| 209 | {
|
|---|
| 210 | SphericalPointDistribution::Polygon_t remainingreturnpolygon;
|
|---|
| 211 | IndexArray_t indices(_matchingindices.begin(), _matchingindices.end());
|
|---|
| 212 | std::sort(indices.begin(), indices.end());
|
|---|
| 213 | LOG(4, "DEBUG: sorted matching is " << indices);
|
|---|
| 214 | IndexArray_t remainingindices(_points.size(), -1);
|
|---|
| 215 | std::generate(remainingindices.begin(), remainingindices.end(), UniqueNumber);
|
|---|
| 216 | IndexArray_t::iterator remainiter = std::set_difference(
|
|---|
| 217 | remainingindices.begin(), remainingindices.end(),
|
|---|
| 218 | indices.begin(), indices.end(),
|
|---|
| 219 | remainingindices.begin());
|
|---|
| 220 | remainingindices.erase(remainiter, remainingindices.end());
|
|---|
| 221 | LOG(4, "DEBUG: remaining indices are " << remainingindices);
|
|---|
| 222 | for (IndexArray_t::const_iterator iter = remainingindices.begin();
|
|---|
| 223 | iter != remainingindices.end(); ++iter) {
|
|---|
| 224 | remainingreturnpolygon.push_back(_points[*iter]);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | return remainingreturnpolygon;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | /** Recursive function to go through all possible matchings.
|
|---|
| 231 | *
|
|---|
| 232 | * \param _MCS structure holding global information to the recursion
|
|---|
| 233 | * \param _matching current matching being build up
|
|---|
| 234 | * \param _indices contains still available indices
|
|---|
| 235 | * \param _matchingsize
|
|---|
| 236 | */
|
|---|
| 237 | void SphericalPointDistribution::recurseMatchings(
|
|---|
| 238 | MatchingControlStructure &_MCS,
|
|---|
| 239 | IndexList_t &_matching,
|
|---|
| 240 | IndexList_t _indices,
|
|---|
| 241 | unsigned int _matchingsize)
|
|---|
| 242 | {
|
|---|
| 243 | LOG(4, "DEBUG: Recursing with current matching " << _matching
|
|---|
| 244 | << ", remaining indices " << _indices
|
|---|
| 245 | << ", and sought size " << _matching.size()+_matchingsize);
|
|---|
| 246 | //!> threshold for L1 error below which matching is immediately acceptable
|
|---|
| 247 | const double L1THRESHOLD = 1e-2;
|
|---|
| 248 | if (!_MCS.foundflag) {
|
|---|
| 249 | LOG(4, "DEBUG: Current matching has size " << _matching.size() << ", places left " << _matchingsize);
|
|---|
| 250 | if (_matchingsize > 0) {
|
|---|
| 251 | // go through all indices
|
|---|
| 252 | for (IndexList_t::iterator iter = _indices.begin();
|
|---|
| 253 | (iter != _indices.end()) && (!_MCS.foundflag);) {
|
|---|
| 254 | // add index to matching
|
|---|
| 255 | _matching.push_back(*iter);
|
|---|
| 256 | LOG(5, "DEBUG: Adding " << *iter << " to matching.");
|
|---|
| 257 | // remove index but keep iterator to position (is the next to erase element)
|
|---|
| 258 | IndexList_t::iterator backupiter = _indices.erase(iter);
|
|---|
| 259 | // recurse with decreased _matchingsize
|
|---|
| 260 | recurseMatchings(_MCS, _matching, _indices, _matchingsize-1);
|
|---|
| 261 | // re-add chosen index and reset index to new position
|
|---|
| 262 | _indices.insert(backupiter, _matching.back());
|
|---|
| 263 | iter = backupiter;
|
|---|
| 264 | // remove index from _matching to make space for the next one
|
|---|
| 265 | _matching.pop_back();
|
|---|
| 266 | }
|
|---|
| 267 | // gone through all indices then exit recursion
|
|---|
| 268 | if (_matching.empty())
|
|---|
| 269 | _MCS.foundflag = true;
|
|---|
| 270 | } else {
|
|---|
| 271 | LOG(3, "INFO: Found matching " << _matching);
|
|---|
| 272 | // calculate errors
|
|---|
| 273 | std::pair<double, double> errors = calculateErrorOfMatching(
|
|---|
| 274 | _MCS.oldpoints, _MCS.newpoints, _matching);
|
|---|
| 275 | if (errors.first < L1THRESHOLD) {
|
|---|
| 276 | _MCS.bestmatching = _matching;
|
|---|
| 277 | _MCS.foundflag = true;
|
|---|
| 278 | } else if (_MCS.bestL2 > errors.second) {
|
|---|
| 279 | _MCS.bestmatching = _matching;
|
|---|
| 280 | _MCS.bestL2 = errors.second;
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | /** Finds combinatorially the best matching between points in \a _polygon
|
|---|
| 287 | * and \a _newpolygon.
|
|---|
| 288 | *
|
|---|
| 289 | * We find the matching with the smallest L2 error, where we break when we stumble
|
|---|
| 290 | * upon a matching with zero error.
|
|---|
| 291 | *
|
|---|
| 292 | * As points in \a _polygon may be have a weight greater 1 we have to match it to
|
|---|
| 293 | * multiple points in \a _newpolygon. Eventually, these multiple points are combined
|
|---|
| 294 | * for their center of weight, which is the only thing follow-up function see of
|
|---|
| 295 | * these multiple points. Hence, we actually modify \a _newpolygon in the process
|
|---|
| 296 | * such that the returned IndexList_t indicates a bijective mapping in the end.
|
|---|
| 297 | *
|
|---|
| 298 | * \sa recurseMatchings() for going through all matchings
|
|---|
| 299 | *
|
|---|
| 300 | * \param _polygon here, we have indices 0,1,2,...
|
|---|
| 301 | * \param _newpolygon and here we need to find the correct indices
|
|---|
| 302 | * \return list of indices: first in \a _polygon goes to first index for \a _newpolygon
|
|---|
| 303 | */
|
|---|
| 304 | SphericalPointDistribution::IndexList_t SphericalPointDistribution::findBestMatching(
|
|---|
| 305 | const WeightedPolygon_t &_polygon,
|
|---|
| 306 | Polygon_t &_newpolygon
|
|---|
| 307 | )
|
|---|
| 308 | {
|
|---|
| 309 | MatchingControlStructure MCS;
|
|---|
| 310 | MCS.foundflag = false;
|
|---|
| 311 | MCS.bestL2 = std::numeric_limits<double>::max();
|
|---|
| 312 | for (WeightedPolygon_t::const_iterator iter = _polygon.begin();
|
|---|
| 313 | iter != _polygon.end(); ++iter)
|
|---|
| 314 | MCS.oldpoints.push_back(iter->first);
|
|---|
| 315 | MCS.newpoints.insert(MCS.newpoints.begin(), _newpolygon.begin(),_newpolygon.end() );
|
|---|
| 316 |
|
|---|
| 317 | // search for bestmatching combinatorially
|
|---|
| 318 | {
|
|---|
| 319 | // translate polygon into vector to enable index addressing
|
|---|
| 320 | IndexList_t indices(_newpolygon.size());
|
|---|
| 321 | std::generate(indices.begin(), indices.end(), UniqueNumber);
|
|---|
| 322 | IndexList_t matching;
|
|---|
| 323 |
|
|---|
| 324 | // walk through all matchings
|
|---|
| 325 | const unsigned int matchingsize = _polygon.size();
|
|---|
| 326 | ASSERT( matchingsize <= indices.size(),
|
|---|
| 327 | "SphericalPointDistribution::matchSphericalPointDistributions() - not enough new points to choose for matching to old ones.");
|
|---|
| 328 | recurseMatchings(MCS, matching, indices, matchingsize);
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | // combine multiple points and create simple IndexList from IndexTupleList
|
|---|
| 332 | IndexTupleList_t bestmatching;
|
|---|
| 333 | for (IndexList_t::const_iterator iter = MCS.bestmatching.begin();
|
|---|
| 334 | iter != MCS.bestmatching.end(); ++iter)
|
|---|
| 335 | bestmatching.push_back(IndexList_t(1, *iter));
|
|---|
| 336 | const SphericalPointDistribution::IndexList_t IndexList =
|
|---|
| 337 | joinPoints(_newpolygon, MCS.newpoints, bestmatching);
|
|---|
| 338 |
|
|---|
| 339 | return IndexList;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | SphericalPointDistribution::IndexList_t SphericalPointDistribution::joinPoints(
|
|---|
| 343 | Polygon_t &_newpolygon,
|
|---|
| 344 | const VectorArray_t &_newpoints,
|
|---|
| 345 | const IndexTupleList_t &_bestmatching
|
|---|
| 346 | )
|
|---|
| 347 | {
|
|---|
| 348 | // combine all multiple points
|
|---|
| 349 | IndexList_t IndexList;
|
|---|
| 350 | IndexArray_t removalpoints;
|
|---|
| 351 | unsigned int UniqueIndex = _newpolygon.size(); // all indices up to size are used right now
|
|---|
| 352 | VectorArray_t newCenters;
|
|---|
| 353 | newCenters.reserve(_bestmatching.size());
|
|---|
| 354 | for (IndexTupleList_t::const_iterator tupleiter = _bestmatching.begin();
|
|---|
| 355 | tupleiter != _bestmatching.end(); ++tupleiter) {
|
|---|
| 356 | ASSERT (tupleiter->size() > 0,
|
|---|
| 357 | "findBestMatching() - encountered tuple in bestmatching with size 0.");
|
|---|
| 358 | if (tupleiter->size() == 1) {
|
|---|
| 359 | // add point and index
|
|---|
| 360 | IndexList.push_back(*tupleiter->begin());
|
|---|
| 361 | } else {
|
|---|
| 362 | // combine into weighted and normalized center
|
|---|
| 363 | Vector Center = calculateCenter(_newpoints, *tupleiter);
|
|---|
| 364 | Center.Normalize();
|
|---|
| 365 | _newpolygon.push_back(Center);
|
|---|
| 366 | LOG(5, "DEBUG: Combining " << tupleiter->size() << "points to weighted center "
|
|---|
| 367 | << Center << " with new index " << UniqueIndex);
|
|---|
| 368 | // mark for removal
|
|---|
| 369 | removalpoints.insert(removalpoints.end(), tupleiter->begin(), tupleiter->end());
|
|---|
| 370 | // add new index
|
|---|
| 371 | IndexList.push_back(UniqueIndex++);
|
|---|
| 372 | }
|
|---|
| 373 | }
|
|---|
| 374 | // IndexList is now our new bestmatching (that is bijective)
|
|---|
| 375 | LOG(4, "DEBUG: Our new bijective IndexList reads as " << IndexList);
|
|---|
| 376 |
|
|---|
| 377 | // modifying _newpolygon: remove all points in removalpoints, add those in newCenters
|
|---|
| 378 | Polygon_t allnewpoints = _newpolygon;
|
|---|
| 379 | {
|
|---|
| 380 | _newpolygon.clear();
|
|---|
| 381 | std::sort(removalpoints.begin(), removalpoints.end());
|
|---|
| 382 | size_t i = 0;
|
|---|
| 383 | IndexArray_t::const_iterator removeiter = removalpoints.begin();
|
|---|
| 384 | for (Polygon_t::iterator iter = allnewpoints.begin();
|
|---|
| 385 | iter != allnewpoints.end(); ++iter, ++i) {
|
|---|
| 386 | if ((removeiter != removalpoints.end()) && (i == *removeiter)) {
|
|---|
| 387 | // don't add, go to next remove index
|
|---|
| 388 | ++removeiter;
|
|---|
| 389 | } else {
|
|---|
| 390 | // otherwise add points
|
|---|
| 391 | _newpolygon.push_back(*iter);
|
|---|
| 392 | }
|
|---|
| 393 | }
|
|---|
| 394 | }
|
|---|
| 395 | LOG(4, "DEBUG: The polygon with recentered points removed is " << _newpolygon);
|
|---|
| 396 |
|
|---|
| 397 | // map IndexList to new shrinked _newpolygon
|
|---|
| 398 | typedef std::set<unsigned int> IndexSet_t;
|
|---|
| 399 | IndexSet_t SortedIndexList(IndexList.begin(), IndexList.end());
|
|---|
| 400 | IndexList.clear();
|
|---|
| 401 | {
|
|---|
| 402 | size_t offset = 0;
|
|---|
| 403 | IndexSet_t::const_iterator listiter = SortedIndexList.begin();
|
|---|
| 404 | IndexArray_t::const_iterator removeiter = removalpoints.begin();
|
|---|
| 405 | for (size_t i = 0; i < allnewpoints.size(); ++i) {
|
|---|
| 406 | if ((removeiter != removalpoints.end()) && (i == *removeiter)) {
|
|---|
| 407 | ++offset;
|
|---|
| 408 | ++removeiter;
|
|---|
| 409 | } else if ((listiter != SortedIndexList.end()) && (i == *listiter)) {
|
|---|
| 410 | IndexList.push_back(*listiter - offset);
|
|---|
| 411 | ++listiter;
|
|---|
| 412 | }
|
|---|
| 413 | }
|
|---|
| 414 | }
|
|---|
| 415 | LOG(4, "DEBUG: Our new bijective IndexList corrected for removed points reads as "
|
|---|
| 416 | << IndexList);
|
|---|
| 417 |
|
|---|
| 418 | return IndexList;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | SphericalPointDistribution::Rotation_t SphericalPointDistribution::findPlaneAligningRotation(
|
|---|
| 422 | const VectorArray_t &_referencepositions,
|
|---|
| 423 | const VectorArray_t &_currentpositions,
|
|---|
| 424 | const IndexList_t &_bestmatching
|
|---|
| 425 | )
|
|---|
| 426 | {
|
|---|
| 427 | bool dontcheck = false;
|
|---|
| 428 | // initialize to no rotation
|
|---|
| 429 | Rotation_t Rotation;
|
|---|
| 430 | Rotation.first.Zero();
|
|---|
| 431 | Rotation.first[0] = 1.;
|
|---|
| 432 | Rotation.second = 0.;
|
|---|
| 433 |
|
|---|
| 434 | // calculate center of triangle/line/point consisting of first points of matching
|
|---|
| 435 | Vector oldCenter;
|
|---|
| 436 | Vector newCenter;
|
|---|
| 437 | calculateOldAndNewCenters(
|
|---|
| 438 | oldCenter, newCenter,
|
|---|
| 439 | _referencepositions, _currentpositions, _bestmatching);
|
|---|
| 440 |
|
|---|
| 441 | if ((!oldCenter.IsZero()) && (!newCenter.IsZero())) {
|
|---|
| 442 | LOG(4, "DEBUG: oldCenter is " << oldCenter << ", newCenter is " << newCenter);
|
|---|
| 443 | oldCenter.Normalize();
|
|---|
| 444 | newCenter.Normalize();
|
|---|
| 445 | if (!oldCenter.IsEqualTo(newCenter)) {
|
|---|
| 446 | // calculate rotation axis and angle
|
|---|
| 447 | Rotation.first = oldCenter;
|
|---|
| 448 | Rotation.first.VectorProduct(newCenter);
|
|---|
| 449 | Rotation.second = oldCenter.Angle(newCenter); // /(M_PI/2.);
|
|---|
| 450 | } else {
|
|---|
| 451 | // no rotation required anymore
|
|---|
| 452 | }
|
|---|
| 453 | } else {
|
|---|
| 454 | LOG(4, "DEBUG: oldCenter is " << oldCenter << ", newCenter is " << newCenter);
|
|---|
| 455 | if ((oldCenter.IsZero()) && (newCenter.IsZero())) {
|
|---|
| 456 | // either oldCenter or newCenter (or both) is directly at origin
|
|---|
| 457 | if (_bestmatching.size() == 2) {
|
|---|
| 458 | // line case
|
|---|
| 459 | Vector oldPosition = _currentpositions[*_bestmatching.begin()];
|
|---|
| 460 | Vector newPosition = _referencepositions[0];
|
|---|
| 461 | // check whether we need to rotate at all
|
|---|
| 462 | if (!oldPosition.IsEqualTo(newPosition)) {
|
|---|
| 463 | Rotation.first = oldPosition;
|
|---|
| 464 | Rotation.first.VectorProduct(newPosition);
|
|---|
| 465 | // orientation will fix the sign here eventually
|
|---|
| 466 | Rotation.second = oldPosition.Angle(newPosition);
|
|---|
| 467 | } else {
|
|---|
| 468 | // no rotation required anymore
|
|---|
| 469 | }
|
|---|
| 470 | } else {
|
|---|
| 471 | // triangle case
|
|---|
| 472 | // both triangles/planes have same center, hence get axis by
|
|---|
| 473 | // VectorProduct of Normals
|
|---|
| 474 | Plane newplane(_referencepositions[0], _referencepositions[1], _referencepositions[2]);
|
|---|
| 475 | VectorArray_t vectors;
|
|---|
| 476 | for (IndexList_t::const_iterator iter = _bestmatching.begin();
|
|---|
| 477 | iter != _bestmatching.end(); ++iter)
|
|---|
| 478 | vectors.push_back(_currentpositions[*iter]);
|
|---|
| 479 | Plane oldplane(vectors[0], vectors[1], vectors[2]);
|
|---|
| 480 | Vector oldPosition = oldplane.getNormal();
|
|---|
| 481 | Vector newPosition = newplane.getNormal();
|
|---|
| 482 | // check whether we need to rotate at all
|
|---|
| 483 | if (!oldPosition.IsEqualTo(newPosition)) {
|
|---|
| 484 | Rotation.first = oldPosition;
|
|---|
| 485 | Rotation.first.VectorProduct(newPosition);
|
|---|
| 486 | Rotation.first.Normalize();
|
|---|
| 487 |
|
|---|
| 488 | // construct reference vector to determine direction of rotation
|
|---|
| 489 | const double sign = determineSignOfRotation(oldPosition, newPosition, Rotation.first);
|
|---|
| 490 | Rotation.second = sign * oldPosition.Angle(newPosition);
|
|---|
| 491 | LOG(5, "DEBUG: Rotating plane normals by " << Rotation.second
|
|---|
| 492 | << " around axis " << Rotation.first);
|
|---|
| 493 | } else {
|
|---|
| 494 | // else do nothing
|
|---|
| 495 | }
|
|---|
| 496 | }
|
|---|
| 497 | } else {
|
|---|
| 498 | // TODO: we can't do anything here, but this case needs to be dealt with when
|
|---|
| 499 | // we have no ideal geometries anymore
|
|---|
| 500 | if ((oldCenter-newCenter).Norm() > warn_amplitude)
|
|---|
| 501 | ELOG(2, "oldCenter is " << oldCenter << ", yet newCenter is " << newCenter);
|
|---|
| 502 | // else they are considered close enough
|
|---|
| 503 | dontcheck = true;
|
|---|
| 504 | }
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | #ifndef NDEBUG
|
|---|
| 508 | // check: rotation brings newCenter onto oldCenter position
|
|---|
| 509 | if (!dontcheck) {
|
|---|
| 510 | Line Axis(zeroVec, Rotation.first);
|
|---|
| 511 | Vector test = Axis.rotateVector(newCenter, Rotation.second);
|
|---|
| 512 | LOG(4, "CHECK: rotated newCenter is " << test
|
|---|
| 513 | << ", oldCenter is " << oldCenter);
|
|---|
| 514 | ASSERT( (test - oldCenter).NormSquared() < std::numeric_limits<double>::epsilon()*1e4,
|
|---|
| 515 | "matchSphericalPointDistributions() - rotation does not work as expected by "
|
|---|
| 516 | +toString((test - oldCenter).NormSquared())+".");
|
|---|
| 517 | }
|
|---|
| 518 | #endif
|
|---|
| 519 |
|
|---|
| 520 | return Rotation;
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | SphericalPointDistribution::Rotation_t SphericalPointDistribution::findPointAligningRotation(
|
|---|
| 524 | const VectorArray_t &remainingold,
|
|---|
| 525 | const VectorArray_t &remainingnew,
|
|---|
| 526 | const IndexList_t &_bestmatching)
|
|---|
| 527 | {
|
|---|
| 528 | // initialize rotation to zero
|
|---|
| 529 | Rotation_t Rotation;
|
|---|
| 530 | Rotation.first.Zero();
|
|---|
| 531 | Rotation.first[0] = 1.;
|
|---|
| 532 | Rotation.second = 0.;
|
|---|
| 533 |
|
|---|
| 534 | // recalculate center
|
|---|
| 535 | Vector oldCenter;
|
|---|
| 536 | Vector newCenter;
|
|---|
| 537 | calculateOldAndNewCenters(
|
|---|
| 538 | oldCenter, newCenter,
|
|---|
| 539 | remainingold, remainingnew, _bestmatching);
|
|---|
| 540 |
|
|---|
| 541 | Vector oldPosition = remainingnew[*_bestmatching.begin()];
|
|---|
| 542 | Vector newPosition = remainingold[0];
|
|---|
| 543 | LOG(6, "DEBUG: oldPosition is " << oldPosition << " and newPosition is " << newPosition);
|
|---|
| 544 | if (!oldPosition.IsEqualTo(newPosition)) {
|
|---|
| 545 | if ((!oldCenter.IsZero()) && (!newCenter.IsZero())) {
|
|---|
| 546 | oldCenter.Normalize(); // note weighted sum of normalized weight is not normalized
|
|---|
| 547 | Rotation.first = oldCenter;
|
|---|
| 548 | LOG(6, "DEBUG: Picking normalized oldCenter as Rotation.first " << oldCenter);
|
|---|
| 549 | oldPosition.ProjectOntoPlane(Rotation.first);
|
|---|
| 550 | newPosition.ProjectOntoPlane(Rotation.first);
|
|---|
| 551 | LOG(6, "DEBUG: Positions after projection are " << oldPosition << " and " << newPosition);
|
|---|
| 552 | } else {
|
|---|
| 553 | if (_bestmatching.size() == 2) {
|
|---|
| 554 | // line situation
|
|---|
| 555 | try {
|
|---|
| 556 | Plane oldplane(oldPosition, oldCenter, newPosition);
|
|---|
| 557 | Rotation.first = oldplane.getNormal();
|
|---|
| 558 | LOG(6, "DEBUG: Plane is " << oldplane << " and normal is " << Rotation.first);
|
|---|
| 559 | } catch (LinearDependenceException &e) {
|
|---|
| 560 | LOG(6, "DEBUG: Vectors defining plane are linearly dependent.");
|
|---|
| 561 | // oldPosition and newPosition are on a line, just flip when not equal
|
|---|
| 562 | if (!oldPosition.IsEqualTo(newPosition)) {
|
|---|
| 563 | Rotation.first.Zero();
|
|---|
| 564 | Rotation.first.GetOneNormalVector(oldPosition);
|
|---|
| 565 | LOG(6, "DEBUG: For flipping we use Rotation.first " << Rotation.first);
|
|---|
| 566 | assert( Rotation.first.ScalarProduct(oldPosition) < std::numeric_limits<double>::epsilon()*1e4);
|
|---|
| 567 | // Rotation.second = M_PI;
|
|---|
| 568 | } else {
|
|---|
| 569 | LOG(6, "DEBUG: oldPosition and newPosition are equivalent.");
|
|---|
| 570 | }
|
|---|
| 571 | }
|
|---|
| 572 | } else {
|
|---|
| 573 | // triangle situation
|
|---|
| 574 | Plane oldplane(remainingold[0], remainingold[1], remainingold[2]);
|
|---|
| 575 | Rotation.first = oldplane.getNormal();
|
|---|
| 576 | LOG(6, "DEBUG: oldPlane is " << oldplane << " and normal is " << Rotation.first);
|
|---|
| 577 | oldPosition.ProjectOntoPlane(Rotation.first);
|
|---|
| 578 | LOG(6, "DEBUG: Positions after projection are " << oldPosition << " and " << newPosition);
|
|---|
| 579 | }
|
|---|
| 580 | }
|
|---|
| 581 | // construct reference vector to determine direction of rotation
|
|---|
| 582 | const double sign = determineSignOfRotation(oldPosition, newPosition, Rotation.first);
|
|---|
| 583 | Rotation.second = sign * oldPosition.Angle(newPosition);
|
|---|
| 584 | } else {
|
|---|
| 585 | LOG(6, "DEBUG: oldPosition and newPosition are equivalent, hence no orientating rotation.");
|
|---|
| 586 | }
|
|---|
| 587 |
|
|---|
| 588 | return Rotation;
|
|---|
| 589 | }
|
|---|
| 590 |
|
|---|
| 591 |
|
|---|
| 592 | SphericalPointDistribution::Polygon_t
|
|---|
| 593 | SphericalPointDistribution::matchSphericalPointDistributions(
|
|---|
| 594 | const SphericalPointDistribution::WeightedPolygon_t &_polygon,
|
|---|
| 595 | SphericalPointDistribution::Polygon_t &_newpolygon
|
|---|
| 596 | )
|
|---|
| 597 | {
|
|---|
| 598 | SphericalPointDistribution::Polygon_t remainingpoints;
|
|---|
| 599 | VectorArray_t remainingold;
|
|---|
| 600 | for (WeightedPolygon_t::const_iterator iter = _polygon.begin();
|
|---|
| 601 | iter != _polygon.end(); ++iter)
|
|---|
| 602 | remainingold.push_back(iter->first);
|
|---|
| 603 | VectorArray_t remainingnew(_newpolygon.begin(), _newpolygon.end());
|
|---|
| 604 | LOG(2, "INFO: Matching old polygon " << _polygon
|
|---|
| 605 | << " with new polygon " << _newpolygon);
|
|---|
| 606 |
|
|---|
| 607 | if (_polygon.size() == _newpolygon.size()) {
|
|---|
| 608 | // same number of points desired as are present? Do nothing
|
|---|
| 609 | LOG(2, "INFO: There are no vacant points to return.");
|
|---|
| 610 | return remainingpoints;
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| 613 | if (_polygon.size() > 0) {
|
|---|
| 614 | IndexList_t bestmatching = findBestMatching(_polygon, _newpolygon);
|
|---|
| 615 | LOG(2, "INFO: Best matching is " << bestmatching);
|
|---|
| 616 |
|
|---|
| 617 | // determine rotation angles to align the two point distributions with
|
|---|
| 618 | // respect to bestmatching:
|
|---|
| 619 | // we use the center between the three first matching points
|
|---|
| 620 | /// the first rotation brings these two centers to coincide
|
|---|
| 621 | VectorArray_t rotated_newpolygon = remainingnew;
|
|---|
| 622 | {
|
|---|
| 623 | Rotation_t Rotation = findPlaneAligningRotation(
|
|---|
| 624 | remainingold,
|
|---|
| 625 | remainingnew,
|
|---|
| 626 | bestmatching);
|
|---|
| 627 | LOG(5, "DEBUG: Rotating coordinate system by " << Rotation.second
|
|---|
| 628 | << " around axis " << Rotation.first);
|
|---|
| 629 | Line Axis(zeroVec, Rotation.first);
|
|---|
| 630 |
|
|---|
| 631 | // apply rotation angle to bring newCenter to oldCenter
|
|---|
| 632 | for (VectorArray_t::iterator iter = rotated_newpolygon.begin();
|
|---|
| 633 | iter != rotated_newpolygon.end(); ++iter) {
|
|---|
| 634 | Vector ¤t = *iter;
|
|---|
| 635 | LOG(6, "DEBUG: Original point is " << current);
|
|---|
| 636 | current = Axis.rotateVector(current, Rotation.second);
|
|---|
| 637 | LOG(6, "DEBUG: Rotated point is " << current);
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | #ifndef NDEBUG
|
|---|
| 641 | // check: rotated "newCenter" should now equal oldCenter
|
|---|
| 642 | {
|
|---|
| 643 | Vector oldCenter;
|
|---|
| 644 | Vector rotatednewCenter;
|
|---|
| 645 | calculateOldAndNewCenters(
|
|---|
| 646 | oldCenter, rotatednewCenter,
|
|---|
| 647 | remainingold, rotated_newpolygon, bestmatching);
|
|---|
| 648 | // NOTE: Center must not necessarily lie on the sphere with norm 1, hence, we
|
|---|
| 649 | // have to normalize it just as before, as oldCenter and newCenter lengths may differ.
|
|---|
| 650 | if ((!oldCenter.IsZero()) && (!rotatednewCenter.IsZero())) {
|
|---|
| 651 | oldCenter.Normalize();
|
|---|
| 652 | rotatednewCenter.Normalize();
|
|---|
| 653 | LOG(4, "CHECK: rotatednewCenter is " << rotatednewCenter
|
|---|
| 654 | << ", oldCenter is " << oldCenter);
|
|---|
| 655 | ASSERT( (rotatednewCenter - oldCenter).NormSquared() < std::numeric_limits<double>::epsilon()*1e4,
|
|---|
| 656 | "matchSphericalPointDistributions() - rotation does not work as expected by "
|
|---|
| 657 | +toString((rotatednewCenter - oldCenter).NormSquared())+".");
|
|---|
| 658 | }
|
|---|
| 659 | }
|
|---|
| 660 | #endif
|
|---|
| 661 | }
|
|---|
| 662 | /// the second (orientation) rotation aligns the planes such that the
|
|---|
| 663 | /// points themselves coincide
|
|---|
| 664 | if (bestmatching.size() > 1) {
|
|---|
| 665 | Rotation_t Rotation = findPointAligningRotation(
|
|---|
| 666 | remainingold,
|
|---|
| 667 | rotated_newpolygon,
|
|---|
| 668 | bestmatching);
|
|---|
| 669 |
|
|---|
| 670 | // construct RotationAxis and two points on its plane, defining the angle
|
|---|
| 671 | Rotation.first.Normalize();
|
|---|
| 672 | const Line RotationAxis(zeroVec, Rotation.first);
|
|---|
| 673 |
|
|---|
| 674 | LOG(5, "DEBUG: Rotating around self is " << Rotation.second
|
|---|
| 675 | << " around axis " << RotationAxis);
|
|---|
| 676 |
|
|---|
| 677 | #ifndef NDEBUG
|
|---|
| 678 | // check: first bestmatching in rotated_newpolygon and remainingnew
|
|---|
| 679 | // should now equal
|
|---|
| 680 | {
|
|---|
| 681 | const IndexList_t::const_iterator iter = bestmatching.begin();
|
|---|
| 682 | Vector rotatednew = RotationAxis.rotateVector(
|
|---|
| 683 | rotated_newpolygon[*iter],
|
|---|
| 684 | Rotation.second);
|
|---|
| 685 | LOG(4, "CHECK: rotated first new bestmatching is " << rotatednew
|
|---|
| 686 | << " while old was " << remainingold[0]);
|
|---|
| 687 | ASSERT( (rotatednew - remainingold[0]).Norm() < warn_amplitude,
|
|---|
| 688 | "matchSphericalPointDistributions() - orientation rotation ends up off by more than "
|
|---|
| 689 | +toString(warn_amplitude)+".");
|
|---|
| 690 | }
|
|---|
| 691 | #endif
|
|---|
| 692 |
|
|---|
| 693 | for (VectorArray_t::iterator iter = rotated_newpolygon.begin();
|
|---|
| 694 | iter != rotated_newpolygon.end(); ++iter) {
|
|---|
| 695 | Vector ¤t = *iter;
|
|---|
| 696 | LOG(6, "DEBUG: Original point is " << current);
|
|---|
| 697 | current = RotationAxis.rotateVector(current, Rotation.second);
|
|---|
| 698 | LOG(6, "DEBUG: Rotated point is " << current);
|
|---|
| 699 | }
|
|---|
| 700 | }
|
|---|
| 701 |
|
|---|
| 702 | // remove all points in matching and return remaining ones
|
|---|
| 703 | SphericalPointDistribution::Polygon_t remainingpoints =
|
|---|
| 704 | removeMatchingPoints(rotated_newpolygon, bestmatching);
|
|---|
| 705 | LOG(2, "INFO: Remaining points are " << remainingpoints);
|
|---|
| 706 | return remainingpoints;
|
|---|
| 707 | } else
|
|---|
| 708 | return _newpolygon;
|
|---|
| 709 | }
|
|---|