| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 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/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * FillSurfaceAction.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Mar 29, 2012
 | 
|---|
| 27 |  *      Author: heber, bollerhe
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include "Actions/UndoRedoHelpers.hpp"
 | 
|---|
| 38 | #include "Atom/atom.hpp"
 | 
|---|
| 39 | #include "Atom/AtomicInfo.hpp"
 | 
|---|
| 40 | #include "Atom/CopyAtoms/CopyAtoms_withBonds.hpp"
 | 
|---|
| 41 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 42 | #include "Filling/Cluster.hpp"
 | 
|---|
| 43 | #include "Filling/Filler.hpp"
 | 
|---|
| 44 | #include "Filling/Inserter/Inserter.hpp"
 | 
|---|
| 45 | #include "Filling/Inserter/SurfaceInserter.hpp"
 | 
|---|
| 46 | #include "Filling/Mesh/MeshAdaptor.hpp"
 | 
|---|
| 47 | #include "Filling/Predicates/IsVoidNode_FillPredicate.hpp"
 | 
|---|
| 48 | #include "Filling/Preparators/ShapeSurfaceFillerPreparator.hpp"
 | 
|---|
| 49 | #include "molecule.hpp"
 | 
|---|
| 50 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
| 51 | #include "Shapes/ShapeRegistry.hpp"
 | 
|---|
| 52 | #include "Shapes/ShapeType.hpp"
 | 
|---|
| 53 | #include "World.hpp"
 | 
|---|
| 54 | 
 | 
|---|
| 55 | 
 | 
|---|
| 56 | #include <algorithm>
 | 
|---|
| 57 | #include <boost/foreach.hpp>
 | 
|---|
| 58 | #include <boost/function.hpp>
 | 
|---|
| 59 | #include <iostream>
 | 
|---|
| 60 | #include <string>
 | 
|---|
| 61 | #include <vector>
 | 
|---|
| 62 | 
 | 
|---|
| 63 | #include "Actions/FillAction/FillSurfaceAction.hpp"
 | 
|---|
| 64 | 
 | 
|---|
| 65 | using namespace MoleCuilder;
 | 
|---|
| 66 | 
 | 
|---|
| 67 | // and construct the stuff
 | 
|---|
| 68 | #include "FillSurfaceAction.def"
 | 
|---|
| 69 | #include "Action_impl_pre.hpp"
 | 
|---|
| 70 | /** =========== define the function ====================== */
 | 
|---|
| 71 | ActionState::ptr FillSurfaceAction::performCall() {
 | 
|---|
| 72 |   // get the filler molecule
 | 
|---|
| 73 |   const std::vector< molecule *> molecules = World::getInstance().getSelectedMolecules();
 | 
|---|
| 74 |   std::vector<AtomicInfo> movedatoms;
 | 
|---|
| 75 |   if (molecules.size() != 1) {
 | 
|---|
| 76 |     STATUS("No exactly one molecule selected, aborting,");
 | 
|---|
| 77 |     return Action::failure;
 | 
|---|
| 78 |   }
 | 
|---|
| 79 |   molecule *filler = *(molecules.begin());
 | 
|---|
| 80 |   for(molecule::const_iterator iter = const_cast<const molecule *>(filler)->begin();
 | 
|---|
| 81 |       iter != const_cast<const molecule *>(filler)->end(); ++iter)
 | 
|---|
| 82 |     movedatoms.push_back( AtomicInfo(*(*iter)) );
 | 
|---|
| 83 |   LOG(1, "INFO: Chosen molecule has " << filler->size() << " atoms.");
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   // center filler's tip at origin
 | 
|---|
| 86 |   filler->CenterEdge();
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   // determine center with respect to alignment axis
 | 
|---|
| 89 |   Vector sum = zeroVec;
 | 
|---|
| 90 |   for (molecule::const_iterator it2=const_cast<const molecule *>(filler)->begin();
 | 
|---|
| 91 |       it2 !=const_cast<const molecule *>(filler)->end();++it2) {
 | 
|---|
| 92 |     const Vector helper = (**it2).getPosition().partition(params.AlignedAxis.get()).second;
 | 
|---|
| 93 |     sum += helper;
 | 
|---|
| 94 |   }
 | 
|---|
| 95 |   sum *= 1./filler->size();
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   // translate molecule's closest atom to origin (such that is resides on the filler spot)
 | 
|---|
| 98 |   LOG(1, "DEBUG: molecule is off Alignment axis by " << sum << ", shifting ...");
 | 
|---|
| 99 |   {
 | 
|---|
| 100 |     Vector translater = -1.*sum;
 | 
|---|
| 101 |     filler->Translate(translater);
 | 
|---|
| 102 |   }
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   // prepare the filler preparator
 | 
|---|
| 105 |   if (ShapeRegistry::getInstance().countSelectedShapes() != (size_t)1) {
 | 
|---|
| 106 |     STATUS("Not exactly one shape selected.");
 | 
|---|
| 107 |     return Action::failure;
 | 
|---|
| 108 |   }
 | 
|---|
| 109 |   const std::vector<Shape*> shapes = ShapeRegistry::getInstance().getSelectedShapes();
 | 
|---|
| 110 |   const Shape &shape = **shapes.begin();
 | 
|---|
| 111 | 
 | 
|---|
| 112 |   // hard check whether shape is of allowed type, not all are implemented
 | 
|---|
| 113 |   // but these only fail with an assertion, hence not with disable-debug
 | 
|---|
| 114 |   switch (shape.getType()) {
 | 
|---|
| 115 |     case NowhereType:
 | 
|---|
| 116 |     case EverywhereType:
 | 
|---|
| 117 |       STATUS("The shape type "+toString(shape.getType())+" is currently not supported.");
 | 
|---|
| 118 |       return Action::failure;
 | 
|---|
| 119 |       break;
 | 
|---|
| 120 |     default:
 | 
|---|
| 121 |       break;
 | 
|---|
| 122 |   }
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   ShapeSurfaceFillerPreparator filler_preparator(filler);
 | 
|---|
| 125 |   if (params.SphereRadius.get() != 0.) {
 | 
|---|
| 126 |     if (World::getInstance().beginAtomSelection() == World::getInstance().endAtomSelection()) {
 | 
|---|
| 127 |       STATUS("You have given a sphere radius "+toString(params.SphereRadius.get())
 | 
|---|
| 128 |           +" != 0, but have not select any atoms.");
 | 
|---|
| 129 |       return Action::failure;
 | 
|---|
| 130 |     }
 | 
|---|
| 131 |     std::vector<atom*> atoms(World::getInstance().getSelectedAtoms());
 | 
|---|
| 132 |     filler_preparator.addSurfacePredicate(
 | 
|---|
| 133 |         params.SphereRadius.get(),
 | 
|---|
| 134 |         atoms,
 | 
|---|
| 135 |         params.mindistance.get());
 | 
|---|
| 136 |   }
 | 
|---|
| 137 |   filler_preparator.addVoidPredicate(params.mindistance.get());
 | 
|---|
| 138 |   filler_preparator.addSurfaceRandomInserter(
 | 
|---|
| 139 |       shape,
 | 
|---|
| 140 |       params.AlignedAxis.get(),
 | 
|---|
| 141 |       params.RandAtomDisplacement.get(),
 | 
|---|
| 142 |       params.RandMoleculeDisplacement.get());
 | 
|---|
| 143 |   filler_preparator.addShapeMesh(
 | 
|---|
| 144 |       shape,
 | 
|---|
| 145 |       params.N.get());
 | 
|---|
| 146 |   if (!filler_preparator()) {
 | 
|---|
| 147 |     STATUS("Filler was not fully constructed.");
 | 
|---|
| 148 |     return Action::failure;
 | 
|---|
| 149 |   }
 | 
|---|
| 150 | 
 | 
|---|
| 151 |   // use filler
 | 
|---|
| 152 |   bool successflag = false;
 | 
|---|
| 153 |   FillSurfaceState *UndoState = NULL;
 | 
|---|
| 154 |   {
 | 
|---|
| 155 |     // fill
 | 
|---|
| 156 |     {
 | 
|---|
| 157 |       Filler *fillerFunction = filler_preparator.obtainFiller();
 | 
|---|
| 158 |       ClusterInterface::Cluster_impl cluster( new Cluster( filler->getAtomIds(), filler->getBoundingSphere() ) );
 | 
|---|
| 159 |       CopyAtoms_withBonds copyMethod;
 | 
|---|
| 160 |       Filler::ClusterVector_t ClonedClusters;
 | 
|---|
| 161 |       successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters);
 | 
|---|
| 162 |       delete fillerFunction;
 | 
|---|
| 163 | 
 | 
|---|
| 164 |       // append each cluster's atoms to clonedatoms (however not selected ones)
 | 
|---|
| 165 |       std::vector<const atom *> clonedatoms;
 | 
|---|
| 166 |       std::vector<AtomicInfo> clonedatominfos;
 | 
|---|
| 167 |       for (Filler::ClusterVector_t::const_iterator iter = ClonedClusters.begin();
 | 
|---|
| 168 |           iter != ClonedClusters.end(); ++iter) {
 | 
|---|
| 169 |         const AtomIdSet &atoms = (*iter)->getAtomIds();
 | 
|---|
| 170 |         clonedatoms.reserve(clonedatoms.size()+atoms.size());
 | 
|---|
| 171 |         for (AtomIdSet::const_iterator atomiter = atoms.begin(); atomiter != atoms.end(); ++atomiter)
 | 
|---|
| 172 |           if (!filler->containsAtom(*atomiter)) {
 | 
|---|
| 173 |             clonedatoms.push_back( *atomiter );
 | 
|---|
| 174 |             clonedatominfos.push_back( AtomicInfo(*(*atomiter)) );
 | 
|---|
| 175 |           }
 | 
|---|
| 176 |       }
 | 
|---|
| 177 |       std::vector< BondInfo > clonedbonds;
 | 
|---|
| 178 |       StoreBondInformationFromAtoms(clonedatoms, clonedbonds);
 | 
|---|
| 179 |       LOG(2, "DEBUG: There are " << clonedatominfos.size() << " newly created atoms with "
 | 
|---|
| 180 |           << clonedbonds.size()/2 << " bonds.");
 | 
|---|
| 181 | 
 | 
|---|
| 182 |       if (!successflag) {
 | 
|---|
| 183 |         STATUS("Insertion failed, removing inserted clusters, translating original one back");
 | 
|---|
| 184 |         RemoveAtomsFromAtomicInfo(clonedatominfos);
 | 
|---|
| 185 |         clonedatoms.clear();
 | 
|---|
| 186 |         SetAtomsFromAtomicInfo(movedatoms);
 | 
|---|
| 187 |       } else {
 | 
|---|
| 188 |         std::vector<Vector> MovedToVector(filler->size(), zeroVec);
 | 
|---|
| 189 |         std::transform(filler->begin(), filler->end(), MovedToVector.begin(),
 | 
|---|
| 190 |             boost::bind(&AtomInfo::getPosition, _1) );
 | 
|---|
| 191 |         UndoState = new FillSurfaceState(clonedatominfos,clonedbonds,movedatoms,MovedToVector,params);
 | 
|---|
| 192 |       }
 | 
|---|
| 193 |     }
 | 
|---|
| 194 |   }
 | 
|---|
| 195 | 
 | 
|---|
| 196 |   if (successflag)
 | 
|---|
| 197 |     return ActionState::ptr(UndoState);
 | 
|---|
| 198 |   else
 | 
|---|
| 199 |     return Action::failure;
 | 
|---|
| 200 | }
 | 
|---|
| 201 | 
 | 
|---|
| 202 | ActionState::ptr FillSurfaceAction::performUndo(ActionState::ptr _state) {
 | 
|---|
| 203 |   FillSurfaceState *state = assert_cast<FillSurfaceState*>(_state.get());
 | 
|---|
| 204 | 
 | 
|---|
| 205 |   // remove all created atoms
 | 
|---|
| 206 |   RemoveAtomsFromAtomicInfo(state->clonedatoms);
 | 
|---|
| 207 |   // add the original cluster
 | 
|---|
| 208 |   SetAtomsFromAtomicInfo(state->movedatoms);
 | 
|---|
| 209 | 
 | 
|---|
| 210 |   return ActionState::ptr(_state);
 | 
|---|
| 211 | }
 | 
|---|
| 212 | 
 | 
|---|
| 213 | ActionState::ptr FillSurfaceAction::performRedo(ActionState::ptr _state){
 | 
|---|
| 214 |   FillSurfaceState *state = assert_cast<FillSurfaceState*>(_state.get());
 | 
|---|
| 215 | 
 | 
|---|
| 216 |   // place filler cluster again at new spot
 | 
|---|
| 217 |   ResetAtomPosition(state->movedatoms, state->MovedToVector);
 | 
|---|
| 218 | 
 | 
|---|
| 219 |   // re-create all clusters
 | 
|---|
| 220 |   bool statusflag = AddAtomsFromAtomicInfo(state->clonedatoms);
 | 
|---|
| 221 | 
 | 
|---|
| 222 |   // re-create the bonds
 | 
|---|
| 223 |   statusflag = statusflag && AddBondsFromBondInfo(state->clonedbonds);
 | 
|---|
| 224 |   if (statusflag)
 | 
|---|
| 225 |     return ActionState::ptr(_state);
 | 
|---|
| 226 |   else {
 | 
|---|
| 227 |     STATUS("Failed to re-added filled in atoms.");
 | 
|---|
| 228 |     return Action::failure;
 | 
|---|
| 229 |   }
 | 
|---|
| 230 | }
 | 
|---|
| 231 | 
 | 
|---|
| 232 | bool FillSurfaceAction::canUndo() {
 | 
|---|
| 233 |   return true;
 | 
|---|
| 234 | }
 | 
|---|
| 235 | 
 | 
|---|
| 236 | bool FillSurfaceAction::shouldUndo() {
 | 
|---|
| 237 |   return true;
 | 
|---|
| 238 | }
 | 
|---|
| 239 | /** =========== end of function ====================== */
 | 
|---|