def render_scene(self, params="none", x_range=(-10, 10), y_range=(-10, 10), camera_position='default', projection_type='none', resolution=5, density=5, black_ref=-1.0, white_ref=1.0, default=hl.BLUE, style='uniform', region_params="none", display=False, save=False, filename='default', backdrop="new"): if params == "none": params = {} points, _ = self.frame_at_p(params=params, camera_position=camera_position, projection_type=projection_type, region_params=region_params, style=style, density=density) arr = hl.set_to_gradient(points=points, x_range=x_range, y_range=y_range, black_ref=black_ref, white_ref=white_ref, default=default, resolution=resolution, backdrop=backdrop) if display: hl.render_from_array(arr) if save: hl.save_img(arr, filename) return arr
def video(frame_function, frame_arguments, filename, fps=5, preview=False, parallel_frames=True): """ Create a video with frames generated by applying the frame_func to each element in the frame_arguments list. Final video duration in seconds is len(frame_arguments)/fps :param frame_function: function called to generate a frame of the video using a frame_argument frames should be 1 or 3 channel arrays from [0, 255] :param frame_arguments: list of arguments used to generate each frame of the video :param filename: :param fps: :param parallel: """ if preview: hl.render_from_array(frame_function(frame_arguments[0])) mid = int(len(frame_arguments) / 2) hl.render_from_array(frame_function(frame_arguments[mid])) hl.render_from_array(frame_function(frame_arguments[-1])) # Create threads to render frames in parallel. Store rendered frames in a list if parallel_frames: with ProcessingPool() as pool: frames = pool.map(frame_function, frame_arguments[1:]) # OpenCV sometimes crashed with multiprocess! else: frames = list(map(frame_function, frame_arguments[1:])) frame_size = np.shape(frames[0]) is_color = len(frame_size) > 2 and frame_size[2] > 1 h = frame_size[0] w = frame_size[1] fourcc = cv2.VideoWriter_fourcc(*'MP42') video_writer = cv2.VideoWriter('{0}.avi'.format(filename), fourcc, float(fps), (w, h), isColor=is_color) # Create a video of the rendered frames. Have to transpose rows and columns for the video writer... # https://github.com/opencv/opencv/issues/4655 # TODO Don't do it like this. It will flip the image for frame in frames: video_writer.write(frame.astype(np.uint8)) video_writer.release() print("Wrote video {}".format(filename))
import hallucinator as hl clothoid = hl.plot_clothoid(padding=5, plot_range=(0, 400), resolution=6) hl.render_from_array(clothoid)
import sys sys.path.append('../hallucinator') import hallucinator import numpy as np import math f = lambda x, y: math.sin(500 * math.sqrt(x**2 + y**2) / (2 * np.pi)) arr = hallucinator.phasegrid(f, (-5, 5), (-5, 5), resolution=100) grad = hallucinator.arr_to_gradient(arr, black_ref=-2.0, white_ref=2.0) hallucinator.render_from_array(grad)
return ne.evaluate("values % threshold") def real(values): return ne.evaluate("cos(values)") def imaginary(values): return ne.evaluate("sin(values)") def phase_conjugate(values, threshold=2 * math.pi): return ne.evaluate("threshold - values") '''xy = xy_plane() persp_xy = perspective_plane(xy) zp = perspective_zp(persp_xy) hl.render_from_array(imagify(xy)) hl.render_from_array(imagify(persp_xy)) hl.render_from_array(imagify(zp))''' # hl.video2( # frame_function=lambda z: imagify(perspective_zp(x2y2, z)), # frame_arguments=np.geomspace(1, 1000, num=200), # fps=10, # preview=False, # filename="../images/perspective_zone" # ) ##################################################################
a_density = 30 b_density = 30 direction = (0, 0, 1) x_range = (-20, 20) y_range = (-20, 20) zp = make_zone_plate(distance=distance, wavelength=wavelength, phase_diff=phase_diff, a_density=a_density, b_density=b_density, direction=direction, x_range=x_range, y_range=y_range) hl.render_from_array(zp) hl.save_img( zp, 'zoneplates/zoneplate_d{0}-w{1}-p{2}-a{3}-b{4}-dir{5}-xr{6}-yr{7}'.format( distance, wavelength, phase_diff, a_density, b_density, direction, x_range, y_range)) ''' hl.video(frame_func=lambda t: make_zone_plate(distance=distance, wavelength=wavelength, phase_diff=phase_diff, a_density=a_density, b_density=b_density*t, direction=direction), filename='zoneplates/zone_plate_change_b_density', t_range=(1, 5),
rotate_x = math.pi / 4 scene.add_object(hl.ParaObject3(hl.gen_ripple(amplitude=0.5, frequency=3, phase=0), region_type='2d', region_params={'surface_range': ((-10, 10), (-10, 10))}, species='surface').rotate(theta=rotate_x, axis=(1, 0, 0)).translate(location), "ripple") scene.add_object(hl.ParaObject3(hl.gen_ripple(amplitude=0.5, frequency=3, phase=0), region_type='2d', region_params={'surface_range': ((-10, 10), (-10, 10))}, species='surface').rotate(theta=-rotate_x, axis=(1, 1, 0)).translate(location_2), "ripple2") scene.add_object(hl.path_3(path_func=hl.gen_spiral(coil_density=1, radius=2), p_range=(0, 10), path_length=10 * math.pi).translate(spiral_location), "spiral") camscene = scene.render_scene(camera_position=(0, -3, -50), projection_type=hl.Projections.WEAK, styles={'ripple': hl.Styles.WIREFRAME, 'ripple2': hl.Styles.UNIFORM, 'spiral': hl.Styles.UNIFORM}, x_range=(-10, 10), y_range=(-10, 10), resolution=50, densities={'ripple': (3, 3), 'ripple2': (5, 5), 'spiral': 50}) hl.render_from_array(camscene)
import hallucinator as hl import math zp = hl.fourier_zp(10) zp_pi = hl.fourier_zp(10, phase=math.pi) zp1 = hl.fourier_zp(15, (-0.5, 0)) zp1_pi = hl.fourier_zp(15, (-0.5, 0), math.pi) zp2 = hl.fourier_zp(15, (0.5, 0)) zp2_pi = hl.fourier_zp(15, (0.5, 0), math.pi) plane = hl.sampling_image(lambda p: abs(zp1(p) + zp2(p)), value_range=(-10, 10), image_size=(1000, 1000), binary=False) hl.render_from_array(plane) '''plane = hl.sampling_image(zp, value_range=(-10, 10), image_size=(800, 800), binary=False) hl.render_from_array(plane) plane = hl.sampling_image(zp_pi, value_range=(-10, 10), image_size=(800, 800), binary=False) hl.render_from_array(plane)'''
print(hl.intensity_at(sources=[source1], location=(1, 0))) print(hl.intensity_at(sources=[source2], location=(1, 0))) print(hl.intensity_at(sources=[source1, source2], location=(1, 0))) print(hl.intensity_at(sources=[source1, source2], location=(1.01, 0)))''' a_range = (-1, 1) b_range = (-1, 1) intensities = hl.eval_surface_intensity( sources=[*line_of_sources], surface=surface, a_range=a_range, b_range=b_range, a_density=2000, b_density=2000, # density=100 ) canv = hl.set_to_gradient( intensities, x_range=a_range, y_range=b_range, black_ref=0, white_ref=4.0, default=hl.GRAY, resolution=2000, ) hl.render_from_array(canv) hl.save_img(canv, 'width{0}_{1}-points_random'.format(slit_width, num_sources)) #hl.save_img(canv, 'two_point_stacked')
import hallucinator as hl scene = hl.MonochromeScene() scene.add_object(hl.rectangle(h=30, w=40), "rect") rect_arr = scene.render_scene(x_range=(-50, 50), y_range=(-50, 50), densities=1, projection_type=hl.Projections.ORTHO) hl.render_from_array(rect_arr)
rotate_z=1, location=(0, 0, 40)), "surface") camscene = scene.render_scene(camera_position=(0, 0, -15), projection_type=hl.Projections.WEAK, styles=hl.Styles.UNIFORM, x_range=(-7, 7), y_range=(-7, 7), resolution=75, densities=(6, 30)) return camscene hl.render_from_array(wavy_scene(t=0)) params = dict( frame_function=lambda d: wavy_scene(**d), frame_arguments=hl.unroll_dict(dict( t=hl.np.linspace(0, 37, num=1500), )), filename=f"../videos/lasagna3", fps=15, preview=True, parallel_frames=False, ) #hl.video(**params)
b_range=(-30, 30), a_density=sampling_density, b_density=sampling_density) canv = hl.set_to_gradient(points, x_range=(-30, 30), y_range=(-30, 30), black_ref=0, white_ref=4.0, default=hl.GRAY, resolution=50) return canv '''canv = make_pinholes(distance=10, wavelength=0.001, phase_diff=0, sampling_density=20, separation=0.1) hl.render_from_array(canv) hl.save_img(canv, 'two_pinhole_d10-w0.001-s0.1-sd20')''' hl._deprecated_video(frame_func=lambda t: make_pinholes(distance=10, wavelength=0.001, phase_diff=0, sampling_density=t**2, separation=0.1), filename='pinholes_2', t_range=(0, 7),
print('wf(0, 1, 1): ', wf(0, 1, 1)) print('wf(1, 0, 1): ', wf(1, 0, 1)) frame1 = hallucinator.gradient_frame(f=wf, t=0, white_ref=4, black_ref=0, x_range=(-5, 5), y_range=(-5, 5), resolution=20) frame2 = hallucinator.gradient_frame(f=wf, t=1, white_ref=4, black_ref=0, x_range=(-5, 5), y_range=(-5, 5), resolution=20) hallucinator.render_from_array(frame1) hallucinator.render_from_array(frame2) hallucinator.wave_2_gradient_video(wf, t_range=(0, 5), x_range=(-5, 5), y_range=(-5, 5), resolution=5, white_ref=4, black_ref=0, fps=4, filename='propw2d')
def vid(): def zp_func(x, y, z, zoom): return hl.opl_zp( hl.perspective_plane(p=[x, y, z], xy=hl.xy_plane(value_range=(-zoom, zoom), resolution=500))) def zp(zoom): return hl.phase_conjugate(zp_func(0, 0, 10, zoom)) zp10 = zp(1) zp10pt = hl.phase_threshold(zp10) def percieve_zp(zp1, zp2): # zp2pt = hl.phase_threshold(zp2) zp1 = hl.phase_threshold(zp1) zp2 = hl.phase_threshold(zp2) zp3 = hl.phase_threshold(zp1 + zp2) # print(zp2) # combined1 = hl.np.concatenate([zp3, zp10pt]) # combined2 = hl.np.concatenate([zp2pt, zp3]) # combined = hl.np.hstack([combined1, combined2]) # return hl.imagify(zp3, hsv=True, bwref=(0, 2*math.pi)) return hl.imagify(zp3, hsv=False, bwref=(0, 2 * math.pi)) def filenamer(x=0, y=0, z=10, zoom=10, range=(-10, 10)): return f"crazy_thing.png_x={x}_y={y}_z={z}_zoom={zoom}_range={str(range)}_res={500}" hl.render_from_array( hl.imagify(percieve_zp(zp10, zp_func(1, 1, 10, 1)), hsv=False, bwref=(0, 2 * math.pi))) return # hl.video( # frame_function=lambda t: percieve_zp(zp10, zp_func(10, 10, 10*math.sin(t)+10, 10)), # frame_arguments=hl.np.linspace(0, 10, num=3000), # fps=30, # filename=filenamer(z="10sin(t)+10", range=(0, 10)), # parallel_frames=True # ) # hl.video( # frame_function=lambda t: percieve_zp(zp10, zp_func(0, 0, t, 10)), # frame_arguments=hl.np.linspace(0, 30, num=3000), # fps=30, # filename=filenamer(z="t", range=(0, 30)), # parallel_frames=True # ) # hl.video( # frame_function=lambda t: percieve_zp(zp10, zp_func(t, 0, 10, 10)), # frame_arguments=hl.np.linspace(-20, 20, num=3000), # fps=30, # filename=filenamer(x="t", range=(-20, 20)), # parallel_frames=True # ) # hl.video( # frame_function=lambda t: percieve_zp(zp(t), zp_func(0, 0, 5, t)), # frame_arguments=hl.np.linspace(0, 400, num=3000)[::-1], # fps=10, # filename=filenamer(z=5, zoom="t", range=(400, 0)), # parallel_frames=True # ) # hl.video( # frame_function=lambda t: percieve_zp(zp10, zp_func(t, 0, t+10, 10)), # frame_arguments=hl.np.linspace(-20, 20, num=3000), # fps=30, # filename=filenamer(x="t", z="t+10", range=(-20, 20)), # parallel_frames=True # ) # hl.video( # frame_function=lambda t: percieve_zp(zp10, zp_func(t, 0, 5*math.sin(t)+5, 10)), # frame_arguments=hl.np.linspace(-6, 6, num=3000), # fps=30, # filename=filenamer(x="t", z="5sin(t)+5", range=(-6, 6)), # parallel_frames=True # ) # hl.video( # frame_function=lambda t: percieve_zp(zp10, zp_func(t*math.cos(t), 0, 10+t*math.sin(t), 10)), # frame_arguments=hl.np.linspace(-10, 10, num=3000), # fps=30, # filename=filenamer(x="tcos(t)", z="10+tsin(t)", range=(-10, 10)), # parallel_frames=True # ) hl.video(frame_function=lambda t: percieve_zp( zp10, zp_func(100 * math.cos(t), 100 * math.sin(t), 15, 10)), frame_arguments=hl.np.linspace(0, math.pi, num=600), fps=60, filename=filenamer(x="cos(t)", y="sin(t)", range=(0, 2 * math.pi)), parallel_frames=True, preview=True)
zp3 = hl.fourier_phase_zp(10, phase=math.pi) zp4 = hl.fourier_phase_zp(10, phase=4)''' # xy = hl.xy_plane(value_range=(-10, 10)) # persp_xy = hl.perspective_plane(xy, p=(0, 0, 100)) # persp_xy_2 = hl.perspective_plane(xy, p=(0, 0, 100)) # zp_opl = hl.opl_zp(persp_xy) # zp_opl_2 = hl.phase_conjugate(hl.opl_zp(persp_xy_2)) # #zp_opl_2 = hl.opl_zp(persp_xy) # zp1_phase = hl.phase_threshold(zp_opl) # zp2_phase = hl.phase_threshold(zp_opl_2) # zp1_r = hl.real(zp1_phase) # zp2_r = hl.real(zp2_phase) # zp1_i = hl.imaginary(hl.phase_threshold(zp_opl)) # zp2_i = hl.imaginary(hl.phase_threshold(zp_opl_2)) ''''hl.render_from_array(hl.imagify(zp1, bwref=(-1, 1))) hl.render_from_array(hl.imagify(zp2, bwref=(-1, 1))) hl.render_from_array(hl.imagify(zp1_i, bwref=(-1, 1))) hl.render_from_array(hl.imagify(zp2_i, bwref=(-1, 1)))''' #hl.render_from_array(hl.imagify(zp1_phase, bwref=(0, 2*math.pi))) #hl.render_from_array(hl.imagify(zp2_phase, bwref=(0, 2*math.pi))) '''hl.plot_images([hl.imagify(zp1_phase, bwref=(0, 2*math.pi)), hl.imagify(zp2_phase, bwref=(0, 2*math.pi))], titles=['original', 'conjugate'])''' def zoneplate_product(): xy = hl.xy_plane(value_range=(-10, 10), resolution=300) def images_func(a, b): zp1 = hl.opl_zp(hl.perspective_plane(xy, p=[0, 0, a])) zp2 = hl.phase_conjugate(