| 1 | /*
|
|---|
| 2 | * TranslateAction.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: May 10, 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/TranslateAction.hpp"
|
|---|
| 16 | #include "Actions/ActionRegistry.hpp"
|
|---|
| 17 | #include "Helpers/Log.hpp"
|
|---|
| 18 | #include "atom.hpp"
|
|---|
| 19 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 20 | #include "Helpers/Verbose.hpp"
|
|---|
| 21 | #include "World.hpp"
|
|---|
| 22 |
|
|---|
| 23 | #include <iostream>
|
|---|
| 24 | #include <fstream>
|
|---|
| 25 | #include <string>
|
|---|
| 26 |
|
|---|
| 27 | using namespace std;
|
|---|
| 28 |
|
|---|
| 29 | #include "UIElements/UIFactory.hpp"
|
|---|
| 30 | #include "UIElements/Dialog.hpp"
|
|---|
| 31 | #include "Actions/ValueStorage.hpp"
|
|---|
| 32 |
|
|---|
| 33 | /****** AtomTranslateAction *****/
|
|---|
| 34 |
|
|---|
| 35 | // memento to remember the state when undoing
|
|---|
| 36 |
|
|---|
| 37 | class AtomTranslateState : public ActionState {
|
|---|
| 38 | public:
|
|---|
| 39 | AtomTranslateState(const std::vector<atom*> &_selectedAtoms, const Vector &_v, const bool _periodic) :
|
|---|
| 40 | selectedAtoms(_selectedAtoms),
|
|---|
| 41 | v(_v),
|
|---|
| 42 | periodic(_periodic)
|
|---|
| 43 | {}
|
|---|
| 44 | std::vector<atom*> selectedAtoms;
|
|---|
| 45 | Vector v;
|
|---|
| 46 | bool periodic;
|
|---|
| 47 | };
|
|---|
| 48 |
|
|---|
| 49 | const char AtomTranslateAction::NAME[] = "translate-atoms";
|
|---|
| 50 |
|
|---|
| 51 | AtomTranslateAction::AtomTranslateAction() :
|
|---|
| 52 | Action(NAME)
|
|---|
| 53 | {}
|
|---|
| 54 |
|
|---|
| 55 | AtomTranslateAction::~AtomTranslateAction()
|
|---|
| 56 | {}
|
|---|
| 57 |
|
|---|
| 58 | void AtomTranslate(Vector &x, bool periodic = false) {
|
|---|
| 59 | ValueStorage::getInstance().setCurrentValue(AtomTranslateAction::NAME, x);
|
|---|
| 60 | ValueStorage::getInstance().setCurrentValue("periodic", periodic);
|
|---|
| 61 | ActionRegistry::getInstance().getActionByName(AtomTranslateAction::NAME)->call(Action::NonInteractive);
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | Dialog* AtomTranslateAction::fillDialog(Dialog *dialog) {
|
|---|
| 65 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
|---|
| 66 |
|
|---|
| 67 | dialog->queryVector(NAME, false, ValueStorage::getInstance().getDescription(NAME));
|
|---|
| 68 | dialog->queryBoolean("periodic", ValueStorage::getInstance().getDescription("periodic"));
|
|---|
| 69 |
|
|---|
| 70 | return dialog;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | Action::state_ptr AtomTranslateAction::performCall() {
|
|---|
| 74 | Vector v;
|
|---|
| 75 | bool periodic = false;
|
|---|
| 76 | Box &domain = World::getInstance().getDomain();
|
|---|
| 77 | std::vector<atom *> selectedAtoms = World::getInstance().getSelectedAtoms();
|
|---|
| 78 |
|
|---|
| 79 | ValueStorage::getInstance().queryCurrentValue(NAME, v);
|
|---|
| 80 | if (!ValueStorage::getInstance().queryCurrentValue("periodic", periodic, true))
|
|---|
| 81 | periodic = false;
|
|---|
| 82 |
|
|---|
| 83 | // TODO: use AtomSet::translate
|
|---|
| 84 | for (std::vector<atom *>::iterator iter = selectedAtoms.begin(); iter != selectedAtoms.end(); ++iter) {
|
|---|
| 85 | *(*iter) += v;
|
|---|
| 86 | if (periodic)
|
|---|
| 87 | (*iter)->setPosition(domain.WrapPeriodically((*iter)->getPosition()));
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | return Action::state_ptr(new AtomTranslateState(selectedAtoms, v, periodic));
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | Action::state_ptr AtomTranslateAction::performUndo(Action::state_ptr _state) {
|
|---|
| 94 | AtomTranslateState *state = assert_cast<AtomTranslateState*>(_state.get());
|
|---|
| 95 | Box &domain = World::getInstance().getDomain();
|
|---|
| 96 |
|
|---|
| 97 | for (std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter) {
|
|---|
| 98 | *(*iter) -= state->v;
|
|---|
| 99 | if (state->periodic)
|
|---|
| 100 | (*iter)->setPosition(domain.WrapPeriodically((*iter)->getPosition()));
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | return Action::state_ptr(_state);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | Action::state_ptr AtomTranslateAction::performRedo(Action::state_ptr _state){
|
|---|
| 107 | AtomTranslateState *state = assert_cast<AtomTranslateState*>(_state.get());
|
|---|
| 108 | Box &domain = World::getInstance().getDomain();
|
|---|
| 109 |
|
|---|
| 110 | for (std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter) {
|
|---|
| 111 | *(*iter) += state->v;
|
|---|
| 112 | if (state->periodic)
|
|---|
| 113 | (*iter)->setPosition(domain.WrapPeriodically((*iter)->getPosition()));
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | return Action::state_ptr(_state);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | bool AtomTranslateAction::canUndo() {
|
|---|
| 120 | return true;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | bool AtomTranslateAction::shouldUndo() {
|
|---|
| 124 | return true;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | const string AtomTranslateAction::getName() {
|
|---|
| 128 | return NAME;
|
|---|
| 129 | }
|
|---|