| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
|  | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [eaf4ae] | 8 | /* | 
|---|
|  | 9 | * RotateAroundSelfByAngleAction.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Aug 06, 2010 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [eaf4ae] | 20 | #include "Helpers/MemDebug.hpp" | 
|---|
|  | 21 |  | 
|---|
|  | 22 | #include "Helpers/Log.hpp" | 
|---|
|  | 23 | #include "Helpers/Verbose.hpp" | 
|---|
|  | 24 | #include "LinearAlgebra/Line.hpp" | 
|---|
|  | 25 | #include "LinearAlgebra/Vector.hpp" | 
|---|
|  | 26 | #include "atom.hpp" | 
|---|
|  | 27 | #include "molecule.hpp" | 
|---|
|  | 28 |  | 
|---|
|  | 29 |  | 
|---|
|  | 30 | #include <iostream> | 
|---|
|  | 31 | #include <fstream> | 
|---|
|  | 32 | #include <string> | 
|---|
|  | 33 |  | 
|---|
|  | 34 | using namespace std; | 
|---|
|  | 35 |  | 
|---|
| [1fd675] | 36 | #include "Actions/MoleculeAction/RotateAroundSelfByAngleAction.hpp" | 
|---|
| [eaf4ae] | 37 |  | 
|---|
| [1fd675] | 38 | // and construct the stuff | 
|---|
|  | 39 | #include "RotateAroundSelfByAngleAction.def" | 
|---|
|  | 40 | #include "Action_impl_pre.hpp" | 
|---|
|  | 41 | /** =========== define the function ====================== */ | 
|---|
| [eaf4ae] | 42 | Action::state_ptr MoleculeRotateAroundSelfByAngleAction::performCall() { | 
|---|
|  | 43 | molecule *mol = NULL; | 
|---|
|  | 44 |  | 
|---|
| [1fd675] | 45 | // obtain information | 
|---|
|  | 46 | getParametersfromValueStorage(); | 
|---|
| [eaf4ae] | 47 |  | 
|---|
|  | 48 | // check whether a single atom and molecule is selected | 
|---|
| [90bc51] | 49 | if (World::getInstance().getSelectedMolecules().size() != 1) | 
|---|
| [eaf4ae] | 50 | return Action::failure; | 
|---|
|  | 51 | mol = World::getInstance().beginMoleculeSelection()->second; | 
|---|
|  | 52 |  | 
|---|
| [90bc51] | 53 | // check whether Axis is valid | 
|---|
| [1fd675] | 54 | if (params.Axis.IsZero()) | 
|---|
| [90bc51] | 55 | return Action::failure; | 
|---|
|  | 56 |  | 
|---|
|  | 57 | // convert from degrees to radian | 
|---|
| [1fd675] | 58 | params.angle *= M_PI/180.; | 
|---|
| [90bc51] | 59 |  | 
|---|
| [eaf4ae] | 60 | // Creation Line that is the rotation axis | 
|---|
|  | 61 | Vector *CenterOfGravity = mol->DetermineCenterOfGravity(); | 
|---|
| [1fd675] | 62 | Line RotationAxis(*CenterOfGravity, params.Axis); | 
|---|
| [eaf4ae] | 63 | delete(CenterOfGravity); | 
|---|
| [1fd675] | 64 | DoLog(0) && (Log() << Verbose(0) << "Rotate around self by " << params.angle << " along " << RotationAxis << "." << endl); | 
|---|
| [eaf4ae] | 65 |  | 
|---|
|  | 66 | for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) { | 
|---|
| [1fd675] | 67 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), params.angle)); | 
|---|
| [eaf4ae] | 68 | } | 
|---|
|  | 69 | DoLog(0) && (Log() << Verbose(0) << "done." << endl); | 
|---|
|  | 70 |  | 
|---|
| [1fd675] | 71 | return Action::state_ptr(new MoleculeRotateAroundSelfByAngleState(mol, params)); | 
|---|
| [eaf4ae] | 72 | } | 
|---|
|  | 73 |  | 
|---|
|  | 74 | Action::state_ptr MoleculeRotateAroundSelfByAngleAction::performUndo(Action::state_ptr _state) { | 
|---|
| [2204b0] | 75 | MoleculeRotateAroundSelfByAngleState *state = assert_cast<MoleculeRotateAroundSelfByAngleState*>(_state.get()); | 
|---|
|  | 76 |  | 
|---|
|  | 77 | Vector *CenterOfGravity = state->mol->DetermineCenterOfGravity(); | 
|---|
| [1fd675] | 78 | Line RotationAxis(*CenterOfGravity, state->params.Axis); | 
|---|
| [2204b0] | 79 | delete(CenterOfGravity); | 
|---|
| [eaf4ae] | 80 |  | 
|---|
| [2204b0] | 81 | for (molecule::iterator iter = state->mol->begin(); iter != state->mol->end(); ++iter) { | 
|---|
| [1fd675] | 82 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), -state->params.angle)); | 
|---|
| [2204b0] | 83 | } | 
|---|
| [eaf4ae] | 84 |  | 
|---|
| [2204b0] | 85 | return Action::state_ptr(_state); | 
|---|
| [eaf4ae] | 86 | } | 
|---|
|  | 87 |  | 
|---|
|  | 88 | Action::state_ptr MoleculeRotateAroundSelfByAngleAction::performRedo(Action::state_ptr _state){ | 
|---|
| [2204b0] | 89 | MoleculeRotateAroundSelfByAngleState *state = assert_cast<MoleculeRotateAroundSelfByAngleState*>(_state.get()); | 
|---|
|  | 90 |  | 
|---|
|  | 91 | Vector *CenterOfGravity = state->mol->DetermineCenterOfGravity(); | 
|---|
| [1fd675] | 92 | Line RotationAxis(*CenterOfGravity, state->params.Axis); | 
|---|
| [2204b0] | 93 | delete(CenterOfGravity); | 
|---|
|  | 94 |  | 
|---|
|  | 95 | for (molecule::iterator iter = state->mol->begin(); iter != state->mol->end(); ++iter) { | 
|---|
| [1fd675] | 96 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), state->params.angle)); | 
|---|
| [2204b0] | 97 | } | 
|---|
|  | 98 |  | 
|---|
|  | 99 | return Action::state_ptr(_state); | 
|---|
| [eaf4ae] | 100 | } | 
|---|
|  | 101 |  | 
|---|
|  | 102 | bool MoleculeRotateAroundSelfByAngleAction::canUndo() { | 
|---|
| [2204b0] | 103 | return true; | 
|---|
| [eaf4ae] | 104 | } | 
|---|
|  | 105 |  | 
|---|
|  | 106 | bool MoleculeRotateAroundSelfByAngleAction::shouldUndo() { | 
|---|
| [2204b0] | 107 | return true; | 
|---|
| [eaf4ae] | 108 | } | 
|---|
|  | 109 |  | 
|---|
|  | 110 | const string MoleculeRotateAroundSelfByAngleAction::getName() { | 
|---|
|  | 111 | return NAME; | 
|---|
|  | 112 | } | 
|---|
| [1fd675] | 113 | /** =========== end of function ====================== */ | 
|---|