| [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 |
|
|---|
| [146cff2] | 31 | #include <cmath>
|
|---|
| [22c44bf] | 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 |
|
|---|
| [0b2ce9] | 73 | void AtomRotateAroundOriginByAngleAction::getParametersfromValueStorage()
|
|---|
| 74 | {};
|
|---|
| 75 |
|
|---|
| [22c44bf] | 76 | Dialog* AtomRotateAroundOriginByAngleAction::fillDialog(Dialog *dialog) {
|
|---|
| 77 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
|---|
| 78 |
|
|---|
| [c89fb4] | 79 | dialog->queryDouble(NAME, ValueStorage::getInstance().getDescription(NAME));
|
|---|
| 80 | dialog->queryVector("position", false, ValueStorage::getInstance().getDescription("position"));
|
|---|
| [22c44bf] | 81 |
|
|---|
| 82 | return dialog;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | Action::state_ptr AtomRotateAroundOriginByAngleAction::performCall() {
|
|---|
| 86 | double alpha = 0.;
|
|---|
| 87 | Vector Axis;
|
|---|
| 88 | std::vector<atom *> selectedAtoms = World::getInstance().getSelectedAtoms();
|
|---|
| 89 |
|
|---|
| 90 | // obtain axis to rotate to
|
|---|
| 91 | ValueStorage::getInstance().queryCurrentValue(NAME, alpha);
|
|---|
| 92 | ValueStorage::getInstance().queryCurrentValue("position", Axis);
|
|---|
| 93 |
|
|---|
| 94 | // check whether Axis is valid
|
|---|
| 95 | if (Axis.IsZero())
|
|---|
| 96 | return Action::failure;
|
|---|
| 97 |
|
|---|
| 98 | // convert from degrees to radian
|
|---|
| 99 | alpha *= M_PI/180.;
|
|---|
| 100 |
|
|---|
| 101 | // Creation Line that is the rotation axis
|
|---|
| 102 | Line RotationAxis(Vector(0.,0.,0.), Axis);
|
|---|
| 103 |
|
|---|
| 104 | DoLog(0) && (Log() << Verbose(0) << "Rotate around origin by " << alpha << " radian, axis from origin to " << Axis << "." << endl);
|
|---|
| 105 | // TODO: use AtomSet::rotate?
|
|---|
| 106 | for (std::vector<atom *>::iterator iter = selectedAtoms.begin(); iter != selectedAtoms.end(); ++iter) {
|
|---|
| 107 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), alpha));
|
|---|
| 108 | }
|
|---|
| 109 | DoLog(0) && (Log() << Verbose(0) << "done." << endl);
|
|---|
| 110 | return Action::state_ptr(new AtomRotateAroundOriginByAngleState(World::getInstance().getSelectedAtoms(), Axis, alpha));
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | Action::state_ptr AtomRotateAroundOriginByAngleAction::performUndo(Action::state_ptr _state) {
|
|---|
| 114 | AtomRotateAroundOriginByAngleState *state = assert_cast<AtomRotateAroundOriginByAngleState*>(_state.get());
|
|---|
| 115 |
|
|---|
| 116 | // Creation Line that is the rotation axis
|
|---|
| 117 | Line RotationAxis(Vector(0.,0.,0.), state->Axis);
|
|---|
| 118 |
|
|---|
| 119 | for (std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter) {
|
|---|
| 120 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), -state->alpha));
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | return Action::state_ptr(_state);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | Action::state_ptr AtomRotateAroundOriginByAngleAction::performRedo(Action::state_ptr _state){
|
|---|
| 127 | AtomRotateAroundOriginByAngleState *state = assert_cast<AtomRotateAroundOriginByAngleState*>(_state.get());
|
|---|
| 128 |
|
|---|
| 129 | // Creation Line that is the rotation axis
|
|---|
| 130 | Line RotationAxis(Vector(0.,0.,0.), state->Axis);
|
|---|
| 131 |
|
|---|
| 132 | for (std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter) {
|
|---|
| 133 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), state->alpha));
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | return Action::state_ptr(_state);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | bool AtomRotateAroundOriginByAngleAction::canUndo() {
|
|---|
| 140 | return true;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | bool AtomRotateAroundOriginByAngleAction::shouldUndo() {
|
|---|
| 144 | return true;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | const string AtomRotateAroundOriginByAngleAction::getName() {
|
|---|
| 148 | return NAME;
|
|---|
| 149 | }
|
|---|