| 1 | /* | 
|---|
| 2 | * RepeatBoxAction.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: May 12, 2010 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "Actions/WorldAction/RepeatBoxAction.hpp" | 
|---|
| 9 | #include "CommandLineParser.hpp" | 
|---|
| 10 | #include "atom.hpp" | 
|---|
| 11 | #include "log.hpp" | 
|---|
| 12 | #include "molecule.hpp" | 
|---|
| 13 | #include "vector.hpp" | 
|---|
| 14 | #include "verbose.hpp" | 
|---|
| 15 | #include "World.hpp" | 
|---|
| 16 |  | 
|---|
| 17 | #include <iostream> | 
|---|
| 18 | #include <string> | 
|---|
| 19 |  | 
|---|
| 20 | using namespace std; | 
|---|
| 21 |  | 
|---|
| 22 | #include "UIElements/UIFactory.hpp" | 
|---|
| 23 | #include "UIElements/Dialog.hpp" | 
|---|
| 24 | #include "Actions/MapOfActions.hpp" | 
|---|
| 25 |  | 
|---|
| 26 | const char WorldRepeatBoxAction::NAME[] = "repeat-box"; | 
|---|
| 27 |  | 
|---|
| 28 | WorldRepeatBoxAction::WorldRepeatBoxAction() : | 
|---|
| 29 | Action(NAME) | 
|---|
| 30 | {} | 
|---|
| 31 |  | 
|---|
| 32 | WorldRepeatBoxAction::~WorldRepeatBoxAction() | 
|---|
| 33 | {} | 
|---|
| 34 |  | 
|---|
| 35 | Action::state_ptr WorldRepeatBoxAction::performCall() { | 
|---|
| 36 | Dialog *dialog = UIFactory::getInstance().makeDialog(); | 
|---|
| 37 | Vector Repeater; | 
|---|
| 38 | int count; | 
|---|
| 39 | const element ** Elements; | 
|---|
| 40 | molecule *mol = NULL; | 
|---|
| 41 | int j = 0; | 
|---|
| 42 |  | 
|---|
| 43 | dialog->queryVector(NAME, &Repeater, World::getInstance().getDomain(), false, MapOfActions::getInstance().getDescription(NAME)); | 
|---|
| 44 | dialog->queryMolecule("molecule-by-id", &mol,MapOfActions::getInstance().getDescription("molecule-by-id")); | 
|---|
| 45 |  | 
|---|
| 46 | if(dialog->display()) { | 
|---|
| 47 | double * const cell_size = World::getInstance().getDomain(); | 
|---|
| 48 | Vector x,y; | 
|---|
| 49 | for (int axis = 1; axis <= NDIM; axis++) { | 
|---|
| 50 | Vector ** vectors; | 
|---|
| 51 | Repeater[axis] = floor(Repeater[axis]); | 
|---|
| 52 | if (Repeater[axis] < 1) { | 
|---|
| 53 | DoeLog(1) && (eLog()<< Verbose(1) << "Repetition factor must be greater than 1!" << endl); | 
|---|
| 54 | Repeater[axis] = 1; | 
|---|
| 55 | } | 
|---|
| 56 | mol->CountAtoms();  // recount atoms | 
|---|
| 57 | if (mol->AtomCount != 0) {  // if there is more than none | 
|---|
| 58 | count = mol->AtomCount;   // is changed becausing of adding, thus has to be stored away beforehand | 
|---|
| 59 | Elements = new const element *[count]; | 
|---|
| 60 | vectors = new Vector *[count]; | 
|---|
| 61 | j = 0; | 
|---|
| 62 | atom *first = mol->start; | 
|---|
| 63 | while (first->next != mol->end) {  // make a list of all atoms with coordinates and element | 
|---|
| 64 | first = first->next; | 
|---|
| 65 | Elements[j] = first->type; | 
|---|
| 66 | vectors[j] = &first->x; | 
|---|
| 67 | j++; | 
|---|
| 68 | } | 
|---|
| 69 | if (count != j) | 
|---|
| 70 | DoeLog(1) && (eLog()<< Verbose(1) << "AtomCount " << count << " is not equal to number of atoms in molecule " << j << "!" << endl); | 
|---|
| 71 | x.Zero(); | 
|---|
| 72 | y.Zero(); | 
|---|
| 73 | y[abs(axis)-1] = cell_size[(abs(axis) == 2) ? 2 : ((abs(axis) == 3) ? 5 : 0)] * abs(axis)/axis; // last term is for sign, first is for magnitude | 
|---|
| 74 | for (int i=1;i<Repeater[axis];i++) {  // then add this list with respective translation factor times | 
|---|
| 75 | x += y; // per factor one cell width further | 
|---|
| 76 | for (int k=count;k--;) { // go through every atom of the original cell | 
|---|
| 77 | first = World::getInstance().createAtom(); // create a new body | 
|---|
| 78 | first->x = (*vectors[k]) + x; | 
|---|
| 79 | first->type = Elements[k];  // insert original element | 
|---|
| 80 | mol->AddAtom(first);        // and add to the molecule (which increments ElementsInMolecule, AtomCount, ...) | 
|---|
| 81 | } | 
|---|
| 82 | } | 
|---|
| 83 | // free memory | 
|---|
| 84 | delete[](Elements); | 
|---|
| 85 | delete[](vectors); | 
|---|
| 86 | // correct cell size | 
|---|
| 87 | if (axis < 0) { // if sign was negative, we have to translate everything | 
|---|
| 88 | x =(-(Repeater[axis]-1)) * y; | 
|---|
| 89 | mol->Translate(&x); | 
|---|
| 90 | } | 
|---|
| 91 | cell_size[(abs(axis) == 2) ? 2 : ((abs(axis) == 3) ? 5 : 0)] *= Repeater[axis]; | 
|---|
| 92 | } | 
|---|
| 93 | } | 
|---|
| 94 | delete dialog; | 
|---|
| 95 | return Action::success; | 
|---|
| 96 | } else { | 
|---|
| 97 | delete dialog; | 
|---|
| 98 | return Action::failure; | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | Action::state_ptr WorldRepeatBoxAction::performUndo(Action::state_ptr _state) { | 
|---|
| 103 | //  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get()); | 
|---|
| 104 |  | 
|---|
| 105 | return Action::failure; | 
|---|
| 106 | //  string newName = state->mol->getName(); | 
|---|
| 107 | //  state->mol->setName(state->lastName); | 
|---|
| 108 | // | 
|---|
| 109 | //  return Action::state_ptr(new ParserLoadXyzState(state->mol,newName)); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | Action::state_ptr WorldRepeatBoxAction::performRedo(Action::state_ptr _state){ | 
|---|
| 113 | return Action::failure; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | bool WorldRepeatBoxAction::canUndo() { | 
|---|
| 117 | return false; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | bool WorldRepeatBoxAction::shouldUndo() { | 
|---|
| 121 | return false; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | const string WorldRepeatBoxAction::getName() { | 
|---|
| 125 | return NAME; | 
|---|
| 126 | } | 
|---|