source: src/Actions/MoleculeAction/RotateAroundBondAction.cpp@ 5e17bf

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 5e17bf was 5e17bf, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

Added test in RotateAroundBondAction whether given bond is cyclic.

  • TESTS: also added regression test.
  • Property mode set to 100644
File size: 6.3 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#include "Graph/CyclicStructureAnalysis.hpp"
47#include "Graph/DepthFirstSearchAnalysis.hpp"
48
49#include "Atom/atom.hpp"
50#include "Atom/AtomicInfo.hpp"
51#include "Bond/bond.hpp"
52#include "molecule.hpp"
53#include "World.hpp"
54#include "WorldTime.hpp"
55
56using namespace MoleCuilder;
57
58// and construct the stuff
59#include "RotateAroundBondAction.def"
60#include "Action_impl_pre.hpp"
61
62/** =========== define the function ====================== */
63static bool IsBondContainedInCycle(const bond::ptr &_bond)
64{
65 // get the BackEdgeStack from somewhere
66 DepthFirstSearchAnalysis DFS;
67 DFS();
68 DFS.CyclicBondAnalysis();
69// std::deque<bond::ptr> BackEdgeStack = DFS.getBackEdgeStack();
70// // then we analyse the cycles and get them
71// CyclicStructureAnalysis CycleAnalysis(ExcludeHydrogen); // hydrogens never contained in cycles
72// CycleAnalysis(&BackEdgeStack);
73 return _bond->Cyclic;
74}
75
76ActionState::ptr MoleculeRotateAroundBondAction::performCall()
77{
78 // check preconditions
79 const std::vector< atom *> atoms = World::getInstance().getSelectedAtoms();
80 if (atoms.size() != 2) {
81 STATUS("Exactly two atoms must be selected to specify a bond.");
82 return Action::failure;
83 }
84 if (!atoms[0]->IsBondedTo(WorldTime::getTime(), atoms[1])) {
85 STATUS("Two given atoms are not bonded.");
86 return Action::failure;
87 }
88 molecule *mol = World::getInstance().
89 getMolecule(MoleculeById(atoms[0]->getMolecule()->getId()));
90 if (mol != atoms[1]->getMolecule()) {
91 STATUS("The two selected atoms must belong to the same molecule.");
92 return Action::failure;
93 }
94 if (IsBondContainedInCycle(atoms[0]->getBond(atoms[1]))) {
95 STATUS("The given bond is contained in a cycle, cannot rotate!");
96 return Action::failure;
97 }
98
99 // gather undo information: store position of all atoms of molecule
100 std::vector<AtomicInfo> UndoInfo;
101 UndoInfo.reserve(mol->size());
102 {
103 for (molecule::const_iterator iter = const_cast<molecule const *>(mol)->begin();
104 iter != const_cast<molecule const *>(mol)->end();
105 ++iter) {
106 const atom * const Walker = *iter;
107 UndoInfo.push_back(AtomicInfo(*Walker));
108 }
109 }
110
111 // convert from degrees to radian
112 const double angle_radian = params.angle.get() * M_PI/180.;
113
114 // create the bond plane and mid-distance
115 Vector NormalVector = (atoms[0]->getPosition() - atoms[1]->getPosition());
116 NormalVector.Normalize();
117 const Vector OffsetVector = 0.5*(atoms[0]->getPosition() + atoms[1]->getPosition());
118 Plane bondplane(NormalVector, OffsetVector);
119 Line RotationAxis(OffsetVector, NormalVector);
120 // go through the molecule and rotate each atom relative two plane
121 for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) {
122 const Vector &position = (*iter)->getPosition();
123 const double signed_distance = bondplane.SignedDistance(position);
124 LOG(3, "DEBUG: Inspecting atom " << **iter << " at " << position);
125 if ((params.bondside.get() && (signed_distance > 0)) ||
126 (!params.bondside.get() && (signed_distance < 0))) {
127 LOG(4, "DEBUG: Rotating atom " << **iter << " by " << angle_radian
128 << " rad around " << RotationAxis);
129 (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), angle_radian));
130 LOG(4, "DEBUG: New position of atom " << **iter);
131 ASSERT( fabs(signed_distance - bondplane.SignedDistance(position)) < MYEPSILON,
132 "MoleculeRotateAroundBondAction::performCall() - signed distance was "
133 +toString(signed_distance)+" and is now "+toString(bondplane.SignedDistance(position)));
134 }
135 }
136
137 MoleculeRotateAroundBondState *UndoState =
138 new MoleculeRotateAroundBondState(UndoInfo, OffsetVector, NormalVector, mol, params);
139 return ActionState::ptr(UndoState);
140}
141
142ActionState::ptr MoleculeRotateAroundBondAction::performUndo(ActionState::ptr _state) {
143 MoleculeRotateAroundBondState *state = assert_cast<MoleculeRotateAroundBondState*>(_state.get());
144
145 // set stored old state
146 SetAtomsFromAtomicInfo(state->UndoInfo);
147
148 return ActionState::ptr(_state);
149}
150
151ActionState::ptr MoleculeRotateAroundBondAction::performRedo(ActionState::ptr _state){
152 MoleculeRotateAroundBondState *state = assert_cast<MoleculeRotateAroundBondState*>(_state.get());
153
154 // create the bond plane and mid-distance
155 Plane bondplane(state->NormalVector, state->OffsetVector);
156 Line RotationAxis(state->OffsetVector, state->NormalVector);
157 const double angle_radian = params.angle.get() * M_PI/180.;
158 // go through the molecule and rotate each atom relative two plane
159 for (molecule::iterator iter = state->mol->begin(); iter != state->mol->end(); ++iter) {
160 const Vector &position = (*iter)->getPosition();
161 if ((params.bondside.get() && (bondplane.SignedDistance(position) > 0)) ||
162 (!params.bondside.get() && (bondplane.SignedDistance(position) < 0))) {
163 (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), angle_radian));
164 }
165 }
166
167 return ActionState::ptr(_state);
168}
169
170bool MoleculeRotateAroundBondAction::canUndo() {
171 return true;
172}
173
174bool MoleculeRotateAroundBondAction::shouldUndo() {
175 return true;
176}
177/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.