| [1cc87e] | 1 | /*
 | 
|---|
 | 2 |  * AllAtomsInsideSphereAction.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Aug 9, 2010
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 9 | 
 | 
|---|
 | 10 | #include "Actions/SelectionAction/AllAtomsInsideSphereAction.hpp"
 | 
|---|
 | 11 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
 | 12 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
 | 13 | #include "Descriptors/AtomShapeDescriptor.hpp"
 | 
|---|
 | 14 | #include "atom.hpp"
 | 
|---|
 | 15 | #include "Helpers/Log.hpp"
 | 
|---|
 | 16 | #include "Helpers/Verbose.hpp"
 | 
|---|
 | 17 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
 | 18 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
 | 19 | #include "Shapes/Shape.hpp"
 | 
|---|
 | 20 | #include "Shapes/ShapeOps.hpp"
 | 
|---|
 | 21 | #include "World.hpp"
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | #include <iostream>
 | 
|---|
 | 24 | #include <string>
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | using namespace std;
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | #include "UIElements/UIFactory.hpp"
 | 
|---|
 | 29 | #include "UIElements/Dialog.hpp"
 | 
|---|
 | 30 | #include "Actions/ValueStorage.hpp"
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | // memento to remember the state when undoing
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | class SelectionAllAtomsInsideSphereState : public ActionState {
 | 
|---|
 | 36 | public:
 | 
|---|
 | 37 |   SelectionAllAtomsInsideSphereState(std::vector<atom*> _selectedAtoms, const Vector &_position, const double _radius) :
 | 
|---|
 | 38 |     selectedAtoms(_selectedAtoms),
 | 
|---|
 | 39 |     position(_position),
 | 
|---|
 | 40 |     radius(_radius)
 | 
|---|
 | 41 |   {}
 | 
|---|
 | 42 |   std::vector<atom*> selectedAtoms;
 | 
|---|
 | 43 |   Vector position;
 | 
|---|
 | 44 |   double radius;
 | 
|---|
 | 45 | };
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 | const char SelectionAllAtomsInsideSphereAction::NAME[] = "select-atoms-inside-sphere";
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | SelectionAllAtomsInsideSphereAction::SelectionAllAtomsInsideSphereAction() :
 | 
|---|
 | 50 |   Action(NAME)
 | 
|---|
 | 51 | {}
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | SelectionAllAtomsInsideSphereAction::~SelectionAllAtomsInsideSphereAction()
 | 
|---|
 | 54 | {}
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 | void SelectionAllAtomsInsideSphere(const Vector &position, const double radius) {
 | 
|---|
 | 57 |   ValueStorage::getInstance().setCurrentValue(SelectionAllAtomsInsideSphereAction::NAME, radius);
 | 
|---|
 | 58 |   ValueStorage::getInstance().setCurrentValue("position", position);
 | 
|---|
 | 59 |   ActionRegistry::getInstance().getActionByName(SelectionAllAtomsInsideSphereAction::NAME)->call(Action::NonInteractive);
 | 
|---|
 | 60 | };
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 | Dialog* SelectionAllAtomsInsideSphereAction::fillDialog(Dialog *dialog) {
 | 
|---|
 | 63 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 |   dialog->queryDouble(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
 | 66 |   dialog->queryVector("position", false, ValueStorage::getInstance().getDescription("position"));
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 |   return dialog;
 | 
|---|
 | 69 | }
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 | Action::state_ptr SelectionAllAtomsInsideSphereAction::performCall() {
 | 
|---|
 | 72 |   std::vector<atom *> selectedAtoms = World::getInstance().getSelectedAtoms();
 | 
|---|
 | 73 |   double radius = 0.;
 | 
|---|
 | 74 |   Vector position;
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 |   ValueStorage::getInstance().queryCurrentValue(NAME, radius);
 | 
|---|
 | 77 |   ValueStorage::getInstance().queryCurrentValue("position", position);
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 |   DoLog(1) && (Log() << Verbose(1) << "Selecting all atoms inside a sphere at " << position << " with radius " << radius << "." << endl);
 | 
|---|
 | 80 |   Shape s = translate(resize(Sphere(),radius),position);
 | 
|---|
 | 81 |   World::getInstance().selectAllAtoms(AtomByShape(s));
 | 
|---|
 | 82 |   return Action::state_ptr(new SelectionAllAtomsInsideSphereState(selectedAtoms, position, radius));
 | 
|---|
 | 83 | }
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 | Action::state_ptr SelectionAllAtomsInsideSphereAction::performUndo(Action::state_ptr _state) {
 | 
|---|
 | 86 |   SelectionAllAtomsInsideSphereState *state = assert_cast<SelectionAllAtomsInsideSphereState*>(_state.get());
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 |   World::getInstance().clearAtomSelection();
 | 
|---|
 | 89 |   for(std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter)
 | 
|---|
 | 90 |     World::getInstance().selectAtom(*iter);
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |   return Action::state_ptr(new SelectionAllAtomsInsideSphereState(state->selectedAtoms, state->position, state->radius));
 | 
|---|
 | 93 | }
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 | Action::state_ptr SelectionAllAtomsInsideSphereAction::performRedo(Action::state_ptr _state){
 | 
|---|
 | 96 |   SelectionAllAtomsInsideSphereState *state = assert_cast<SelectionAllAtomsInsideSphereState*>(_state.get());
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 |   Shape s = translate(resize(Sphere(),state->radius),state->position);
 | 
|---|
 | 99 |   World::getInstance().selectAllAtoms(AtomByShape(s));
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 |   return Action::state_ptr(new SelectionAllAtomsInsideSphereState(state->selectedAtoms, state->position, state->radius));
 | 
|---|
 | 102 | }
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 | bool SelectionAllAtomsInsideSphereAction::canUndo() {
 | 
|---|
 | 105 |   return true;
 | 
|---|
 | 106 | }
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | bool SelectionAllAtomsInsideSphereAction::shouldUndo() {
 | 
|---|
 | 109 |   return true;
 | 
|---|
 | 110 | }
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 | const string SelectionAllAtomsInsideSphereAction::getName() {
 | 
|---|
 | 113 |   return NAME;
 | 
|---|
 | 114 | }
 | 
|---|