def test_composite_vr(self): ds = fake_random_ds(64) dd = ds.sphere(ds.domain_center, 0.45 * ds.domain_width[0]) ds.field_info[ds.field_list[0]].take_log = False sc = Scene() cam = sc.add_camera(ds) cam.resolution = (512, 512) vr = create_volume_source(dd, field=ds.field_list[0]) vr.transfer_function.clear() vr.transfer_function.grey_opacity = True vr.transfer_function.map_to_colormap(0.0, 1.0, scale=10.0, colormap="Reds") sc.add_source(vr) cam.set_width(1.8 * ds.domain_width) cam.lens.setup_box_properties(cam) # Create Arbitrary Z-buffer empty = cam.lens.new_image(cam) z = np.empty(empty.shape[:2], dtype="float64") # Let's put a blue plane right through the center z[:] = cam.width[2] / 2.0 empty[:, :, 2] = 1.0 # Set blue to 1's empty[:, :, 3] = 1.0 # Set alpha to 1's zbuffer = ZBuffer(empty, z) zsource = OpaqueSource() zsource.set_zbuffer(zbuffer) sc.add_source(zsource) im = sc.render() im.write_png("composite.png")
def test_points_vr(self): ds = fake_random_ds(64) dd = ds.sphere(ds.domain_center, 0.45 * ds.domain_width[0]) ds.field_info[ds.field_list[0]].take_log = False sc = Scene() cam = sc.add_camera(ds) cam.resolution = (512, 512) vr = create_volume_source(dd, field=ds.field_list[0]) vr.transfer_function.clear() vr.transfer_function.grey_opacity = False vr.transfer_function.map_to_colormap(0.0, 1.0, scale=10.0, colormap="Reds") sc.add_source(vr) cam.set_width(1.8 * ds.domain_width) cam.lens.setup_box_properties(cam) # DRAW SOME POINTS npoints = 1000 vertices = np.random.random([npoints, 3]) colors = np.random.random([npoints, 4]) colors[:, 3] = 0.10 points_source = PointSource(vertices, colors=colors) sc.add_source(points_source) im = sc.render() im.write_png("points.png") return im
def test_perspective_lens(self): sc = Scene() cam = sc.add_camera(self.ds, lens_type="perspective") cam.position = self.ds.arr(np.array([1.0, 1.0, 1.0]), "code_length") vol = create_volume_source(self.ds, field=self.field) tf = vol.transfer_function tf.grey_opacity = True sc.add_source(vol) sc.save(f"test_perspective_{self.field[1]}.png", sigma_clip=6.0)
def test_spherical_lens(self): sc = Scene() cam = sc.add_camera(self.ds, lens_type="spherical") cam.resolution = [256, 128] cam.position = self.ds.arr(np.array([0.6, 0.5, 0.5]), "code_length") vol = create_volume_source(self.ds, field=self.field) tf = vol.transfer_function tf.grey_opacity = True sc.add_source(vol) sc.save(f"test_spherical_{self.field[1]}.png", sigma_clip=6.0)
def test_plane_lens(self): dd = self.ds.sphere(self.ds.domain_center, self.ds.domain_width[0] / 10) sc = Scene() cam = sc.add_camera(dd, lens_type="plane-parallel") cam.set_width(self.ds.domain_width * 1e-2) v, c = self.ds.find_max("density") vol = create_volume_source(dd, field=self.field) tf = vol.transfer_function tf.grey_opacity = True sc.add_source(vol) sc.save(f"test_plane_{self.field[1]}.png", sigma_clip=6.0)
def test_fisheye_lens(self): dd = self.ds.sphere(self.ds.domain_center, self.ds.domain_width[0] / 10) sc = Scene() cam = sc.add_camera(dd, lens_type="fisheye") cam.lens.fov = 360.0 cam.set_width(self.ds.domain_width) v, c = self.ds.find_max("density") cam.set_position(c - 0.0005 * self.ds.domain_width) vol = create_volume_source(dd, field=self.field) tf = vol.transfer_function tf.grey_opacity = True sc.add_source(vol) sc.save(f"test_fisheye_{self.field[1]}.png", sigma_clip=6.0)
def test_composite_vr(self): ds = fake_random_ds(64) dd = ds.sphere(ds.domain_center, 0.45 * ds.domain_width[0]) ds.field_info[ds.field_list[0]].take_log = False sc = Scene() cam = sc.add_camera(ds) cam.resolution = (512, 512) vr = create_volume_source(dd, field=ds.field_list[0]) vr.transfer_function.clear() vr.transfer_function.grey_opacity = True vr.transfer_function.map_to_colormap(0.0, 1.0, scale=3.0, colormap="Reds") sc.add_source(vr) cam.set_width(1.8 * ds.domain_width) cam.lens.setup_box_properties(cam) # DRAW SOME LINES npoints = 100 vertices = np.random.random([npoints, 2, 3]) colors = np.random.random([npoints, 4]) colors[:, 3] = 0.10 box_source = BoxSource(ds.domain_left_edge, ds.domain_right_edge, color=[1.0, 1.0, 1.0, 1.0]) sc.add_source(box_source) LE = ds.domain_left_edge + np.array([0.1, 0.0, 0.3 ]) * ds.domain_left_edge.uq RE = ds.domain_right_edge - np.array([0.1, 0.2, 0.3 ]) * ds.domain_left_edge.uq color = np.array([0.0, 1.0, 0.0, 0.10]) box_source = BoxSource(LE, RE, color=color) sc.add_source(box_source) line_source = LineSource(vertices, colors) sc.add_source(line_source) im = sc.render() im = ImageArray(im.d) im.write_png("composite.png") return im
def test_rotation(self): ds = fake_random_ds(32) ds2 = fake_random_ds(32) dd = ds.sphere(ds.domain_center, ds.domain_width[0] / 2) dd2 = ds2.sphere(ds2.domain_center, ds2.domain_width[0] / 2) im, sc = volume_render(dd, field=("gas", "density")) im.write_png("test.png") vol = sc.get_source(0) tf = vol.transfer_function tf.clear() mi, ma = dd.quantities.extrema(("gas", "density")) mi = np.log10(mi) ma = np.log10(ma) mi_bound = ((ma - mi) * (0.10)) + mi ma_bound = ((ma - mi) * (0.90)) + mi tf.map_to_colormap(mi_bound, ma_bound, scale=0.01, colormap="Blues_r") vol2 = create_volume_source(dd2, field=("gas", "density")) sc.add_source(vol2) tf = vol2.transfer_function tf.clear() mi, ma = dd2.quantities.extrema(("gas", "density")) mi = np.log10(mi) ma = np.log10(ma) mi_bound = ((ma - mi) * (0.10)) + mi ma_bound = ((ma - mi) * (0.90)) + mi tf.map_to_colormap(mi_bound, ma_bound, scale=0.01, colormap="Reds_r") fname = "test_scene.pdf" sc.save(fname, sigma_clip=6.0) assert_fname(fname) fname = "test_rot.png" sc.camera.pitch(np.pi) sc.render() sc.save(fname, sigma_clip=6.0, render=False) assert_fname(fname)
def test_orientation(): ds = fake_vr_orientation_test_ds() sc = Scene() vol = create_volume_source(ds, field=("gas", "density")) sc.add_source(vol) tf = vol.transfer_function tf = ColorTransferFunction((0.1, 1.0)) tf.sample_colormap(1.0, 0.01, colormap="coolwarm") tf.sample_colormap(0.8, 0.01, colormap="coolwarm") tf.sample_colormap(0.6, 0.01, colormap="coolwarm") tf.sample_colormap(0.3, 0.01, colormap="coolwarm") n_frames = 1 orientations = [[-0.3, -0.1, 0.8]] theta = np.pi / n_frames test_name = "vr_orientation" for lens_type, decimals in [("perspective", 12), ("plane-parallel", 2)]: # set a much lower precision for plane-parallel tests, see # https://github.com/yt-project/yt/issue/3069 # https://github.com/yt-project/yt/pull/3068 # https://github.com/yt-project/yt/pull/3294 frame = 0 cam = sc.add_camera(ds, lens_type=lens_type) cam.resolution = (1000, 1000) cam.position = ds.arr(np.array([-4.0, 0.0, 0.0]), "code_length") cam.switch_orientation(normal_vector=[1.0, 0.0, 0.0], north_vector=[0.0, 0.0, 1.0]) cam.set_width(ds.domain_width * 2.0) desc = "%s_%04d" % (lens_type, frame) test1 = VRImageComparisonTest(sc, ds, desc, decimals) test1.answer_name = test_name yield test1 for _ in range(n_frames): frame += 1 center = ds.arr([0, 0, 0], "code_length") cam.yaw(theta, rot_center=center) desc = "yaw_%s_%04d" % (lens_type, frame) test2 = VRImageComparisonTest(sc, ds, desc, decimals) test2.answer_name = test_name yield test2 for _ in range(n_frames): frame += 1 theta = np.pi / n_frames center = ds.arr([0, 0, 0], "code_length") cam.pitch(theta, rot_center=center) desc = "pitch_%s_%04d" % (lens_type, frame) test3 = VRImageComparisonTest(sc, ds, desc, decimals) test3.answer_name = test_name yield test3 for _ in range(n_frames): frame += 1 theta = np.pi / n_frames center = ds.arr([0, 0, 0], "code_length") cam.roll(theta, rot_center=center) desc = "roll_%s_%04d" % (lens_type, frame) test4 = VRImageComparisonTest(sc, ds, desc, decimals) test4.answer_name = test_name yield test4 center = [0.5, 0.5, 0.5] width = [1.0, 1.0, 1.0] for i, orientation in enumerate(orientations): image = off_axis_projection(ds, center, orientation, width, 512, ("gas", "density"), no_ghost=False) def offaxis_image_func(filename_prefix): return image.write_image(filename_prefix) test5 = GenericImageTest(ds, offaxis_image_func, decimals) test5.prefix = f"oap_orientation_{i}" test5.answer_name = test_name yield test5
import yt from yt.visualization.volume_rendering.api import Scene, create_volume_source field = ("gas", "density") # normal_vector points from camera to the center of the final projection. # Now we look at the positive x direction. normal_vector = [1.0, 0.0, 0.0] # north_vector defines the "top" direction of the projection, which is # positive z direction here. north_vector = [0.0, 0.0, 1.0] # Follow the simple_volume_rendering cookbook for the first part of this. ds = yt.load("IsolatedGalaxy/galaxy0030/galaxy0030") sc = Scene() vol = create_volume_source(ds, field=field) tf = vol.transfer_function tf.grey_opacity = True # Plane-parallel lens cam = sc.add_camera(ds, lens_type="plane-parallel") # Set the resolution of the final projection. cam.resolution = [250, 250] # Set the location of the camera to be (x=0.2, y=0.5, z=0.5) # For plane-parallel lens, the location info along the normal_vector (here # is x=0.2) is ignored. cam.position = ds.arr(np.array([0.2, 0.5, 0.5]), "code_length") # Set the orientation of the camera. cam.switch_orientation(normal_vector=normal_vector, north_vector=north_vector) # Set the width of the camera, where width[0] and width[1] specify the length and # height of final projection, while width[2] in plane-parallel lens is not used.
def test_lazy_volume_source_construction(self): sc = Scene() source = create_volume_source(self.ds.all_data(), "density") assert source._volume is None assert source._transfer_function is None source.tfh.bounds = (0.1, 1) source.set_log(False) assert not source.log_field assert source.transfer_function.x_bounds == [0.1, 1] assert source._volume is None source.set_log(True) assert source.log_field assert source.transfer_function.x_bounds == [-1, 0] assert source._volume is None source.transfer_function = None source.tfh.bounds = None ad = self.ds.all_data() np.testing.assert_allclose( source.transfer_function.x_bounds, np.log10(ad.quantities.extrema("density")), ) assert source.tfh.log == source.log_field source.set_field("velocity_x") source.set_log(False) assert source.transfer_function.x_bounds == list( ad.quantities.extrema("velocity_x") ) assert source._volume is None source.set_field("density") assert source.volume is not None assert not source.volume._initialized assert source.volume.fields is None del source.volume assert source._volume is None sc.add_source(source) sc.add_camera() sc.render() assert source.volume is not None assert source.volume._initialized assert source.volume.fields == [("gas", "density")] assert source.volume.log_fields == [True] source.set_field("velocity_x") source.set_log(False) sc.render() assert source.volume is not None assert source.volume._initialized assert source.volume.fields == [("gas", "velocity_x")] assert source.volume.log_fields == [False]
import yt from yt.visualization.volume_rendering.api import Scene, create_volume_source filePath = "Sedov_3d/sedov_hdf5_chk_0003" ds = yt.load(filePath) ds.force_periodicity() sc = Scene() # set up camera cam = sc.add_camera(ds, lens_type="perspective") cam.resolution = [400, 400] cam.position = ds.arr([1, 1, 1], "cm") cam.switch_orientation() # add rendering of density field dens = create_volume_source(ds, field=("flash", "dens")) dens.use_ghost_zones = True sc.add_source(dens) sc.save("density.png", sigma_clip=6) # add rendering of x-velocity field vel = create_volume_source(ds, field=("flash", "velx")) vel.use_ghost_zones = True sc.add_source(vel) sc.save("density_any_velocity.png", sigma_clip=6)
def doit(plotfile): ds = CastroDataset(plotfile) ds._periodicity = (True, True, True) field = ('boxlib', 'enuc') ds._get_field_info(field).take_log = True sc = Scene() # add a volume: select a sphere #center = (0, 0, 0) #R = (5.e8, 'cm') #dd = ds.sphere(center, R) vol = create_volume_source(ds.all_data(), field=field) sc.add_source(vol) # transfer function vals = [16.5, 17.0, 17.5, 18.0, 18.5, 19.0, 19.5] sigma = 0.1 tf = yt.ColorTransferFunction((min(vals), max(vals))) tf.clear() cmap = "viridis" for v in vals: if v < 19.0: alpha = 0.25 else: alpha = 0.75 tf.sample_colormap(v, sigma**2, alpha=alpha, colormap=cmap) sc.get_source(0).transfer_function = tf cam = sc.add_camera(ds, lens_type="perspective") cam.resolution = (1920, 1280) # view 1 cam.position = [ 0.75 * ds.domain_right_edge[0], 0.5 * (ds.domain_left_edge[1] + ds.domain_right_edge[1]), ds.domain_right_edge[2] ] # + 0.25 * (ds.domain_right_edge[2] - ds.domain_left_edge[2])] # look toward the center center = 0.5 * (ds.domain_left_edge + ds.domain_right_edge) # set the center in the vertical direction to be the height of the underlying base layer center[-1] = 2000 * cm normal = (center - cam.position) normal /= np.sqrt(normal.dot(normal)) cam.switch_orientation(normal_vector=normal, north_vector=[0., 0., 1.]) cam.set_width(3 * ds.domain_width) cam.zoom(3.0) sc.camera = cam sc.save_annotated( "{}_Hnuc_annotated_side.png".format(plotfile), text_annotate=[[(0.05, 0.05), "t = {}".format(ds.current_time.d), dict(horizontalalignment="left")], [(0.5, 0.95), "Castro simulation of XRB flame spreading", dict(color="y", fontsize="24", horizontalalignment="center")]]) # view 2 dx = ds.domain_right_edge[0] - ds.domain_left_edge[0] cam.position = [ 0.5 * (ds.domain_left_edge[0] + ds.domain_right_edge[0]) + 0.0001 * dx, 0.5 * (ds.domain_left_edge[1] + ds.domain_right_edge[1]), ds.domain_right_edge[2] ] # + 0.25 * (ds.domain_right_edge[2] - ds.domain_left_edge[2])] # look toward the center center = 0.5 * (ds.domain_left_edge + ds.domain_right_edge) # set the center in the vertical direction to be the height of the underlying base layer center[-1] = 2000 * cm normal = (center - cam.position) normal /= np.sqrt(normal.dot(normal)) cam.switch_orientation(normal_vector=normal, north_vector=[0., 0., 1.]) cam.set_width(3 * ds.domain_width) cam.zoom(0.6) sc.camera = cam sc.save_annotated( "{}_Hnuc_annotated_top.png".format(plotfile), text_annotate=[[(0.05, 0.05), "t = {}".format(ds.current_time.d), dict(horizontalalignment="left")], [(0.5, 0.95), "Castro simulation of XRB flame spreading", dict(color="y", fontsize="24", horizontalalignment="center")]])
import yt from yt.visualization.volume_rendering.api import Scene, create_volume_source filePath = "Sedov_3d/sedov_hdf5_chk_0003" ds = yt.load(filePath) ds.periodicity = (True, True, True) sc = Scene() # set up camera cam = sc.add_camera(ds, lens_type="perspective") cam.resolution = [400, 400] cam.position = ds.arr([1, 1, 1], "cm") cam.switch_orientation() # add rendering of density field dens = create_volume_source(ds, field="dens") dens.use_ghost_zones = True sc.add_source(dens) sc.save("density.png", sigma_clip=6) # add rendering of x-velocity field vel = create_volume_source(ds, field="velx") vel.use_ghost_zones = True sc.add_source(vel) sc.save("density_any_velocity.png", sigma_clip=6)
def test_orientation(): ds = fake_vr_orientation_test_ds() sc = Scene() vol = create_volume_source(ds, field=("gas", "density")) sc.add_source(vol) tf = vol.transfer_function tf = ColorTransferFunction((0.1, 1.0)) tf.sample_colormap(1.0, 0.01, colormap="coolwarm") tf.sample_colormap(0.8, 0.01, colormap="coolwarm") tf.sample_colormap(0.6, 0.01, colormap="coolwarm") tf.sample_colormap(0.3, 0.01, colormap="coolwarm") n_frames = 1 orientations = [[-0.3, -0.1, 0.8]] theta = np.pi / n_frames decimals = 12 test_name = "vr_orientation" for lens_type in [ "perspective", # final name VRImageComparison_UniformGridData_vr_pitch_plane-parallel_0002 # deactivated because of a random failure since numpy 0.20.0 and 0.20.1 # "plane-parallel" ]: frame = 0 cam = sc.add_camera(ds, lens_type=lens_type) cam.resolution = (1000, 1000) cam.position = ds.arr(np.array([-4.0, 0.0, 0.0]), "code_length") cam.switch_orientation(normal_vector=[1.0, 0.0, 0.0], north_vector=[0.0, 0.0, 1.0]) cam.set_width(ds.domain_width * 2.0) desc = "%s_%04d" % (lens_type, frame) test1 = VRImageComparisonTest(sc, ds, desc, decimals) test1.answer_name = test_name yield test1 for _ in range(n_frames): frame += 1 center = ds.arr([0, 0, 0], "code_length") cam.yaw(theta, rot_center=center) desc = "yaw_%s_%04d" % (lens_type, frame) test2 = VRImageComparisonTest(sc, ds, desc, decimals) test2.answer_name = test_name yield test2 for _ in range(n_frames): frame += 1 theta = np.pi / n_frames center = ds.arr([0, 0, 0], "code_length") cam.pitch(theta, rot_center=center) desc = "pitch_%s_%04d" % (lens_type, frame) test3 = VRImageComparisonTest(sc, ds, desc, decimals) test3.answer_name = test_name yield test3 for _ in range(n_frames): frame += 1 theta = np.pi / n_frames center = ds.arr([0, 0, 0], "code_length") cam.roll(theta, rot_center=center) desc = "roll_%s_%04d" % (lens_type, frame) test4 = VRImageComparisonTest(sc, ds, desc, decimals) test4.answer_name = test_name yield test4 center = [0.5, 0.5, 0.5] width = [1.0, 1.0, 1.0] for i, orientation in enumerate(orientations): image = off_axis_projection(ds, center, orientation, width, 512, "density", no_ghost=False) def offaxis_image_func(filename_prefix): return image.write_image(filename_prefix) test5 = GenericImageTest(ds, offaxis_image_func, decimals) test5.prefix = f"oap_orientation_{i}" test5.answer_name = test_name yield test5