| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2014 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 | * MirrorAction.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Sep 10, 2014 | 
|---|
| 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 "CodePatterns/Log.hpp" | 
|---|
| 38 | #include "Atom/atom.hpp" | 
|---|
| 39 | #include "LinearAlgebra/Plane.hpp" | 
|---|
| 40 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 41 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 42 | #include "World.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #include <iostream> | 
|---|
| 45 | #include <fstream> | 
|---|
| 46 | #include <string> | 
|---|
| 47 |  | 
|---|
| 48 | #include "Actions/AtomAction/MirrorAction.hpp" | 
|---|
| 49 |  | 
|---|
| 50 | using namespace MoleCuilder; | 
|---|
| 51 |  | 
|---|
| 52 | // and construct the stuff | 
|---|
| 53 | #include "MirrorAction.def" | 
|---|
| 54 | #include "Action_impl_pre.hpp" | 
|---|
| 55 | /** =========== define the function ====================== */ | 
|---|
| 56 | ActionState::ptr AtomMirrorAction::performCall() { | 
|---|
| 57 | Box &domain = World::getInstance().getDomain(); | 
|---|
| 58 | std::vector<atom *> selectedAtoms = World::getInstance().getSelectedAtoms(); | 
|---|
| 59 |  | 
|---|
| 60 | Plane *MirrorPlane = NULL; | 
|---|
| 61 | std::vector<atom *>::iterator iter = selectedAtoms.begin(); | 
|---|
| 62 | try { | 
|---|
| 63 | MirrorPlane = new Plane(params.normal.get(), params.offset.get()); | 
|---|
| 64 | for (; iter != selectedAtoms.end(); ++iter) { | 
|---|
| 65 | (*iter)->setPosition(MirrorPlane->mirrorVector((*iter)->getPosition())); | 
|---|
| 66 | if (params.periodic.get()) | 
|---|
| 67 | (*iter)->setPosition(domain.enforceBoundaryConditions((*iter)->getPosition())); | 
|---|
| 68 | } | 
|---|
| 69 | } catch( LinearAlgebraException &e) { | 
|---|
| 70 | // flip back | 
|---|
| 71 | for (std::vector<atom *>::iterator undoiter = selectedAtoms.begin(); | 
|---|
| 72 | undoiter != iter; ++undoiter) | 
|---|
| 73 | (*iter)->setPosition(MirrorPlane->mirrorVector((*iter)->getPosition())); | 
|---|
| 74 | delete MirrorPlane; | 
|---|
| 75 | return Action::failure; | 
|---|
| 76 | } | 
|---|
| 77 | delete MirrorPlane; | 
|---|
| 78 |  | 
|---|
| 79 | return ActionState::ptr(new AtomMirrorState(selectedAtoms, params)); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | ActionState::ptr AtomMirrorAction::performUndo(ActionState::ptr _state) { | 
|---|
| 83 | AtomMirrorState *state = assert_cast<AtomMirrorState*>(_state.get()); | 
|---|
| 84 | Box &domain = World::getInstance().getDomain(); | 
|---|
| 85 |  | 
|---|
| 86 | Plane MirrorPlane(state->params.normal.get(), state->params.offset.get()); | 
|---|
| 87 | for (std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter) { | 
|---|
| 88 | (*iter)->setPosition(MirrorPlane.mirrorVector((*iter)->getPosition())); | 
|---|
| 89 | if (state->params.periodic.get()) | 
|---|
| 90 | (*iter)->setPosition(domain.enforceBoundaryConditions((*iter)->getPosition())); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | return ActionState::ptr(_state); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | ActionState::ptr AtomMirrorAction::performRedo(ActionState::ptr _state){ | 
|---|
| 97 | AtomMirrorState *state = assert_cast<AtomMirrorState*>(_state.get()); | 
|---|
| 98 | Box &domain = World::getInstance().getDomain(); | 
|---|
| 99 |  | 
|---|
| 100 | Plane MirrorPlane(state->params.normal.get(), state->params.offset.get()); | 
|---|
| 101 | for (std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter) { | 
|---|
| 102 | (*iter)->setPosition(MirrorPlane.mirrorVector((*iter)->getPosition())); | 
|---|
| 103 | if (state->params.periodic.get()) | 
|---|
| 104 | (*iter)->setPosition(domain.enforceBoundaryConditions((*iter)->getPosition())); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | return ActionState::ptr(_state); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | bool AtomMirrorAction::canUndo() { | 
|---|
| 111 | return true; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | bool AtomMirrorAction::shouldUndo() { | 
|---|
| 115 | return true; | 
|---|
| 116 | } | 
|---|
| 117 | /** =========== end of function ====================== */ | 
|---|