source: src/Actions/MoleculeAction/RotateAroundBondAction.cpp@ 788dce

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.1 ChangeBugEmailaddress ChemicalSpaceEvaluator Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_oldresults ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps
Last change on this file since 788dce was 788dce, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

Added RotateAroundBondAction.

  • TESTS: added regression test.
  • DOCU: added explanation to userguide.
  • Property mode set to 100644
File size: 5.6 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2017 Frederik Heber. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * RotateAroundBondAction.cpp
25 *
26 * Created on: Mar 22, 2017
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35//#include "CodePatterns/MemDebug.hpp"
36
37#include "Actions/MoleculeAction/RotateAroundBondAction.hpp"
38
39#include "CodePatterns/Assert.hpp"
40#include "CodePatterns/Log.hpp"
41
42#include "LinearAlgebra/Line.hpp"
43#include "LinearAlgebra/Plane.hpp"
44
45#include "Actions/UndoRedoHelpers.hpp"
46
47#include "Atom/atom.hpp"
48#include "Atom/AtomicInfo.hpp"
49#include "Bond/bond.hpp"
50#include "molecule.hpp"
51#include "World.hpp"
52#include "WorldTime.hpp"
53
54using namespace MoleCuilder;
55
56// and construct the stuff
57#include "RotateAroundBondAction.def"
58#include "Action_impl_pre.hpp"
59/** =========== define the function ====================== */
60ActionState::ptr MoleculeRotateAroundBondAction::performCall()
61{
62 // check preconditions
63 const std::vector< atom *> atoms = World::getInstance().getSelectedAtoms();
64 if (atoms.size() != 2) {
65 STATUS("Exactly two atoms must be selected to specify a bond.");
66 return Action::failure;
67 }
68 if (!atoms[0]->IsBondedTo(WorldTime::getTime(), atoms[1])) {
69 STATUS("Two given atoms are not bonded.");
70 return Action::failure;
71 }
72 molecule *mol = World::getInstance().
73 getMolecule(MoleculeById(atoms[0]->getMolecule()->getId()));
74 if (mol != atoms[1]->getMolecule()) {
75 STATUS("The two selected atoms must belong to the same molecule.");
76 return Action::failure;
77 }
78
79 // gather undo information: store position of all atoms of molecule
80 std::vector<AtomicInfo> UndoInfo;
81 UndoInfo.reserve(mol->size());
82 {
83 for (molecule::const_iterator iter = const_cast<molecule const *>(mol)->begin();
84 iter != const_cast<molecule const *>(mol)->end();
85 ++iter) {
86 const atom * const Walker = *iter;
87 UndoInfo.push_back(AtomicInfo(*Walker));
88 }
89 }
90
91 // convert from degrees to radian
92 const double angle_radian = params.angle.get() * M_PI/180.;
93
94 // create the bond plane and mid-distance
95 Vector NormalVector = (atoms[0]->getPosition() - atoms[1]->getPosition());
96 NormalVector.Normalize();
97 const Vector OffsetVector = 0.5*(atoms[0]->getPosition() + atoms[1]->getPosition());
98 Plane bondplane(NormalVector, OffsetVector);
99 Line RotationAxis(OffsetVector, NormalVector);
100 // go through the molecule and rotate each atom relative two plane
101 for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) {
102 const Vector &position = (*iter)->getPosition();
103 const double signed_distance = bondplane.SignedDistance(position);
104 LOG(3, "DEBUG: Inspecting atom " << **iter << " at " << position);
105 if ((params.bondside.get() && (signed_distance > 0)) ||
106 (!params.bondside.get() && (signed_distance < 0))) {
107 LOG(4, "DEBUG: Rotating atom " << **iter << " by " << angle_radian
108 << " rad around " << RotationAxis);
109 (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), angle_radian));
110 LOG(4, "DEBUG: New position of atom " << **iter);
111 ASSERT( fabs(signed_distance - bondplane.SignedDistance(position)) < MYEPSILON,
112 "MoleculeRotateAroundBondAction::performCall() - signed distance was "
113 +toString(signed_distance)+" and is now "+toString(bondplane.SignedDistance(position)));
114 }
115 }
116
117 MoleculeRotateAroundBondState *UndoState =
118 new MoleculeRotateAroundBondState(UndoInfo, OffsetVector, NormalVector, mol, params);
119 return ActionState::ptr(UndoState);
120}
121
122ActionState::ptr MoleculeRotateAroundBondAction::performUndo(ActionState::ptr _state) {
123 MoleculeRotateAroundBondState *state = assert_cast<MoleculeRotateAroundBondState*>(_state.get());
124
125 // set stored old state
126 SetAtomsFromAtomicInfo(state->UndoInfo);
127
128 return ActionState::ptr(_state);
129}
130
131ActionState::ptr MoleculeRotateAroundBondAction::performRedo(ActionState::ptr _state){
132 MoleculeRotateAroundBondState *state = assert_cast<MoleculeRotateAroundBondState*>(_state.get());
133
134 // create the bond plane and mid-distance
135 Plane bondplane(state->NormalVector, state->OffsetVector);
136 Line RotationAxis(state->OffsetVector, state->NormalVector);
137 const double angle_radian = params.angle.get() * M_PI/180.;
138 // go through the molecule and rotate each atom relative two plane
139 for (molecule::iterator iter = state->mol->begin(); iter != state->mol->end(); ++iter) {
140 const Vector &position = (*iter)->getPosition();
141 if ((params.bondside.get() && (bondplane.SignedDistance(position) > 0)) ||
142 (!params.bondside.get() && (bondplane.SignedDistance(position) < 0))) {
143 (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), angle_radian));
144 }
145 }
146
147 return ActionState::ptr(_state);
148}
149
150bool MoleculeRotateAroundBondAction::canUndo() {
151 return true;
152}
153
154bool MoleculeRotateAroundBondAction::shouldUndo() {
155 return true;
156}
157/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.