/*
* Project: MoleCuilder
* Description: creates and alters molecular systems
* Copyright (C) 2015 Frederik Heber. 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 .
*/
/*
* SaturateAction.cpp
*
* Created on: Jan 26, 2015
* Author: heber
*/
// include config.h
#ifdef HAVE_CONFIG_H
#include
#endif
#include "CodePatterns/MemDebug.hpp"
#include "CodePatterns/Log.hpp"
#include "Actions/AtomAction/SaturateAction.hpp"
#include "Actions/UndoRedoHelpers.hpp"
#include "Atom/atom.hpp"
#include "Atom/AtomicInfo.hpp"
#include "Element/element.hpp"
#include "Element/periodentafel.hpp"
#include "Fragmentation/Exporters/SphericalPointDistribution.hpp"
#include "molecule.hpp"
#include "World.hpp"
#include "WorldTime.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "SaturateAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr AtomSaturateAction::performCall() {
// get hydrogen element
const element * const hydrogen = World::getInstance().getPeriode()->FindElement(1);
// go through each atom
std::vector addedHydrogens;
World &world = World::getInstance();
for (World::AtomSelectionIterator iter = world.beginAtomSelection();
iter != world.endAtomSelection(); ++iter) {
atom * const _atom = iter->second;
// if atom is attached to molecule, add hydrogens there, too
molecule * mol = NULL;
if (_atom->getMolecule() == NULL) {
mol = World::getInstance().createMolecule();
mol->AddAtom(_atom);
} else {
mol = World::getInstance().getMolecule(MoleculeById(_atom->getMolecule()->getId()));
}
ASSERT( mol != NULL,
"ChangeElementAction::performCall() - mol is still NULL." );
// add the hydrogens
const Vector AtomsPosition = _atom->getPosition();
double typical_distance = _atom->getType()->getHBondDistance(1);
if (typical_distance == -1.)
typical_distance = 1.;
SphericalPointDistribution PointSphere(typical_distance);
PointSphere.initSelf(_atom->getType()->getNoValenceOrbitals());
for (SphericalPointDistribution::Polygon_t::const_iterator iter = PointSphere.points.begin();
iter != PointSphere.points.end(); ++iter) {
// for every Vector add a point relative to atom's position
atom * hydrogenAtom = World::getInstance().createAtom();
hydrogenAtom->setType(hydrogen);
hydrogenAtom->setPosition(AtomsPosition + *iter);
mol->AddAtom(hydrogenAtom);
// add bond
_atom->addBond(WorldTime::getTime(), hydrogenAtom);
// mark down for undo state
addedHydrogens.push_back(AtomicInfo(*(hydrogenAtom)));
}
}
return ActionState::ptr(new AtomSaturateState(addedHydrogens, params));
}
ActionState::ptr AtomSaturateAction::performUndo(ActionState::ptr _state) {
AtomSaturateState *state = assert_cast(_state.get());
// remove all added hydrogen atoms
RemoveAtomsFromAtomicInfo(state->addedHydrogens);
return ActionState::ptr(_state);
}
ActionState::ptr AtomSaturateAction::performRedo(ActionState::ptr _state){
AtomSaturateState *state = assert_cast(_state.get());
// re-add all added hydrogen atoms
AddAtomsFromAtomicInfo(state->addedHydrogens);
return ActionState::ptr(_state);
}
bool AtomSaturateAction::canUndo() {
return true;
}
bool AtomSaturateAction::shouldUndo() {
return true;
}
/** =========== end of function ====================== */