[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(),
|
---|
| 134 | atoms);
|
---|
| 135 | }
|
---|
| 136 | filler_preparator.addVoidPredicate(params.mindistance.get());
|
---|
| 137 | filler_preparator.addSurfaceRandomInserter(
|
---|
| 138 | shape,
|
---|
| 139 | params.AlignedAxis.get(),
|
---|
| 140 | params.RandAtomDisplacement.get(),
|
---|
| 141 | params.RandMoleculeDisplacement.get());
|
---|
| 142 | filler_preparator.addShapeMesh(
|
---|
| 143 | shape,
|
---|
| 144 | params.N.get());
|
---|
| 145 | if (!filler_preparator()) {
|
---|
| 146 | STATUS("Filler was not fully constructed.");
|
---|
| 147 | return Action::failure;
|
---|
| 148 | }
|
---|
[09eaac] | 149 |
|
---|
[df9f20] | 150 | // use filler
|
---|
| 151 | bool successflag = false;
|
---|
| 152 | FillSurfaceState *UndoState = NULL;
|
---|
| 153 | {
|
---|
[2db053] | 154 | // fill
|
---|
| 155 | {
|
---|
[df9f20] | 156 | Filler *fillerFunction = filler_preparator.obtainFiller();
|
---|
[aeb694] | 157 | ClusterInterface::Cluster_impl cluster( new Cluster( filler->getAtomIds(), filler->getBoundingSphere() ) );
|
---|
[cb7676] | 158 | CopyAtoms_withBonds copyMethod;
|
---|
[42b6de] | 159 | Filler::ClusterVector_t ClonedClusters;
|
---|
| 160 | successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters);
|
---|
[2db053] | 161 | delete fillerFunction;
|
---|
[42b6de] | 162 |
|
---|
[80ca29] | 163 | // append each cluster's atoms to clonedatoms (however not selected ones)
|
---|
| 164 | std::vector<const atom *> clonedatoms;
|
---|
| 165 | std::vector<AtomicInfo> clonedatominfos;
|
---|
[42b6de] | 166 | for (Filler::ClusterVector_t::const_iterator iter = ClonedClusters.begin();
|
---|
| 167 | iter != ClonedClusters.end(); ++iter) {
|
---|
| 168 | const AtomIdSet &atoms = (*iter)->getAtomIds();
|
---|
| 169 | clonedatoms.reserve(clonedatoms.size()+atoms.size());
|
---|
| 170 | for (AtomIdSet::const_iterator atomiter = atoms.begin(); atomiter != atoms.end(); ++atomiter)
|
---|
[80ca29] | 171 | if (!filler->containsAtom(*atomiter)) {
|
---|
| 172 | clonedatoms.push_back( *atomiter );
|
---|
| 173 | clonedatominfos.push_back( AtomicInfo(*(*atomiter)) );
|
---|
| 174 | }
|
---|
[42b6de] | 175 | }
|
---|
[80ca29] | 176 | std::vector< BondInfo > clonedbonds;
|
---|
| 177 | StoreBondInformationFromAtoms(clonedatoms, clonedbonds);
|
---|
| 178 | LOG(2, "DEBUG: There are " << clonedatominfos.size() << " newly created atoms with "
|
---|
| 179 | << clonedbonds.size()/2 << " bonds.");
|
---|
| 180 |
|
---|
[42b6de] | 181 | if (!successflag) {
|
---|
[26b4d62] | 182 | STATUS("Insertion failed, removing inserted clusters, translating original one back");
|
---|
[80ca29] | 183 | RemoveAtomsFromAtomicInfo(clonedatominfos);
|
---|
[42b6de] | 184 | clonedatoms.clear();
|
---|
[80ca29] | 185 | SetAtomsFromAtomicInfo(movedatoms);
|
---|
| 186 | } else {
|
---|
| 187 | std::vector<Vector> MovedToVector(filler->size(), zeroVec);
|
---|
| 188 | std::transform(filler->begin(), filler->end(), MovedToVector.begin(),
|
---|
| 189 | boost::bind(&AtomInfo::getPosition, _1) );
|
---|
[09eaac] | 190 | UndoState = new FillSurfaceState(clonedatominfos,clonedbonds,movedatoms,MovedToVector,params);
|
---|
[42b6de] | 191 | }
|
---|
[2db053] | 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | if (successflag)
|
---|
[b5b01e] | 196 | return ActionState::ptr(UndoState);
|
---|
[2db053] | 197 | else
|
---|
| 198 | return Action::failure;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[b5b01e] | 201 | ActionState::ptr FillSurfaceAction::performUndo(ActionState::ptr _state) {
|
---|
[09eaac] | 202 | FillSurfaceState *state = assert_cast<FillSurfaceState*>(_state.get());
|
---|
[42b6de] | 203 |
|
---|
| 204 | // remove all created atoms
|
---|
| 205 | RemoveAtomsFromAtomicInfo(state->clonedatoms);
|
---|
[80ca29] | 206 | // add the original cluster
|
---|
| 207 | SetAtomsFromAtomicInfo(state->movedatoms);
|
---|
[42b6de] | 208 |
|
---|
[b5b01e] | 209 | return ActionState::ptr(_state);
|
---|
[2db053] | 210 | }
|
---|
| 211 |
|
---|
[b5b01e] | 212 | ActionState::ptr FillSurfaceAction::performRedo(ActionState::ptr _state){
|
---|
[09eaac] | 213 | FillSurfaceState *state = assert_cast<FillSurfaceState*>(_state.get());
|
---|
[42b6de] | 214 |
|
---|
[80ca29] | 215 | // place filler cluster again at new spot
|
---|
| 216 | ResetAtomPosition(state->movedatoms, state->MovedToVector);
|
---|
| 217 |
|
---|
| 218 | // re-create all clusters
|
---|
| 219 | bool statusflag = AddAtomsFromAtomicInfo(state->clonedatoms);
|
---|
| 220 |
|
---|
| 221 | // re-create the bonds
|
---|
| 222 | statusflag = statusflag && AddBondsFromBondInfo(state->clonedbonds);
|
---|
| 223 | if (statusflag)
|
---|
[b5b01e] | 224 | return ActionState::ptr(_state);
|
---|
[26b4d62] | 225 | else {
|
---|
| 226 | STATUS("Failed to re-added filled in atoms.");
|
---|
[42b6de] | 227 | return Action::failure;
|
---|
[26b4d62] | 228 | }
|
---|
[2db053] | 229 | }
|
---|
| 230 |
|
---|
[09eaac] | 231 | bool FillSurfaceAction::canUndo() {
|
---|
[42b6de] | 232 | return true;
|
---|
[2db053] | 233 | }
|
---|
| 234 |
|
---|
[09eaac] | 235 | bool FillSurfaceAction::shouldUndo() {
|
---|
[42b6de] | 236 | return true;
|
---|
[2db053] | 237 | }
|
---|
| 238 | /** =========== end of function ====================== */
|
---|