| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [bcf653] | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [97ebf8] | 8 | /*
 | 
|---|
 | 9 |  * BoundInBoxAction.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: May 8, 2010
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 21 | 
 | 
|---|
| [e670e4] | 22 | #include <boost/shared_ptr.hpp>
 | 
|---|
 | 23 | 
 | 
|---|
| [ad011c] | 24 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [97ebf8] | 25 | #include "molecule.hpp"
 | 
|---|
 | 26 | #include "World.hpp"
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | #include <iostream>
 | 
|---|
 | 29 | #include <string>
 | 
|---|
 | 30 | #include <vector>
 | 
|---|
 | 31 | 
 | 
|---|
| [1fd675] | 32 | #include "Actions/WorldAction/BoundInBoxAction.hpp"
 | 
|---|
| [805636] | 33 | 
 | 
|---|
| [ce7fdc] | 34 | using namespace MoleCuilder;
 | 
|---|
 | 35 | 
 | 
|---|
| [1fd675] | 36 | // and construct the stuff
 | 
|---|
 | 37 | #include "BoundInBoxAction.def"
 | 
|---|
 | 38 | #include "Action_impl_pre.hpp"
 | 
|---|
 | 39 | /** =========== define the function ====================== */
 | 
|---|
| [805636] | 40 | Action::state_ptr WorldBoundInBoxAction::performCall() {
 | 
|---|
| [e670e4] | 41 |   // create undo state
 | 
|---|
 | 42 |   std::vector< boost::shared_ptr<Vector> > OldPositions;
 | 
|---|
 | 43 |   std::vector<molecule*> AllMolecules = World::getInstance().getAllMolecules();
 | 
|---|
 | 44 |   for (vector<molecule*>::iterator MolRunner = AllMolecules.begin();
 | 
|---|
 | 45 |       MolRunner != AllMolecules.end();
 | 
|---|
 | 46 |       ++MolRunner) {
 | 
|---|
 | 47 |     for(molecule::const_iterator AtomRunner = (*MolRunner)->begin();
 | 
|---|
 | 48 |         AtomRunner != (*MolRunner)->end();
 | 
|---|
 | 49 |         ++AtomRunner) {
 | 
|---|
 | 50 |       OldPositions.push_back(
 | 
|---|
 | 51 |           boost::shared_ptr<Vector>(new Vector(
 | 
|---|
 | 52 |               (*AtomRunner)->getPosition()
 | 
|---|
 | 53 |               ))
 | 
|---|
 | 54 |           );
 | 
|---|
 | 55 |     }
 | 
|---|
 | 56 |   }
 | 
|---|
 | 57 |   WorldBoundInBoxState *undoState = new WorldBoundInBoxState(OldPositions, params);
 | 
|---|
 | 58 | 
 | 
|---|
| [805636] | 59 |   // center
 | 
|---|
| [e670e4] | 60 |   for (std::vector<molecule*>::iterator MolRunner = AllMolecules.begin(); MolRunner != AllMolecules.end(); ++MolRunner) {
 | 
|---|
| [805636] | 61 |     (*MolRunner)->BoundInBox();
 | 
|---|
| [97ebf8] | 62 |   }
 | 
|---|
| [e670e4] | 63 |   return Action::state_ptr(undoState);
 | 
|---|
| [97ebf8] | 64 | }
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 | Action::state_ptr WorldBoundInBoxAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| [e670e4] | 67 |   WorldBoundInBoxState *state = assert_cast<WorldBoundInBoxState*>(_state.get());
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 |   // place atoms on old positions
 | 
|---|
 | 70 |   std::vector< boost::shared_ptr<Vector> >::const_iterator OldPositionsIter = state->OldPositions.begin();
 | 
|---|
 | 71 |   std::vector<molecule*> AllMolecules = World::getInstance().getAllMolecules();
 | 
|---|
 | 72 |   for (std::vector<molecule*>::iterator MolRunner = AllMolecules.begin();
 | 
|---|
 | 73 |       MolRunner != AllMolecules.end();
 | 
|---|
 | 74 |       ++MolRunner) {
 | 
|---|
| [59fff1] | 75 |     for(molecule::iterator AtomRunner = (*MolRunner)->begin();
 | 
|---|
| [e670e4] | 76 |         AtomRunner != (*MolRunner)->end();
 | 
|---|
 | 77 |         ++AtomRunner) {
 | 
|---|
 | 78 |       ASSERT(OldPositionsIter != state->OldPositions.end(),
 | 
|---|
 | 79 |           "WorldBoundInBoxAction::performUndo() - too few positions stored in UndoState.");
 | 
|---|
 | 80 |       (*AtomRunner)->setPosition(**(OldPositionsIter++));
 | 
|---|
 | 81 |     }
 | 
|---|
 | 82 |   }
 | 
|---|
| [97ebf8] | 83 | 
 | 
|---|
| [e670e4] | 84 |   return Action::state_ptr(_state);
 | 
|---|
| [97ebf8] | 85 | }
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 | Action::state_ptr WorldBoundInBoxAction::performRedo(Action::state_ptr _state){
 | 
|---|
| [e670e4] | 88 | //  WorldBoundInBoxState *state = assert_cast<WorldBoundInBoxState*>(_state.get());
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 |   // center
 | 
|---|
 | 91 |   std::vector<molecule*> AllMolecules = World::getInstance().getAllMolecules();
 | 
|---|
 | 92 |   for (std::vector<molecule*>::iterator MolRunner = AllMolecules.begin();
 | 
|---|
 | 93 |       MolRunner != AllMolecules.end();
 | 
|---|
 | 94 |       ++MolRunner) {
 | 
|---|
 | 95 |     (*MolRunner)->BoundInBox();
 | 
|---|
 | 96 |   }
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 |   return Action::state_ptr(_state);
 | 
|---|
| [97ebf8] | 99 | }
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 | bool WorldBoundInBoxAction::canUndo() {
 | 
|---|
| [e670e4] | 102 |   return true;
 | 
|---|
| [97ebf8] | 103 | }
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 | bool WorldBoundInBoxAction::shouldUndo() {
 | 
|---|
| [e670e4] | 106 |   return true;
 | 
|---|
| [97ebf8] | 107 | }
 | 
|---|
| [1fd675] | 108 | /** =========== end of function ====================== */
 | 
|---|