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