[c449d9] | 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/>.
|
---|
[c449d9] | 21 | */
|
---|
| 22 |
|
---|
| 23 | /*
|
---|
[d4a44c] | 24 | * CreateAdjacencyAction.cpp
|
---|
[c449d9] | 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 |
|
---|
[ad011c] | 35 | #include "CodePatterns/MemDebug.hpp"
|
---|
[c449d9] | 36 |
|
---|
[055ad7] | 37 | #include "CodePatterns/Assert.hpp"
|
---|
[ad011c] | 38 | #include "CodePatterns/Log.hpp"
|
---|
[fa9d1d] | 39 |
|
---|
| 40 | #include "Descriptors/AtomSelectionDescriptor.hpp"
|
---|
[129204] | 41 | #include "Graph/BondGraph.hpp"
|
---|
[c449d9] | 42 | #include "molecule.hpp"
|
---|
| 43 | #include "World.hpp"
|
---|
| 44 |
|
---|
| 45 | #include <iostream>
|
---|
[34c43a] | 46 | #include <list>
|
---|
[c449d9] | 47 | #include <string>
|
---|
| 48 |
|
---|
[d09093] | 49 | #include "Actions/GraphAction/CreateAdjacencyAction.hpp"
|
---|
[c449d9] | 50 |
|
---|
[ce7fdc] | 51 | using namespace MoleCuilder;
|
---|
| 52 |
|
---|
[055ad7] | 53 | // Storing undo state
|
---|
| 54 | struct BondInfo_t {
|
---|
| 55 | atomId_t leftatom;
|
---|
| 56 | atomId_t rightatom;
|
---|
| 57 | size_t degree;
|
---|
| 58 | };
|
---|
| 59 |
|
---|
[c449d9] | 60 | // and construct the stuff
|
---|
[d4a44c] | 61 | #include "CreateAdjacencyAction.def"
|
---|
[c449d9] | 62 | #include "Action_impl_pre.hpp"
|
---|
| 63 | /** =========== define the function ====================== */
|
---|
[d09093] | 64 | Action::state_ptr GraphCreateAdjacencyAction::performCall() {
|
---|
[f71baf] | 65 | BondGraph *BG = World::getInstance().getBondGraph();
|
---|
[d09093] | 66 | ASSERT(BG != NULL, "GraphCreateAdjacencyAction: BondGraph is NULL.");
|
---|
[fa9d1d] | 67 |
|
---|
[055ad7] | 68 | // count all present bonds
|
---|
[fa9d1d] | 69 | World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
|
---|
[055ad7] | 70 | std::vector<BondInfo_t> bonds;
|
---|
| 71 | {
|
---|
| 72 | size_t count_bonds = 0;
|
---|
| 73 | for (World::AtomComposite::const_iterator iter = Set.begin();
|
---|
| 74 | iter != Set.end();
|
---|
| 75 | ++iter) {
|
---|
| 76 | const atom * const Walker = *iter;
|
---|
| 77 | count_bonds += Walker->getListOfBonds().size();
|
---|
| 78 | }
|
---|
| 79 | bonds.reserve(count_bonds/2);
|
---|
| 80 | }
|
---|
| 81 | // Storing undo info
|
---|
| 82 | for (World::AtomComposite::const_iterator iter = Set.begin();
|
---|
| 83 | iter != Set.end();
|
---|
| 84 | ++iter) {
|
---|
| 85 | const atom * const Walker = *iter;
|
---|
| 86 | const BondList& ListOfBonds = Walker->getListOfBonds();
|
---|
| 87 | for (BondList::const_iterator bonditer = ListOfBonds.begin();
|
---|
| 88 | bonditer != ListOfBonds.end();
|
---|
| 89 | ++bonditer) {
|
---|
| 90 | const bond::ptr &CurrentBond = *bonditer;
|
---|
| 91 | // if both atoms are in selected set, we check ids otherwise not
|
---|
| 92 | if (((!World::getInstance().isSelected(CurrentBond->leftatom))
|
---|
| 93 | || (!World::getInstance().isSelected(CurrentBond->rightatom)))
|
---|
| 94 | || (CurrentBond->leftatom->getId() < CurrentBond->rightatom->getId())) {
|
---|
| 95 | BondInfo_t BondInfo;
|
---|
| 96 | BondInfo.leftatom = CurrentBond->leftatom->getId();
|
---|
| 97 | BondInfo.rightatom = CurrentBond->rightatom->getId();
|
---|
[1f693d] | 98 | BondInfo.degree = CurrentBond->getDegree();
|
---|
[055ad7] | 99 | bonds.push_back(BondInfo);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | GraphCreateAdjacencyState *UndoState = new GraphCreateAdjacencyState(bonds, params);
|
---|
| 104 |
|
---|
| 105 | // now recreate adjacency
|
---|
[fa9d1d] | 106 | BG->CreateAdjacency(Set);
|
---|
| 107 |
|
---|
[055ad7] | 108 | // give info
|
---|
[fa9d1d] | 109 | size_t BondCount = 0;
|
---|
| 110 | std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
|
---|
| 111 | for (std::vector<molecule *>::const_iterator iter = molecules.begin();
|
---|
| 112 | iter != molecules.end(); ++iter)
|
---|
| 113 | BondCount += (*iter)->getBondCount();
|
---|
| 114 | LOG(0, "STATUS: Recognized " << BondCount << " bonds.");
|
---|
[c449d9] | 115 |
|
---|
[055ad7] | 116 | return Action::state_ptr(UndoState);
|
---|
[c449d9] | 117 | }
|
---|
| 118 |
|
---|
[d09093] | 119 | Action::state_ptr GraphCreateAdjacencyAction::performUndo(Action::state_ptr _state) {
|
---|
[055ad7] | 120 | GraphCreateAdjacencyState *state = assert_cast<GraphCreateAdjacencyState*>(_state.get());
|
---|
| 121 |
|
---|
| 122 | BondGraph *BG = World::getInstance().getBondGraph();
|
---|
| 123 | ASSERT(BG != NULL, "GraphCreateAdjacencyAction: BondGraph is NULL.");
|
---|
[c449d9] | 124 |
|
---|
[055ad7] | 125 | // remove all bonds in the set
|
---|
| 126 | World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
|
---|
| 127 | BG->cleanAdjacencyList(Set);
|
---|
| 128 |
|
---|
| 129 | // recreate from stored info
|
---|
| 130 | const size_t CurrentTime = WorldTime::getTime();
|
---|
| 131 | std::vector<BondInfo_t> &bonds = state->bonds;
|
---|
| 132 | for(std::vector<BondInfo_t>::const_iterator iter = bonds.begin();
|
---|
| 133 | iter != bonds.end(); ++iter) {
|
---|
| 134 | atom * const Walker = World::getInstance().getAtom(AtomById(iter->leftatom));
|
---|
| 135 | ASSERT( Walker != NULL,
|
---|
| 136 | "GraphCreateAdjacencyAction::performUndo() - "+toString(iter->leftatom)+" missing.");
|
---|
| 137 | atom * const OtherWalker = World::getInstance().getAtom(AtomById(iter->rightatom));
|
---|
| 138 | ASSERT( OtherWalker != NULL,
|
---|
| 139 | "GraphCreateAdjacencyAction::performUndo() - "+toString(iter->rightatom)+" missing.");
|
---|
| 140 | bond::ptr CurrentBond = Walker->addBond(CurrentTime, OtherWalker);
|
---|
[1f693d] | 141 | CurrentBond->setDegree(iter->degree);
|
---|
[055ad7] | 142 | }
|
---|
| 143 | return Action::state_ptr(_state);
|
---|
[c449d9] | 144 | }
|
---|
| 145 |
|
---|
[d09093] | 146 | Action::state_ptr GraphCreateAdjacencyAction::performRedo(Action::state_ptr _state){
|
---|
[055ad7] | 147 | BondGraph *BG = World::getInstance().getBondGraph();
|
---|
| 148 | ASSERT(BG != NULL, "GraphCreateAdjacencyAction: BondGraph is NULL.");
|
---|
| 149 |
|
---|
| 150 | // now recreate adjacency
|
---|
| 151 | World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
|
---|
| 152 | BG->CreateAdjacency(Set);
|
---|
| 153 |
|
---|
| 154 | return Action::state_ptr(_state);
|
---|
[c449d9] | 155 | }
|
---|
| 156 |
|
---|
[d09093] | 157 | bool GraphCreateAdjacencyAction::canUndo() {
|
---|
[055ad7] | 158 | return true;
|
---|
[c449d9] | 159 | }
|
---|
| 160 |
|
---|
[d09093] | 161 | bool GraphCreateAdjacencyAction::shouldUndo() {
|
---|
[055ad7] | 162 | return true;
|
---|
[c449d9] | 163 | }
|
---|
| 164 | /** =========== end of function ====================== */
|
---|