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