[8b886f] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2012 University of Bonn. 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 | * ChangeBondAngleAction.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: Nov 14, 2012
|
---|
| 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 "Actions/MoleculeAction/ChangeBondAngleAction.hpp"
|
---|
| 38 |
|
---|
| 39 | #include <boost/assign.hpp>
|
---|
| 40 |
|
---|
| 41 | #include "CodePatterns/Log.hpp"
|
---|
| 42 | #include "CodePatterns/Verbose.hpp"
|
---|
| 43 |
|
---|
| 44 | #include "LinearAlgebra/Plane.hpp"
|
---|
| 45 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 46 |
|
---|
| 47 | #include "Atom/atom.hpp"
|
---|
| 48 | #include "Bond/bond.hpp"
|
---|
| 49 | #include "CodePatterns/Log.hpp"
|
---|
| 50 | #include "CodePatterns/Verbose.hpp"
|
---|
| 51 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
| 52 | #include "molecule.hpp"
|
---|
| 53 | #include "World.hpp"
|
---|
| 54 | #include "WorldTime.hpp"
|
---|
| 55 |
|
---|
| 56 | using namespace boost::assign;
|
---|
| 57 | using namespace MoleCuilder;
|
---|
| 58 |
|
---|
| 59 | // and construct the stuff
|
---|
| 60 | #include "ChangeBondAngleAction.def"
|
---|
| 61 | #include "Action_impl_pre.hpp"
|
---|
| 62 | /** =========== define the function ====================== */
|
---|
| 63 |
|
---|
| 64 | const std::vector<size_t> sortIndicesFromCentralAtom(const std::vector< atom *> &atoms)
|
---|
| 65 | {
|
---|
| 66 | std::vector<size_t> indices;
|
---|
| 67 | {
|
---|
| 68 | std::vector<size_t> matches;
|
---|
| 69 | size_t index = 0;
|
---|
| 70 | for (; index < 3; ++index)
|
---|
| 71 | if ((atoms[index]->IsBondedTo(WorldTime::getTime(), atoms[(index+1)%3]))
|
---|
| 72 | && (atoms[index]->IsBondedTo(WorldTime::getTime(), atoms[(index+2)%3])))
|
---|
| 73 | matches.push_back(index);
|
---|
| 74 | if (matches.size() != 1) {
|
---|
| 75 | ELOG(1, "The three atoms are not linearly bonded.");
|
---|
| 76 | } else {
|
---|
| 77 | LOG(1, "INFO: Picking " << *atoms[ matches[0] ] << " as the central atom.");
|
---|
| 78 | indices += matches[0], (matches[0]+1)%3, (matches[0]+2)%3;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | return indices;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void ChangeBondAngleSymmetrically(
|
---|
| 85 | const std::vector<size_t> &indices,
|
---|
| 86 | const double newangle,
|
---|
| 87 | const std::vector<atom*> &atoms)
|
---|
| 88 | {
|
---|
| 89 | const Vector firstPosition = atoms[ indices[1] ]->getPosition();
|
---|
| 90 | const Vector secondPosition = atoms[ indices[2] ]->getPosition();
|
---|
| 91 |
|
---|
| 92 | // create the bond plane and mid-distance
|
---|
| 93 | const double normaldistance = firstPosition.distance(secondPosition);
|
---|
| 94 | const Vector NormalVector = (1. / normaldistance) * (firstPosition - secondPosition);
|
---|
| 95 | const Vector OffsetVector = atoms[indices[0]]->getPosition();
|
---|
| 96 | Plane midplane(NormalVector, OffsetVector);
|
---|
| 97 | // go through the molecule and stretch each atom relative two plane
|
---|
| 98 | for (size_t index = 1; index < 3; ++index) {
|
---|
| 99 | const Vector &position = atoms[indices[index]]->getPosition();
|
---|
| 100 | const Vector planespot = midplane.getClosestPoint(position);
|
---|
| 101 | const double opposite_side = position.distance(planespot);
|
---|
| 102 | const double adjacent_side = OffsetVector.distance(planespot);
|
---|
| 103 | const double hypotenuse = position.distance(OffsetVector);
|
---|
| 104 | if (fabs(hypotenuse) > MYEPSILON) {
|
---|
| 105 | const double angle = acos(adjacent_side / hypotenuse);
|
---|
| 106 | LOG(1, "INFO: Old angle is " << (180. / M_PI) * (2. * angle) << ".");
|
---|
| 107 | const double new_opposite_side = hypotenuse * sin(newangle);
|
---|
| 108 | const double new_adjacent_side = hypotenuse * cos(newangle);
|
---|
| 109 | Vector newposition = (new_opposite_side / opposite_side) * (position - planespot);
|
---|
| 110 | newposition += (new_adjacent_side / adjacent_side) * (planespot - OffsetVector);
|
---|
| 111 | newposition += OffsetVector;
|
---|
| 112 | atoms[indices[index]]->setPosition(newposition);
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | Action::state_ptr MoleculeChangeBondAngleAction::performCall()
|
---|
| 118 | {
|
---|
| 119 | // check precondition: three atoms
|
---|
| 120 | const std::vector< atom *> atoms = World::getInstance().getSelectedAtoms();
|
---|
| 121 | if (atoms.size() != 3) {
|
---|
| 122 | ELOG(1, "Exactly three atoms must be selected.");
|
---|
| 123 | return Action::failure;
|
---|
| 124 | }
|
---|
| 125 | // check precondition: linearly bonded atoms
|
---|
| 126 | const std::vector<size_t> indices = sortIndicesFromCentralAtom(atoms);
|
---|
| 127 | if (indices.size() != 3)
|
---|
| 128 | return Action::failure;
|
---|
| 129 | const molecule *mol = atoms[0]->getMolecule();
|
---|
| 130 | if ((mol != atoms[1]->getMolecule()) || (mol != atoms[2]->getMolecule())) {
|
---|
| 131 | ELOG(1, "The two selected atoms must belong to the same molecule.");
|
---|
| 132 | return Action::failure;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | // gather undo info
|
---|
| 136 | const Vector firstPosition = atoms[ indices[1] ]->getPosition();
|
---|
| 137 | const Vector secondPosition = atoms[ indices[2] ]->getPosition();
|
---|
| 138 |
|
---|
| 139 | // change the bond angle
|
---|
| 140 | const double newangle = .5* (M_PI / 180.) * params.bondangle.get();
|
---|
| 141 | LOG(1, "INFO: New angle is " << (180. / M_PI) * (2. * newangle) << ".");
|
---|
| 142 | ChangeBondAngleSymmetrically(indices, newangle, atoms);
|
---|
| 143 |
|
---|
| 144 | // create undo information
|
---|
| 145 | MoleculeChangeBondAngleState *UndoState = new MoleculeChangeBondAngleState(
|
---|
| 146 | indices[1], indices[2],
|
---|
| 147 | firstPosition, secondPosition,
|
---|
| 148 | atoms[ indices[1] ]->getPosition(), atoms[ indices[2] ]->getPosition(),
|
---|
| 149 | params);
|
---|
| 150 | return Action::state_ptr(UndoState);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | Action::state_ptr MoleculeChangeBondAngleAction::performUndo(Action::state_ptr _state) {
|
---|
| 154 | MoleculeChangeBondAngleState *state = assert_cast<MoleculeChangeBondAngleState*>(_state.get());
|
---|
| 155 |
|
---|
| 156 | // undo both position changes
|
---|
| 157 | atom * const first = World::getInstance().getAtom(AtomById(state->firstId));
|
---|
| 158 | atom * const second = World::getInstance().getAtom(AtomById(state->secondId));
|
---|
| 159 | first->setPosition(state->firstOldPosition);
|
---|
| 160 | second->setPosition(state->secondOldPosition);
|
---|
| 161 |
|
---|
| 162 | return Action::state_ptr(_state);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | Action::state_ptr MoleculeChangeBondAngleAction::performRedo(Action::state_ptr _state){
|
---|
| 166 | MoleculeChangeBondAngleState *state = assert_cast<MoleculeChangeBondAngleState*>(_state.get());
|
---|
| 167 |
|
---|
| 168 | // redo both position changes
|
---|
| 169 | atom * const first = World::getInstance().getAtom(AtomById(state->firstId));
|
---|
| 170 | atom * const second = World::getInstance().getAtom(AtomById(state->secondId));
|
---|
| 171 | first->setPosition(state->firstNewPosition);
|
---|
| 172 | second->setPosition(state->secondNewPosition);
|
---|
| 173 |
|
---|
| 174 | return Action::state_ptr(_state);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | bool MoleculeChangeBondAngleAction::canUndo() {
|
---|
| 178 | return true;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | bool MoleculeChangeBondAngleAction::shouldUndo() {
|
---|
| 182 | return true;
|
---|
| 183 | }
|
---|
| 184 | /** =========== end of function ====================== */
|
---|