forked from alex-berman/tforms
/
revisualize_ancestry_prerender.py
executable file
·202 lines (170 loc) · 6.79 KB
/
revisualize_ancestry_prerender.py
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
#!/usr/bin/python
from tr_log_reader import TrLogReader
from argparse import ArgumentParser
from session import Session
from interpret import Interpreter
import math
import copy
import sys
sys.path.append("visual-experiments")
import rectangular_visualizer as visualizer
from OpenGL.GL import *
import sys
from bezier import make_bezier
from ancestry_plotter import *
from vector import Vector2d
from smoother import Smoother
CURVE_PRECISION = 50
MARGIN = 20
LINE_WIDTH = 2.0 / 640
FORWARD = "forward"
BACKWARD = "backward"
PINGPONG = "pingpong"
class Ancestry(visualizer.Visualizer, AncestryPlotter):
def __init__(self, tr_log, pieces, args):
visualizer.Visualizer.__init__(self, args)
AncestryPlotter.__init__(self, tr_log.total_file_size(), tr_log.lastchunktime(), args)
self._unfold_function = getattr(self, "_unfold_%s" % args.unfold)
self._pre_render()
self._autozoom = (args.geometry == CIRCLE and self.args.autozoom)
if self._autozoom:
self._max_pxy = 0
self._zoom_smoother = Smoother()
@staticmethod
def add_parser_arguments(parser):
AncestryPlotter.add_parser_arguments(parser)
visualizer.Visualizer.add_parser_arguments(parser)
parser.add_argument("-z", dest="timefactor", type=float, default=1.0)
def _pre_render(self):
print "pre-rendering..."
self._timeline = []
for piece in pieces:
self.add_piece(piece["id"], piece["t"], piece["begin"], piece["end"])
frame = {"t": piece["t"],
"tracker": copy.deepcopy(self._tracker),
"num_pieces": self._num_pieces}
self._timeline.append(frame)
print "ok"
def InitGL(self):
visualizer.Visualizer.InitGL(self)
glClearColor(0.0, 0.0, 0.0, 0.0)
def ReSizeGLScene(self, width, height):
visualizer.Visualizer.ReSizeGLScene(self, width, height)
self._size = min(width, height) - 2*MARGIN
AncestryPlotter.set_size(self, self._size, self._size)
def render(self):
glTranslatef(MARGIN + (self.width - self._size)/2, MARGIN, 0)
glLineWidth(LINE_WIDTH * self.width)
glEnable(GL_LINE_SMOOTH)
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glColor3f(1,1,1)
self._unfold_function(self.current_time() * self.args.timefactor)
frame = self._frame_at_cursor()
self._activate_frame(frame)
if self._autozoom:
self._zoom = self._zoom_smoother.value()
if self._zoom is None:
self._zoom = 0.0
else:
self._zoom = 1.0
self.plot()
if self._autozoom:
if self._max_pxy == 0:
zoom = 0.5
else:
zoom = 0.5 + self.cursor_t/self._duration * 0.5 / self._max_pxy
self._zoom_smoother.smooth(zoom, self.time_increment)
def _unfold_backward(self, t):
self._cursor_t = self._duration - t % self._duration
def _unfold_forward(self, t):
self._cursor_t = t % self._duration
def _frame_at_cursor(self):
for frame in self._timeline:
if frame["t"] >= self._cursor_t:
return frame
return self._timeline[-1]
def _activate_frame(self, frame):
self._num_pieces = frame["num_pieces"]
self._tracker = frame["tracker"]
# def _follow_piece(self, piece):
# if len(piece.growth) > 0:
# path = [(piece.t,
# (piece.begin + piece.end) / 2)]
# for older_version in reversed(piece.growth):
# path.append((older_version.t,
# (older_version.begin + older_version.end) / 2))
# self.draw_path(path)
# for parent in piece.parents.values():
# if self.min_t < parent.t < self.max_t:
# self._connect_child_and_parent(
# piece.t, (piece.begin + piece.end) / 2,
# parent.t, (parent.begin + parent.end) / 2)
# self._follow_piece(parent)
# else:
# if self.args.unfold == BACKWARD:
# t = self.cursor_t - pow(self.cursor_t - parent.t, 0.7)
# else:
# t = self.cursor_t
# self._connect_child_and_parent(
# piece.t, (piece.begin + piece.end) / 2,
# t, (parent.begin + parent.end) / 2)
def _rect_position(self, t, byte_pos):
x = float(byte_pos) / self._total_size * self._width
y = (1 - t / self._duration) * self._height
return x, y
def _circle_position(self, t, byte_pos):
angle = float(byte_pos) / self._total_size * 2*math.pi
rel_t = 1 - t / self._duration
px = rel_t * math.cos(angle)
py = rel_t * math.sin(angle)
x = self._width / 2 + (px * self._zoom) * self._width / 2
y = self._height / 2 + (py * self._zoom) * self._height / 2
if self._autozoom:
self._max_pxy = max([self._max_pxy, abs(px), abs(py)])
return x, y
def draw_path(self, points):
glBegin(GL_LINE_STRIP)
for (t, b) in points:
x, y = self._position(t, b)
glVertex2f(x, y)
glEnd()
def draw_curve(self, x1, y1, x2, y2):
control_points = [
Vector2d(x1, y1),
Vector2d(x1 + (x2 - x1) * 0.3, y1),
Vector2d(x1 + (x2 - x1) * 0.7, y2),
Vector2d(x2, y2)
]
bezier = make_bezier([(p.x, p.y) for p in control_points])
points = bezier(CURVE_PRECISION)
glBegin(GL_LINE_STRIP)
for x, y in points:
glVertex2f(x, y)
glEnd()
parser = ArgumentParser()
parser.add_argument("sessiondir")
parser.add_argument("--file", dest="selected_files", type=int, nargs="+")
parser.add_argument("-t", "--torrent", dest="torrentname", default="")
parser.add_argument("-interpret", action="store_true")
parser.add_argument("-autozoom", action="store_true")
parser.add_argument("--unfold", choices=[FORWARD, BACKWARD, PINGPONG], default=BACKWARD)
Ancestry.add_parser_arguments(parser)
options = parser.parse_args()
options.standalone = True
sessiondir = options.sessiondir
logfilename = "%s/session.log" % sessiondir
print "session: %s" % sessiondir
tr_log = TrLogReader(logfilename, options.torrentname,
realtime=False,
pretend_sequential=False).get_log()
if options.selected_files:
tr_log.select_files(options.selected_files)
print >> sys.stderr, "found %d chunks" % len(tr_log.chunks)
tr_log.ignore_non_downloaded_files()
if options.interpret:
pieces = Interpreter().interpret(tr_log.chunks)
else:
pieces = tr_log.chunks
Ancestry(tr_log, pieces, options).run()