| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-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 |  * AddAction.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: May 9, 2010
 | 
|---|
| 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 "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| 38 | #include "Atom/atom.hpp"
 | 
|---|
| 39 | #include "Element/element.hpp"
 | 
|---|
| 40 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 41 | #include "molecule.hpp"
 | 
|---|
| 42 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 43 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 44 | #include "World.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include <iostream>
 | 
|---|
| 47 | #include <string>
 | 
|---|
| 48 | 
 | 
|---|
| 49 | #include "Actions/AtomAction/AddAction.hpp"
 | 
|---|
| 50 | 
 | 
|---|
| 51 | using namespace MoleCuilder;
 | 
|---|
| 52 | 
 | 
|---|
| 53 | // and construct the stuff
 | 
|---|
| 54 | #include "AddAction.def"
 | 
|---|
| 55 | #include "Action_impl_pre.hpp"
 | 
|---|
| 56 | /** =========== define the function ====================== */
 | 
|---|
| 57 | 
 | 
|---|
| 58 | atom * getNewAtom(const AtomAddAction::AtomAddParameters &_params)
 | 
|---|
| 59 | {
 | 
|---|
| 60 |   atom * first = World::getInstance().createAtom();
 | 
|---|
| 61 |   first->setType(_params.elemental.get());
 | 
|---|
| 62 |   first->setPosition(_params.position.get());
 | 
|---|
| 63 | 
 | 
|---|
| 64 |   return first;
 | 
|---|
| 65 | }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | atom * regetNewAtom(const AtomAddAction::AtomAddParameters &_params, const atomId_t _id)
 | 
|---|
| 68 | {
 | 
|---|
| 69 |   atom * first = World::getInstance().recreateAtom(_id);
 | 
|---|
| 70 |   first->setType(_params.elemental.get());
 | 
|---|
| 71 |   first->setPosition(_params.position.get());
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   return first;
 | 
|---|
| 74 | }
 | 
|---|
| 75 | 
 | 
|---|
| 76 | std::vector<atomId_t> createAtoms(
 | 
|---|
| 77 |     const AtomAddAction::AtomAddParameters &_params,
 | 
|---|
| 78 |     std::vector<molecule *> &_molecules)
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   std::vector<atomId_t> ids;
 | 
|---|
| 81 |   if (!_molecules.empty()) {
 | 
|---|
| 82 |     if (_molecules.size() == 1) {
 | 
|---|
| 83 |         atom *first = getNewAtom(_params);
 | 
|---|
| 84 |         molecule *mol = *_molecules.begin();
 | 
|---|
| 85 |         LOG(1, "Adding new atom with element " << first->getType()->getName()
 | 
|---|
| 86 |             << " at " << (first->getPosition()) << " to selected molecule "
 | 
|---|
| 87 |             << mol->getName()+".");
 | 
|---|
| 88 |         mol->AddAtom(first);
 | 
|---|
| 89 |       ids.push_back(first->getId());
 | 
|---|
| 90 |     }
 | 
|---|
| 91 |   } else {
 | 
|---|
| 92 |     atom *first = getNewAtom(_params);
 | 
|---|
| 93 |     molecule *mol = World::getInstance().createMolecule();
 | 
|---|
| 94 |     mol->setName("none");
 | 
|---|
| 95 |     mol->AddAtom(first);
 | 
|---|
| 96 |     LOG(1, "Adding new atom with element " << first->getType()->getName()
 | 
|---|
| 97 |         << " at " << (first->getPosition()) << " to new molecule.");
 | 
|---|
| 98 |     ids.push_back(first->getId());
 | 
|---|
| 99 |   }
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   return ids;
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | std::vector<atomId_t> recreateAtoms(
 | 
|---|
| 105 |     const AtomAddAction::AtomAddParameters &_params,
 | 
|---|
| 106 |     const std::vector<atomId_t> &_ids,
 | 
|---|
| 107 |     std::vector<molecule *> &_molecules)
 | 
|---|
| 108 | {
 | 
|---|
| 109 |   molecule *mol = NULL;
 | 
|---|
| 110 |   if (!_molecules.empty()) {
 | 
|---|
| 111 |     ASSERT (_molecules.size() == 1,
 | 
|---|
| 112 |         "recreateAtoms() - more than one molecules given.");
 | 
|---|
| 113 |     mol = *_molecules.begin();
 | 
|---|
| 114 |   } else {
 | 
|---|
| 115 |     mol = World::getInstance().createMolecule();
 | 
|---|
| 116 |     mol->setName("none");
 | 
|---|
| 117 |   }
 | 
|---|
| 118 |   std::vector<atomId_t> newids;
 | 
|---|
| 119 |   for (std::vector<atomId_t>::const_iterator iter = _ids.begin();
 | 
|---|
| 120 |       iter != _ids.end(); ++iter) {
 | 
|---|
| 121 |     atom *first = regetNewAtom(_params, *iter);
 | 
|---|
| 122 |     if (first != NULL) {
 | 
|---|
| 123 |       newids.push_back(first->getId());
 | 
|---|
| 124 |       mol->AddAtom(first);
 | 
|---|
| 125 |       LOG(1, "Adding new atom with element " << first->getType()->getName()
 | 
|---|
| 126 |           << " at " << (first->getPosition()) << " to new molecule.");
 | 
|---|
| 127 |     } else {
 | 
|---|
| 128 |       ELOG(1, "Could not recreate atom with id " << *iter);
 | 
|---|
| 129 |       break;
 | 
|---|
| 130 |     }
 | 
|---|
| 131 |   }
 | 
|---|
| 132 | 
 | 
|---|
| 133 |   return newids;
 | 
|---|
| 134 | }
 | 
|---|
| 135 | 
 | 
|---|
| 136 | ActionState::ptr AtomAddAction::performCall() {
 | 
|---|
| 137 |   // execute action
 | 
|---|
| 138 |   std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
 | 
|---|
| 139 |   std::vector<atomId_t> ids = createAtoms(params, molecules);
 | 
|---|
| 140 | 
 | 
|---|
| 141 |   if (molecules.size() > 1)
 | 
|---|
| 142 |      return Action::failure;
 | 
|---|
| 143 |   else
 | 
|---|
| 144 |     return ActionState::ptr(new AtomAddState(ids, params));
 | 
|---|
| 145 | }
 | 
|---|
| 146 | 
 | 
|---|
| 147 | ActionState::ptr AtomAddAction::performUndo(ActionState::ptr _state) {
 | 
|---|
| 148 |   AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
 | 
|---|
| 149 | 
 | 
|---|
| 150 |   for (std::vector<atomId_t>::const_iterator iter = state->ids.begin();
 | 
|---|
| 151 |       iter != state->ids.end(); ++iter) {
 | 
|---|
| 152 |     LOG(1, "Removing atom with id " << *iter << ".");
 | 
|---|
| 153 |     World::getInstance().destroyAtom(*iter);
 | 
|---|
| 154 |   }
 | 
|---|
| 155 | 
 | 
|---|
| 156 |   return ActionState::ptr(_state);
 | 
|---|
| 157 | }
 | 
|---|
| 158 | 
 | 
|---|
| 159 | ActionState::ptr AtomAddAction::performRedo(ActionState::ptr _state){
 | 
|---|
| 160 |   AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
 | 
|---|
| 161 | 
 | 
|---|
| 162 |   std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
 | 
|---|
| 163 |   std::vector<atomId_t> newids = recreateAtoms(state->params, state->ids, molecules);
 | 
|---|
| 164 |   if (newids.size() != state->ids.size()) {
 | 
|---|
| 165 |     STATUS("Could not recreate all atoms after undo.");
 | 
|---|
| 166 |     for (std::vector<atomId_t>::const_iterator iter = newids.begin(); iter != newids.end();++iter)
 | 
|---|
| 167 |       World::getInstance().destroyAtom(*iter);
 | 
|---|
| 168 |     return Action::failure;
 | 
|---|
| 169 |   }
 | 
|---|
| 170 |   return ActionState::ptr(_state);
 | 
|---|
| 171 | }
 | 
|---|
| 172 | 
 | 
|---|
| 173 | bool AtomAddAction::canUndo() {
 | 
|---|
| 174 |   return true;
 | 
|---|
| 175 | }
 | 
|---|
| 176 | 
 | 
|---|
| 177 | bool AtomAddAction::shouldUndo() {
 | 
|---|
| 178 |   return true;
 | 
|---|
| 179 | }
 | 
|---|
| 180 | /** =========== end of function ====================== */
 | 
|---|