| [2db053] | 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/>.
 | 
|---|
| [2db053] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /*
 | 
|---|
| [09eaac] | 24 |  * FillSurfaceAction.cpp
 | 
|---|
| [2db053] | 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 | 
 | 
|---|
| [42b6de] | 37 | #include "Actions/UndoRedoHelpers.hpp"
 | 
|---|
| [80ca29] | 38 | #include "Atom/atom.hpp"
 | 
|---|
| [42b6de] | 39 | #include "Atom/AtomicInfo.hpp"
 | 
|---|
| [cb7676] | 40 | #include "Atom/CopyAtoms/CopyAtoms_withBonds.hpp"
 | 
|---|
| [2db053] | 41 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 42 | #include "Filling/Cluster.hpp"
 | 
|---|
 | 43 | #include "Filling/Filler.hpp"
 | 
|---|
 | 44 | #include "Filling/Inserter/Inserter.hpp"
 | 
|---|
| [cb7676] | 45 | #include "Filling/Inserter/SurfaceInserter.hpp"
 | 
|---|
| [2db053] | 46 | #include "Filling/Mesh/MeshAdaptor.hpp"
 | 
|---|
 | 47 | #include "Filling/Predicates/IsVoidNode_FillPredicate.hpp"
 | 
|---|
| [df9f20] | 48 | #include "Filling/Preparators/ShapeSurfaceFillerPreparator.hpp"
 | 
|---|
| [cb7676] | 49 | #include "molecule.hpp"
 | 
|---|
| [2db053] | 50 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
| [842aac] | 51 | #include "Shapes/ShapeRegistry.hpp"
 | 
|---|
| [df9f20] | 52 | #include "Shapes/ShapeType.hpp"
 | 
|---|
| [2db053] | 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 | 
 | 
|---|
| [09eaac] | 63 | #include "Actions/FillAction/FillSurfaceAction.hpp"
 | 
|---|
| [2db053] | 64 | 
 | 
|---|
 | 65 | using namespace MoleCuilder;
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 | // and construct the stuff
 | 
|---|
| [09eaac] | 68 | #include "FillSurfaceAction.def"
 | 
|---|
| [2db053] | 69 | #include "Action_impl_pre.hpp"
 | 
|---|
 | 70 | /** =========== define the function ====================== */
 | 
|---|
| [b5b01e] | 71 | ActionState::ptr FillSurfaceAction::performCall() {
 | 
|---|
| [80ca29] | 72 |   // get the filler molecule
 | 
|---|
| [cb7676] | 73 |   const std::vector< molecule *> molecules = World::getInstance().getSelectedMolecules();
 | 
|---|
| [80ca29] | 74 |   std::vector<AtomicInfo> movedatoms;
 | 
|---|
| [cb7676] | 75 |   if (molecules.size() != 1) {
 | 
|---|
| [26b4d62] | 76 |     STATUS("No exactly one molecule selected, aborting,");
 | 
|---|
| [2db053] | 77 |     return Action::failure;
 | 
|---|
 | 78 |   }
 | 
|---|
| [cb7676] | 79 |   molecule *filler = *(molecules.begin());
 | 
|---|
| [f01769] | 80 |   for(molecule::const_iterator iter = const_cast<const molecule *>(filler)->begin();
 | 
|---|
 | 81 |       iter != const_cast<const molecule *>(filler)->end(); ++iter)
 | 
|---|
| [80ca29] | 82 |     movedatoms.push_back( AtomicInfo(*(*iter)) );
 | 
|---|
| [cb7676] | 83 |   LOG(1, "INFO: Chosen molecule has " << filler->size() << " atoms.");
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 |   // center filler's tip at origin
 | 
|---|
| [833b15] | 86 |   filler->CenterEdge();
 | 
|---|
| [cb7676] | 87 | 
 | 
|---|
 | 88 |   // determine center with respect to alignment axis
 | 
|---|
 | 89 |   Vector sum = zeroVec;
 | 
|---|
| [f01769] | 90 |   for (molecule::const_iterator it2=const_cast<const molecule *>(filler)->begin();
 | 
|---|
 | 91 |       it2 !=const_cast<const molecule *>(filler)->end();++it2) {
 | 
|---|
| [f10b0c] | 92 |     const Vector helper = (**it2).getPosition().partition(params.AlignedAxis.get()).second;
 | 
|---|
| [cb7676] | 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;
 | 
|---|
| [833b15] | 101 |     filler->Translate(translater);
 | 
|---|
| [2db053] | 102 |   }
 | 
|---|
 | 103 | 
 | 
|---|
| [df9f20] | 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 |   }
 | 
|---|
| [842aac] | 123 | 
 | 
|---|
| [df9f20] | 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.");
 | 
|---|
| [842aac] | 129 |       return Action::failure;
 | 
|---|
 | 130 |     }
 | 
|---|
| [df9f20] | 131 |     std::vector<atom*> atoms(World::getInstance().getSelectedAtoms());
 | 
|---|
 | 132 |     filler_preparator.addSurfacePredicate(
 | 
|---|
 | 133 |         params.SphereRadius.get(),
 | 
|---|
| [df0c80] | 134 |         atoms,
 | 
|---|
 | 135 |         params.mindistance.get());
 | 
|---|
| [df9f20] | 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 |   }
 | 
|---|
| [09eaac] | 150 | 
 | 
|---|
| [df9f20] | 151 |   // use filler
 | 
|---|
 | 152 |   bool successflag = false;
 | 
|---|
 | 153 |   FillSurfaceState *UndoState = NULL;
 | 
|---|
 | 154 |   {
 | 
|---|
| [2db053] | 155 |     // fill
 | 
|---|
 | 156 |     {
 | 
|---|
| [df9f20] | 157 |       Filler *fillerFunction = filler_preparator.obtainFiller();
 | 
|---|
| [aeb694] | 158 |       ClusterInterface::Cluster_impl cluster( new Cluster( filler->getAtomIds(), filler->getBoundingSphere() ) );
 | 
|---|
| [cb7676] | 159 |       CopyAtoms_withBonds copyMethod;
 | 
|---|
| [42b6de] | 160 |       Filler::ClusterVector_t ClonedClusters;
 | 
|---|
 | 161 |       successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters);
 | 
|---|
| [2db053] | 162 |       delete fillerFunction;
 | 
|---|
| [42b6de] | 163 | 
 | 
|---|
| [80ca29] | 164 |       // append each cluster's atoms to clonedatoms (however not selected ones)
 | 
|---|
 | 165 |       std::vector<const atom *> clonedatoms;
 | 
|---|
 | 166 |       std::vector<AtomicInfo> clonedatominfos;
 | 
|---|
| [42b6de] | 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)
 | 
|---|
| [80ca29] | 172 |           if (!filler->containsAtom(*atomiter)) {
 | 
|---|
 | 173 |             clonedatoms.push_back( *atomiter );
 | 
|---|
 | 174 |             clonedatominfos.push_back( AtomicInfo(*(*atomiter)) );
 | 
|---|
 | 175 |           }
 | 
|---|
| [42b6de] | 176 |       }
 | 
|---|
| [80ca29] | 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 | 
 | 
|---|
| [42b6de] | 182 |       if (!successflag) {
 | 
|---|
| [26b4d62] | 183 |         STATUS("Insertion failed, removing inserted clusters, translating original one back");
 | 
|---|
| [80ca29] | 184 |         RemoveAtomsFromAtomicInfo(clonedatominfos);
 | 
|---|
| [42b6de] | 185 |         clonedatoms.clear();
 | 
|---|
| [80ca29] | 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) );
 | 
|---|
| [09eaac] | 191 |         UndoState = new FillSurfaceState(clonedatominfos,clonedbonds,movedatoms,MovedToVector,params);
 | 
|---|
| [42b6de] | 192 |       }
 | 
|---|
| [2db053] | 193 |     }
 | 
|---|
 | 194 |   }
 | 
|---|
 | 195 | 
 | 
|---|
 | 196 |   if (successflag)
 | 
|---|
| [b5b01e] | 197 |     return ActionState::ptr(UndoState);
 | 
|---|
| [2db053] | 198 |   else
 | 
|---|
 | 199 |     return Action::failure;
 | 
|---|
 | 200 | }
 | 
|---|
 | 201 | 
 | 
|---|
| [b5b01e] | 202 | ActionState::ptr FillSurfaceAction::performUndo(ActionState::ptr _state) {
 | 
|---|
| [09eaac] | 203 |   FillSurfaceState *state = assert_cast<FillSurfaceState*>(_state.get());
 | 
|---|
| [42b6de] | 204 | 
 | 
|---|
 | 205 |   // remove all created atoms
 | 
|---|
 | 206 |   RemoveAtomsFromAtomicInfo(state->clonedatoms);
 | 
|---|
| [80ca29] | 207 |   // add the original cluster
 | 
|---|
 | 208 |   SetAtomsFromAtomicInfo(state->movedatoms);
 | 
|---|
| [42b6de] | 209 | 
 | 
|---|
| [b5b01e] | 210 |   return ActionState::ptr(_state);
 | 
|---|
| [2db053] | 211 | }
 | 
|---|
 | 212 | 
 | 
|---|
| [b5b01e] | 213 | ActionState::ptr FillSurfaceAction::performRedo(ActionState::ptr _state){
 | 
|---|
| [09eaac] | 214 |   FillSurfaceState *state = assert_cast<FillSurfaceState*>(_state.get());
 | 
|---|
| [42b6de] | 215 | 
 | 
|---|
| [80ca29] | 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)
 | 
|---|
| [b5b01e] | 225 |     return ActionState::ptr(_state);
 | 
|---|
| [26b4d62] | 226 |   else {
 | 
|---|
 | 227 |     STATUS("Failed to re-added filled in atoms.");
 | 
|---|
| [42b6de] | 228 |     return Action::failure;
 | 
|---|
| [26b4d62] | 229 |   }
 | 
|---|
| [2db053] | 230 | }
 | 
|---|
 | 231 | 
 | 
|---|
| [09eaac] | 232 | bool FillSurfaceAction::canUndo() {
 | 
|---|
| [42b6de] | 233 |   return true;
 | 
|---|
| [2db053] | 234 | }
 | 
|---|
 | 235 | 
 | 
|---|
| [09eaac] | 236 | bool FillSurfaceAction::shouldUndo() {
 | 
|---|
| [42b6de] | 237 |   return true;
 | 
|---|
| [2db053] | 238 | }
 | 
|---|
 | 239 | /** =========== end of function ====================== */
 | 
|---|