[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 |
|
---|
[ce7fdc] | 57 | using namespace MoleCuilder;
|
---|
[e2e0a5a] | 58 |
|
---|
[807c0e] | 59 | /** =========== define the function ====================== */
|
---|
[faca99] | 60 | Action::state_ptr MoleculeCreateMicelleAction::performCall()
|
---|
| 61 | {
|
---|
[37f9d4] | 62 | typedef std::vector <atom *> AtomVector;
|
---|
| 63 | typedef std::vector <molecule *> MoleculeVector;
|
---|
| 64 |
|
---|
[faca99] | 65 | getParametersfromValueStorage();
|
---|
[807c0e] | 66 |
|
---|
[faca99] | 67 | AtomVector ever = World::getInstance().getAllAtoms();
|
---|
[e2e0a5a] | 68 |
|
---|
[faca99] | 69 | // as all parsed atoms go into same molecule
|
---|
| 70 | // we don't need to create one and add them all to it
|
---|
| 71 | MoleculeVector all = World::getInstance().getSelectedMolecules();
|
---|
[f6ba43] | 72 |
|
---|
| 73 | // return success when none are selected
|
---|
| 74 | if (all.empty())
|
---|
| 75 | return Action::success;
|
---|
| 76 |
|
---|
| 77 | // check that there is precisely one molecule
|
---|
| 78 | ASSERT(all.size() == 1,
|
---|
| 79 | "MoleculeCreateMicelleAction::performCall() - more than one molecule selected, this is not supported.");
|
---|
| 80 |
|
---|
[faca99] | 81 | molecule *stick = *(all.begin());
|
---|
[e2e0a5a] | 82 |
|
---|
| 83 |
|
---|
[f6ba43] | 84 | // create undo state for original molecule
|
---|
| 85 | std::vector<Vector> stickPositions;
|
---|
| 86 | for (molecule::const_iterator iter = stick->begin(); iter != stick->end(); ++iter)
|
---|
| 87 | stickPositions.push_back((*iter)->getPosition());
|
---|
[e2e0a5a] | 88 |
|
---|
[37f9d4] | 89 | // determine principal axis system and rotate such that greatest extension is along z axis
|
---|
[f6ba43] | 90 | if(stick->size() > 1) { // only rotate if more than one molecule present
|
---|
| 91 | Vector den(0.0,0.0,1.0);
|
---|
[e2e0a5a] | 92 |
|
---|
[f6ba43] | 93 | MoleculeRotateToPrincipalAxisSystem(den);
|
---|
| 94 | }
|
---|
[e2e0a5a] | 95 |
|
---|
[f6ba43] | 96 | // center molecule
|
---|
| 97 | stick->CenterOrigin();
|
---|
[e2e0a5a] | 98 |
|
---|
[37f9d4] | 99 | /// Align molecule with its PAS multiple times with the some surface
|
---|
[e2e0a5a] | 100 |
|
---|
[37f9d4] | 101 | // get points on surface
|
---|
[e2e0a5a] | 102 |
|
---|
[faca99] | 103 | //double radius= 1.5*sqrt(pow(1.55, 2)*params.N);
|
---|
[e2e0a5a] | 104 |
|
---|
[faca99] | 105 | Shape s = resize(Sphere(), params.radius);
|
---|
| 106 | std::vector<Vector> pt = s.getHomogeneousPointsOnSurface(params.N);
|
---|
[e2e0a5a] | 107 |
|
---|
[37f9d4] | 108 | // mirror along x-y plane
|
---|
[e2e0a5a] | 109 |
|
---|
[f6ba43] | 110 | // additionally invert molecule if desired
|
---|
| 111 | if (params.DoRotate) {
|
---|
| 112 | for (molecule::iterator it2=stick->begin();it2 !=stick->end();++it2)
|
---|
| 113 | {
|
---|
| 114 | Vector pos= (**it2).getPosition();
|
---|
| 115 | pos[2]= - pos[2];// -Min[2]
|
---|
| 116 | (**it2).setPosition(pos);
|
---|
| 117 | }
|
---|
[faca99] | 118 | }
|
---|
[e2e0a5a] | 119 |
|
---|
[37f9d4] | 120 | // shift molecule by its extension along z axis
|
---|
[e2e0a5a] | 121 |
|
---|
[faca99] | 122 | for (molecule::iterator it2=stick->begin();it2 !=stick->end();++it2)
|
---|
| 123 | {
|
---|
| 124 | Vector pos= (**it2).getPosition();
|
---|
[f6ba43] | 125 | pos[2]=pos[2]+params.radius;
|
---|
[faca99] | 126 | (**it2).setPosition(pos);
|
---|
| 127 | }
|
---|
[e2e0a5a] | 128 |
|
---|
[37f9d4] | 129 | // copy molecule N times and rotate it to point radially away from surface
|
---|
[e2e0a5a] | 130 |
|
---|
[faca99] | 131 | //double MYEPSILON=1e-10;
|
---|
| 132 |
|
---|
[f6ba43] | 133 | // gather created molecules for undo state
|
---|
| 134 | std::vector<molecule *> CreatedMolecules;
|
---|
| 135 |
|
---|
| 136 | // TODO: remove this when we do not need MoleculeListClass anymore
|
---|
| 137 | MoleculeListClass *&molecules = World::getInstance().getMolecules();
|
---|
| 138 |
|
---|
| 139 | size_t ka = 0;
|
---|
| 140 | for (; ka<pt.size()-1; ka++)
|
---|
[faca99] | 141 | {
|
---|
[37f9d4] | 142 | LOG(1, "STATUS: Creating " << ka+1 << " copy of tenside molecule 'stick' with " << stick->getAtomCount() << " atoms, ");
|
---|
[f6ba43] | 143 | molecule *Tensid=stick->CopyMolecule();
|
---|
| 144 | molecules->insert(Tensid);
|
---|
| 145 | CreatedMolecules.push_back(Tensid);
|
---|
[faca99] | 146 |
|
---|
| 147 | Vector ZAxis(Vector(0.0,0.0,1.0));
|
---|
| 148 | Vector Axis(pt[ka]);
|
---|
| 149 | const double alpha = ZAxis.Angle(Axis);
|
---|
| 150 | Axis.VectorProduct(ZAxis);
|
---|
| 151 | Line RotationAxis(Vector(0.0,0.0,1.0), Axis); // pt is the current Vector of point on surface
|
---|
[f6ba43] | 152 | LOG(2, "INFO: Rotating along axis " << RotationAxis << " by angle " << alpha << ".");
|
---|
[faca99] | 153 |
|
---|
| 154 | for (molecule::iterator it2=Tensid->begin();it2 !=Tensid->end();++it2)
|
---|
| 155 | {
|
---|
| 156 | (*it2)->setPosition(RotationAxis.rotateVector((*it2)->getPosition(),alpha));
|
---|
| 157 | *(*it2)+=params.center;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | Tensid=NULL;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[37f9d4] | 163 | // shift molecule at given position on surface
|
---|
[f6ba43] | 164 | {
|
---|
| 165 | LOG(1, "STATUS: Shifting " << ka+1 << " copy of tenside molecule 'stick' with " << stick->getAtomCount() << " atoms, ");
|
---|
| 166 | molecule *Tensid=stick;
|
---|
| 167 | // this molecule was present before, hence do not remove it!
|
---|
| 168 | //CreatedMolecules.push_back(Tensid);
|
---|
[37f9d4] | 169 |
|
---|
[f6ba43] | 170 | Vector ZAxis(Vector(0.0,0.0,1.0));
|
---|
| 171 | Vector Axis(pt[pt.size()-1]);
|
---|
| 172 | const double alpha = ZAxis.Angle(Axis);
|
---|
| 173 | Axis.VectorProduct(ZAxis);
|
---|
| 174 | Line RotationAxis(Vector(0.0,0.0,1.0), Axis); // pt is the current Vector of point on surface
|
---|
| 175 | LOG(2, "INFO: Rotating along axis " << RotationAxis << " by angle " << alpha << ".");
|
---|
[e2e0a5a] | 176 |
|
---|
[f6ba43] | 177 | for (molecule::iterator it2=Tensid->begin();it2 !=Tensid->end();++it2)
|
---|
| 178 | {
|
---|
| 179 | (*it2)->setPosition(RotationAxis.rotateVector((*it2)->getPosition(),alpha));
|
---|
| 180 | *(*it2)+=params.center;
|
---|
| 181 | }
|
---|
[faca99] | 182 | }
|
---|
[e2e0a5a] | 183 |
|
---|
| 184 |
|
---|
[f6ba43] | 185 | return Action::state_ptr(new MoleculeCreateMicelleState(CreatedMolecules, stick, stickPositions, params));
|
---|
[e2e0a5a] | 186 | }
|
---|
| 187 |
|
---|
[faca99] | 188 | Action::state_ptr MoleculeCreateMicelleAction::performUndo(Action::state_ptr _state)
|
---|
| 189 | {
|
---|
[f6ba43] | 190 | MoleculeCreateMicelleState *state = assert_cast<MoleculeCreateMicelleState*>(_state.get());
|
---|
| 191 |
|
---|
| 192 | // removing all created molecules
|
---|
| 193 | MoleculeListClass *&molecules = World::getInstance().getMolecules();
|
---|
| 194 | LOG(0, "STATUS: Removing " << state->CreatedMolecules.size() << " molecules again.");
|
---|
| 195 | for(std::vector<molecule *>::const_iterator iter = state->CreatedMolecules.begin();
|
---|
| 196 | iter != state->CreatedMolecules.end();
|
---|
| 197 | ++iter) {
|
---|
| 198 | LOG(2, "INFO: Removing " << (*iter)->getName());
|
---|
| 199 | for(molecule::iterator AtomRunner = (*iter)->begin();
|
---|
| 200 | !(*iter)->empty();
|
---|
| 201 | AtomRunner = (*iter)->begin()) {
|
---|
| 202 | World::getInstance().destroyAtom(*AtomRunner);
|
---|
| 203 | }
|
---|
| 204 | //LOG(3, "INFO: molecules contains still " << molecules->ListOfMolecules.size() << " molecules.");
|
---|
| 205 | molecules->erase(*iter);
|
---|
| 206 | World::getInstance().destroyMolecule(*iter);
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | // undo changes to original molecule
|
---|
| 210 | ASSERT(state->stickPositions.size() == state->stick->size(),
|
---|
| 211 | "MoleculeCreateMicelleAction::performUndo() - number of stored positions ("
|
---|
| 212 | +toString(state->stickPositions.size())+") and number of atoms ("
|
---|
| 213 | +toString(state->stick->size())+") do not match.");
|
---|
| 214 | std::vector<Vector>::const_iterator PosIter = state->stickPositions.begin();
|
---|
| 215 | molecule::iterator iter = state->stick->begin();
|
---|
| 216 | for (; iter != state->stick->end(); ++iter, ++PosIter)
|
---|
| 217 | (*iter)->setPosition(*PosIter);
|
---|
| 218 |
|
---|
| 219 | return Action::state_ptr(_state);
|
---|
[e2e0a5a] | 220 | }
|
---|
[807c0e] | 221 |
|
---|
[faca99] | 222 | Action::state_ptr MoleculeCreateMicelleAction::performRedo(Action::state_ptr _state)
|
---|
| 223 | {
|
---|
[f6ba43] | 224 | LOG(0, "STATUS: CreateMicelleAction::performRedo() is nit implemented yet.");
|
---|
| 225 | return Action::failure;
|
---|
[e2e0a5a] | 226 | }
|
---|
| 227 |
|
---|
[807c0e] | 228 |
|
---|
[faca99] | 229 | bool MoleculeCreateMicelleAction::canUndo()
|
---|
| 230 | {
|
---|
[f6ba43] | 231 | return true;
|
---|
[e2e0a5a] | 232 | }
|
---|
| 233 |
|
---|
[faca99] | 234 | bool MoleculeCreateMicelleAction::shouldUndo()
|
---|
| 235 | {
|
---|
[f6ba43] | 236 | return true;
|
---|
[e2e0a5a] | 237 | }
|
---|
| 238 |
|
---|
[807c0e] | 239 | /** =========== end of function ====================== */
|
---|