| 1 | /*
 | 
|---|
| 2 |  * AddEmptyBoundaryAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: May 8, 2010
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "Actions/WorldAction/AddEmptyBoundaryAction.hpp"
 | 
|---|
| 11 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 12 | #include "atom.hpp"
 | 
|---|
| 13 | #include "Helpers/Log.hpp"
 | 
|---|
| 14 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 15 | #include "World.hpp"
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <iostream>
 | 
|---|
| 18 | #include <string>
 | 
|---|
| 19 | #include <vector>
 | 
|---|
| 20 | 
 | 
|---|
| 21 | using namespace std;
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 24 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 25 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| 26 | #include "Helpers/Assert.hpp"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | const char WorldAddEmptyBoundaryAction::NAME[] = "boundary";
 | 
|---|
| 29 | 
 | 
|---|
| 30 | WorldAddEmptyBoundaryAction::WorldAddEmptyBoundaryAction() :
 | 
|---|
| 31 |   Action(NAME)
 | 
|---|
| 32 | {}
 | 
|---|
| 33 | 
 | 
|---|
| 34 | WorldAddEmptyBoundaryAction::~WorldAddEmptyBoundaryAction()
 | 
|---|
| 35 | {}
 | 
|---|
| 36 | 
 | 
|---|
| 37 | void WorldAddEmptyBoundary(Vector &boundary) {
 | 
|---|
| 38 |   ValueStorage::getInstance().setCurrentValue(WorldAddEmptyBoundaryAction::NAME, boundary);
 | 
|---|
| 39 |   ActionRegistry::getInstance().getActionByName(WorldAddEmptyBoundaryAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 40 | };
 | 
|---|
| 41 | 
 | 
|---|
| 42 | Dialog* WorldAddEmptyBoundaryAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 43 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 44 | 
 | 
|---|
| 45 |   dialog->queryVector(NAME, false, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 46 | 
 | 
|---|
| 47 |   return dialog;
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | Action::state_ptr WorldAddEmptyBoundaryAction::performCall() {
 | 
|---|
| 51 |   Vector boundary;
 | 
|---|
| 52 |   Vector Min;
 | 
|---|
| 53 |   Vector Max;
 | 
|---|
| 54 |   int j=0;
 | 
|---|
| 55 | 
 | 
|---|
| 56 |   ValueStorage::getInstance().queryCurrentValue(NAME, boundary);
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   // get maximum and minimum
 | 
|---|
| 59 |   vector<atom *> AllAtoms = World::getInstance().getAllAtoms();
 | 
|---|
| 60 |   ASSERT(AllAtoms.size() > 0, "There must be atoms present for AddingEmptyBoundary.");
 | 
|---|
| 61 |   vector<atom *>::iterator AtomRunner = AllAtoms.begin();
 | 
|---|
| 62 |   Min = (*AtomRunner)->getPosition();
 | 
|---|
| 63 |   Max = (*AtomRunner)->getPosition();
 | 
|---|
| 64 |   for (; AtomRunner != AllAtoms.end(); ++AtomRunner) {
 | 
|---|
| 65 |     for (int i=0;i<NDIM;i++) {
 | 
|---|
| 66 |       if ((*AtomRunner)->at(i) > Max[i])
 | 
|---|
| 67 |         Max[i] = (*AtomRunner)->at(i);
 | 
|---|
| 68 |       if ((*AtomRunner)->at(i) < Min[i])
 | 
|---|
| 69 |         Min[i] = (*AtomRunner)->at(i);
 | 
|---|
| 70 |     }
 | 
|---|
| 71 |   }
 | 
|---|
| 72 |   // set new box size
 | 
|---|
| 73 |   double * const cell_size = new double[6];
 | 
|---|
| 74 |   for (j=0;j<6;j++)
 | 
|---|
| 75 |     cell_size[j] = 0.;
 | 
|---|
| 76 |   j=-1;
 | 
|---|
| 77 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
| 78 |     j += i+1;
 | 
|---|
| 79 |     cell_size[j] = (Max[i]-Min[i]+2.*boundary[i]);
 | 
|---|
| 80 |   }
 | 
|---|
| 81 |   World::getInstance().setDomain(cell_size);
 | 
|---|
| 82 |   delete[] cell_size;
 | 
|---|
| 83 |   // translate all atoms, such that Min is aty (0,0,0)
 | 
|---|
| 84 |   AtomRunner = AllAtoms.begin();
 | 
|---|
| 85 |   for (; AtomRunner != AllAtoms.end(); ++AtomRunner)
 | 
|---|
| 86 |     *(*AtomRunner) -= Min - boundary;
 | 
|---|
| 87 |   return Action::success;
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | Action::state_ptr WorldAddEmptyBoundaryAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 91 | //  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   return Action::failure;
 | 
|---|
| 94 | //  string newName = state->mol->getName();
 | 
|---|
| 95 | //  state->mol->setName(state->lastName);
 | 
|---|
| 96 | //
 | 
|---|
| 97 | //  return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | Action::state_ptr WorldAddEmptyBoundaryAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 101 |   return Action::failure;
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | bool WorldAddEmptyBoundaryAction::canUndo() {
 | 
|---|
| 105 |   return false;
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | bool WorldAddEmptyBoundaryAction::shouldUndo() {
 | 
|---|
| 109 |   return false;
 | 
|---|
| 110 | }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | const string WorldAddEmptyBoundaryAction::getName() {
 | 
|---|
| 113 |   return NAME;
 | 
|---|
| 114 | }
 | 
|---|