| 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 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * RotateAroundOriginByAngleAction.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Aug 06, 2010
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 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 <cmath>
 | 
|---|
| 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 | /****** AtomRotateAroundOriginByAngleAction *****/
 | 
|---|
| 43 | 
 | 
|---|
| 44 | // memento to remember the state when undoing
 | 
|---|
| 45 | 
 | 
|---|
| 46 | class AtomRotateAroundOriginByAngleState : public ActionState {
 | 
|---|
| 47 | public:
 | 
|---|
| 48 |   AtomRotateAroundOriginByAngleState(std::vector<atom*> _selectedAtoms, const Vector &_Axis, const double _alpha) :
 | 
|---|
| 49 |     selectedAtoms(_selectedAtoms),
 | 
|---|
| 50 |     Axis(_Axis),
 | 
|---|
| 51 |     alpha(_alpha)
 | 
|---|
| 52 |   {}
 | 
|---|
| 53 |   std::vector<atom*> selectedAtoms;
 | 
|---|
| 54 |   Vector Axis;
 | 
|---|
| 55 |   double alpha;
 | 
|---|
| 56 | };
 | 
|---|
| 57 | 
 | 
|---|
| 58 | const char AtomRotateAroundOriginByAngleAction::NAME[] = "rotate-origin";
 | 
|---|
| 59 | 
 | 
|---|
| 60 | AtomRotateAroundOriginByAngleAction::AtomRotateAroundOriginByAngleAction() :
 | 
|---|
| 61 |   Action(NAME)
 | 
|---|
| 62 | {}
 | 
|---|
| 63 | 
 | 
|---|
| 64 | AtomRotateAroundOriginByAngleAction::~AtomRotateAroundOriginByAngleAction()
 | 
|---|
| 65 | {}
 | 
|---|
| 66 | 
 | 
|---|
| 67 | void AtomRotateAroundOriginByAngle(const Vector &Axis, double angle) {
 | 
|---|
| 68 |   ValueStorage::getInstance().setCurrentValue(AtomRotateAroundOriginByAngleAction::NAME, angle);
 | 
|---|
| 69 |   ValueStorage::getInstance().setCurrentValue("position", Axis);
 | 
|---|
| 70 |   ActionRegistry::getInstance().getActionByName(AtomRotateAroundOriginByAngleAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 71 | };
 | 
|---|
| 72 | 
 | 
|---|
| 73 | Dialog* AtomRotateAroundOriginByAngleAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 74 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   dialog->queryDouble(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 77 |   dialog->queryVector("position", false, ValueStorage::getInstance().getDescription("position"));
 | 
|---|
| 78 | 
 | 
|---|
| 79 |   return dialog;
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | Action::state_ptr AtomRotateAroundOriginByAngleAction::performCall() {
 | 
|---|
| 83 |   double alpha = 0.;
 | 
|---|
| 84 |   Vector Axis;
 | 
|---|
| 85 |   std::vector<atom *> selectedAtoms = World::getInstance().getSelectedAtoms();
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   // obtain axis to rotate to
 | 
|---|
| 88 |   ValueStorage::getInstance().queryCurrentValue(NAME, alpha);
 | 
|---|
| 89 |   ValueStorage::getInstance().queryCurrentValue("position", Axis);
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   // check whether Axis is valid
 | 
|---|
| 92 |   if (Axis.IsZero())
 | 
|---|
| 93 |     return Action::failure;
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   // convert from degrees to radian
 | 
|---|
| 96 |   alpha *= M_PI/180.;
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   // Creation Line that is the rotation axis
 | 
|---|
| 99 |   Line RotationAxis(Vector(0.,0.,0.), Axis);
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   DoLog(0) && (Log() << Verbose(0) << "Rotate around origin by " << alpha << " radian, axis from origin to " << Axis << "." << endl);
 | 
|---|
| 102 |   // TODO: use AtomSet::rotate?
 | 
|---|
| 103 |   for (std::vector<atom *>::iterator iter = selectedAtoms.begin(); iter != selectedAtoms.end(); ++iter) {
 | 
|---|
| 104 |     (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), alpha));
 | 
|---|
| 105 |   }
 | 
|---|
| 106 |   DoLog(0) && (Log() << Verbose(0) << "done." << endl);
 | 
|---|
| 107 |   return Action::state_ptr(new AtomRotateAroundOriginByAngleState(World::getInstance().getSelectedAtoms(), Axis, alpha));
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | Action::state_ptr AtomRotateAroundOriginByAngleAction::performUndo(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 | Action::state_ptr AtomRotateAroundOriginByAngleAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 124 |   AtomRotateAroundOriginByAngleState *state = assert_cast<AtomRotateAroundOriginByAngleState*>(_state.get());
 | 
|---|
| 125 | 
 | 
|---|
| 126 |   // Creation Line that is the rotation axis
 | 
|---|
| 127 |   Line RotationAxis(Vector(0.,0.,0.), state->Axis);
 | 
|---|
| 128 | 
 | 
|---|
| 129 |   for (std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter) {
 | 
|---|
| 130 |     (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), state->alpha));
 | 
|---|
| 131 |   }
 | 
|---|
| 132 | 
 | 
|---|
| 133 |   return Action::state_ptr(_state);
 | 
|---|
| 134 | }
 | 
|---|
| 135 | 
 | 
|---|
| 136 | bool AtomRotateAroundOriginByAngleAction::canUndo() {
 | 
|---|
| 137 |   return true;
 | 
|---|
| 138 | }
 | 
|---|
| 139 | 
 | 
|---|
| 140 | bool AtomRotateAroundOriginByAngleAction::shouldUndo() {
 | 
|---|
| 141 |   return true;
 | 
|---|
| 142 | }
 | 
|---|
| 143 | 
 | 
|---|
| 144 | const string AtomRotateAroundOriginByAngleAction::getName() {
 | 
|---|
| 145 |   return NAME;
 | 
|---|
| 146 | }
 | 
|---|