| [eaf4ae] | 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/MoleculeAction/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 | /****** MoleculeRotateAroundOriginByAngleAction *****/
|
|---|
| 30 |
|
|---|
| 31 | // memento to remember the state when undoing
|
|---|
| 32 |
|
|---|
| [2204b0] | 33 | class MoleculeRotateAroundOriginByAngleState : public ActionState {
|
|---|
| 34 | public:
|
|---|
| 35 | MoleculeRotateAroundOriginByAngleState(const std::vector<molecule*> &_molecules,const Vector &_Axis, const double _alpha) :
|
|---|
| 36 | molecules(_molecules),
|
|---|
| 37 | Axis(_Axis),
|
|---|
| 38 | alpha(_alpha)
|
|---|
| 39 | {}
|
|---|
| 40 | std::vector<molecule*> molecules;
|
|---|
| 41 | Vector Axis;
|
|---|
| 42 | double alpha;
|
|---|
| 43 | };
|
|---|
| [eaf4ae] | 44 |
|
|---|
| 45 | const char MoleculeRotateAroundOriginByAngleAction::NAME[] = "rotate-origin";
|
|---|
| 46 |
|
|---|
| 47 | MoleculeRotateAroundOriginByAngleAction::MoleculeRotateAroundOriginByAngleAction() :
|
|---|
| 48 | Action(NAME)
|
|---|
| 49 | {}
|
|---|
| 50 |
|
|---|
| 51 | MoleculeRotateAroundOriginByAngleAction::~MoleculeRotateAroundOriginByAngleAction()
|
|---|
| 52 | {}
|
|---|
| 53 |
|
|---|
| 54 | void MoleculeRotateAroundOriginByAngle(double angle) {
|
|---|
| 55 | ValueStorage::getInstance().setCurrentValue(MoleculeRotateAroundOriginByAngleAction::NAME, angle);
|
|---|
| 56 | ActionRegistry::getInstance().getActionByName(MoleculeRotateAroundOriginByAngleAction::NAME)->call(Action::NonInteractive);
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | Dialog* MoleculeRotateAroundOriginByAngleAction::fillDialog(Dialog *dialog) {
|
|---|
| 60 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
|---|
| 61 |
|
|---|
| 62 | dialog->queryDouble(NAME, MapOfActions::getInstance().getDescription(NAME));
|
|---|
| 63 | dialog->queryVector("position", false, MapOfActions::getInstance().getDescription("position"));
|
|---|
| 64 |
|
|---|
| 65 | return dialog;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | Action::state_ptr MoleculeRotateAroundOriginByAngleAction::performCall() {
|
|---|
| 69 | molecule *mol = NULL;
|
|---|
| 70 | double alpha = 0.;
|
|---|
| 71 | Vector Axis;
|
|---|
| 72 |
|
|---|
| 73 | // obtain axis to rotate to
|
|---|
| 74 | ValueStorage::getInstance().queryCurrentValue(NAME, alpha);
|
|---|
| 75 | ValueStorage::getInstance().queryCurrentValue("position", Axis);
|
|---|
| 76 |
|
|---|
| [2204b0] | 77 | // check whether Axis is valid
|
|---|
| 78 | if (Axis.IsZero())
|
|---|
| 79 | return Action::failure;
|
|---|
| 80 |
|
|---|
| 81 | // convert from degrees to radian
|
|---|
| 82 | alpha *= M_PI/180.;
|
|---|
| 83 |
|
|---|
| 84 | DoLog(0) && (Log() << Verbose(0) << "Rotate around origin by " << alpha << " radian, axis from origin to " << Axis << "." << endl);
|
|---|
| [eaf4ae] | 85 | for (World::MoleculeSelectionIterator iter = World::getInstance().beginMoleculeSelection(); iter != World::getInstance().endMoleculeSelection(); ++iter) {
|
|---|
| 86 | mol = iter->second;
|
|---|
| 87 |
|
|---|
| 88 | // Creation Line that is the rotation axis
|
|---|
| 89 | Line RotationAxis(Vector(0.,0.,0.), Axis);
|
|---|
| 90 |
|
|---|
| 91 | for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) {
|
|---|
| 92 | *((*iter)->node) = RotationAxis.rotateVector(*((*iter)->node), alpha);
|
|---|
| 93 | }
|
|---|
| 94 | DoLog(0) && (Log() << Verbose(0) << "done." << endl);
|
|---|
| 95 | }
|
|---|
| [2204b0] | 96 | return Action::state_ptr(new MoleculeRotateAroundOriginByAngleState(World::getInstance().getSelectedMolecules(), Axis, alpha));
|
|---|
| [eaf4ae] | 97 | }
|
|---|
| 98 |
|
|---|
| 99 | Action::state_ptr MoleculeRotateAroundOriginByAngleAction::performUndo(Action::state_ptr _state) {
|
|---|
| [2204b0] | 100 | MoleculeRotateAroundOriginByAngleState *state = assert_cast<MoleculeRotateAroundOriginByAngleState*>(_state.get());
|
|---|
| 101 | molecule *mol = NULL;
|
|---|
| [eaf4ae] | 102 |
|
|---|
| [2204b0] | 103 | for (std::vector<molecule *>::const_iterator iter = state->molecules.begin(); iter != state->molecules.end(); ++iter) {
|
|---|
| 104 | mol = *iter;
|
|---|
| 105 |
|
|---|
| 106 | // Creation Line that is the rotation axis
|
|---|
| 107 | Line RotationAxis(Vector(0.,0.,0.), state->Axis);
|
|---|
| 108 |
|
|---|
| 109 | for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) {
|
|---|
| 110 | *((*iter)->node) = RotationAxis.rotateVector(*((*iter)->node), -state->alpha);
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| [eaf4ae] | 113 |
|
|---|
| [2204b0] | 114 | return Action::state_ptr(_state);
|
|---|
| [eaf4ae] | 115 | }
|
|---|
| 116 |
|
|---|
| 117 | Action::state_ptr MoleculeRotateAroundOriginByAngleAction::performRedo(Action::state_ptr _state){
|
|---|
| [2204b0] | 118 | MoleculeRotateAroundOriginByAngleState *state = assert_cast<MoleculeRotateAroundOriginByAngleState*>(_state.get());
|
|---|
| 119 | molecule *mol = NULL;
|
|---|
| 120 |
|
|---|
| 121 | for (std::vector<molecule *>::const_iterator iter = state->molecules.begin(); iter != state->molecules.end(); ++iter) {
|
|---|
| 122 | mol = *iter;
|
|---|
| 123 |
|
|---|
| 124 | // Creation Line that is the rotation axis
|
|---|
| 125 | Line RotationAxis(Vector(0.,0.,0.), state->Axis);
|
|---|
| 126 |
|
|---|
| 127 | for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) {
|
|---|
| 128 | *((*iter)->node) = RotationAxis.rotateVector(*((*iter)->node), state->alpha);
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | return Action::state_ptr(_state);
|
|---|
| [eaf4ae] | 133 | }
|
|---|
| 134 |
|
|---|
| 135 | bool MoleculeRotateAroundOriginByAngleAction::canUndo() {
|
|---|
| [2204b0] | 136 | return true;
|
|---|
| [eaf4ae] | 137 | }
|
|---|
| 138 |
|
|---|
| 139 | bool MoleculeRotateAroundOriginByAngleAction::shouldUndo() {
|
|---|
| [2204b0] | 140 | return true;
|
|---|
| [eaf4ae] | 141 | }
|
|---|
| 142 |
|
|---|
| 143 | const string MoleculeRotateAroundOriginByAngleAction::getName() {
|
|---|
| 144 | return NAME;
|
|---|
| 145 | }
|
|---|