| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2017 Frederik Heber. 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 |  * GeometryPlaneToVectorAction.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Mar 28, 2017
 | 
|---|
| 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 | 
 | 
|---|
| 37 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #include <string>
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #include "LinearAlgebra/Plane.hpp"
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "Atom/atom.hpp"
 | 
|---|
| 44 | #include "Geometry/GeometryObject.hpp"
 | 
|---|
| 45 | #include "Geometry/GeometryRegistry.hpp"
 | 
|---|
| 46 | #include "World.hpp"
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #include "Actions/GeometryAction/GeometryPlaneToVectorAction.hpp"
 | 
|---|
| 49 | 
 | 
|---|
| 50 | using namespace MoleCuilder;
 | 
|---|
| 51 | 
 | 
|---|
| 52 | // and construct the stuff
 | 
|---|
| 53 | #include "../GeometryAction/GeometryPlaneToVectorAction.def"
 | 
|---|
| 54 | #include "Action_impl_pre.hpp"
 | 
|---|
| 55 | /** =========== define the function ====================== */
 | 
|---|
| 56 | ActionState::ptr GeometryPlaneToVectorAction::performCall() {
 | 
|---|
| 57 |   // check preconditions
 | 
|---|
| 58 |   World& world = World::getInstance();
 | 
|---|
| 59 |   if (world.countSelectedAtoms() != 3) {
 | 
|---|
| 60 |     STATUS("There must be exactly three atoms selected for GeometryAction PlaneToVector.");
 | 
|---|
| 61 |     return Action::failure;
 | 
|---|
| 62 |   }
 | 
|---|
| 63 |   if (GeometryRegistry::getInstance().isPresentByName(params.name.get())) {
 | 
|---|
| 64 |     STATUS("Name "+params.name.get()+" already present in registry.");
 | 
|---|
| 65 |     return Action::failure;
 | 
|---|
| 66 |   }
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   World::AtomSelectionIterator iter = world.beginAtomSelection();
 | 
|---|
| 69 |   const atom * const Walker = (iter++)->second;
 | 
|---|
| 70 |   const atom * const OtherWalker = (iter++)->second;
 | 
|---|
| 71 |   const atom * const AnotherWalker = (iter++)->second;
 | 
|---|
| 72 |   ASSERT( iter == world.endAtomSelection(),
 | 
|---|
| 73 |       "GeometryDistanceToVectorAction::performCall() - selection iter not at end?");
 | 
|---|
| 74 |   Vector PlaneNormal;
 | 
|---|
| 75 |   try {
 | 
|---|
| 76 |     const Plane AtomPlane(Walker->getPosition(), OtherWalker->getPosition(), AnotherWalker->getPosition());
 | 
|---|
| 77 |     PlaneNormal = AtomPlane.getNormal();
 | 
|---|
| 78 |   } catch (LinearDependenceException &e) {
 | 
|---|
| 79 |     ELOG(2, "The selected atomic positions are linearly dependent.");
 | 
|---|
| 80 |     return Action::failure;
 | 
|---|
| 81 |   }
 | 
|---|
| 82 |   PlaneNormal *= (params.reverse.get() ? -1. : 1.);
 | 
|---|
| 83 |   const GeometryObject vec(PlaneNormal, params.name.get());
 | 
|---|
| 84 |   LOG(4, "DEBUG: Adding geometry object " << vec << " with norm " << PlaneNormal.Norm());
 | 
|---|
| 85 |   GeometryRegistry::getInstance().addGeometry(vec);
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   GeometryPlaneToVectorState *UndoState = new GeometryPlaneToVectorState(PlaneNormal, params);
 | 
|---|
| 88 |   return ActionState::ptr(UndoState);
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | ActionState::ptr GeometryPlaneToVectorAction::performUndo(ActionState::ptr _state) {
 | 
|---|
| 92 | //  GeometryPlaneToVectorState *state = assert_cast<GeometryPlaneToVectorState*>(_state.get());
 | 
|---|
| 93 | 
 | 
|---|
| 94 |   GeometryRegistry::getInstance().removeGeometry(params.name.get());
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   return ActionState::ptr(_state);
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | ActionState::ptr GeometryPlaneToVectorAction::performRedo(ActionState::ptr _state){
 | 
|---|
| 100 |   GeometryPlaneToVectorState *state = assert_cast<GeometryPlaneToVectorState*>(_state.get());
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   const GeometryObject vec(state->PlaneNormal, params.name.get());
 | 
|---|
| 103 |   LOG(4, "DEBUG: Adding geometry object " << vec);
 | 
|---|
| 104 |   GeometryRegistry::getInstance().addGeometry(vec);
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   return ActionState::ptr(_state);
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | bool GeometryPlaneToVectorAction::canUndo() {
 | 
|---|
| 110 |   return true;
 | 
|---|
| 111 | }
 | 
|---|
| 112 | 
 | 
|---|
| 113 | bool GeometryPlaneToVectorAction::shouldUndo() {
 | 
|---|
| 114 |   return true;
 | 
|---|
| 115 | }
 | 
|---|
| 116 | /** =========== end of function ====================== */
 | 
|---|