-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndoBuffer.cpp
More file actions
305 lines (253 loc) · 9.77 KB
/
UndoBuffer.cpp
File metadata and controls
305 lines (253 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/***********************************************************************
UndoBuffer - Class to encapsulate undo and redo functionality for
dihedral angle updates on proteins.
Copyright (c) 2002-2020 Oliver Kreylos
This file is part of VR ProtoShop.
VR ProtoShop is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
VR ProtoShop is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with VR ProtoShop; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "UndoBuffer.h"
#include <Geometry/AffineTransformation.h>
/*******************************************
Methods of class UndoBuffer::UndoQueueEntry:
*******************************************/
void UndoBuffer::UndoQueueEntry::set(MD::Protein* sProtein,const Geometry::OrthonormalTransformation<double,3>& sProteinTransformation,const std::list<DihedralAngleSequence> previousSequences[2])
{
/* Copy parameters: */
protein=sProtein;
proteinTransformation=sProteinTransformation;
sequences[0].clear();
sequences[1].clear();
/* Process both lists of sequences: */
for(int i=0;i<2;++i)
{
/* Calculate and store dihedral angle changes for all left/right sequences: */
for(std::list<DihedralAngleSequence>::const_iterator pIt=previousSequences[i].begin();pIt!=previousSequences[i].end();++pIt)
{
/* Add sequence to sequence list: */
DihedralAngleSequence& deltas=*sequences[i].insert(sequences[i].end(),DihedralAngleSequence());
/* Calculate (negative) angle changes: */
deltas.resize(pIt->getFirstResidue(),pIt->getNumResidues());
protein->getDihedralAngles(deltas.getFirstResidue(),deltas.getNumResidues(),deltas.angles);
for(int j=0;j<deltas.getNumResidues();++j)
deltas.angles[j].delta(pIt->angles[j]);
}
}
}
void UndoBuffer::UndoQueueEntry::undo(void)
{
/* Apply inverse protein transformation: */
protein->transform(Geometry::AffineTransformation<MD::Scalar,3>(Geometry::invert(proteinTransformation)));
/* Process both lists of sequences: */
for(int i=0;i<2;++i)
{
/* Determine update direction: */
int direction=i==0?-1:1;
/* Process all left/right sequences: */
for(std::list<DihedralAngleSequence>::const_iterator pIt=sequences[i].begin();pIt!=sequences[i].end();++pIt)
{
/* Apply negative angle changes: */
protein->changeDihedralAngles(pIt->getFirstResidue(),pIt->getNumResidues(),pIt->angles,direction);
}
}
}
void UndoBuffer::UndoQueueEntry::redo(void)
{
/* Process both lists of sequences: */
for(int i=0;i<2;++i)
{
/* Determine update direction: */
int direction=i==0?-1:1;
/* Process all left/right sequences: */
for(std::list<DihedralAngleSequence>::const_iterator pIt=sequences[i].begin();pIt!=sequences[i].end();++pIt)
{
/* Negate angle deltas: */
for(int j=0;j<pIt->getNumResidues();++j)
pIt->angles[j].negate();
/* Apply positive angle changes: */
protein->changeDihedralAngles(pIt->getFirstResidue(),pIt->getNumResidues(),pIt->angles,direction);
/* Negate angle deltas again: */
for(int j=0;j<pIt->getNumResidues();++j)
pIt->angles[j].negate();
}
}
/* Apply protein transformation: */
protein->transform(Geometry::AffineTransformation<MD::Scalar,3>(proteinTransformation));
}
/***************************
Methods of class UndoBuffer:
***************************/
UndoBuffer::UndoBuffer(void)
:queueTail(queue.end()),
currentProtein(0),lastSavedDepth(0)
{
}
UndoBuffer::~UndoBuffer(void)
{
}
void UndoBuffer::startInteraction(MD::Protein* protein)
{
/* Retrieve parameters from given structure: */
currentProtein=protein;
}
void UndoBuffer::addResidueSequence(int startResidue,int numResidues,int direction)
{
/* Create a single residue sequence: */
int i=direction==-1?0:1;
DihedralAngleSequence& prev=*previousSequences[i].insert(previousSequences[i].end(),DihedralAngleSequence());
prev.resize(startResidue,numResidues);
/* Retrieve current dihedral angles: */
currentProtein->getDihedralAngles(prev.getFirstResidue(),prev.getNumResidues(),prev.angles);
}
void UndoBuffer::startInteraction(MD::Protein* protein,MD::Protein::Residue* selectedResidue,int direction)
{
/* Retrieve parameters from given structure: */
currentProtein=protein;
/* Create a single residue sequence: */
int i=direction==-1?0:1;
DihedralAngleSequence& prev=*previousSequences[i].insert(previousSequences[i].end(),DihedralAngleSequence());
prev.resize(protein->getResidueIndex(selectedResidue),1);
/* Retrieve current dihedral angles: */
currentProtein->getDihedralAngles(prev.getFirstResidue(),prev.getNumResidues(),prev.angles);
}
void UndoBuffer::startInteraction(MD::Protein* protein,int startResidue,int numResidues,int direction)
{
/* Retrieve parameters from given structure: */
currentProtein=protein;
/* Create a single residue sequence: */
int i=direction==-1?0:1;
DihedralAngleSequence& prev=*previousSequences[i].insert(previousSequences[i].end(),DihedralAngleSequence());
prev.resize(startResidue,numResidues);
/* Retrieve current dihedral angles: */
currentProtein->getDihedralAngles(prev.getFirstResidue(),prev.getNumResidues(),prev.angles);
}
void UndoBuffer::startInteraction(const MD::Protein::StructureSelector& selectedStructure,int direction)
{
/* Retrieve parameters from given structure: */
currentProtein=selectedStructure.getProtein();
/* Create a single residue sequence: */
int i=direction==-1?0:1;
DihedralAngleSequence& prev=*previousSequences[i].insert(previousSequences[i].end(),DihedralAngleSequence());
prev.resize(selectedStructure.getFirstResidueIndex(),selectedStructure.getNumResidues());
/* Retrieve current dihedral angles: */
currentProtein->getDihedralAngles(prev.getFirstResidue(),prev.getNumResidues(),prev.angles);
}
void UndoBuffer::startInteraction(const std::list<MD::Protein::StructureSelector>& leftStructures,const std::list<MD::Protein::StructureSelector>& rightStructures)
{
/* Retrieve parameters from given structure: */
currentProtein=0;
/* Create left/right residue sequences: */
for(int i=0;i<2;++i)
{
const std::list<MD::Protein::StructureSelector>& str=i==0?leftStructures:rightStructures;
for(std::list<MD::Protein::StructureSelector>::const_iterator strIt=str.begin();strIt!=str.end();++strIt)
{
/* Get protein from structure (silently assume all structures belong to the same protein): */
currentProtein=strIt->getProtein();
/* Create a single residue sequence: */
DihedralAngleSequence& prev=*previousSequences[i].insert(previousSequences[i].end(),DihedralAngleSequence());
prev.resize(strIt->getFirstResidueIndex(),strIt->getNumResidues());
/* Retrieve current dihedral angles: */
currentProtein->getDihedralAngles(prev.getFirstResidue(),prev.getNumResidues(),prev.angles);
}
}
}
void UndoBuffer::finishInteraction(void)
{
if(currentProtein!=0)
{
/* Insert a new undo queue entry before the current tail: */
std::list<UndoQueueEntry>::iterator newIt=queue.insert(queueTail,UndoQueueEntry());
/* Remove any queue entries behind the current tail (therefore flushing the redo buffer): */
queue.erase(queueTail,queue.end());
queueTail=queue.end();
if(lastSavedDepth<0) // The last saved state has been flushed from the undo buffer
{
/* Invalidate the marker: */
lastSavedDepth=queue.size(); // One-behind-last will never reach zero
}
else
++lastSavedDepth;
/* Set the undo queue entry's data: */
newIt->set(currentProtein,Geometry::OrthonormalTransformation<double,3>::identity,previousSequences);
}
/* Clean up: */
currentProtein=0;
previousSequences[0].clear();
previousSequences[1].clear();
}
void UndoBuffer::finishInteraction(const Geometry::OrthonormalTransformation<double,3>& proteinTransformation)
{
if(currentProtein!=0)
{
/* Insert a new undo queue entry before the current tail: */
std::list<UndoQueueEntry>::iterator newIt=queue.insert(queueTail,UndoQueueEntry());
/* Remove any queue entries behind the current tail (therefore flushing the redo buffer): */
queue.erase(queueTail,queue.end());
queueTail=queue.end();
if(lastSavedDepth<0) // The last saved state has been flushed from the undo buffer
{
/* Invalidate the marker: */
lastSavedDepth=queue.size(); // One-behind-last will never reach zero
}
else
++lastSavedDepth;
/* Set the undo queue entry's data: */
newIt->set(currentProtein,proteinTransformation,previousSequences);
}
/* Clean up: */
currentProtein=0;
previousSequences[0].clear();
previousSequences[1].clear();
}
MD::Protein* UndoBuffer::undo(void)
{
/* Rewind the queue tail by one and un-apply the skipped entry's dihedral angle change: */
MD::Protein* result=0;
if(queueTail!=queue.begin())
{
--queueTail;
result=queueTail->getProtein();
queueTail->undo();
--lastSavedDepth;
}
return result;
}
MD::Protein* UndoBuffer::redo(void)
{
/* Advance the queue tail by one and re-apply the skipped entry's dihedral angle change: */
MD::Protein* result=0;
if(queueTail!=queue.end())
{
result=queueTail->getProtein();
queueTail->redo();
++queueTail;
++lastSavedDepth;
}
return result;
}
void UndoBuffer::deleteProtein(const MD::Protein* protein)
{
std::list<UndoQueueEntry>::iterator qIt=queue.begin();
while(qIt!=queue.end())
{
if(qIt->getProtein()==protein)
{
/* Delete this entry: */
std::list<UndoQueueEntry>::iterator del=qIt;
++qIt;
queue.erase(del);
}
else
++qIt;
}
}