| [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"
 | 
|---|
| [cb7676] | 48 | #include "molecule.hpp"
 | 
|---|
| [2db053] | 49 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
| [842aac] | 50 | #include "Shapes/ShapeRegistry.hpp"
 | 
|---|
| [2db053] | 51 | #include "World.hpp"
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | #include <algorithm>
 | 
|---|
 | 55 | #include <boost/foreach.hpp>
 | 
|---|
 | 56 | #include <boost/function.hpp>
 | 
|---|
 | 57 | #include <iostream>
 | 
|---|
 | 58 | #include <string>
 | 
|---|
 | 59 | #include <vector>
 | 
|---|
 | 60 | 
 | 
|---|
| [09eaac] | 61 | #include "Actions/FillAction/FillSurfaceAction.hpp"
 | 
|---|
| [2db053] | 62 | 
 | 
|---|
 | 63 | using namespace MoleCuilder;
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 | // and construct the stuff
 | 
|---|
| [09eaac] | 66 | #include "FillSurfaceAction.def"
 | 
|---|
| [2db053] | 67 | #include "Action_impl_pre.hpp"
 | 
|---|
 | 68 | /** =========== define the function ====================== */
 | 
|---|
| [b5b01e] | 69 | ActionState::ptr FillSurfaceAction::performCall() {
 | 
|---|
| [80ca29] | 70 |   // get the filler molecule
 | 
|---|
| [cb7676] | 71 |   const std::vector< molecule *> molecules = World::getInstance().getSelectedMolecules();
 | 
|---|
| [80ca29] | 72 |   std::vector<AtomicInfo> movedatoms;
 | 
|---|
| [cb7676] | 73 |   if (molecules.size() != 1) {
 | 
|---|
 | 74 |     ELOG(1, "No exactly one molecule selected, aborting,");
 | 
|---|
| [2db053] | 75 |     return Action::failure;
 | 
|---|
 | 76 |   }
 | 
|---|
| [cb7676] | 77 |   molecule *filler = *(molecules.begin());
 | 
|---|
| [80ca29] | 78 |   for(molecule::const_iterator iter = filler->begin(); iter != filler->end(); ++iter)
 | 
|---|
 | 79 |     movedatoms.push_back( AtomicInfo(*(*iter)) );
 | 
|---|
| [cb7676] | 80 |   LOG(1, "INFO: Chosen molecule has " << filler->size() << " atoms.");
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 |   // center filler's tip at origin
 | 
|---|
 | 83 |   Vector max;
 | 
|---|
 | 84 |   filler->CenterEdge(&max);
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |   // determine center with respect to alignment axis
 | 
|---|
 | 87 |   Vector sum = zeroVec;
 | 
|---|
 | 88 |   for (molecule::iterator it2=filler->begin();it2 !=filler->end();++it2) {
 | 
|---|
| [f10b0c] | 89 |     const Vector helper = (**it2).getPosition().partition(params.AlignedAxis.get()).second;
 | 
|---|
| [cb7676] | 90 |     sum += helper;
 | 
|---|
 | 91 |   }
 | 
|---|
 | 92 |   sum *= 1./filler->size();
 | 
|---|
 | 93 | 
 | 
|---|
 | 94 |   // translate molecule's closest atom to origin (such that is resides on the filler spot)
 | 
|---|
 | 95 |   LOG(1, "DEBUG: molecule is off Alignment axis by " << sum << ", shifting ...");
 | 
|---|
 | 96 |   {
 | 
|---|
 | 97 |     Vector translater = -1.*sum;
 | 
|---|
 | 98 |     filler->Translate(&translater);
 | 
|---|
| [2db053] | 99 |   }
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 |   // create predicate, mesh, and filler
 | 
|---|
| [09eaac] | 102 |   FillSurfaceState *UndoState = NULL;
 | 
|---|
| [2db053] | 103 |   bool successflag = false;
 | 
|---|
 | 104 |   {
 | 
|---|
 | 105 |     FillPredicate *voidnode_predicate = new FillPredicate(
 | 
|---|
 | 106 |         IsVoidNode_FillPredicate(
 | 
|---|
| [f10b0c] | 107 |             Sphere(zeroVec, params.mindistance.get())
 | 
|---|
| [2db053] | 108 |             )
 | 
|---|
 | 109 |         );
 | 
|---|
| [09eaac] | 110 | 
 | 
|---|
| [842aac] | 111 | 
 | 
|---|
 | 112 |     std::vector<Shape*> selectedShapes = ShapeRegistry::getInstance().getSelectedShapes();
 | 
|---|
 | 113 |     if (selectedShapes.size() != 1){
 | 
|---|
 | 114 |       ELOG(1, "FillSurfaceAction::performCall(): there has to be exactly 1 selected shape.");
 | 
|---|
 | 115 |       return Action::failure;
 | 
|---|
 | 116 |     }
 | 
|---|
| [09eaac] | 117 | 
 | 
|---|
| [2db053] | 118 |     boost::function<const NodeSet ()> func = 
 | 
|---|
| [842aac] | 119 |         boost::bind(&Shape::getHomogeneousPointsOnSurface, boost::ref(*selectedShapes[0]), params.N.get());
 | 
|---|
| [2db053] | 120 |     Mesh *mesh = new MeshAdaptor(func);
 | 
|---|
 | 121 |     Inserter *inserter = new Inserter(
 | 
|---|
| [842aac] | 122 |         Inserter::impl_ptr(new SurfaceInserter(*selectedShapes[0], params.AlignedAxis.get())));
 | 
|---|
| [2db053] | 123 | 
 | 
|---|
 | 124 |     // fill
 | 
|---|
 | 125 |     {
 | 
|---|
 | 126 |       Filler *fillerFunction = new Filler(*mesh, *voidnode_predicate, *inserter);
 | 
|---|
| [aeb694] | 127 |       ClusterInterface::Cluster_impl cluster( new Cluster( filler->getAtomIds(), filler->getBoundingSphere() ) );
 | 
|---|
| [cb7676] | 128 |       CopyAtoms_withBonds copyMethod;
 | 
|---|
| [42b6de] | 129 |       Filler::ClusterVector_t ClonedClusters;
 | 
|---|
 | 130 |       successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters);
 | 
|---|
| [2db053] | 131 |       delete fillerFunction;
 | 
|---|
| [42b6de] | 132 | 
 | 
|---|
| [80ca29] | 133 |       // append each cluster's atoms to clonedatoms (however not selected ones)
 | 
|---|
 | 134 |       std::vector<const atom *> clonedatoms;
 | 
|---|
 | 135 |       std::vector<AtomicInfo> clonedatominfos;
 | 
|---|
| [42b6de] | 136 |       for (Filler::ClusterVector_t::const_iterator iter = ClonedClusters.begin();
 | 
|---|
 | 137 |           iter != ClonedClusters.end(); ++iter) {
 | 
|---|
 | 138 |         const AtomIdSet &atoms = (*iter)->getAtomIds();
 | 
|---|
 | 139 |         clonedatoms.reserve(clonedatoms.size()+atoms.size());
 | 
|---|
 | 140 |         for (AtomIdSet::const_iterator atomiter = atoms.begin(); atomiter != atoms.end(); ++atomiter)
 | 
|---|
| [80ca29] | 141 |           if (!filler->containsAtom(*atomiter)) {
 | 
|---|
 | 142 |             clonedatoms.push_back( *atomiter );
 | 
|---|
 | 143 |             clonedatominfos.push_back( AtomicInfo(*(*atomiter)) );
 | 
|---|
 | 144 |           }
 | 
|---|
| [42b6de] | 145 |       }
 | 
|---|
| [80ca29] | 146 |       std::vector< BondInfo > clonedbonds;
 | 
|---|
 | 147 |       StoreBondInformationFromAtoms(clonedatoms, clonedbonds);
 | 
|---|
 | 148 |       LOG(2, "DEBUG: There are " << clonedatominfos.size() << " newly created atoms with "
 | 
|---|
 | 149 |           << clonedbonds.size()/2 << " bonds.");
 | 
|---|
 | 150 | 
 | 
|---|
| [42b6de] | 151 |       if (!successflag) {
 | 
|---|
| [80ca29] | 152 |         ELOG(1, "Insertion failed, removing inserted clusters, translating original one back");
 | 
|---|
 | 153 |         RemoveAtomsFromAtomicInfo(clonedatominfos);
 | 
|---|
| [42b6de] | 154 |         clonedatoms.clear();
 | 
|---|
| [80ca29] | 155 |         SetAtomsFromAtomicInfo(movedatoms);
 | 
|---|
 | 156 |       } else {
 | 
|---|
 | 157 |         std::vector<Vector> MovedToVector(filler->size(), zeroVec);
 | 
|---|
 | 158 |         std::transform(filler->begin(), filler->end(), MovedToVector.begin(),
 | 
|---|
 | 159 |             boost::bind(&AtomInfo::getPosition, _1) );
 | 
|---|
| [09eaac] | 160 |         UndoState = new FillSurfaceState(clonedatominfos,clonedbonds,movedatoms,MovedToVector,params);
 | 
|---|
| [42b6de] | 161 |       }
 | 
|---|
| [2db053] | 162 |     }
 | 
|---|
 | 163 | 
 | 
|---|
 | 164 |     // remove
 | 
|---|
 | 165 |     delete mesh;
 | 
|---|
 | 166 |     delete inserter;
 | 
|---|
 | 167 |     delete voidnode_predicate;
 | 
|---|
 | 168 |   }
 | 
|---|
 | 169 | 
 | 
|---|
 | 170 |   if (successflag)
 | 
|---|
| [b5b01e] | 171 |     return ActionState::ptr(UndoState);
 | 
|---|
| [2db053] | 172 |   else
 | 
|---|
 | 173 |     return Action::failure;
 | 
|---|
 | 174 | }
 | 
|---|
 | 175 | 
 | 
|---|
| [b5b01e] | 176 | ActionState::ptr FillSurfaceAction::performUndo(ActionState::ptr _state) {
 | 
|---|
| [09eaac] | 177 |   FillSurfaceState *state = assert_cast<FillSurfaceState*>(_state.get());
 | 
|---|
| [42b6de] | 178 | 
 | 
|---|
 | 179 |   // remove all created atoms
 | 
|---|
 | 180 |   RemoveAtomsFromAtomicInfo(state->clonedatoms);
 | 
|---|
| [80ca29] | 181 |   // add the original cluster
 | 
|---|
 | 182 |   SetAtomsFromAtomicInfo(state->movedatoms);
 | 
|---|
| [42b6de] | 183 | 
 | 
|---|
| [b5b01e] | 184 |   return ActionState::ptr(_state);
 | 
|---|
| [2db053] | 185 | }
 | 
|---|
 | 186 | 
 | 
|---|
| [b5b01e] | 187 | ActionState::ptr FillSurfaceAction::performRedo(ActionState::ptr _state){
 | 
|---|
| [09eaac] | 188 |   FillSurfaceState *state = assert_cast<FillSurfaceState*>(_state.get());
 | 
|---|
| [42b6de] | 189 | 
 | 
|---|
| [80ca29] | 190 |   // place filler cluster again at new spot
 | 
|---|
 | 191 |   ResetAtomPosition(state->movedatoms, state->MovedToVector);
 | 
|---|
 | 192 | 
 | 
|---|
 | 193 |   // re-create all clusters
 | 
|---|
 | 194 |   bool statusflag = AddAtomsFromAtomicInfo(state->clonedatoms);
 | 
|---|
 | 195 | 
 | 
|---|
 | 196 |   // re-create the bonds
 | 
|---|
 | 197 |   statusflag = statusflag && AddBondsFromBondInfo(state->clonedbonds);
 | 
|---|
 | 198 |   if (statusflag)
 | 
|---|
| [b5b01e] | 199 |     return ActionState::ptr(_state);
 | 
|---|
| [42b6de] | 200 |   else
 | 
|---|
 | 201 |     return Action::failure;
 | 
|---|
| [2db053] | 202 | }
 | 
|---|
 | 203 | 
 | 
|---|
| [09eaac] | 204 | bool FillSurfaceAction::canUndo() {
 | 
|---|
| [42b6de] | 205 |   return true;
 | 
|---|
| [2db053] | 206 | }
 | 
|---|
 | 207 | 
 | 
|---|
| [09eaac] | 208 | bool FillSurfaceAction::shouldUndo() {
 | 
|---|
| [42b6de] | 209 |   return true;
 | 
|---|
| [2db053] | 210 | }
 | 
|---|
 | 211 | /** =========== end of function ====================== */
 | 
|---|