| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2011 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 "MoleculeListClass.hpp"
 | 
|---|
| 36 | #include "World.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include <iostream>
 | 
|---|
| 39 | #include <string>
 | 
|---|
| 40 | #include <vector>
 | 
|---|
| 41 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 58 | using namespace MoleCuilder;
 | 
|---|
| 59 | 
 | 
|---|
| 60 | /** =========== define the function ====================== */
 | 
|---|
| 61 | Action::state_ptr MoleculeCreateMicelleAction::performCall()
 | 
|---|
| 62 | {
 | 
|---|
| 63 |   typedef std::vector <atom *> AtomVector;
 | 
|---|
| 64 |   typedef std::vector <molecule *> MoleculeVector;
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   getParametersfromValueStorage();
 | 
|---|
| 67 |         
 | 
|---|
| 68 |   AtomVector ever = World::getInstance().getAllAtoms();
 | 
|---|
| 69 | 
 | 
|---|
| 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();
 | 
|---|
| 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 | 
 | 
|---|
| 82 |   molecule *stick = *(all.begin());
 | 
|---|
| 83 | 
 | 
|---|
| 84 | 
 | 
|---|
| 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());
 | 
|---|
| 89 | 
 | 
|---|
| 90 |   // determine principal axis system and rotate such that greatest extension is along z axis
 | 
|---|
| 91 |   if(stick->size() > 1) { // only rotate if more than one molecule present
 | 
|---|
| 92 |     Vector den(0.0,0.0,1.0);
 | 
|---|
| 93 | 
 | 
|---|
| 94 |     MoleculeRotateToPrincipalAxisSystem(den);
 | 
|---|
| 95 |   }
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   // center molecule
 | 
|---|
| 98 |   stick->CenterOrigin();
 | 
|---|
| 99 | 
 | 
|---|
| 100 |   /// Align molecule with its PAS multiple times with the some surface
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   // get points on surface
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   //double radius= 1.5*sqrt(pow(1.55, 2)*params.N);
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   Shape s = resize(Sphere(), params.radius);
 | 
|---|
| 107 |   std::vector<Vector> pt = s.getHomogeneousPointsOnSurface(params.N);
 | 
|---|
| 108 | 
 | 
|---|
| 109 |   // mirror along x-y plane
 | 
|---|
| 110 | 
 | 
|---|
| 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 |     }
 | 
|---|
| 119 |   }
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   // shift molecule by its extension along z axis
 | 
|---|
| 122 | 
 | 
|---|
| 123 |   for (molecule::iterator it2=stick->begin();it2 !=stick->end();++it2)
 | 
|---|
| 124 |   {
 | 
|---|
| 125 |           Vector pos= (**it2).getPosition();
 | 
|---|
| 126 |           pos[2]=pos[2]+params.radius;
 | 
|---|
| 127 |           (**it2).setPosition(pos);
 | 
|---|
| 128 |   }
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   // copy molecule N times and rotate it to point radially away from surface
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   //double MYEPSILON=1e-10;
 | 
|---|
| 133 | 
 | 
|---|
| 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++)
 | 
|---|
| 142 |   {
 | 
|---|
| 143 |     LOG(1, "STATUS: Creating " << ka+1 << " copy of tenside molecule 'stick' with " << stick->getAtomCount() << " atoms, ");
 | 
|---|
| 144 |     molecule *Tensid=stick->CopyMolecule();
 | 
|---|
| 145 |     molecules->insert(Tensid);
 | 
|---|
| 146 |     CreatedMolecules.push_back(Tensid);
 | 
|---|
| 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
 | 
|---|
| 153 |     LOG(2, "INFO: Rotating along axis " << RotationAxis << " by angle " << alpha << ".");
 | 
|---|
| 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 | 
 | 
|---|
| 164 |   // shift molecule at given position on surface
 | 
|---|
| 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);
 | 
|---|
| 170 | 
 | 
|---|
| 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 << ".");
 | 
|---|
| 177 | 
 | 
|---|
| 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 |     }
 | 
|---|
| 183 |   }
 | 
|---|
| 184 | 
 | 
|---|
| 185 | 
 | 
|---|
| 186 |   return Action::state_ptr(new MoleculeCreateMicelleState(CreatedMolecules, stick, stickPositions, params));
 | 
|---|
| 187 | }
 | 
|---|
| 188 | 
 | 
|---|
| 189 | Action::state_ptr MoleculeCreateMicelleAction::performUndo(Action::state_ptr _state)
 | 
|---|
| 190 | {
 | 
|---|
| 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);
 | 
|---|
| 221 | }
 | 
|---|
| 222 | 
 | 
|---|
| 223 | Action::state_ptr MoleculeCreateMicelleAction::performRedo(Action::state_ptr _state)
 | 
|---|
| 224 | {
 | 
|---|
| 225 |   LOG(0, "STATUS: CreateMicelleAction::performRedo() is nit implemented yet.");
 | 
|---|
| 226 |   return Action::failure;
 | 
|---|
| 227 | }
 | 
|---|
| 228 | 
 | 
|---|
| 229 | 
 | 
|---|
| 230 | bool MoleculeCreateMicelleAction::canUndo()
 | 
|---|
| 231 | {
 | 
|---|
| 232 |   return true;
 | 
|---|
| 233 | }
 | 
|---|
| 234 | 
 | 
|---|
| 235 | bool MoleculeCreateMicelleAction::shouldUndo()
 | 
|---|
| 236 | {
 | 
|---|
| 237 |   return true;
 | 
|---|
| 238 | }
 | 
|---|
| 239 | 
 | 
|---|
| 240 | /** =========== end of function ====================== */
 | 
|---|