-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperationRecorder.py
More file actions
199 lines (159 loc) · 5.59 KB
/
OperationRecorder.py
File metadata and controls
199 lines (159 loc) · 5.59 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
# import threading
import random
# import time
from typing import Any
from dataclasses import dataclass
# from timer import Timer
def Next(ptr, size, step = 1):
return (ptr + step) % size
def Prev(ptr, size, step = 1):
return (ptr - step + size) % size
class OperationRecorder:
def __init__(self, size = 10000):
self.size = size
self.buffer = [None] * size
self.head = 0
self.tail = 0
self.count = 0
self.undocount = 0
def record(self, data: Any):
if self.undocount > 0:
self.count -= self.undocount
self.undocount = 0
self.buffer[self.tail] = data
if self.count < self.size:
self.count += 1
else:
self.head = Next(self.head, self.size)
self.tail = Next(self.tail, self.size)
def undo(self) -> Any:
if self.undocount < self.count:
self.tail = Prev(self.tail, self.size)
self.undocount += 1
return self.buffer[self.tail]
def redo(self) -> Any:
if self.undocount > 0:
self.undocount -= 1
res = self.buffer[self.tail]
self.tail = Next(self.tail, self.size)
return res
def clear(self):
self.head = 0
self.tail = 0
self.count = 0
self.undocount = 0
@dataclass
class Frame:
pose: tuple[float, float, float] # X, Y, Angle
velocity: tuple[float, float, float] # Vx, Vy, 0
def __str__(self):
x, y, a = self.pose
vx, vy, _ = self.velocity
return f"pose=({x:.2f},{y:.2f},{a:.2f}) vel=({vx:.2f},{vy:.2f})"
__repr__ = __str__
def generate_random_frame() -> Frame:
frame = Frame(pose = (random.random(), random.random(), random.random()), velocity = (random.random(), random.random(), 0))
print(f"Generated frame: {frame}")
return frame
class Client():
def __init__(self, capacity = 10000, size = 16):
self.capacity = capacity
self.size = size
self.segments: list[list[Frame]] = [None] * capacity
self.head = 0
self.tail = 0
self.count = 0
self.seg: list[Frame] = [None] * self.size
self.seg_idx = 0
self.seg_snap = 0
self.timeline_end = 0
self.timeline_cursor = 0
self.replay_mode = False
self.direction = 0
self.paused = False
def execute(self, frame: Frame):
print(f"Execute frame: {frame}")
self.signal_resume()
def record(self, data: Frame):
self.seg[self.seg_idx] = data
self.seg_idx += 1
if self.seg_idx == self.size:
self.segments[self.tail] = self.seg.copy()
self.tail = Next(self.tail, self.capacity)
if self.count < self.capacity:
self.count += 1
else:
self.head = Next(self.head, self.capacity)
self.seg_idx = 0
def replay(self):
if not self.replay_mode:
self.seg_snap = self.seg_idx
self.timeline_end = self.timeline_cursor = self.count * self.size
self.replay_mode = True
self.direction = -1
else:
seg_pos, frame_pos = divmod(self.timeline_cursor, self.size)
if seg_pos < self.count: # seg_idx = 0
self.count = seg_pos
self.tail = Next(self.head, self.capacity, self.count)
if frame_pos > 0:
self.seg = self.segments[Next(self.head, self.capacity, seg_pos)].copy()
self.seg_idx = frame_pos
self.replay_mode = False
self.direction = 0
def rewind(self):
match self.direction:
case 0: # do nothing
pass
case -1: # undo
if self.seg_idx > 0:
self.seg_idx -= 1
self.execute(self.seg[self.seg_idx])
return
if self.timeline_cursor > 0:
self.timeline_cursor -= 1
seg_pos, frame_pos = divmod(self.timeline_cursor, self.size)
self.execute(self.segments[Next(self.head, self.capacity, seg_pos)][frame_pos])
return
case 1: # redo
if self.timeline_cursor < self.timeline_end:
seg_pos, frame_pos = divmod(self.timeline_cursor, self.size)
self.timeline_cursor += 1
self.execute(self.segments[Next(self.head, self.capacity, seg_pos)][frame_pos])
return
if self.seg_idx < self.seg_snap:
self.seg_idx += 1
self.execute(self.seg[self.seg_idx - 1])
return
self.signal_stop()
def sub(self):
match self.direction:
case 0:
self.direction = -1
self.signal_resume()
case 1:
self.direction = 0
self.signal_stop()
def add(self):
match self.direction:
case -1:
self.direction = 0
self.signal_stop()
case 0:
self.direction = 1
self.signal_resume()
def clear(self):
self.head = 0
self.tail = 0
self.count = 0
self.seg_idx = 0
self.replay_mode = False
self.direction = 0
def signal_stop(self):
if not self.paused:
print("Signal stop")
self.paused = True
def signal_resume(self):
if self.paused:
print("Signal resume")
self.paused = False