| [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"
|
|---|
| [d2be22] | 49 | #include "Graph/BondGraph.hpp"
|
|---|
| [bccbe9] | 50 | #include "Graph/BoostGraphHelpers.hpp"
|
|---|
| [3a51bd] | 51 | #include "World.hpp"
|
|---|
| 52 |
|
|---|
| 53 | using namespace MoleCuilder;
|
|---|
| 54 |
|
|---|
| 55 | // and construct the stuff
|
|---|
| 56 | #include "StretchBondAction.def"
|
|---|
| 57 | #include "Action_impl_pre.hpp"
|
|---|
| [3b74fa] | 58 |
|
|---|
| 59 |
|
|---|
| [3a51bd] | 60 | /** =========== define the function ====================== */
|
|---|
| [b5b01e] | 61 | ActionState::ptr MoleculeStretchBondAction::performCall()
|
|---|
| [3a51bd] | 62 | {
|
|---|
| [d2be22] | 63 | BondGraph *BG = World::getInstance().getBondGraph();
|
|---|
| 64 | if (!BG->IsBondLengthTableLoaded()) {
|
|---|
| 65 | STATUS("BondLength table has not been loaded.");
|
|---|
| 66 | return Action::failure;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| [3a51bd] | 69 | const std::vector< atom *> atoms = World::getInstance().getSelectedAtoms();
|
|---|
| [9f55b9] | 70 | StretchBondUtil stretcher(atoms);
|
|---|
| [38bcbe] | 71 |
|
|---|
| 72 | const double bonddistance = params.bonddistance.get();
|
|---|
| 73 | bool status = stretcher(bonddistance);
|
|---|
| [9f55b9] | 74 |
|
|---|
| 75 | if (status) {
|
|---|
| 76 | MoleculeStretchBondState *UndoState =
|
|---|
| 77 | new MoleculeStretchBondState(
|
|---|
| 78 | stretcher.getShift(),
|
|---|
| 79 | stretcher.getBondSides(),
|
|---|
| 80 | &stretcher.getMolecule(),
|
|---|
| 81 | params);
|
|---|
| 82 | return ActionState::ptr(UndoState);
|
|---|
| 83 | } else {
|
|---|
| 84 | STATUS("Failed, exactly two atoms must be selected.");
|
|---|
| [3a51bd] | 85 | return Action::failure;
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| [b5b01e] | 89 | ActionState::ptr MoleculeStretchBondAction::performUndo(ActionState::ptr _state) {
|
|---|
| [3a51bd] | 90 | MoleculeStretchBondState *state = assert_cast<MoleculeStretchBondState*>(_state.get());
|
|---|
| 91 |
|
|---|
| 92 | // use given plane to undo
|
|---|
| 93 | Box &domain = World::getInstance().getDomain();
|
|---|
| [3b74fa] | 94 | for (size_t i=0;i<2;++i) {
|
|---|
| [bccbe9] | 95 | for (BoostGraphHelpers::Nodeset_t::const_iterator iter = state->bondside_sets[i].begin();
|
|---|
| [3b74fa] | 96 | iter != state->bondside_sets[i].end(); ++iter) {
|
|---|
| 97 | atom &walker = *World::getInstance().getAtom(AtomById(*iter));
|
|---|
| 98 | const Vector &position = walker.getPosition();
|
|---|
| 99 | walker.setPosition( domain.enforceBoundaryConditions(position-state->Shift[i]) );
|
|---|
| [3a51bd] | 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| [b5b01e] | 103 | return ActionState::ptr(_state);
|
|---|
| [3a51bd] | 104 | }
|
|---|
| 105 |
|
|---|
| [b5b01e] | 106 | ActionState::ptr MoleculeStretchBondAction::performRedo(ActionState::ptr _state){
|
|---|
| [3a51bd] | 107 | MoleculeStretchBondState *state = assert_cast<MoleculeStretchBondState*>(_state.get());
|
|---|
| 108 |
|
|---|
| 109 | Box &domain = World::getInstance().getDomain();
|
|---|
| [3b74fa] | 110 | for (size_t i=0;i<2;++i) {
|
|---|
| [bccbe9] | 111 | for (BoostGraphHelpers::Nodeset_t::const_iterator iter = state->bondside_sets[i].begin();
|
|---|
| [3b74fa] | 112 | iter != state->bondside_sets[i].end(); ++iter) {
|
|---|
| 113 | atom &walker = *World::getInstance().getAtom(AtomById(*iter));
|
|---|
| 114 | const Vector &position = walker.getPosition();
|
|---|
| 115 | walker.setPosition( domain.enforceBoundaryConditions(position+state->Shift[i]) );
|
|---|
| [3a51bd] | 116 | }
|
|---|
| 117 | }
|
|---|
| [3b74fa] | 118 |
|
|---|
| [b5b01e] | 119 | return ActionState::ptr(_state);
|
|---|
| [3a51bd] | 120 | }
|
|---|
| 121 |
|
|---|
| 122 | bool MoleculeStretchBondAction::canUndo() {
|
|---|
| 123 | return true;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | bool MoleculeStretchBondAction::shouldUndo() {
|
|---|
| 127 | return true;
|
|---|
| 128 | }
|
|---|
| 129 | /** =========== end of function ====================== */
|
|---|