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