[1cc87e] | 1 | /*
|
---|
| 2 | * AllAtomsInsideCuboidAction.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Aug 9, 2010
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[bf3817] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[1cc87e] | 13 | #include "Helpers/MemDebug.hpp"
|
---|
| 14 |
|
---|
| 15 | #include "Actions/SelectionAction/AllAtomsInsideCuboidAction.hpp"
|
---|
| 16 | #include "Actions/ActionRegistry.hpp"
|
---|
| 17 | #include "Descriptors/AtomDescriptor.hpp"
|
---|
| 18 | #include "Descriptors/AtomShapeDescriptor.hpp"
|
---|
| 19 | #include "atom.hpp"
|
---|
| 20 | #include "Helpers/Log.hpp"
|
---|
| 21 | #include "Helpers/Verbose.hpp"
|
---|
[31fb1d] | 22 | #include "LinearAlgebra/Matrix.hpp"
|
---|
[1cc87e] | 23 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 24 | #include "Shapes/BaseShapes.hpp"
|
---|
| 25 | #include "Shapes/Shape.hpp"
|
---|
| 26 | #include "Shapes/ShapeOps.hpp"
|
---|
| 27 | #include "World.hpp"
|
---|
| 28 |
|
---|
| 29 | #include <iostream>
|
---|
| 30 | #include <string>
|
---|
| 31 |
|
---|
| 32 | using namespace std;
|
---|
| 33 |
|
---|
| 34 | #include "UIElements/UIFactory.hpp"
|
---|
| 35 | #include "UIElements/Dialog.hpp"
|
---|
| 36 | #include "Actions/ValueStorage.hpp"
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | // memento to remember the state when undoing
|
---|
| 40 |
|
---|
| 41 | class SelectionAllAtomsInsideCuboidState : public ActionState {
|
---|
| 42 | public:
|
---|
[31fb1d] | 43 | SelectionAllAtomsInsideCuboidState(std::vector<atom*> _selectedAtoms, const Vector &_position, const Vector &_extension, const double _Xangle, const double _Yangle, const double _Zangle) :
|
---|
[1cc87e] | 44 | selectedAtoms(_selectedAtoms),
|
---|
| 45 | position(_position),
|
---|
[31fb1d] | 46 | extension(_extension),
|
---|
| 47 | Xangle(_Xangle),
|
---|
| 48 | Yangle(_Yangle),
|
---|
| 49 | Zangle(_Zangle)
|
---|
[1cc87e] | 50 | {}
|
---|
| 51 | std::vector<atom*> selectedAtoms;
|
---|
| 52 | Vector position;
|
---|
| 53 | Vector extension;
|
---|
[31fb1d] | 54 | double Xangle;
|
---|
| 55 | double Yangle;
|
---|
| 56 | double Zangle;
|
---|
[1cc87e] | 57 | };
|
---|
| 58 |
|
---|
| 59 | const char SelectionAllAtomsInsideCuboidAction::NAME[] = "select-atoms-inside-cuboid";
|
---|
| 60 |
|
---|
| 61 | SelectionAllAtomsInsideCuboidAction::SelectionAllAtomsInsideCuboidAction() :
|
---|
| 62 | Action(NAME)
|
---|
| 63 | {}
|
---|
| 64 |
|
---|
| 65 | SelectionAllAtomsInsideCuboidAction::~SelectionAllAtomsInsideCuboidAction()
|
---|
| 66 | {}
|
---|
| 67 |
|
---|
| 68 | void SelectionAllAtomsInsideCuboid(const Vector &position, const Vector &extension) {
|
---|
| 69 | ValueStorage::getInstance().setCurrentValue(SelectionAllAtomsInsideCuboidAction::NAME, extension);
|
---|
| 70 | ValueStorage::getInstance().setCurrentValue("position", position);
|
---|
| 71 | ActionRegistry::getInstance().getActionByName(SelectionAllAtomsInsideCuboidAction::NAME)->call(Action::NonInteractive);
|
---|
| 72 | };
|
---|
| 73 |
|
---|
[31fb1d] | 74 | void SelectionAllAtomsInsideCuboid(const Vector &position, const Vector &extension, const double Xangle, const double Yangle, const double Zangle) {
|
---|
| 75 | ValueStorage::getInstance().setCurrentValue(SelectionAllAtomsInsideCuboidAction::NAME, extension);
|
---|
| 76 | ValueStorage::getInstance().setCurrentValue("position", position);
|
---|
| 77 | ValueStorage::getInstance().setCurrentValue("angle-x", Xangle);
|
---|
| 78 | ValueStorage::getInstance().setCurrentValue("angle-y", Yangle);
|
---|
| 79 | ValueStorage::getInstance().setCurrentValue("angle-z", Zangle);
|
---|
| 80 | ActionRegistry::getInstance().getActionByName(SelectionAllAtomsInsideCuboidAction::NAME)->call(Action::NonInteractive);
|
---|
| 81 | };
|
---|
| 82 |
|
---|
[1cc87e] | 83 | Dialog* SelectionAllAtomsInsideCuboidAction::fillDialog(Dialog *dialog) {
|
---|
| 84 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
| 85 |
|
---|
| 86 | dialog->queryVector(NAME, false, ValueStorage::getInstance().getDescription(NAME));
|
---|
| 87 | dialog->queryVector("position", false, ValueStorage::getInstance().getDescription("position"));
|
---|
| 88 |
|
---|
| 89 | return dialog;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | Action::state_ptr SelectionAllAtomsInsideCuboidAction::performCall() {
|
---|
| 93 | std::vector<atom *> selectedAtoms = World::getInstance().getSelectedAtoms();
|
---|
| 94 | Vector position;
|
---|
| 95 | Vector extension;
|
---|
[31fb1d] | 96 | double Xangle = 0.;
|
---|
| 97 | double Yangle = 0.;
|
---|
| 98 | double Zangle = 0.;
|
---|
| 99 | Matrix RotationMatrix;
|
---|
[1cc87e] | 100 |
|
---|
| 101 | ValueStorage::getInstance().queryCurrentValue("position", position);
|
---|
| 102 | ValueStorage::getInstance().queryCurrentValue(NAME, extension);
|
---|
[31fb1d] | 103 | // check whether a rotation is given.
|
---|
| 104 | if (ValueStorage::getInstance().queryCurrentValue("angle-x", Xangle, true))
|
---|
| 105 | Xangle = 0.;
|
---|
| 106 | if (ValueStorage::getInstance().queryCurrentValue("angle-y", Yangle, true))
|
---|
| 107 | Yangle = 0.;
|
---|
| 108 | if (ValueStorage::getInstance().queryCurrentValue("angle-z", Zangle, true))
|
---|
| 109 | Zangle = 0.;
|
---|
| 110 | RotationMatrix.rotation(Xangle, Yangle, Zangle);
|
---|
| 111 |
|
---|
| 112 | DoLog(1) && (Log() << Verbose(1) << "Selecting all atoms inside a rotated " << RotationMatrix << " cuboid at " << position << " and extension of " << extension << "." << endl);
|
---|
| 113 | Shape s = translate(transform(stretch(Cuboid(),extension),RotationMatrix),position);
|
---|
[1cc87e] | 114 | World::getInstance().selectAllAtoms(AtomByShape(s));
|
---|
[31fb1d] | 115 | return Action::state_ptr(new SelectionAllAtomsInsideCuboidState(selectedAtoms, position, extension, Xangle, Yangle, Zangle));
|
---|
[1cc87e] | 116 | }
|
---|
| 117 |
|
---|
| 118 | Action::state_ptr SelectionAllAtomsInsideCuboidAction::performUndo(Action::state_ptr _state) {
|
---|
| 119 | SelectionAllAtomsInsideCuboidState *state = assert_cast<SelectionAllAtomsInsideCuboidState*>(_state.get());
|
---|
| 120 |
|
---|
| 121 | World::getInstance().clearAtomSelection();
|
---|
| 122 | for(std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter)
|
---|
| 123 | World::getInstance().selectAtom(*iter);
|
---|
| 124 |
|
---|
[31fb1d] | 125 | return Action::state_ptr(_state);
|
---|
[1cc87e] | 126 | }
|
---|
| 127 |
|
---|
| 128 | Action::state_ptr SelectionAllAtomsInsideCuboidAction::performRedo(Action::state_ptr _state){
|
---|
| 129 | SelectionAllAtomsInsideCuboidState *state = assert_cast<SelectionAllAtomsInsideCuboidState*>(_state.get());
|
---|
[31fb1d] | 130 | Matrix RotationMatrix;
|
---|
[1cc87e] | 131 |
|
---|
[31fb1d] | 132 | RotationMatrix.rotation(state->Xangle, state->Yangle, state->Zangle);
|
---|
| 133 | Shape s = translate(transform(stretch(Cuboid(),state->extension),RotationMatrix),state->position);
|
---|
[1cc87e] | 134 | World::getInstance().selectAllAtoms(AtomByShape(s));
|
---|
| 135 |
|
---|
[31fb1d] | 136 | return Action::state_ptr(_state);
|
---|
[1cc87e] | 137 | }
|
---|
| 138 |
|
---|
| 139 | bool SelectionAllAtomsInsideCuboidAction::canUndo() {
|
---|
| 140 | return true;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | bool SelectionAllAtomsInsideCuboidAction::shouldUndo() {
|
---|
| 144 | return true;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | const string SelectionAllAtomsInsideCuboidAction::getName() {
|
---|
| 148 | return NAME;
|
---|
| 149 | }
|
---|