From cb69732f68c0bd46c1574de16ce1aee6f38e439b Mon Sep 17 00:00:00 2001 From: Brett Weiland Date: Tue, 11 Jun 2024 14:50:14 -0500 Subject: restarting --- mask_tests/; | 30 ++++++++++ mask_tests/asdf.png | Bin 0 -> 1073534 bytes mask_tests/kernel.c | 49 ++++++++++++++++ mask_tests/mask.png | 1 + mask_tests/mask.py | 153 +++++++++++++++++++++++++++++++++++++++++++++++++ mask_tests/mask.py.bak | 142 +++++++++++++++++++++++++++++++++++++++++++++ mask_tests/notes | 79 +++++++++++++++++++++++++ 7 files changed, 454 insertions(+) create mode 100644 mask_tests/; create mode 100644 mask_tests/asdf.png create mode 100644 mask_tests/kernel.c create mode 120000 mask_tests/mask.png create mode 100755 mask_tests/mask.py create mode 100644 mask_tests/mask.py.bak create mode 100644 mask_tests/notes (limited to 'mask_tests') diff --git a/mask_tests/; b/mask_tests/; new file mode 100644 index 0000000..665c6ce --- /dev/null +++ b/mask_tests/; @@ -0,0 +1,30 @@ +unsigned int secant_fractal(double x, double y, unsigned int escape, unsigned int iterations) { + for(unsigned int iter = 0; iter < iterations; iter++) { + double next_x; + next_x = x / cos(y); + y = y / cos(x); + x = next_x; + if((pow(x, 2) + pow(y, 2)) >= escape) return iter; + } +} +unsigned int cosecant_fractal(double x, double y, unsigned int escape, unsigned int iterations) { + for(unsigned int iter = 0; iter < iterations; iter++) { + double next_x; + next_x = x / sin(y); + y = y / sin(x); + x = next_x; + if((pow(x, 2) + pow(y, 2)) >= escape) return iter; + } +} + +__kernel void render_frame(__global unsigned int *frame_output, //uint8_t *mask, //more bit depth is possible + double x_step, double y_step, + double x_start, double y_start, + unsigned int iterations, unsigned int escape, double ratio) { + unsigned int result; + double on_x = (get_global_id(0) * x_step) + x_start; + double on_y = (get_global_id(1) * y_step) + y_start; + + frame_output[(get_global_id(1) * get_global_size(1)) + get_global_id(0)] = + secant_fractal(on_x, on_y, escape, iterations); +} diff --git a/mask_tests/asdf.png b/mask_tests/asdf.png new file mode 100644 index 0000000..66d8a87 Binary files /dev/null and b/mask_tests/asdf.png differ diff --git a/mask_tests/kernel.c b/mask_tests/kernel.c new file mode 100644 index 0000000..669c382 --- /dev/null +++ b/mask_tests/kernel.c @@ -0,0 +1,49 @@ +unsigned int secant_fractal(double x, double y, unsigned int escape, unsigned int iterations) { + for(unsigned int iter = 0; iter < iterations; iter++) { + double next_x; + next_x = x / cos(y); + y = y / cos(x); + x = next_x; + if((pow(x, 2) + pow(y, 2)) >= escape) return iter; + } +} +unsigned int cosecant_fractal(double x, double y, unsigned int escape, unsigned int iterations) { + for(unsigned int iter = 0; iter < iterations; iter++) { + double next_x; + next_x = x / sin(y); + y = y / sin(x); + x = next_x; + if((pow(x, 2) + pow(y, 2)) >= escape) return iter; + } +} + +double cosecant_single(double a, double b) { return a / sin(b); } +double secant_single(double a, double b) { return a / tan(b); } + + +__kernel void render_frame(__global unsigned int *frame_output, __global double *mask, + double x_step, double y_step, + double x_start, double y_start, + unsigned int iterations, unsigned int escape, double ratio) { + unsigned int result; + double x = (get_global_id(0) * x_step) + x_start; + double y = (get_global_id(1) * y_step) + y_start; + size_t img_index = (get_global_id(1) * get_global_size(1)) + get_global_id(0); + + unsigned int iter; + + + for(iter = 0; iter < iterations; iter++) { + double next_x; + double r = mask[img_index]; + next_x = (r * cosecant_single(x, y)) + ((1 - r) * secant_single(x, y)); + y = (r * cosecant_single(y, x)) + ((1 - r) * secant_single(y, x)); + x = next_x; + if((pow(x, 2) + pow(y, 2)) >= escape) break; + } + + + + frame_output[img_index] = iter; + //frame_output[img_index] = mask[img_index] * 255; +} diff --git a/mask_tests/mask.png b/mask_tests/mask.png new file mode 120000 index 0000000..5209b4e --- /dev/null +++ b/mask_tests/mask.png @@ -0,0 +1 @@ +../masks/threedots.png \ No newline at end of file diff --git a/mask_tests/mask.py b/mask_tests/mask.py new file mode 100755 index 0000000..ee801d4 --- /dev/null +++ b/mask_tests/mask.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python3 +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import pyopencl as cl +from alive_progress import alive_bar +from matplotlib.backend_bases import MouseButton +from PIL import Image + +img_res_x = 1000 +img_res_y = 1000 +total_pixels = img_res_x * img_res_y # so we don't gotta compute it every time + +periods = 1 +square_x = 0 +square_y = 0 + +x_min = (-periods * np.pi) + (square_x * np.pi) +x_max = (periods * np.pi) + (square_x * np.pi) +y_min = (-periods * np.pi) + (square_y * np.pi) +y_max = (periods * np.pi) + (square_y * np.pi) + +escape = 10000 +iterations = 255*3 +c_x = 2 * np.pi +c_y = 2 * np.pi + +animation_progres_save = "ani1_less.mp4" +single_frame_save = "asdf.png" + +opencl_context = cl.create_some_context(interactive=False) +opencl_queue = cl.CommandQueue(opencl_context) + +kernel_src_path = "./kernel.c" + +frames = 1 +rendered_frames = [] + +image = np.empty([img_res_x, img_res_y], np.uint32) + +plt.style.use('dark_background') +# fuck this shit +fig = plt.figure(frameon=False) +fig.set_size_inches(img_res_x/fig.dpi, img_res_y/fig.dpi) +#fig.set_size_inches(width/height, 1, forward=False) + + +ax = plt.Axes(fig, [0., 0., 1., 1.]) +#ax.set_axis_off() +fig.add_axes(ax) + +cmap = plt.cm.viridis +cmap.set_bad((0,0,0)) +cmap.set_over((0,0,0)) +cmap.set_under((0,0,0)) + +mask_path = "mask.png" + +bruh = None + +def on_click(event): + global bruh + split_ratio = 1 + if (event.button is MouseButton.MIDDLE) and (event.inaxes): + # there's probably a way to set global coordinates; + # I don't expect this to go anywhere so I don't really care + on_x = ((event.xdata / img_res_x) * abs(x_max - x_min)) + x_min + on_y = ((event.ydata / img_res_y) * abs(y_max - y_min)) + y_min + + #I, uh, also don't know the best way to replicate the openCL code automaticly in python + #so ajust if nessesary + x_hops = [] + y_hops = [] + for i in range(iterations): + x_hops.append(((on_x - x_min) / abs(x_max - x_min)) * img_res_x) + y_hops.append(((on_y - y_min) / abs(y_max - y_min)) * img_res_y) + next_x = on_x/np.tan(on_y) + on_y = on_y/np.tan(on_x) + on_x = next_x + if on_x**2 + on_y**2 > escape: + break + print(y_hops[0]) + print(on_y) + print("{} hops".format(len(x_hops))) + if bruh: + bruh.pop(0).remove() + bruh = ax.plot(x_hops, y_hops) + plt.draw() + + + + print(on_x, on_y) + + +print("compiling openCL kernel...") +with open(kernel_src_path, 'r') as kernel_src: + compiled_kernel = cl.Program(opencl_context, kernel_src.read()).build() + +encoding_progress = alive_bar(frames, bar = 'filling', spinner = 'waves') + +def display_encoder_progress(current_frame: int, total_frames: int): + print("Encoding: frame {}/{}".format(current_frame, frames)) + +#open image +mask = np.asarray(Image.open(mask_path).convert("L"), dtype=np.double) +mask.setflags(write=1) +mask /= np.max(mask) # normalize +print(mask.shape) +print(mask.dtype) +#ax.imshow(mask, norm="linear") +#plt.show() + +image_buffer = cl.Buffer(opencl_context, cl.mem_flags.WRITE_ONLY, image.nbytes) +mask_buffer = cl.Buffer(opencl_context, cl.mem_flags.READ_ONLY, mask.nbytes) +cl.enqueue_copy(opencl_queue, mask_buffer, mask).wait() +#mask_buffer = cl.array.Array(opencl_context,mask.shape, dtype=np.uint8, data=mask) + + + + +# TODO clean this up +print("Rendering {} frames...".format(frames)) +if frames > 1: + with alive_bar(frames, bar = 'filling', spinner = 'waves') as bar_total: + for frame_i in range(0, frames): + compiled_kernel.render_frame(opencl_queue, image.shape, None, image_buffer, + np.double(abs(x_max - x_min) / img_res_x), + np.double(abs(y_max - y_min) / img_res_y), + np.double(x_min), np.double(y_min), + np.uint32(iterations), np.uint32(escape), + np.double(frame_i / frames)) + + + cl.enqueue_copy(opencl_queue, image, image_buffer).wait() + rendered_frame = ax.imshow(image, norm="log", aspect="auto", cmap=cmap, animated="True") + rendered_frames.append([rendered_frame]) + bar_total() + print("Encoding/Saving...") + ani = animation.ArtistAnimation(fig, rendered_frames, interval=30, blit=True) + ani.save(animation_progres_save, extra_args=['-preset', 'lossless'], progress_callback=display_encoder_progress, codec="h264_nvenc") +else: + compiled_kernel.render_frame(opencl_queue, image.shape, None, image_buffer, mask_buffer, + np.double(abs(x_max - x_min) / img_res_x), + np.double(abs(y_max - y_min) / img_res_y), + np.double(x_min), np.double(y_min), + np.uint32(iterations), np.uint32(escape), + np.double(1)) + cl.enqueue_copy(opencl_queue, image, image_buffer).wait() + ax.imshow(image, norm="log", aspect="auto", cmap=cmap) + fig.savefig(single_frame_save) + plt.autoscale(False) + plt.connect('motion_notify_event', on_click) + plt.show() diff --git a/mask_tests/mask.py.bak b/mask_tests/mask.py.bak new file mode 100644 index 0000000..8fd3513 --- /dev/null +++ b/mask_tests/mask.py.bak @@ -0,0 +1,142 @@ +#!/usr/bin/env python3 +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import pyopencl as cl +from alive_progress import alive_bar +from matplotlib.backend_bases import MouseButton +from PIL import Image + +img_res_x = 1000 +img_res_y = 1000 +total_pixels = img_res_x * img_res_y # so we don't gotta compute it every time + +periods = 1 +square_x = 0 +square_y = 0 + +x_min = (-periods * np.pi) + (square_x * np.pi) +x_max = (periods * np.pi) + (square_x * np.pi) +y_min = (-periods * np.pi) + (square_y * np.pi) +y_max = (periods * np.pi) + (square_y * np.pi) + +escape = 10000 +iterations = 255*3 +c_x = 2 * np.pi +c_y = 2 * np.pi + +animation_progres_save = "ani1_less.mp4" +single_frame_save = "asdf.png" + +opencl_context = cl.create_some_context(interactive=False) +opencl_queue = cl.CommandQueue(opencl_context) + +kernel_src_path = "./kernel.c" + +frames = 1 +rendered_frames = [] + +image = np.empty([img_res_x, img_res_y], np.uint32) +image_buffer = cl.Buffer(opencl_context, cl.mem_flags.WRITE_ONLY, image.nbytes) + +plt.style.use('dark_background') +# fuck this shit +fig = plt.figure(frameon=False) +fig.set_size_inches(img_res_x/fig.dpi, img_res_y/fig.dpi) +#fig.set_size_inches(width/height, 1, forward=False) + + +ax = plt.Axes(fig, [0., 0., 1., 1.]) +#ax.set_axis_off() +fig.add_axes(ax) + +cmap = plt.cm.viridis +cmap.set_bad((0,0,0)) +cmap.set_over((0,0,0)) +cmap.set_under((0,0,0)) + +mask_path = "mask.png" + +bruh = None + +def on_click(event): + global bruh + split_ratio = 1 + if (event.button is MouseButton.MIDDLE) and (event.inaxes): + # there's probably a way to set global coordinates; + # I don't expect this to go anywhere so I don't really care + on_x = ((event.xdata / img_res_x) * abs(x_max - x_min)) + x_min + on_y = ((event.ydata / img_res_y) * abs(y_max - y_min)) + y_min + + #I, uh, also don't know the best way to replicate the openCL code automaticly in python + #so ajust if nessesary + x_hops = [] + y_hops = [] + for i in range(iterations): + x_hops.append(((on_x - x_min) / abs(x_max - x_min)) * img_res_x) + y_hops.append(((on_y - y_min) / abs(y_max - y_min)) * img_res_y) + next_x = on_x/np.tan(on_y) + on_y = on_y/np.tan(on_x) + on_x = next_x + if on_x**2 + on_y**2 > escape: + break + print(y_hops[0]) + print(on_y) + print("{} hops".format(len(x_hops))) + if bruh: + bruh.pop(0).remove() + bruh = ax.plot(x_hops, y_hops) + plt.draw() + + + + print(on_x, on_y) + + +print("compiling openCL kernel...") +with open(kernel_src_path, 'r') as kernel_src: + compiled_kernel = cl.Program(opencl_context, kernel_src.read()).build() + +encoding_progress = alive_bar(frames, bar = 'filling', spinner = 'waves') + +def display_encoder_progress(current_frame: int, total_frames: int): + print("Encoding: frame {}/{}".format(current_frame, frames)) + +with open G + + +# TODO clean this up +print("Rendering {} frames...".format(frames)) +if frames > 1: + with alive_bar(frames, bar = 'filling', spinner = 'waves') as bar_total: + for frame_i in range(0, frames): + compiled_kernel.render_frame(opencl_queue, image.shape, None, image_buffer, + np.double(abs(x_max - x_min) / img_res_x), + np.double(abs(y_max - y_min) / img_res_y), + np.double(x_min), np.double(y_min), + np.uint32(iterations), np.uint32(escape), + np.double(frame_i / frames)) + + + cl.enqueue_copy(opencl_queue, image, image_buffer).wait() + rendered_frame = ax.imshow(image, norm="log", aspect="auto", cmap=cmap, animated="True") + rendered_frames.append([rendered_frame]) + bar_total() + print("Encoding/Saving...") + ani = animation.ArtistAnimation(fig, rendered_frames, interval=30, blit=True) + ani.save(animation_progres_save, extra_args=['-preset', 'lossless'], progress_callback=display_encoder_progress, codec="h264_nvenc") +else: + compiled_kernel.render_frame(opencl_queue, image.shape, None, image_buffer, + np.double(abs(x_max - x_min) / img_res_x), + np.double(abs(y_max - y_min) / img_res_y), + np.double(x_min), np.double(y_min), + np.uint32(iterations), np.uint32(escape), + np.double(1)) + cl.enqueue_copy(opencl_queue, image, image_buffer).wait() + ax.imshow(image, norm="log", aspect="auto", cmap=cmap) + fig.savefig(single_frame_save) + plt.autoscale(False) + plt.connect('motion_notify_event', on_click) + plt.show() + + diff --git a/mask_tests/notes b/mask_tests/notes new file mode 100644 index 0000000..50249d7 --- /dev/null +++ b/mask_tests/notes @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import pyopencl as cl +from alive_progress import alive_bar + +img_res_x = 2000 +img_res_y = 2000 +total_pixels = img_res_x * img_res_y # so we don't gotta compute it every time + +periods = 1 +square_x = 0 +square_y = 0 + +xmin = (-periods * np.pi) + (square_x * np.pi) +xmax = (periods * np.pi) + (square_x * np.pi) +ymin = (-periods * np.pi) + (square_y * np.pi) +ymax = (periods * np.pi) + (square_y * np.pi) + +escape = 10000 +iterations = 255*3 +c_x = 2 * np.pi +c_y = 2 * np.pi + +animation_progres_save = "./animations" +frames = 120 + +rendered_frames = [] + + +plt.style.use('dark_background') +# fuck this shit +fig = plt.figure(frameon=False) +fig.set_size_inches(img_res_x/fig.dpi, img_res_y/fig.dpi) +#fig.set_size_inches(width/height, 1, forward=False) + +ax = plt.Axes(fig, [0., 0., 1., 1.]) +ax.set_axis_off() +fig.add_axes(ax) + +cmap = plt.cm.viridis +cmap.set_bad((0,0,0)) +cmap.set_over((0,0,0)) +cmap.set_under((0,0,0)) + +opencl_context = cl.create_some_context() +opencl_queue = cl.CommandQueue(opencl_context) + + +#def render(): +# image = np.empty([img_res_y, img_res_x]) +# print("Rendering frames") +# with alive_bar(frames, bar = 'filling', spinner = 'waves') as bar_total: +# for frame in range(frames): +# split_ratio = frame / frames +# for pix_y, y in enumerate(np.linspace(ymin, ymax, img_res_y)): +# for pix_x, x in enumerate(np.linspace(xmin, xmax, img_res_x)): +# on_x = x +# on_y = y +# for i in range(iterations): +# next_x = (split_ratio * (on_x/np.sin(on_y))) + ((1 - split_ratio) * on_x/np.tan(on_y)) +# on_y = (split_ratio * (on_y/np.sin(on_x))) + ((1 - split_ratio) * on_y/np.tan(on_x)) +# on_x = next_x +# if on_x**2 + on_y**2 > escape: +# break +# image[pix_y][pix_x] = i +# rendered_frame = ax.imshow(image, norm="log", aspect="auto", cmap=cmap, animated=False) +# rendered_frames.append([rendered_frame]) +# bar_total() + +def display(): + print(rendered_frames) + ani = animation.ArtistAnimation(fig, rendered_frames, interval=30, blit=True) + ani.save("test.mp4") + plt.show() + +render() +display() -- cgit v1.2.3