| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2017 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 | * RotateAroundBondAction.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Mar 22, 2017 | 
|---|
| 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/RotateAroundBondAction.hpp" | 
|---|
| 38 |  | 
|---|
| 39 | #include "CodePatterns/Assert.hpp" | 
|---|
| 40 | #include "CodePatterns/Log.hpp" | 
|---|
| 41 |  | 
|---|
| 42 | #include "LinearAlgebra/Line.hpp" | 
|---|
| 43 | #include "LinearAlgebra/Plane.hpp" | 
|---|
| 44 |  | 
|---|
| 45 | #include "Actions/UndoRedoHelpers.hpp" | 
|---|
| 46 | #include "Graph/CyclicStructureAnalysis.hpp" | 
|---|
| 47 | #include "Graph/DepthFirstSearchAnalysis.hpp" | 
|---|
| 48 | #include "Graph/BoostGraphCreator.hpp" | 
|---|
| 49 | #include "Graph/BoostGraphHelpers.hpp" | 
|---|
| 50 | #include "Graph/BreadthFirstSearchGatherer.hpp" | 
|---|
| 51 |  | 
|---|
| 52 | #include "Atom/atom.hpp" | 
|---|
| 53 | #include "Atom/AtomicInfo.hpp" | 
|---|
| 54 | #include "Bond/bond.hpp" | 
|---|
| 55 | #include "molecule.hpp" | 
|---|
| 56 | #include "World.hpp" | 
|---|
| 57 | #include "WorldTime.hpp" | 
|---|
| 58 |  | 
|---|
| 59 | using namespace MoleCuilder; | 
|---|
| 60 |  | 
|---|
| 61 | // and construct the stuff | 
|---|
| 62 | #include "RotateAroundBondAction.def" | 
|---|
| 63 | #include "Action_impl_pre.hpp" | 
|---|
| 64 |  | 
|---|
| 65 | /** =========== define the function ====================== */ | 
|---|
| 66 | static bool IsBondContainedInCycle(const bond::ptr &_bond) | 
|---|
| 67 | { | 
|---|
| 68 | // get the BackEdgeStack from somewhere | 
|---|
| 69 | DepthFirstSearchAnalysis DFS; | 
|---|
| 70 | DFS(); | 
|---|
| 71 | DFS.CyclicBondAnalysis(); | 
|---|
| 72 | //  std::deque<bond::ptr> BackEdgeStack = DFS.getBackEdgeStack(); | 
|---|
| 73 | //  // then we analyse the cycles and get them | 
|---|
| 74 | //  CyclicStructureAnalysis CycleAnalysis(ExcludeHydrogen); // hydrogens never contained in cycles | 
|---|
| 75 | //  CycleAnalysis(&BackEdgeStack); | 
|---|
| 76 | return _bond->Cyclic; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | static bool addEdgePredicate( | 
|---|
| 80 | const bond &_bond, | 
|---|
| 81 | const std::vector<atomId_t> &_atomids) | 
|---|
| 82 | { | 
|---|
| 83 | ASSERT(_atomids.size() == (size_t)2, | 
|---|
| 84 | "addEdgePredicate() - atomids must contain exactly two ids."); | 
|---|
| 85 | // do not add selected edge | 
|---|
| 86 | return ((_bond.leftatom->getId() != _atomids[0]) | 
|---|
| 87 | || (_bond.rightatom->getId() != _atomids[1])); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | ActionState::ptr MoleculeRotateAroundBondAction::performCall() | 
|---|
| 91 | { | 
|---|
| 92 | // check preconditions | 
|---|
| 93 | const std::vector< atom *> atoms = World::getInstance().getSelectedAtoms(); | 
|---|
| 94 | if (atoms.size() != 2) { | 
|---|
| 95 | STATUS("Exactly two atoms must be selected to specify a bond."); | 
|---|
| 96 | return Action::failure; | 
|---|
| 97 | } | 
|---|
| 98 | if (!atoms[0]->IsBondedTo(WorldTime::getTime(), atoms[1])) { | 
|---|
| 99 | STATUS("Two given atoms are not bonded."); | 
|---|
| 100 | return Action::failure; | 
|---|
| 101 | } | 
|---|
| 102 | molecule *mol = World::getInstance(). | 
|---|
| 103 | getMolecule(MoleculeById(atoms[0]->getMolecule()->getId())); | 
|---|
| 104 | if (mol != atoms[1]->getMolecule()) { | 
|---|
| 105 | STATUS("The two selected atoms must belong to the same molecule."); | 
|---|
| 106 | return Action::failure; | 
|---|
| 107 | } | 
|---|
| 108 | if (IsBondContainedInCycle(atoms[0]->getBond(atoms[1]))) { | 
|---|
| 109 | STATUS("The given bond is contained in a cycle, cannot rotate!"); | 
|---|
| 110 | return Action::failure; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | // gather undo information: store position of all atoms of molecule | 
|---|
| 114 | std::vector<AtomicInfo> UndoInfo; | 
|---|
| 115 | UndoInfo.reserve(mol->size()); | 
|---|
| 116 | { | 
|---|
| 117 | for (molecule::const_iterator iter = const_cast<molecule const *>(mol)->begin(); | 
|---|
| 118 | iter != const_cast<molecule const *>(mol)->end(); | 
|---|
| 119 | ++iter) { | 
|---|
| 120 | const atom * const Walker = *iter; | 
|---|
| 121 | UndoInfo.push_back(AtomicInfo(*Walker)); | 
|---|
| 122 | } | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | // gather sorted ids | 
|---|
| 126 | std::vector<atomId_t> atomids(2); | 
|---|
| 127 | atomids[0] = atoms[0]->getId(); | 
|---|
| 128 | atomids[1] = atoms[1]->getId(); | 
|---|
| 129 | std::sort(atomids.begin(), atomids.end()); | 
|---|
| 130 | LOG(1, "DEBUG: Selected nodes are " << atomids); | 
|---|
| 131 |  | 
|---|
| 132 | // get nodes on either side of selected bond via BFS discovery | 
|---|
| 133 | BoostGraphCreator BGcreator; | 
|---|
| 134 | BGcreator.createFromMolecule(*mol, | 
|---|
| 135 | boost::bind(addEdgePredicate, _1, boost::ref(atomids))); | 
|---|
| 136 | BreadthFirstSearchGatherer NodeGatherer(BGcreator); | 
|---|
| 137 | BoostGraphHelpers::Nodeset_t bondside_sets = | 
|---|
| 138 | NodeGatherer(params.bondside.get() ? atoms[0]->getId() : atoms[1]->getId()); | 
|---|
| 139 | std::sort(bondside_sets.begin(), bondside_sets.end()); | 
|---|
| 140 |  | 
|---|
| 141 | // convert from degrees to radian | 
|---|
| 142 | const double angle_radian = params.angle.get() * M_PI/180.; | 
|---|
| 143 |  | 
|---|
| 144 | // create the bond plane and mid-distance | 
|---|
| 145 | Vector NormalVector = (atoms[0]->getPosition() - atoms[1]->getPosition()); | 
|---|
| 146 | NormalVector.Normalize(); | 
|---|
| 147 | const Vector OffsetVector = 0.5*(atoms[0]->getPosition() + atoms[1]->getPosition()); | 
|---|
| 148 | Plane bondplane(NormalVector, OffsetVector); | 
|---|
| 149 | Line RotationAxis(OffsetVector, NormalVector); | 
|---|
| 150 |  | 
|---|
| 151 | // go through the molecule and rotate each atom relative two plane | 
|---|
| 152 | for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) { | 
|---|
| 153 | const Vector &position = (*iter)->getPosition(); | 
|---|
| 154 | const double signed_distance = bondplane.SignedDistance(position); | 
|---|
| 155 | // for each atom determine in which set of nodes it is and shift accordingly | 
|---|
| 156 | const atomId_t &atomid = (*iter)->getId(); | 
|---|
| 157 | LOG(3, "DEBUG: Inspecting atom " << **iter << " at " << position); | 
|---|
| 158 | if (std::binary_search(bondside_sets.begin(), bondside_sets.end(), atomid)) { | 
|---|
| 159 | LOG(4, "DEBUG: Rotating atom " << **iter << " by " << angle_radian | 
|---|
| 160 | << " rad around " << RotationAxis); | 
|---|
| 161 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), angle_radian)); | 
|---|
| 162 | LOG(4, "DEBUG: New position of atom " << **iter); | 
|---|
| 163 | ASSERT( fabs(signed_distance - bondplane.SignedDistance(position)) < MYEPSILON, | 
|---|
| 164 | "MoleculeRotateAroundBondAction::performCall() - signed distance was " | 
|---|
| 165 | +toString(signed_distance)+" and is now "+toString(bondplane.SignedDistance(position))); | 
|---|
| 166 | } | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | MoleculeRotateAroundBondState *UndoState = | 
|---|
| 170 | new MoleculeRotateAroundBondState(UndoInfo, OffsetVector, NormalVector, mol, params); | 
|---|
| 171 | return ActionState::ptr(UndoState); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | ActionState::ptr MoleculeRotateAroundBondAction::performUndo(ActionState::ptr _state) { | 
|---|
| 175 | MoleculeRotateAroundBondState *state = assert_cast<MoleculeRotateAroundBondState*>(_state.get()); | 
|---|
| 176 |  | 
|---|
| 177 | // set stored old state | 
|---|
| 178 | SetAtomsFromAtomicInfo(state->UndoInfo); | 
|---|
| 179 |  | 
|---|
| 180 | return ActionState::ptr(_state); | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | ActionState::ptr MoleculeRotateAroundBondAction::performRedo(ActionState::ptr _state){ | 
|---|
| 184 | MoleculeRotateAroundBondState *state = assert_cast<MoleculeRotateAroundBondState*>(_state.get()); | 
|---|
| 185 |  | 
|---|
| 186 | // create the bond plane and mid-distance | 
|---|
| 187 | Plane bondplane(state->NormalVector, state->OffsetVector); | 
|---|
| 188 | Line RotationAxis(state->OffsetVector, state->NormalVector); | 
|---|
| 189 | const double angle_radian = params.angle.get() * M_PI/180.; | 
|---|
| 190 | // go through the molecule and rotate each atom relative two plane | 
|---|
| 191 | for (molecule::iterator iter = state->mol->begin(); iter != state->mol->end(); ++iter) { | 
|---|
| 192 | const Vector &position = (*iter)->getPosition(); | 
|---|
| 193 | if ((params.bondside.get() && (bondplane.SignedDistance(position) > 0)) || | 
|---|
| 194 | (!params.bondside.get() && (bondplane.SignedDistance(position) < 0))) { | 
|---|
| 195 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), angle_radian)); | 
|---|
| 196 | } | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | return ActionState::ptr(_state); | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | bool MoleculeRotateAroundBondAction::canUndo() { | 
|---|
| 203 | return true; | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | bool MoleculeRotateAroundBondAction::shouldUndo() { | 
|---|
| 207 | return true; | 
|---|
| 208 | } | 
|---|
| 209 | /** =========== end of function ====================== */ | 
|---|