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