| [a88452] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [a88452] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /*
 | 
|---|
 | 24 |  * FillRegularGridAction.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Jan 12, 2012
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 36 | 
 | 
|---|
| [42b6de] | 37 | #include "Actions/UndoRedoHelpers.hpp"
 | 
|---|
| [a88452] | 38 | #include "Atom/atom.hpp"
 | 
|---|
| [42b6de] | 39 | #include "Atom/AtomicInfo.hpp"
 | 
|---|
| [a88452] | 40 | #include "Atom/CopyAtoms/CopyAtoms_withBonds.hpp"
 | 
|---|
| [80ca29] | 41 | #include "Bond/BondInfo.hpp"
 | 
|---|
| [a88452] | 42 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 43 | #include "Descriptors/MoleculeOrderDescriptor.hpp"
 | 
|---|
 | 44 | #include "Filling/Cluster.hpp"
 | 
|---|
 | 45 | #include "Filling/Filler.hpp"
 | 
|---|
| [cae614] | 46 | #include "Filling/Preparators/BoxFillerPreparator.hpp"
 | 
|---|
| [345eda] | 47 | #include "molecule.hpp"
 | 
|---|
| [a88452] | 48 | #include "MoleculeListClass.hpp"
 | 
|---|
 | 49 | #include "Parser/FormatParserInterface.hpp"
 | 
|---|
 | 50 | #include "Parser/FormatParserStorage.hpp"
 | 
|---|
 | 51 | #include "World.hpp"
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | #include <algorithm>
 | 
|---|
 | 54 | #include <iostream>
 | 
|---|
 | 55 | #include <string>
 | 
|---|
 | 56 | #include <vector>
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 | #include "Actions/FillAction/FillRegularGridAction.hpp"
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 | using namespace MoleCuilder;
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 | // and construct the stuff
 | 
|---|
 | 63 | #include "FillRegularGridAction.def"
 | 
|---|
 | 64 | #include "Action_impl_pre.hpp"
 | 
|---|
 | 65 | /** =========== define the function ====================== */
 | 
|---|
| [b5b01e] | 66 | ActionState::ptr FillRegularGridAction::performCall() {
 | 
|---|
| [a88452] | 67 |   typedef std::vector<atom*> AtomVector;
 | 
|---|
 | 68 | 
 | 
|---|
| [80ca29] | 69 |   // get the filler molecule
 | 
|---|
 | 70 |   std::vector<AtomicInfo> movedatoms;
 | 
|---|
| [e9ad43] | 71 |   const std::vector< molecule *> molecules = World::getInstance().getSelectedMolecules();
 | 
|---|
 | 72 |   if (molecules.size() != 1) {
 | 
|---|
| [26b4d62] | 73 |     STATUS("No exactly one molecule selected, aborting,");
 | 
|---|
| [5a4cbc] | 74 |     return Action::failure;
 | 
|---|
 | 75 |   }
 | 
|---|
| [e9ad43] | 76 |   molecule *filler = *(molecules.begin());
 | 
|---|
| [80ca29] | 77 |   for(molecule::const_iterator iter = filler->begin(); iter != filler->end(); ++iter)
 | 
|---|
 | 78 |     movedatoms.push_back( AtomicInfo(*(*iter)) );
 | 
|---|
| [e9ad43] | 79 |   LOG(1, "INFO: Chosen molecule has " << filler->size() << " atoms.");
 | 
|---|
| [a88452] | 80 | 
 | 
|---|
| [a090e3] | 81 |   // center filler's tip at origin
 | 
|---|
 | 82 |   filler->CenterEdge();
 | 
|---|
 | 83 | 
 | 
|---|
| [cae614] | 84 |   // prepare the filler preparator
 | 
|---|
 | 85 |   BoxFillerPreparator filler_preparator(filler);
 | 
|---|
| [f10b0c] | 86 |   if (params.SphereRadius.get() != 0.) {
 | 
|---|
| [cae614] | 87 |     if (World::getInstance().beginAtomSelection() == World::getInstance().endAtomSelection()) {
 | 
|---|
| [26b4d62] | 88 |       STATUS("You have given a sphere radius "+toString(params.SphereRadius.get())
 | 
|---|
 | 89 |           +" != 0, but have not select any atoms.");
 | 
|---|
| [80ca29] | 90 |       return Action::failure;
 | 
|---|
| [345eda] | 91 |     }
 | 
|---|
| [cae614] | 92 |     std::vector<atom*> atoms(World::getInstance().getSelectedAtoms());
 | 
|---|
 | 93 |     filler_preparator.addSurfacePredicate(
 | 
|---|
 | 94 |         params.SphereRadius.get(),
 | 
|---|
 | 95 |         atoms);
 | 
|---|
 | 96 |   }
 | 
|---|
 | 97 |   filler_preparator.addVoidPredicate(params.mindistance.get());
 | 
|---|
 | 98 |   filler_preparator.addRandomInserter(
 | 
|---|
 | 99 |       params.RandAtomDisplacement.get(),
 | 
|---|
 | 100 |       params.RandMoleculeDisplacement.get(),
 | 
|---|
 | 101 |       params.DoRotate.get());
 | 
|---|
 | 102 |   filler_preparator.addCubeMesh(
 | 
|---|
 | 103 |       params.counts.get(),
 | 
|---|
 | 104 |       params.offset.get(),
 | 
|---|
 | 105 |       World::getInstance().getDomain().getM());
 | 
|---|
 | 106 |   if (!filler_preparator()) {
 | 
|---|
 | 107 |     STATUS("Filler was not fully constructed.");
 | 
|---|
 | 108 |     return Action::failure;
 | 
|---|
| [345eda] | 109 |   }
 | 
|---|
 | 110 | 
 | 
|---|
| [cae614] | 111 |   // use filler
 | 
|---|
| [68abe5] | 112 |   bool successflag = false;
 | 
|---|
| [cae614] | 113 |   FillRegularGridState *UndoState = NULL;
 | 
|---|
| [a88452] | 114 |   {
 | 
|---|
 | 115 |     // fill
 | 
|---|
| [cae614] | 116 |     Filler *fillerFunction = filler_preparator.obtainFiller();
 | 
|---|
 | 117 |     // TODO: When molecule::getBoundingSphere() does not use a sphere anymore,
 | 
|---|
 | 118 |     // we need to check whether we rotate the molecule randomly. For this to
 | 
|---|
 | 119 |     // work we need a sphere!
 | 
|---|
 | 120 |     const Shape s = filler->getBoundingSphere(params.RandAtomDisplacement.get());
 | 
|---|
 | 121 |     ClusterInterface::Cluster_impl cluster( new Cluster(filler->getAtomIds(), s) );
 | 
|---|
 | 122 |     CopyAtoms_withBonds copyMethod;
 | 
|---|
 | 123 |     Filler::ClusterVector_t ClonedClusters;
 | 
|---|
 | 124 |     successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters);
 | 
|---|
 | 125 |     delete fillerFunction;
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 |     // append each cluster's atoms to clonedatoms (however not selected ones)
 | 
|---|
 | 128 |     std::vector<const atom *> clonedatoms;
 | 
|---|
 | 129 |     std::vector<AtomicInfo> clonedatominfos;
 | 
|---|
 | 130 |     for (Filler::ClusterVector_t::const_iterator iter = ClonedClusters.begin();
 | 
|---|
 | 131 |         iter != ClonedClusters.end(); ++iter) {
 | 
|---|
 | 132 |       const AtomIdSet &atoms = (*iter)->getAtomIds();
 | 
|---|
 | 133 |       clonedatoms.reserve(clonedatoms.size()+atoms.size());
 | 
|---|
 | 134 |       for (AtomIdSet::const_iterator atomiter = atoms.begin(); atomiter != atoms.end(); ++atomiter)
 | 
|---|
 | 135 |         if (!filler->containsAtom(*atomiter)) {
 | 
|---|
 | 136 |           clonedatoms.push_back( *atomiter );
 | 
|---|
 | 137 |           clonedatominfos.push_back( AtomicInfo(*(*atomiter)) );
 | 
|---|
 | 138 |         }
 | 
|---|
 | 139 |     }
 | 
|---|
 | 140 |     std::vector< BondInfo > clonedbonds;
 | 
|---|
 | 141 |     StoreBondInformationFromAtoms(clonedatoms, clonedbonds);
 | 
|---|
 | 142 |     LOG(2, "DEBUG: There are " << clonedatominfos.size() << " newly created atoms.");
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 |     if (!successflag) {
 | 
|---|
 | 145 |       STATUS("Insertion failed, removing inserted clusters, translating original one back");
 | 
|---|
 | 146 |       RemoveAtomsFromAtomicInfo(clonedatominfos);
 | 
|---|
 | 147 |       clonedatoms.clear();
 | 
|---|
 | 148 |       SetAtomsFromAtomicInfo(movedatoms);
 | 
|---|
 | 149 |     } else {
 | 
|---|
 | 150 |       std::vector<Vector> MovedToVector(filler->size(), zeroVec);
 | 
|---|
 | 151 |       std::transform(filler->begin(), filler->end(), MovedToVector.begin(),
 | 
|---|
 | 152 |           boost::bind(&AtomInfo::getPosition, _1) );
 | 
|---|
 | 153 |       UndoState = new FillRegularGridState(clonedatominfos,clonedbonds,movedatoms,MovedToVector,params);
 | 
|---|
| [e9ad43] | 154 |     }
 | 
|---|
| [a88452] | 155 |   }
 | 
|---|
 | 156 | 
 | 
|---|
| [68abe5] | 157 |   if (successflag)
 | 
|---|
| [b5b01e] | 158 |     return ActionState::ptr(UndoState);
 | 
|---|
| [26b4d62] | 159 |   else {
 | 
|---|
| [68abe5] | 160 |     return Action::failure;
 | 
|---|
| [26b4d62] | 161 |   }
 | 
|---|
| [a88452] | 162 | }
 | 
|---|
 | 163 | 
 | 
|---|
| [b5b01e] | 164 | ActionState::ptr FillRegularGridAction::performUndo(ActionState::ptr _state) {
 | 
|---|
| [42b6de] | 165 |   FillRegularGridState *state = assert_cast<FillRegularGridState*>(_state.get());
 | 
|---|
 | 166 | 
 | 
|---|
 | 167 |   // remove all created atoms
 | 
|---|
 | 168 |   RemoveAtomsFromAtomicInfo(state->clonedatoms);
 | 
|---|
| [80ca29] | 169 |   // add the original cluster
 | 
|---|
 | 170 |   SetAtomsFromAtomicInfo(state->movedatoms);
 | 
|---|
| [42b6de] | 171 | 
 | 
|---|
| [b5b01e] | 172 |   return ActionState::ptr(_state);
 | 
|---|
| [a88452] | 173 | }
 | 
|---|
 | 174 | 
 | 
|---|
| [b5b01e] | 175 | ActionState::ptr FillRegularGridAction::performRedo(ActionState::ptr _state){
 | 
|---|
| [42b6de] | 176 |   FillRegularGridState *state = assert_cast<FillRegularGridState*>(_state.get());
 | 
|---|
| [a88452] | 177 | 
 | 
|---|
| [80ca29] | 178 |   // place filler cluster again at new spot
 | 
|---|
 | 179 |   ResetAtomPosition(state->movedatoms, state->MovedToVector);
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 |   // re-create all clusters
 | 
|---|
 | 182 |   bool statusflag = AddAtomsFromAtomicInfo(state->clonedatoms);
 | 
|---|
 | 183 | 
 | 
|---|
 | 184 |   // re-create the bonds
 | 
|---|
 | 185 |   if (statusflag)
 | 
|---|
 | 186 |     AddBondsFromBondInfo(state->clonedbonds);
 | 
|---|
 | 187 |   if (statusflag)
 | 
|---|
| [b5b01e] | 188 |     return ActionState::ptr(_state);
 | 
|---|
| [26b4d62] | 189 |   else {
 | 
|---|
 | 190 |     STATUS("Failed re-adding filled in atoms.");
 | 
|---|
| [42b6de] | 191 |     return Action::failure;
 | 
|---|
| [26b4d62] | 192 |   }
 | 
|---|
| [a88452] | 193 | }
 | 
|---|
 | 194 | 
 | 
|---|
 | 195 | bool FillRegularGridAction::canUndo() {
 | 
|---|
| [42b6de] | 196 |   return true;
 | 
|---|
| [a88452] | 197 | }
 | 
|---|
 | 198 | 
 | 
|---|
 | 199 | bool FillRegularGridAction::shouldUndo() {
 | 
|---|
| [42b6de] | 200 |   return true;
 | 
|---|
| [a88452] | 201 | }
 | 
|---|
 | 202 | /** =========== end of function ====================== */
 | 
|---|