| 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 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * CreateMicelleAction.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Sept 29, 2010 | 
|---|
| 12 | *      Author: dueck | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 20 |  | 
|---|
| 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" | 
|---|
| 30 | #include "CodePatterns/Assert.hpp" | 
|---|
| 31 | #include "CodePatterns/Log.hpp" | 
|---|
| 32 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 33 | #include "LinearAlgebra/Line.hpp" | 
|---|
| 34 | #include "molecule.hpp" | 
|---|
| 35 | #include "World.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #include <iostream> | 
|---|
| 38 | #include <string> | 
|---|
| 39 | #include <vector> | 
|---|
| 40 |  | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 57 | using namespace MoleCuilder; | 
|---|
| 58 |  | 
|---|
| 59 | /** =========== define the function ====================== */ | 
|---|
| 60 | Action::state_ptr MoleculeCreateMicelleAction::performCall() | 
|---|
| 61 | { | 
|---|
| 62 | typedef std::vector <atom *> AtomVector; | 
|---|
| 63 | typedef std::vector <molecule *> MoleculeVector; | 
|---|
| 64 |  | 
|---|
| 65 | getParametersfromValueStorage(); | 
|---|
| 66 |  | 
|---|
| 67 | AtomVector ever = World::getInstance().getAllAtoms(); | 
|---|
| 68 |  | 
|---|
| 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(); | 
|---|
| 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 |  | 
|---|
| 81 | molecule *stick = *(all.begin()); | 
|---|
| 82 |  | 
|---|
| 83 |  | 
|---|
| 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()); | 
|---|
| 88 |  | 
|---|
| 89 | // determine principal axis system and rotate such that greatest extension is along z axis | 
|---|
| 90 | if(stick->size() > 1) { // only rotate if more than one molecule present | 
|---|
| 91 | Vector den(0.0,0.0,1.0); | 
|---|
| 92 |  | 
|---|
| 93 | MoleculeRotateToPrincipalAxisSystem(den); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | // center molecule | 
|---|
| 97 | stick->CenterOrigin(); | 
|---|
| 98 |  | 
|---|
| 99 | /// Align molecule with its PAS multiple times with the some surface | 
|---|
| 100 |  | 
|---|
| 101 | // get points on surface | 
|---|
| 102 |  | 
|---|
| 103 | //double radius= 1.5*sqrt(pow(1.55, 2)*params.N); | 
|---|
| 104 |  | 
|---|
| 105 | Shape s = resize(Sphere(), params.radius); | 
|---|
| 106 | std::vector<Vector> pt = s.getHomogeneousPointsOnSurface(params.N); | 
|---|
| 107 |  | 
|---|
| 108 | // mirror along x-y plane | 
|---|
| 109 |  | 
|---|
| 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 | } | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | // shift molecule by its extension along z axis | 
|---|
| 121 |  | 
|---|
| 122 | for (molecule::iterator it2=stick->begin();it2 !=stick->end();++it2) | 
|---|
| 123 | { | 
|---|
| 124 | Vector pos= (**it2).getPosition(); | 
|---|
| 125 | pos[2]=pos[2]+params.radius; | 
|---|
| 126 | (**it2).setPosition(pos); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | // copy molecule N times and rotate it to point radially away from surface | 
|---|
| 130 |  | 
|---|
| 131 | //double MYEPSILON=1e-10; | 
|---|
| 132 |  | 
|---|
| 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++) | 
|---|
| 141 | { | 
|---|
| 142 | LOG(1, "STATUS: Creating " << ka+1 << " copy of tenside molecule 'stick' with " << stick->getAtomCount() << " atoms, "); | 
|---|
| 143 | molecule *Tensid=stick->CopyMolecule(); | 
|---|
| 144 | molecules->insert(Tensid); | 
|---|
| 145 | CreatedMolecules.push_back(Tensid); | 
|---|
| 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 | 
|---|
| 152 | LOG(2, "INFO: Rotating along axis " << RotationAxis << " by angle " << alpha << "."); | 
|---|
| 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 |  | 
|---|
| 163 | // shift molecule at given position on surface | 
|---|
| 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); | 
|---|
| 169 |  | 
|---|
| 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 << "."); | 
|---|
| 176 |  | 
|---|
| 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 | } | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 |  | 
|---|
| 185 | return Action::state_ptr(new MoleculeCreateMicelleState(CreatedMolecules, stick, stickPositions, params)); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | Action::state_ptr MoleculeCreateMicelleAction::performUndo(Action::state_ptr _state) | 
|---|
| 189 | { | 
|---|
| 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); | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | Action::state_ptr MoleculeCreateMicelleAction::performRedo(Action::state_ptr _state) | 
|---|
| 223 | { | 
|---|
| 224 | LOG(0, "STATUS: CreateMicelleAction::performRedo() is nit implemented yet."); | 
|---|
| 225 | return Action::failure; | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 |  | 
|---|
| 229 | bool MoleculeCreateMicelleAction::canUndo() | 
|---|
| 230 | { | 
|---|
| 231 | return true; | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | bool MoleculeCreateMicelleAction::shouldUndo() | 
|---|
| 235 | { | 
|---|
| 236 | return true; | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | /** =========== end of function ====================== */ | 
|---|