[e2e0a5a] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[807c0e] | 8 | /*
|
---|
| 9 | * CreateMicelleAction.cpp
|
---|
[e2e0a5a] | 10 | *
|
---|
[807c0e] | 11 | * Created on: Sept 29, 2010
|
---|
| 12 | * Author: dueck
|
---|
[e2e0a5a] | 13 | */
|
---|
| 14 |
|
---|
| 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 20 |
|
---|
[807c0e] | 21 | #include "Actions/ActionHistory.hpp"
|
---|
| 22 | #include "Actions/ActionRegistry.hpp"
|
---|
| 23 | #include "Actions/GraphAction/SubgraphDissectionAction.hpp"
|
---|
| 24 | #include "Actions/MoleculeAction/RotateToPrincipalAxisSystemAction.hpp"
|
---|
| 25 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
| 26 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
| 27 |
|
---|
| 28 | #include "atom.hpp"
|
---|
| 29 | #include "Bond/bond.hpp"
|
---|
[faca99] | 30 | #include "CodePatterns/Assert.hpp"
|
---|
| 31 | #include "CodePatterns/Log.hpp"
|
---|
| 32 | #include "CodePatterns/Verbose.hpp"
|
---|
| 33 | #include "LinearAlgebra/Line.hpp"
|
---|
[807c0e] | 34 | #include "molecule.hpp"
|
---|
| 35 | #include "World.hpp"
|
---|
| 36 |
|
---|
| 37 | #include <iostream>
|
---|
| 38 | #include <string>
|
---|
[37f9d4] | 39 | #include <vector>
|
---|
[807c0e] | 40 |
|
---|
[faca99] | 41 | #include "Parser/PdbParser.hpp"
|
---|
| 42 | #include "Parser/TremoloParser.hpp"
|
---|
| 43 | #include "Parser/XyzParser.hpp"
|
---|
| 44 | #include "Parser/FormatParserStorage.hpp"
|
---|
| 45 | #include "Shapes/BaseShapes.hpp"
|
---|
| 46 | #include "Shapes/ShapeOps.hpp"
|
---|
| 47 |
|
---|
[807c0e] | 48 | #include "Actions/MoleculeAction/CreateMicelleAction.hpp"
|
---|
| 49 |
|
---|
| 50 | #include "CreateMicelleAction.def"
|
---|
| 51 | #include "Action_impl_pre.hpp"
|
---|
| 52 |
|
---|
| 53 | #include "UIElements/UIFactory.hpp"
|
---|
| 54 | #include "UIElements/Dialog.hpp"
|
---|
| 55 | #include "Actions/ValueStorage.hpp"
|
---|
| 56 |
|
---|
[e2e0a5a] | 57 |
|
---|
[807c0e] | 58 | /** =========== define the function ====================== */
|
---|
[faca99] | 59 | Action::state_ptr MoleculeCreateMicelleAction::performCall()
|
---|
| 60 | {
|
---|
[37f9d4] | 61 | typedef std::vector <atom *> AtomVector;
|
---|
| 62 | typedef std::vector <molecule *> MoleculeVector;
|
---|
| 63 |
|
---|
[faca99] | 64 | getParametersfromValueStorage();
|
---|
[807c0e] | 65 |
|
---|
[faca99] | 66 | AtomVector ever = World::getInstance().getAllAtoms();
|
---|
[e2e0a5a] | 67 |
|
---|
[faca99] | 68 | // as all parsed atoms go into same molecule
|
---|
| 69 | // we don't need to create one and add them all to it
|
---|
| 70 | MoleculeVector all = World::getInstance().getSelectedMolecules();
|
---|
[f6ba43] | 71 |
|
---|
| 72 | // return success when none are selected
|
---|
| 73 | if (all.empty())
|
---|
| 74 | return Action::success;
|
---|
| 75 |
|
---|
| 76 | // check that there is precisely one molecule
|
---|
| 77 | ASSERT(all.size() == 1,
|
---|
| 78 | "MoleculeCreateMicelleAction::performCall() - more than one molecule selected, this is not supported.");
|
---|
| 79 |
|
---|
[faca99] | 80 | molecule *stick = *(all.begin());
|
---|
[e2e0a5a] | 81 |
|
---|
| 82 |
|
---|
[f6ba43] | 83 | // create undo state for original molecule
|
---|
| 84 | std::vector<Vector> stickPositions;
|
---|
| 85 | for (molecule::const_iterator iter = stick->begin(); iter != stick->end(); ++iter)
|
---|
| 86 | stickPositions.push_back((*iter)->getPosition());
|
---|
[e2e0a5a] | 87 |
|
---|
[37f9d4] | 88 | // determine principal axis system and rotate such that greatest extension is along z axis
|
---|
[f6ba43] | 89 | if(stick->size() > 1) { // only rotate if more than one molecule present
|
---|
| 90 | Vector den(0.0,0.0,1.0);
|
---|
[e2e0a5a] | 91 |
|
---|
[f6ba43] | 92 | MoleculeRotateToPrincipalAxisSystem(den);
|
---|
| 93 | }
|
---|
[e2e0a5a] | 94 |
|
---|
[f6ba43] | 95 | // center molecule
|
---|
| 96 | stick->CenterOrigin();
|
---|
[e2e0a5a] | 97 |
|
---|
[37f9d4] | 98 | /// Align molecule with its PAS multiple times with the some surface
|
---|
[e2e0a5a] | 99 |
|
---|
[37f9d4] | 100 | // get points on surface
|
---|
[e2e0a5a] | 101 |
|
---|
[faca99] | 102 | //double radius= 1.5*sqrt(pow(1.55, 2)*params.N);
|
---|
[e2e0a5a] | 103 |
|
---|
[faca99] | 104 | Shape s = resize(Sphere(), params.radius);
|
---|
| 105 | std::vector<Vector> pt = s.getHomogeneousPointsOnSurface(params.N);
|
---|
[e2e0a5a] | 106 |
|
---|
[37f9d4] | 107 | // mirror along x-y plane
|
---|
[e2e0a5a] | 108 |
|
---|
[f6ba43] | 109 | // additionally invert molecule if desired
|
---|
| 110 | if (params.DoRotate) {
|
---|
| 111 | for (molecule::iterator it2=stick->begin();it2 !=stick->end();++it2)
|
---|
| 112 | {
|
---|
| 113 | Vector pos= (**it2).getPosition();
|
---|
| 114 | pos[2]= - pos[2];// -Min[2]
|
---|
| 115 | (**it2).setPosition(pos);
|
---|
| 116 | }
|
---|
[faca99] | 117 | }
|
---|
[e2e0a5a] | 118 |
|
---|
[37f9d4] | 119 | // shift molecule by its extension along z axis
|
---|
[e2e0a5a] | 120 |
|
---|
[faca99] | 121 | for (molecule::iterator it2=stick->begin();it2 !=stick->end();++it2)
|
---|
| 122 | {
|
---|
| 123 | Vector pos= (**it2).getPosition();
|
---|
[f6ba43] | 124 | pos[2]=pos[2]+params.radius;
|
---|
[faca99] | 125 | (**it2).setPosition(pos);
|
---|
| 126 | }
|
---|
[e2e0a5a] | 127 |
|
---|
[37f9d4] | 128 | // copy molecule N times and rotate it to point radially away from surface
|
---|
[e2e0a5a] | 129 |
|
---|
[faca99] | 130 | //double MYEPSILON=1e-10;
|
---|
| 131 |
|
---|
[f6ba43] | 132 | // gather created molecules for undo state
|
---|
| 133 | std::vector<molecule *> CreatedMolecules;
|
---|
| 134 |
|
---|
| 135 | // TODO: remove this when we do not need MoleculeListClass anymore
|
---|
| 136 | MoleculeListClass *&molecules = World::getInstance().getMolecules();
|
---|
| 137 |
|
---|
| 138 | size_t ka = 0;
|
---|
| 139 | for (; ka<pt.size()-1; ka++)
|
---|
[faca99] | 140 | {
|
---|
[37f9d4] | 141 | LOG(1, "STATUS: Creating " << ka+1 << " copy of tenside molecule 'stick' with " << stick->getAtomCount() << " atoms, ");
|
---|
[f6ba43] | 142 | molecule *Tensid=stick->CopyMolecule();
|
---|
| 143 | molecules->insert(Tensid);
|
---|
| 144 | CreatedMolecules.push_back(Tensid);
|
---|
[faca99] | 145 |
|
---|
| 146 | Vector ZAxis(Vector(0.0,0.0,1.0));
|
---|
| 147 | Vector Axis(pt[ka]);
|
---|
| 148 | const double alpha = ZAxis.Angle(Axis);
|
---|
| 149 | Axis.VectorProduct(ZAxis);
|
---|
| 150 | Line RotationAxis(Vector(0.0,0.0,1.0), Axis); // pt is the current Vector of point on surface
|
---|
[f6ba43] | 151 | LOG(2, "INFO: Rotating along axis " << RotationAxis << " by angle " << alpha << ".");
|
---|
[faca99] | 152 |
|
---|
| 153 | for (molecule::iterator it2=Tensid->begin();it2 !=Tensid->end();++it2)
|
---|
| 154 | {
|
---|
| 155 | (*it2)->setPosition(RotationAxis.rotateVector((*it2)->getPosition(),alpha));
|
---|
| 156 | *(*it2)+=params.center;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | Tensid=NULL;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[37f9d4] | 162 | // shift molecule at given position on surface
|
---|
[f6ba43] | 163 | {
|
---|
| 164 | LOG(1, "STATUS: Shifting " << ka+1 << " copy of tenside molecule 'stick' with " << stick->getAtomCount() << " atoms, ");
|
---|
| 165 | molecule *Tensid=stick;
|
---|
| 166 | // this molecule was present before, hence do not remove it!
|
---|
| 167 | //CreatedMolecules.push_back(Tensid);
|
---|
[37f9d4] | 168 |
|
---|
[f6ba43] | 169 | Vector ZAxis(Vector(0.0,0.0,1.0));
|
---|
| 170 | Vector Axis(pt[pt.size()-1]);
|
---|
| 171 | const double alpha = ZAxis.Angle(Axis);
|
---|
| 172 | Axis.VectorProduct(ZAxis);
|
---|
| 173 | Line RotationAxis(Vector(0.0,0.0,1.0), Axis); // pt is the current Vector of point on surface
|
---|
| 174 | LOG(2, "INFO: Rotating along axis " << RotationAxis << " by angle " << alpha << ".");
|
---|
[e2e0a5a] | 175 |
|
---|
[f6ba43] | 176 | for (molecule::iterator it2=Tensid->begin();it2 !=Tensid->end();++it2)
|
---|
| 177 | {
|
---|
| 178 | (*it2)->setPosition(RotationAxis.rotateVector((*it2)->getPosition(),alpha));
|
---|
| 179 | *(*it2)+=params.center;
|
---|
| 180 | }
|
---|
[faca99] | 181 | }
|
---|
[e2e0a5a] | 182 |
|
---|
| 183 |
|
---|
[f6ba43] | 184 | return Action::state_ptr(new MoleculeCreateMicelleState(CreatedMolecules, stick, stickPositions, params));
|
---|
[e2e0a5a] | 185 | }
|
---|
| 186 |
|
---|
[faca99] | 187 | Action::state_ptr MoleculeCreateMicelleAction::performUndo(Action::state_ptr _state)
|
---|
| 188 | {
|
---|
[f6ba43] | 189 | MoleculeCreateMicelleState *state = assert_cast<MoleculeCreateMicelleState*>(_state.get());
|
---|
| 190 |
|
---|
| 191 | // removing all created molecules
|
---|
| 192 | MoleculeListClass *&molecules = World::getInstance().getMolecules();
|
---|
| 193 | LOG(0, "STATUS: Removing " << state->CreatedMolecules.size() << " molecules again.");
|
---|
| 194 | for(std::vector<molecule *>::const_iterator iter = state->CreatedMolecules.begin();
|
---|
| 195 | iter != state->CreatedMolecules.end();
|
---|
| 196 | ++iter) {
|
---|
| 197 | LOG(2, "INFO: Removing " << (*iter)->getName());
|
---|
| 198 | for(molecule::iterator AtomRunner = (*iter)->begin();
|
---|
| 199 | !(*iter)->empty();
|
---|
| 200 | AtomRunner = (*iter)->begin()) {
|
---|
| 201 | World::getInstance().destroyAtom(*AtomRunner);
|
---|
| 202 | }
|
---|
| 203 | //LOG(3, "INFO: molecules contains still " << molecules->ListOfMolecules.size() << " molecules.");
|
---|
| 204 | molecules->erase(*iter);
|
---|
| 205 | World::getInstance().destroyMolecule(*iter);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | // undo changes to original molecule
|
---|
| 209 | ASSERT(state->stickPositions.size() == state->stick->size(),
|
---|
| 210 | "MoleculeCreateMicelleAction::performUndo() - number of stored positions ("
|
---|
| 211 | +toString(state->stickPositions.size())+") and number of atoms ("
|
---|
| 212 | +toString(state->stick->size())+") do not match.");
|
---|
| 213 | std::vector<Vector>::const_iterator PosIter = state->stickPositions.begin();
|
---|
| 214 | molecule::iterator iter = state->stick->begin();
|
---|
| 215 | for (; iter != state->stick->end(); ++iter, ++PosIter)
|
---|
| 216 | (*iter)->setPosition(*PosIter);
|
---|
| 217 |
|
---|
| 218 | return Action::state_ptr(_state);
|
---|
[e2e0a5a] | 219 | }
|
---|
[807c0e] | 220 |
|
---|
[faca99] | 221 | Action::state_ptr MoleculeCreateMicelleAction::performRedo(Action::state_ptr _state)
|
---|
| 222 | {
|
---|
[f6ba43] | 223 | LOG(0, "STATUS: CreateMicelleAction::performRedo() is nit implemented yet.");
|
---|
| 224 | return Action::failure;
|
---|
[e2e0a5a] | 225 | }
|
---|
| 226 |
|
---|
[807c0e] | 227 |
|
---|
[faca99] | 228 | bool MoleculeCreateMicelleAction::canUndo()
|
---|
| 229 | {
|
---|
[f6ba43] | 230 | return true;
|
---|
[e2e0a5a] | 231 | }
|
---|
| 232 |
|
---|
[faca99] | 233 | bool MoleculeCreateMicelleAction::shouldUndo()
|
---|
| 234 | {
|
---|
[f6ba43] | 235 | return true;
|
---|
[e2e0a5a] | 236 | }
|
---|
| 237 |
|
---|
[807c0e] | 238 | /** =========== end of function ====================== */
|
---|