| 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 | 
 | 
|---|
| 35 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include "Actions/MoleculeAction/StretchBondAction.hpp"
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #include <boost/bind.hpp>
 | 
|---|
| 40 | 
 | 
|---|
| 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"
 | 
|---|
| 48 | #include "Bond/StretchBond.hpp"
 | 
|---|
| 49 | #include "Graph/BoostGraphHelpers.hpp"
 | 
|---|
| 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"
 | 
|---|
| 57 | 
 | 
|---|
| 58 | 
 | 
|---|
| 59 | /** =========== define the function ====================== */
 | 
|---|
| 60 | ActionState::ptr MoleculeStretchBondAction::performCall()
 | 
|---|
| 61 | {
 | 
|---|
| 62 |   const std::vector< atom *> atoms = World::getInstance().getSelectedAtoms();
 | 
|---|
| 63 |   StretchBondUtil stretcher(atoms);
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   const double bonddistance = params.bonddistance.get();
 | 
|---|
| 66 |   bool status = stretcher(bonddistance);
 | 
|---|
| 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.");
 | 
|---|
| 78 |     return Action::failure;
 | 
|---|
| 79 |   }
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | ActionState::ptr MoleculeStretchBondAction::performUndo(ActionState::ptr _state) {
 | 
|---|
| 83 |   MoleculeStretchBondState *state = assert_cast<MoleculeStretchBondState*>(_state.get());
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   // use given plane to undo
 | 
|---|
| 86 |   Box &domain = World::getInstance().getDomain();
 | 
|---|
| 87 |   for (size_t i=0;i<2;++i) {
 | 
|---|
| 88 |     for (BoostGraphHelpers::Nodeset_t::const_iterator iter = state->bondside_sets[i].begin();
 | 
|---|
| 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]) );
 | 
|---|
| 93 |     }
 | 
|---|
| 94 |   }
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   return ActionState::ptr(_state);
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | ActionState::ptr MoleculeStretchBondAction::performRedo(ActionState::ptr _state){
 | 
|---|
| 100 |   MoleculeStretchBondState *state = assert_cast<MoleculeStretchBondState*>(_state.get());
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   Box &domain = World::getInstance().getDomain();
 | 
|---|
| 103 |   for (size_t i=0;i<2;++i) {
 | 
|---|
| 104 |     for (BoostGraphHelpers::Nodeset_t::const_iterator iter = state->bondside_sets[i].begin();
 | 
|---|
| 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]) );
 | 
|---|
| 109 |     }
 | 
|---|
| 110 |   }
 | 
|---|
| 111 | 
 | 
|---|
| 112 |   return ActionState::ptr(_state);
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | bool MoleculeStretchBondAction::canUndo() {
 | 
|---|
| 116 |   return true;
 | 
|---|
| 117 | }
 | 
|---|
| 118 | 
 | 
|---|
| 119 | bool MoleculeStretchBondAction::shouldUndo() {
 | 
|---|
| 120 |   return true;
 | 
|---|
| 121 | }
 | 
|---|
| 122 | /** =========== end of function ====================== */
 | 
|---|