| 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 |
|
|---|
| 8 | /*
|
|---|
| 9 | * RotateAroundSelfByAngleAction.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * Created on: Aug 06, 2010
|
|---|
| 12 | * Author: heber
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | // include config.h
|
|---|
| 16 | #ifdef HAVE_CONFIG_H
|
|---|
| 17 | #include <config.h>
|
|---|
| 18 | #endif
|
|---|
| 19 |
|
|---|
| 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 |
|
|---|
| 36 | #include "Actions/MoleculeAction/RotateAroundSelfByAngleAction.hpp"
|
|---|
| 37 |
|
|---|
| 38 | // and construct the stuff
|
|---|
| 39 | #include "RotateAroundSelfByAngleAction.def"
|
|---|
| 40 | #include "Action_impl_pre.hpp"
|
|---|
| 41 | /** =========== define the function ====================== */
|
|---|
| 42 | Action::state_ptr MoleculeRotateAroundSelfByAngleAction::performCall() {
|
|---|
| 43 | molecule *mol = NULL;
|
|---|
| 44 |
|
|---|
| 45 | // obtain information
|
|---|
| 46 | getParametersfromValueStorage();
|
|---|
| 47 |
|
|---|
| 48 | // check whether a single atom and molecule is selected
|
|---|
| 49 | if (World::getInstance().getSelectedMolecules().size() != 1)
|
|---|
| 50 | return Action::failure;
|
|---|
| 51 | mol = World::getInstance().beginMoleculeSelection()->second;
|
|---|
| 52 |
|
|---|
| 53 | // check whether Axis is valid
|
|---|
| 54 | if (params.Axis.IsZero())
|
|---|
| 55 | return Action::failure;
|
|---|
| 56 |
|
|---|
| 57 | // convert from degrees to radian
|
|---|
| 58 | params.angle *= M_PI/180.;
|
|---|
| 59 |
|
|---|
| 60 | // Creation Line that is the rotation axis
|
|---|
| 61 | Vector *CenterOfGravity = mol->DetermineCenterOfGravity();
|
|---|
| 62 | Line RotationAxis(*CenterOfGravity, params.Axis);
|
|---|
| 63 | delete(CenterOfGravity);
|
|---|
| 64 | DoLog(0) && (Log() << Verbose(0) << "Rotate around self by " << params.angle << " along " << RotationAxis << "." << endl);
|
|---|
| 65 |
|
|---|
| 66 | for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) {
|
|---|
| 67 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), params.angle));
|
|---|
| 68 | }
|
|---|
| 69 | DoLog(0) && (Log() << Verbose(0) << "done." << endl);
|
|---|
| 70 |
|
|---|
| 71 | return Action::state_ptr(new MoleculeRotateAroundSelfByAngleState(mol, params));
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | Action::state_ptr MoleculeRotateAroundSelfByAngleAction::performUndo(Action::state_ptr _state) {
|
|---|
| 75 | MoleculeRotateAroundSelfByAngleState *state = assert_cast<MoleculeRotateAroundSelfByAngleState*>(_state.get());
|
|---|
| 76 |
|
|---|
| 77 | Vector *CenterOfGravity = state->mol->DetermineCenterOfGravity();
|
|---|
| 78 | Line RotationAxis(*CenterOfGravity, state->params.Axis);
|
|---|
| 79 | delete(CenterOfGravity);
|
|---|
| 80 |
|
|---|
| 81 | for (molecule::iterator iter = state->mol->begin(); iter != state->mol->end(); ++iter) {
|
|---|
| 82 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), -state->params.angle));
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | return Action::state_ptr(_state);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | Action::state_ptr MoleculeRotateAroundSelfByAngleAction::performRedo(Action::state_ptr _state){
|
|---|
| 89 | MoleculeRotateAroundSelfByAngleState *state = assert_cast<MoleculeRotateAroundSelfByAngleState*>(_state.get());
|
|---|
| 90 |
|
|---|
| 91 | Vector *CenterOfGravity = state->mol->DetermineCenterOfGravity();
|
|---|
| 92 | Line RotationAxis(*CenterOfGravity, state->params.Axis);
|
|---|
| 93 | delete(CenterOfGravity);
|
|---|
| 94 |
|
|---|
| 95 | for (molecule::iterator iter = state->mol->begin(); iter != state->mol->end(); ++iter) {
|
|---|
| 96 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), state->params.angle));
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | return Action::state_ptr(_state);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | bool MoleculeRotateAroundSelfByAngleAction::canUndo() {
|
|---|
| 103 | return true;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | bool MoleculeRotateAroundSelfByAngleAction::shouldUndo() {
|
|---|
| 107 | return true;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | const string MoleculeRotateAroundSelfByAngleAction::getName() {
|
|---|
| 111 | return NAME;
|
|---|
| 112 | }
|
|---|
| 113 | /** =========== end of function ====================== */
|
|---|