[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 "Actions/MoleculeAction/RotateAroundSelfByAngleAction.hpp"
|
---|
| 23 | #include "Actions/ActionRegistry.hpp"
|
---|
| 24 | #include "Helpers/Log.hpp"
|
---|
| 25 | #include "Helpers/Verbose.hpp"
|
---|
| 26 | #include "LinearAlgebra/Line.hpp"
|
---|
| 27 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 28 | #include "atom.hpp"
|
---|
| 29 | #include "molecule.hpp"
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | #include <iostream>
|
---|
| 33 | #include <fstream>
|
---|
| 34 | #include <string>
|
---|
| 35 |
|
---|
| 36 | using namespace std;
|
---|
| 37 |
|
---|
| 38 | #include "UIElements/UIFactory.hpp"
|
---|
| 39 | #include "UIElements/Dialog.hpp"
|
---|
| 40 | #include "Actions/ValueStorage.hpp"
|
---|
| 41 |
|
---|
| 42 | /****** MoleculeRotateAroundSelfByAngleAction *****/
|
---|
| 43 |
|
---|
| 44 | // memento to remember the state when undoing
|
---|
| 45 |
|
---|
[2204b0] | 46 | class MoleculeRotateAroundSelfByAngleState : public ActionState {
|
---|
| 47 | public:
|
---|
[90bc51] | 48 | MoleculeRotateAroundSelfByAngleState(molecule * const _mol, const Vector &_Axis, const double _alpha) :
|
---|
[2204b0] | 49 | mol(_mol),
|
---|
[90bc51] | 50 | Axis(_Axis),
|
---|
[2204b0] | 51 | alpha(_alpha)
|
---|
| 52 | {}
|
---|
| 53 | molecule* const mol;
|
---|
[90bc51] | 54 | Vector Axis;
|
---|
[2204b0] | 55 | double alpha;
|
---|
| 56 | };
|
---|
[eaf4ae] | 57 |
|
---|
| 58 | const char MoleculeRotateAroundSelfByAngleAction::NAME[] = "rotate-self";
|
---|
| 59 |
|
---|
| 60 | MoleculeRotateAroundSelfByAngleAction::MoleculeRotateAroundSelfByAngleAction() :
|
---|
| 61 | Action(NAME)
|
---|
| 62 | {}
|
---|
| 63 |
|
---|
| 64 | MoleculeRotateAroundSelfByAngleAction::~MoleculeRotateAroundSelfByAngleAction()
|
---|
| 65 | {}
|
---|
| 66 |
|
---|
[90bc51] | 67 | void MoleculeRotateAroundSelfByAngle(const Vector &Axis, const double angle) {
|
---|
[eaf4ae] | 68 | ValueStorage::getInstance().setCurrentValue(MoleculeRotateAroundSelfByAngleAction::NAME, angle);
|
---|
[90bc51] | 69 | ValueStorage::getInstance().setCurrentValue("position", Axis);
|
---|
[eaf4ae] | 70 | ActionRegistry::getInstance().getActionByName(MoleculeRotateAroundSelfByAngleAction::NAME)->call(Action::NonInteractive);
|
---|
| 71 | };
|
---|
| 72 |
|
---|
[0b2ce9] | 73 | void MoleculeRotateAroundSelfByAngleAction::getParametersfromValueStorage()
|
---|
| 74 | {};
|
---|
| 75 |
|
---|
[eaf4ae] | 76 | Dialog* MoleculeRotateAroundSelfByAngleAction::fillDialog(Dialog *dialog) {
|
---|
| 77 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
| 78 |
|
---|
[c89fb4] | 79 | dialog->queryDouble(NAME, ValueStorage::getInstance().getDescription(NAME));
|
---|
| 80 | dialog->queryVector("position", false, ValueStorage::getInstance().getDescription(NAME));
|
---|
[eaf4ae] | 81 |
|
---|
| 82 | return dialog;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | Action::state_ptr MoleculeRotateAroundSelfByAngleAction::performCall() {
|
---|
| 86 | molecule *mol = NULL;
|
---|
| 87 | double alpha = 0.;
|
---|
[90bc51] | 88 | Vector Axis;
|
---|
[eaf4ae] | 89 |
|
---|
[90bc51] | 90 | // obtain angle and axis around which rotate
|
---|
[eaf4ae] | 91 | ValueStorage::getInstance().queryCurrentValue(NAME, alpha);
|
---|
[90bc51] | 92 | ValueStorage::getInstance().queryCurrentValue("position", Axis);
|
---|
[eaf4ae] | 93 |
|
---|
| 94 | // check whether a single atom and molecule is selected
|
---|
[90bc51] | 95 | if (World::getInstance().getSelectedMolecules().size() != 1)
|
---|
[eaf4ae] | 96 | return Action::failure;
|
---|
| 97 | mol = World::getInstance().beginMoleculeSelection()->second;
|
---|
| 98 |
|
---|
[90bc51] | 99 | // check whether Axis is valid
|
---|
| 100 | if (Axis.IsZero())
|
---|
| 101 | return Action::failure;
|
---|
| 102 |
|
---|
| 103 | // convert from degrees to radian
|
---|
| 104 | alpha *= M_PI/180.;
|
---|
| 105 |
|
---|
[eaf4ae] | 106 | // Creation Line that is the rotation axis
|
---|
| 107 | Vector *CenterOfGravity = mol->DetermineCenterOfGravity();
|
---|
[90bc51] | 108 | Line RotationAxis(*CenterOfGravity, Axis);
|
---|
[eaf4ae] | 109 | delete(CenterOfGravity);
|
---|
[90bc51] | 110 | DoLog(0) && (Log() << Verbose(0) << "Rotate around self by " << alpha << " along " << RotationAxis << "." << endl);
|
---|
[eaf4ae] | 111 |
|
---|
| 112 | for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) {
|
---|
[8f4df1] | 113 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), alpha));
|
---|
[eaf4ae] | 114 | }
|
---|
| 115 | DoLog(0) && (Log() << Verbose(0) << "done." << endl);
|
---|
| 116 |
|
---|
[90bc51] | 117 | return Action::state_ptr(new MoleculeRotateAroundSelfByAngleState(mol, Axis, alpha));
|
---|
[eaf4ae] | 118 | }
|
---|
| 119 |
|
---|
| 120 | Action::state_ptr MoleculeRotateAroundSelfByAngleAction::performUndo(Action::state_ptr _state) {
|
---|
[2204b0] | 121 | MoleculeRotateAroundSelfByAngleState *state = assert_cast<MoleculeRotateAroundSelfByAngleState*>(_state.get());
|
---|
| 122 |
|
---|
| 123 | Vector *CenterOfGravity = state->mol->DetermineCenterOfGravity();
|
---|
[90bc51] | 124 | Line RotationAxis(*CenterOfGravity, state->Axis);
|
---|
[2204b0] | 125 | delete(CenterOfGravity);
|
---|
[eaf4ae] | 126 |
|
---|
[2204b0] | 127 | for (molecule::iterator iter = state->mol->begin(); iter != state->mol->end(); ++iter) {
|
---|
[8f4df1] | 128 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), -state->alpha));
|
---|
[2204b0] | 129 | }
|
---|
[eaf4ae] | 130 |
|
---|
[2204b0] | 131 | return Action::state_ptr(_state);
|
---|
[eaf4ae] | 132 | }
|
---|
| 133 |
|
---|
| 134 | Action::state_ptr MoleculeRotateAroundSelfByAngleAction::performRedo(Action::state_ptr _state){
|
---|
[2204b0] | 135 | MoleculeRotateAroundSelfByAngleState *state = assert_cast<MoleculeRotateAroundSelfByAngleState*>(_state.get());
|
---|
| 136 |
|
---|
| 137 | Vector *CenterOfGravity = state->mol->DetermineCenterOfGravity();
|
---|
[90bc51] | 138 | Line RotationAxis(*CenterOfGravity, state->Axis);
|
---|
[2204b0] | 139 | delete(CenterOfGravity);
|
---|
| 140 |
|
---|
| 141 | for (molecule::iterator iter = state->mol->begin(); iter != state->mol->end(); ++iter) {
|
---|
[8f4df1] | 142 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), state->alpha));
|
---|
[2204b0] | 143 | }
|
---|
| 144 |
|
---|
| 145 | return Action::state_ptr(_state);
|
---|
[eaf4ae] | 146 | }
|
---|
| 147 |
|
---|
| 148 | bool MoleculeRotateAroundSelfByAngleAction::canUndo() {
|
---|
[2204b0] | 149 | return true;
|
---|
[eaf4ae] | 150 | }
|
---|
| 151 |
|
---|
| 152 | bool MoleculeRotateAroundSelfByAngleAction::shouldUndo() {
|
---|
[2204b0] | 153 | return true;
|
---|
[eaf4ae] | 154 | }
|
---|
| 155 |
|
---|
| 156 | const string MoleculeRotateAroundSelfByAngleAction::getName() {
|
---|
| 157 | return NAME;
|
---|
| 158 | }
|
---|