1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2012 University of Bonn. 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 | * ChangeBondAngleAction.cpp
|
---|
25 | *
|
---|
26 | * Created on: Nov 14, 2012
|
---|
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/ChangeBondAngleAction.hpp"
|
---|
38 |
|
---|
39 | #include <boost/assign.hpp>
|
---|
40 |
|
---|
41 | #include "CodePatterns/Log.hpp"
|
---|
42 | #include "CodePatterns/Verbose.hpp"
|
---|
43 |
|
---|
44 | #include "LinearAlgebra/Plane.hpp"
|
---|
45 | #include "LinearAlgebra/Vector.hpp"
|
---|
46 |
|
---|
47 | #include "Atom/atom.hpp"
|
---|
48 | #include "Bond/bond.hpp"
|
---|
49 | #include "CodePatterns/Log.hpp"
|
---|
50 | #include "CodePatterns/Verbose.hpp"
|
---|
51 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
52 | #include "molecule.hpp"
|
---|
53 | #include "World.hpp"
|
---|
54 | #include "WorldTime.hpp"
|
---|
55 |
|
---|
56 | using namespace boost::assign;
|
---|
57 | using namespace MoleCuilder;
|
---|
58 |
|
---|
59 | // and construct the stuff
|
---|
60 | #include "ChangeBondAngleAction.def"
|
---|
61 | #include "Action_impl_pre.hpp"
|
---|
62 | /** =========== define the function ====================== */
|
---|
63 |
|
---|
64 | const std::vector<size_t> sortIndicesFromCentralAtom(const std::vector< atom *> &atoms)
|
---|
65 | {
|
---|
66 | std::vector<size_t> indices;
|
---|
67 | {
|
---|
68 | std::vector<size_t> matches;
|
---|
69 | size_t index = 0;
|
---|
70 | for (; index < 3; ++index)
|
---|
71 | if ((atoms[index]->IsBondedTo(WorldTime::getTime(), atoms[(index+1)%3]))
|
---|
72 | && (atoms[index]->IsBondedTo(WorldTime::getTime(), atoms[(index+2)%3])))
|
---|
73 | matches.push_back(index);
|
---|
74 | if (matches.size() != 1) {
|
---|
75 | ELOG(1, "The three atoms are not linearly bonded.");
|
---|
76 | } else {
|
---|
77 | LOG(1, "INFO: Picking " << *atoms[ matches[0] ] << " as the central atom.");
|
---|
78 | indices += matches[0], (matches[0]+1)%3, (matches[0]+2)%3;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | return indices;
|
---|
82 | }
|
---|
83 |
|
---|
84 | void ChangeBondAngleSymmetrically(
|
---|
85 | const std::vector<size_t> &indices,
|
---|
86 | const double newangle,
|
---|
87 | const std::vector<atom*> &atoms)
|
---|
88 | {
|
---|
89 | const Vector firstPosition = atoms[ indices[1] ]->getPosition();
|
---|
90 | const Vector secondPosition = atoms[ indices[2] ]->getPosition();
|
---|
91 |
|
---|
92 | // create the bond plane and mid-distance
|
---|
93 | const double normaldistance = firstPosition.distance(secondPosition);
|
---|
94 | const Vector NormalVector = (1. / normaldistance) * (firstPosition - secondPosition);
|
---|
95 | const Vector OffsetVector = atoms[indices[0]]->getPosition();
|
---|
96 | Plane midplane(NormalVector, OffsetVector);
|
---|
97 | // go through the molecule and stretch each atom relative two plane
|
---|
98 | for (size_t index = 1; index < 3; ++index) {
|
---|
99 | const Vector &position = atoms[indices[index]]->getPosition();
|
---|
100 | const Vector planespot = midplane.getClosestPoint(position);
|
---|
101 | const double opposite_side = position.distance(planespot);
|
---|
102 | const double adjacent_side = OffsetVector.distance(planespot);
|
---|
103 | const double hypotenuse = position.distance(OffsetVector);
|
---|
104 | if (fabs(hypotenuse) > MYEPSILON) {
|
---|
105 | const double angle = acos(adjacent_side / hypotenuse);
|
---|
106 | LOG(1, "INFO: Old angle is " << (180. / M_PI) * (2. * angle) << ".");
|
---|
107 | const double new_opposite_side = hypotenuse * sin(newangle);
|
---|
108 | const double new_adjacent_side = hypotenuse * cos(newangle);
|
---|
109 | Vector newposition = (new_opposite_side / opposite_side) * (position - planespot);
|
---|
110 | newposition += (new_adjacent_side / adjacent_side) * (planespot - OffsetVector);
|
---|
111 | newposition += OffsetVector;
|
---|
112 | atoms[indices[index]]->setPosition(newposition);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | ActionState::ptr MoleculeChangeBondAngleAction::performCall()
|
---|
118 | {
|
---|
119 | // check precondition: three atoms
|
---|
120 | const std::vector< atom *> atoms = World::getInstance().getSelectedAtoms();
|
---|
121 | if (atoms.size() != 3) {
|
---|
122 | STATUS("Exactly three atoms must be selected.");
|
---|
123 | return Action::failure;
|
---|
124 | }
|
---|
125 | // check precondition: linearly bonded atoms
|
---|
126 | const std::vector<size_t> indices = sortIndicesFromCentralAtom(atoms);
|
---|
127 | if (indices.size() != 3) {
|
---|
128 | STATUS("Selected atoms must be linearly bonded/form a chain");
|
---|
129 | return Action::failure;
|
---|
130 | }
|
---|
131 | const molecule *mol = atoms[0]->getMolecule();
|
---|
132 | if ((mol != atoms[1]->getMolecule()) || (mol != atoms[2]->getMolecule())) {
|
---|
133 | STATUS("The two selected atoms must belong to the same molecule.");
|
---|
134 | return Action::failure;
|
---|
135 | }
|
---|
136 |
|
---|
137 | // gather undo info
|
---|
138 | const Vector firstPosition = atoms[ indices[1] ]->getPosition();
|
---|
139 | const Vector secondPosition = atoms[ indices[2] ]->getPosition();
|
---|
140 |
|
---|
141 | // change the bond angle
|
---|
142 | const double newangle = .5* (M_PI / 180.) * params.bondangle.get();
|
---|
143 | LOG(1, "INFO: New angle is " << (180. / M_PI) * (2. * newangle) << ".");
|
---|
144 | ChangeBondAngleSymmetrically(indices, newangle, atoms);
|
---|
145 |
|
---|
146 | // create undo information
|
---|
147 | MoleculeChangeBondAngleState *UndoState = new MoleculeChangeBondAngleState(
|
---|
148 | indices[1], indices[2],
|
---|
149 | firstPosition, secondPosition,
|
---|
150 | atoms[ indices[1] ]->getPosition(), atoms[ indices[2] ]->getPosition(),
|
---|
151 | params);
|
---|
152 | return ActionState::ptr(UndoState);
|
---|
153 | }
|
---|
154 |
|
---|
155 | ActionState::ptr MoleculeChangeBondAngleAction::performUndo(ActionState::ptr _state) {
|
---|
156 | MoleculeChangeBondAngleState *state = assert_cast<MoleculeChangeBondAngleState*>(_state.get());
|
---|
157 |
|
---|
158 | // undo both position changes
|
---|
159 | atom * const first = World::getInstance().getAtom(AtomById(state->firstId));
|
---|
160 | atom * const second = World::getInstance().getAtom(AtomById(state->secondId));
|
---|
161 | first->setPosition(state->firstOldPosition);
|
---|
162 | second->setPosition(state->secondOldPosition);
|
---|
163 |
|
---|
164 | return ActionState::ptr(_state);
|
---|
165 | }
|
---|
166 |
|
---|
167 | ActionState::ptr MoleculeChangeBondAngleAction::performRedo(ActionState::ptr _state){
|
---|
168 | MoleculeChangeBondAngleState *state = assert_cast<MoleculeChangeBondAngleState*>(_state.get());
|
---|
169 |
|
---|
170 | // redo both position changes
|
---|
171 | atom * const first = World::getInstance().getAtom(AtomById(state->firstId));
|
---|
172 | atom * const second = World::getInstance().getAtom(AtomById(state->secondId));
|
---|
173 | first->setPosition(state->firstNewPosition);
|
---|
174 | second->setPosition(state->secondNewPosition);
|
---|
175 |
|
---|
176 | return ActionState::ptr(_state);
|
---|
177 | }
|
---|
178 |
|
---|
179 | bool MoleculeChangeBondAngleAction::canUndo() {
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 |
|
---|
183 | bool MoleculeChangeBondAngleAction::shouldUndo() {
|
---|
184 | return true;
|
---|
185 | }
|
---|
186 | /** =========== end of function ====================== */
|
---|