/
UndoRedoSystem.cpp
469 lines (401 loc) · 14.9 KB
/
UndoRedoSystem.cpp
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
///////////////////////////////////////////////////////////////////////////////////////////////////
// Project: Espina Volume Editor
// Author: Félix de las Pozas Alvarez
//
// File: UndoRedoSystem.cpp
// Purpose: Manages the undo/redo operation buffers
// Notes: Not the most elegant implementation, but it's easy and can be expanded.
///////////////////////////////////////////////////////////////////////////////////////////////////
// qt includes
#include <QMessageBox>
#include <QObject>
#include <QAbstractButton>
#include <QPushButton>
// project includes
#include "UndoRedoSystem.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
UndoRedoSystem::UndoRedoSystem(DataManager *dataManager)
: m_current{nullptr}
, m_size{150 * 1024 * 1024}
, m_used{0}
, m_dataManager{dataManager}
, m_bufferFull{false}
, m_sizePoint{sizeof(std::pair<Vector3ui, unsigned short>)}
, m_sizeAction{sizeof(struct action)}
, m_sizeObject{sizeof(std::pair<unsigned short, struct DataManager::ObjectInformation*>)}
, m_sizeColor{4 * sizeof(unsigned char)}
, m_sizeLabel{sizeof(unsigned short)}
{
}
///////////////////////////////////////////////////////////////////////////////////////////////////
UndoRedoSystem::~UndoRedoSystem()
{
// NOTE: for this and the rest of UndoRedoSystem operations: the pointers to objects allocated
// in the undo buffer are never freed because are in use by DataManager and it will free them
// on destruction. On the other hand clearing the redo buffer deletes dynamically allocated
// objects created by DataManager, stored in UndoRedoSystem and NOT in use by DataManager.
clear(Type::ALL);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void UndoRedoSystem::clear(const Type type)
{
// in destruction of the object we must check if objects are NULL because some could have
// been previously deallocated while DataManager object destruction. we don't check for
// NULL on the rest of the class because we know that we're deleting existing objects.
unsigned long int capacity = 0;
switch (type)
{
case Type::UNDO:
for (auto it: m_undo)
{
capacity += it.points.size() * m_sizePoint;
capacity += it.objects.size() * m_sizeObject;
capacity += it.description.capacity();
capacity += it.lut->GetNumberOfTableValues() * m_sizeColor;
capacity += it.labels.size() * m_sizeLabel;
capacity += m_sizeAction;
}
m_undo.clear();
break;
case Type::REDO:
for (auto it: m_redo)
{
capacity += it.points.size() * m_sizePoint;
capacity += it.objects.size() * m_sizeObject;
capacity += it.description.capacity();
capacity += it.lut->GetNumberOfTableValues() * m_sizeColor;
capacity += it.labels.size() * m_sizeLabel;
capacity += m_sizeAction;
// delete dinamically allocated objects no longer needed by DataManager
while (!it.objects.empty())
{
it.objects.back().second = nullptr;
it.objects.pop_back();
}
}
m_redo.clear();
break;
case Type::ALL:
clear(Type::REDO);
clear(Type::UNDO);
break;
default:
break;
}
m_used -= capacity;
Q_ASSERT(m_used >= 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void UndoRedoSystem::signalBeginAction(const std::string &actionstring, const std::set<unsigned short> labelSet, vtkSmartPointer<vtkLookupTable> lut)
{
unsigned long int capacity = 0;
//because an action started, the redo buffer must be empty
clear(Type::REDO);
// we need to copy the values of the table
double rgba[4];
vtkSmartPointer<vtkLookupTable> lutCopy = vtkSmartPointer<vtkLookupTable>::New();
lutCopy->Allocate();
lutCopy->SetNumberOfTableValues(lut->GetNumberOfTableValues());
for (int index = 0; index != lut->GetNumberOfTableValues(); index++)
{
lut->GetTableValue(index, rgba);
lutCopy->SetTableValue(index, rgba);
}
lutCopy->SetTableRange(0, lut->GetNumberOfTableValues() - 1);
// create new action
m_current = new struct action;
m_current->description = actionstring;
m_current->labels = labelSet;
m_current->lut = lutCopy;
capacity += m_sizeAction;
capacity += (*m_current).description.capacity();
capacity += (*m_current).labels.size() * m_sizeLabel;
capacity += (*m_current).lut->GetNumberOfTableValues() * m_sizeColor;
// we need to know if we are at the limit of our buffer
while ((m_used + capacity) > m_size)
{
assert(!m_undo.empty());
// start deleting undo actions from the beginning of the list
m_used -= (*m_undo.begin()).points.size() * m_sizePoint;
m_used -= (*m_undo.begin()).objects.size() * m_sizeObject;
m_used -= (*m_undo.begin()).description.capacity();
m_used -= ((*m_undo.begin()).lut)->GetNumberOfTableValues() * m_sizeColor;
m_used -= (*m_undo.begin()).labels.size() * m_sizeLabel;
m_used -= m_sizeAction;
m_undo.erase(m_undo.begin());
}
m_used += capacity;
m_bufferFull = false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void UndoRedoSystem::signalEndAction(void)
{
if (!m_bufferFull)
{
m_undo.push_back(*m_current);
m_current = nullptr;
}
else
{
m_bufferFull = false;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void UndoRedoSystem::storePoint(const Vector3ui &point, const unsigned short label)
{
// if buffer has been marked as full (one action is too big and doesn't fit in the
// buffer) don't do anything else.
if (m_bufferFull) return;
(*m_current).points.push_back(std::pair<Vector3ui, unsigned short>(point, label));
m_used += m_sizePoint;
// we need to know if we are at the limit of our buffer
checkLimits();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void UndoRedoSystem::storeObject(std::pair<unsigned short, std::shared_ptr<DataManager::ObjectInformation>> value)
{
// if buffer marked as full, just return. complete action doesn't fit into memory
if (m_bufferFull) return;
(*m_current).objects.push_back(value);
m_used += m_sizeObject;
// we need to know if we are at the limit of our buffer
checkLimits();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void UndoRedoSystem::checkLimits()
{
while (m_used > m_size)
{
// start deleting undo actions from the beginning of the list, check first if this action
// fits in our buffer, and if not, delete all points entered until now and mark buffer as
// completely full (the action doesn't fit)
if (m_undo.empty())
{
m_bufferFull = true;
m_used -= (*m_current).points.size() * m_sizePoint;
m_used -= (*m_current).objects.size() * m_sizeObject;
m_used -= (*m_current).description.capacity();
m_used -= ((*m_current).lut)->GetNumberOfTableValues() * m_sizeColor;
m_used -= (*m_current).labels.size() * m_sizeLabel;
m_used -= m_sizeAction;
delete m_current;
m_current = nullptr;
}
else
{
m_used -= (*m_undo.begin()).points.size() * m_sizePoint;
m_used -= (*m_undo.begin()).objects.size() * m_sizeObject;
m_used -= (*m_undo.begin()).description.capacity();
m_used -= ((*m_undo.begin()).lut)->GetNumberOfTableValues() * m_sizeColor;
m_used -= (*m_undo.begin()).labels.size() * m_sizeLabel;
m_used -= m_sizeAction;
m_undo.erase(m_undo.begin());
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
const bool UndoRedoSystem::isEmpty(const Type type) const
{
switch (type)
{
case Type::UNDO:
return (0 == m_undo.size());
break;
case Type::REDO:
return (0 == m_redo.size());
break;
default:
break;
}
// not dead code
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void UndoRedoSystem::changeSize(const unsigned long int size)
{
m_size = size;
if (size >= m_used) return;
// we need to delete actions to fit in that size, first redo
while ((m_used > size) && (!isEmpty(Type::REDO)))
{
m_used -= (*m_redo.begin()).points.size() * m_sizePoint;
m_used -= (*m_redo.begin()).objects.size() * m_sizeObject;
m_used -= (*m_redo.begin()).description.capacity();
m_used -= ((*m_redo.begin()).lut)->GetNumberOfTableValues() * m_sizeColor;
m_used -= (*m_redo.begin()).labels.size() * m_sizeLabel;
m_used -= m_sizeAction;
// delete dinamically allocated objects in redo
while (!(*m_redo.begin()).objects.empty())
{
(*m_redo.begin()).objects.back().second = nullptr;
(*m_redo.begin()).objects.pop_back();
}
m_redo.erase(m_redo.begin());
}
// then undo
while ((m_used > size) && (!isEmpty(Type::UNDO)))
{
m_used -= (*m_undo.begin()).points.size() * m_sizePoint;
m_used -= (*m_undo.begin()).objects.size() * m_sizeObject;
m_used -= (*m_undo.begin()).description.capacity();
m_used -= ((*m_undo.begin()).lut)->GetNumberOfTableValues() * m_sizeColor;
m_used -= (*m_undo.begin()).labels.size() * m_sizeLabel;
m_used -= m_sizeAction;
m_undo.erase(m_undo.begin());
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
const unsigned long int UndoRedoSystem::size() const
{
return m_size;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
const unsigned long int UndoRedoSystem::capacity() const
{
return m_used;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void UndoRedoSystem::doAction(const Type type)
{
std::vector<std::pair<Vector3ui, unsigned short> > action_vector;
assert(!m_current);
m_current = new struct action;
// get the data from the right buffer
switch (type)
{
case Type::UNDO:
action_vector = m_undo.back().points;
(*m_current).description = m_undo.back().description;
(*m_current).lut = m_undo.back().lut;
(*m_current).objects = m_undo.back().objects;
(*m_current).labels = m_undo.back().labels;
break;
case Type::REDO:
action_vector = m_redo.back().points;
(*m_current).description = m_redo.back().description;
(*m_current).lut = m_redo.back().lut;
(*m_current).objects = m_redo.back().objects;
(*m_current).labels = m_redo.back().labels;
break;
default:
break;
}
// vector size changes while updating data, need to get the size before
auto numberOfPoints = action_vector.size();
// we "delete" the size of the points not to mess with memory while using
// SetVoxelScalar, at the end the memory size it's the same, in fact memory
// used for points is constant during "do undo/redo action" as we delete
// one point while adding a new one to the opposite action, so the memory
// variation is about the size of a point plus the size of a label.
m_used -= (numberOfPoints * m_sizePoint);
// reverse labels from all points in the action
for (unsigned long int i = 0; i < numberOfPoints; i++)
{
auto action_point = action_vector.back();
action_vector.pop_back();
Vector3ui point = action_point.first;
m_dataManager->SetVoxelScalar(point, action_point.second);
}
m_dataManager->SignalDataAsModified();
// insert the changed action in the right buffer and apply the actual LookupTable and table values
// in fact only the pointers in the original ObjectVector (a std::map) are changed the objects are
// only deleted when the undoredosystem deletes them
auto temporalSet = m_dataManager->GetSelectedLabelsSet();
switch (type)
{
case Type::UNDO:
m_redo.push_back(*m_current);
m_undo.pop_back();
m_used -= m_redo.back().lut->GetNumberOfColors() * m_sizeColor;
m_dataManager->SwitchLookupTables(m_redo.back().lut);
m_used += m_redo.back().lut->GetNumberOfColors() * m_sizeColor;
m_used -= m_redo.back().labels.size() * m_sizeLabel;
m_dataManager->SetSelectedLabelsSet(m_redo.back().labels);
m_redo.back().labels = temporalSet;
m_used += m_redo.back().labels.size() * m_sizeLabel;
for (auto it: m_redo.back().objects)
{
(*m_dataManager->GetObjectTablePointer()).erase(it.first);
}
break;
case Type::REDO:
m_undo.push_back(*m_current);
m_redo.pop_back();
m_used -= m_undo.back().lut->GetNumberOfColors() * m_sizeColor;
m_dataManager->SwitchLookupTables(m_undo.back().lut);
m_used += m_undo.back().lut->GetNumberOfColors() * m_sizeColor;
m_used -= m_undo.back().labels.size() * m_sizeLabel;
m_dataManager->SetSelectedLabelsSet(m_undo.back().labels);
m_undo.back().labels = temporalSet;
m_used += m_undo.back().labels.size() * m_sizeLabel;
for (auto it: m_undo.back().objects)
{
(*m_dataManager->GetObjectTablePointer())[it.first] = it.second;
}
break;
default:
break;
}
checkLimits();
m_current = nullptr;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
const std::string UndoRedoSystem::getActionString(const Type type) const
{
switch (type)
{
case Type::UNDO:
if (!isEmpty(Type::UNDO))
{
return m_undo.back().description;
}
break;
case Type::REDO:
if (!isEmpty(Type::REDO))
{
return m_redo.back().description;
}
break;
case Type::ACTUAL:
if (m_current)
{
return (*m_current).description;
}
break;
case Type::ALL:
default:
break;
}
return std::string();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void UndoRedoSystem::signalCancelAction()
{
m_bufferFull = false;
m_used -= (*m_current).points.size() * m_sizePoint;
m_used -= (*m_current).objects.size() * m_sizeObject;
m_used -= (*m_current).description.capacity();
m_used -= ((*m_current).lut)->GetNumberOfTableValues() * m_sizeColor;
m_used -= (*m_current).labels.size() * m_sizeLabel;
m_used -= m_sizeAction;
// undo changes if there are some, bypassing the undo system as we don't want to store this
// modifications to the data
while ((*m_current).points.size() != 0)
{
auto action_point = (*m_current).points.back();
(*m_current).points.pop_back();
auto point = action_point.first;
m_dataManager->SetVoxelScalarRaw(point, action_point.second);
}
m_dataManager->SignalDataAsModified();
// delete dinamically allocated objects
while (!(*m_current).objects.empty())
{
auto pair = (*m_current).objects.back();
(*m_dataManager->GetObjectTablePointer()).erase(pair.first);
pair.second = nullptr;
(*m_current).objects.pop_back();
}
delete m_current;
m_current = nullptr;
}