| 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 <cmath>
|
|---|
| 46 | #include <limits>
|
|---|
| 47 | #include <list>
|
|---|
| 48 | #include <vector>
|
|---|
| 49 | #include <map>
|
|---|
| 50 |
|
|---|
| 51 | #include "LinearAlgebra/Line.hpp"
|
|---|
| 52 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
|---|
| 53 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 54 |
|
|---|
| 55 | typedef std::list<unsigned int> IndexList_t;
|
|---|
| 56 | typedef std::vector<unsigned int> IndexArray_t;
|
|---|
| 57 | typedef std::vector<Vector> VectorArray_t;
|
|---|
| 58 | typedef std::vector<double> DistanceArray_t;
|
|---|
| 59 |
|
|---|
| 60 | // static instances
|
|---|
| 61 | const double SphericalPointDistribution::SQRT_3(sqrt(3.0));
|
|---|
| 62 |
|
|---|
| 63 | DistanceArray_t calculatePairwiseDistances(
|
|---|
| 64 | const std::vector<Vector> &_returnpolygon,
|
|---|
| 65 | const IndexList_t &_indices
|
|---|
| 66 | )
|
|---|
| 67 | {
|
|---|
| 68 | DistanceArray_t result;
|
|---|
| 69 | for (IndexList_t::const_iterator firstiter = _indices.begin();
|
|---|
| 70 | firstiter != _indices.end(); ++firstiter) {
|
|---|
| 71 | for (IndexList_t::const_iterator seconditer = firstiter;
|
|---|
| 72 | seconditer != _indices.end(); ++seconditer) {
|
|---|
| 73 | if (firstiter == seconditer)
|
|---|
| 74 | continue;
|
|---|
| 75 | const double distance = (_returnpolygon[*firstiter] - _returnpolygon[*seconditer]).NormSquared();
|
|---|
| 76 | result.push_back(distance);
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 | return result;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | // class generator: taken from www.cplusplus.com example std::generate
|
|---|
| 83 | struct c_unique {
|
|---|
| 84 | int current;
|
|---|
| 85 | c_unique() {current=0;}
|
|---|
| 86 | int operator()() {return ++current;}
|
|---|
| 87 | } UniqueNumber;
|
|---|
| 88 |
|
|---|
| 89 | /** Returns squared L2 error of the given \a _Matching.
|
|---|
| 90 | *
|
|---|
| 91 | * We compare the pair-wise distances of each associated matching
|
|---|
| 92 | * and check whether these distances each match between \a _old and
|
|---|
| 93 | * \a _new.
|
|---|
| 94 | *
|
|---|
| 95 | * \param _old first set of returnpolygon (fewer or equal to \a _new)
|
|---|
| 96 | * \param _new second set of returnpolygon
|
|---|
| 97 | * \param _Matching matching between the two sets
|
|---|
| 98 | * \return pair with L1 and squared L2 error
|
|---|
| 99 | */
|
|---|
| 100 | std::pair<double, double> calculateErrorOfMatching(
|
|---|
| 101 | const std::vector<Vector> &_old,
|
|---|
| 102 | const std::vector<Vector> &_new,
|
|---|
| 103 | const IndexList_t &_Matching)
|
|---|
| 104 | {
|
|---|
| 105 | std::pair<double, double> errors( std::make_pair( 0., 0. ) );
|
|---|
| 106 |
|
|---|
| 107 | if (_Matching.size() > 1) {
|
|---|
| 108 | LOG(3, "INFO: Matching is " << _Matching);
|
|---|
| 109 |
|
|---|
| 110 | // calculate all pair-wise distances
|
|---|
| 111 | IndexList_t keys(_Matching.size());
|
|---|
| 112 | std::generate (keys.begin(), keys.end(), UniqueNumber);
|
|---|
| 113 | const DistanceArray_t firstdistances = calculatePairwiseDistances(_old, keys);
|
|---|
| 114 | const DistanceArray_t seconddistances = calculatePairwiseDistances(_new, _Matching);
|
|---|
| 115 |
|
|---|
| 116 | ASSERT( firstdistances.size() == seconddistances.size(),
|
|---|
| 117 | "calculateL2ErrorOfMatching() - mismatch in pair-wise distance array sizes.");
|
|---|
| 118 | DistanceArray_t::const_iterator firstiter = firstdistances.begin();
|
|---|
| 119 | DistanceArray_t::const_iterator seconditer = seconddistances.begin();
|
|---|
| 120 | for (;(firstiter != firstdistances.end()) && (seconditer != seconddistances.end());
|
|---|
| 121 | ++firstiter, ++seconditer) {
|
|---|
| 122 | const double gap = *firstiter - *seconditer;
|
|---|
| 123 | // L1 error
|
|---|
| 124 | if (errors.first < gap)
|
|---|
| 125 | errors.first = gap;
|
|---|
| 126 | // L2 error
|
|---|
| 127 | errors.second += gap*gap;
|
|---|
| 128 | }
|
|---|
| 129 | } else
|
|---|
| 130 | ELOG(2, "calculateErrorOfMatching() - Given matching is empty.");
|
|---|
| 131 | LOG(3, "INFO: Resulting errors for matching (L1, L2): "
|
|---|
| 132 | << errors.first << "," << errors.second << ".");
|
|---|
| 133 |
|
|---|
| 134 | return errors;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | SphericalPointDistribution::Polygon_t removeMatchingPoints(
|
|---|
| 138 | const SphericalPointDistribution::Polygon_t &_returnpolygon,
|
|---|
| 139 | const IndexList_t &_matchingindices
|
|---|
| 140 | )
|
|---|
| 141 | {
|
|---|
| 142 | SphericalPointDistribution::Polygon_t remainingreturnpolygon;
|
|---|
| 143 | IndexArray_t indices(_matchingindices.begin(), _matchingindices.end());
|
|---|
| 144 | std::sort(indices.begin(), indices.end());
|
|---|
| 145 | LOG(4, "DEBUG: sorted matching is " << indices);
|
|---|
| 146 | IndexArray_t::const_iterator valueiter = indices.begin();
|
|---|
| 147 | SphericalPointDistribution::Polygon_t::const_iterator pointiter =
|
|---|
| 148 | _returnpolygon.begin();
|
|---|
| 149 | for (unsigned int i=0; i< _returnpolygon.size(); ++i, ++pointiter) {
|
|---|
| 150 | // skip all those in values
|
|---|
| 151 | if (*valueiter == i)
|
|---|
| 152 | ++valueiter;
|
|---|
| 153 | else
|
|---|
| 154 | remainingreturnpolygon.push_back(*pointiter);
|
|---|
| 155 | }
|
|---|
| 156 | LOG(4, "DEBUG: remaining indices are " << remainingreturnpolygon);
|
|---|
| 157 |
|
|---|
| 158 | return remainingreturnpolygon;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /** Rotates a given polygon around x, y, and z axis by the given angles.
|
|---|
| 162 | *
|
|---|
| 163 | * Essentially, we concentrate on the three returnpolygon of the polygon to rotate
|
|---|
| 164 | * to the correct position. First, we rotate its center via \a angles,
|
|---|
| 165 | * then we rotate the "triangle" around itself/\a _RotationAxis by
|
|---|
| 166 | * \a _RotationAngle.
|
|---|
| 167 | *
|
|---|
| 168 | * \param _polygon polygon whose returnpolygon to rotate
|
|---|
| 169 | * \param _angles vector with rotation angles for x,y,z axis
|
|---|
| 170 | * \param _RotationAxis
|
|---|
| 171 | * \param _RotationAngle
|
|---|
| 172 | */
|
|---|
| 173 | SphericalPointDistribution::Polygon_t rotatePolygon(
|
|---|
| 174 | const SphericalPointDistribution::Polygon_t &_polygon,
|
|---|
| 175 | const std::vector<double> &_angles,
|
|---|
| 176 | const Line &_RotationAxis,
|
|---|
| 177 | const double _RotationAngle)
|
|---|
| 178 | {
|
|---|
| 179 | SphericalPointDistribution::Polygon_t rotated_polygon = _polygon;
|
|---|
| 180 | RealSpaceMatrix rotation;
|
|---|
| 181 | ASSERT( _angles.size() == 3,
|
|---|
| 182 | "rotatePolygon() - not exactly "+toString(3)+" angles given.");
|
|---|
| 183 | rotation.setRotation(_angles[0] * M_PI/180., _angles[1] * M_PI/180., _angles[2] * M_PI/180.);
|
|---|
| 184 | LOG(4, "DEBUG: Rotation matrix is " << rotation);
|
|---|
| 185 |
|
|---|
| 186 | // apply rotation angles
|
|---|
| 187 | for (SphericalPointDistribution::Polygon_t::iterator iter = rotated_polygon.begin();
|
|---|
| 188 | iter != rotated_polygon.end(); ++iter) {
|
|---|
| 189 | *iter = rotation * (*iter);
|
|---|
| 190 | _RotationAxis.rotateVector(*iter, _RotationAngle);
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | return rotated_polygon;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | struct MatchingControlStructure {
|
|---|
| 197 | bool foundflag;
|
|---|
| 198 | double bestL2;
|
|---|
| 199 | IndexList_t bestmatching;
|
|---|
| 200 | VectorArray_t oldreturnpolygon;
|
|---|
| 201 | VectorArray_t newreturnpolygon;
|
|---|
| 202 | };
|
|---|
| 203 |
|
|---|
| 204 | /** Recursive function to go through all possible matchings.
|
|---|
| 205 | *
|
|---|
| 206 | * \param _MCS structure holding global information to the recursion
|
|---|
| 207 | * \param _matching current matching being build up
|
|---|
| 208 | * \param _indices contains still available indices
|
|---|
| 209 | * \param _matchingsize
|
|---|
| 210 | */
|
|---|
| 211 | void recurseMatchings(
|
|---|
| 212 | MatchingControlStructure &_MCS,
|
|---|
| 213 | IndexList_t &_matching,
|
|---|
| 214 | IndexList_t _indices,
|
|---|
| 215 | unsigned int _matchingsize)
|
|---|
| 216 | {
|
|---|
| 217 | LOG(4, "DEBUG: Recursing with current matching " << _matching
|
|---|
| 218 | << ", remaining indices " << _indices
|
|---|
| 219 | << ", and sought size " << _matchingsize);
|
|---|
| 220 | //!> threshold for L1 error below which matching is immediately acceptable
|
|---|
| 221 | const double L1THRESHOLD = 1e-2;
|
|---|
| 222 | if (!_MCS.foundflag) {
|
|---|
| 223 | LOG(3, "INFO: Current matching has size " << _matching.size() << " of " << _matchingsize);
|
|---|
| 224 | if (_matching.size() < _matchingsize) {
|
|---|
| 225 | // go through all indices
|
|---|
| 226 | for (IndexList_t::iterator iter = _indices.begin();
|
|---|
| 227 | iter != _indices.end();) {
|
|---|
| 228 | // add index to matching
|
|---|
| 229 | _matching.push_back(*iter);
|
|---|
| 230 | LOG(4, "DEBUG: Adding " << *iter << " to matching.");
|
|---|
| 231 | // remove index but keep iterator to position (is the next to erase element)
|
|---|
| 232 | IndexList_t::iterator backupiter = _indices.erase(iter);
|
|---|
| 233 | // recurse with decreased _matchingsize
|
|---|
| 234 | recurseMatchings(_MCS, _matching, _indices, _matchingsize-1);
|
|---|
| 235 | // re-add chosen index and reset index to new position
|
|---|
| 236 | _indices.insert(backupiter, _matching.back());
|
|---|
| 237 | iter = backupiter;
|
|---|
| 238 | // remove index from _matching to make space for the next one
|
|---|
| 239 | _matching.pop_back();
|
|---|
| 240 | }
|
|---|
| 241 | // gone through all indices then exit recursion
|
|---|
| 242 | _MCS.foundflag = true;
|
|---|
| 243 | } else {
|
|---|
| 244 | LOG(3, "INFO: Found matching " << _matching);
|
|---|
| 245 | // calculate errors
|
|---|
| 246 | std::pair<double, double> errors = calculateErrorOfMatching(
|
|---|
| 247 | _MCS.oldreturnpolygon, _MCS.newreturnpolygon, _matching);
|
|---|
| 248 | if (errors.first < L1THRESHOLD) {
|
|---|
| 249 | _MCS.bestmatching = _matching;
|
|---|
| 250 | _MCS.foundflag = true;
|
|---|
| 251 | }
|
|---|
| 252 | if (_MCS.bestL2 > errors.second) {
|
|---|
| 253 | _MCS.bestmatching = _matching;
|
|---|
| 254 | _MCS.bestL2 = errors.second;
|
|---|
| 255 | }
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | SphericalPointDistribution::Polygon_t
|
|---|
| 261 | SphericalPointDistribution::matchSphericalPointDistributions(
|
|---|
| 262 | const SphericalPointDistribution::Polygon_t &_polygon,
|
|---|
| 263 | const SphericalPointDistribution::Polygon_t &_newpolygon
|
|---|
| 264 | )
|
|---|
| 265 | {
|
|---|
| 266 | SphericalPointDistribution::Polygon_t remainingreturnpolygon;
|
|---|
| 267 | VectorArray_t remainingold(_polygon.begin(), _polygon.end());
|
|---|
| 268 | VectorArray_t remainingnew(_newpolygon.begin(), _newpolygon.end());
|
|---|
| 269 | LOG(3, "INFO: Matching old polygon " << _polygon
|
|---|
| 270 | << " with new polygon " << _newpolygon);
|
|---|
| 271 |
|
|---|
| 272 | if (_polygon.size() > 0) {
|
|---|
| 273 | MatchingControlStructure MCS;
|
|---|
| 274 | MCS.foundflag = false;
|
|---|
| 275 | MCS.bestL2 = std::numeric_limits<double>::max();
|
|---|
| 276 | MCS.oldreturnpolygon.insert(MCS.oldreturnpolygon.begin(), _polygon.begin(),_polygon.end() );
|
|---|
| 277 | MCS.newreturnpolygon.insert(MCS.newreturnpolygon.begin(), _newpolygon.begin(),_newpolygon.end() );
|
|---|
| 278 |
|
|---|
| 279 | // search for bestmatching combinatorially
|
|---|
| 280 | {
|
|---|
| 281 | // translate polygon into vector to enable index addressing
|
|---|
| 282 | IndexList_t indices(_newpolygon.size());
|
|---|
| 283 | std::generate(indices.begin(), indices.end(), UniqueNumber);
|
|---|
| 284 | IndexList_t matching;
|
|---|
| 285 |
|
|---|
| 286 | // walk through all matchings
|
|---|
| 287 | const unsigned int matchingsize = _polygon.size();
|
|---|
| 288 | ASSERT( matchingsize <= indices.size(),
|
|---|
| 289 | "SphericalPointDistribution::matchSphericalPointDistributions() - not enough new returnpolygon to choose for matching to old ones.");
|
|---|
| 290 | recurseMatchings(MCS, matching, indices, matchingsize);
|
|---|
| 291 | }
|
|---|
| 292 | LOG(3, "INFO: Best matching is " << MCS.bestmatching);
|
|---|
| 293 |
|
|---|
| 294 | // determine rotation angles to align the two point distributions with
|
|---|
| 295 | // respect to bestmatching
|
|---|
| 296 | std::vector<double> angles(3);
|
|---|
| 297 | Vector newCenter;
|
|---|
| 298 | {
|
|---|
| 299 | // calculate center of triangle/line/point consisting of first returnpolygon of matching
|
|---|
| 300 | Vector oldCenter;
|
|---|
| 301 | IndexList_t::const_iterator iter = MCS.bestmatching.begin();
|
|---|
| 302 | unsigned int i = 0;
|
|---|
| 303 | for (; (i<3) && (i<MCS.bestmatching.size()); ++i, ++iter) {
|
|---|
| 304 | oldCenter += remainingold[i];
|
|---|
| 305 | newCenter += remainingnew[*iter];
|
|---|
| 306 | }
|
|---|
| 307 | oldCenter *= 1./(double)i;
|
|---|
| 308 | newCenter *= 1./(double)i;
|
|---|
| 309 | LOG(3, "INFO: oldCenter is " << oldCenter << ", newCenter is " << newCenter);
|
|---|
| 310 |
|
|---|
| 311 | Vector direction(0.,0.,0.);
|
|---|
| 312 | for(size_t i=0;i<NDIM;++i) {
|
|---|
| 313 | // create new rotation axis
|
|---|
| 314 | direction[i] = 1.;
|
|---|
| 315 | const Line axis (zeroVec, direction);
|
|---|
| 316 | // calculate rotation angle for this axis
|
|---|
| 317 | const double alpha = direction.Angle(oldCenter) - direction.Angle(newCenter);
|
|---|
| 318 | // perform rotation
|
|---|
| 319 | axis.rotateVector(newCenter, alpha);
|
|---|
| 320 | // store angle
|
|---|
| 321 | angles[i] = alpha;
|
|---|
| 322 | // reset direction component for next iteration
|
|---|
| 323 | direction[i] = 0.;
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|
| 326 | LOG(3, "INFO: (x,y,z) angles are" << angles);
|
|---|
| 327 | const Line RotationAxis(zeroVec, newCenter);
|
|---|
| 328 | const double RotationAngle =
|
|---|
| 329 | newCenter.Angle(remainingold[0])
|
|---|
| 330 | - newCenter.Angle(remainingnew[*MCS.bestmatching.begin()]);
|
|---|
| 331 | LOG(3, "INFO: Rotate around self is " << RotationAngle
|
|---|
| 332 | << " around axis " << RotationAxis);
|
|---|
| 333 |
|
|---|
| 334 | // rotate _newpolygon
|
|---|
| 335 | SphericalPointDistribution::Polygon_t rotated_newpolygon =
|
|---|
| 336 | rotatePolygon(_newpolygon, angles, RotationAxis, RotationAngle);
|
|---|
| 337 | LOG(3, "INFO: Rotated new polygon is " << rotated_newpolygon);
|
|---|
| 338 |
|
|---|
| 339 | // remove all returnpolygon in matching and return remaining ones
|
|---|
| 340 | return removeMatchingPoints(rotated_newpolygon, MCS.bestmatching);
|
|---|
| 341 | } else
|
|---|
| 342 | return _newpolygon;
|
|---|
| 343 | }
|
|---|