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 | * CreateAdjacencyAction.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 "CodePatterns/Assert.hpp"
|
---|
38 | #include "CodePatterns/Log.hpp"
|
---|
39 |
|
---|
40 | #include "Descriptors/AtomSelectionDescriptor.hpp"
|
---|
41 | #include "Graph/BondGraph.hpp"
|
---|
42 | #include "molecule.hpp"
|
---|
43 | #include "World.hpp"
|
---|
44 |
|
---|
45 | #include <iostream>
|
---|
46 | #include <list>
|
---|
47 | #include <string>
|
---|
48 |
|
---|
49 | #include "Actions/GraphAction/CreateAdjacencyAction.hpp"
|
---|
50 |
|
---|
51 | using namespace MoleCuilder;
|
---|
52 |
|
---|
53 | // Storing undo state
|
---|
54 | struct BondInfo_t {
|
---|
55 | atomId_t leftatom;
|
---|
56 | atomId_t rightatom;
|
---|
57 | size_t degree;
|
---|
58 | };
|
---|
59 |
|
---|
60 | // and construct the stuff
|
---|
61 | #include "CreateAdjacencyAction.def"
|
---|
62 | #include "Action_impl_pre.hpp"
|
---|
63 | /** =========== define the function ====================== */
|
---|
64 | Action::state_ptr GraphCreateAdjacencyAction::performCall() {
|
---|
65 | BondGraph *BG = World::getInstance().getBondGraph();
|
---|
66 | ASSERT(BG != NULL, "GraphCreateAdjacencyAction: BondGraph is NULL.");
|
---|
67 |
|
---|
68 | // count all present bonds
|
---|
69 | World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
|
---|
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();
|
---|
98 | BondInfo.degree = CurrentBond->BondDegree;
|
---|
99 | bonds.push_back(BondInfo);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | GraphCreateAdjacencyState *UndoState = new GraphCreateAdjacencyState(bonds, params);
|
---|
104 |
|
---|
105 | // now recreate adjacency
|
---|
106 | BG->CreateAdjacency(Set);
|
---|
107 |
|
---|
108 | // give info
|
---|
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.");
|
---|
115 |
|
---|
116 | return Action::state_ptr(UndoState);
|
---|
117 | }
|
---|
118 |
|
---|
119 | Action::state_ptr GraphCreateAdjacencyAction::performUndo(Action::state_ptr _state) {
|
---|
120 | GraphCreateAdjacencyState *state = assert_cast<GraphCreateAdjacencyState*>(_state.get());
|
---|
121 |
|
---|
122 | BondGraph *BG = World::getInstance().getBondGraph();
|
---|
123 | ASSERT(BG != NULL, "GraphCreateAdjacencyAction: BondGraph is NULL.");
|
---|
124 |
|
---|
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);
|
---|
141 | CurrentBond->BondDegree = iter->degree;
|
---|
142 | }
|
---|
143 | return Action::state_ptr(_state);
|
---|
144 | }
|
---|
145 |
|
---|
146 | Action::state_ptr GraphCreateAdjacencyAction::performRedo(Action::state_ptr _state){
|
---|
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);
|
---|
155 | }
|
---|
156 |
|
---|
157 | bool GraphCreateAdjacencyAction::canUndo() {
|
---|
158 | return true;
|
---|
159 | }
|
---|
160 |
|
---|
161 | bool GraphCreateAdjacencyAction::shouldUndo() {
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 | /** =========== end of function ====================== */
|
---|