| 1 | /*
|
|---|
| 2 | * RotateAroundOriginByAngleAction.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Aug 06, 2010
|
|---|
| 5 | * Author: heber
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #include "Helpers/MemDebug.hpp"
|
|---|
| 9 |
|
|---|
| 10 | #include "Actions/AtomAction/RotateAroundOriginByAngleAction.hpp"
|
|---|
| 11 | #include "Actions/ActionRegistry.hpp"
|
|---|
| 12 | #include "Helpers/Log.hpp"
|
|---|
| 13 | #include "Helpers/Verbose.hpp"
|
|---|
| 14 | #include "LinearAlgebra/Line.hpp"
|
|---|
| 15 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 16 | #include "molecule.hpp"
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 | #include <iostream>
|
|---|
| 20 | #include <fstream>
|
|---|
| 21 | #include <string>
|
|---|
| 22 |
|
|---|
| 23 | using namespace std;
|
|---|
| 24 |
|
|---|
| 25 | #include "UIElements/UIFactory.hpp"
|
|---|
| 26 | #include "UIElements/Dialog.hpp"
|
|---|
| 27 | #include "Actions/ValueStorage.hpp"
|
|---|
| 28 |
|
|---|
| 29 | /****** AtomRotateAroundOriginByAngleAction *****/
|
|---|
| 30 |
|
|---|
| 31 | // memento to remember the state when undoing
|
|---|
| 32 |
|
|---|
| 33 | class AtomRotateAroundOriginByAngleState : public ActionState {
|
|---|
| 34 | public:
|
|---|
| 35 | AtomRotateAroundOriginByAngleState(std::vector<atom*> _selectedAtoms, const Vector &_Axis, const double _alpha) :
|
|---|
| 36 | selectedAtoms(_selectedAtoms),
|
|---|
| 37 | Axis(_Axis),
|
|---|
| 38 | alpha(_alpha)
|
|---|
| 39 | {}
|
|---|
| 40 | std::vector<atom*> selectedAtoms;
|
|---|
| 41 | Vector Axis;
|
|---|
| 42 | double alpha;
|
|---|
| 43 | };
|
|---|
| 44 |
|
|---|
| 45 | const char AtomRotateAroundOriginByAngleAction::NAME[] = "rotate-origin";
|
|---|
| 46 |
|
|---|
| 47 | AtomRotateAroundOriginByAngleAction::AtomRotateAroundOriginByAngleAction() :
|
|---|
| 48 | Action(NAME)
|
|---|
| 49 | {}
|
|---|
| 50 |
|
|---|
| 51 | AtomRotateAroundOriginByAngleAction::~AtomRotateAroundOriginByAngleAction()
|
|---|
| 52 | {}
|
|---|
| 53 |
|
|---|
| 54 | void AtomRotateAroundOriginByAngle(const Vector &Axis, double angle) {
|
|---|
| 55 | ValueStorage::getInstance().setCurrentValue(AtomRotateAroundOriginByAngleAction::NAME, angle);
|
|---|
| 56 | ValueStorage::getInstance().setCurrentValue("position", Axis);
|
|---|
| 57 | ActionRegistry::getInstance().getActionByName(AtomRotateAroundOriginByAngleAction::NAME)->call(Action::NonInteractive);
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | Dialog* AtomRotateAroundOriginByAngleAction::fillDialog(Dialog *dialog) {
|
|---|
| 61 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
|---|
| 62 |
|
|---|
| 63 | dialog->queryDouble(NAME, ValueStorage::getInstance().getDescription(NAME));
|
|---|
| 64 | dialog->queryVector("position", false, ValueStorage::getInstance().getDescription("position"));
|
|---|
| 65 |
|
|---|
| 66 | return dialog;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | Action::state_ptr AtomRotateAroundOriginByAngleAction::performCall() {
|
|---|
| 70 | double alpha = 0.;
|
|---|
| 71 | Vector Axis;
|
|---|
| 72 | std::vector<atom *> selectedAtoms = World::getInstance().getSelectedAtoms();
|
|---|
| 73 |
|
|---|
| 74 | // obtain axis to rotate to
|
|---|
| 75 | ValueStorage::getInstance().queryCurrentValue(NAME, alpha);
|
|---|
| 76 | ValueStorage::getInstance().queryCurrentValue("position", Axis);
|
|---|
| 77 |
|
|---|
| 78 | // check whether Axis is valid
|
|---|
| 79 | if (Axis.IsZero())
|
|---|
| 80 | return Action::failure;
|
|---|
| 81 |
|
|---|
| 82 | // convert from degrees to radian
|
|---|
| 83 | alpha *= M_PI/180.;
|
|---|
| 84 |
|
|---|
| 85 | // Creation Line that is the rotation axis
|
|---|
| 86 | Line RotationAxis(Vector(0.,0.,0.), Axis);
|
|---|
| 87 |
|
|---|
| 88 | DoLog(0) && (Log() << Verbose(0) << "Rotate around origin by " << alpha << " radian, axis from origin to " << Axis << "." << endl);
|
|---|
| 89 | // TODO: use AtomSet::rotate?
|
|---|
| 90 | for (std::vector<atom *>::iterator iter = selectedAtoms.begin(); iter != selectedAtoms.end(); ++iter) {
|
|---|
| 91 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), alpha));
|
|---|
| 92 | }
|
|---|
| 93 | DoLog(0) && (Log() << Verbose(0) << "done." << endl);
|
|---|
| 94 | return Action::state_ptr(new AtomRotateAroundOriginByAngleState(World::getInstance().getSelectedAtoms(), Axis, alpha));
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | Action::state_ptr AtomRotateAroundOriginByAngleAction::performUndo(Action::state_ptr _state) {
|
|---|
| 98 | AtomRotateAroundOriginByAngleState *state = assert_cast<AtomRotateAroundOriginByAngleState*>(_state.get());
|
|---|
| 99 |
|
|---|
| 100 | // Creation Line that is the rotation axis
|
|---|
| 101 | Line RotationAxis(Vector(0.,0.,0.), state->Axis);
|
|---|
| 102 |
|
|---|
| 103 | for (std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter) {
|
|---|
| 104 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), -state->alpha));
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | return Action::state_ptr(_state);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | Action::state_ptr AtomRotateAroundOriginByAngleAction::performRedo(Action::state_ptr _state){
|
|---|
| 111 | AtomRotateAroundOriginByAngleState *state = assert_cast<AtomRotateAroundOriginByAngleState*>(_state.get());
|
|---|
| 112 |
|
|---|
| 113 | // Creation Line that is the rotation axis
|
|---|
| 114 | Line RotationAxis(Vector(0.,0.,0.), state->Axis);
|
|---|
| 115 |
|
|---|
| 116 | for (std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter) {
|
|---|
| 117 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), state->alpha));
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | return Action::state_ptr(_state);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | bool AtomRotateAroundOriginByAngleAction::canUndo() {
|
|---|
| 124 | return true;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | bool AtomRotateAroundOriginByAngleAction::shouldUndo() {
|
|---|
| 128 | return true;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | const string AtomRotateAroundOriginByAngleAction::getName() {
|
|---|
| 132 | return NAME;
|
|---|
| 133 | }
|
|---|