| [3a51bd] | 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 | * StretchBondAction.cpp | 
|---|
|  | 25 | * | 
|---|
|  | 26 | *  Created on: Sep 26, 2012 | 
|---|
|  | 27 | *      Author: heber | 
|---|
|  | 28 | */ | 
|---|
|  | 29 |  | 
|---|
|  | 30 | // include config.h | 
|---|
|  | 31 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 32 | #include <config.h> | 
|---|
|  | 33 | #endif | 
|---|
|  | 34 |  | 
|---|
| [9eb71b3] | 35 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| [3a51bd] | 36 |  | 
|---|
|  | 37 | #include "Actions/MoleculeAction/StretchBondAction.hpp" | 
|---|
|  | 38 |  | 
|---|
| [d24ef58] | 39 | #include <boost/bind.hpp> | 
|---|
|  | 40 |  | 
|---|
| [3a51bd] | 41 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 42 | #include "CodePatterns/Verbose.hpp" | 
|---|
|  | 43 |  | 
|---|
|  | 44 | #include "LinearAlgebra/Plane.hpp" | 
|---|
|  | 45 |  | 
|---|
|  | 46 | #include "Atom/atom.hpp" | 
|---|
|  | 47 | #include "Bond/bond.hpp" | 
|---|
| [9f55b9] | 48 | #include "Bond/StretchBond.hpp" | 
|---|
| [bccbe9] | 49 | #include "Graph/BoostGraphHelpers.hpp" | 
|---|
| [3a51bd] | 50 | #include "World.hpp" | 
|---|
|  | 51 |  | 
|---|
|  | 52 | using namespace MoleCuilder; | 
|---|
|  | 53 |  | 
|---|
|  | 54 | // and construct the stuff | 
|---|
|  | 55 | #include "StretchBondAction.def" | 
|---|
|  | 56 | #include "Action_impl_pre.hpp" | 
|---|
| [3b74fa] | 57 |  | 
|---|
|  | 58 |  | 
|---|
| [3a51bd] | 59 | /** =========== define the function ====================== */ | 
|---|
| [b5b01e] | 60 | ActionState::ptr MoleculeStretchBondAction::performCall() | 
|---|
| [3a51bd] | 61 | { | 
|---|
|  | 62 | const std::vector< atom *> atoms = World::getInstance().getSelectedAtoms(); | 
|---|
| [9f55b9] | 63 | StretchBondUtil stretcher(atoms); | 
|---|
| [38bcbe] | 64 |  | 
|---|
|  | 65 | const double bonddistance = params.bonddistance.get(); | 
|---|
|  | 66 | bool status = stretcher(bonddistance); | 
|---|
| [9f55b9] | 67 |  | 
|---|
|  | 68 | if (status) { | 
|---|
|  | 69 | MoleculeStretchBondState *UndoState = | 
|---|
|  | 70 | new MoleculeStretchBondState( | 
|---|
|  | 71 | stretcher.getShift(), | 
|---|
|  | 72 | stretcher.getBondSides(), | 
|---|
|  | 73 | &stretcher.getMolecule(), | 
|---|
|  | 74 | params); | 
|---|
|  | 75 | return ActionState::ptr(UndoState); | 
|---|
|  | 76 | } else { | 
|---|
|  | 77 | STATUS("Failed, exactly two atoms must be selected."); | 
|---|
| [3a51bd] | 78 | return Action::failure; | 
|---|
|  | 79 | } | 
|---|
|  | 80 | } | 
|---|
|  | 81 |  | 
|---|
| [b5b01e] | 82 | ActionState::ptr MoleculeStretchBondAction::performUndo(ActionState::ptr _state) { | 
|---|
| [3a51bd] | 83 | MoleculeStretchBondState *state = assert_cast<MoleculeStretchBondState*>(_state.get()); | 
|---|
|  | 84 |  | 
|---|
|  | 85 | // use given plane to undo | 
|---|
|  | 86 | Box &domain = World::getInstance().getDomain(); | 
|---|
| [3b74fa] | 87 | for (size_t i=0;i<2;++i) { | 
|---|
| [bccbe9] | 88 | for (BoostGraphHelpers::Nodeset_t::const_iterator iter = state->bondside_sets[i].begin(); | 
|---|
| [3b74fa] | 89 | iter != state->bondside_sets[i].end(); ++iter) { | 
|---|
|  | 90 | atom &walker = *World::getInstance().getAtom(AtomById(*iter)); | 
|---|
|  | 91 | const Vector &position = walker.getPosition(); | 
|---|
|  | 92 | walker.setPosition( domain.enforceBoundaryConditions(position-state->Shift[i]) ); | 
|---|
| [3a51bd] | 93 | } | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
| [b5b01e] | 96 | return ActionState::ptr(_state); | 
|---|
| [3a51bd] | 97 | } | 
|---|
|  | 98 |  | 
|---|
| [b5b01e] | 99 | ActionState::ptr MoleculeStretchBondAction::performRedo(ActionState::ptr _state){ | 
|---|
| [3a51bd] | 100 | MoleculeStretchBondState *state = assert_cast<MoleculeStretchBondState*>(_state.get()); | 
|---|
|  | 101 |  | 
|---|
|  | 102 | Box &domain = World::getInstance().getDomain(); | 
|---|
| [3b74fa] | 103 | for (size_t i=0;i<2;++i) { | 
|---|
| [bccbe9] | 104 | for (BoostGraphHelpers::Nodeset_t::const_iterator iter = state->bondside_sets[i].begin(); | 
|---|
| [3b74fa] | 105 | iter != state->bondside_sets[i].end(); ++iter) { | 
|---|
|  | 106 | atom &walker = *World::getInstance().getAtom(AtomById(*iter)); | 
|---|
|  | 107 | const Vector &position = walker.getPosition(); | 
|---|
|  | 108 | walker.setPosition( domain.enforceBoundaryConditions(position+state->Shift[i]) ); | 
|---|
| [3a51bd] | 109 | } | 
|---|
|  | 110 | } | 
|---|
| [3b74fa] | 111 |  | 
|---|
| [b5b01e] | 112 | return ActionState::ptr(_state); | 
|---|
| [3a51bd] | 113 | } | 
|---|
|  | 114 |  | 
|---|
|  | 115 | bool MoleculeStretchBondAction::canUndo() { | 
|---|
|  | 116 | return true; | 
|---|
|  | 117 | } | 
|---|
|  | 118 |  | 
|---|
|  | 119 | bool MoleculeStretchBondAction::shouldUndo() { | 
|---|
|  | 120 | return true; | 
|---|
|  | 121 | } | 
|---|
|  | 122 | /** =========== end of function ====================== */ | 
|---|