1import numpy as np
2
3import yt
4from yt.visualization.volume_rendering.api import Scene, create_volume_source
5
6field = ("gas", "density")
7
8# normal_vector points from camera to the center of tbe final projection.
9# Now we look at the positive x direction.
10normal_vector = [1.0, 0.0, 0.0]
11# north_vector defines the "top" direction of the projection, which is
12# positive z direction here.
13north_vector = [0.0, 0.0, 1.0]
14
15# Follow the simple_volume_rendering cookbook for the first part of this.
16ds = yt.load("IsolatedGalaxy/galaxy0030/galaxy0030")
17sc = Scene()
18vol = create_volume_source(ds, field=field)
19tf = vol.transfer_function
20tf.grey_opacity = True
21
22# Plane-parallel lens
23cam = sc.add_camera(ds, lens_type="plane-parallel")
24# Set the resolution of tbe final projection.
25cam.resolution = [250, 250]
26# Set the location of the camera to be (x=0.2, y=0.5, z=0.5)
27# For plane-parallel lens, the location info along the normal_vector (here
28# is x=0.2) is ignored.
29cam.position = ds.arr(np.array([0.2, 0.5, 0.5]), "code_length")
30# Set the orientation of the camera.
31cam.switch_orientation(normal_vector=normal_vector, north_vector=north_vector)
32# Set the width of the camera, where width[0] and width[1] specify the length and
33# height of final projection, while width[2] in plane-parallel lens is not used.
34cam.set_width(ds.domain_width * 0.5)
35sc.add_source(vol)
36sc.save("lens_plane-parallel.png", sigma_clip=6.0)
37
38# Perspective lens
39cam = sc.add_camera(ds, lens_type="perspective")
40cam.resolution = [250, 250]
41# Standing at (x=0.2, y=0.5, z=0.5), we look at the area of x>0.2 (with some open angle
42# specified by camera width) along the positive x direction.
43cam.position = ds.arr([0.2, 0.5, 0.5], "code_length")
44cam.switch_orientation(normal_vector=normal_vector, north_vector=north_vector)
45# Set the width of the camera, where width[0] and width[1] specify the length and
46# height of the final projection, while width[2] specifies the distance between the
47# camera and the final image.
48cam.set_width(ds.domain_width * 0.5)
49sc.add_source(vol)
50sc.save("lens_perspective.png", sigma_clip=6.0)
51
52# Stereo-perspective lens
53cam = sc.add_camera(ds, lens_type="stereo-perspective")
54# Set the size ratio of the final projection to be 2:1, since stereo-perspective lens
55# will generate the final image with both left-eye and right-eye ones jointed together.
56cam.resolution = [500, 250]
57cam.position = ds.arr([0.2, 0.5, 0.5], "code_length")
58cam.switch_orientation(normal_vector=normal_vector, north_vector=north_vector)
59cam.set_width(ds.domain_width * 0.5)
60# Set the distance between left-eye and right-eye.
61cam.lens.disparity = ds.domain_width[0] * 1.0e-3
62sc.add_source(vol)
63sc.save("lens_stereo-perspective.png", sigma_clip=6.0)
64
65# Fisheye lens
66dd = ds.sphere(ds.domain_center, ds.domain_width[0] / 10)
67cam = sc.add_camera(dd, lens_type="fisheye")
68cam.resolution = [250, 250]
69v, c = ds.find_max(field)
70cam.set_position(c - 0.0005 * ds.domain_width)
71cam.switch_orientation(normal_vector=normal_vector, north_vector=north_vector)
72cam.set_width(ds.domain_width)
73cam.lens.fov = 360.0
74sc.add_source(vol)
75sc.save("lens_fisheye.png", sigma_clip=6.0)
76
77# Spherical lens
78cam = sc.add_camera(ds, lens_type="spherical")
79# Set the size ratio of the final projection to be 2:1, since spherical lens
80# will generate the final image with length of 2*pi and height of pi.
81# Recommended resolution for YouTube 360-degree videos is [3840, 2160]
82cam.resolution = [500, 250]
83# Standing at (x=0.4, y=0.5, z=0.5), we look in all the radial directions
84# from this point in spherical coordinate.
85cam.position = ds.arr([0.4, 0.5, 0.5], "code_length")
86cam.switch_orientation(normal_vector=normal_vector, north_vector=north_vector)
87# In (stereo)spherical camera, camera width is not used since the entire volume
88# will be rendered
89sc.add_source(vol)
90sc.save("lens_spherical.png", sigma_clip=6.0)
91
92# Stereo-spherical lens
93cam = sc.add_camera(ds, lens_type="stereo-spherical")
94# Set the size ratio of the final projection to be 1:1, since spherical-perspective lens
95# will generate the final image with both left-eye and right-eye ones jointed together,
96# with left-eye image on top and right-eye image on bottom.
97# Recommended resolution for YouTube virtual reality videos is [3840, 2160]
98cam.resolution = [500, 500]
99cam.position = ds.arr([0.4, 0.5, 0.5], "code_length")
100cam.switch_orientation(normal_vector=normal_vector, north_vector=north_vector)
101# In (stereo)spherical camera, camera width is not used since the entire volume
102# will be rendered
103# Set the distance between left-eye and right-eye.
104cam.lens.disparity = ds.domain_width[0] * 1.0e-3
105sc.add_source(vol)
106sc.save("lens_stereo-spherical.png", sigma_clip=6.0)
107