source: src/Actions/AtomAction/AddAction.cpp@ a844d8

Candidate_v1.6.1
Last change on this file since a844d8 was 9eb71b3, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

Commented out MemDebug include and Memory::ignore.

  • MemDebug clashes with various allocation operators that use a specific placement in memory. It is so far not possible to wrap new/delete fully. Hence, we stop this effort which so far has forced us to put ever more includes (with clashes) into MemDebug and thereby bloat compilation time.
  • MemDebug does not add that much usefulness which is not also provided by valgrind.
  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[94d5ac6]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/>.
[bcf653]21 */
22
[97ebf8]23/*
24 * AddAction.cpp
25 *
26 * Created on: May 9, 2010
27 * Author: heber
28 */
29
[bf3817]30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
[9eb71b3]35//#include "CodePatterns/MemDebug.hpp"
[112b09]36
[ea2830]37#include "Descriptors/AtomIdDescriptor.hpp"
[6f0841]38#include "Atom/atom.hpp"
[3bdb6d]39#include "Element/element.hpp"
[ad011c]40#include "CodePatterns/Log.hpp"
[f0a3ec]41#include "molecule.hpp"
[57f243]42#include "LinearAlgebra/Vector.hpp"
[ad011c]43#include "CodePatterns/Verbose.hpp"
[97ebf8]44#include "World.hpp"
45
46#include <iostream>
47#include <string>
48
[8bb05e]49#include "Actions/AtomAction/AddAction.hpp"
[9d33ba]50
[ce7fdc]51using namespace MoleCuilder;
52
[8bb05e]53// and construct the stuff
54#include "AddAction.def"
55#include "Action_impl_pre.hpp"
56/** =========== define the function ====================== */
[ceaab1]57
58atom * getNewAtom(const AtomAddAction::AtomAddParameters &_params)
59{
[85537a]60 atom * first = World::getInstance().createAtom();
[ceaab1]61 first->setType(_params.elemental.get());
62 first->setPosition(_params.position.get());
63
64 return first;
65}
66
[8ad68b]67atom * 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
[ceaab1]76std::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());
[97ebf8]99 }
[ceaab1]100
101 return ids;
102}
103
[8ad68b]104std::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
[ceaab1]136ActionState::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));
[97ebf8]145}
146
[b5b01e]147ActionState::ptr AtomAddAction::performUndo(ActionState::ptr _state) {
[ea2830]148 AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
[97ebf8]149
[ceaab1]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 }
[ea2830]155
[b5b01e]156 return ActionState::ptr(_state);
[97ebf8]157}
158
[b5b01e]159ActionState::ptr AtomAddAction::performRedo(ActionState::ptr _state){
[ea2830]160 AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
161
[ceaab1]162 std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
[8ad68b]163 std::vector<atomId_t> newids = recreateAtoms(state->params, state->ids, molecules);
[ceaab1]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;
[ea2830]169 }
[8ad68b]170 return ActionState::ptr(_state);
[97ebf8]171}
172
173bool AtomAddAction::canUndo() {
[ea2830]174 return true;
[97ebf8]175}
176
177bool AtomAddAction::shouldUndo() {
[ea2830]178 return true;
[97ebf8]179}
[8bb05e]180/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.