/*
* Project: MoleCuilder
* Description: creates and alters molecular systems
* Copyright (C) 2010-2012 University of Bonn. All rights reserved.
*
*
* This file is part of MoleCuilder.
*
* MoleCuilder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* MoleCuilder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MoleCuilder. If not, see .
*/
/*
* BoundInBoxAction.cpp
*
* Created on: May 8, 2010
* Author: heber
*/
// include config.h
#ifdef HAVE_CONFIG_H
#include
#endif
#include "CodePatterns/MemDebug.hpp"
#include
#include "CodePatterns/Log.hpp"
#include "molecule.hpp"
#include "World.hpp"
#include
#include
#include
#include "Actions/WorldAction/BoundInBoxAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "BoundInBoxAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
Action::state_ptr WorldBoundInBoxAction::performCall() {
// create undo state
std::vector< boost::shared_ptr > OldPositions;
std::vector AllMolecules = World::getInstance().getAllMolecules();
for (vector::iterator MolRunner = AllMolecules.begin();
MolRunner != AllMolecules.end();
++MolRunner) {
for(molecule::const_iterator AtomRunner = (*MolRunner)->begin();
AtomRunner != (*MolRunner)->end();
++AtomRunner) {
OldPositions.push_back(
boost::shared_ptr(new Vector(
(*AtomRunner)->getPosition()
))
);
}
}
WorldBoundInBoxState *undoState = new WorldBoundInBoxState(OldPositions, params);
// center
for (std::vector::iterator MolRunner = AllMolecules.begin(); MolRunner != AllMolecules.end(); ++MolRunner) {
(*MolRunner)->BoundInBox();
}
return Action::state_ptr(undoState);
}
Action::state_ptr WorldBoundInBoxAction::performUndo(Action::state_ptr _state) {
WorldBoundInBoxState *state = assert_cast(_state.get());
// place atoms on old positions
std::vector< boost::shared_ptr >::const_iterator OldPositionsIter = state->OldPositions.begin();
std::vector AllMolecules = World::getInstance().getAllMolecules();
for (std::vector::iterator MolRunner = AllMolecules.begin();
MolRunner != AllMolecules.end();
++MolRunner) {
for(molecule::iterator AtomRunner = (*MolRunner)->begin();
AtomRunner != (*MolRunner)->end();
++AtomRunner) {
ASSERT(OldPositionsIter != state->OldPositions.end(),
"WorldBoundInBoxAction::performUndo() - too few positions stored in UndoState.");
(*AtomRunner)->setPosition(**(OldPositionsIter++));
}
}
return Action::state_ptr(_state);
}
Action::state_ptr WorldBoundInBoxAction::performRedo(Action::state_ptr _state){
// WorldBoundInBoxState *state = assert_cast(_state.get());
// center
std::vector AllMolecules = World::getInstance().getAllMolecules();
for (std::vector::iterator MolRunner = AllMolecules.begin();
MolRunner != AllMolecules.end();
++MolRunner) {
(*MolRunner)->BoundInBox();
}
return Action::state_ptr(_state);
}
bool WorldBoundInBoxAction::canUndo() {
return true;
}
bool WorldBoundInBoxAction::shouldUndo() {
return true;
}
/** =========== end of function ====================== */