This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mandelbrot_enhanced.py
420 lines (336 loc) · 12.2 KB
/
mandelbrot_enhanced.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
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
from __future__ import absolute_import, print_function
import time# after imports
import random# add comments
import sys# for:
from colorsys import hsv_to_rgb# https://github.com/rfk/pypyjs/issues/109
import js# from PyPy.js
PY2 = sys.version_info[0] == 2
if PY2:
# Python 2
range = xrange
CANVAS_ID="#mandelbrot"
PROGRESS_BAR_ID="#progress-bar"
class jQuery(object):
def __init__(self):
self.jquery = js.globals["$"]
def get_by_id(self, id):
assert id.startswith("#")
dom_object = self.jquery(id)
if dom_object == js.undefined:
print("ERROR: Object with ID %r not found!" % id)
sys.exit()
return dom_object
def __call__(self, *args, **kwargs):
return self.jquery(*args, **kwargs)
jquery = jQuery()
class ProgressBar(object):
def __init__(self):
self.progress_bar = jquery.get_by_id(PROGRESS_BAR_ID)
def set_percent(self, percent, text):
percent = int(percent)
self.progress_bar.attr("aria-valuenow", percent)
self.progress_bar.width("%i%%" % percent)
self.progress_bar.text(text)
class Canvas(object):
def __init__(self, width, height):
self.width = width
self.height = height
self.reset()
# self.pixel = self.context.createImageData(1, 1)
# self.pixel_data = self.pixel.data
def draw_rect(self, x, y, r, g, b, alpha=255, width=1, height=1):
self.context.fillStyle = 'rgba(%i,%i,%i,%i)' % (r, g, b, alpha)
self.context.fillRect(x, y, width, height)
def reset(self):
self.canvas = jquery.get_by_id(CANVAS_ID)[0]
self.canvas.width = self.width
self.canvas.height = self.height
self.context = self.canvas.getContext('2d')
def interlace_generator(limit):
def gen_pow(limit):
interlace_steps = []
step=0
while True:
value = 2**step
if value>=limit:
return interlace_steps
interlace_steps.append(value)
step+=1
interlace_steps = gen_pow(limit)
interlace_steps.reverse()
#~ print("interlace_steps:", interlace_steps)
pos = 0
step = 1
iteration = 0
size = interlace_steps[iteration]
while True:
yield (pos, size)
pos += (size * step)
if pos>limit:
step = 2
iteration += 1
try:
size = interlace_steps[iteration]
except IndexError:
return
pos = size
class Mandelbrot(object):
LEFT = -2.0
RIGHT = 2.0
TOP = 2.0
BOTTOM = -2.0
def __init__(self, canvas):
self.canvas = canvas
self.progress_bar = ProgressBar()
self.width = self.canvas.width
self.height = self.canvas.height
self.set_timeout_ids=[]
self.running = True
self.center()
self.reset()
self.calc_dimensions()
def center(self):
self.horizontal_offset = 0
self.vertical_offset = 0
self.zoom = 1.0
self.running = True
def reset(self):
self.canvas.reset()
self.progress_bar.set_percent(0, "start")
self.change_scale = 0.5
self.y = 0
self.step = self.height // 4
self.line_count = 0
self.last_update = self.start_time = time.time()
self.last_pos = 0
def calc_dimensions(self):
print("offset [h,v].......: %s, %s" % (
self.horizontal_offset, self.vertical_offset
))
print("zoom...............: x%.1f (%s)" % (1/self.zoom, self.zoom))
self.left=(self.LEFT * self.zoom) + self.horizontal_offset
self.right=(self.RIGHT * self.zoom) + self.horizontal_offset
self.top=(self.TOP * self.zoom) + self.vertical_offset
self.bottom=(self.BOTTOM * self.zoom) + self.vertical_offset
print("coordinates........:", self.left, self.right, self.top, self.bottom)
self.iterations = int(jquery.get_by_id("#iterations").val())
print("Iterations.........:", self.iterations)
_interlace_generator = interlace_generator(self.height)
try:
self.interlace_generator_next = _interlace_generator.next # Python 2
except AttributeError:
self.interlace_generator_next = _interlace_generator.__next__ # Python 3
self.done = False
# self.color_func = self.color_func_random
# self.color_func = self.color_func_monochrome_red
# self.color_func = self.color_func_hsv2rgb # very colorfull
# self.color_func = self.color_func_red_green_ramp # red <-> green color ramp
# self.color_func = self.color_func_color_steps
# self.color_func = self.color_func_psychedelic # Psychedelic colors
self.color_func = self.color_func_color_map # use COLOR_MAP
self.reset()
def move_right(self):
self.horizontal_offset += (self.change_scale * self.zoom)
def move_left(self):
self.horizontal_offset -= (self.change_scale * self.zoom)
def move_top(self):
self.vertical_offset += (self.change_scale * self.zoom)
def move_bottom(self):
self.vertical_offset -= (self.change_scale * self.zoom)
def zoom_in(self):
self.zoom -= self.zoom * self.change_scale
self.horizontal_offset -= self.horizontal_offset * self.zoom
self.vertical_offset -= self.vertical_offset * self.zoom
def zoom_out(self):
self.zoom += self.zoom * self.change_scale
self.horizontal_offset -= self.horizontal_offset * self.zoom
self.vertical_offset -= self.vertical_offset * self.zoom
def stop(self):
self.running = False
print("stop rendering")
for id in self.set_timeout_ids:
js.globals.clearTimeout(id)
if self.set_timeout_ids:
print("cleared timeouts:", self.set_timeout_ids)
self.set_timeout_ids=[]
def color_func_monochrome_red(self, count, norm, iterations):
return (int(256 / iterations * norm), 0, 0)
RANDOM_MAP={}
def color_func_random(self, count, norm, iterations):
try:
return self.RANDOM_MAP[count]
except KeyError:
self.RANDOM_MAP[count] = (r,g,b) = (random.randrange(255),random.randrange(255),random.randrange(255))
return (r,g,b)
def color_func_hsv2rgb(self, count, norm, iterations):
# very colorfull ;)
(r,g,b) = hsv_to_rgb(h=norm/iterations,s=1,v=1)
return int(r*255), int(g*255), int(b*255)
def color_func_red_green_ramp(self, count, norm, iterations):
# red <-> green color ramp
return (
(255 * count) // iterations, # red
(255 * (iterations - count)) // iterations, # green
0, # blue
)
def color_func_color_steps(self, count, norm, iterations):
if count <= 5:
return (0, (255 // 5) * count, 0) # monochrome green
elif count <= 8:
return (0, 255, 0) # green
elif count <= 10:
return (0, 0, 255) # blue
elif count <= 12:
return (255, 0, 0) # red
elif count <= 15:
return (255, 255, 0) # yellow
else:
return (0, 0, 0) # black
def color_func_psychedelic(self, count, norm, iterations):
# Psychedelic colors:
color = int((65536.0 / iterations) * count)
return (
(color >> 16) & 0xFF, # red
(color >> 8) & 0xFF, # green
color & 0xFF, # blue
)
COLOR_MAP = {
0: (66, 30, 15),
1: (25, 7, 26),
2: (9, 1, 47),
3: (4, 4, 73),
4: (0, 7, 100),
5: (12, 44, 138),
6: (24, 82, 177),
7: (57, 125, 209),
8: (134, 181, 229),
9: (211, 236, 248),
10: (241, 233, 191),
11: (248, 201, 95),
12: (255, 170, 0),
13: (204, 128, 0),
14: (153, 87, 0),
15: (106, 52, 3),
16: (90, 40, 0),
17: (40, 20, 0),
18: (20, 10, 0),
19: (10, 5, 0),
20: (5, 0, 0),
}
def color_func_color_map(self, count, norm, iterations):
try:
return self.COLOR_MAP[count]
except KeyError:
if count <= 33:
return (count * 7, count * 5, 0)
else:
return (0, 0, 0) # black
def _render_line(self, canvas, color_func, y, left, right, top, bottom, width, height, iterations, rect_height):
count=0
for x in range(width):
z = complex(0.0, 0.0)
c = complex(
left + x * (right - left) / width,
top + y * (bottom - top) / height
)
norm = abs(z) ** 2.0
for count in range(iterations):
if norm <= 4.0:
z = z * z + c
norm = abs(z * z)
else:
break
(r,g,b)=color_func(count, norm, iterations)
canvas.draw_rect(x, y, r, g, b, height=rect_height)
def render_mandelbrot(self):
if not self.running or self.done:
return self.stop()
next_return = time.time() + 0.5
while time.time() < next_return:
try:
y, size = self.interlace_generator_next()
except StopIteration:
self.done = True
duration = time.time() - self.start_time
self.display_stats() # Should display 100% ;)
msg = "%ix%ipx Rendered in %iSec." % (self.width, self.height, duration)
print(msg)
print(" --- END --- ")
return self.stop()
self._render_line(
self.canvas,
self.color_func,
y,
self.left, self.right, self.top, self.bottom,
self.width, self.height, self.iterations,
rect_height=size
)
self.line_count += 1
self.display_stats()
def display_stats(self):
pos = (self.line_count * self.width)
pos_diff = pos - self.last_pos
self.last_pos = pos
duration = time.time() - self.last_update
self.last_update = time.time()
rate = pos_diff / duration
percent = 100.0 * self.line_count / self.height
self.progress_bar.set_percent(percent, "%.1f%% (%i Pixel/sec.)" % (percent, rate))
if __name__ == "__main__":
print("startup rendering...")
width = height = 400
canvas = Canvas(width, height)
mandelbrot = Mandelbrot(canvas)
@js.Function
def pause_mandelbrot(event):
if mandelbrot.running:
print("pause")
mandelbrot.stop()
jquery.get_by_id("#pause").text("resume")
else:
print("resume")
jquery.get_by_id("#pause").text("pause")
mandelbrot.running = True
render_mandelbrot()
pause_button = jquery.get_by_id("#pause")
pause_button.click(pause_mandelbrot)
@js.Function
def update_mandelbrot(event):
print("update...")
mandelbrot.calc_dimensions()
mandelbrot.running = True
render_mandelbrot()
update_button = jquery.get_by_id("#update")
update_button.click(update_mandelbrot)
@js.Function
def data_form_change(event):
print("form, changed:")
update_mandelbrot(event)
data_form = jquery.get_by_id("#data_form")
data_form.change(data_form_change)
@js.Function
def change_mandelbrot(event):
mandelbrot.stop()
obj_id = event.target.id
obj_id = str(obj_id) # convert js.String to real string
# print("clicked on ID %r" % obj_id)
try:
func = getattr(mandelbrot, obj_id)
except Exception as err:
print("Error: %s" % err)
else:
print("call 'mandelbrot.%s()'" % func.__name__)
func()
update_mandelbrot(event)
jquery(".change_mandelbrot").click(change_mandelbrot)
@js.Function
def render_mandelbrot():
mandelbrot.render_mandelbrot()
if not mandelbrot.done: # not complete, yet
# see: https://github.com/rfk/pypyjs/issues/117
mandelbrot.set_timeout_ids.append(
int(js.globals.setTimeout(render_mandelbrot, 0))
)
else:
print("done.")
render_mandelbrot()